Submission #270672

# Submission time Handle Problem Language Result Execution time Memory
270672 2020-08-17T21:48:21 Z Haunted_Cpp Progression (NOI20_progression) C++17
100 / 100
1780 ms 89584 KB
/**
 *  author: Haunted_Cpp
**/
 
#include <bits/stdc++.h>
using namespace std;
 
#pragma GCC optimize("Ofast")
#pragma GCC target("fma,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC optimize("unroll-loops")
 
template<typename T> ostream &operator << (ostream &os, const vector<T> &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << '}'; }
template<typename T, size_t size> ostream &operator << (ostream &os, const array<T, size> &arr) { os << '{'; string sep; for (const auto &x : arr) os << sep << x, sep = ", "; return os << '}'; }
template<typename A, typename B> ostream &operator << (ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
 
void debug_out() { cerr << endl; }
template<typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << ' ' << H; debug_out(T...); }
 
#ifdef LOCAL
#define debug(...) cerr << "(" << #__VA_ARGS__ << "):", debug_out(__VA_ARGS__)
#else
#define debug(...) 47
#endif

const long long INVALID = -1e18;

class SegmentTree {
private:
  struct Node {
    int pre, suf, best;
    long long lazy;
    long long lazy_set;
    long long delta_pre, delta_suf;
    long long sum;
    int t;
    Node() {
      pre = suf = best = 0;
      delta_pre = delta_suf = 0;
      t = 0;
      sum = 0;
      lazy_set = INVALID;
      lazy = INVALID;
    }
    void merge(Node l, Node r) {
      t = l.t + r.t;
      pre = l.pre;
      delta_pre = l.delta_pre;
      suf = r.suf;
      delta_suf = r.delta_suf;
      sum = l.sum + r.sum;
      if (l.pre == l.t && l.delta_pre == r.delta_pre) {
        pre = max(pre, l.pre + r.pre);
      }
      if (r.suf == r.t && l.delta_suf == r.delta_suf) {
        suf = max(suf, l.suf + r.suf);
      }
      best = max(l.best, r.best);
      if (l.delta_suf == r.delta_pre) {
        best = max(best, l.suf + r.pre);
      }
    }
  };
  const int LO, HI;
  vector<Node> seg;
  void build(int l, int r, int node, const vector<long long> &arr) {
    if (l == r) {
      seg[node].pre = seg[node].suf = seg[node].best = 1;
      seg[node].delta_pre = arr[l];
      seg[node].delta_suf = arr[l];
      seg[node].t = 1;
      seg[node].sum = arr[l];
      return;
    }
    const int mid = l + (r - l) / 2;
    build(l, mid, 2 * node + 1, arr);
    build(mid + 1, r, 2 * node + 2, arr);
    seg[node].merge(seg[2 * node + 1], seg[2 * node + 2]);
  }
  Node range_query(int ql, int qr, int l, int r, int node) {
    push(l, r, node);
    if (l > qr || r < ql) {
     Node empty;
     empty.t = -1;
     return empty; 
    }
    if (l >= ql && r <= qr) {
      return seg[node];
    }
    const int mid = l + (r - l) / 2;
    Node esq = range_query(ql, qr, l, mid, 2 * node + 1);
    Node dir = range_query(ql, qr, mid + 1, r, 2 * node + 2);
    assert( (esq.t == -1 && dir.t == -1) == false);
    if (esq.t == -1) return dir;
    if (dir.t == -1) return esq;
    Node ans;
    ans.merge(esq, dir);
    return ans;
  }
  void push(int l, int r, int node) {
    if (seg[node].lazy_set != INVALID) {
      seg[node].delta_pre = seg[node].lazy_set;
      seg[node].delta_suf = seg[node].lazy_set;
      seg[node].pre = seg[node].suf = seg[node].best = r - l + 1;
      seg[node].sum = 1LL * (r - l + 1) * seg[node].lazy_set;
      if (l != r) {
        seg[2 * node + 1].lazy = INVALID;
        seg[2 * node + 1].lazy_set = seg[node].lazy_set;     
        seg[2 * node + 2].lazy = INVALID;
        seg[2 * node + 2].lazy_set = seg[node].lazy_set;
      }
      seg[node].lazy_set = INVALID;
    }
    if (seg[node].lazy != INVALID) {
      seg[node].delta_pre += seg[node].lazy;
      seg[node].delta_suf += seg[node].lazy;
      seg[node].sum += 1LL * (r - l + 1) * seg[node].lazy;
      if (l != r) {
        if (seg[2 * node + 1].lazy == INVALID) seg[2 * node + 1].lazy = 0;
        seg[2 * node + 1].lazy += seg[node].lazy;     
        if (seg[2 * node + 2].lazy == INVALID) seg[2 * node + 2].lazy = 0;
        seg[2 * node + 2].lazy += seg[node].lazy;
      }
      seg[node].lazy = INVALID;
    }
  }
  void range_update(int ql, int qr, long long delta, bool type, int l, int r, int node) {
    push(l, r, node);
    if (l > qr || r < ql) return;
    if (l >= ql && r <= qr) {
      if (type == 0) {
        seg[node].lazy = delta;
      } else {
        seg[node].lazy_set = delta;
      }
      push(l, r, node);
      return;
    }
    const int mid = l + (r - l) / 2;
    range_update(ql, qr, delta, type, l, mid, 2 * node + 1);
    range_update(ql, qr, delta, type, mid + 1, r, 2 * node + 2);
    seg[node].merge(seg[2 * node + 1], seg[2 * node + 2]);
  }
public:
  SegmentTree(const vector<long long> &arr) : LO(0), HI((int)arr.size() - 1) {
    if (arr.empty()) return;
    seg.clear();
    seg.resize(4 * (int)arr.size());
    build(LO, HI, 0, arr);
  }
  int range_query(int lo, int hi) {
    if (lo > hi) return 0;
    Node ans = range_query(lo, hi, LO, HI, 0);
    return ans.best;
  }
  long long range_sum(int lo, int hi) {
    if (lo > hi) return 0;
    Node ans = range_query(lo, hi, LO, HI, 0);
    return ans.sum;
  }
  void range_update(int ql, int qr, long long delta) {
    if (ql > qr) return;
    range_update(ql, qr, delta, 0, LO, HI, 0);
  }
  void range_set(int ql, int qr, long long delta) {
    if (ql > qr) return;
    range_update(ql, qr, delta, 1, LO, HI, 0);
  }
};
 
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0); 
  int n, q;
  cin >> n >> q;
  vector<int> arr(n);
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  vector<long long> diff(n - 1);
  for (int i = 1; i < n; i++) {
    diff[i - 1] = arr[i] - arr[i - 1];
  }
  if(diff.empty()) diff.emplace_back(0);
  SegmentTree seg(diff);
  debug(diff);
  long long first_element = arr[0];
  while(q--) {
    int t, lo, hi;
    cin >> t >> lo >> hi;
    --lo; hi -= 2; 
    if (t == 1) {
      int delta, d;
      cin >> delta >> d;
      if (lo == 0) {
        first_element += delta;
      }
      seg.range_update(lo, hi, +d);
     
      if (lo - 1 >= 0) {
        seg.range_update(lo - 1, lo - 1, +delta);
      }
      ++hi;
      if (hi < n - 1) {
        const long long sum = 1LL * d * (hi - lo); 
        seg.range_update(hi, hi, -delta - sum);
      }
    } else if (t == 2) {
      int delta, d;
      cin >> delta >> d;
      long long left_side = first_element + (lo - 2 >= 0 ? seg.range_sum(0, lo - 2) : 0);
      long long right_side =  (hi + 1 < n - 1 ? first_element + seg.range_sum(0, hi + 1) : -1);
      seg.range_set(lo, hi, d);
      if (lo == 0) {
        first_element = delta;
      }
      seg.range_set(lo, hi, d);
      if (lo - 1 >= 0) {
        const long long first_term = delta;
        seg.range_set(lo - 1, lo - 1, first_term - left_side);
      }
      ++hi;
      if (hi < n - 1) {
        const long long last_term = delta + 1LL * d * (hi - lo);
        seg.range_set(hi, hi, right_side - last_term);
      }
    } else {
      cout << seg.range_query(lo, hi) + 1 << '\n';
    }
  }
  return 0;
}

