Submission #3996554


Source Code Expand

#include<bits/stdc++.h>
#define X first
#define Y second
#define pb emplace_back
#define FOR(i,a,b) for(int (i)=(a);i<(b);++(i))
#define EFOR(i,a,b) for(int (i)=(a);i<=(b);++(i))
#define rep(X,Y) for (int (X) = 0;(X) < (Y);++(X))
#define reps(X,S,Y) for (int (X) = S;(X) < (Y);++(X))
#define rrep(X,Y) for (int (X) = (Y)-1;(X) >=0;--(X))
#define rreps(X,S,Y) for (int (X) = (Y)-1;(X) >= (S);--(X))
#define all(X) (X).begin(),(X).end()
#define rall(X) (X).rbegin(),(X).rend()
#define eb emplace_back
#define UNIQUE(X) (X).erase(unique(all(X)),(X).end())

using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef ll LL;
typedef pii PII;
typedef pll PLL;
template<class T> using vv=vector<vector<T>>;
template<class T> inline bool MX(T &l,const T &r){return l<r?l=r,1:0;}
template<class T> inline bool MN(T &l,const T &r){return l>r?l=r,1:0;}
const ll MOD=1e9+7;
const LL INF = 0x3fffffffff;

int N;
int Q;
int bs[214514];
int dp[214514];

struct LazySeg {
  int size;
  vector<char> has_lazy;
  vector<LL> seg;
  vector<LL> lazy;
  vector<LL> width;
  vector<LL> assi;

  LazySeg(vector<LL> A) {
    int n = A.size();
    size = 1;
    while (size < n) size *= 2;

    seg.resize(size);
    seg.insert(seg.end(), A.begin(), A.end());
    seg.resize(size*2);
    lazy.resize(size*2);
    has_lazy.resize(size*2);
    width.resize(size*2);
    assi.resize(size*2);
    rrep(i, size) {
      seg[i] = min(seg[i*2], seg[i*2+1]);
      width[i] = width[i*2] + width[i*2+1]; 
    }
  }

  void LazyPropagate(int k) {
    if (has_lazy[k]) {
      seg[k] = assi[k];
      if (k < size) {
        has_lazy[k*2] = true;
        has_lazy[k*2+1] = true;
        assi[k*2] = assi[k]; 
        assi[k*2+1] = assi[k];
        lazy[k*2] = 0;
        lazy[k*2+1] = 0;
      }
      has_lazy[k] = false;
    }

    seg[k] += lazy[k];
    if (k < size) {
      lazy[k*2] += lazy[k];
      lazy[k*2+1] += lazy[k];
    }
    lazy[k] = 0;
  }

  void Add(int wishl, int wishr, int k, int watchl, int watchr, LL x) {
    if (wishr <= watchl || watchr <= wishl) {
      LazyPropagate(k);
      return;
    }

    if (wishl <= watchl && watchr <= wishr) {
      lazy[k] += x;
      LazyPropagate(k);
      return;
    }

    LazyPropagate(k);
    int mid = (watchl+watchr)/2;
    Add(wishl, wishr, k*2, watchl, mid, x);
    Add(wishl, wishr, k*2+1, mid, watchr, x);
    seg[k] = min(seg[k*2], seg[k*2+1]);
  }

  void Move(int wishl, int wishr, int k, int watchl, int watchr, LL x) {
    if (wishr <= watchl || watchr <= wishl) {
      LazyPropagate(k);
      return;
    }

    if (wishl <= watchl && watchr <= wishr) {
      has_lazy[k] = true;
      lazy[k] = 0;
      assi[k] = x;
      LazyPropagate(k);
      return;
    }

    LazyPropagate(k);
    int mid = (watchl+watchr)/2;
    Move(wishl, wishr, k*2, watchl, mid, x);
    Move(wishl, wishr, k*2+1, mid, watchr, x);
    seg[k] = min(seg[k*2], seg[k*2+1]);
  }

