Submission #408842

# Submission time Handle Problem Language Result Execution time Memory
408842 2021-05-19T17:53:08 Z MohamedAhmed04 New Home (APIO18_new_home) C++17
57 / 100
5000 ms 178352 KB
#include <bits/stdc++.h>

using namespace std ;

const int inf = 1e9 ;
const int MAX = 3e5 + 10 ;

int ans[MAX] ;
int n , k , q ;

multiset<int>s[MAX] ;

vector<int>v ;
vector< array<int , 4> >queries ;

int tree[4 * MAX] ;
multiset<int>vals[4 * MAX] ;

void update(int node , int l , int r , int idx , int val)
{
	if(idx > r || idx < l)
		return ;
	if(l == r)
	{
		if(val < 0)
			vals[node].erase(vals[node].find(-1 * val)) ;
		else
			vals[node].insert(val) ;
		if(vals[node].size())
			tree[node] = *vals[node].rbegin() ;
		else
			tree[node] = 0 ;
		return ;
	}
	int mid = (l + r) >> 1 ;
	update(node << 1 , l , mid , idx , val) ;
	update(node << 1 | 1 , mid+1 , r , idx , val) ;
	tree[node] = max(tree[node << 1]  , tree[node << 1 | 1]) ;
}

int query(int node , int l , int r , int from , int to)
{
	if(from > r || to < l)
		return 0 ;
	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) ;
}

bool cmp(array<int , 4>&a , array<int , 4>&b)
{
	if(a[0] != b[0])
		return a[0] < b[0] ;
	else
		return a[3] < b[3] ;
}

int getidx(int x)
{
	return lower_bound(v.begin() , v.end() , x) - v.begin() ;
}

void Add(int x , int t)
{
	int prv = *prev(s[t].lower_bound(x)) ;
	int nxt = *s[t].lower_bound(x) ;
	update(1 , 0 , n , getidx(prv) , -1 * nxt) ;
	update(1 , 0 , n , getidx(prv) , x) ;
	update(1 , 0 , n , getidx(x) , nxt) ;
	s[t].insert(x) ;
}

void Erase(int x , int t)
{
	s[t].erase(s[t].find(x)) ;
	int prv = *prev(s[t].lower_bound(x)) ;
	int nxt = *s[t].lower_bound(x) ;
	update(1 , 0 , n , getidx(prv) , -1 * x) ;
	update(1 , 0 , n , getidx(prv) , nxt) ;
	update(1 , 0 , n , getidx(x) , -1 * nxt) ;
}

bool check(int x , int y)
{
	int idx = lower_bound(v.begin() , v.end() , x - y) - v.begin() ;
	idx-- ;
	idx = max(idx , 0) ;
	return (query(1 , 0 , n , 0 , idx) <= x + y) ;
}

int solve(int x)
{
	int l = 0 , r = 1e8 ;
	int ans = -1 ;
	while(l <= r)
	{
		int mid = (l + r) >> 1 ;
		if(check(x , mid))
			ans = mid , r = mid-1 ;
		else
			l = mid+1 ;
	}
	return ans ;
}

