Submission #1113319

# Submission time Handle Problem Language Result Execution time Memory
1113319 2024-11-16T11:16:46 Z dwuy Fish 2 (JOI22_fish2) C++17
60 / 100
4000 ms 41136 KB
/**         - dwuy -

      />    フ
      |  _  _|
      /`ミ _x ノ
     /      |
    /   ヽ   ?
 / ̄|   | | |
 | ( ̄ヽ__ヽ_)_)
 \二つ

**/
#include <bits/stdc++.h>

#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL)
#define file(a) freopen(a".inp","r",stdin); freopen(a".out", "w",stdout)
#define fi first
#define se second
#define endl "\n"
#define len(s) (int)((s).size())
#define MASK(k)(1LL<<(k))
#define TASK "test"

using namespace std;

typedef tuple<int, int, int> tpiii;
typedef pair<double, double> pdd;
typedef pair<int, int> pii;
typedef long long ll;

const long long OO = 1e18;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const int MX = 100005;

int n;
int a[MX];

struct Dwuy{
    int n;
    vector<ll> tree;

    Dwuy(int n = 0) : n(n), tree(n + 5, 0) {}

    void update(int idx, ll val){
        for(; idx<=n; idx+=-idx&idx) tree[idx] += val;
    }

    ll gsum(int idx){
        ll res = 0;
        for(; idx; idx-=-idx&idx) res += tree[idx];
        return res;
    }

    int gsum(int l, int r){
        return min(1LL*INF, gsum(r) - gsum(l - 1));
    }
} dwuy;

struct Taiki{
    int n;
    vector<int> tree;

    Taiki(int n = 0) : n(n), tree(n<<2|3, 0) {}

    void update(int pos, int val){
        int id = 1;
        for(int lo=1, hi=n; lo<hi;){
            int mid = (lo + hi)>>1;
            if(pos <= mid) hi = mid, id = id<<1;
            else lo = mid + 1, id = id<<1|1;
        }
        tree[id] = val;
        for(id>>=1; id; id>>=1) tree[id] = max(tree[id<<1], tree[id<<1|1]);
    }

    int lmx(int l, int r, int id, const int &lim, const int &pos, const int &val){
        if(l > pos || tree[id] <= val) return l - 1;
        if(r < lim) return lim - 1;
        if(l == r) return l;
        int mid = (l + r)>>1;
        int p = lmx(mid + 1, r, id<<1|1, lim, pos, val);
        return p != mid? p : lmx(l, mid, id<<1, lim, pos, val);
    }

    int lmx(int lim, int pos, int val){
        return lmx(1, n, 1, lim, pos, val);
    }

    int rmx(int l, int r, int id, const int &lim, const int &pos, const int &val){
        if(r < pos || tree[id] <= val) return r + 1;
        if(l > lim) return lim + 1;
        if(l == r) return l;
        int mid = (l + r)>>1;
        int p = rmx(l, mid, id<<1, lim, pos, val);
        return p != mid + 1? p : rmx(mid + 1, r, id<<1|1, lim, pos, val);
    }
    
    int rmx(int lim, int pos, int val){
        return rmx(1, n, 1, lim, pos, val);
    }

} taiki;

struct Cnode{
    vector<pii> pl, pr;
    int l, r;
    /// pl: {pl[i], ...} -> pr[i] = R;
    /// pr: {pr[i], ...} -> pl[i] = L;
    Cnode() : pl({}), pr({}), l(0), r(0) {}
};

struct Chii{
    int n;
    vector<Cnode> tree;

    Chii(int n = 0) : n(n), tree(n<<2|3, Cnode()) {}

    void build(int l, int r, int id){
        tree[id].l = l;
        tree[id].r = r;
        if(l == r) return;
        int mid = (l + r)>>1;
        build(l, mid, id<<1);
        build(mid + 1, r, id<<1|1);
    }

    void build(){
        build(1, n, 1);
    }

