Submission #681625

# Submission time Handle Problem Language Result Execution time Memory
681625 2023-01-13T13:19:29 Z stevancv Progression (NOI20_progression) C++14
35 / 100
1259 ms 116932 KB
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 3e5 + 2;
const ll linf = 9e18;
struct Segtree {
    ll st[4 * N], lzys[4 * N], lzya[4 * N];
    void Reset1(int n) {
        for (int i = 1; i <= 4 * n; i++) {
            lzys[i] = linf;
            st[i] = lzya[i] = 0;
        }
    }
    void Propagate1(int node, int l, int r) {
        if (lzys[node] == linf && lzya[node] == 0) return;
        if (l < r) {
            if (lzys[node] != linf) {
                lzys[2 * node] = lzys[2 * node + 1] = lzys[node];
                lzya[2 * node] = lzya[2 * node + 1] = 0;
            }
            if (lzya[node] != 0) {
                if (lzys[2 * node] != linf) lzys[2 * node] += lzya[node];
                else lzya[2 * node] += lzya[node];
                if (lzys[2 * node + 1] != linf) lzys[2 * node + 1] += lzya[node];
                else lzya[2 * node + 1] += lzya[node];
            }
        }
        if (lzys[node] != linf) {st[node] = lzys[node]; lzys[node] = linf;}
        if (lzya[node] != 0) {st[node] += lzya[node]; lzya[node] = 0;}
    }
    void Set1(int node, int l, int r, int ql, int qr, ll y) {
        Propagate1(node, l, r);
        if (r < ql || qr < l || ql > qr) return;
        if (ql <= l && r <= qr) {
            lzys[node] = y;
            Propagate1(node, l, r);
            return;
        }
        int mid = l + r >> 1;
        Set1(2 * node, l, mid, ql, qr, y);
        Set1(2 * node + 1, mid + 1, r, ql, qr, y);
        st[node] = min(st[2 * node], st[2 * node + 1]);
    }
    void Add1(int node, int l, int r, int ql, int qr, ll y) {
        Propagate1(node, l, r);
        if (r < ql || qr < l || ql > qr) return;
        if (ql <= l && r <= qr) {
            lzya[node] = y;
            Propagate1(node, l, r);
            return;
        }
        int mid = l + r >> 1;
        Add1(2 * node, l, mid, ql, qr, y);
        Add1(2 * node + 1, mid + 1, r, ql, qr, y);
        st[node] = min(st[2 * node], st[2 * node + 1]);
    }
    ll Get1(int node, int l, int r, int x) {
        Propagate1(node, l, r);
        if (l == r) return st[node];
        int mid = l + r >> 1;
        if (x <= mid) return Get1(2 * node, l, mid, x);
        return Get1(2 * node + 1, mid + 1, r, x);
    }
}st1, st2;
struct Node {
    int ans, llen, rlen, seg;
    ll lval, rval;
    Node() {
        ans = llen = rlen = seg = 0;
        lval = rval = linf;
    }
    Node(int a, int b, int c, int d, ll e, ll f) {
        ans = a; llen = b; rlen = c; seg = d; lval = e; rval = f;
    }
}st[4 * N];
Node Merge(Node a, Node b) {
    Node c = Node();
    if (a.lval == linf) {c = b; return c;}
    if (b.lval == linf) {c = a; return c;}
    c.ans = max(a.ans, b.ans); c.lval = a.lval; c.rval = b.rval; c.llen = a.llen; c.rlen = b.rlen; c.seg = a.seg + b.seg;
    if (a.rval != b.lval) return c;
    smax(c.ans, a.rlen + b.llen);
    if (a.llen == a.seg) c.llen += b.llen;
    if (b.rlen == b.seg) c.rlen += a.rlen;
    return c;
}
ll a[N], d[N], lzys[4 * N], lzya[4 * N];
void Init(int node, int l, int r) {
    lzys[node] = linf; lzya[node] = 0;
    if (l == r) {
        st[node] = Node(1, 1, 1, 1, d[l], d[l]);
        return;
    }
    int mid = l + r >> 1;
    Init(2 * node, l, mid);
    Init(2 * node + 1, mid + 1, r);
    st[node] = Merge(st[2 * node], st[2 * node + 1]);
}
void Propagate(int node, int l, int r) {
    if (lzys[node] == linf && lzya[node] == 0) return;
    if (l < r) {
        if (lzys[node] != linf) {
            lzys[2 * node] = lzys[2 * node + 1] = lzys[node];
            lzya[2 * node] = lzya[2 * node + 1] = 0;
        }
        if (lzya[node] != 0) {
            if (lzys[2 * node] != linf) lzys[2 * node] += lzya[node];
            else lzya[2 * node] += lzya[node];
            if (lzys[2 * node + 1] != linf) lzys[2 * node + 1] += lzya[node];
            else lzya[2 * node + 1] += lzya[node];
        }
    }
    if (lzys[node] != linf) {
        st[node] = Node(r - l + 1, r - l + 1, r - l + 1, r - l + 1, lzys[node], lzys[node]);
        lzys[node] = linf;
    }
    if (lzya[node] != 0) {
        st[node].lval += lzya[node]; st[node].rval += lzya[node];
        lzya[node] = 0;
    }
}
void Set(int node, int l, int r, int ql, int qr, ll y) {
    Propagate(node, l, r);
    if (r < ql || qr < l || ql > qr) return;
    if (ql <= l && r <= qr) {
        lzys[node] = y;
        Propagate(node, l, r);
        return;
    }
    int mid = l + r >> 1;
    Set(2 * node, l, mid, ql, qr, y);
    Set(2 * node + 1, mid + 1, r, ql, qr, y);
    st[node] = Merge(st[2 * node], st[2 * node + 1]);
}
void Add(int node, int l, int r, int ql, int qr, ll y) {
    Propagate(node, l, r);
    if (r < ql || qr < l || ql > qr) return;
    if (ql <= l && r <= qr) {
        lzya[node] = y;
        Propagate(node, l, r);
        return;
    }
    int mid = l + r >> 1;
    Add(2 * node, l, mid, ql, qr, y);
    Add(2 * node + 1, mid + 1, r, ql, qr, y);
    st[node] = Merge(st[2 * node], st[2 * node + 1]);
}
Node Get(int node, int l, int r, int ql, int qr) {
    Propagate(node, l, r);
    if (r < ql || qr < l || ql > qr) return Node();
    if (ql <= l && r <= qr) return st[node];
    int mid = l + r >> 1;
    return Merge(Get(2 * node, l, mid, ql, qr), Get(2 * node + 1, mid + 1, r, ql, qr));
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n, q;
    cin >> n >> q;
    st1.Reset1(n); st2.Reset1(n);
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        st1.Add1(1, 1, n, i, i, a[i]);
    }
    for (int i = 1; i < n; i++) d[i] = a[i + 1] - a[i];
    if (n == 1) {
        while (q--) {
            int ty, l, r, s, c;
            cin >> ty;
            if (ty <= 2) cin >> l >> r >> s >> c;
            else cin >> l >> r;
            cout << 1 << en;
        }
        return 0;
    }
    Init(1, 1, n - 1);
    while (q--) {
        int ty; cin >> ty;
        if (ty == 1) {
            int l, r;
            ll s, c;
            cin >> l >> r >> s >> c;
            st1.Add1(1, 1, n, l, r, s - l * c);
            st2.Add1(1, 1, n, l, r, c);
            if (l > 1) Add(1, 1, n - 1, l - 1, l - 1, s);
            Add(1, 1, n - 1, l, r - 1, c);
            if (r < n) Add(1, 1, n - 1, r, r, -s - (r - l) * c);
        }
        else if (ty == 2) {
            int l, r;
            ll s, c;
            cin >> l >> r >> s >> c;
            st1.Set1(1, 1, n, l, r, s - l * c);
            st2.Set1(1, 1, n, l, r, c);
            if (l > 1) {
                ll val = st1.Get1(1, 1, n, l - 1) + (l - 1) * st2.Get1(1, 1, n, l - 1);
                Set(1, 1, n - 1, l - 1, l - 1, s - val);
            }
            Set(1, 1, n - 1, l, r - 1, c);
            if (r < n) {
                ll val = st1.Get1(1, 1, n, r + 1) + (r + 1) * st2.Get1(1, 1, n, r + 1);
                Set(1, 1, n - 1, r, r, val - s - (r - l) * c);
            }
        }
        else {
            int l, r;
            cin >> l >> r;
            cout << Get(1, 1, n - 1, l, r - 1).ans + 1 << en;
        }
    }
    return 0;
}

