Submission #698962

# Submission time Handle Problem Language Result Execution time Memory
698962 2023-02-15T03:10:09 Z vuavisao Progression (NOI20_progression) C++14
70 / 100
3000 ms 158432 KB
#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define ll long long
using namespace std;
 
template<typename Lhs, typename Rhs> inline bool Max_self(Lhs &a, Rhs b) { if(b > a) { a = b; return true; } return false; }
template<typename Lhs, typename Rhs> inline bool Min_self(Lhs &a, Rhs b) { if(b < a) { a = b; return true; } return false; }
 
const ll N = 3e5 + 10;
const ll INF = 1e18;
 
struct que {
    ll type = 0;
    ll l = 0, r = 0;
    ll s = 0, c = 0;
    que() {};
    friend istream& operator >> (istream& is, que& other) {
        is >> other.type >> other.l >> other.r;
        if(other.type <= 2) is >> other.s >> other.c;
        return is;
    }
};
 
ll n, q;
ll a[N];
que qq[N];
 
namespace sub1 {
    bool check() {
        for(ll i = 1; i <= q; ++ i) {
            if(qq[i].l != 1 || qq[i].r != n) return false;
        }
        return true;
    }
 
    ll res = 1;
    void solve() {
        for(ll i = 1, j; i < n; i = j - 1) {
            j = i + 1;
            ll s = a[j] - a[i];
            while(j <= n && a[j] - a[j - 1] == s) ++ j;
            res = max(res, j - i);
        }
        for(ll i = 1; i <= q; ++ i) {
            const auto& psy = qq[i];
            if(psy.type == 3) cout << res << '\n';
            else if(psy.type == 2) {
                res = n;
            }
        }
    }
}
 
namespace sub2 {
    void solve() {
        auto get_far = [&] (ll l, ll r) -> ll {
            ll res = 1;
            for(ll i = l, j; i < r; i = j - 1) {
                j = i + 1;
                ll s = a[j] - a[i];
                while(j <= r && a[j] - a[j - 1] == s) ++ j;
                res = max(res, j - i);
            }
            return res;
        };
        for(ll i = 1; i <= q; ++ i) {
            const auto& psy = qq[i];
            ll type = psy.type;
            if(type == 1) {
                ll add[2] = {psy.s, psy.c};
                for(ll i = psy.l; i <= psy.r; ++ i) {
                    a[i] += add[0];
                    add[0] += add[1];
                }
            }
            else if(type == 2) {
                ll add[2] = {psy.s, psy.c};
                for(ll i = psy.l; i <= psy.r; ++ i) {
                    a[i] = add[0];
                    add[0] += add[1];
                }
            }
            else {
//                cout << psy.l << ' ' << psy.r << '\n';
                cout << get_far(psy.l, psy.r) << '\n';
            }
        }
    }
}
 
namespace sub3 {
    bool check() {
        for(ll i = 1; i <= q; ++ i) {
            if(qq[i].type < 3) return false;
        }
        return true;
    }
 
    struct Node {
        struct state {
            ll val = 0, len = 0;
            state() {};
            state(ll _val, ll _len) { val = _val; len = _len; };
        };
        state pref = state(), suf = state();
        state sub = state();
        ll len = 0;
        Node() {};
        Node(ll _val, ll _len) {
            pref = state(_val, _len);
            suf = state(_val, _len);
            sub = state(0, 0);
            len = _len;
        }
        Node operator + (const Node& other) const {
            Node res;
            res.pref = this -> pref;
            if(this -> len == this -> pref.len && this -> pref.val == other.pref.val) {
                res.pref.len += other.pref.len;
            }
            res.suf = other.suf;
            if(other.len == other.suf.len && this -> suf.val == other.suf.val) {
                res.suf.len += this -> suf.len;
            }
            if(Max_self(res.sub.len, this -> sub.len)) res.sub = this -> sub;
            if(Max_self(res.sub.len, other.sub.len)) res.sub = other.sub;
            ll len_pref = min(this -> len - 1, this -> suf.len);
            if(Max_self(res.sub.len, len_pref)) res.sub.val = this -> suf.val;
            ll len_suf = min(other.len - 1, other.pref.len);
            if(Max_self(res.sub.len, len_suf)) res.sub.val = other.pref.val;
            if(this -> suf.val == other.pref.val && Max_self(res.sub.len, len_pref + len_suf)) {
                res.sub.val = this -> suf.val;
            }
            res.len = this -> len + other.len;
            return res;
        }
    };
 
