Submission #401865

# Submission time Handle Problem Language Result Execution time Memory
401865 2021-05-10T22:36:10 Z faresbasbs New Home (APIO18_new_home) C++14
5 / 100
5000 ms 1048580 KB
#include <bits/stdc++.h>
using namespace std;

struct query{
	int x,t,type;
	bool operator <(query b) const{
		if(t == b.t){
			return type < b.type;
		}
		return t < b.t;
	}
};

struct node{
	int l,r,cnt;
	node *lc,*rc;
	node(){
		l = r = cnt = 0;
		lc = rc = NULL;
	}
};

int n,q,k,t,t2,ans[300001];
multiset<int> ms[300001];
vector<node*> segment;
vector<query> qs;
vector<int> all;

void del2(node *curr , int val){
	while(curr->l != curr->r){
		int mid = (curr->l+curr->r)/2;
		if(val <= mid){
			curr = curr->lc;
		}else{
			curr = curr->rc;
		}
		curr->cnt -= 1;
	}
}

void del(int curr , int val){
	while(curr){
		del2(segment[curr],val);
		curr /= 2;
	}
}

void add2(node *curr , int val){
	while(curr->l != curr->r){
		int mid = (curr->l+curr->r)/2;
		if(val <= mid){
			if(curr->lc == NULL){
				curr->lc = new node();
				curr->lc->l = curr->l , curr->lc->r = mid;
			}
			curr = curr->lc;
		}else{
			if(curr->rc == NULL){
				curr->rc = new node();
				curr->rc->l = mid+1 , curr->rc->r = curr->r;
			}
			curr = curr->rc;
		}
		curr->cnt += 1;
	}
}

void add(int curr , int val){
	while(curr){
		if(segment[curr] == NULL){
			segment[curr] = new node();
			segment[curr]->l = 1 , segment[curr]->r = t;
		}
		add2(segment[curr],val);
		curr /= 2;
	}
}

int cmp(int val){
	return lower_bound(all.begin(),all.end(),val)-all.begin()+1;
}

int num2(int a , int b , node *curr , int l , int r){
	if(b < l || a > r || curr == NULL){
		return 0;
	}
	if(a <= l && b >= r){
		return curr->cnt;
	}
	int mid = (l+r)/2;
	return num2(a,b,curr->lc,l,mid)+num2(a,b,curr->rc,mid+1,r);
}

int num(int a , int b , int curr , int l , int r){
	if(b < l || a > r){
		return 0;
	}
	if(a <= l && b >= r){
		if(segment[curr] == NULL){
			return 0;
		}
		return num2(b+1,t,segment[curr],1,t);
	}
	int mid = (l+r)/2;
	return num(a,b,2*curr,l,mid)+num(a,b,2*curr+1,mid+1,r);
}