Compilation message

Progression.cpp: In member function 'void Segtree::Set1(int, int, int, int, int, long long int)':
Progression.cpp:44:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   44 |         int mid = l + r >> 1;
      |                   ~~^~~
Progression.cpp: In member function 'void Segtree::Add1(int, int, int, int, int, long long int)':
Progression.cpp:57:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   57 |         int mid = l + r >> 1;
      |                   ~~^~~
Progression.cpp: In member function 'long long int Segtree::Get1(int, int, int, int)':
Progression.cpp:65:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   65 |         int mid = l + r >> 1;
      |                   ~~^~~
Progression.cpp: In function 'void Init(int, int, int)':
Progression.cpp:99:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   99 |     int mid = l + r >> 1;
      |               ~~^~~
Progression.cpp: In function 'void Set(int, int, int, int, int, long long int)':
Progression.cpp:135:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  135 |     int mid = l + r >> 1;
      |               ~~^~~
Progression.cpp: In function 'void Add(int, int, int, int, int, long long int)':
Progression.cpp:148:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  148 |     int mid = l + r >> 1;
      |               ~~^~~
Progression.cpp: In function 'Node Get(int, int, int, int, int)':
Progression.cpp:157:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  157 |     int mid = l + r >> 1;
      |               ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 249 ms 115980 KB Output is correct
