#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct st{
ll a,b;
};
#define MAXN 500005
const ll INF = 1e18;
st all[MAXN];
map <ll,int> mp;
int hsh = 0;
bool cmp(st x,st y)
{
return x.a < y.a;
}
ll tree[4*MAXN];
ll lazy[4*MAXN];
void push(int node,int left,int right)
{
if(lazy[node]!=0)
{
tree[node] += lazy[node];
if(left != right)
{
lazy[node*2] += lazy[node];
lazy[node*2+1] += lazy[node];
}
lazy[node] = 0;
}
}
void update(int node,int left,int right,int l,int r,ll val)
{
push(node,left,right);
if(left > right || left > r || l > right)return ;
if(left >= l && right <= r)
{
lazy[node]+=val;
push(node,left,right);
return ;
}
int mid = left+right>>1;
update(node*2,left,mid,l,r,val);
update(node*2+1,mid+1,right,l,r,val);
tree[node]=max(tree[node*2],tree[node*2+1]);
}
ll query(int node,int left,int right,int l,int r)
{
push(node,left,right);
if(left > right || left > r || l > right)return -INF;
if(left >= l && right <= r)return tree[node];
int mid = left+right>>1;
return max(query(node*2,left,mid,l,r),query(node*2+1,mid+1,right,l,r));
}
void build(int node,int left,int right)
{
if(left == right)
{
tree[node]=-INF;
return ;
}
int mid=left+right>>1;
build(node*2,left,mid);
build(node*2+1,mid+1,right);
tree[node]=-INF;
}
int main()
{
ios_base::sync_with_stdio(false);
int n;
cin >> n;
for(int i = 0; i < n; i++)
{
ll a,b;
cin >> a >> b;
st ss;
ss.a=a;ss.b=b;
all[i]=ss;
}
sort(all,all+n,cmp);
build(1,1,n);
ll ans = -INF;
for(int i = 0; i < n; i++)
{
ll a = all[i].a;
ll b = all[i].b;
if(!mp[a])mp[a]=++hsh;
int code = mp[a];
if(query(1,1,n,code,code) == -INF)update(1,1,n,code,code,INF+a);
update(1,1,n,1,code,b);
ans = max(ans,query(1,1,n,1,n)-a);
}
cout << ans;
return 0;
}
Compilation message
art.cpp: In function 'void update(int, int, int, int, int, ll)':
art.cpp:48:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = left+right>>1;
~~~~^~~~~~
art.cpp: In function 'll query(int, int, int, int, int)':
art.cpp:58:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = left+right>>1;
~~~~^~~~~~
art.cpp: In function 'void build(int, int, int)':
art.cpp:68:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=left+right>>1;
~~~~^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 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 |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 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 |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
4 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 |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
6 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 |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 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 |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
4 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 |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
6 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 |
512 KB |
Output is correct |
27 |
Correct |
11 ms |
1152 KB |
Output is correct |
28 |
Correct |
11 ms |
1152 KB |
Output is correct |
29 |
Correct |
11 ms |
1152 KB |
Output is correct |
30 |
Correct |
11 ms |
1152 KB |
Output is correct |
31 |
Correct |
12 ms |
1152 KB |
Output is correct |
32 |
Correct |
11 ms |
1152 KB |
Output is correct |
33 |
Correct |
11 ms |
1152 KB |
Output is correct |
34 |
Correct |
11 ms |
1280 KB |
Output is correct |
35 |
Correct |
10 ms |
1152 KB |
Output is correct |
36 |
Correct |
10 ms |
1152 KB |
Output is correct |
37 |
Correct |
11 ms |
1152 KB |
Output is correct |
38 |
Correct |
11 ms |
1152 KB |
Output is correct |
39 |
Correct |
10 ms |
1152 KB |
Output is correct |
40 |
Correct |
10 ms |
1152 KB |
Output is correct |
41 |
Correct |
10 ms |
1152 KB |
Output is correct |
42 |
Correct |
11 ms |
1152 KB |
Output is correct |
43 |
Correct |
10 ms |
1152 KB |
Output is correct |
44 |
Correct |
11 ms |
1152 KB |
Output is correct |
45 |
Correct |
10 ms |
1152 KB |
Output is correct |
46 |
Correct |
11 ms |
1152 KB |
Output is correct |
47 |
Correct |
11 ms |
1152 KB |
Output is correct |
48 |
Correct |
11 ms |
1152 KB |
Output is correct |
49 |
Correct |
11 ms |
1152 KB |
Output is correct |
50 |
Correct |
11 ms |
1152 KB |
Output is correct |
51 |
Correct |
10 ms |
1152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 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 |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
4 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 |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
6 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 |
512 KB |
Output is correct |
27 |
Correct |
11 ms |
1152 KB |
Output is correct |
28 |
Correct |
11 ms |
1152 KB |
Output is correct |
29 |
Correct |
11 ms |
1152 KB |
Output is correct |
30 |
Correct |
11 ms |
1152 KB |
Output is correct |
31 |
Correct |
12 ms |
1152 KB |
Output is correct |
32 |
Correct |
11 ms |
1152 KB |
Output is correct |
33 |
Correct |
11 ms |
1152 KB |
Output is correct |
34 |
Correct |
11 ms |
1280 KB |
Output is correct |
35 |
Correct |
10 ms |
1152 KB |
Output is correct |
36 |
Correct |
10 ms |
1152 KB |
Output is correct |
37 |
Correct |
11 ms |
1152 KB |
Output is correct |
38 |
Correct |
11 ms |
1152 KB |
Output is correct |
39 |
Correct |
10 ms |
1152 KB |
Output is correct |
40 |
Correct |
10 ms |
1152 KB |
Output is correct |
41 |
Correct |
10 ms |
1152 KB |
Output is correct |
42 |
Correct |
11 ms |
1152 KB |
Output is correct |
43 |
Correct |
10 ms |
1152 KB |
Output is correct |
44 |
Correct |
11 ms |
1152 KB |
Output is correct |
45 |
Correct |
10 ms |
1152 KB |
Output is correct |
46 |
Correct |
11 ms |
1152 KB |
Output is correct |
47 |
Correct |
11 ms |
1152 KB |
Output is correct |
48 |
Correct |
11 ms |
1152 KB |
Output is correct |
49 |
Correct |
11 ms |
1152 KB |
Output is correct |
50 |
Correct |
11 ms |
1152 KB |
Output is correct |
51 |
Correct |
10 ms |
1152 KB |
Output is correct |
52 |
Correct |
833 ms |
58260 KB |
Output is correct |
53 |
Correct |
851 ms |
58404 KB |
Output is correct |
54 |
Correct |
901 ms |
58328 KB |
Output is correct |
55 |
Correct |
854 ms |
58236 KB |
Output is correct |
56 |
Correct |
835 ms |
58360 KB |
Output is correct |
57 |
Correct |
863 ms |
58364 KB |
Output is correct |
58 |
Correct |
848 ms |
58360 KB |
Output is correct |
59 |
Correct |
880 ms |
58104 KB |
Output is correct |
60 |
Correct |
868 ms |
58232 KB |
Output is correct |
61 |
Correct |
867 ms |
58160 KB |
Output is correct |
62 |
Correct |
863 ms |
57592 KB |
Output is correct |
63 |
Correct |
888 ms |
57592 KB |
Output is correct |
64 |
Correct |
894 ms |
57188 KB |
Output is correct |
65 |
Correct |
869 ms |
57336 KB |
Output is correct |
66 |
Correct |
841 ms |
57192 KB |
Output is correct |
67 |
Correct |
883 ms |
57208 KB |
Output is correct |
68 |
Correct |
841 ms |
57336 KB |
Output is correct |