Submission #548281

# Submission time Handle Problem Language Result Execution time Memory
548281 2022-04-12T20:59:16 Z duality Ants and Sugar (JOI22_sugar) C++17
100 / 100
3828 ms 258028 KB
#define DEBUG 0

#include <bits/stdc++.h>
using namespace std;

#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}

// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
    public:
        template<typename T>
        _Debug& operator,(T val) {
            cout << val << endl;
            return *this;
        }
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif

// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back

// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;

// ---------- END OF TEMPLATE ----------
#pragma GCC optimize("Ofast")

int T[500000],X[500000],A[500000];
vpii all;
struct node { LLI ans,ss,ee,both,sum; };
node com(node a,node b) {
    node c;
    c.ans = max(a.ans+b.ans,a.ss+b.ee);
    c.ss = max(a.ans+b.ss,a.ss+max(b.both,b.sum));
    c.ee = max(a.ee+b.ans,max(a.both,a.sum)+b.ee);
    c.both = max(max(a.ee+b.ss,a.both+max(b.both,b.sum)),a.sum+b.both);
    c.sum = a.sum+b.sum;
    return c;
}
int on[1500000];
node tree[1 << 22];
LLI lazys[1 << 22],lazye[1 << 22];
int build(int s,int e,int i) {
    if (s == e) {
        tree[i] = (node){0LL,(LLI) -3e18,(LLI) -3e18,(LLI) -3e18,0LL};
        return 0;
    }

    int mid = (s+e) / 2;
    build(s,mid,2*i+1),build(mid+1,e,2*i+2);
    tree[i] = (node){0LL,(LLI) -3e18,(LLI) -3e18,(LLI) -3e18,0LL};
    return 0;
}
int prop(int s,int e,int i) {
    if (lazys[i] != 0) {
        tree[i].ss += lazys[i],tree[i].both += lazys[i];
        if (s != e) lazys[2*i+1] += lazys[i],lazys[2*i+2] += lazys[i];
        lazys[i] = 0;
    }
    if (lazye[i] != 0) {
        tree[i].ee += lazye[i],tree[i].both += lazye[i];
        if (s != e) lazye[2*i+1] += lazye[i],lazye[2*i+2] += lazye[i];
        lazye[i] = 0;
    }
    return 0;
}
int update(int s,int e,int ai,int i,int num) {
    prop(s,e,i);
    if ((s > ai) || (e < ai)) return 0;
    else if (s == e) {
        tree[i].sum += num;
        return 0;
    }

    int mid = (s+e) / 2;
    update(s,mid,ai,2*i+1,num),update(mid+1,e,ai,2*i+2,num);
    tree[i] = com(tree[2*i+1],tree[2*i+2]);
    //cout<<"tree["<<s<<","<<e<<"]:"<<tree[i].ss<<","<<tree[i].ee<<","<<tree[i].ans<<","<<tree[i].both<<","<<tree[i].sum<<endl;
    //cout<<"tree["<<s<<","<<mid<<"]:"<<tree[2*i+1].ss<<","<<tree[2*i+1].ee<<","<<tree[2*i+1].ans<<","<<tree[2*i+1].both<<","<<tree[2*i+1].sum<<endl;
    //cout<<"tree["<<mid+1<<","<<e<<"]:"<<tree[2*i+2].ss<<","<<tree[2*i+2].ee<<","<<tree[2*i+2].ans<<","<<tree[2*i+2].both<<","<<tree[2*i+2].sum<<endl;
    return 0;
}
int activate(int s,int e,int ai,int i) {
    prop(s,e,i);
    if ((s > ai) || (e < ai)) return 0;
    else if (s == e) {
        //cout<<"activate tree["<<s<<"]: "<<tree[i].ss<<","<<tree[i].ee<<endl;
        if (all[s].second == -1) tree[i].ss += (LLI) 3e18;
        else tree[i].ee += (LLI) 3e18;
        //cout<<s<<": ss="<<tree[i].ss<<endl;
        //cout<<s<<": ee="<<tree[i].ee<<endl;
        return 0;
    }

    int mid = (s+e) / 2;
    activate(s,mid,ai,2*i+1),activate(mid+1,e,ai,2*i+2);
    tree[i] = com(tree[2*i+1],tree[2*i+2]);
    return 0;
}
int update2(int s,int e,int as,int ae,int i,int t,int num) {
    prop(s,e,i);
    if ((s > ae) || (e < as)) return 0;
    else if ((s >= as) && (e <= ae)) {
        if (t == 0) lazys[i] += num;
        else lazye[i] += num;
        prop(s,e,i);
        //cout<<"tree["<<s<<"]: "<<tree[i].ss<<","<<tree[i].ee<<endl;
        return 0;
    }

    int mid = (s+e) / 2;
    update2(s,mid,as,ae,2*i+1,t,num),update2(mid+1,e,as,ae,2*i+2,t,num);
    tree[i] = com(tree[2*i+1],tree[2*i+2]);
    return 0;
}
int main() {
    int i;
    int Q,L;
    scanf("%d %d",&Q,&L);
    for (i = 0; i < Q; i++) {
        scanf("%d %d %d",&T[i],&X[i],&A[i]);
        if (T[i] == 1) all.pb(mp(X[i]-L,-1)),all.pb(mp(X[i],0)),all.pb(mp(X[i]+L,1));
        else all.pb(mp(X[i],0));
    }

    LLI sum = 0;
    sort(all.begin(),all.end());
    all.resize(unique(all.begin(),all.end())-all.begin());
    build(0,all.size()-1,0);
    //for (pii p: all) cout<<p.first<<","<<p.second<<" ";
    //cout<<endl;
    for (i = 0; i < Q; i++) {
        if (T[i] == 1) {
            int p = lower_bound(all.begin(),all.end(),mp(X[i]-L,-1))-all.begin();
            int q = lower_bound(all.begin(),all.end(),mp(X[i],0))-all.begin();
            int r = lower_bound(all.begin(),all.end(),mp(X[i]+L,1))-all.begin();
            if (!on[p]) activate(0,all.size()-1,p,0),on[p] = 1;
            if (!on[r]) activate(0,all.size()-1,r,0),on[r] = 1;
            update(0,all.size()-1,q,0,A[i]);
            update2(0,all.size()-1,p+1,q,0,0,-A[i]);
            update2(0,all.size()-1,q,r-1,0,1,-A[i]);
            //cout<<p<<";"<<q<<";"<<r<<endl;
            sum += A[i];
        }
        else {
            int p = lower_bound(all.begin(),all.end(),mp(X[i],0))-all.begin();
            update(0,all.size()-1,p,0,-A[i]);
        }
        printf("%lld\n",sum-tree[0].ans);
    }

    return 0;
}

