#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(max(a.ans+b.ans,a.ss+b.ee),(LLI) -3e18);
c.ss = max(max(a.ans+b.ss,a.ss+max(b.both,b.sum)),(LLI) -3e18);
c.ee = max(max(a.ee+b.ans,max(a.both,a.sum)+b.ee),(LLI) -3e18);
c.both = max(max(max(a.ee+b.ss,a.both+max(b.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)) {
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]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
320 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 |
316 KB |
Output is correct |
6 |
Correct |
4 ms |
980 KB |
Output is correct |
7 |
Correct |
4 ms |
912 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
6 ms |
1352 KB |
Output is correct |
11 |
Correct |
6 ms |
1492 KB |
Output is correct |
12 |
Correct |
6 ms |
1452 KB |
Output is correct |
13 |
Correct |
7 ms |
1524 KB |
Output is correct |
14 |
Correct |
3 ms |
724 KB |
Output is correct |
15 |
Correct |
3 ms |
852 KB |
Output is correct |
16 |
Correct |
9 ms |
2516 KB |
Output is correct |
17 |
Correct |
11 ms |
2676 KB |
Output is correct |
18 |
Correct |
9 ms |
2388 KB |
Output is correct |
19 |
Correct |
6 ms |
1492 KB |
Output is correct |
20 |
Correct |
10 ms |
2388 KB |
Output is correct |
21 |
Correct |
7 ms |
1492 KB |
Output is correct |
22 |
Correct |
13 ms |
2388 KB |
Output is correct |
23 |
Correct |
7 ms |
1492 KB |
Output is correct |
24 |
Correct |
12 ms |
2372 KB |
Output is correct |
25 |
Correct |
8 ms |
1364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
312 KB |
Output is correct |
2 |
Correct |
1 ms |
308 KB |
Output is correct |
3 |
Correct |
1 ms |
312 KB |
Output is correct |
4 |
Correct |
1016 ms |
75172 KB |
Output is correct |
5 |
Correct |
501 ms |
69720 KB |
Output is correct |
6 |
Correct |
1223 ms |
126336 KB |
Output is correct |
7 |
Correct |
505 ms |
69680 KB |
Output is correct |
8 |
Correct |
1543 ms |
138112 KB |
Output is correct |
9 |
Correct |
1050 ms |
140064 KB |
Output is correct |
10 |
Correct |
1470 ms |
137804 KB |
Output is correct |
11 |
Correct |
1038 ms |
139552 KB |
Output is correct |
12 |
Correct |
454 ms |
67380 KB |
Output is correct |
13 |
Correct |
668 ms |
127352 KB |
Output is correct |
14 |
Correct |
1047 ms |
135460 KB |
Output is correct |
15 |
Correct |
1038 ms |
135332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
470 ms |
66856 KB |
Output is correct |
3 |
Correct |
672 ms |
125992 KB |
Output is correct |
4 |
Correct |
1058 ms |
134848 KB |
Output is correct |
5 |
Correct |
1053 ms |
134572 KB |
Output is correct |
6 |
Correct |
1193 ms |
131552 KB |
Output is correct |
7 |
Correct |
67 ms |
9144 KB |
Output is correct |
8 |
Correct |
589 ms |
66816 KB |
Output is correct |
9 |
Correct |
889 ms |
70948 KB |
Output is correct |
10 |
Correct |
1635 ms |
136500 KB |
Output is correct |
11 |
Correct |
1707 ms |
136892 KB |
Output is correct |
12 |
Correct |
1635 ms |
136532 KB |
Output is correct |
13 |
Correct |
1607 ms |
136292 KB |
Output is correct |
14 |
Correct |
1683 ms |
136612 KB |
Output is correct |
15 |
Correct |
1534 ms |
131500 KB |
Output is correct |
16 |
Correct |
1617 ms |
135580 KB |
Output is correct |
17 |
Correct |
1951 ms |
138316 KB |
Output is correct |
18 |
Correct |
2220 ms |
139208 KB |
Output is correct |
19 |
Correct |
2335 ms |
139244 KB |
Output is correct |
20 |
Correct |
2129 ms |
139256 KB |
Output is correct |
21 |
Correct |
2325 ms |
139372 KB |
Output is correct |
22 |
Correct |
2432 ms |
138844 KB |
Output is correct |
23 |
Correct |
2381 ms |
138520 KB |
Output is correct |
24 |
Correct |
2290 ms |
130992 KB |
Output is correct |
25 |
Correct |
2439 ms |
135272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
320 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 |
316 KB |
Output is correct |
6 |
Correct |
4 ms |
980 KB |
Output is correct |
7 |
Correct |
4 ms |
912 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
6 ms |
1352 KB |
Output is correct |
11 |
Correct |
6 ms |
1492 KB |
Output is correct |
12 |
Correct |
6 ms |
1452 KB |
Output is correct |
13 |
Correct |
7 ms |
1524 KB |
Output is correct |
14 |
Correct |
3 ms |
724 KB |
Output is correct |
15 |
Correct |
3 ms |
852 KB |
Output is correct |
16 |
Correct |
9 ms |
2516 KB |
Output is correct |
17 |
Correct |
11 ms |
2676 KB |
Output is correct |
18 |
Correct |
9 ms |
2388 KB |
Output is correct |
19 |
Correct |
6 ms |
1492 KB |
Output is correct |
20 |
Correct |
10 ms |
2388 KB |
Output is correct |
21 |
Correct |
7 ms |
1492 KB |
Output is correct |
22 |
Correct |
13 ms |
2388 KB |
Output is correct |
23 |
Correct |
7 ms |
1492 KB |
Output is correct |
24 |
Correct |
12 ms |
2372 KB |
Output is correct |
25 |
Correct |
8 ms |
1364 KB |
Output is correct |
26 |
Correct |
1 ms |
312 KB |
Output is correct |
27 |
Correct |
1 ms |
308 KB |
Output is correct |
28 |
Correct |
1 ms |
312 KB |
Output is correct |
29 |
Correct |
1016 ms |
75172 KB |
Output is correct |
30 |
Correct |
501 ms |
69720 KB |
Output is correct |
31 |
Correct |
1223 ms |
126336 KB |
Output is correct |
32 |
Correct |
505 ms |
69680 KB |
Output is correct |
33 |
Correct |
1543 ms |
138112 KB |
Output is correct |
34 |
Correct |
1050 ms |
140064 KB |
Output is correct |
35 |
Correct |
1470 ms |
137804 KB |
Output is correct |
36 |
Correct |
1038 ms |
139552 KB |
Output is correct |
37 |
Correct |
454 ms |
67380 KB |
Output is correct |
38 |
Correct |
668 ms |
127352 KB |
Output is correct |
39 |
Correct |
1047 ms |
135460 KB |
Output is correct |
40 |
Correct |
1038 ms |
135332 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
470 ms |
66856 KB |
Output is correct |
43 |
Correct |
672 ms |
125992 KB |
Output is correct |
44 |
Correct |
1058 ms |
134848 KB |
Output is correct |
45 |
Correct |
1053 ms |
134572 KB |
Output is correct |
46 |
Correct |
1193 ms |
131552 KB |
Output is correct |
47 |
Correct |
67 ms |
9144 KB |
Output is correct |
48 |
Correct |
589 ms |
66816 KB |
Output is correct |
49 |
Correct |
889 ms |
70948 KB |
Output is correct |
50 |
Correct |
1635 ms |
136500 KB |
Output is correct |
51 |
Correct |
1707 ms |
136892 KB |
Output is correct |
52 |
Correct |
1635 ms |
136532 KB |
Output is correct |
53 |
Correct |
1607 ms |
136292 KB |
Output is correct |
54 |
Correct |
1683 ms |
136612 KB |
Output is correct |
55 |
Correct |
1534 ms |
131500 KB |
Output is correct |
56 |
Correct |
1617 ms |
135580 KB |
Output is correct |
57 |
Correct |
1951 ms |
138316 KB |
Output is correct |
58 |
Correct |
2220 ms |
139208 KB |
Output is correct |
59 |
Correct |
2335 ms |
139244 KB |
Output is correct |
60 |
Correct |
2129 ms |
139256 KB |
Output is correct |
61 |
Correct |
2325 ms |
139372 KB |
Output is correct |
62 |
Correct |
2432 ms |
138844 KB |
Output is correct |
63 |
Correct |
2381 ms |
138520 KB |
Output is correct |
64 |
Correct |
2290 ms |
130992 KB |
Output is correct |
65 |
Correct |
2439 ms |
135272 KB |
Output is correct |
66 |
Correct |
856 ms |
71796 KB |
Output is correct |
67 |
Correct |
1069 ms |
131868 KB |
Output is correct |
68 |
Correct |
1309 ms |
135372 KB |
Output is correct |
69 |
Correct |
1205 ms |
134260 KB |
Output is correct |
70 |
Correct |
1664 ms |
137776 KB |
Output is correct |
71 |
Correct |
1658 ms |
139172 KB |
Output is correct |
72 |
Correct |
1681 ms |
139964 KB |
Output is correct |
73 |
Correct |
1682 ms |
140324 KB |
Output is correct |
74 |
Correct |
766 ms |
53992 KB |
Output is correct |
75 |
Correct |
817 ms |
74776 KB |
Output is correct |
76 |
Correct |
2617 ms |
258220 KB |
Output is correct |
77 |
Correct |
2534 ms |
252156 KB |
Output is correct |
78 |
Correct |
2704 ms |
254196 KB |
Output is correct |
79 |
Correct |
1783 ms |
140892 KB |
Output is correct |
80 |
Correct |
3227 ms |
256048 KB |
Output is correct |
81 |
Correct |
1697 ms |
139916 KB |
Output is correct |
82 |
Correct |
3747 ms |
256040 KB |
Output is correct |
83 |
Correct |
2286 ms |
150592 KB |
Output is correct |
84 |
Correct |
3938 ms |
264936 KB |
Output is correct |
85 |
Correct |
2426 ms |
150792 KB |
Output is correct |
86 |
Execution timed out |
4097 ms |
254932 KB |
Time limit exceeded |
87 |
Halted |
0 ms |
0 KB |
- |