    Node tree[N << 2];
    ll diff[N];
 
    void make_first(ll node, ll l, ll r, ll* value) {
        if(l == r) {
            tree[node] = Node(value[l], 1);
            return;
        }
        ll mid = (l + r) >> 1;
        make_first(node << 1, l, mid, value);
        make_first(node << 1 | 1, mid + 1, r, value);
        tree[node] = tree[node << 1] + tree[node << 1 | 1];
    }
 
    Node query(ll node, ll l, ll r, ll L, ll R) {
//        assert(L <= R && l <= r);
        if(l > r || L > R) assert(false);
        if(L <= l && r <= R) return tree[node];
        ll mid = (l + r) >> 1;
        if(R <= mid) return query(node << 1, l, mid, L, R);
        if(L > mid) return query(node << 1 | 1, mid + 1, r, L, R);
        return query(node << 1, l, mid, L, R) + query(node << 1 | 1, mid + 1, r, L, R);
    }
 
    void solve() {
        for(ll i = 1; i <= n; ++ i) diff[i] = a[i] - a[i - 1];
        make_first(1, 1, n, diff);
        for(ll i = 1; i <= q; ++ i) {
            const auto& psy = qq[i];
            ll res = 0;
            Node tmp = query(1, 1, n, psy.l, psy.r);
            Max_self(res, tmp.pref.len);
//            cout << res << '\n';
            Max_self(res, tmp.sub.len + 1);
//            cout << res << '\n';
            Max_self(res, tmp.suf.len + 1);
//            cout << res << '\n';
            Min_self(res, psy.r - psy.l + 1);
            cout << res << '\n';
        }
    }
}
 
namespace sub4 {
    bool check() {
        for(ll i = 1; i <= q; ++ i) {
            if(qq[i].type <= 2 && qq[i].l != qq[i].r) return false;
        }
        return true;
    }
 
    struct Node {
        struct state {
            ll val = 0, len = 0;
            state() {};
            state(ll _val, ll _len) { val = _val; len = _len; };
        };
        state pref = state(), suf = state();
        state sub = state();
        ll len = 0;
        Node() {};
        Node(ll _val, ll _len) {
            pref = state(_val, _len);
            suf = state(_val, _len);
            sub = state(0, 0);
            len = _len;
        }
        Node operator + (const Node& other) const {
            Node res;
            res.pref = this -> pref;
            if(this -> len == this -> pref.len && this -> pref.val == other.pref.val) {
                res.pref.len += other.pref.len;
            }
            res.suf = other.suf;
            if(other.len == other.suf.len && this -> suf.val == other.suf.val) {
                res.suf.len += this -> suf.len;
            }
            if(Max_self(res.sub.len, this -> sub.len)) res.sub = this -> sub;
            if(Max_self(res.sub.len, other.sub.len)) res.sub = other.sub;
            ll len_pref = min(this -> len - 1, this -> suf.len);
            if(Max_self(res.sub.len, len_pref)) res.sub.val = this -> suf.val;
            ll len_suf = min(other.len - 1, other.pref.len);
            if(Max_self(res.sub.len, len_suf)) res.sub.val = other.pref.val;
            if(this -> suf.val == other.pref.val && Max_self(res.sub.len, len_pref + len_suf)) {
                res.sub.val = this -> suf.val;
            }
            res.len = this -> len + other.len;
            return res;
        }
    };
 
    Node tree[N << 2];
    ll diff[N];
 
    void make_first(ll node, ll l, ll r, ll* value) {
        if(l == r) {
            tree[node] = Node(value[l], 1);
            return;
        }
        ll mid = (l + r) >> 1;
        make_first(node << 1, l, mid, value);
        make_first(node << 1 | 1, mid + 1, r, value);
        tree[node] = tree[node << 1] + tree[node << 1 | 1];
    }
 
    void update(ll node, ll l, ll r, ll idx, ll val) {
        if(l == r) {
            tree[node] = Node(val, 1);
            return;
        }
        ll mid = (l + r) >> 1;
        if(idx <= mid) update(node << 1, l, mid, idx, val);
        else update(node << 1 | 1, mid + 1, r, idx, val);
        tree[node] = tree[node << 1] + tree[node << 1 | 1];
    }
 
