Submission #974678

# Submission time Handle Problem Language Result Execution time Memory
974678 2024-05-03T15:43:23 Z hotboy2703 Ants and Sugar (JOI22_sugar) C++14
100 / 100
1026 ms 86384 KB
#include<bits/stdc++.h>
using ll = long long;
using namespace std;
#define pll pair <ll,ll>
#define fi first
#define se second
#define MP make_pair
#define sz(a) (ll((a).size()))
#define BIT(mask,i) (((mask) >> (i))&1)
#define MASK(i) (1LL << (i))
const ll MAXQ = 5e5;
const ll INF = 1e18;
ll q,L;
struct query{
    ll t,x,a;
};
query all[MAXQ+100];
struct node{
    ll a[2][2];
    ll border;
};
node operator + (const node &x,const node &y){
    node res;
    for (ll i = 0;i < 2;i ++)for (ll j= 0 ;j < 2;j ++)res.a[i][j] = INF;
    for (ll ix = 0;ix < 2;ix ++){
    for (ll jx = 0;jx < 2;jx ++){
    for (ll iy = 0;iy < 2;iy ++){
    for (ll jy = 0;jy < 2;jy ++){
        res.a[ix][jy] = min(res.a[ix][jy],x.a[ix][jx] + y.a[iy][jy] + x.border*(jx&&iy));
    }
    }
    }
    }
    res.border = y.border;
    return res;
}
node tree[MAXQ*4+100];
ll lazy[MAXQ * 4 + 100];
void build(ll id,ll l,ll r){
    if (l == r){
        tree[id].a[0][1] = INF;
        tree[id].a[1][0] = INF;
    }
    else{
        ll mid = (l + r) >> 1;
        build(id<<1,l,mid);
        build(id<<1|1,mid+1,r);
    }
}
void push(ll id,ll v){
    lazy[id]+=v;
    for (ll i = 0;i < 2;i ++)for (ll j = 0;j < 2;j ++)tree[id].a[i][j] += v;
    tree[id].a[0][0] = min(tree[id].a[0][0],0LL);
    tree[id].border -= v;
}
void lz(ll id){
    push(id<<1,lazy[id]);
    push(id<<1|1,lazy[id]);
    lazy[id] = 0;
}

