답안 #843475

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
843475 2023-09-04T03:18:54 Z CDuong Fish 2 (JOI22_fish2) C++17
100 / 100
491 ms 221408 KB
// Judges with GCC >= 12 only needs Ofast
// #pragma GCC optimize("O3,no-stack-protector,fast-math,unroll-loops,tree-vectorize")
// MLE optimization
// #pragma GCC optimize("conserve-stack")
// Old judges
// #pragma GCC target("sse4.2,popcnt,lzcnt,abm,mmx,fma,bmi,bmi2")
// New judges. Test with assert(__builtin_cpu_supports("avx2"));
// #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma,tune=native")
// Atcoder
// #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma")
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
- insert(x),
- find_by_order(k): return iterator to the k-th smallest element
- order_of_key(x): the number of elements that are strictly smaller
*/
#include <bits/stdc++.h>
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
uniform_real_distribution<> pp(0.0, 1.0);
// #define int long long
#define ld long double
#define pii pair<int, int>
#define piii pair<pii, int>
#define mpp make_pair
#define fi first
#define se second
const int inf = 1e9;
const int mod = 1e9 + 7;
const int maxn = 100005;
const int bl = 650;
const int maxs = 655;
const int maxm = 200005;
const int maxq = 500005;
const int maxl = 35;
const int maxa = 250000;
const int root = 3;
/*
int power(int a,int n){
    int res=1;
    while(n){
        if(n&1) res=res*a%mod;
        a=a*a%mod;n>>=1;
    }
    return res;
}
const int iroot=power(3,mod-2);
*/
const int base = 101;
int n, f[maxn];
struct node
{
    int pl[maxl], pr[maxl], cl[maxl], cr[maxl], sl[maxl], sr[maxl];
    int szl, szr, sum, cnt;
    node() {}
    void reset(int x)
    {
        szl = szr = 1;
        pl[0] = pr[0] = x;
        sl[0] = sr[0] = cl[0] = cr[0] = 0;
        sum = f[x];
        cnt = 1;
    }
    void print(int l, int r)
    {
        cout << '*' << l << ' ' << r << '\n';
        cout << sum << ' ' << cnt << '\n';
        cout << "#rr\n";
        for (int i = 0; i < szr; i++)
            cout << pr[i] << ' ' << cr[i] << ' ' << sr[i] << '\n';
        cout << "#ll\n";
        for (int i = 0; i < szl; i++)
            cout << pl[i] << ' ' << cl[i] << ' ' << sl[i] << '\n';
    }
};