    Node query(ll node, ll l, ll r, ll L, ll R) {
//        assert(L <= R && l <= r);
        if(l > r || L > R) assert(false);
        if(L <= l && r <= R) return tree[node];
        ll mid = (l + r) >> 1;
        if(R <= mid) return query(node << 1, l, mid, L, R);
        if(L > mid) return query(node << 1 | 1, mid + 1, r, L, R);
        return query(node << 1, l, mid, L, R) + query(node << 1 | 1, mid + 1, r, L, R);
    }
 
    void solve() {
        for(ll i = 1; i <= n; ++ i) diff[i] = a[i] - a[i - 1];
        make_first(1, 1, n, diff);
        for(ll i = 1; i <= q; ++ i) {
            const auto& psy = qq[i];
            if(psy.type <= 2) {
                ll idx = psy.l;
                if(psy.type == 2) a[idx] = psy.s;
                else a[idx] += psy.s;
                diff[idx] = a[idx] - a[idx - 1];
                update(1, 1, n, idx, diff[idx]);
                if(idx < n) {
                    ++ idx;
                    diff[idx] = a[idx] - a[idx - 1];
                    update(1, 1, n, idx, diff[idx]);
                }
            }
            else {
                ll res = 0;
                Node tmp = query(1, 1, n, psy.l, psy.r);
                Max_self(res, tmp.pref.len);
    //            cout << res << '\n';
                Max_self(res, tmp.sub.len + 1);
    //            cout << res << '\n';
                Max_self(res, tmp.suf.len + 1);
    //            cout << res << '\n';
                Min_self(res, psy.r - psy.l + 1);
                cout << res << '\n';
            }
        }
    }
}
 
int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    if (fopen("Progression.inp", "r")) {
        freopen("Progression.inp", "r", stdin);
        freopen("Progression.out", "w", stdout);
    }
    cin >> n >> q;
    for(ll i = 1; i <= n; ++ i) cin >> a[i];
    for(ll i = 1; i <= q; ++ i) cin >> qq[i];
    if(sub1::check()) {
        sub1::solve();
        return 0;
    }
    if(sub3::check()) {
        sub3::solve();
        return 0;
    }
    if(sub4::check()) {
        sub4::solve();
        return 0;
    }
    sub2::solve();
    return 0;
}
 
/// Code by vuavisao

Compilation message

