Submission #722778

# Submission time Handle Problem Language Result Execution time Memory
722778 2023-04-12T20:29:40 Z fatemetmhr Ants and Sugar (JOI22_sugar) C++17
100 / 100
1945 ms 120552 KB
// ~ Be Name Khoda ~

#include <bits/stdc++.h>
//#pragma GCC optimize ("Ofast")
#pragma GCC target("avx2")
#pragma GCC optimize("unroll-loops,Ofast")

using namespace std;

typedef long long ll;

#define pb       push_back
#define mp       make_pair
#define all(x)   x.begin(), x.end()
#define fi       first
#define se       second

const int maxn  =  5e5   + 10;
const int maxn5 =  5e5   + 10;
const int maxnt =  2e6   + 10;
const int maxn3 =  1e3   + 10;
const int mod   =  1e9   +  7;
const int lg    =  19;
const ll  inf   =  1e18;

int lim, m, a[maxn5], ty[maxn5], x[maxn5];
vector <int> av;
ll sum = 0;

struct segst{

    ll dp[2][2], lazy, tomerge;
    segst(){
        dp[0][1] = dp[1][0] = dp[0][0] = -inf;
        dp[1][1] = 0;
        lazy = tomerge = 0;
    }

} seg[maxnt];

inline void output(int v){
    cout << "checking " << v << ' ' << seg[v].tomerge << ' ' << seg[v].dp[0][0] << ' ' << seg[v].dp[0][1];
    cout << ' ' << seg[v].dp[1][0] << ' ' << seg[v].dp[1][1] << '\n';
}

inline segst operator +(segst a, segst b){
    segst ret;
    ret.tomerge = b.tomerge;
    ret.dp[1][1] = -inf;
    for(int i = 0; i < 2; i++){
        ret.dp[i][0] = max({ret.dp[i][0], a.dp[i][0], a.dp[i][1]});
        ret.dp[0][i] = max({ret.dp[0][i], b.dp[0][i], b.dp[1][i]});
        for(int j = 0; j < 2; j++)
            for(int k = 0; k < 2; k++)
                for(int l = 0; l < 2; l++)
                    ret.dp[i][j] = max(ret.dp[i][j], a.dp[i][k] + b.dp[l][j] + ((k && l) * a.tomerge));
    }
    return ret;
}

inline void lahaz(int v, ll val){
    seg[v].lazy += val;
    seg[v].tomerge -= val;
    for(int i = 0; i < 2; i++) for(int j = 0; j < 2; j++)
        seg[v].dp[i][j] += val;
}

inline void shift(int v){
    lahaz(v * 2, seg[v].lazy);
    lahaz(v * 2 + 1, seg[v].lazy);
    seg[v].lazy = 0;
}


void add(int l, int r, int lq, int rq, int val1, int val2, int v){
    if(rq <= l || r <= lq)
        return;
    if(lq <= l && r <= rq){
        if(r - l == 1){
            seg[v].tomerge += val2;
            seg[v].dp[1][1] += val1;
        }
        else
            lahaz(v, val1);
        return;
    }
    int mid = (l + r) >> 1; shift(v);
    add(l, mid, lq, rq, val1, val2, v * 2);
    add(mid, r, lq, rq, val1, val2, v * 2 + 1);
    seg[v] = seg[v * 2] + seg[v * 2 + 1];
}

void upd(int l, int r, int id, int val, int v){
    if(r - l == 1){
        seg[v].dp[1][1] += val;
        return;
    }
    int mid = (l + r) >> 1; shift(v);
    if(id < mid)
        upd(l, mid, id, val, v * 2);
    else
        upd(mid, r, id, val, v * 2 + 1);
    seg[v] = seg[v * 2] + seg[v * 2 + 1];
    //output(v);
}

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(0);

    int n; cin >> n >> lim;
    for(int i = 0; i < n; i++){
        cin >> ty[i] >> x[i] >> a[i];
        ty[i]--;
        av.pb(x[i]);
    }
    sort(all(av));
    av.resize(unique(all(av)) - av.begin());
    m = av.size();
    sum = 0;
    for(int i = 0; i < n; i++){
        int pt = lower_bound(all(av), x[i]) - av.begin();
        if(ty[i]){
            sum += a[i];
            upd(0, m, pt, a[i], 1);
        }
        else{
            int l = lower_bound(all(av), x[i] - lim) - av.begin();
            int r = upper_bound(all(av), x[i] + lim) - av.begin();
            add(0, m, r - 1, r, -a[i], 0, 1);
            if(l < r - 1)
                add(0, m, l, r - 1, -a[i], a[i], 1);
        }
        cout << sum - max({0LL, seg[1].dp[0][0], seg[1].dp[1][0], seg[1].dp[0][1], seg[1].dp[1][1]}) << '\n';
    }
}