void update1(ll id,ll l,ll r,ll l1,ll r1,ll v){
    if (l > r1 || l1 > r || l1 > r1)return;
    if (l1 <= l && r <= r1){
        push(id,v);
        return;
    }
    lz(id);
    ll mid = (l + r) >> 1;
    update1(id<<1,l,mid,l1,r1,v);
    update1(id<<1|1,mid+1,r,l1,r1,v);
    tree[id] = tree[id<<1] + tree[id<<1|1];
}
void update2(ll id,ll l,ll r,ll i,ll v){
    if (l > i || r < i)return;
    if (l==r){
        tree[id].a[1][1] -= v;
//        cout<<"BRUH "<<tree[id].a[1][1]<<'\n';
        return;
    }
    lz(id);
    ll mid = (l + r) >> 1;
    update2(id<<1,l,mid,i,v);
    update2(id<<1|1,mid+1,r,i,v);
    tree[id] = tree[id<<1] + tree[id<<1|1];
}
void update3(ll id,ll l,ll r,ll i,ll v){

    if (l > i || r < i)return;

    if (l==r){
        tree[id].a[1][1] += v;
        return;
    }
    lz(id);
    ll mid = (l + r) >> 1;
    update3(id<<1,l,mid,i,v);
    update3(id<<1|1,mid+1,r,i,v);
//    cout<<"UPDATE3 "<<id<<' '<<l<<' '<<r<<endl;
//    cout<<"PRE ";for (ll i = 0;i < 2;i ++)for (ll j = 0;j < 2;j ++)cout<<tree[id].a[i][j]<<' ';
//    cout<<endl;
    tree[id] = tree[id<<1] + tree[id<<1|1];
//    cout<<"AFT ";for (ll i = 0;i < 2;i ++)for (ll j = 0;j < 2;j ++)cout<<tree[id].a[i][j]<<' ';
//    cout<<endl;
}
ll val[MAXQ+100];
int main(){
    ios_base::sync_with_stdio(0);cin.tie(nullptr);
    cin>>q>>L;
    for (ll i = 1;i <= q;i ++){
        cin>>all[i].t>>all[i].x>>all[i].a;
    }
    ll n = 0;
    for (ll i = 1;i <= q;i ++){
        if (all[i].t==1){
            val[++n]=all[i].x;
        }
    }
    sort(val+1,val+1+n);
    n = unique(val+1,val+1+n)-val-1;
    if (n==0){
        for (ll i = 1;i <= q;i ++){
            cout<<0<<'\n';
        }
        return 0;
    }
//    for (ll i = 1;i <= n;i ++){
//        cout<<val[i]<<' ';
//    }
//    cout<<'\n';
    build(1,1,n);
    ll sum = 0;
    for (ll i = 1;i <= q;i ++){
        if (all[i].t==1){update2(1,1,n,lower_bound(val+1,val+1+n,all[i].x)-val,all[i].a);sum += all[i].a;}
        else{
            ll l = lower_bound(val+1,val+1+n,all[i].x-L)-val;
            ll r = upper_bound(val+1,val+1+n,all[i].x+L)-val-1;
            if (l <= r){
//                cout<<i<<' '<<l<<' '<<r<<endl;
                update1(1,1,n,l,r-1,all[i].a);
                update3(1,1,n,r,all[i].a);
            }
        }
        ll res = INF;
        for (ll i = 0;i < 2;i ++)for (ll j = 0;j < 2;j ++)res = min(res,tree[1].a[i][j]);
//        for (ll k = 1;k <= 3;k ++){
//            for (ll i = 0;i < 2;i ++)for (ll j = 0;j < 2;j ++)cout<<tree[k].a[i][j]<<' ';
//            cout<<'\n';
//        }
        cout<<sum+res<<'\n';
    }

}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4440 KB Output is correct
2 Correct 2 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4700 KB Output is correct
5 Correct 2 ms 4444 KB Output is correct
6 Correct 3 ms 4952 KB Output is correct
7 Correct 2 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 2 ms 4444 KB Output is correct
10 Correct 5 ms 4700 KB Output is correct
11 Correct 3 ms 4700 KB Output is correct
12 Correct 4 ms 4956 KB Output is correct
13 Correct 5 ms 4700 KB Output is correct
14 Correct 2 ms 2396 KB Output is correct
15 Correct 2 ms 4444 KB Output is correct
16 Correct 4 ms 4700 KB Output is correct
17 Correct 3 ms 4700 KB Output is correct
18 Correct 3 ms 4700 KB Output is correct
19 Correct 3 ms 4700 KB Output is correct
20 Correct 4 ms 4700 KB Output is correct
21 Correct 4 ms 4628 KB Output is correct
22 Correct 4 ms 4628 KB Output is correct
23 Correct 4 ms 4700 KB Output is correct
24 Correct 3 ms 4700 KB Output is correct
25 Correct 5 ms 4700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 451 ms 31988 KB Output is correct
5 Correct 263 ms 32456 KB Output is correct
6 Correct 555 ms 54704 KB Output is correct
7 Correct 282 ms 32508 KB Output is correct
8 Correct 689 ms 58072 KB Output is correct
9 Correct 576 ms 58552 KB Output is correct
10 Correct 738 ms 58132 KB Output is correct
11 Correct 604 ms 58608 KB Output is correct
12 Correct 241 ms 30396 KB Output is correct
13 Correct 369 ms 47208 KB Output is correct
14 Correct 591 ms 55216 KB Output is correct
15 Correct 601 ms 55140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 263 ms 26452 KB Output is correct
3 Correct 367 ms 47096 KB Output is correct
4 Correct 614 ms 55284 KB Output is correct
5 Correct 629 ms 55304 KB Output is correct
6 Correct 572 ms 51708 KB Output is correct
7 Correct 25 ms 7600 KB Output is correct
8 Correct 249 ms 27828 KB Output is correct
9 Correct 409 ms 34764 KB Output is correct
10 Correct 756 ms 56504 KB Output is correct
11 Correct 819 ms 56552 KB Output is correct
12 Correct 706 ms 56660 KB Output is correct
13 Correct 634 ms 56484 KB Output is correct
14 Correct 706 ms 56652 KB Output is correct
15 Correct 463 ms 56192 KB Output is correct
16 Correct 597 ms 56596 KB Output is correct
17 Correct 880 ms 56860 KB Output is correct
18 Correct 981 ms 56812 KB Output is correct
19 Correct 952 ms 56692 KB Output is correct
20 Correct 902 ms 56636 KB Output is correct
21 Correct 922 ms 56832 KB Output is correct
22 Correct 982 ms 56604 KB Output is correct
23 Correct 926 ms 56656 KB Output is correct
24 Correct 685 ms 56720 KB Output is correct
25 Correct 870 ms 56696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4440 KB Output is correct
2 Correct 2 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4700 KB Output is correct
5 Correct 2 ms 4444 KB Output is correct
6 Correct 3 ms 4952 KB Output is correct
7 Correct 2 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 2 ms 4444 KB Output is correct
10 Correct 5 ms 4700 KB Output is correct
11 Correct 3 ms 4700 KB Output is correct
12 Correct 4 ms 4956 KB Output is correct
13 Correct 5 ms 4700 KB Output is correct
14 Correct 2 ms 2396 KB Output is correct
15 Correct 2 ms 4444 KB Output is correct
16 Correct 4 ms 4700 KB Output is correct
17 Correct 3 ms 4700 KB Output is correct
18 Correct 3 ms 4700 KB Output is correct
19 Correct 3 ms 4700 KB Output is correct
20 Correct 4 ms 4700 KB Output is correct
21 Correct 4 ms 4628 KB Output is correct
22 Correct 4 ms 4628 KB Output is correct
23 Correct 4 ms 4700 KB Output is correct
24 Correct 3 ms 4700 KB Output is correct
25 Correct 5 ms 4700 KB Output is correct
26 Correct 1 ms 4444 KB Output is correct
27 Correct 1 ms 4444 KB Output is correct
28 Correct 1 ms 2396 KB Output is correct
29 Correct 451 ms 31988 KB Output is correct
30 Correct 263 ms 32456 KB Output is correct
31 Correct 555 ms 54704 KB Output is correct
32 Correct 282 ms 32508 KB Output is correct
33 Correct 689 ms 58072 KB Output is correct
34 Correct 576 ms 58552 KB Output is correct
35 Correct 738 ms 58132 KB Output is correct
36 Correct 604 ms 58608 KB Output is correct
37 Correct 241 ms 30396 KB Output is correct
38 Correct 369 ms 47208 KB Output is correct
39 Correct 591 ms 55216 KB Output is correct
40 Correct 601 ms 55140 KB Output is correct
41 Correct 1 ms 4444 KB Output is correct
42 Correct 263 ms 26452 KB Output is correct
43 Correct 367 ms 47096 KB Output is correct
44 Correct 614 ms 55284 KB Output is correct
45 Correct 629 ms 55304 KB Output is correct
46 Correct 572 ms 51708 KB Output is correct
47 Correct 25 ms 7600 KB Output is correct
48 Correct 249 ms 27828 KB Output is correct
49 Correct 409 ms 34764 KB Output is correct
50 Correct 756 ms 56504 KB Output is correct
51 Correct 819 ms 56552 KB Output is correct
52 Correct 706 ms 56660 KB Output is correct
53 Correct 634 ms 56484 KB Output is correct
54 Correct 706 ms 56652 KB Output is correct
55 Correct 463 ms 56192 KB Output is correct
56 Correct 597 ms 56596 KB Output is correct
57 Correct 880 ms 56860 KB Output is correct
58 Correct 981 ms 56812 KB Output is correct
59 Correct 952 ms 56692 KB Output is correct
60 Correct 902 ms 56636 KB Output is correct
61 Correct 922 ms 56832 KB Output is correct
62 Correct 982 ms 56604 KB Output is correct
63 Correct 926 ms 56656 KB Output is correct
64 Correct 685 ms 56720 KB Output is correct
65 Correct 870 ms 56696 KB Output is correct
66 Correct 303 ms 33896 KB Output is correct
67 Correct 437 ms 50416 KB Output is correct
68 Correct 586 ms 54528 KB Output is correct
69 Correct 532 ms 51016 KB Output is correct
70 Correct 517 ms 59220 KB Output is correct
71 Correct 679 ms 59712 KB Output is correct
72 Correct 684 ms 59712 KB Output is correct
73 Correct 723 ms 59600 KB Output is correct
74 Correct 124 ms 25064 KB Output is correct
75 Correct 200 ms 32832 KB Output is correct
76 Correct 803 ms 85132 KB Output is correct
77 Correct 722 ms 79560 KB Output is correct
78 Correct 737 ms 79532 KB Output is correct
79 Correct 807 ms 59804 KB Output is correct
80 Correct 750 ms 79700 KB Output is correct
81 Correct 733 ms 59732 KB Output is correct
82 Correct 728 ms 79720 KB Output is correct
83 Correct 906 ms 60004 KB Output is correct
84 Correct 822 ms 79956 KB Output is correct
85 Correct 931 ms 60100 KB Output is correct
86 Correct 807 ms 79532 KB Output is correct
87 Correct 1026 ms 60024 KB Output is correct
88 Correct 737 ms 79724 KB Output is correct
89 Correct 837 ms 86384 KB Output is correct