    Cnode combine(Cnode a, Cnode b){
        Cnode res;
        res.l = a.l;
        res.r = b.r;
        if(a.pl.size() == 0 || b.pl.size() == 0) return res;

        res.pr = a.pr;
        res.pr.pop_back();
        res.pl = b.pl;
        res.pl.pop_back();

        pii extrLeft = {-1, -1};
        pii extrRight = {-1, -1};

        int bsum = dwuy.gsum(b.l, b.pr[0].fi);
        for(int i=0, j=b.l - 1; i<len(b.pr);){
            while(j >= a.l){
                int p = taiki.lmx(a.l, j, bsum);
                p = max(p, a.l - 1);
                if(p != j){
                    bsum = min(bsum + dwuy.gsum(p + 1, j), INF);
                    j = p;
                    while(i + 1 < len(b.pr) && bsum >= ::a[b.pr[i].fi + 1]){
                        b.pr[i + 1].se += b.pr[i].se;
                        bsum = min(bsum + dwuy.gsum(b.pr[i].fi + 1, b.pr[i + 1].fi), INF);
                        i++;
                    }
                }
                else break;
            }
            if(j < a.l) res.pr.push_back(b.pr[i]);
            i++;
            if(i == len(b.pr)) extrLeft = {j + 1, b.pr.back().se};                
            else bsum = min(bsum + dwuy.gsum(b.pr[i - 1].fi + 1, b.pr[i].fi), INF);
            while(i + 1 < len(b.pr) && bsum >= ::a[b.pr[i].fi + 1]){
                b.pr[i + 1].se += b.pr[i].se;
                bsum = min(bsum + dwuy.gsum(b.pr[i].fi + 1, b.pr[i + 1].fi), INF);
                i++;
            }
        }

        bsum = dwuy.gsum(a.pl[0].fi, a.r);
        for(int i=0, j=a.r + 1; i<len(a.pl); ){
            while(j <= b.r){
                int p = taiki.rmx(b.r, j, bsum);
                p = min(p, b.r + 1);
                if(p != j){
                    bsum = min(bsum + dwuy.gsum(j, p - 1), INF);
                    j = p;
                    while(i + 1 <len(a.pl) && bsum >= ::a[a.pl[i].fi - 1]){
                        a.pl[i + 1].se += a.pl[i].se;
                        bsum = min(bsum + dwuy.gsum(a.pl[i + 1].fi, a.pl[i].fi - 1), INF);
                        i++;
                    }
                }
                else break;
            }
            if(j > b.r) res.pl.push_back(a.pl[i]);
            i++;
            if(i == len(a.pl)) extrRight = {j - 1, a.pl.back().se};
            else bsum = min(bsum + dwuy.gsum(a.pl[i].fi, a.pl[i - 1].fi - 1), INF);
            while(i + 1 <len(a.pl) && bsum >= ::a[a.pl[i].fi - 1]){
                a.pl[i + 1].se += a.pl[i].se;
                bsum = min(bsum + dwuy.gsum(a.pl[i + 1].fi, a.pl[i].fi - 1), INF);
                i++;
            }
        }
        bool flag = 1;
        for(pii &p: res.pr) if(p.fi == extrRight.fi){
            p.se += extrRight.se;
            flag = 0;
        }
        if(flag){
            res.pr.push_back(extrRight);
            int i = len(res.pr) - 1;
            while(i > 0 && res.pr[i].fi < res.pr[i - 1].fi){
                swap(res.pr[i], res.pr[i - 1]);
                i--;
            }
        }
        flag = 1;
        for(pii &p: res.pl) if(p.fi == extrLeft.fi){
            p.se += extrLeft.se;
            flag = 0;
        }
        if(flag){
            res.pl.push_back(extrLeft);
            int i = len(res.pl) - 1;
            while(i > 0 && res.pl[i].fi > res.pl[i - 1].fi){
                swap(res.pl[i], res.pl[i - 1]);
                i--;
            }
        }
        return res;
    }

    void update(int pos){
        int id = 1;
        for(int lo=1, hi=n; lo<hi;){
            int mid = (lo + hi)>>1;
            if(pos <= mid) hi = mid, id = id<<1;
            else lo = mid + 1, id = id<<1|1;
        }
        tree[id].pl = {{pos, 1}};
        tree[id].pr = {{pos, 1}};
        for(id>>=1; id; id>>=1) tree[id] = combine(tree[id<<1], tree[id<<1|1]);
    }

    Cnode get(int l, int r, int id, const int &u, const int &v){
        if(l >= u && r <= v) return tree[id];
        int mid = (l + r)>>1;
        if(mid >= v) return get(l, mid, id<<1, u, v);
        if(mid < u) return get(mid + 1, r, id<<1|1, u, v);
        return combine(get(l, mid, id<<1, u, v), get(mid + 1, r, id<<1|1, u, v));
    }