namespace Segtree
{
    node tree[4 * maxn];
    node s[2];
    int st = -1;
    int add(int a, int b)
    {
        return min(a + b, inf);
    }
    void merge(node &res, node &a, node &b)
    {
        res.szl = a.szl;
        res.szr = b.szr;
        res.sum = add(a.sum, b.sum);
        res.cnt = 0;
        for (int i = 0; i < res.szl; i++)
            res.pl[i] = a.pl[i], res.cl[i] = a.cl[i], res.sl[i] = a.sl[i];
        for (int i = 0; i < res.szr; i++)
            res.pr[i] = b.pr[i], res.cr[i] = b.cr[i], res.sr[i] = b.sr[i];
        int len = 0, ok = 0;
        if (a.sum > b.sum)
        {
            int cur = b.sum;
            len = res.szr;
            ok = 0;
            for (int i = 0; i < a.szr; i++)
            {
                if (cur + a.sr[i] < f[a.pr[i]])
                {
                    res.pr[res.szr] = a.pr[i];
                    res.sr[res.szr] = add(cur, a.sr[i]);
                    res.cr[res.szr++] = 0;
                }
            }
        }
        else
        {
            int cur = a.sum;
            len = res.szl;
            ok = 1;
            for (int i = 0; i < b.szl; i++)
            {
                if (cur + b.sl[i] < f[b.pl[i]])
                {
                    res.pl[res.szl] = b.pl[i];
                    res.sl[res.szl] = add(cur, b.sl[i]);
                    res.cl[res.szl++] = 0;
                }
            }
        }
        b.sl[b.szl] = b.sum;
        b.cl[b.szl] = b.cnt;
        a.sr[a.szr] = a.sum;
        a.cr[a.szr] = a.cnt;
        int ca = 0, cb = 0, cc = len;
        auto fix = [&](int val)
        {
            while (true)
            {
                if (ca < a.szr && a.sr[ca] + b.sl[cb] >= f[a.pr[ca]])
                    ca++;
                else if (cb < b.szl && a.sr[ca] + b.sl[cb] >= f[b.pl[cb]])
                    cb++;
                else
                    break;
            }
            if (ok && ca == a.szr)
            {
                while (cc < res.szl && (cb == b.szl || res.pl[cc] < b.pl[cb]))
                    cc++;
                if (cc < res.szl)
                    res.cl[cc] += val;
                else
                    res.cnt += val;
            }
            if (!ok && cb == b.szl)
            {
                while (cc < res.szr && (ca == a.szr || res.pr[cc] > a.pr[ca]))
                    cc++;
                if (cc < res.szr)
                    res.cr[cc] += val;
                else
                    res.cnt += val;
            }
        };
        for (int i = 1; i <= a.szr; i++)
        {
            ca = max(ca, i);
            fix(a.cr[i]);
        }
        ca = cb = 0;
        cc = len;
        for (int i = 1; i <= b.szl; i++)
        {
            cb = max(cb, i);
            fix(b.cl[i]);
        }
    }
    void query(int l, int r, int id, int tl, int tr)
    {
        if (r < tl || tr < l)
            return;
        if (tl <= l && r <= tr)
        {
            if (st == -1)
                s[0] = tree[id], st = 0;
            else
                st ^= 1, merge(s[st], s[st ^ 1], tree[id]);
            return;
        }
        int mid = (l + r) >> 1;
        query(l, mid, id << 1, tl, tr);
        query(mid + 1, r, id << 1 | 1, tl, tr);
    }
    int query(int l, int r)
    {
        st = -1;
        query(1, n, 1, l, r);
        return s[st].cnt;
    }
    void update(int l, int r, int id, int pos)
    {
        if (l == r)
        {
            tree[id].reset(l);
            return;
        }
        int mid = (l + r) >> 1;
        if (pos <= mid)
            update(l, mid, id << 1, pos);
        else
            update(mid + 1, r, id << 1 | 1, pos);
        merge(tree[id], tree[id << 1], tree[id << 1 | 1]);
    }
    void build(int l, int r, int id)
    {
        if (l == r)
        {
            tree[id].reset(l);
            return;
        }
        int mid = (l + r) >> 1;
        build(l, mid, id << 1);
        build(mid + 1, r, id << 1 | 1);
        merge(tree[id], tree[id << 1], tree[id << 1 | 1]);
    }
}
void solve()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> f[i];
    Segtree::build(1, n, 1);
    int q;
    cin >> q;
    for (int i = 1; i <= q; i++)
    {
        int t, x, y;
        cin >> t >> x >> y;
        if (t == 1)
            f[x] = y, Segtree::update(1, n, 1, x);
        else
            cout << Segtree::query(x, y) << '\n';
    }
}
signed main()
{
    // freopen("input.txt","r",stdin);
    // freopen("out.txt","w",stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int test = 1; // cin >> test;
    while (test--)
        solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 2 ms 2652 KB Output is correct
6 Correct 1 ms 2648 KB Output is correct
7 Correct 1 ms 2648 KB Output is correct
8 Correct 1 ms 2648 KB Output is correct
9 Correct 2 ms 2648 KB Output is correct
10 Correct 1 ms 2648 KB Output is correct
11 Correct 2 ms 2648 KB Output is correct
12 Correct 1 ms 2648 KB Output is correct
13 Correct 2 ms 2648 KB Output is correct
14 Correct 1 ms 2652 KB Output is correct
15 Correct 1 ms 2648 KB Output is correct
16 Correct 1 ms 2652 KB Output is correct
17 Correct 1 ms 2648 KB Output is correct
18 Correct 1 ms 2648 KB Output is correct
19 Correct 1 ms 2648 KB Output is correct
20 Correct 1 ms 2648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 37 ms 220424 KB Output is correct
3 Correct 39 ms 220512 KB Output is correct
4 Correct 37 ms 220760 KB Output is correct
5 Correct 37 ms 220780 KB Output is correct
6 Correct 37 ms 220496 KB Output is correct
7 Correct 36 ms 220496 KB Output is correct
8 Correct 37 ms 220496 KB Output is correct
9 Correct 34 ms 220496 KB Output is correct
10 Correct 38 ms 220496 KB Output is correct
11 Correct 36 ms 220496 KB Output is correct
12 Correct 36 ms 220496 KB Output is correct
13 Correct 34 ms 220496 KB Output is correct
14 Correct 36 ms 220496 KB Output is correct
15 Correct 36 ms 220496 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 2 ms 2652 KB Output is correct
6 Correct 1 ms 2648 KB Output is correct
7 Correct 1 ms 2648 KB Output is correct
8 Correct 1 ms 2648 KB Output is correct
9 Correct 2 ms 2648 KB Output is correct
10 Correct 1 ms 2648 KB Output is correct
11 Correct 2 ms 2648 KB Output is correct
12 Correct 1 ms 2648 KB Output is correct
13 Correct 2 ms 2648 KB Output is correct
14 Correct 1 ms 2652 KB Output is correct
15 Correct 1 ms 2648 KB Output is correct
16 Correct 1 ms 2652 KB Output is correct
17 Correct 1 ms 2648 KB Output is correct
18 Correct 1 ms 2648 KB Output is correct
19 Correct 1 ms 2648 KB Output is correct
20 Correct 1 ms 2648 KB Output is correct
21 Correct 0 ms 344 KB Output is correct
22 Correct 37 ms 220424 KB Output is correct
23 Correct 39 ms 220512 KB Output is correct
24 Correct 37 ms 220760 KB Output is correct
25 Correct 37 ms 220780 KB Output is correct
26 Correct 37 ms 220496 KB Output is correct
27 Correct 36 ms 220496 KB Output is correct
28 Correct 37 ms 220496 KB Output is correct
29 Correct 34 ms 220496 KB Output is correct
30 Correct 38 ms 220496 KB Output is correct
31 Correct 36 ms 220496 KB Output is correct
32 Correct 36 ms 220496 KB Output is correct
33 Correct 34 ms 220496 KB Output is correct
34 Correct 36 ms 220496 KB Output is correct
35 Correct 36 ms 220496 KB Output is correct
36 Correct 46 ms 220608 KB Output is correct
37 Correct 40 ms 220496 KB Output is correct
38 Correct 41 ms 220344 KB Output is correct
39 Correct 39 ms 220444 KB Output is correct
40 Correct 44 ms 220536 KB Output is correct
41 Correct 39 ms 220496 KB Output is correct
42 Correct 39 ms 220496 KB Output is correct
43 Correct 38 ms 220496 KB Output is correct
44 Correct 39 ms 220500 KB Output is correct
45 Correct 43 ms 220496 KB Output is correct
46 Correct 41 ms 220500 KB Output is correct
47 Correct 40 ms 220496 KB Output is correct
48 Correct 38 ms 220496 KB Output is correct
49 Correct 37 ms 220500 KB Output is correct
50 Correct 38 ms 220496 KB Output is correct
51 Correct 40 ms 220496 KB Output is correct
52 Correct 38 ms 220496 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 37 ms 220424 KB Output is correct
3 Correct 39 ms 220512 KB Output is correct
4 Correct 37 ms 220760 KB Output is correct
5 Correct 37 ms 220780 KB Output is correct
6 Correct 37 ms 220496 KB Output is correct
7 Correct 36 ms 220496 KB Output is correct
8 Correct 37 ms 220496 KB Output is correct
9 Correct 34 ms 220496 KB Output is correct
10 Correct 38 ms 220496 KB Output is correct
11 Correct 36 ms 220496 KB Output is correct
12 Correct 36 ms 220496 KB Output is correct
13 Correct 34 ms 220496 KB Output is correct
14 Correct 36 ms 220496 KB Output is correct
15 Correct 36 ms 220496 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 463 ms 221120 KB Output is correct
18 Correct 377 ms 221012 KB Output is correct
19 Correct 454 ms 220752 KB Output is correct
20 Correct 451 ms 220896 KB Output is correct
21 Correct 486 ms 220872 KB Output is correct
22 Correct 368 ms 220996 KB Output is correct
23 Correct 477 ms 220812 KB Output is correct
24 Correct 470 ms 220888 KB Output is correct
25 Correct 460 ms 220804 KB Output is correct
26 Correct 460 ms 220912 KB Output is correct
27 Correct 269 ms 221264 KB Output is correct
28 Correct 262 ms 221008 KB Output is correct
29 Correct 271 ms 221264 KB Output is correct
30 Correct 365 ms 220752 KB Output is correct
31 Correct 359 ms 220680 KB Output is correct
32 Correct 469 ms 220844 KB Output is correct
33 Correct 491 ms 221008 KB Output is correct
34 Correct 463 ms 220840 KB Output is correct
35 Correct 450 ms 220844 KB Output is correct
36 Correct 468 ms 221004 KB Output is correct
37 Correct 242 ms 221276 KB Output is correct
38 Correct 221 ms 220848 KB Output is correct
39 Correct 284 ms 221008 KB Output is correct
40 Correct 281 ms 220968 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 37 ms 220424 KB Output is correct
3 Correct 39 ms 220512 KB Output is correct
4 Correct 37 ms 220760 KB Output is correct
5 Correct 37 ms 220780 KB Output is correct
6 Correct 37 ms 220496 KB Output is correct
7 Correct 36 ms 220496 KB Output is correct
8 Correct 37 ms 220496 KB Output is correct
9 Correct 34 ms 220496 KB Output is correct
10 Correct 38 ms 220496 KB Output is correct
11 Correct 36 ms 220496 KB Output is correct
12 Correct 36 ms 220496 KB Output is correct
13 Correct 34 ms 220496 KB Output is correct
14 Correct 36 ms 220496 KB Output is correct
15 Correct 36 ms 220496 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 324 ms 220500 KB Output is correct
18 Correct 295 ms 220624 KB Output is correct
19 Correct 265 ms 220636 KB Output is correct
20 Correct 223 ms 220660 KB Output is correct
21 Correct 319 ms 220756 KB Output is correct
22 Correct 298 ms 220752 KB Output is correct
23 Correct 371 ms 220496 KB Output is correct
24 Correct 271 ms 220676 KB Output is correct
25 Correct 312 ms 220828 KB Output is correct
26 Correct 200 ms 220672 KB Output is correct
27 Correct 246 ms 220880 KB Output is correct
28 Correct 216 ms 220744 KB Output is correct
29 Correct 198 ms 220752 KB Output is correct
30 Correct 233 ms 220576 KB Output is correct
31 Correct 261 ms 220572 KB Output is correct
32 Correct 290 ms 220628 KB Output is correct
33 Correct 303 ms 220608 KB Output is correct
34 Correct 260 ms 220696 KB Output is correct
35 Correct 268 ms 220776 KB Output is correct
36 Correct 245 ms 220496 KB Output is correct
37 Correct 242 ms 220724 KB Output is correct
38 Correct 163 ms 220752 KB Output is correct
39 Correct 190 ms 220752 KB Output is correct
40 Correct 107 ms 220888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 2 ms 2652 KB Output is correct
6 Correct 1 ms 2648 KB Output is correct
7 Correct 1 ms 2648 KB Output is correct
8 Correct 1 ms 2648 KB Output is correct
9 Correct 2 ms 2648 KB Output is correct
10 Correct 1 ms 2648 KB Output is correct
11 Correct 2 ms 2648 KB Output is correct
12 Correct 1 ms 2648 KB Output is correct
13 Correct 2 ms 2648 KB Output is correct
14 Correct 1 ms 2652 KB Output is correct
15 Correct 1 ms 2648 KB Output is correct
16 Correct 1 ms 2652 KB Output is correct
17 Correct 1 ms 2648 KB Output is correct
18 Correct 1 ms 2648 KB Output is correct
19 Correct 1 ms 2648 KB Output is correct
20 Correct 1 ms 2648 KB Output is correct
21 Correct 0 ms 344 KB Output is correct
22 Correct 37 ms 220424 KB Output is correct
23 Correct 39 ms 220512 KB Output is correct
24 Correct 37 ms 220760 KB Output is correct
25 Correct 37 ms 220780 KB Output is correct
26 Correct 37 ms 220496 KB Output is correct
27 Correct 36 ms 220496 KB Output is correct
28 Correct 37 ms 220496 KB Output is correct
29 Correct 34 ms 220496 KB Output is correct
30 Correct 38 ms 220496 KB Output is correct
31 Correct 36 ms 220496 KB Output is correct
32 Correct 36 ms 220496 KB Output is correct
33 Correct 34 ms 220496 KB Output is correct
34 Correct 36 ms 220496 KB Output is correct
35 Correct 36 ms 220496 KB Output is correct
36 Correct 46 ms 220608 KB Output is correct
37 Correct 40 ms 220496 KB Output is correct
38 Correct 41 ms 220344 KB Output is correct
39 Correct 39 ms 220444 KB Output is correct
40 Correct 44 ms 220536 KB Output is correct
41 Correct 39 ms 220496 KB Output is correct
42 Correct 39 ms 220496 KB Output is correct
43 Correct 38 ms 220496 KB Output is correct
44 Correct 39 ms 220500 KB Output is correct
45 Correct 43 ms 220496 KB Output is correct
46 Correct 41 ms 220500 KB Output is correct
47 Correct 40 ms 220496 KB Output is correct
48 Correct 38 ms 220496 KB Output is correct
49 Correct 37 ms 220500 KB Output is correct
50 Correct 38 ms 220496 KB Output is correct
51 Correct 40 ms 220496 KB Output is correct
52 Correct 38 ms 220496 KB Output is correct
53 Correct 0 ms 344 KB Output is correct
54 Correct 463 ms 221120 KB Output is correct
55 Correct 377 ms 221012 KB Output is correct
56 Correct 454 ms 220752 KB Output is correct
57 Correct 451 ms 220896 KB Output is correct
58 Correct 486 ms 220872 KB Output is correct
59 Correct 368 ms 220996 KB Output is correct
60 Correct 477 ms 220812 KB Output is correct
61 Correct 470 ms 220888 KB Output is correct
62 Correct 460 ms 220804 KB Output is correct
63 Correct 460 ms 220912 KB Output is correct
64 Correct 269 ms 221264 KB Output is correct
65 Correct 262 ms 221008 KB Output is correct
66 Correct 271 ms 221264 KB Output is correct
67 Correct 365 ms 220752 KB Output is correct
68 Correct 359 ms 220680 KB Output is correct
69 Correct 469 ms 220844 KB Output is correct
70 Correct 491 ms 221008 KB Output is correct
71 Correct 463 ms 220840 KB Output is correct
72 Correct 450 ms 220844 KB Output is correct
73 Correct 468 ms 221004 KB Output is correct
74 Correct 242 ms 221276 KB Output is correct
75 Correct 221 ms 220848 KB Output is correct
76 Correct 284 ms 221008 KB Output is correct
77 Correct 281 ms 220968 KB Output is correct
78 Correct 0 ms 344 KB Output is correct
79 Correct 324 ms 220500 KB Output is correct
80 Correct 295 ms 220624 KB Output is correct
81 Correct 265 ms 220636 KB Output is correct
82 Correct 223 ms 220660 KB Output is correct
83 Correct 319 ms 220756 KB Output is correct
84 Correct 298 ms 220752 KB Output is correct
85 Correct 371 ms 220496 KB Output is correct
86 Correct 271 ms 220676 KB Output is correct
87 Correct 312 ms 220828 KB Output is correct
88 Correct 200 ms 220672 KB Output is correct
89 Correct 246 ms 220880 KB Output is correct
90 Correct 216 ms 220744 KB Output is correct
91 Correct 198 ms 220752 KB Output is correct
92 Correct 233 ms 220576 KB Output is correct
93 Correct 261 ms 220572 KB Output is correct
94 Correct 290 ms 220628 KB Output is correct
95 Correct 303 ms 220608 KB Output is correct
96 Correct 260 ms 220696 KB Output is correct
97 Correct 268 ms 220776 KB Output is correct
98 Correct 245 ms 220496 KB Output is correct
99 Correct 242 ms 220724 KB Output is correct
100 Correct 163 ms 220752 KB Output is correct
101 Correct 190 ms 220752 KB Output is correct
102 Correct 107 ms 220888 KB Output is correct
103 Correct 427 ms 220448 KB Output is correct
104 Correct 337 ms 220612 KB Output is correct
105 Correct 457 ms 221408 KB Output is correct
106 Correct 367 ms 220944 KB Output is correct
107 Correct 407 ms 220496 KB Output is correct
108 Correct 306 ms 220768 KB Output is correct
109 Correct 431 ms 220752 KB Output is correct
110 Correct 339 ms 220900 KB Output is correct
111 Correct 467 ms 221024 KB Output is correct
112 Correct 376 ms 220752 KB Output is correct
113 Correct 254 ms 220932 KB Output is correct
114 Correct 254 ms 221264 KB Output is correct
115 Correct 322 ms 220492 KB Output is correct
116 Correct 324 ms 220776 KB Output is correct
117 Correct 258 ms 220832 KB Output is correct
118 Correct 352 ms 221008 KB Output is correct
119 Correct 244 ms 220604 KB Output is correct
120 Correct 303 ms 220788 KB Output is correct
121 Correct 336 ms 221104 KB Output is correct
122 Correct 308 ms 220612 KB Output is correct
123 Correct 430 ms 220696 KB Output is correct
124 Correct 358 ms 220752 KB Output is correct
125 Correct 433 ms 220752 KB Output is correct
126 Correct 380 ms 220752 KB Output is correct
127 Correct 295 ms 220572 KB Output is correct
128 Correct 260 ms 220896 KB Output is correct
129 Correct 270 ms 220744 KB Output is correct
130 Correct 259 ms 220864 KB Output is correct