int main()
{
	ios_base::sync_with_stdio(0) ;
	cin.tie(0) ;
	cin>>n>>k>>q ;
	for(int i = 0 ; i < n ; ++i)
	{
		int x , t , a , b ;
		cin>>x>>t>>a>>b ;
		queries.push_back({a , x , t , 0}) ;
		queries.push_back({b+1 , x , t , -1}) ;
		v.push_back(x) ;
	}
	for(int i = 0 ; i < q ; ++i)
	{
		int x , y ;
		cin>>x>>y ;
		queries.push_back({y , x , i , 1}) ;
	}
	v.push_back(0) ;
	sort(v.begin() , v.end()) ;
	v.erase(unique(v.begin() , v.end()) , v.end()) ;
	sort(queries.begin() , queries.end() , cmp) ;
	for(int i = 1 ; i <= k ; ++i)
		s[i].insert(0) , s[i].insert(inf) , update(1 , 0 , n , 0 , inf) ;
	for(auto &a : queries)
	{
		if(a[3] == 0)
			Add(a[1] , a[2]) ;
		else if(a[3] == -1)
			Erase(a[1] , a[2]) ;
		else
			ans[a[2]] = solve(a[1]) ;
	}
	for(int i = 0 ; i < q ; ++i)
		cout<<ans[i]<<"\n" ;
	return 0 ;
}		
# Verdict Execution time Memory Grader output
1 Correct 37 ms 70764 KB Output is correct
2 Correct 37 ms 70764 KB Output is correct
3 Correct 37 ms 70712 KB Output is correct
4 Correct 37 ms 70732 KB Output is correct
5 Correct 40 ms 70724 KB Output is correct
6 Correct 40 ms 70808 KB Output is correct
7 Correct 41 ms 70864 KB Output is correct
8 Correct 41 ms 70872 KB Output is correct
9 Correct 41 ms 70808 KB Output is correct
10 Correct 40 ms 70876 KB Output is correct
11 Correct 38 ms 70748 KB Output is correct
12 Correct 39 ms 70828 KB Output is correct
13 Correct 40 ms 70728 KB Output is correct
14 Correct 39 ms 70724 KB Output is correct
15 Correct 39 ms 70772 KB Output is correct
16 Correct 39 ms 70880 KB Output is correct
17 Correct 41 ms 70904 KB Output is correct
18 Correct 39 ms 70772 KB Output is correct
19 Correct 39 ms 70864 KB Output is correct
20 Correct 40 ms 70852 KB Output is correct
21 Correct 39 ms 70904 KB Output is correct
22 Correct 39 ms 70848 KB Output is correct
23 Correct 39 ms 70872 KB Output is correct
24 Correct 39 ms 70852 KB Output is correct
25 Correct 39 ms 70744 KB Output is correct
26 Correct 39 ms 70856 KB Output is correct
27 Correct 40 ms 70772 KB Output is correct
28 Correct 38 ms 70816 KB Output is correct
29 Correct 39 ms 70796 KB Output is correct
30 Correct 39 ms 70844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 70764 KB Output is correct
2 Correct 37 ms 70764 KB Output is correct
3 Correct 37 ms 70712 KB Output is correct
4 Correct 37 ms 70732 KB Output is correct
5 Correct 40 ms 70724 KB Output is correct
6 Correct 40 ms 70808 KB Output is correct
7 Correct 41 ms 70864 KB Output is correct
8 Correct 41 ms 70872 KB Output is correct
9 Correct 41 ms 70808 KB Output is correct
10 Correct 40 ms 70876 KB Output is correct
11 Correct 38 ms 70748 KB Output is correct
12 Correct 39 ms 70828 KB Output is correct
13 Correct 40 ms 70728 KB Output is correct
14 Correct 39 ms 70724 KB Output is correct
15 Correct 39 ms 70772 KB Output is correct
16 Correct 39 ms 70880 KB Output is correct
17 Correct 41 ms 70904 KB Output is correct
18 Correct 39 ms 70772 KB Output is correct
19 Correct 39 ms 70864 KB Output is correct
20 Correct 40 ms 70852 KB Output is correct
21 Correct 39 ms 70904 KB Output is correct
22 Correct 39 ms 70848 KB Output is correct
23 Correct 39 ms 70872 KB Output is correct
24 Correct 39 ms 70852 KB Output is correct
25 Correct 39 ms 70744 KB Output is correct
26 Correct 39 ms 70856 KB Output is correct
27 Correct 40 ms 70772 KB Output is correct
28 Correct 38 ms 70816 KB Output is correct
29 Correct 39 ms 70796 KB Output is correct
30 Correct 39 ms 70844 KB Output is correct
31 Correct 760 ms 83708 KB Output is correct
32 Correct 536 ms 77516 KB Output is correct
33 Correct 748 ms 79804 KB Output is correct
34 Correct 706 ms 80336 KB Output is correct
35 Correct 769 ms 83608 KB Output is correct
36 Correct 786 ms 83588 KB Output is correct
37 Correct 699 ms 78352 KB Output is correct
38 Correct 705 ms 78116 KB Output is correct
39 Correct 644 ms 78156 KB Output is correct
40 Correct 657 ms 77952 KB Output is correct
41 Correct 572 ms 78340 KB Output is correct
42 Correct 550 ms 78132 KB Output is correct
43 Correct 427 ms 82648 KB Output is correct
44 Correct 572 ms 78292 KB Output is correct
45 Correct 583 ms 78256 KB Output is correct
46 Correct 603 ms 78284 KB Output is correct
47 Correct 526 ms 77772 KB Output is correct
48 Correct 546 ms 77876 KB Output is correct
49 Correct 609 ms 77868 KB Output is correct
50 Correct 569 ms 77976 KB Output is correct
51 Correct 602 ms 77936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4027 ms 138392 KB Output is correct
2 Correct 4848 ms 133460 KB Output is correct
3 Correct 3888 ms 178352 KB Output is correct
4 Correct 3992 ms 149948 KB Output is correct
5 Correct 4795 ms 133072 KB Output is correct
6 Correct 4765 ms 133540 KB Output is correct
7 Correct 3776 ms 178280 KB Output is correct
8 Correct 3582 ms 149980 KB Output is correct
9 Correct 3583 ms 139948 KB Output is correct
10 Correct 4253 ms 134592 KB Output is correct
11 Correct 3572 ms 132848 KB Output is correct
12 Correct 3713 ms 134388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4508 ms 130564 KB Output is correct
2 Correct 2679 ms 120404 KB Output is correct
3 Execution timed out 5049 ms 133252 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 37 ms 70764 KB Output is correct
2 Correct 37 ms 70764 KB Output is correct
3 Correct 37 ms 70712 KB Output is correct
4 Correct 37 ms 70732 KB Output is correct
5 Correct 40 ms 70724 KB Output is correct
6 Correct 40 ms 70808 KB Output is correct
7 Correct 41 ms 70864 KB Output is correct
8 Correct 41 ms 70872 KB Output is correct
9 Correct 41 ms 70808 KB Output is correct
10 Correct 40 ms 70876 KB Output is correct
11 Correct 38 ms 70748 KB Output is correct
12 Correct 39 ms 70828 KB Output is correct
13 Correct 40 ms 70728 KB Output is correct
14 Correct 39 ms 70724 KB Output is correct
15 Correct 39 ms 70772 KB Output is correct
16 Correct 39 ms 70880 KB Output is correct
17 Correct 41 ms 70904 KB Output is correct
18 Correct 39 ms 70772 KB Output is correct
19 Correct 39 ms 70864 KB Output is correct
20 Correct 40 ms 70852 KB Output is correct
21 Correct 39 ms 70904 KB Output is correct
22 Correct 39 ms 70848 KB Output is correct
23 Correct 39 ms 70872 KB Output is correct
24 Correct 39 ms 70852 KB Output is correct
25 Correct 39 ms 70744 KB Output is correct
26 Correct 39 ms 70856 KB Output is correct
27 Correct 40 ms 70772 KB Output is correct
28 Correct 38 ms 70816 KB Output is correct
29 Correct 39 ms 70796 KB Output is correct
30 Correct 39 ms 70844 KB Output is correct
31 Correct 760 ms 83708 KB Output is correct
32 Correct 536 ms 77516 KB Output is correct
33 Correct 748 ms 79804 KB Output is correct
34 Correct 706 ms 80336 KB Output is correct
35 Correct 769 ms 83608 KB Output is correct
36 Correct 786 ms 83588 KB Output is correct
37 Correct 699 ms 78352 KB Output is correct
38 Correct 705 ms 78116 KB Output is correct
39 Correct 644 ms 78156 KB Output is correct
40 Correct 657 ms 77952 KB Output is correct
41 Correct 572 ms 78340 KB Output is correct
42 Correct 550 ms 78132 KB Output is correct
43 Correct 427 ms 82648 KB Output is correct
44 Correct 572 ms 78292 KB Output is correct
45 Correct 583 ms 78256 KB Output is correct
46 Correct 603 ms 78284 KB Output is correct
47 Correct 526 ms 77772 KB Output is correct
48 Correct 546 ms 77876 KB Output is correct
49 Correct 609 ms 77868 KB Output is correct
50 Correct 569 ms 77976 KB Output is correct
51 Correct 602 ms 77936 KB Output is correct
52 Correct 662 ms 91980 KB Output is correct
53 Correct 652 ms 88344 KB Output is correct
54 Correct 619 ms 86236 KB Output is correct
55 Correct 603 ms 82892 KB Output is correct
56 Correct 636 ms 85144 KB Output is correct
57 Correct 595 ms 79604 KB Output is correct
58 Correct 600 ms 82760 KB Output is correct
59 Correct 616 ms 85132 KB Output is correct
60 Correct 574 ms 79600 KB Output is correct
61 Correct 493 ms 91172 KB Output is correct
62 Correct 676 ms 92132 KB Output is correct
63 Correct 643 ms 86864 KB Output is correct
64 Correct 629 ms 84524 KB Output is correct
65 Correct 585 ms 80264 KB Output is correct
66 Correct 563 ms 78360 KB Output is correct
67 Correct 477 ms 77604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 70764 KB Output is correct
2 Correct 37 ms 70764 KB Output is correct
3 Correct 37 ms 70712 KB Output is correct
4 Correct 37 ms 70732 KB Output is correct
5 Correct 40 ms 70724 KB Output is correct
6 Correct 40 ms 70808 KB Output is correct
7 Correct 41 ms 70864 KB Output is correct
8 Correct 41 ms 70872 KB Output is correct
9 Correct 41 ms 70808 KB Output is correct
10 Correct 40 ms 70876 KB Output is correct
11 Correct 38 ms 70748 KB Output is correct
12 Correct 39 ms 70828 KB Output is correct
13 Correct 40 ms 70728 KB Output is correct
14 Correct 39 ms 70724 KB Output is correct
15 Correct 39 ms 70772 KB Output is correct
16 Correct 39 ms 70880 KB Output is correct
17 Correct 41 ms 70904 KB Output is correct
18 Correct 39 ms 70772 KB Output is correct
19 Correct 39 ms 70864 KB Output is correct
20 Correct 40 ms 70852 KB Output is correct
21 Correct 39 ms 70904 KB Output is correct
22 Correct 39 ms 70848 KB Output is correct
23 Correct 39 ms 70872 KB Output is correct
24 Correct 39 ms 70852 KB Output is correct
25 Correct 39 ms 70744 KB Output is correct
26 Correct 39 ms 70856 KB Output is correct
27 Correct 40 ms 70772 KB Output is correct
28 Correct 38 ms 70816 KB Output is correct
29 Correct 39 ms 70796 KB Output is correct
30 Correct 39 ms 70844 KB Output is correct
31 Correct 760 ms 83708 KB Output is correct
32 Correct 536 ms 77516 KB Output is correct
33 Correct 748 ms 79804 KB Output is correct
34 Correct 706 ms 80336 KB Output is correct
35 Correct 769 ms 83608 KB Output is correct
36 Correct 786 ms 83588 KB Output is correct
37 Correct 699 ms 78352 KB Output is correct
38 Correct 705 ms 78116 KB Output is correct
39 Correct 644 ms 78156 KB Output is correct
40 Correct 657 ms 77952 KB Output is correct
41 Correct 572 ms 78340 KB Output is correct
42 Correct 550 ms 78132 KB Output is correct
43 Correct 427 ms 82648 KB Output is correct
44 Correct 572 ms 78292 KB Output is correct
45 Correct 583 ms 78256 KB Output is correct
46 Correct 603 ms 78284 KB Output is correct
47 Correct 526 ms 77772 KB Output is correct
48 Correct 546 ms 77876 KB Output is correct
49 Correct 609 ms 77868 KB Output is correct
50 Correct 569 ms 77976 KB Output is correct
51 Correct 602 ms 77936 KB Output is correct
52 Correct 4027 ms 138392 KB Output is correct
53 Correct 4848 ms 133460 KB Output is correct
54 Correct 3888 ms 178352 KB Output is correct
55 Correct 3992 ms 149948 KB Output is correct
56 Correct 4795 ms 133072 KB Output is correct
57 Correct 4765 ms 133540 KB Output is correct
58 Correct 3776 ms 178280 KB Output is correct
59 Correct 3582 ms 149980 KB Output is correct
60 Correct 3583 ms 139948 KB Output is correct
61 Correct 4253 ms 134592 KB Output is correct
62 Correct 3572 ms 132848 KB Output is correct
63 Correct 3713 ms 134388 KB Output is correct
64 Correct 4508 ms 130564 KB Output is correct
65 Correct 2679 ms 120404 KB Output is correct
66 Execution timed out 5049 ms 133252 KB Time limit exceeded
67 Halted 0 ms 0 KB -