Submission #261324

# Submission time Handle Problem Language Result Execution time Memory
261324 2020-08-11T16:15:42 Z Nightlight Progression (NOI20_progression) C++14
100 / 100
1752 ms 69368 KB
#include <bits/stdc++.h>
#define L(n) (n << 1)
#define R(n) (n << 1 | 1)
using namespace std;

const long long INF = 2e18;
//segtree nyimpen selisih antara D[i] dan D[i + 1]

struct node {
  int best;
  long long l, r, sum;
  int r_sz, l_sz;
};

int N, Q;
int D[300005];
node tree[1200005];
long long lazy1[1200005];//nambah
long long lazy2[1200005];//ubah

inline void merge(node &now, node le, node ri) {
  now.best = max(le.best, ri.best);
  now.sum = le.sum + ri.sum;
  if(le.l_sz == -1 && ri.l_sz == -1) {
    if(le.r == ri.r) {
      now.r = le.r;
      now.r_sz =  le.r_sz + ri.r_sz;
      now.l_sz = -1;
    }else {
      now.l = le.r;
      now.l_sz = le.r_sz;
      now.r = ri.r;
      now.r_sz = ri.r_sz;
    }
  }else if(le.l_sz == -1) {
    if(le.r == ri.l) {
      now.l = le.r;
      now.l_sz = le.r_sz + ri.l_sz;
    }else {
      now.best = max(now.best, ri.l_sz);
      now.l = le.r;
      now.l_sz = le.r_sz;
    }
    now.r = ri.r;
    now.r_sz = ri.r_sz;
  }else if(ri.l_sz == -1) {
    if(le.r == ri.r) {
      now.r = ri.r;
      now.r_sz = le.r_sz + ri.r_sz;
    }else {
      now.best = max(now.best, le.r_sz);
      now.r = ri.r;
      now.r_sz = ri.r_sz;
    }
    now.l = le.l;
    now.l_sz = le.l_sz;
  }else {
    if(le.r == ri.l) {
      now.best = max(now.best, le.r_sz + ri.l_sz);
    }else {
      now.best = max(now.best, max(le.r_sz, ri.l_sz));
    }

    now.l = le.l;
    now.l_sz = le.l_sz;
    now.r = ri.r;
    now.r_sz = ri.r_sz;
  }
}

void build(int n, int l, int r) {
  lazy2[n] = INF;
  if(l == r) {
    tree[n].best = 1;
    tree[n].l_sz = -1;
    tree[n].r_sz = 1;
    tree[n].r = tree[n].sum = D[l + 1] - D[l];
//    cout << n <<  ": " << tree[n].r << "\n";
    return;
  }
  int mid = (l + r) >> 1;
  build(L(n), l, mid);
  build(R(n), mid + 1, r);
  merge(tree[n], tree[L(n)], tree[R(n)]);/*
  cout << l << " - " << r << "\n";
  cout << tree[n].best << "\n";
  cout << tree[n].l << " -> " << tree[n].l_sz << "\n";
  cout << tree[n].r << " -> " << tree[n].r_sz << "\n\n";*/
}

void unlazy(int n, int l, int r) {
  if(lazy2[n] != INF) {
    tree[n].l_sz = -1;
    tree[n].r_sz = r - l + 1;
    tree[n].r = lazy2[n];
    tree[n].sum = lazy2[n] * (r - l + 1);

    if(l != r) {
      if(lazy1[L(n)]) lazy1[L(n)] = 0;
      if(lazy1[R(n)]) lazy1[R(n)] = 0;
      lazy2[L(n)] = lazy2[n];
      lazy2[R(n)] = lazy2[n];
    }
    lazy2[n] = INF;
  }

  if(lazy1[n]) {
    tree[n].l += lazy1[n];
    tree[n].r += lazy1[n];
    tree[n].sum += lazy1[n] * (r - l + 1);

    if(l != r) {
      lazy1[L(n)] += lazy1[n];
      lazy1[R(n)] += lazy1[n];
    }
    lazy1[n] = 0;
  }
}

void trans(int n, int l, int r) {
  unlazy(n, l, r);
  if(l == r) {
    cout << tree[n].r << " ";
    return;
  }
  int mid = (l + r) >> 1;
  trans(L(n), l, mid);
  trans(R(n), mid + 1, r);
}

void trans() {trans(1, 0, N); cout << "\n";}

