Submission #4571384


Source Code Expand

#include<iostream>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include<functional>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cassert>
#include<ctime>
using namespace std;

#define mind(a,b) (a>b?b:a)
#define maxd(a,b) (a>b?a:b)
#define absd(x) (x<0?-(x):x)
#define pow2(x) ((x)*(x))
#define rep(i,n) for(int i=0; i<n; ++i)
#define repr(i,n) for(int i=n-1; i>=0; --i)
#define repl(i,s,n) for(int i=s; i<=n; ++i)
#define replr(i,s,n) for(int i=n; i>=s; --i)
#define repf(i,s,n,j) for(int i=s; i<=n; i+=j)
#define repe(e,obj) for(auto e : obj)

#define SP << " " <<
#define COL << " : " <<
#define COM << ", " <<
#define ARR << " -> " <<
#define PNT(STR) cout << STR << endl
#define POS(X,Y) "(" << X << ", " << Y << ")"
#define DEB(A) " (" << #A << ") " << A
#define DEBREP(i,n,val) for(int i=0; i<n; ++i) cout << val << " "; cout << endl
#define ALL(V) (V).begin(), (V).end()
#define INF 1000000007
#define INFLL 1000000000000000007LL
#define EPS 1e-9

typedef unsigned int uint;
typedef unsigned long ulong;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
#define P_TYPE int
typedef pair<P_TYPE, P_TYPE> P;
typedef pair<P, P_TYPE> PI;
typedef pair<P_TYPE, P> IP;
typedef pair<P, P> PP;
typedef priority_queue<P, vector<P>, greater<P> > pvqueue;

class LiChaoTree {
  struct Line {
    ll a, b;
    ll f(ll x) const { return a*x + b; }
    Line(ll a, ll b) : a(a), b(b) {}
  };
  struct Node {
    Node *left, *right;
    Line line;

    Node(Line line) : left(nullptr), right(nullptr), line(line) {}
  };
  const ll inf = 1e16L;
  const Line inf_line = Line{0, inf};

  Node* root;
  ll xl, xr;

  Node* _add_line(Node *nd, Line line, ll l, ll r) {
    if(l == r) return nullptr;

    ll m = (l + r) >> 1;
    if(nd == nullptr) return new Node(line);

    bool left = (line.f(l) < nd->line.f(l));
    bool mid = (line.f(m) < nd->line.f(m));
    bool right = (line.f(r) < nd->line.f(r));
    if(left && right) {
      nd->line = line;
      return nd;
    }
    if(!left && !right) {
      return nd;
    }
    if(mid) {
      swap(nd->line, line);
    }
    if(left != mid) {
      nd->left = _add_line(nd->left, line, l, m);
    } else {
      nd->right = _add_line(nd->right, line, m, r);
    }
    return nd;
  }

  Node* _add_segment_line(ll a, ll b, Node *nd, Line line, ll l, ll r) {
    if(r <= a || b <= l) {
      return nd;
    }
    if(a <= l && r <= b) {
      return _add_line(nd, line, l, r);
    }

    if(nd == nullptr) {
      nd = new Node(inf_line);
    }

    ll m = (l + r) >> 1;
    nd->left = _add_segment_line(a, b, nd->left, line, l, m);
    nd->right = _add_segment_line(a, b, nd->right, line, m, r);
    return nd;
  }

  ll _query(ll k, ll l, ll r) {
    Node *nd = root;
    ll s = inf;
    while(r-l > 0 && nd != nullptr) {
      ll m = (l + r) >> 1;
      s = min(s, nd->line.f(k));
      if(k <= m) {
        r = m;
        nd = nd->left;
      } else {
        l = m;
        nd = nd->right;
      }
    }
    return s;
  }

public:
  // [xl, xr)
  LiChaoTree(ll xl, ll xr) : xl(xl), xr(xr), root(nullptr) {}

  void add_line(ll a, ll b) {
    Line line = Line{a, b};
    root = _add_line(root, line, xl, xr);
  }

  void add_segment_line(ll a, ll b, ll l, ll r) {
    Line line = Line{a, b};
    root = _add_segment_line(l, r, root, line, xl, xr);
  }

  ll query(ll x) {
    return _query(x, xl, xr);
  }
};

#define N 100017

class SegmentTree {
  int n0;
  LiChaoTree **lct;

public:
  SegmentTree(int n) {
    n0 = 1;
    while(n0 < n) n0 <<= 1;
    lct = new LiChaoTree*[n0*2];
    rep(i, 2*n0) lct[i] = new LiChaoTree(0, 1e12+7);
  }

  void update(int l, int r, ll a, ll b) {
    int l0 = l + n0, r0 = r + n0;
    while(l0 < r0) {
      if(r0 & 1) {
        --r0;
        lct[r0-1]->add_line(a, b);
      }
      if(l0 & 1) {
        lct[l0-1]->add_line(a, b);
        ++l0;
      }
      l0 >>= 1; r0 >>= 1;
    }
  }

  ll query(int k, ll x) {
    k += n0-1;
    ll s = lct[k]->query(x);
    while(k > 0) {
      k = (k - 1) / 2;
      s = min(s, lct[k]->query(x));
      ll r = lct[k]->query(x);
    }
    return s;
  }
};

int n;

int main() {
  scanf("%d", &n);
  SegmentTree seg(100010);
  rep(i, n) {
    int o, c; ll d, x;
    scanf("%d %d %lld %lld", &o, &c, &d, &x);
    if(c == -1) c = 100010;
    ll s = min(d*d + seg.query(o-1, d), x);
    seg.update(o, c+1, -2*d, s + d*d);
    printf("%lld\n", s);
  }
  return 0;
}

Submission Info

Submission Time
Task I - Ramen
User yaketake08
Language C++14 (GCC 5.4.1)
Score 300
Code Size 4696 Byte
Status AC
Exec Time 1128 ms
Memory 74240 KiB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:196:18: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &n);
                  ^
./Main.cpp:200:45: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d %lld %lld", &o, &c, &d, &x);
                                             ^

Judge Result

Set Name All
Score / Max Score 300 / 300
Status
AC × 19
Set Name Test Cases
All 0-sample-1, 0-sample-2, 1-random-0, 1-random-1, 1-random-2, 1-random-3, 1-random-4, 2-0, 2-1, 3-0, 3-1, 4-1, 4-2, 5-0, 5-1, 6-0, 6-1, 6-2, 7-1
Case Name Status Exec Time Memory
0-sample-1 AC 15 ms 18688 KiB
0-sample-2 AC 15 ms 18688 KiB
1-random-0 AC 33 ms 20096 KiB
1-random-1 AC 43 ms 20608 KiB
1-random-2 AC 40 ms 20480 KiB
1-random-3 AC 511 ms 48768 KiB
1-random-4 AC 1128 ms 74240 KiB
2-0 AC 686 ms 55680 KiB
2-1 AC 694 ms 56832 KiB
3-0 AC 711 ms 52352 KiB
3-1 AC 719 ms 52480 KiB
4-1 AC 150 ms 30080 KiB
4-2 AC 139 ms 28928 KiB
5-0 AC 650 ms 40832 KiB
5-1 AC 654 ms 40704 KiB
6-0 AC 81 ms 19968 KiB
6-1 AC 92 ms 20480 KiB
6-2 AC 95 ms 20864 KiB
7-1 AC 79 ms 24576 KiB