답안 #605459

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
605459 2022-07-25T18:01:14 Z MohamedAhmed04 Team Contest (JOI22_team) C++14
100 / 100
669 ms 35336 KB
#include <bits/stdc++.h>

using namespace std ;

#define int long long 

const int MAX = 2e5 + 10 ;

int arr[MAX][3] ;
int n ;

int tree[4 * MAX] , lazy[4 * MAX] ;

void prop(int node , int l , int r)
{
	tree[node] += lazy[node] ;
	if(l != r)
	{
		lazy[node << 1] += lazy[node] ;
		lazy[node << 1 | 1] += lazy[node] ;
	}
	lazy[node] = 0 ;
}

void Add(int node , int l , int r , int from , int to , int val)
{
	prop(node , l , r) ;
	if(from > r || to < l)
		return ;
	if(l >= from && r <= to)
	{
		lazy[node] += val ;
		prop(node , l , r) ;
		return ;
	}
	int mid = (l + r) >> 1 ;
	Add(node << 1 , l , mid , from , to , val) ;
	Add(node << 1 | 1 , mid+1 , r , from , to , val) ;
	tree[node] = max(tree[node << 1] , tree[node << 1 | 1]) ;
}

int query(int node , int l , int r , int from , int to)
{
	prop(node , l , r) ;
	if(from > r || to < l)
		return (-1e9) ;
	if(l >= from && r <= to)
		return tree[node] ;
	int mid = (l + r) >> 1 ;
	int x = query(node << 1 , l , mid , from , to) ;
	int y = query(node << 1 | 1 , mid+1 , r , from , to) ;
	return max(x , y) ;
}

int tree2[4 * MAX] ;

void update(int node , int l , int r , int idx , int val)
{
	if(idx > r || idx < l)
		return ;
	if(l == r)
	{
		tree2[node] = val ;
		return ;
	}
	int mid = (l + r) >> 1 ;
	update(node << 1 , l , mid , idx , val) ;
	update(node << 1 | 1 , mid+1 , r , idx , val) ;
	tree2[node] = max(tree2[node << 1] , tree2[node << 1 | 1]) ;
}

int FindFirst(int node , int l , int r , int from , int to , int val)
{
	if(from > r || to < l || from > to)
		return (n+1) ;
	if(val > tree2[node])
		return (n+1) ;
	if(l == r)
		return l ;
	int mid = (l + r) >> 1 ;
	int x = FindFirst(node << 1 , l , mid , from , to , val) ;
	if(x != n+1)
		return x ;
	else
		return FindFirst(node << 1 | 1 , mid+1 , r , from , to , val) ;
}

int pos[MAX] , nxt[MAX] ;

set<int>s ;

vector< array<int , 3> >v ;
vector< pair<int , int> >vp ;

void Insert(int idx)
{
	Add(1 , 1 , n , idx , idx , -1e9) ;
	s.insert(idx) ;
	int nxtidx = FindFirst(1 , 1 , n , idx+1 , n , v[idx][1]+1) ;
	int l = nxt[idx] , r = nxtidx-1 ;
	if(l <= r)
	{
		Add(1 , 1 , n , l , r , v[idx][1]) ;
	}
}

void Erase(int idx)
{
	Add(1 , 1 , n , idx , idx , 1e9) ;
	s.erase(idx) ;
	int nxtidx = FindFirst(1 , 1 , n , idx+1 , n , v[idx][1]+1) ;
	int l = nxt[idx] , r = nxtidx-1 ;
	if(l <= r)
		Add(1 , 1 , n , l , r , -1 * v[idx][1]) ;
}

void Insert_Prefixes(int cur)
{
	int last = 0 ;
	while(s.find(cur) == s.end())
	{
		if(v[cur][1] == v[last][1])
			Add(1 , 1 , n , cur , cur , -1e9) ;
		else
			Insert(cur) ;
		last = cur ;
		cur = FindFirst(1 , 1 , n , cur+1 , n , v[cur][1]) ;
	}
}

void Del(int idx)
{
	if(s.find(idx) == s.end())
	{
		update(1 , 1 , n , idx , -1e9) ;
		Add(1 , 1 , n , idx , idx , -1e9) ;
		return ;
	}
	int prv = -1 ;
	if((*s.begin()) != idx)
	{
		prv = *prev(s.lower_bound(idx)) ;
		Erase(prv) ;
	}
	Erase(idx) ;
	update(1 , 1 , n , idx , -1e9) ;
	Add(1 , 1 , n , idx , idx , -1e9) ;
	int cur = prv ;
	if(cur == -1)
		cur = FindFirst(1 , 1 , n , 1 , n , 0) ;
	Insert_Prefixes(cur) ;
}