//update nambah
void update1(int n, int l, int r, int ql, int qr, int val) {
  unlazy(n, l, r);
  if(qr < l || r < ql) return;
  if(ql <= l && r <= qr) {
    lazy1[n] += val;
    unlazy(n, l, r);
    return;
  }
  int mid = (l + r) >> 1;
  update1(L(n), l, mid, ql, qr, val);
  update1(R(n), mid + 1, r, ql, qr, val);
  merge(tree[n], tree[L(n)], tree[R(n)]);
}

void update1(int ql, int qr, int val) {update1(1, 0, N, ql, qr, val);}

void update2(int n, int l, int r, int ql, int qr, int val) {
  unlazy(n, l, r);
  if(qr < l || r < ql) return;
  if(ql <= l && r <= qr) {
    lazy2[n] = val;
    lazy1[n] = 0;
    unlazy(n, l, r);
    return;
  }
  int mid = (l + r) >> 1;
  update2(L(n), l, mid, ql, qr, val);
  update2(R(n), mid + 1, r, ql, qr, val);
  merge(tree[n], tree[L(n)], tree[R(n)]);
}

void update2(int ql, int qr, int val) {update2(1, 0, N, ql, qr, val);}

node res;
long long res_sum;
long long sumL, sumR;

void sumq(int n, int l, int r, int ql, int qr) {
  unlazy(n, l, r);
  if(ql <= l && r <= qr) {
    res_sum += tree[n].sum;
    return;
  }
  int mid = (l + r) >> 1;
  if(ql <= mid) sumq(L(n), l, mid, ql, qr);
  if(qr > mid) sumq(R(n), mid + 1, r, ql, qr);
}

void sumq(int ql, int qr) {
  res_sum = 0;
  if(ql > qr) return;
  sumq(1, 0, N, ql, qr);
}

void query(int n, int l, int r, int ql, int qr) {
  unlazy(n, l, r);
  if(ql <= l && r <= qr) {
    merge(res, res, tree[n]);
//    cout << tree[n].r << " " << tree[n].r_sz << "\n";
    return;
  }
  int mid = (l + r) >> 1;
  if(ql <= mid) query(L(n), l, mid, ql, qr);
  if(qr > mid) query(R(n), mid + 1, r, ql, qr);
}

void query(int ql, int qr) {
  res.best = 0;
  res.l_sz = -1;
  res.r = INF;
  res.r_sz = 0;
  query(1, 0, N, ql, qr);
  res.best = max(res.best, res.r_sz);
}

int main() {  
  scanf("%d %d", &N, &Q);
  for(int i = 1; i <= N; i++) {
    scanf("%d", &D[i]);
  }
  build(1, 0, N);
//  cout << "\n";
  int ty, l, r, s;
  long long c;
//  trans();
  while(Q--) {
    scanf("%d", &ty);
    if(ty == 1) {
      scanf("%d %d %d %lld", &l, &r, &s, &c);
      update1(l - 1, l - 1, s);
      if(l < r) {
        update1(l, r - 1, c);
      }
      update1(r, r, -((r - l) * c + s));
//      trans();
    }else if(ty == 2) {
      scanf("%d %d %d %lld", &l, &r, &s, &c);
      sumq(0, l - 2);
      sumL = res_sum;
      sumq(0, r);
      sumR = res_sum;
//      cout << s - sumL << " " << sumR << "\n";
      update2(l - 1, l - 1, s - sumL);
//      trans();
      if(l < r) {
        update2(l, r - 1, c);
      }
      update2(r, r, sumR - ((r - l) * c + s));
//      trans();
    }else {
      scanf("%d %d", &l, &r);
      if(r - l > 1) {
        query(l, r - 1);
        printf("%d\n", res.best + 1);
//        cout << res.best << " " << res.r << "\n";
      }else printf("%d\n", r - l + 1);
    }
  }
  cin >> N;
} 

Compilation message

Progression.cpp: In function 'int main()':
Progression.cpp:210:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &N, &Q);
   ~~~~~^~~~~~~~~~~~~~~~~
Progression.cpp:212:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &D[i]);
     ~~~~~^~~~~~~~~~~~~
Progression.cpp:220:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &ty);
     ~~~~~^~~~~~~~~~~