/*
20 268886972
1 984472666 733463744
1 478477245 94817772
1 242536956 330762563
1 65794782 319137646
1 320548477 937296140
1 815011370 938193848
1 565184190 917533785
1 245417414 534089975
1 529908772 977043962
1 603891865 700935654
2 167042244 479827216
2 173921297 798343455
2 916159596 810126726
2 999299355 465535307
2 965968070 501768990
2 936073643 174976034
2 832859952 778072072
2 955489596 704853861
2 246733786 382428992
2 227669861 390905006
*/
# Verdict Execution time Memory Grader output
1 Correct 41 ms 94156 KB Output is correct
2 Correct 42 ms 94200 KB Output is correct
3 Correct 42 ms 94152 KB Output is correct
4 Correct 40 ms 94244 KB Output is correct
5 Correct 41 ms 94168 KB Output is correct
6 Correct 43 ms 94284 KB Output is correct
7 Correct 44 ms 94272 KB Output is correct
8 Correct 50 ms 94280 KB Output is correct
9 Correct 41 ms 94264 KB Output is correct
10 Correct 44 ms 94336 KB Output is correct
11 Correct 46 ms 94436 KB Output is correct
12 Correct 48 ms 94364 KB Output is correct
13 Correct 45 ms 94320 KB Output is correct
14 Correct 51 ms 94300 KB Output is correct
15 Correct 55 ms 94408 KB Output is correct
16 Correct 46 ms 94388 KB Output is correct
17 Correct 59 ms 94288 KB Output is correct
18 Correct 46 ms 94500 KB Output is correct
19 Correct 42 ms 94412 KB Output is correct
20 Correct 52 ms 94376 KB Output is correct
21 Correct 51 ms 94304 KB Output is correct
22 Correct 59 ms 94288 KB Output is correct
23 Correct 54 ms 94364 KB Output is correct
24 Correct 46 ms 94288 KB Output is correct
25 Correct 48 ms 94484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 94256 KB Output is correct
2 Correct 39 ms 94160 KB Output is correct
3 Correct 39 ms 94196 KB Output is correct
4 Correct 816 ms 110368 KB Output is correct
5 Correct 480 ms 106056 KB Output is correct
6 Correct 907 ms 114812 KB Output is correct
7 Correct 399 ms 106260 KB Output is correct
8 Correct 1246 ms 118620 KB Output is correct
9 Correct 828 ms 119264 KB Output is correct
10 Correct 1145 ms 118664 KB Output is correct
11 Correct 831 ms 119308 KB Output is correct
12 Correct 370 ms 103788 KB Output is correct
13 Correct 492 ms 107456 KB Output is correct
14 Correct 812 ms 115784 KB Output is correct
15 Correct 847 ms 115768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 94156 KB Output is correct
2 Correct 388 ms 103860 KB Output is correct
3 Correct 499 ms 107396 KB Output is correct
4 Correct 827 ms 115960 KB Output is correct
5 Correct 783 ms 115772 KB Output is correct
6 Correct 962 ms 111500 KB Output is correct
7 Correct 84 ms 95652 KB Output is correct
8 Correct 631 ms 102932 KB Output is correct
9 Correct 746 ms 107088 KB Output is correct
10 Correct 1227 ms 117008 KB Output is correct
11 Correct 1270 ms 117092 KB Output is correct
12 Correct 1411 ms 117012 KB Output is correct
13 Correct 1143 ms 117116 KB Output is correct
14 Correct 1279 ms 117092 KB Output is correct
15 Correct 1190 ms 116924 KB Output is correct
16 Correct 1302 ms 117072 KB Output is correct
17 Correct 1406 ms 117136 KB Output is correct
18 Correct 1419 ms 117164 KB Output is correct
19 Correct 1292 ms 117172 KB Output is correct
20 Correct 1310 ms 117220 KB Output is correct
21 Correct 1356 ms 117268 KB Output is correct
22 Correct 1403 ms 117108 KB Output is correct
23 Correct 1290 ms 117288 KB Output is correct
24 Correct 1040 ms 117108 KB Output is correct
25 Correct 1138 ms 117216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 94156 KB Output is correct
2 Correct 42 ms 94200 KB Output is correct
3 Correct 42 ms 94152 KB Output is correct
4 Correct 40 ms 94244 KB Output is correct
5 Correct 41 ms 94168 KB Output is correct
6 Correct 43 ms 94284 KB Output is correct
7 Correct 44 ms 94272 KB Output is correct
8 Correct 50 ms 94280 KB Output is correct
9 Correct 41 ms 94264 KB Output is correct
10 Correct 44 ms 94336 KB Output is correct
11 Correct 46 ms 94436 KB Output is correct
12 Correct 48 ms 94364 KB Output is correct
13 Correct 45 ms 94320 KB Output is correct
14 Correct 51 ms 94300 KB Output is correct
15 Correct 55 ms 94408 KB Output is correct
16 Correct 46 ms 94388 KB Output is correct
17 Correct 59 ms 94288 KB Output is correct
18 Correct 46 ms 94500 KB Output is correct
19 Correct 42 ms 94412 KB Output is correct
20 Correct 52 ms 94376 KB Output is correct
21 Correct 51 ms 94304 KB Output is correct
22 Correct 59 ms 94288 KB Output is correct
23 Correct 54 ms 94364 KB Output is correct
24 Correct 46 ms 94288 KB Output is correct
25 Correct 48 ms 94484 KB Output is correct
26 Correct 39 ms 94256 KB Output is correct
27 Correct 39 ms 94160 KB Output is correct
28 Correct 39 ms 94196 KB Output is correct
29 Correct 816 ms 110368 KB Output is correct
30 Correct 480 ms 106056 KB Output is correct
31 Correct 907 ms 114812 KB Output is correct
32 Correct 399 ms 106260 KB Output is correct
33 Correct 1246 ms 118620 KB Output is correct
34 Correct 828 ms 119264 KB Output is correct
35 Correct 1145 ms 118664 KB Output is correct
36 Correct 831 ms 119308 KB Output is correct
37 Correct 370 ms 103788 KB Output is correct
38 Correct 492 ms 107456 KB Output is correct
39 Correct 812 ms 115784 KB Output is correct
40 Correct 847 ms 115768 KB Output is correct
41 Correct 40 ms 94156 KB Output is correct
42 Correct 388 ms 103860 KB Output is correct
43 Correct 499 ms 107396 KB Output is correct
44 Correct 827 ms 115960 KB Output is correct
45 Correct 783 ms 115772 KB Output is correct
46 Correct 962 ms 111500 KB Output is correct
47 Correct 84 ms 95652 KB Output is correct
48 Correct 631 ms 102932 KB Output is correct
49 Correct 746 ms 107088 KB Output is correct
50 Correct 1227 ms 117008 KB Output is correct
51 Correct 1270 ms 117092 KB Output is correct
52 Correct 1411 ms 117012 KB Output is correct
53 Correct 1143 ms 117116 KB Output is correct
54 Correct 1279 ms 117092 KB Output is correct
55 Correct 1190 ms 116924 KB Output is correct
56 Correct 1302 ms 117072 KB Output is correct
57 Correct 1406 ms 117136 KB Output is correct
58 Correct 1419 ms 117164 KB Output is correct
59 Correct 1292 ms 117172 KB Output is correct
60 Correct 1310 ms 117220 KB Output is correct
61 Correct 1356 ms 117268 KB Output is correct
62 Correct 1403 ms 117108 KB Output is correct
63 Correct 1290 ms 117288 KB Output is correct
64 Correct 1040 ms 117108 KB Output is correct
65 Correct 1138 ms 117216 KB Output is correct
66 Correct 615 ms 108336 KB Output is correct
67 Correct 721 ms 111132 KB Output is correct
68 Correct 868 ms 114224 KB Output is correct
69 Correct 809 ms 112404 KB Output is correct
70 Correct 1036 ms 119928 KB Output is correct
71 Correct 1111 ms 120180 KB Output is correct
72 Correct 1152 ms 120264 KB Output is correct
73 Correct 1170 ms 120316 KB Output is correct
74 Correct 833 ms 113872 KB Output is correct
75 Correct 956 ms 119468 KB Output is correct
76 Correct 1567 ms 119444 KB Output is correct
77 Correct 1416 ms 113860 KB Output is correct
78 Correct 1634 ms 113840 KB Output is correct
79 Correct 1384 ms 120204 KB Output is correct
80 Correct 1945 ms 113924 KB Output is correct
81 Correct 1304 ms 120112 KB Output is correct
82 Correct 1856 ms 113888 KB Output is correct
83 Correct 1530 ms 120468 KB Output is correct
84 Correct 1832 ms 113936 KB Output is correct
85 Correct 1361 ms 120360 KB Output is correct
86 Correct 1931 ms 113792 KB Output is correct
87 Correct 1407 ms 120552 KB Output is correct
88 Correct 1649 ms 114068 KB Output is correct
89 Correct 1096 ms 120236 KB Output is correct