  void Add(int wishl, int wishr, LL x) {
    return Add(wishl, wishr, 1, 0, size, x);
  }

  void Move(int wishl, int wishr, LL x) {
    return Move(wishl, wishr, 1, 0, size, x);
  }

  LL Get(int wishl, int wishr, int k, int watchl, int watchr) {
    LL lval, rval;

    if (wishr <= watchl || watchr <= wishl) return INF;

    if (wishl <= watchl && watchr <= wishr) {
      LazyPropagate(k);
      return seg[k];
    }

    LazyPropagate(k);
    int mid = (watchl+watchr)/2;
    lval = Get(wishl, wishr, k*2, watchl, mid);
    rval = Get(wishl, wishr, k*2+1, mid, watchr);
    return min(lval, rval);
  }

  LL Get(int wishl, int wishr) {
    return Get(wishl, wishr, 1, 0, size);
  }
};

int main() {
  scanf("%d", &N);
  reps(i, 1, N+1) {
    scanf("%d", &bs[i]);
  }

  scanf("%d", &Q);

  vector<pii> segs;
  rep(i, Q) {
    int l, r;
    scanf("%d%d", &l, &r);
    ++r;
    segs.eb(pii(l, r));
  }
  sort(all(segs), greater<pii>());

  // dp[i][j] := we've processed [1, i).  should fill with 1s [*, j)
  LazySeg seg(vector<LL>(N+10, INF/2));
  seg.Move(0, 1, 0);
  reps(i, 1, N+1) {
    // dp[i+1][k] <- dp[i][*]
    //cout << "i: " << i << endl;
    while (1) {
      auto s = segs.back();
      if (s.X != i) break;
      segs.pop_back();

      //cout << "process " << s.X << ", " << s.Y << endl;
      int r = s.Y;
      LL v = seg.Get(0, r+1);
      seg.Move(r, r+1, v);
    }

    /*cout << "seg: ";
    rep(j, N+2) {
      auto v = seg.Get(j, j+1);
      if (v < INF/2) cout << v << " ";
      else cout << "INF" << " ";
    }cout << endl;*/

    LL x = seg.Get(0, 1);
    LL y = seg.Get(i+1, i+2);
    seg.Move(0, 1, min(x+bs[i], y+!bs[i]));
    if (!bs[i]) {
      seg.Add(i+2, N+3, 1);
    }
    seg.Move(i+1, i+2, INF/2);

    /*cout << "seg: ";
    rep(j, N+2) {
      auto v = seg.Get(j, j+1);
      if (v < INF/2) cout << v << " ";
      else cout << "INF" << " ";
    }cout << endl;*/
  }
  assert(segs.empty());
  printf("%lld\n", seg.Get(0, N+3));
}

Submission Info

Submission Time
Task F - NRE
User potetisensei
Language C++14 (GCC 5.4.1)
Score 0
Code Size 5127 Byte
Status RE
Exec Time 486 ms
Memory 21584 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:153:18: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &N);
                  ^
./Main.cpp:155:24: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &bs[i]);
                        ^
./Main.cpp:158:18: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &Q);
                  ^
./Main.cpp:163:26: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d", &l, &r);
                          ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 1000
