#include <bits/stdc++.h>
#define int long long
#define INF 1000000000000000000
#define lchild(i) (i*2 + 1)
#define rchild(i) (i*2 + 2)
#define mid(l, u) ((l+u)/2)
#define MOD 998244353
using namespace std;
const int N = 5e5 + 5;
int lazy[N*4], rmq[N*4];
pair<int, int> a[N];
int build(int l, int u, int i){
lazy[i] = 0;
if(l==u) return rmq[i] = a[l].first;
return rmq[i] = max(build(l, mid(l, u), lchild(i)), build(mid(l, u)+1, u, rchild(i)));
}
int update(int l, int u, int i, int ll, int uu,int tu){
if(lazy[i]){
rmq[i]+=lazy[i];
if(l!=u){
lazy[lchild(i)] += lazy[i];
lazy[rchild(i)] += lazy[i];
}
lazy[i] = 0;
}
if(l>=ll && u<=uu){
rmq[i]+=tu;
if(l!=u){
lazy[lchild(i)]+=tu;
lazy[rchild(i)]+=tu;
}
return rmq[i];
}
if(l>uu || u<ll) return rmq[i];
return rmq[i] = max(update(l, mid(l, u), lchild(i), ll, uu, tu), update(mid(l, u)+1, u, rchild(i), ll, uu, tu));
}
int query(int l, int u, int i, int ll, int uu){
if(lazy[i]){
rmq[i]+=lazy[i];
if(l!=u){
lazy[lchild(i)] += lazy[i];
lazy[rchild(i)] += lazy[i];
}
lazy[i] = 0;
}
if(l>=ll && u<=uu) return rmq[i];
if(l>uu || u<ll) return (-INF);
return max(query(l, mid(l, u), lchild(i), ll, uu), query(mid(l, u)+1, u, rchild(i), ll, uu));
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin>>n;
for(int i = 1;i<=n;i++) cin>>a[i].first>>a[i].second;
sort(a+1, a+n+1);
build(1, n, 0);
int ans = -INF;
for(int i = 1;i<=n;i++){
//cout<<i<<endl;
update(1, n, 0, 1, i, a[i].second);
int tempans = -a[i].first + query(1, n, 0, 1, i);
//cout<<(-a[i].first)<<" "<<query(1, n, 0, 1, i)<<"\n";
ans = max(ans, tempans);
}
cout<<ans<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
6 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
6 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
416 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
6 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
416 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
9 ms |
768 KB |
Output is correct |
28 |
Correct |
8 ms |
768 KB |
Output is correct |
29 |
Correct |
8 ms |
768 KB |
Output is correct |
30 |
Correct |
8 ms |
768 KB |
Output is correct |
31 |
Correct |
9 ms |
896 KB |
Output is correct |
32 |
Correct |
8 ms |
896 KB |
Output is correct |
33 |
Correct |
9 ms |
896 KB |
Output is correct |
34 |
Correct |
8 ms |
768 KB |
Output is correct |
35 |
Correct |
8 ms |
896 KB |
Output is correct |
36 |
Correct |
8 ms |
768 KB |
Output is correct |
37 |
Correct |
8 ms |
768 KB |
Output is correct |
38 |
Correct |
8 ms |
896 KB |
Output is correct |
39 |
Correct |
8 ms |
768 KB |
Output is correct |
40 |
Correct |
9 ms |
768 KB |
Output is correct |
41 |
Correct |
8 ms |
768 KB |
Output is correct |
42 |
Correct |
8 ms |
768 KB |
Output is correct |
43 |
Correct |
8 ms |
896 KB |
Output is correct |
44 |
Correct |
8 ms |
768 KB |
Output is correct |
45 |
Correct |
8 ms |
768 KB |
Output is correct |
46 |
Correct |
8 ms |
768 KB |
Output is correct |
47 |
Correct |
8 ms |
896 KB |
Output is correct |
48 |
Correct |
8 ms |
768 KB |
Output is correct |
49 |
Correct |
8 ms |
768 KB |
Output is correct |
50 |
Correct |
8 ms |
896 KB |
Output is correct |
51 |
Correct |
8 ms |
768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
6 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
416 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
9 ms |
768 KB |
Output is correct |
28 |
Correct |
8 ms |
768 KB |
Output is correct |
29 |
Correct |
8 ms |
768 KB |
Output is correct |
30 |
Correct |
8 ms |
768 KB |
Output is correct |
31 |
Correct |
9 ms |
896 KB |
Output is correct |
32 |
Correct |
8 ms |
896 KB |
Output is correct |
33 |
Correct |
9 ms |
896 KB |
Output is correct |
34 |
Correct |
8 ms |
768 KB |
Output is correct |
35 |
Correct |
8 ms |
896 KB |
Output is correct |
36 |
Correct |
8 ms |
768 KB |
Output is correct |
37 |
Correct |
8 ms |
768 KB |
Output is correct |
38 |
Correct |
8 ms |
896 KB |
Output is correct |
39 |
Correct |
8 ms |
768 KB |
Output is correct |
40 |
Correct |
9 ms |
768 KB |
Output is correct |
41 |
Correct |
8 ms |
768 KB |
Output is correct |
42 |
Correct |
8 ms |
768 KB |
Output is correct |
43 |
Correct |
8 ms |
896 KB |
Output is correct |
44 |
Correct |
8 ms |
768 KB |
Output is correct |
45 |
Correct |
8 ms |
768 KB |
Output is correct |
46 |
Correct |
8 ms |
768 KB |
Output is correct |
47 |
Correct |
8 ms |
896 KB |
Output is correct |
48 |
Correct |
8 ms |
768 KB |
Output is correct |
49 |
Correct |
8 ms |
768 KB |
Output is correct |
50 |
Correct |
8 ms |
896 KB |
Output is correct |
51 |
Correct |
8 ms |
768 KB |
Output is correct |
52 |
Correct |
436 ms |
37384 KB |
Output is correct |
53 |
Correct |
442 ms |
37240 KB |
Output is correct |
54 |
Correct |
434 ms |
37208 KB |
Output is correct |
55 |
Correct |
408 ms |
37316 KB |
Output is correct |
56 |
Correct |
418 ms |
37408 KB |
Output is correct |
57 |
Correct |
418 ms |
37368 KB |
Output is correct |
58 |
Correct |
404 ms |
37240 KB |
Output is correct |
59 |
Correct |
412 ms |
37240 KB |
Output is correct |
60 |
Correct |
418 ms |
37492 KB |
Output is correct |
61 |
Correct |
427 ms |
37188 KB |
Output is correct |
62 |
Correct |
416 ms |
37216 KB |
Output is correct |
63 |
Correct |
409 ms |
37320 KB |
Output is correct |
64 |
Correct |
411 ms |
37240 KB |
Output is correct |
65 |
Correct |
422 ms |
37516 KB |
Output is correct |
66 |
Correct |
389 ms |
37240 KB |
Output is correct |
67 |
Correct |
422 ms |
37320 KB |
Output is correct |
68 |
Correct |
401 ms |
37240 KB |
Output is correct |