Compilation message

Progression.cpp: In function 'int main()':
Progression.cpp:22:20: warning: statement has no effect [-Wunused-value]
   22 | #define debug(...) 47
      |                    ^~
Progression.cpp:185:3: note: in expansion of macro 'debug'
  185 |   debug(diff);
      |   ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 207 ms 79652 KB Output is correct
2 Correct 108 ms 768 KB Output is correct
3 Correct 103 ms 724 KB Output is correct
4 Correct 107 ms 632 KB Output is correct
5 Correct 102 ms 760 KB Output is correct
6 Correct 119 ms 668 KB Output is correct
7 Correct 97 ms 760 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 199 ms 79736 KB Output is correct
12 Correct 206 ms 79608 KB Output is correct
13 Correct 201 ms 79868 KB Output is correct
14 Correct 202 ms 79864 KB Output is correct
15 Correct 199 ms 79912 KB Output is correct
16 Correct 202 ms 79736 KB Output is correct
17 Correct 201 ms 79608 KB Output is correct
18 Correct 199 ms 79608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 640 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 3 ms 640 KB Output is correct
9 Correct 4 ms 640 KB Output is correct
10 Correct 3 ms 640 KB Output is correct
11 Correct 2 ms 640 KB Output is correct
12 Correct 3 ms 640 KB Output is correct
13 Correct 2 ms 640 KB Output is correct
14 Correct 2 ms 640 KB Output is correct
15 Correct 3 ms 640 KB Output is correct
16 Correct 3 ms 640 KB Output is correct
17 Correct 3 ms 640 KB Output is correct
18 Correct 3 ms 640 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 453 ms 80120 KB Output is correct
2 Correct 115 ms 1020 KB Output is correct
3 Correct 109 ms 888 KB Output is correct
4 Correct 104 ms 888 KB Output is correct
5 Correct 107 ms 1144 KB Output is correct
6 Correct 109 ms 1016 KB Output is correct
7 Correct 107 ms 1016 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 468 ms 79992 KB Output is correct
12 Correct 441 ms 80248 KB Output is correct
13 Correct 462 ms 79864 KB Output is correct
14 Correct 451 ms 79992 KB Output is correct
15 Correct 435 ms 80120 KB Output is correct
16 Correct 455 ms 80504 KB Output is correct
17 Correct 454 ms 80508 KB Output is correct
18 Correct 450 ms 80632 KB Output is correct
19 Correct 408 ms 79864 KB Output is correct
20 Correct 412 ms 79864 KB Output is correct
21 Correct 401 ms 79864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 828 ms 79260 KB Output is correct
2 Correct 172 ms 504 KB Output is correct
3 Correct 183 ms 3704 KB Output is correct
4 Correct 187 ms 3704 KB Output is correct
5 Correct 179 ms 3832 KB Output is correct
6 Correct 180 ms 3704 KB Output is correct
7 Correct 180 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 875 ms 85652 KB Output is correct
12 Correct 830 ms 88952 KB Output is correct
13 Correct 821 ms 85880 KB Output is correct
14 Correct 824 ms 85828 KB Output is correct
15 Correct 786 ms 88824 KB Output is correct
16 Correct 856 ms 88952 KB Output is correct
17 Correct 860 ms 88952 KB Output is correct
18 Correct 847 ms 89184 KB Output is correct
19 Correct 826 ms 88824 KB Output is correct
20 Correct 848 ms 88824 KB Output is correct
21 Correct 825 ms 88992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 453 ms 80120 KB Output is correct
2 Correct 115 ms 1020 KB Output is correct
3 Correct 109 ms 888 KB Output is correct
4 Correct 104 ms 888 KB Output is correct
5 Correct 107 ms 1144 KB Output is correct
6 Correct 109 ms 1016 KB Output is correct
7 Correct 107 ms 1016 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 468 ms 79992 KB Output is correct
12 Correct 441 ms 80248 KB Output is correct
13 Correct 462 ms 79864 KB Output is correct
14 Correct 451 ms 79992 KB Output is correct
15 Correct 435 ms 80120 KB Output is correct
16 Correct 455 ms 80504 KB Output is correct
17 Correct 454 ms 80508 KB Output is correct
18 Correct 450 ms 80632 KB Output is correct
19 Correct 408 ms 79864 KB Output is correct
20 Correct 412 ms 79864 KB Output is correct
21 Correct 401 ms 79864 KB Output is correct
22 Correct 1088 ms 79352 KB Output is correct
23 Correct 177 ms 632 KB Output is correct
24 Correct 169 ms 760 KB Output is correct
25 Correct 171 ms 760 KB Output is correct
26 Correct 174 ms 632 KB Output is correct
27 Correct 174 ms 632 KB Output is correct
28 Correct 184 ms 932 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 1137 ms 79500 KB Output is correct
33 Correct 1075 ms 79484 KB Output is correct
34 Correct 1187 ms 79608 KB Output is correct
35 Correct 1162 ms 79608 KB Output is correct
36 Correct 898 ms 80124 KB Output is correct
37 Correct 900 ms 79788 KB Output is correct
38 Correct 936 ms 79672 KB Output is correct
39 Correct 1101 ms 79608 KB Output is correct
40 Correct 1157 ms 79608 KB Output is correct
41 Correct 1149 ms 79608 KB Output is correct
42 Correct 1203 ms 79864 KB Output is correct
43 Correct 1086 ms 79564 KB Output is correct
44 Correct 1076 ms 79480 KB Output is correct
45 Correct 1075 ms 79608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 207 ms 79652 KB Output is correct
2 Correct 108 ms 768 KB Output is correct
3 Correct 103 ms 724 KB Output is correct
4 Correct 107 ms 632 KB Output is correct
5 Correct 102 ms 760 KB Output is correct
6 Correct 119 ms 668 KB Output is correct
7 Correct 97 ms 760 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 199 ms 79736 KB Output is correct
12 Correct 206 ms 79608 KB Output is correct
13 Correct 201 ms 79868 KB Output is correct
14 Correct 202 ms 79864 KB Output is correct
15 Correct 199 ms 79912 KB Output is correct
16 Correct 202 ms 79736 KB Output is correct
17 Correct 201 ms 79608 KB Output is correct
18 Correct 199 ms 79608 KB Output is correct
19 Correct 3 ms 640 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 2 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 3 ms 640 KB Output is correct
27 Correct 4 ms 640 KB Output is correct
28 Correct 3 ms 640 KB Output is correct
29 Correct 2 ms 640 KB Output is correct
30 Correct 3 ms 640 KB Output is correct
31 Correct 2 ms 640 KB Output is correct
32 Correct 2 ms 640 KB Output is correct
33 Correct 3 ms 640 KB Output is correct
34 Correct 3 ms 640 KB Output is correct
35 Correct 3 ms 640 KB Output is correct
36 Correct 3 ms 640 KB Output is correct
37 Correct 1 ms 384 KB Output is correct
38 Correct 1 ms 384 KB Output is correct
39 Correct 1 ms 384 KB Output is correct
40 Correct 453 ms 80120 KB Output is correct
41 Correct 115 ms 1020 KB Output is correct
42 Correct 109 ms 888 KB Output is correct
43 Correct 104 ms 888 KB Output is correct
44 Correct 107 ms 1144 KB Output is correct
45 Correct 109 ms 1016 KB Output is correct
46 Correct 107 ms 1016 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 468 ms 79992 KB Output is correct
51 Correct 441 ms 80248 KB Output is correct
52 Correct 462 ms 79864 KB Output is correct
53 Correct 451 ms 79992 KB Output is correct
54 Correct 435 ms 80120 KB Output is correct
55 Correct 455 ms 80504 KB Output is correct
56 Correct 454 ms 80508 KB Output is correct
57 Correct 450 ms 80632 KB Output is correct
58 Correct 408 ms 79864 KB Output is correct
59 Correct 412 ms 79864 KB Output is correct
60 Correct 401 ms 79864 KB Output is correct
61 Correct 828 ms 79260 KB Output is correct
62 Correct 172 ms 504 KB Output is correct
63 Correct 183 ms 3704 KB Output is correct
64 Correct 187 ms 3704 KB Output is correct
65 Correct 179 ms 3832 KB Output is correct
66 Correct 180 ms 3704 KB Output is correct
67 Correct 180 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 875 ms 85652 KB Output is correct
72 Correct 830 ms 88952 KB Output is correct
73 Correct 821 ms 85880 KB Output is correct
74 Correct 824 ms 85828 KB Output is correct
75 Correct 786 ms 88824 KB Output is correct
76 Correct 856 ms 88952 KB Output is correct
77 Correct 860 ms 88952 KB Output is correct
78 Correct 847 ms 89184 KB Output is correct
79 Correct 826 ms 88824 KB Output is correct
80 Correct 848 ms 88824 KB Output is correct
81 Correct 825 ms 88992 KB Output is correct
82 Correct 1088 ms 79352 KB Output is correct
83 Correct 177 ms 632 KB Output is correct
84 Correct 169 ms 760 KB Output is correct
85 Correct 171 ms 760 KB Output is correct
86 Correct 174 ms 632 KB Output is correct
87 Correct 174 ms 632 KB Output is correct
88 Correct 184 ms 932 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 1137 ms 79500 KB Output is correct
93 Correct 1075 ms 79484 KB Output is correct
94 Correct 1187 ms 79608 KB Output is correct
95 Correct 1162 ms 79608 KB Output is correct
96 Correct 898 ms 80124 KB Output is correct
97 Correct 900 ms 79788 KB Output is correct
98 Correct 936 ms 79672 KB Output is correct
99 Correct 1101 ms 79608 KB Output is correct
100 Correct 1157 ms 79608 KB Output is correct
101 Correct 1149 ms 79608 KB Output is correct
102 Correct 1203 ms 79864 KB Output is correct
103 Correct 1086 ms 79564 KB Output is correct
104 Correct 1076 ms 79480 KB Output is correct
105 Correct 1075 ms 79608 KB Output is correct
106 Correct 1521 ms 89524 KB Output is correct
107 Correct 246 ms 3832 KB Output is correct
108 Correct 245 ms 3832 KB Output is correct
109 Correct 244 ms 3804 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 1170 ms 88484 KB Output is correct
114 Correct 1225 ms 88344 KB Output is correct
115 Correct 1244 ms 88604 KB Output is correct
116 Correct 1149 ms 88460 KB Output is correct
117 Correct 1780 ms 89464 KB Output is correct
118 Correct 1097 ms 88440 KB Output is correct
119 Correct 1087 ms 88564 KB Output is correct
120 Correct 531 ms 86904 KB Output is correct
121 Correct 468 ms 86904 KB Output is correct
122 Correct 485 ms 87032 KB Output is correct
123 Correct 406 ms 86008 KB Output is correct
124 Correct 387 ms 86008 KB Output is correct
125 Correct 391 ms 86136 KB Output is correct
126 Correct 1479 ms 86216 KB Output is correct
127 Correct 1514 ms 86008 KB Output is correct
128 Correct 1469 ms 89412 KB Output is correct
129 Correct 1507 ms 86136 KB Output is correct
130 Correct 1099 ms 86480 KB Output is correct
131 Correct 1014 ms 86264 KB Output is correct
132 Correct 1020 ms 85988 KB Output is correct
133 Correct 1496 ms 89544 KB Output is correct
134 Correct 1479 ms 89468 KB Output is correct
135 Correct 1488 ms 89584 KB Output is correct
136 Correct 244 ms 3832 KB Output is correct
137 Correct 244 ms 3832 KB Output is correct
138 Correct 246 ms 3832 KB Output is correct