Status
AC × 7
AC × 117
RE × 10
Set Name Test Cases
Sample example_0, example_1, example_2, example_3, example_4, example_5, example_6
All cent_0, cent_1, cent_2, cent_3, cent_4, cent_5, cent_6, cent_7, cent_8, cent_9, example_0, example_1, example_2, example_3, example_4, example_5, example_6, full_0, full_1, full_10, full_11, full_12, full_13, full_14, full_15, full_16, full_17, full_18, full_19, full_2, full_3, full_4, full_5, full_6, full_7, full_8, full_9, maxrand_0, maxrand_1, maxrand_10, maxrand_11, maxrand_12, maxrand_13, maxrand_14, maxrand_15, maxrand_16, maxrand_17, maxrand_18, maxrand_19, maxrand_2, maxrand_20, maxrand_21, maxrand_22, maxrand_23, maxrand_24, maxrand_25, maxrand_26, maxrand_27, maxrand_28, maxrand_29, maxrand_3, maxrand_4, maxrand_5, maxrand_6, maxrand_7, maxrand_8, maxrand_9, rand_0, rand_1, rand_10, rand_11, rand_12, rand_13, rand_14, rand_15, rand_16, rand_17, rand_18, rand_19, rand_2, rand_3, rand_4, rand_5, rand_6, rand_7, rand_8, rand_9, small_0, small_1, small_2, small_3, small_4, small_5, small_6, small_7, small_8, small_9, smallwidth_0, smallwidth_1, smallwidth_10, smallwidth_11, smallwidth_12, smallwidth_13, smallwidth_14, smallwidth_15, smallwidth_16, smallwidth_17, smallwidth_18, smallwidth_19, smallwidth_2, smallwidth_20, smallwidth_21, smallwidth_22, smallwidth_23, smallwidth_24, smallwidth_25, smallwidth_26, smallwidth_27, smallwidth_28, smallwidth_29, smallwidth_3, smallwidth_4, smallwidth_5, smallwidth_6, smallwidth_7, smallwidth_8, smallwidth_9
Case Name Status Exec Time Memory
cent_0 AC 444 ms 21100 KB
cent_1 AC 446 ms 21100 KB
cent_2 AC 436 ms 21100 KB
cent_3 AC 441 ms 21100 KB
cent_4 AC 442 ms 21100 KB
cent_5 AC 443 ms 21100 KB
cent_6 AC 448 ms 21100 KB
cent_7 AC 443 ms 21100 KB
cent_8 AC 478 ms 21100 KB
cent_9 AC 427 ms 21100 KB
example_0 AC 1 ms 256 KB
example_1 AC 1 ms 256 KB
example_2 AC 1 ms 256 KB
example_3 AC 1 ms 256 KB
example_4 AC 1 ms 256 KB
example_5 AC 1 ms 256 KB
example_6 AC 1 ms 256 KB
full_0 RE 367 ms 19580 KB
full_1 AC 466 ms 21100 KB
full_10 RE 372 ms 19580 KB
full_11 AC 457 ms 21100 KB
full_12 RE 353 ms 19580 KB
full_13 AC 464 ms 21100 KB
full_14 RE 360 ms 19580 KB
full_15 AC 438 ms 21100 KB
full_16 RE 363 ms 19580 KB
full_17 AC 443 ms 21100 KB
full_18 RE 375 ms 19580 KB
full_19 AC 446 ms 21100 KB
full_2 RE 367 ms 20064 KB
full_3 AC 467 ms 21100 KB
full_4 RE 371 ms 19580 KB
full_5 AC 449 ms 21100 KB
full_6 RE 357 ms 19580 KB
full_7 AC 446 ms 21100 KB
full_8 RE 369 ms 19580 KB
full_9 AC 457 ms 21100 KB
maxrand_0 AC 273 ms 19580 KB
maxrand_1 AC 474 ms 21100 KB
maxrand_10 AC 274 ms 20064 KB
maxrand_11 AC 444 ms 21100 KB
maxrand_12 AC 274 ms 19580 KB
maxrand_13 AC 447 ms 21100 KB
maxrand_14 AC 272 ms 19452 KB
maxrand_15 AC 462 ms 21100 KB
maxrand_16 AC 263 ms 19452 KB
maxrand_17 AC 462 ms 21100 KB
maxrand_18 AC 277 ms 19580 KB
maxrand_19 AC 460 ms 21100 KB
maxrand_2 AC 273 ms 19452 KB
maxrand_20 AC 287 ms 19580 KB
maxrand_21 AC 459 ms 21100 KB
maxrand_22 AC 281 ms 19580 KB
maxrand_23 AC 470 ms 21100 KB
maxrand_24 AC 289 ms 19580 KB
maxrand_25 AC 463 ms 21100 KB
maxrand_26 AC 274 ms 19580 KB
maxrand_27 AC 457 ms 21100 KB
maxrand_28 AC 277 ms 19452 KB
maxrand_29 AC 444 ms 21100 KB
maxrand_3 AC 463 ms 21100 KB
maxrand_4 AC 279 ms 19580 KB
maxrand_5 AC 486 ms 21100 KB
maxrand_6 AC 269 ms 19452 KB
maxrand_7 AC 451 ms 21100 KB
maxrand_8 AC 266 ms 19452 KB
maxrand_9 AC 456 ms 21100 KB
rand_0 AC 460 ms 21100 KB
rand_1 AC 165 ms 6512 KB
rand_10 AC 470 ms 21100 KB
rand_11 AC 205 ms 10612 KB
rand_12 AC 453 ms 21100 KB
rand_13 AC 328 ms 19948 KB
rand_14 AC 465 ms 21100 KB
rand_15 AC 132 ms 6132 KB
rand_16 AC 452 ms 21584 KB
rand_17 AC 209 ms 10736 KB
rand_18 AC 458 ms 21100 KB
rand_19 AC 410 ms 20588 KB
rand_2 AC 459 ms 21100 KB
rand_3 AC 200 ms 6896 KB
rand_4 AC 452 ms 21100 KB
rand_5 AC 100 ms 2420 KB
rand_6 AC 460 ms 21100 KB
rand_7 AC 225 ms 11504 KB
rand_8 AC 461 ms 21100 KB
rand_9 AC 227 ms 19064 KB
small_0 AC 1 ms 256 KB
small_1 AC 1 ms 256 KB
small_2 AC 1 ms 256 KB
small_3 AC 1 ms 256 KB
small_4 AC 1 ms 256 KB
small_5 AC 1 ms 256 KB
small_6 AC 1 ms 256 KB
small_7 AC 1 ms 256 KB
small_8 AC 1 ms 256 KB
small_9 AC 1 ms 256 KB
smallwidth_0 AC 275 ms 19580 KB
smallwidth_1 AC 453 ms 21100 KB
smallwidth_10 AC 288 ms 19452 KB
smallwidth_11 AC 413 ms 21100 KB
smallwidth_12 AC 273 ms 19580 KB
smallwidth_13 AC 442 ms 21100 KB
smallwidth_14 AC 267 ms 19452 KB
smallwidth_15 AC 417 ms 21100 KB
smallwidth_16 AC 264 ms 19452 KB
smallwidth_17 AC 431 ms 21100 KB
smallwidth_18 AC 271 ms 19580 KB
smallwidth_19 AC 410 ms 21100 KB
smallwidth_2 AC 268 ms 19580 KB
smallwidth_20 AC 278 ms 19580 KB
smallwidth_21 AC 439 ms 21100 KB
smallwidth_22 AC 265 ms 19580 KB
smallwidth_23 AC 415 ms 21100 KB
smallwidth_24 AC 258 ms 19580 KB
smallwidth_25 AC 437 ms 21100 KB
smallwidth_26 AC 268 ms 19452 KB
smallwidth_27 AC 405 ms 21100 KB
smallwidth_28 AC 278 ms 19580 KB
smallwidth_29 AC 436 ms 21100 KB
smallwidth_3 AC 409 ms 21100 KB
smallwidth_4 AC 276 ms 19452 KB
smallwidth_5 AC 435 ms 21100 KB
smallwidth_6 AC 266 ms 19452 KB
smallwidth_7 AC 410 ms 21100 KB
smallwidth_8 AC 274 ms 19580 KB
smallwidth_9 AC 436 ms 21100 KB