int32_t main()
{
	ios_base::sync_with_stdio(0) ;
	cin.tie(0) ;
	cin>>n ;
	for(int i = 1 ; i <= n ; ++i)
		cin>>arr[i][0]>>arr[i][1]>>arr[i][2] ;
	for(int i = 1 ; i <= n ; ++i)
		v.push_back({arr[i][1] , arr[i][2] , i}) , vp.emplace_back(arr[i][0] , i) ;
	v.push_back({0 , 0 , 0}) , vp.emplace_back(0 , 0) ;
	sort(v.begin() , v.end()) ;
	sort(vp.begin() , vp.end()) ;

	nxt[n] = n+1 ;
	for(int i = n-1 ; i >= 1 ; --i)
	{
		nxt[i] = nxt[i+1] ;
		if(v[i][0] != v[i+1][0])
			nxt[i] = i+1 ;
	}
	for(int i = 1 ; i <= n ; ++i)
	{
		pos[v[i][2]] = i ;
		Add(1 , 1 , n , i , i , v[i][0]) ;
		update(1 , 1 , n , i , v[i][1]) ;
	}
	s.insert(n+1) ;
	Insert_Prefixes(1) ;
	int j = n , ans = -1 ;
	for(int i = n ; i >= 1 ; --i)
	{
		int idx = vp[i].second ;
		while(j >= 0 && vp[i].first == vp[j].first)
		{
			Del(pos[vp[j].second]) , --j ;
		}
		int l = FindFirst(1 , 1 , n , 1 , n , arr[idx][2]+1) ;
		l = max(l , nxt[pos[idx]]) ;
		if(l <= n)
			ans = max(ans , vp[i].first + query(1 , 1 , n , l , n)) ;
	}
	return cout<<ans<<"\n" , 0 ;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 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 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 440 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 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 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 440 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 6 ms 980 KB Output is correct
27 Correct 7 ms 944 KB Output is correct
28 Correct 9 ms 1032 KB Output is correct
29 Correct 13 ms 1020 KB Output is correct
30 Correct 7 ms 852 KB Output is correct
31 Correct 7 ms 980 KB Output is correct
32 Correct 10 ms 980 KB Output is correct
33 Correct 7 ms 980 KB Output is correct
34 Correct 11 ms 1172 KB Output is correct
35 Correct 2 ms 464 KB Output is correct
36 Correct 2 ms 468 KB Output is correct
37 Correct 8 ms 976 KB Output is correct
38 Correct 9 ms 992 KB Output is correct
39 Correct 3 ms 596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 324 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 328 KB Output is correct
9 Correct 1 ms 324 KB Output is correct
10 Correct 1 ms 328 KB Output is correct
11 Correct 254 ms 25412 KB Output is correct
12 Correct 187 ms 15124 KB Output is correct
13 Correct 250 ms 17056 KB Output is correct
14 Correct 347 ms 25400 KB Output is correct
15 Correct 257 ms 25396 KB Output is correct
16 Correct 236 ms 25392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 324 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 328 KB Output is correct
9 Correct 1 ms 324 KB Output is correct
10 Correct 1 ms 328 KB Output is correct
11 Correct 254 ms 25412 KB Output is correct
12 Correct 187 ms 15124 KB Output is correct
13 Correct 250 ms 17056 KB Output is correct
14 Correct 347 ms 25400 KB Output is correct
15 Correct 257 ms 25396 KB Output is correct
16 Correct 236 ms 25392 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 1 ms 324 KB Output is correct
21 Correct 5 ms 596 KB Output is correct
22 Correct 311 ms 25708 KB Output is correct
23 Correct 306 ms 25140 KB Output is correct
24 Correct 245 ms 15916 KB Output is correct
25 Correct 385 ms 25708 KB Output is correct
26 Correct 274 ms 25564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 324 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 328 KB Output is correct
9 Correct 1 ms 324 KB Output is correct
10 Correct 1 ms 328 KB Output is correct
11 Correct 254 ms 25412 KB Output is correct
12 Correct 187 ms 15124 KB Output is correct
13 Correct 250 ms 17056 KB Output is correct
14 Correct 347 ms 25400 KB Output is correct
15 Correct 257 ms 25396 KB Output is correct
16 Correct 236 ms 25392 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 1 ms 324 KB Output is correct
21 Correct 5 ms 596 KB Output is correct
22 Correct 311 ms 25708 KB Output is correct
23 Correct 306 ms 25140 KB Output is correct
24 Correct 245 ms 15916 KB Output is correct
25 Correct 385 ms 25708 KB Output is correct
26 Correct 274 ms 25564 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 9 ms 912 KB Output is correct
32 Correct 2 ms 468 KB Output is correct
33 Correct 2 ms 468 KB Output is correct
34 Correct 284 ms 25472 KB Output is correct
35 Correct 333 ms 24808 KB Output is correct
36 Correct 362 ms 26092 KB Output is correct
37 Correct 465 ms 26100 KB Output is correct
38 Correct 341 ms 25784 KB Output is correct
39 Correct 107 ms 14916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 324 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 328 KB Output is correct
9 Correct 1 ms 324 KB Output is correct
10 Correct 1 ms 328 KB Output is correct
11 Correct 254 ms 25412 KB Output is correct
12 Correct 187 ms 15124 KB Output is correct
13 Correct 250 ms 17056 KB Output is correct
14 Correct 347 ms 25400 KB Output is correct
15 Correct 257 ms 25396 KB Output is correct
16 Correct 236 ms 25392 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 1 ms 324 KB Output is correct
21 Correct 5 ms 596 KB Output is correct
22 Correct 311 ms 25708 KB Output is correct
23 Correct 306 ms 25140 KB Output is correct
24 Correct 245 ms 15916 KB Output is correct
25 Correct 385 ms 25708 KB Output is correct
26 Correct 274 ms 25564 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 9 ms 912 KB Output is correct
32 Correct 2 ms 468 KB Output is correct
33 Correct 2 ms 468 KB Output is correct
34 Correct 284 ms 25472 KB Output is correct
35 Correct 333 ms 24808 KB Output is correct
36 Correct 362 ms 26092 KB Output is correct
37 Correct 465 ms 26100 KB Output is correct
38 Correct 341 ms 25784 KB Output is correct
39 Correct 107 ms 14916 KB Output is correct
40 Correct 7 ms 876 KB Output is correct
41 Correct 7 ms 980 KB Output is correct
42 Correct 9 ms 912 KB Output is correct
43 Correct 8 ms 972 KB Output is correct
44 Correct 300 ms 26660 KB Output is correct
45 Correct 475 ms 26676 KB Output is correct
46 Correct 438 ms 26548 KB Output is correct
47 Correct 554 ms 26672 KB Output is correct
48 Correct 359 ms 26536 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 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 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 440 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 6 ms 980 KB Output is correct
27 Correct 7 ms 944 KB Output is correct
28 Correct 9 ms 1032 KB Output is correct
29 Correct 13 ms 1020 KB Output is correct
30 Correct 7 ms 852 KB Output is correct
31 Correct 7 ms 980 KB Output is correct
32 Correct 10 ms 980 KB Output is correct
33 Correct 7 ms 980 KB Output is correct
34 Correct 11 ms 1172 KB Output is correct
35 Correct 2 ms 464 KB Output is correct
36 Correct 2 ms 468 KB Output is correct
37 Correct 8 ms 976 KB Output is correct
38 Correct 9 ms 992 KB Output is correct
39 Correct 3 ms 596 KB Output is correct
40 Correct 1 ms 324 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 0 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 0 ms 340 KB Output is correct
47 Correct 1 ms 328 KB Output is correct
48 Correct 1 ms 324 KB Output is correct
49 Correct 1 ms 328 KB Output is correct
50 Correct 254 ms 25412 KB Output is correct
51 Correct 187 ms 15124 KB Output is correct
52 Correct 250 ms 17056 KB Output is correct
53 Correct 347 ms 25400 KB Output is correct
54 Correct 257 ms 25396 KB Output is correct
55 Correct 236 ms 25392 KB Output is correct
56 Correct 1 ms 340 KB Output is correct
57 Correct 1 ms 340 KB Output is correct
58 Correct 0 ms 340 KB Output is correct
59 Correct 1 ms 324 KB Output is correct
60 Correct 5 ms 596 KB Output is correct
61 Correct 311 ms 25708 KB Output is correct
62 Correct 306 ms 25140 KB Output is correct
63 Correct 245 ms 15916 KB Output is correct
64 Correct 385 ms 25708 KB Output is correct
65 Correct 274 ms 25564 KB Output is correct
66 Correct 1 ms 340 KB Output is correct
67 Correct 1 ms 340 KB Output is correct
68 Correct 1 ms 340 KB Output is correct
69 Correct 1 ms 340 KB Output is correct
70 Correct 9 ms 912 KB Output is correct
71 Correct 2 ms 468 KB Output is correct
72 Correct 2 ms 468 KB Output is correct
73 Correct 284 ms 25472 KB Output is correct
74 Correct 333 ms 24808 KB Output is correct
75 Correct 362 ms 26092 KB Output is correct
76 Correct 465 ms 26100 KB Output is correct
77 Correct 341 ms 25784 KB Output is correct
78 Correct 107 ms 14916 KB Output is correct
79 Correct 7 ms 876 KB Output is correct
80 Correct 7 ms 980 KB Output is correct
81 Correct 9 ms 912 KB Output is correct
82 Correct 8 ms 972 KB Output is correct
83 Correct 300 ms 26660 KB Output is correct
84 Correct 475 ms 26676 KB Output is correct
85 Correct 438 ms 26548 KB Output is correct
86 Correct 554 ms 26672 KB Output is correct
87 Correct 359 ms 26536 KB Output is correct
88 Correct 302 ms 28468 KB Output is correct
89 Correct 532 ms 28888 KB Output is correct
90 Correct 332 ms 28512 KB Output is correct
91 Correct 669 ms 28860 KB Output is correct
92 Correct 416 ms 27060 KB Output is correct
93 Correct 630 ms 28084 KB Output is correct
94 Correct 633 ms 27832 KB Output is correct
95 Correct 439 ms 28316 KB Output is correct
96 Correct 575 ms 35336 KB Output is correct
97 Correct 258 ms 28636 KB Output is correct
98 Correct 393 ms 29276 KB Output is correct
99 Correct 403 ms 29236 KB Output is correct