    int get(int l, int r){
        Cnode res = get(1, n, 1, l, r);
        return res.pl.back().se;
    }

    void show(int l, int r, int id){
        cout << " === " << l << ' ' << r << " = " << id << endl;
        cout << "pl: "; for(pii p: tree[id].pl) cout << "{" << p.fi << ", " << p.se << "} ";
        cout << endl;
        cout << "pr: "; for(pii p: tree[id].pr) cout << "{" << p.fi << ", " << p.se << "} ";
        cout << endl << endl;
        if(l == r) return;
        int mid = (l + r)>>1;
        show(l, mid, id<<1);
        show(mid + 1, r, id<<1|1);
    }

    void show(){
        show(1, n, 1);
    }
} chii;



int32_t main(){
    fastIO;
    //file(TASK);

    cin >> n;
    for(int i=1; i<=n; i++) cin >> a[i];

    dwuy = Dwuy(n);
    taiki = Taiki(n);
    chii = Chii(n);
    chii.build();

    for(int i=1; i<=n; i++) dwuy.update(i, a[i]);
    for(int i=1; i<=n; i++) taiki.update(i, a[i]);
    for(int i=1; i<=n; i++) chii.update(i);
    // chii.show();
    
    int q; cin >> q;
    while(q--){
        int oper, u, v;
        cin >> oper >> u >> v;
        if(oper == 1){
            dwuy.update(u, -a[u] + v);
            a[u] = v;
            taiki.update(u, a[u]);
            chii.update(u);
        }
        else{
            cout << chii.get(u, v) << endl;
        }
    }

    return 0;
}




# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 4 ms 824 KB Output is correct
6 Correct 4 ms 592 KB Output is correct
7 Correct 4 ms 592 KB Output is correct
8 Correct 6 ms 592 KB Output is correct
9 Correct 4 ms 592 KB Output is correct
10 Correct 3 ms 592 KB Output is correct
11 Correct 2 ms 592 KB Output is correct
12 Correct 3 ms 764 KB Output is correct
13 Correct 3 ms 592 KB Output is correct
14 Correct 5 ms 592 KB Output is correct
15 Correct 4 ms 592 KB Output is correct
16 Correct 4 ms 592 KB Output is correct
17 Correct 3 ms 592 KB Output is correct
18 Correct 2 ms 592 KB Output is correct
19 Correct 3 ms 592 KB Output is correct
20 Correct 3 ms 516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 155 ms 40008 KB Output is correct
3 Correct 162 ms 39508 KB Output is correct
4 Correct 169 ms 39960 KB Output is correct
5 Correct 159 ms 39496 KB Output is correct
6 Correct 151 ms 37976 KB Output is correct
7 Correct 144 ms 37704 KB Output is correct
8 Correct 144 ms 37704 KB Output is correct
9 Correct 158 ms 37704 KB Output is correct
10 Correct 170 ms 40264 KB Output is correct
11 Correct 162 ms 39124 KB Output is correct
12 Correct 146 ms 37668 KB Output is correct
13 Correct 144 ms 37552 KB Output is correct
14 Correct 156 ms 37864 KB Output is correct
15 Correct 152 ms 37968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 4 ms 824 KB Output is correct
6 Correct 4 ms 592 KB Output is correct
7 Correct 4 ms 592 KB Output is correct
8 Correct 6 ms 592 KB Output is correct
9 Correct 4 ms 592 KB Output is correct
10 Correct 3 ms 592 KB Output is correct
11 Correct 2 ms 592 KB Output is correct
12 Correct 3 ms 764 KB Output is correct
13 Correct 3 ms 592 KB Output is correct
14 Correct 5 ms 592 KB Output is correct
15 Correct 4 ms 592 KB Output is correct
16 Correct 4 ms 592 KB Output is correct
17 Correct 3 ms 592 KB Output is correct
18 Correct 2 ms 592 KB Output is correct
19 Correct 3 ms 592 KB Output is correct
20 Correct 3 ms 516 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 155 ms 40008 KB Output is correct
23 Correct 162 ms 39508 KB Output is correct
24 Correct 169 ms 39960 KB Output is correct
25 Correct 159 ms 39496 KB Output is correct
26 Correct 151 ms 37976 KB Output is correct
27 Correct 144 ms 37704 KB Output is correct
28 Correct 144 ms 37704 KB Output is correct
29 Correct 158 ms 37704 KB Output is correct
30 Correct 170 ms 40264 KB Output is correct
31 Correct 162 ms 39124 KB Output is correct
32 Correct 146 ms 37668 KB Output is correct
33 Correct 144 ms 37552 KB Output is correct
34 Correct 156 ms 37864 KB Output is correct
35 Correct 152 ms 37968 KB Output is correct
36 Correct 184 ms 40000 KB Output is correct
37 Correct 195 ms 39496 KB Output is correct
38 Correct 190 ms 39544 KB Output is correct
39 Correct 189 ms 39932 KB Output is correct
40 Correct 192 ms 39496 KB Output is correct
41 Correct 157 ms 37704 KB Output is correct
42 Correct 151 ms 37708 KB Output is correct
43 Correct 167 ms 37744 KB Output is correct
44 Correct 168 ms 37712 KB Output is correct
45 Correct 238 ms 40708 KB Output is correct
46 Correct 214 ms 40216 KB Output is correct
47 Correct 208 ms 38984 KB Output is correct
48 Correct 160 ms 37704 KB Output is correct
49 Correct 157 ms 37704 KB Output is correct
50 Correct 171 ms 37772 KB Output is correct
51 Correct 165 ms 37920 KB Output is correct
52 Correct 174 ms 37748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 155 ms 40008 KB Output is correct
3 Correct 162 ms 39508 KB Output is correct
4 Correct 169 ms 39960 KB Output is correct
5 Correct 159 ms 39496 KB Output is correct
6 Correct 151 ms 37976 KB Output is correct
7 Correct 144 ms 37704 KB Output is correct
8 Correct 144 ms 37704 KB Output is correct
9 Correct 158 ms 37704 KB Output is correct
10 Correct 170 ms 40264 KB Output is correct
11 Correct 162 ms 39124 KB Output is correct
12 Correct 146 ms 37668 KB Output is correct
13 Correct 144 ms 37552 KB Output is correct
14 Correct 156 ms 37864 KB Output is correct
15 Correct 152 ms 37968 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 2844 ms 39932 KB Output is correct
18 Correct 2048 ms 40544 KB Output is correct
19 Correct 2839 ms 39948 KB Output is correct
20 Correct 3105 ms 39788 KB Output is correct
21 Correct 2862 ms 39752 KB Output is correct
22 Correct 1848 ms 40352 KB Output is correct
23 Correct 2959 ms 39896 KB Output is correct
24 Correct 2852 ms 39964 KB Output is correct
25 Correct 2916 ms 40776 KB Output is correct
26 Correct 2827 ms 39752 KB Output is correct
27 Correct 819 ms 38216 KB Output is correct
28 Correct 778 ms 38356 KB Output is correct
29 Correct 806 ms 38092 KB Output is correct
30 Correct 1494 ms 37896 KB Output is correct
31 Correct 1553 ms 37932 KB Output is correct
32 Execution timed out 4033 ms 39244 KB Time limit exceeded
33 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 155 ms 40008 KB Output is correct
3 Correct 162 ms 39508 KB Output is correct
4 Correct 169 ms 39960 KB Output is correct
5 Correct 159 ms 39496 KB Output is correct
6 Correct 151 ms 37976 KB Output is correct
7 Correct 144 ms 37704 KB Output is correct
8 Correct 144 ms 37704 KB Output is correct
9 Correct 158 ms 37704 KB Output is correct
10 Correct 170 ms 40264 KB Output is correct
11 Correct 162 ms 39124 KB Output is correct
12 Correct 146 ms 37668 KB Output is correct
13 Correct 144 ms 37552 KB Output is correct
14 Correct 156 ms 37864 KB Output is correct
15 Correct 152 ms 37968 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1933 ms 40220 KB Output is correct
18 Correct 1446 ms 40456 KB Output is correct
19 Correct 1711 ms 39596 KB Output is correct
20 Correct 1070 ms 40656 KB Output is correct
21 Correct 1889 ms 40188 KB Output is correct
22 Correct 1581 ms 41032 KB Output is correct
23 Correct 2307 ms 39704 KB Output is correct
24 Correct 1455 ms 39708 KB Output is correct
25 Correct 2029 ms 39720 KB Output is correct
26 Correct 697 ms 40176 KB Output is correct
27 Correct 973 ms 37800 KB Output is correct
28 Correct 1054 ms 38656 KB Output is correct
29 Correct 716 ms 37816 KB Output is correct
30 Correct 1108 ms 37756 KB Output is correct
31 Correct 1395 ms 38492 KB Output is correct
32 Correct 1593 ms 39240 KB Output is correct
33 Correct 2062 ms 40152 KB Output is correct
34 Correct 1515 ms 40520 KB Output is correct
35 Correct 1824 ms 40480 KB Output is correct
36 Correct 1475 ms 41136 KB Output is correct
37 Correct 1310 ms 39752 KB Output is correct
38 Correct 828 ms 38796 KB Output is correct
39 Correct 896 ms 38172 KB Output is correct
40 Correct 469 ms 38472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 4 ms 824 KB Output is correct
6 Correct 4 ms 592 KB Output is correct
7 Correct 4 ms 592 KB Output is correct
8 Correct 6 ms 592 KB Output is correct
9 Correct 4 ms 592 KB Output is correct
10 Correct 3 ms 592 KB Output is correct
11 Correct 2 ms 592 KB Output is correct
12 Correct 3 ms 764 KB Output is correct
13 Correct 3 ms 592 KB Output is correct
14 Correct 5 ms 592 KB Output is correct
15 Correct 4 ms 592 KB Output is correct
16 Correct 4 ms 592 KB Output is correct
17 Correct 3 ms 592 KB Output is correct
18 Correct 2 ms 592 KB Output is correct
19 Correct 3 ms 592 KB Output is correct
20 Correct 3 ms 516 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 155 ms 40008 KB Output is correct
23 Correct 162 ms 39508 KB Output is correct
24 Correct 169 ms 39960 KB Output is correct
25 Correct 159 ms 39496 KB Output is correct
26 Correct 151 ms 37976 KB Output is correct
27 Correct 144 ms 37704 KB Output is correct
28 Correct 144 ms 37704 KB Output is correct
29 Correct 158 ms 37704 KB Output is correct
30 Correct 170 ms 40264 KB Output is correct
31 Correct 162 ms 39124 KB Output is correct
32 Correct 146 ms 37668 KB Output is correct
33 Correct 144 ms 37552 KB Output is correct
34 Correct 156 ms 37864 KB Output is correct
35 Correct 152 ms 37968 KB Output is correct
36 Correct 184 ms 40000 KB Output is correct
37 Correct 195 ms 39496 KB Output is correct
38 Correct 190 ms 39544 KB Output is correct
39 Correct 189 ms 39932 KB Output is correct
40 Correct 192 ms 39496 KB Output is correct
41 Correct 157 ms 37704 KB Output is correct
42 Correct 151 ms 37708 KB Output is correct
43 Correct 167 ms 37744 KB Output is correct
44 Correct 168 ms 37712 KB Output is correct
45 Correct 238 ms 40708 KB Output is correct
46 Correct 214 ms 40216 KB Output is correct
47 Correct 208 ms 38984 KB Output is correct
48 Correct 160 ms 37704 KB Output is correct
49 Correct 157 ms 37704 KB Output is correct
50 Correct 171 ms 37772 KB Output is correct
51 Correct 165 ms 37920 KB Output is correct
52 Correct 174 ms 37748 KB Output is correct
53 Correct 1 ms 336 KB Output is correct
54 Correct 2844 ms 39932 KB Output is correct
55 Correct 2048 ms 40544 KB Output is correct
56 Correct 2839 ms 39948 KB Output is correct
57 Correct 3105 ms 39788 KB Output is correct
58 Correct 2862 ms 39752 KB Output is correct
59 Correct 1848 ms 40352 KB Output is correct
60 Correct 2959 ms 39896 KB Output is correct
61 Correct 2852 ms 39964 KB Output is correct
62 Correct 2916 ms 40776 KB Output is correct
63 Correct 2827 ms 39752 KB Output is correct
64 Correct 819 ms 38216 KB Output is correct
65 Correct 778 ms 38356 KB Output is correct
66 Correct 806 ms 38092 KB Output is correct
67 Correct 1494 ms 37896 KB Output is correct
68 Correct 1553 ms 37932 KB Output is correct
69 Execution timed out 4033 ms 39244 KB Time limit exceeded
70 Halted 0 ms 0 KB -