int main(){
	ios_base::sync_with_stdio(false);
	cout.tie(NULL);
	cin.tie(NULL);
	cin >> n >> k >> q;
	for(int i = 0 ; i < n ; i += 1){
		int x,t,a,b;
		cin >> x >> t >> a >> b;
		all.push_back(x);
		qs.push_back({x,a,t});
		qs.push_back({x,b+1,-t});
	}
	for(int i = 1 ; i <= q ; i += 1){
		int l,y;
		cin >> l >> y;
		all.push_back(l);
		qs.push_back({l,y,k+i});
	}
	sort(all.begin(),all.end());
	all.erase(unique(all.begin(),all.end()),all.end());
	t = pow(2,ceil(log2(all.size()+1)));
	for(int i = 1 ; i <= k ; i += 1){
		ms[i].insert(all.size()+1);
	}
	segment.resize(2*t);
	sort(qs.begin(),qs.end());
	for(auto i : qs){
		if(i.type > k){
			i.type -= k;
			if(num(1,all.size(),1,1,t) < k){
				ans[i.type] = -1;
				continue;
			}
			int first = -1 , last = 100000000;
			while(last-first > 1){
				int mid = (first+last)/2 , val1 = lower_bound(all.begin(),all.end(),i.x-mid)-all.begin()+1;
				int val2 = upper_bound(all.begin(),all.end(),i.x+mid)-all.begin();
				if(num(val1,val2,1,1,t) >= k){
					last = mid;
				}else{
					first = mid;
				}
			}
			ans[i.type] = last;
		}else if(i.type > 0){
			i.x = cmp(i.x);
			auto it = ms[i.type].lower_bound(i.x);
			int val = *it;
			add(i.x+t-1,val);
			if(it != ms[i.type].begin()){
				it--;
				int pos = *it;
				del(pos+t-1,val) , add(pos+t-1,i.x);
			}
			ms[i.type].insert(i.x);
		}else{
			i.x = cmp(i.x);
			i.type = -i.type;
			ms[i.type].erase(ms[i.type].find(i.x));
			auto it = ms[i.type].lower_bound(i.x);
			int val = *it;
			del(i.x+t-1,val);
			if(it != ms[i.type].begin()){
				it--;
				int pos = *it;
				del(pos+t-1,i.x) , add(pos+t-1,val);
			}
		}
	}
	for(int i = 1 ; i <= q ; i += 1){
		cout << ans[i] << '\n';
	}
}
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14284 KB Output is correct
2 Correct 9 ms 14412 KB Output is correct
3 Correct 9 ms 14284 KB Output is correct
4 Correct 9 ms 14360 KB Output is correct
5 Correct 12 ms 14412 KB Output is correct
6 Correct 22 ms 16452 KB Output is correct
7 Correct 12 ms 15072 KB Output is correct
8 Correct 13 ms 16080 KB Output is correct
9 Correct 11 ms 15052 KB Output is correct
10 Correct 19 ms 16596 KB Output is correct
11 Correct 13 ms 15620 KB Output is correct
12 Correct 16 ms 16084 KB Output is correct
13 Correct 11 ms 15308 KB Output is correct
14 Correct 13 ms 15576 KB Output is correct
15 Correct 18 ms 15908 KB Output is correct
16 Correct 18 ms 15668 KB Output is correct
17 Correct 21 ms 16216 KB Output is correct
18 Correct 15 ms 15840 KB Output is correct
19 Correct 17 ms 15684 KB Output is correct
20 Correct 16 ms 16232 KB Output is correct
21 Correct 10 ms 14384 KB Output is correct
22 Correct 12 ms 15056 KB Output is correct
23 Correct 13 ms 15688 KB Output is correct
24 Correct 15 ms 15868 KB Output is correct
25 Correct 16 ms 16292 KB Output is correct
26 Correct 19 ms 16316 KB Output is correct
27 Correct 12 ms 14544 KB Output is correct
28 Correct 14 ms 15668 KB Output is correct
29 Correct 14 ms 15768 KB Output is correct
30 Correct 15 ms 15308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14284 KB Output is correct
2 Correct 9 ms 14412 KB Output is correct
3 Correct 9 ms 14284 KB Output is correct
4 Correct 9 ms 14360 KB Output is correct
5 Correct 12 ms 14412 KB Output is correct
6 Correct 22 ms 16452 KB Output is correct
7 Correct 12 ms 15072 KB Output is correct
8 Correct 13 ms 16080 KB Output is correct
9 Correct 11 ms 15052 KB Output is correct
10 Correct 19 ms 16596 KB Output is correct
11 Correct 13 ms 15620 KB Output is correct
12 Correct 16 ms 16084 KB Output is correct
13 Correct 11 ms 15308 KB Output is correct
14 Correct 13 ms 15576 KB Output is correct
15 Correct 18 ms 15908 KB Output is correct
16 Correct 18 ms 15668 KB Output is correct
17 Correct 21 ms 16216 KB Output is correct
18 Correct 15 ms 15840 KB Output is correct
19 Correct 17 ms 15684 KB Output is correct
20 Correct 16 ms 16232 KB Output is correct
21 Correct 10 ms 14384 KB Output is correct
22 Correct 12 ms 15056 KB Output is correct
23 Correct 13 ms 15688 KB Output is correct
24 Correct 15 ms 15868 KB Output is correct
25 Correct 16 ms 16292 KB Output is correct
26 Correct 19 ms 16316 KB Output is correct
27 Correct 12 ms 14544 KB Output is correct
28 Correct 14 ms 15668 KB Output is correct
29 Correct 14 ms 15768 KB Output is correct
30 Correct 15 ms 15308 KB Output is correct
31 Execution timed out 5077 ms 660604 KB Time limit exceeded
32 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 2452 ms 1048580 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 2378 ms 1048580 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14284 KB Output is correct
2 Correct 9 ms 14412 KB Output is correct
3 Correct 9 ms 14284 KB Output is correct
4 Correct 9 ms 14360 KB Output is correct
5 Correct 12 ms 14412 KB Output is correct
6 Correct 22 ms 16452 KB Output is correct
7 Correct 12 ms 15072 KB Output is correct
8 Correct 13 ms 16080 KB Output is correct
9 Correct 11 ms 15052 KB Output is correct
10 Correct 19 ms 16596 KB Output is correct
11 Correct 13 ms 15620 KB Output is correct
12 Correct 16 ms 16084 KB Output is correct
13 Correct 11 ms 15308 KB Output is correct
14 Correct 13 ms 15576 KB Output is correct
15 Correct 18 ms 15908 KB Output is correct
16 Correct 18 ms 15668 KB Output is correct
17 Correct 21 ms 16216 KB Output is correct
18 Correct 15 ms 15840 KB Output is correct
19 Correct 17 ms 15684 KB Output is correct
20 Correct 16 ms 16232 KB Output is correct
21 Correct 10 ms 14384 KB Output is correct
22 Correct 12 ms 15056 KB Output is correct
23 Correct 13 ms 15688 KB Output is correct
24 Correct 15 ms 15868 KB Output is correct
25 Correct 16 ms 16292 KB Output is correct
26 Correct 19 ms 16316 KB Output is correct
27 Correct 12 ms 14544 KB Output is correct
28 Correct 14 ms 15668 KB Output is correct
29 Correct 14 ms 15768 KB Output is correct
30 Correct 15 ms 15308 KB Output is correct
31 Execution timed out 5077 ms 660604 KB Time limit exceeded
32 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14284 KB Output is correct
2 Correct 9 ms 14412 KB Output is correct
3 Correct 9 ms 14284 KB Output is correct
4 Correct 9 ms 14360 KB Output is correct
5 Correct 12 ms 14412 KB Output is correct
6 Correct 22 ms 16452 KB Output is correct
7 Correct 12 ms 15072 KB Output is correct
8 Correct 13 ms 16080 KB Output is correct
9 Correct 11 ms 15052 KB Output is correct
10 Correct 19 ms 16596 KB Output is correct
11 Correct 13 ms 15620 KB Output is correct
12 Correct 16 ms 16084 KB Output is correct
13 Correct 11 ms 15308 KB Output is correct
14 Correct 13 ms 15576 KB Output is correct
15 Correct 18 ms 15908 KB Output is correct
16 Correct 18 ms 15668 KB Output is correct
17 Correct 21 ms 16216 KB Output is correct
18 Correct 15 ms 15840 KB Output is correct
19 Correct 17 ms 15684 KB Output is correct
20 Correct 16 ms 16232 KB Output is correct
21 Correct 10 ms 14384 KB Output is correct
22 Correct 12 ms 15056 KB Output is correct
23 Correct 13 ms 15688 KB Output is correct
24 Correct 15 ms 15868 KB Output is correct
25 Correct 16 ms 16292 KB Output is correct
26 Correct 19 ms 16316 KB Output is correct
27 Correct 12 ms 14544 KB Output is correct
28 Correct 14 ms 15668 KB Output is correct
29 Correct 14 ms 15768 KB Output is correct
30 Correct 15 ms 15308 KB Output is correct
31 Execution timed out 5077 ms 660604 KB Time limit exceeded
32 Halted 0 ms 0 KB -