#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 ----------
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(max(a.ans+b.ans,a.ss+b.ee),(LLI) -3e18);
c.ss = max(max(max(a.ans+b.ss,a.ss+b.both),a.ss+b.sum),(LLI) -3e18);
c.ee = max(max(max(a.ee+b.ans,a.both+b.ee),a.sum+b.ee),(LLI) -3e18);
c.both = max(max(max(a.ee+b.ss,a.both+b.both),max(a.both+b.sum,a.sum+b.both)),(LLI) -3e18);
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) && (s == e)) {
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:149:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
149 | scanf("%d %d",&Q,&L);
| ~~~~~^~~~~~~~~~~~~~~
sugar.cpp:151:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
151 | 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 |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
312 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
5 ms |
852 KB |
Output is correct |
7 |
Correct |
5 ms |
908 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
3 ms |
596 KB |
Output is correct |
10 |
Correct |
7 ms |
1428 KB |
Output is correct |
11 |
Correct |
7 ms |
1364 KB |
Output is correct |
12 |
Correct |
10 ms |
1416 KB |
Output is correct |
13 |
Correct |
8 ms |
1456 KB |
Output is correct |
14 |
Correct |
5 ms |
724 KB |
Output is correct |
15 |
Correct |
4 ms |
852 KB |
Output is correct |
16 |
Correct |
14 ms |
2376 KB |
Output is correct |
17 |
Correct |
13 ms |
2260 KB |
Output is correct |
18 |
Correct |
12 ms |
2304 KB |
Output is correct |
19 |
Correct |
8 ms |
1344 KB |
Output is correct |
20 |
Correct |
25 ms |
2368 KB |
Output is correct |
21 |
Correct |
12 ms |
1456 KB |
Output is correct |
22 |
Correct |
176 ms |
2356 KB |
Output is correct |
23 |
Correct |
52 ms |
1448 KB |
Output is correct |
24 |
Correct |
659 ms |
2244 KB |
Output is correct |
25 |
Correct |
220 ms |
1388 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 |
316 KB |
Output is correct |
4 |
Correct |
1433 ms |
71552 KB |
Output is correct |
5 |
Correct |
687 ms |
69816 KB |
Output is correct |
6 |
Correct |
1652 ms |
123112 KB |
Output is correct |
7 |
Correct |
671 ms |
69848 KB |
Output is correct |
8 |
Correct |
2008 ms |
134864 KB |
Output is correct |
9 |
Correct |
1486 ms |
135852 KB |
Output is correct |
10 |
Correct |
1956 ms |
134088 KB |
Output is correct |
11 |
Correct |
1436 ms |
135844 KB |
Output is correct |
12 |
Correct |
629 ms |
66864 KB |
Output is correct |
13 |
Correct |
894 ms |
124028 KB |
Output is correct |
14 |
Correct |
1481 ms |
132568 KB |
Output is correct |
15 |
Correct |
1440 ms |
132440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
635 ms |
67356 KB |
Output is correct |
3 |
Correct |
910 ms |
125732 KB |
Output is correct |
4 |
Correct |
1483 ms |
137492 KB |
Output is correct |
5 |
Correct |
1460 ms |
137732 KB |
Output is correct |
6 |
Correct |
1675 ms |
130880 KB |
Output is correct |
7 |
Correct |
117 ms |
8984 KB |
Output is correct |
8 |
Correct |
794 ms |
65860 KB |
Output is correct |
9 |
Correct |
1240 ms |
71728 KB |
Output is correct |
10 |
Correct |
2454 ms |
138968 KB |
Output is correct |
11 |
Correct |
2716 ms |
138680 KB |
Output is correct |
12 |
Correct |
2782 ms |
138792 KB |
Output is correct |
13 |
Correct |
2495 ms |
138792 KB |
Output is correct |
14 |
Correct |
2643 ms |
139340 KB |
Output is correct |
15 |
Correct |
2482 ms |
138688 KB |
Output is correct |
16 |
Correct |
2557 ms |
139044 KB |
Output is correct |
17 |
Execution timed out |
4110 ms |
137712 KB |
Time limit exceeded |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
312 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
5 ms |
852 KB |
Output is correct |
7 |
Correct |
5 ms |
908 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
3 ms |
596 KB |
Output is correct |
10 |
Correct |
7 ms |
1428 KB |
Output is correct |
11 |
Correct |
7 ms |
1364 KB |
Output is correct |
12 |
Correct |
10 ms |
1416 KB |
Output is correct |
13 |
Correct |
8 ms |
1456 KB |
Output is correct |
14 |
Correct |
5 ms |
724 KB |
Output is correct |
15 |
Correct |
4 ms |
852 KB |
Output is correct |
16 |
Correct |
14 ms |
2376 KB |
Output is correct |
17 |
Correct |
13 ms |
2260 KB |
Output is correct |
18 |
Correct |
12 ms |
2304 KB |
Output is correct |
19 |
Correct |
8 ms |
1344 KB |
Output is correct |
20 |
Correct |
25 ms |
2368 KB |
Output is correct |
21 |
Correct |
12 ms |
1456 KB |
Output is correct |
22 |
Correct |
176 ms |
2356 KB |
Output is correct |
23 |
Correct |
52 ms |
1448 KB |
Output is correct |
24 |
Correct |
659 ms |
2244 KB |
Output is correct |
25 |
Correct |
220 ms |
1388 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
316 KB |
Output is correct |
29 |
Correct |
1433 ms |
71552 KB |
Output is correct |
30 |
Correct |
687 ms |
69816 KB |
Output is correct |
31 |
Correct |
1652 ms |
123112 KB |
Output is correct |
32 |
Correct |
671 ms |
69848 KB |
Output is correct |
33 |
Correct |
2008 ms |
134864 KB |
Output is correct |
34 |
Correct |
1486 ms |
135852 KB |
Output is correct |
35 |
Correct |
1956 ms |
134088 KB |
Output is correct |
36 |
Correct |
1436 ms |
135844 KB |
Output is correct |
37 |
Correct |
629 ms |
66864 KB |
Output is correct |
38 |
Correct |
894 ms |
124028 KB |
Output is correct |
39 |
Correct |
1481 ms |
132568 KB |
Output is correct |
40 |
Correct |
1440 ms |
132440 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
635 ms |
67356 KB |
Output is correct |
43 |
Correct |
910 ms |
125732 KB |
Output is correct |
44 |
Correct |
1483 ms |
137492 KB |
Output is correct |
45 |
Correct |
1460 ms |
137732 KB |
Output is correct |
46 |
Correct |
1675 ms |
130880 KB |
Output is correct |
47 |
Correct |
117 ms |
8984 KB |
Output is correct |
48 |
Correct |
794 ms |
65860 KB |
Output is correct |
49 |
Correct |
1240 ms |
71728 KB |
Output is correct |
50 |
Correct |
2454 ms |
138968 KB |
Output is correct |
51 |
Correct |
2716 ms |
138680 KB |
Output is correct |
52 |
Correct |
2782 ms |
138792 KB |
Output is correct |
53 |
Correct |
2495 ms |
138792 KB |
Output is correct |
54 |
Correct |
2643 ms |
139340 KB |
Output is correct |
55 |
Correct |
2482 ms |
138688 KB |
Output is correct |
56 |
Correct |
2557 ms |
139044 KB |
Output is correct |
57 |
Execution timed out |
4110 ms |
137712 KB |
Time limit exceeded |
58 |
Halted |
0 ms |
0 KB |
- |