Progression.cpp:222:12: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
       scanf("%d %d %d %lld", &l, &r, &s, &c);
       ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Progression.cpp:230:12: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
       scanf("%d %d %d %lld", &l, &r, &s, &c);
       ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Progression.cpp:244:12: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
       scanf("%d %d", &l, &r);
       ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 743 ms 59476 KB Output is correct
2 Correct 257 ms 3704 KB Output is correct
3 Correct 261 ms 3604 KB Output is correct
4 Correct 264 ms 3556 KB Output is correct
5 Correct 279 ms 3704 KB Output is correct
6 Correct 278 ms 3704 KB Output is correct
7 Correct 261 ms 3704 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 776 ms 59512 KB Output is correct
12 Correct 724 ms 59416 KB Output is correct
13 Correct 752 ms 59676 KB Output is correct
14 Correct 732 ms 59976 KB Output is correct
15 Correct 729 ms 59640 KB Output is correct
16 Correct 760 ms 59384 KB Output is correct
17 Correct 756 ms 59384 KB Output is correct
18 Correct 753 ms 59356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 512 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 3 ms 512 KB Output is correct
9 Correct 3 ms 512 KB Output is correct
10 Correct 3 ms 512 KB Output is correct
11 Correct 2 ms 512 KB Output is correct
12 Correct 3 ms 512 KB Output is correct
13 Correct 2 ms 512 KB Output is correct
14 Correct 2 ms 512 KB Output is correct
15 Correct 3 ms 512 KB Output is correct
16 Correct 3 ms 512 KB Output is correct
17 Correct 3 ms 512 KB Output is correct
18 Correct 3 ms 512 KB Output is correct
19 Correct 2 ms 512 KB Output is correct
20 Correct 2 ms 384 KB Output is correct
21 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 461 ms 57880 KB Output is correct
2 Correct 149 ms 3068 KB Output is correct
3 Correct 175 ms 3064 KB Output is correct
4 Correct 135 ms 3064 KB Output is correct
5 Correct 154 ms 3192 KB Output is correct
6 Correct 160 ms 3152 KB Output is correct
7 Correct 158 ms 3320 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 535 ms 56524 KB Output is correct
12 Correct 448 ms 57908 KB Output is correct
13 Correct 516 ms 56544 KB Output is correct
14 Correct 551 ms 56440 KB Output is correct
15 Correct 443 ms 57908 KB Output is correct
16 Correct 537 ms 58460 KB Output is correct
17 Correct 533 ms 58360 KB Output is correct
18 Correct 485 ms 58488 KB Output is correct
19 Correct 490 ms 57848 KB Output is correct
20 Correct 463 ms 57848 KB Output is correct
21 Correct 464 ms 57848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 906 ms 66680 KB Output is correct
2 Correct 228 ms 3704 KB Output is correct
3 Correct 241 ms 3704 KB Output is correct
4 Correct 236 ms 3832 KB Output is correct
5 Correct 238 ms 3704 KB Output is correct
6 Correct 247 ms 3704 KB Output is correct
7 Correct 241 ms 3704 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 921 ms 63708 KB Output is correct
12 Correct 908 ms 66168 KB Output is correct
13 Correct 900 ms 63716 KB Output is correct
14 Correct 887 ms 63652 KB Output is correct
15 Correct 863 ms 65912 KB Output is correct
16 Correct 940 ms 65788 KB Output is correct
17 Correct 991 ms 66040 KB Output is correct
18 Correct 1026 ms 66136 KB Output is correct
19 Correct 969 ms 65944 KB Output is correct
20 Correct 959 ms 66208 KB Output is correct
21 Correct 969 ms 66244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 461 ms 57880 KB Output is correct
2 Correct 149 ms 3068 KB Output is correct
3 Correct 175 ms 3064 KB Output is correct
4 Correct 135 ms 3064 KB Output is correct
5 Correct 154 ms 3192 KB Output is correct
6 Correct 160 ms 3152 KB Output is correct
7 Correct 158 ms 3320 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 535 ms 56524 KB Output is correct
12 Correct 448 ms 57908 KB Output is correct
13 Correct 516 ms 56544 KB Output is correct
14 Correct 551 ms 56440 KB Output is correct
15 Correct 443 ms 57908 KB Output is correct
16 Correct 537 ms 58460 KB Output is correct
17 Correct 533 ms 58360 KB Output is correct
18 Correct 485 ms 58488 KB Output is correct
19 Correct 490 ms 57848 KB Output is correct
20 Correct 463 ms 57848 KB Output is correct
21 Correct 464 ms 57848 KB Output is correct
22 Correct 1124 ms 68260 KB Output is correct
23 Correct 235 ms 3652 KB Output is correct
24 Correct 240 ms 3576 KB Output is correct
25 Correct 243 ms 3632 KB Output is correct
26 Correct 242 ms 3568 KB Output is correct
27 Correct 251 ms 3704 KB Output is correct
28 Correct 242 ms 3664 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 1 ms 384 KB Output is correct
31 Correct 1 ms 384 KB Output is correct
32 Correct 1237 ms 65708 KB Output is correct
33 Correct 1233 ms 68192 KB Output is correct
34 Correct 1263 ms 65492 KB Output is correct
35 Correct 1320 ms 65500 KB Output is correct
36 Correct 1045 ms 65400 KB Output is correct
37 Correct 1053 ms 65372 KB Output is correct
38 Correct 965 ms 65332 KB Output is correct
39 Correct 1454 ms 68280 KB Output is correct
40 Correct 1389 ms 68500 KB Output is correct
41 Correct 1285 ms 68344 KB Output is correct
42 Correct 1530 ms 68300 KB Output is correct
43 Correct 1172 ms 68192 KB Output is correct
44 Correct 1481 ms 68312 KB Output is correct
45 Correct 1439 ms 68280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 743 ms 59476 KB Output is correct
2 Correct 257 ms 3704 KB Output is correct
3 Correct 261 ms 3604 KB Output is correct
4 Correct 264 ms 3556 KB Output is correct
5 Correct 279 ms 3704 KB Output is correct
6 Correct 278 ms 3704 KB Output is correct
7 Correct 261 ms 3704 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 776 ms 59512 KB Output is correct
12 Correct 724 ms 59416 KB Output is correct
13 Correct 752 ms 59676 KB Output is correct
14 Correct 732 ms 59976 KB Output is correct
15 Correct 729 ms 59640 KB Output is correct
16 Correct 760 ms 59384 KB Output is correct
17 Correct 756 ms 59384 KB Output is correct
18 Correct 753 ms 59356 KB Output is correct
19 Correct 3 ms 512 KB Output is correct
20 Correct 2 ms 384 KB Output is correct
21 Correct 2 ms 384 KB Output is correct
22 Correct 2 ms 384 KB Output is correct
23 Correct 2 ms 384 KB Output is correct
24 Correct 2 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 3 ms 512 KB Output is correct
27 Correct 3 ms 512 KB Output is correct
28 Correct 3 ms 512 KB Output is correct
29 Correct 2 ms 512 KB Output is correct
30 Correct 3 ms 512 KB Output is correct
31 Correct 2 ms 512 KB Output is correct
32 Correct 2 ms 512 KB Output is correct
33 Correct 3 ms 512 KB Output is correct
34 Correct 3 ms 512 KB Output is correct
35 Correct 3 ms 512 KB Output is correct
36 Correct 3 ms 512 KB Output is correct
37 Correct 2 ms 512 KB Output is correct
38 Correct 2 ms 384 KB Output is correct
39 Correct 2 ms 384 KB Output is correct
40 Correct 461 ms 57880 KB Output is correct
41 Correct 149 ms 3068 KB Output is correct
42 Correct 175 ms 3064 KB Output is correct
43 Correct 135 ms 3064 KB Output is correct
44 Correct 154 ms 3192 KB Output is correct
45 Correct 160 ms 3152 KB Output is correct
46 Correct 158 ms 3320 KB Output is correct
47 Correct 1 ms 384 KB Output is correct
48 Correct 1 ms 384 KB Output is correct
49 Correct 1 ms 384 KB Output is correct
50 Correct 535 ms 56524 KB Output is correct
51 Correct 448 ms 57908 KB Output is correct
52 Correct 516 ms 56544 KB Output is correct
53 Correct 551 ms 56440 KB Output is correct
54 Correct 443 ms 57908 KB Output is correct
55 Correct 537 ms 58460 KB Output is correct
56 Correct 533 ms 58360 KB Output is correct
57 Correct 485 ms 58488 KB Output is correct
58 Correct 490 ms 57848 KB Output is correct
59 Correct 463 ms 57848 KB Output is correct
60 Correct 464 ms 57848 KB Output is correct
61 Correct 906 ms 66680 KB Output is correct
62 Correct 228 ms 3704 KB Output is correct
63 Correct 241 ms 3704 KB Output is correct
64 Correct 236 ms 3832 KB Output is correct
65 Correct 238 ms 3704 KB Output is correct
66 Correct 247 ms 3704 KB Output is correct
67 Correct 241 ms 3704 KB Output is correct
68 Correct 1 ms 384 KB Output is correct
69 Correct 1 ms 384 KB Output is correct
70 Correct 1 ms 384 KB Output is correct
71 Correct 921 ms 63708 KB Output is correct
72 Correct 908 ms 66168 KB Output is correct
73 Correct 900 ms 63716 KB Output is correct
74 Correct 887 ms 63652 KB Output is correct
75 Correct 863 ms 65912 KB Output is correct
76 Correct 940 ms 65788 KB Output is correct
77 Correct 991 ms 66040 KB Output is correct
78 Correct 1026 ms 66136 KB Output is correct
79 Correct 969 ms 65944 KB Output is correct
80 Correct 959 ms 66208 KB Output is correct
81 Correct 969 ms 66244 KB Output is correct
82 Correct 1124 ms 68260 KB Output is correct
83 Correct 235 ms 3652 KB Output is correct
84 Correct 240 ms 3576 KB Output is correct
85 Correct 243 ms 3632 KB Output is correct
86 Correct 242 ms 3568 KB Output is correct
87 Correct 251 ms 3704 KB Output is correct
88 Correct 242 ms 3664 KB Output is correct
89 Correct 1 ms 384 KB Output is correct
90 Correct 1 ms 384 KB Output is correct
91 Correct 1 ms 384 KB Output is correct
92 Correct 1237 ms 65708 KB Output is correct
93 Correct 1233 ms 68192 KB Output is correct
94 Correct 1263 ms 65492 KB Output is correct
95 Correct 1320 ms 65500 KB Output is correct
96 Correct 1045 ms 65400 KB Output is correct
97 Correct 1053 ms 65372 KB Output is correct
98 Correct 965 ms 65332 KB Output is correct
99 Correct 1454 ms 68280 KB Output is correct
100 Correct 1389 ms 68500 KB Output is correct
101 Correct 1285 ms 68344 KB Output is correct
102 Correct 1530 ms 68300 KB Output is correct
103 Correct 1172 ms 68192 KB Output is correct
104 Correct 1481 ms 68312 KB Output is correct
105 Correct 1439 ms 68280 KB Output is correct
106 Correct 1469 ms 69144 KB Output is correct
107 Correct 296 ms 3708 KB Output is correct
108 Correct 311 ms 3832 KB Output is correct
109 Correct 307 ms 3704 KB Output is correct
110 Correct 1 ms 384 KB Output is correct
111 Correct 1 ms 384 KB Output is correct
112 Correct 1 ms 384 KB Output is correct
113 Correct 1285 ms 68320 KB Output is correct
114 Correct 1447 ms 68396 KB Output is correct
115 Correct 1429 ms 68316 KB Output is correct
116 Correct 1238 ms 68192 KB Output is correct
117 Correct 1548 ms 69292 KB Output is correct
118 Correct 1119 ms 68192 KB Output is correct
119 Correct 1207 ms 68200 KB Output is correct
120 Correct 511 ms 58744 KB Output is correct
121 Correct 522 ms 58604 KB Output is correct
122 Correct 598 ms 58712 KB Output is correct
123 Correct 502 ms 57844 KB Output is correct
124 Correct 473 ms 57768 KB Output is correct
125 Correct 489 ms 57752 KB Output is correct
126 Correct 1456 ms 65956 KB Output is correct
127 Correct 1457 ms 65912 KB Output is correct
128 Correct 1650 ms 69240 KB Output is correct
129 Correct 1752 ms 65912 KB Output is correct
130 Correct 1208 ms 65996 KB Output is correct
131 Correct 1270 ms 66008 KB Output is correct
132 Correct 1272 ms 65912 KB Output is correct
133 Correct 1617 ms 69368 KB Output is correct
134 Correct 1631 ms 69256 KB Output is correct
135 Correct 1691 ms 69248 KB Output is correct
136 Correct 319 ms 3764 KB Output is correct
137 Correct 354 ms 3792 KB Output is correct
138 Correct 321 ms 3728 KB Output is correct