Progression.cpp: In function 'int32_t main()':
Progression.cpp:304:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  304 |         freopen("Progression.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Progression.cpp:305:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  305 |         freopen("Progression.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 159 ms 147448 KB Output is correct
2 Correct 121 ms 144876 KB Output is correct
3 Correct 121 ms 144656 KB Output is correct
4 Correct 120 ms 144596 KB Output is correct
5 Correct 125 ms 144716 KB Output is correct
6 Correct 119 ms 144716 KB Output is correct
7 Correct 121 ms 144460 KB Output is correct
8 Correct 55 ms 143536 KB Output is correct
9 Correct 56 ms 143536 KB Output is correct
10 Correct 55 ms 143520 KB Output is correct
11 Correct 162 ms 147336 KB Output is correct
12 Correct 158 ms 147192 KB Output is correct
13 Correct 162 ms 147404 KB Output is correct
14 Correct 164 ms 147528 KB Output is correct
15 Correct 162 ms 147196 KB Output is correct
16 Correct 169 ms 146832 KB Output is correct
17 Correct 170 ms 146888 KB Output is correct
18 Correct 161 ms 146876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 54 ms 143564 KB Output is correct
2 Correct 55 ms 143512 KB Output is correct
3 Correct 56 ms 143468 KB Output is correct
4 Correct 55 ms 143548 KB Output is correct
5 Correct 57 ms 143636 KB Output is correct
6 Correct 55 ms 143564 KB Output is correct
7 Correct 54 ms 143508 KB Output is correct
8 Correct 55 ms 143492 KB Output is correct
9 Correct 55 ms 143484 KB Output is correct
10 Correct 55 ms 143552 KB Output is correct
11 Correct 55 ms 143560 KB Output is correct
12 Correct 59 ms 143576 KB Output is correct
13 Correct 55 ms 143548 KB Output is correct
14 Correct 56 ms 143548 KB Output is correct
15 Correct 56 ms 143564 KB Output is correct
16 Correct 55 ms 143524 KB Output is correct
17 Correct 55 ms 143492 KB Output is correct
18 Correct 56 ms 143612 KB Output is correct
19 Correct 56 ms 143560 KB Output is correct
20 Correct 58 ms 143576 KB Output is correct
21 Correct 55 ms 143588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 237 ms 149332 KB Output is correct
2 Correct 130 ms 146232 KB Output is correct
3 Correct 128 ms 146164 KB Output is correct
4 Correct 118 ms 146256 KB Output is correct
5 Correct 125 ms 146388 KB Output is correct
6 Correct 125 ms 146380 KB Output is correct
7 Correct 125 ms 146376 KB Output is correct
8 Correct 55 ms 143464 KB Output is correct
9 Correct 56 ms 143544 KB Output is correct
10 Correct 55 ms 143580 KB Output is correct
11 Correct 266 ms 154048 KB Output is correct
12 Correct 234 ms 155308 KB Output is correct
13 Correct 257 ms 153928 KB Output is correct
14 Correct 270 ms 153932 KB Output is correct
15 Correct 237 ms 155236 KB Output is correct
16 Correct 274 ms 155884 KB Output is correct
17 Correct 267 ms 155816 KB Output is correct
18 Correct 270 ms 155964 KB Output is correct
19 Correct 234 ms 155180 KB Output is correct
20 Correct 245 ms 155252 KB Output is correct
21 Correct 239 ms 155260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 463 ms 149212 KB Output is correct
2 Correct 160 ms 146892 KB Output is correct
3 Correct 159 ms 146848 KB Output is correct
4 Correct 161 ms 146896 KB Output is correct
5 Correct 171 ms 146888 KB Output is correct
6 Correct 168 ms 146892 KB Output is correct
7 Correct 159 ms 146928 KB Output is correct
8 Correct 59 ms 143564 KB Output is correct
9 Correct 60 ms 143552 KB Output is correct
10 Correct 60 ms 143564 KB Output is correct
11 Correct 455 ms 154876 KB Output is correct
12 Correct 462 ms 158048 KB Output is correct
13 Correct 464 ms 154864 KB Output is correct
14 Correct 457 ms 154828 KB Output is correct
15 Correct 448 ms 158044 KB Output is correct
16 Correct 490 ms 158156 KB Output is correct
17 Correct 496 ms 158196 KB Output is correct
18 Correct 475 ms 158412 KB Output is correct
19 Correct 447 ms 158060 KB Output is correct
20 Correct 452 ms 158112 KB Output is correct
21 Correct 489 ms 158432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 237 ms 149332 KB Output is correct
2 Correct 130 ms 146232 KB Output is correct
3 Correct 128 ms 146164 KB Output is correct
4 Correct 118 ms 146256 KB Output is correct
5 Correct 125 ms 146388 KB Output is correct
6 Correct 125 ms 146380 KB Output is correct
7 Correct 125 ms 146376 KB Output is correct
8 Correct 55 ms 143464 KB Output is correct
9 Correct 56 ms 143544 KB Output is correct
10 Correct 55 ms 143580 KB Output is correct
11 Correct 266 ms 154048 KB Output is correct
12 Correct 234 ms 155308 KB Output is correct
13 Correct 257 ms 153928 KB Output is correct
14 Correct 270 ms 153932 KB Output is correct
15 Correct 237 ms 155236 KB Output is correct
16 Correct 274 ms 155884 KB Output is correct
17 Correct 267 ms 155816 KB Output is correct
18 Correct 270 ms 155964 KB Output is correct
19 Correct 234 ms 155180 KB Output is correct
20 Correct 245 ms 155252 KB Output is correct
21 Correct 239 ms 155260 KB Output is correct
22 Execution timed out 3063 ms 155256 KB Time limit exceeded
23 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 159 ms 147448 KB Output is correct
2 Correct 121 ms 144876 KB Output is correct
3 Correct 121 ms 144656 KB Output is correct
4 Correct 120 ms 144596 KB Output is correct
5 Correct 125 ms 144716 KB Output is correct
6 Correct 119 ms 144716 KB Output is correct
7 Correct 121 ms 144460 KB Output is correct
8 Correct 55 ms 143536 KB Output is correct
9 Correct 56 ms 143536 KB Output is correct
10 Correct 55 ms 143520 KB Output is correct
11 Correct 162 ms 147336 KB Output is correct
12 Correct 158 ms 147192 KB Output is correct
13 Correct 162 ms 147404 KB Output is correct
14 Correct 164 ms 147528 KB Output is correct
15 Correct 162 ms 147196 KB Output is correct
16 Correct 169 ms 146832 KB Output is correct
17 Correct 170 ms 146888 KB Output is correct
18 Correct 161 ms 146876 KB Output is correct
19 Correct 54 ms 143564 KB Output is correct
20 Correct 55 ms 143512 KB Output is correct
21 Correct 56 ms 143468 KB Output is correct
22 Correct 55 ms 143548 KB Output is correct
23 Correct 57 ms 143636 KB Output is correct
24 Correct 55 ms 143564 KB Output is correct
25 Correct 54 ms 143508 KB Output is correct
26 Correct 55 ms 143492 KB Output is correct
27 Correct 55 ms 143484 KB Output is correct
28 Correct 55 ms 143552 KB Output is correct
29 Correct 55 ms 143560 KB Output is correct
30 Correct 59 ms 143576 KB Output is correct
31 Correct 55 ms 143548 KB Output is correct
32 Correct 56 ms 143548 KB Output is correct
33 Correct 56 ms 143564 KB Output is correct
34 Correct 55 ms 143524 KB Output is correct
35 Correct 55 ms 143492 KB Output is correct
36 Correct 56 ms 143612 KB Output is correct
37 Correct 56 ms 143560 KB Output is correct
38 Correct 58 ms 143576 KB Output is correct
39 Correct 55 ms 143588 KB Output is correct
40 Correct 237 ms 149332 KB Output is correct
41 Correct 130 ms 146232 KB Output is correct
42 Correct 128 ms 146164 KB Output is correct
43 Correct 118 ms 146256 KB Output is correct
44 Correct 125 ms 146388 KB Output is correct
45 Correct 125 ms 146380 KB Output is correct
46 Correct 125 ms 146376 KB Output is correct
47 Correct 55 ms 143464 KB Output is correct
48 Correct 56 ms 143544 KB Output is correct
49 Correct 55 ms 143580 KB Output is correct
50 Correct 266 ms 154048 KB Output is correct
51 Correct 234 ms 155308 KB Output is correct
52 Correct 257 ms 153928 KB Output is correct
53 Correct 270 ms 153932 KB Output is correct
54 Correct 237 ms 155236 KB Output is correct
55 Correct 274 ms 155884 KB Output is correct
56 Correct 267 ms 155816 KB Output is correct
57 Correct 270 ms 155964 KB Output is correct
58 Correct 234 ms 155180 KB Output is correct
59 Correct 245 ms 155252 KB Output is correct
60 Correct 239 ms 155260 KB Output is correct
61 Correct 463 ms 149212 KB Output is correct
62 Correct 160 ms 146892 KB Output is correct
63 Correct 159 ms 146848 KB Output is correct
64 Correct 161 ms 146896 KB Output is correct
65 Correct 171 ms 146888 KB Output is correct
66 Correct 168 ms 146892 KB Output is correct
67 Correct 159 ms 146928 KB Output is correct
68 Correct 59 ms 143564 KB Output is correct
69 Correct 60 ms 143552 KB Output is correct
70 Correct 60 ms 143564 KB Output is correct
71 Correct 455 ms 154876 KB Output is correct
72 Correct 462 ms 158048 KB Output is correct
73 Correct 464 ms 154864 KB Output is correct
74 Correct 457 ms 154828 KB Output is correct
75 Correct 448 ms 158044 KB Output is correct
76 Correct 490 ms 158156 KB Output is correct
77 Correct 496 ms 158196 KB Output is correct
78 Correct 475 ms 158412 KB Output is correct
79 Correct 447 ms 158060 KB Output is correct
80 Correct 452 ms 158112 KB Output is correct
81 Correct 489 ms 158432 KB Output is correct
82 Execution timed out 3063 ms 155256 KB Time limit exceeded
83 Halted 0 ms 0 KB -