Compilation message

sugar.cpp: In function 'int main()':
sugar.cpp:150:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  150 |     scanf("%d %d",&Q,&L);
      |     ~~~~~^~~~~~~~~~~~~~~
sugar.cpp:152:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  152 |         scanf("%d %d %d",&T[i],&X[i],&A[i]);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 312 KB Output is correct
6 Correct 4 ms 980 KB Output is correct
7 Correct 3 ms 916 KB Output is correct
8 Correct 2 ms 596 KB Output is correct
9 Correct 2 ms 596 KB Output is correct
10 Correct 5 ms 1456 KB Output is correct
11 Correct 5 ms 1480 KB Output is correct
12 Correct 6 ms 1480 KB Output is correct
13 Correct 6 ms 1452 KB Output is correct
14 Correct 3 ms 712 KB Output is correct
15 Correct 3 ms 968 KB Output is correct
16 Correct 8 ms 2516 KB Output is correct
17 Correct 9 ms 2388 KB Output is correct
18 Correct 9 ms 2388 KB Output is correct
19 Correct 8 ms 1448 KB Output is correct
20 Correct 10 ms 2484 KB Output is correct
21 Correct 8 ms 1484 KB Output is correct
22 Correct 14 ms 2388 KB Output is correct
23 Correct 7 ms 1492 KB Output is correct
24 Correct 11 ms 2388 KB Output is correct
25 Correct 7 ms 1492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 939 ms 73832 KB Output is correct
5 Correct 426 ms 68944 KB Output is correct
6 Correct 1221 ms 124440 KB Output is correct
7 Correct 466 ms 69044 KB Output is correct
8 Correct 1425 ms 136412 KB Output is correct
9 Correct 971 ms 138284 KB Output is correct
10 Correct 1377 ms 136512 KB Output is correct
11 Correct 980 ms 138804 KB Output is correct
12 Correct 431 ms 67020 KB Output is correct
13 Correct 589 ms 126220 KB Output is correct
14 Correct 981 ms 135352 KB Output is correct
15 Correct 1002 ms 135332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 442 ms 67436 KB Output is correct
3 Correct 603 ms 126416 KB Output is correct
4 Correct 962 ms 135108 KB Output is correct
5 Correct 1018 ms 134896 KB Output is correct
6 Correct 1145 ms 131732 KB Output is correct
7 Correct 67 ms 9132 KB Output is correct
8 Correct 554 ms 67032 KB Output is correct
9 Correct 861 ms 71216 KB Output is correct
10 Correct 1566 ms 136940 KB Output is correct
11 Correct 1590 ms 136612 KB Output is correct
12 Correct 1573 ms 136236 KB Output is correct
13 Correct 1508 ms 135868 KB Output is correct
14 Correct 1516 ms 136196 KB Output is correct
15 Correct 1491 ms 131212 KB Output is correct
16 Correct 1519 ms 135312 KB Output is correct
17 Correct 1827 ms 138136 KB Output is correct
18 Correct 2200 ms 138472 KB Output is correct
19 Correct 2122 ms 138344 KB Output is correct
20 Correct 2129 ms 138324 KB Output is correct
21 Correct 2377 ms 138308 KB Output is correct
22 Correct 2424 ms 137952 KB Output is correct
23 Correct 2392 ms 137592 KB Output is correct
24 Correct 2283 ms 130220 KB Output is correct
25 Correct 2289 ms 134340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 312 KB Output is correct
6 Correct 4 ms 980 KB Output is correct
7 Correct 3 ms 916 KB Output is correct
8 Correct 2 ms 596 KB Output is correct
9 Correct 2 ms 596 KB Output is correct
10 Correct 5 ms 1456 KB Output is correct
11 Correct 5 ms 1480 KB Output is correct
12 Correct 6 ms 1480 KB Output is correct
13 Correct 6 ms 1452 KB Output is correct
14 Correct 3 ms 712 KB Output is correct
15 Correct 3 ms 968 KB Output is correct
16 Correct 8 ms 2516 KB Output is correct
17 Correct 9 ms 2388 KB Output is correct
18 Correct 9 ms 2388 KB Output is correct
19 Correct 8 ms 1448 KB Output is correct
20 Correct 10 ms 2484 KB Output is correct
21 Correct 8 ms 1484 KB Output is correct
22 Correct 14 ms 2388 KB Output is correct
23 Correct 7 ms 1492 KB Output is correct
24 Correct 11 ms 2388 KB Output is correct
25 Correct 7 ms 1492 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 0 ms 340 KB Output is correct
29 Correct 939 ms 73832 KB Output is correct
30 Correct 426 ms 68944 KB Output is correct
31 Correct 1221 ms 124440 KB Output is correct
32 Correct 466 ms 69044 KB Output is correct
33 Correct 1425 ms 136412 KB Output is correct
34 Correct 971 ms 138284 KB Output is correct
35 Correct 1377 ms 136512 KB Output is correct
36 Correct 980 ms 138804 KB Output is correct
37 Correct 431 ms 67020 KB Output is correct
38 Correct 589 ms 126220 KB Output is correct
39 Correct 981 ms 135352 KB Output is correct
40 Correct 1002 ms 135332 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 442 ms 67436 KB Output is correct
43 Correct 603 ms 126416 KB Output is correct
44 Correct 962 ms 135108 KB Output is correct
45 Correct 1018 ms 134896 KB Output is correct
46 Correct 1145 ms 131732 KB Output is correct
47 Correct 67 ms 9132 KB Output is correct
48 Correct 554 ms 67032 KB Output is correct
49 Correct 861 ms 71216 KB Output is correct
50 Correct 1566 ms 136940 KB Output is correct
51 Correct 1590 ms 136612 KB Output is correct
52 Correct 1573 ms 136236 KB Output is correct
53 Correct 1508 ms 135868 KB Output is correct
54 Correct 1516 ms 136196 KB Output is correct
55 Correct 1491 ms 131212 KB Output is correct
56 Correct 1519 ms 135312 KB Output is correct
57 Correct 1827 ms 138136 KB Output is correct
58 Correct 2200 ms 138472 KB Output is correct
59 Correct 2122 ms 138344 KB Output is correct
60 Correct 2129 ms 138324 KB Output is correct
61 Correct 2377 ms 138308 KB Output is correct
62 Correct 2424 ms 137952 KB Output is correct
63 Correct 2392 ms 137592 KB Output is correct
64 Correct 2283 ms 130220 KB Output is correct
65 Correct 2289 ms 134340 KB Output is correct
66 Correct 783 ms 70652 KB Output is correct
67 Correct 1053 ms 130872 KB Output is correct
68 Correct 1206 ms 134332 KB Output is correct
69 Correct 1101 ms 133380 KB Output is correct
70 Correct 1454 ms 136836 KB Output is correct
71 Correct 1508 ms 138196 KB Output is correct
72 Correct 1495 ms 139256 KB Output is correct
73 Correct 1561 ms 139256 KB Output is correct
74 Correct 699 ms 53032 KB Output is correct
75 Correct 760 ms 73972 KB Output is correct
76 Correct 2270 ms 258028 KB Output is correct
77 Correct 2198 ms 251728 KB Output is correct
78 Correct 2412 ms 253468 KB Output is correct
79 Correct 1552 ms 139684 KB Output is correct
80 Correct 2918 ms 255180 KB Output is correct
81 Correct 1502 ms 138984 KB Output is correct
82 Correct 3466 ms 255456 KB Output is correct
83 Correct 2045 ms 141088 KB Output is correct
84 Correct 3598 ms 255052 KB Output is correct
85 Correct 2202 ms 140848 KB Output is correct
86 Correct 3828 ms 253424 KB Output is correct
87 Correct 2196 ms 150156 KB Output is correct
88 Correct 3801 ms 253272 KB Output is correct
89 Correct 2959 ms 250768 KB Output is correct