2 Correct 92 ms 38236 KB Output is correct
3 Correct 96 ms 38240 KB Output is correct
4 Correct 96 ms 38236 KB Output is correct
5 Correct 94 ms 38260 KB Output is correct
6 Correct 97 ms 38308 KB Output is correct
7 Correct 126 ms 38220 KB Output is correct
8 Incorrect 16 ms 37908 KB Output isn't correct
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 18 ms 38100 KB Output is correct
2 Correct 17 ms 37936 KB Output is correct
3 Correct 18 ms 37976 KB Output is correct
4 Correct 18 ms 37972 KB Output is correct
5 Incorrect 16 ms 37844 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 402 ms 116304 KB Output is correct
2 Correct 105 ms 38520 KB Output is correct
3 Correct 102 ms 38484 KB Output is correct
4 Correct 97 ms 38524 KB Output is correct
5 Correct 118 ms 38572 KB Output is correct
6 Correct 105 ms 38540 KB Output is correct
7 Correct 103 ms 38544 KB Output is correct
8 Correct 18 ms 37880 KB Output is correct
9 Correct 18 ms 37884 KB Output is correct
10 Correct 16 ms 37840 KB Output is correct
11 Correct 416 ms 116100 KB Output is correct
12 Correct 407 ms 116356 KB Output is correct
13 Correct 502 ms 116056 KB Output is correct
14 Correct 411 ms 116104 KB Output is correct
15 Correct 446 ms 116520 KB Output is correct
16 Correct 409 ms 116628 KB Output is correct
17 Correct 432 ms 116664 KB Output is correct
18 Correct 410 ms 116932 KB Output is correct
19 Correct 412 ms 116040 KB Output is correct
20 Correct 376 ms 116068 KB Output is correct
21 Correct 385 ms 116060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 957 ms 115696 KB Output is correct
2 Correct 189 ms 38132 KB Output is correct
3 Correct 190 ms 38148 KB Output is correct
4 Correct 201 ms 38104 KB Output is correct
5 Correct 195 ms 38160 KB Output is correct
6 Correct 196 ms 38340 KB Output is correct
7 Correct 192 ms 38180 KB Output is correct
8 Incorrect 17 ms 37844 KB Output isn't correct
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 402 ms 116304 KB Output is correct
2 Correct 105 ms 38520 KB Output is correct
3 Correct 102 ms 38484 KB Output is correct
4 Correct 97 ms 38524 KB Output is correct
5 Correct 118 ms 38572 KB Output is correct
6 Correct 105 ms 38540 KB Output is correct
7 Correct 103 ms 38544 KB Output is correct
8 Correct 18 ms 37880 KB Output is correct
9 Correct 18 ms 37884 KB Output is correct
10 Correct 16 ms 37840 KB Output is correct
11 Correct 416 ms 116100 KB Output is correct
12 Correct 407 ms 116356 KB Output is correct
13 Correct 502 ms 116056 KB Output is correct
14 Correct 411 ms 116104 KB Output is correct
15 Correct 446 ms 116520 KB Output is correct
16 Correct 409 ms 116628 KB Output is correct
17 Correct 432 ms 116664 KB Output is correct
18 Correct 410 ms 116932 KB Output is correct
19 Correct 412 ms 116040 KB Output is correct
20 Correct 376 ms 116068 KB Output is correct
21 Correct 385 ms 116060 KB Output is correct
22 Correct 1259 ms 115816 KB Output is correct
23 Correct 237 ms 38220 KB Output is correct
24 Correct 197 ms 38224 KB Output is correct
25 Correct 195 ms 38260 KB Output is correct
26 Correct 201 ms 38232 KB Output is correct
27 Correct 202 ms 38256 KB Output is correct
28 Correct 203 ms 38260 KB Output is correct
29 Incorrect 17 ms 37844 KB Output isn't correct
30 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 249 ms 115980 KB Output is correct
2 Correct 92 ms 38236 KB Output is correct
3 Correct 96 ms 38240 KB Output is correct
4 Correct 96 ms 38236 KB Output is correct
5 Correct 94 ms 38260 KB Output is correct
6 Correct 97 ms 38308 KB Output is correct
7 Correct 126 ms 38220 KB Output is correct
8 Incorrect 16 ms 37908 KB Output isn't correct
9 Halted 0 ms 0 KB -