Submission #252457

# Submission time Handle Problem Language Result Execution time Memory
252457 2020-07-25T15:44:02 Z Saboon New Home (APIO18_new_home) C++14
57 / 100
2652 ms 179308 KB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 3e5 + 20;
const int inf = 1e9;

set<pair<int,int>> tindex[maxn];
int ans[maxn];
vector<int> cmp;
multiset<int> seg[8*maxn];
int mxm[8*maxn], mnm[8*maxn];

pair<int,int> get2(int id, int L, int R, int idx){
	if (R <= idx or idx < L)
		return {inf, 0};
	if (L + 1 == R)
		return {mnm[id], mxm[id]};
	pair<int,int> ret = {mnm[id], mxm[id]};
	int mid = (L + R) >> 1;
	auto it1 = get2(2*id+0, L, mid, idx);
	auto it2 = get2(2*id+1, mid, R, idx);
	ret.first = min({ret.first, it1.first, it2.first});
	ret.second = max({ret.second, it1.second, it2.second});
	return ret;
}

void change2(int id, int L, int R, int l, int r, int x){
	if (r <= L or R <= l)
		return;
	if (l <= L and R <= r){
		mxm[id] = max(mxm[id], x);
		mnm[id] = min(mnm[id], x);
		return;
	}
	int mid = (L + R) >> 1;
	change2(2*id+0, L, mid, l, r, x);
	change2(2*id+1, mid, R, l, r, x);
}

pair<int,int> get(int id, int L, int R, int idx){
	if (R <= idx or idx < L)
		return {inf, 0};
	if (L + 1 == R){
		if (seg[id].empty())
			return {inf,0};
		return {*seg[id].begin(), *seg[id].rbegin()};
	}
	pair<int,int> ret = {inf,0};
	if (!seg[id].empty())
		ret = {*seg[id].begin(), *seg[id].rbegin()};
	int mid = (L + R) >> 1;
	auto it1 = get(2*id+0, L, mid, idx);
	auto it2 = get(2*id+1, mid, R, idx);
	ret.first = min({ret.first, it1.first, it2.first});
	ret.second = max({ret.second, it1.second, it2.second});
	return ret;
}

void change(int id, int L, int R, int l, int r, int x, char type){
	if (r <= L or R <= l)
		return;
	if (l <= L and R <= r){
		if (type == 'A')
			seg[id].insert(x);
		else
			seg[id].erase(seg[id].find(x));
		return;
	}
	int mid = (L + R) >> 1;
	change(2*id+0, L, mid, l, r, x, type);
	change(2*id+1, mid, R, l, r, x, type);
}

int m;

pair<int,int> getpre(int x, int idx, int type){
	auto it = tindex[type].lower_bound(make_pair(x,idx));
	if (it == tindex[type].begin())
		return {-1,-1};
	it --;
	return *it;
}

pair<int,int> getnex(int x, int idx, int type){
	auto it = tindex[type].lower_bound(make_pair(x,idx));
	it ++;
	if (it == tindex[type].end())
		return {-1,-1};
	return *it;
}

void add2(int x, int idx, int type){
	if (x == -1)
		return;
	int pre = getpre(x,idx,type).first, nex = getnex(x,idx,type).first;
	if (pre == -1)
		pre = 0;
	else
		pre = lower_bound(cmp.begin(), cmp.end(), (cmp[pre]+cmp[x]+1)/2) - cmp.begin();
	if (nex == -1)
		nex = m;
	else
		nex = lower_bound(cmp.begin(), cmp.end(), (cmp[nex]+cmp[x]+1)/2) - cmp.begin();
	change2(1, 0, m, pre, nex, x);
}

void add(int x, int idx, int type){
	if (x == -1)
		return;
	int pre = getpre(x,idx,type).first, nex = getnex(x,idx,type).first;
	if (pre == -1)
		pre = 0;
	else
		pre = lower_bound(cmp.begin(), cmp.end(), (cmp[pre]+cmp[x]+1)/2) - cmp.begin();
	if (nex == -1)
		nex = m;
	else
		nex = lower_bound(cmp.begin(), cmp.end(), (cmp[nex]+cmp[x]+1)/2) - cmp.begin();
	change(1, 0, m, pre, nex, x, 'A');
}

void del(int x, int idx, int type){
	if (x == -1)
		return;
	int pre = getpre(x,idx,type).first, nex = getnex(x,idx,type).first;
	if (pre == -1)
		pre = 0;
	else
		pre = lower_bound(cmp.begin(), cmp.end(), (cmp[pre]+cmp[x]+1)/2) - cmp.begin();
	if (nex == -1)
		nex = m;
	else
		nex = lower_bound(cmp.begin(), cmp.end(), (cmp[nex]+cmp[x]+1)/2) - cmp.begin();
	change(1, 0, m, pre, nex, x, 'D');
}

int main(){
	int n, k, q;
	scanf("%d%d%d", &n, &k, &q);
	vector<pair<pair<int,int>, pair<int,int>>> event;
	for (int i = 0; i < n; i++){
		int x, t, a, b;
		scanf("%d%d%d%d", &x, &t, &a, &b);
		event.push_back({{a,i}, {x,t}}); 
		event.push_back({{b,n+1+i}, {x,t}});
		cmp.push_back(x);
	}
	for (int i = 0; i < q; i++){
		int l, y;
		scanf("%d%d", &l, &y);
		cmp.push_back(l);
		event.push_back({{y,n}, {l,i}});
	}
	sort(cmp.begin(), cmp.end());
	cmp.resize(unique(cmp.begin(), cmp.end()) - cmp.begin());
	m = cmp.size();
	for (int i = 0; i < event.size(); i++)
		event[i].second.first = lower_bound(cmp.begin(), cmp.end(), event[i].second.first) - cmp.begin();
	sort(event.begin(), event.end());
	if (n <= 60000 and q <= 60000){
		int cnt = 0;
		for (auto Q : event){
			if (Q.first.second < n){
				int x = Q.second.first, type = Q.second.second, idx = Q.first.second;
				if (tindex[type].empty())
					cnt ++;
				tindex[type].insert({x,idx});
				auto nex = getnex(x, idx, type), pre = getpre(x, idx, type);
				tindex[type].erase({x,idx});
				del(pre.first, pre.second, type);
				del(nex.first, nex.second, type);
				tindex[type].insert({x,idx});
				add(x, idx, type);
				add(pre.first, pre.second, type);
				add(nex.first, nex.second, type);
			}
			else if (Q.first.second > n){
				int x = Q.second.first, type = Q.second.second, idx = Q.first.second - n - 1;
				auto nex = getnex(x,idx,type), pre = getpre(x,idx,type);
				del(x, idx, type);
				del(pre.first, pre.second, type);
				del(nex.first, nex.second, type);
				tindex[type].erase({x,idx});
				if (tindex[type].empty())
					cnt --;
				add(pre.first, pre.second, type);
				add(nex.first, nex.second, type);
			}
			else{
				int x = Q.second.first, idx = Q.second.second;
				if (cnt < k){
					ans[idx] = -1;
					continue;
				}
				auto it = get(1, 0, m, x);
				ans[idx] = max(cmp[x]-cmp[it.first], cmp[it.second]-cmp[x]);
			}
		}
		for (int i = 0; i < q; i++)
			printf("%d\n", ans[i]);
		return 0;
	}
	memset(mnm, 63, sizeof mnm);
	int cnt = 0;
	for (auto Q : event){
		if (Q.first.second < n){
			int x = Q.second.first, type = Q.second.second, idx = Q.first.second;
			if (tindex[type].empty())
				cnt ++;
			tindex[type].insert({x,idx});
		}
	}
	memset(ans, -1, sizeof ans);
	if (cnt == k){
		for (int t = 1; t <= k; t++)
			for (auto [x,idx] : tindex[t])
				add2(x,idx,t);
		for (auto Q : event){
			if (Q.first.second == n){
				int x = Q.second.first, idx = Q.second.second;
				auto it = get2(1, 0, m, x);
				ans[idx] = max(cmp[x]-cmp[it.first], cmp[it.second]-cmp[x]);
			}
		}
	}
	for (int i = 0; i < q; i++)
		printf("%d\n", ans[i]);
}

Compilation message

new_home.cpp: In function 'int main()':
new_home.cpp:157:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < event.size(); i++)
                  ~~^~~~~~~~~~~~~~
new_home.cpp:216:14: warning: decomposition declaration only available with -std=c++1z or -std=gnu++1z
    for (auto [x,idx] : tindex[t])
              ^
new_home.cpp:139:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d%d", &n, &k, &q);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~
new_home.cpp:143:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%d%d", &x, &t, &a, &b);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
new_home.cpp:150:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &l, &y);
   ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 67 ms 127096 KB Output is correct
2 Correct 66 ms 127096 KB Output is correct
3 Correct 67 ms 127096 KB Output is correct
4 Correct 69 ms 127096 KB Output is correct
5 Correct 67 ms 127228 KB Output is correct
6 Correct 68 ms 127224 KB Output is correct
7 Correct 68 ms 127224 KB Output is correct
8 Correct 69 ms 127224 KB Output is correct
9 Correct 68 ms 127352 KB Output is correct
10 Correct 71 ms 127352 KB Output is correct
11 Correct 67 ms 127224 KB Output is correct
12 Correct 68 ms 127224 KB Output is correct
13 Correct 67 ms 127224 KB Output is correct
14 Correct 67 ms 127224 KB Output is correct
15 Correct 80 ms 127224 KB Output is correct
16 Correct 76 ms 127244 KB Output is correct
17 Correct 68 ms 127224 KB Output is correct
18 Correct 68 ms 127224 KB Output is correct
19 Correct 67 ms 127224 KB Output is correct
20 Correct 70 ms 127224 KB Output is correct
21 Correct 66 ms 127224 KB Output is correct
22 Correct 67 ms 127248 KB Output is correct
23 Correct 67 ms 127224 KB Output is correct
24 Correct 67 ms 127224 KB Output is correct
25 Correct 69 ms 127224 KB Output is correct
26 Correct 72 ms 127224 KB Output is correct
27 Correct 68 ms 127224 KB Output is correct
28 Correct 68 ms 127224 KB Output is correct
29 Correct 68 ms 127224 KB Output is correct
30 Correct 66 ms 127224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 67 ms 127096 KB Output is correct
2 Correct 66 ms 127096 KB Output is correct
3 Correct 67 ms 127096 KB Output is correct
4 Correct 69 ms 127096 KB Output is correct
5 Correct 67 ms 127228 KB Output is correct
6 Correct 68 ms 127224 KB Output is correct
7 Correct 68 ms 127224 KB Output is correct
8 Correct 69 ms 127224 KB Output is correct
9 Correct 68 ms 127352 KB Output is correct
10 Correct 71 ms 127352 KB Output is correct
11 Correct 67 ms 127224 KB Output is correct
12 Correct 68 ms 127224 KB Output is correct
13 Correct 67 ms 127224 KB Output is correct
14 Correct 67 ms 127224 KB Output is correct
15 Correct 80 ms 127224 KB Output is correct
16 Correct 76 ms 127244 KB Output is correct
17 Correct 68 ms 127224 KB Output is correct
18 Correct 68 ms 127224 KB Output is correct
19 Correct 67 ms 127224 KB Output is correct
20 Correct 70 ms 127224 KB Output is correct
21 Correct 66 ms 127224 KB Output is correct
22 Correct 67 ms 127248 KB Output is correct
23 Correct 67 ms 127224 KB Output is correct
24 Correct 67 ms 127224 KB Output is correct
25 Correct 69 ms 127224 KB Output is correct
26 Correct 72 ms 127224 KB Output is correct
27 Correct 68 ms 127224 KB Output is correct
28 Correct 68 ms 127224 KB Output is correct
29 Correct 68 ms 127224 KB Output is correct
30 Correct 66 ms 127224 KB Output is correct
31 Correct 2652 ms 159936 KB Output is correct
32 Correct 340 ms 132068 KB Output is correct
33 Correct 1350 ms 137896 KB Output is correct
34 Correct 2326 ms 143104 KB Output is correct
35 Correct 2184 ms 155536 KB Output is correct
36 Correct 1340 ms 145628 KB Output is correct
37 Correct 975 ms 132704 KB Output is correct
38 Correct 826 ms 132072 KB Output is correct
39 Correct 671 ms 132044 KB Output is correct
40 Correct 665 ms 132072 KB Output is correct
41 Correct 632 ms 132068 KB Output is correct
42 Correct 619 ms 132068 KB Output is correct
43 Correct 182 ms 134108 KB Output is correct
44 Correct 604 ms 132072 KB Output is correct
45 Correct 539 ms 132076 KB Output is correct
46 Correct 478 ms 132068 KB Output is correct
47 Correct 346 ms 132068 KB Output is correct
48 Correct 360 ms 132152 KB Output is correct
49 Correct 444 ms 132068 KB Output is correct
50 Correct 525 ms 132076 KB Output is correct
51 Correct 429 ms 132068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1122 ms 179248 KB Output is correct
2 Correct 1069 ms 177980 KB Output is correct
3 Correct 824 ms 170940 KB Output is correct
4 Correct 1069 ms 179132 KB Output is correct
5 Correct 1105 ms 177212 KB Output is correct
6 Correct 1099 ms 177852 KB Output is correct
7 Correct 784 ms 170940 KB Output is correct
8 Correct 1002 ms 179308 KB Output is correct
9 Correct 1020 ms 179260 KB Output is correct
10 Correct 1042 ms 178620 KB Output is correct
11 Correct 811 ms 177852 KB Output is correct
12 Correct 881 ms 178624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 1178 ms 179004 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 67 ms 127096 KB Output is correct
2 Correct 66 ms 127096 KB Output is correct
3 Correct 67 ms 127096 KB Output is correct
4 Correct 69 ms 127096 KB Output is correct
5 Correct 67 ms 127228 KB Output is correct
6 Correct 68 ms 127224 KB Output is correct
7 Correct 68 ms 127224 KB Output is correct
8 Correct 69 ms 127224 KB Output is correct
9 Correct 68 ms 127352 KB Output is correct
10 Correct 71 ms 127352 KB Output is correct
11 Correct 67 ms 127224 KB Output is correct
12 Correct 68 ms 127224 KB Output is correct
13 Correct 67 ms 127224 KB Output is correct
14 Correct 67 ms 127224 KB Output is correct
15 Correct 80 ms 127224 KB Output is correct
16 Correct 76 ms 127244 KB Output is correct
17 Correct 68 ms 127224 KB Output is correct
18 Correct 68 ms 127224 KB Output is correct
19 Correct 67 ms 127224 KB Output is correct
20 Correct 70 ms 127224 KB Output is correct
21 Correct 66 ms 127224 KB Output is correct
22 Correct 67 ms 127248 KB Output is correct
23 Correct 67 ms 127224 KB Output is correct
24 Correct 67 ms 127224 KB Output is correct
25 Correct 69 ms 127224 KB Output is correct
26 Correct 72 ms 127224 KB Output is correct
27 Correct 68 ms 127224 KB Output is correct
28 Correct 68 ms 127224 KB Output is correct
29 Correct 68 ms 127224 KB Output is correct
30 Correct 66 ms 127224 KB Output is correct
31 Correct 2652 ms 159936 KB Output is correct
32 Correct 340 ms 132068 KB Output is correct
33 Correct 1350 ms 137896 KB Output is correct
34 Correct 2326 ms 143104 KB Output is correct
35 Correct 2184 ms 155536 KB Output is correct
36 Correct 1340 ms 145628 KB Output is correct
37 Correct 975 ms 132704 KB Output is correct
38 Correct 826 ms 132072 KB Output is correct
39 Correct 671 ms 132044 KB Output is correct
40 Correct 665 ms 132072 KB Output is correct
41 Correct 632 ms 132068 KB Output is correct
42 Correct 619 ms 132068 KB Output is correct
43 Correct 182 ms 134108 KB Output is correct
44 Correct 604 ms 132072 KB Output is correct
45 Correct 539 ms 132076 KB Output is correct
46 Correct 478 ms 132068 KB Output is correct
47 Correct 346 ms 132068 KB Output is correct
48 Correct 360 ms 132152 KB Output is correct
49 Correct 444 ms 132068 KB Output is correct
50 Correct 525 ms 132076 KB Output is correct
51 Correct 429 ms 132068 KB Output is correct
52 Correct 254 ms 136524 KB Output is correct
53 Correct 238 ms 132992 KB Output is correct
54 Correct 1531 ms 162268 KB Output is correct
55 Correct 498 ms 133340 KB Output is correct
56 Correct 445 ms 134364 KB Output is correct
57 Correct 597 ms 132148 KB Output is correct
58 Correct 521 ms 133212 KB Output is correct
59 Correct 449 ms 134108 KB Output is correct
60 Correct 615 ms 132188 KB Output is correct
61 Correct 155 ms 136924 KB Output is correct
62 Correct 253 ms 136668 KB Output is correct
63 Correct 804 ms 149424 KB Output is correct
64 Correct 1080 ms 148548 KB Output is correct
65 Correct 1058 ms 137052 KB Output is correct
66 Correct 726 ms 132068 KB Output is correct
67 Correct 447 ms 133340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 67 ms 127096 KB Output is correct
2 Correct 66 ms 127096 KB Output is correct
3 Correct 67 ms 127096 KB Output is correct
4 Correct 69 ms 127096 KB Output is correct
5 Correct 67 ms 127228 KB Output is correct
6 Correct 68 ms 127224 KB Output is correct
7 Correct 68 ms 127224 KB Output is correct
8 Correct 69 ms 127224 KB Output is correct
9 Correct 68 ms 127352 KB Output is correct
10 Correct 71 ms 127352 KB Output is correct
11 Correct 67 ms 127224 KB Output is correct
12 Correct 68 ms 127224 KB Output is correct
13 Correct 67 ms 127224 KB Output is correct
14 Correct 67 ms 127224 KB Output is correct
15 Correct 80 ms 127224 KB Output is correct
16 Correct 76 ms 127244 KB Output is correct
17 Correct 68 ms 127224 KB Output is correct
18 Correct 68 ms 127224 KB Output is correct
19 Correct 67 ms 127224 KB Output is correct
20 Correct 70 ms 127224 KB Output is correct
21 Correct 66 ms 127224 KB Output is correct
22 Correct 67 ms 127248 KB Output is correct
23 Correct 67 ms 127224 KB Output is correct
24 Correct 67 ms 127224 KB Output is correct
25 Correct 69 ms 127224 KB Output is correct
26 Correct 72 ms 127224 KB Output is correct
27 Correct 68 ms 127224 KB Output is correct
28 Correct 68 ms 127224 KB Output is correct
29 Correct 68 ms 127224 KB Output is correct
30 Correct 66 ms 127224 KB Output is correct
31 Correct 2652 ms 159936 KB Output is correct
32 Correct 340 ms 132068 KB Output is correct
33 Correct 1350 ms 137896 KB Output is correct
34 Correct 2326 ms 143104 KB Output is correct
35 Correct 2184 ms 155536 KB Output is correct
36 Correct 1340 ms 145628 KB Output is correct
37 Correct 975 ms 132704 KB Output is correct
38 Correct 826 ms 132072 KB Output is correct
39 Correct 671 ms 132044 KB Output is correct
40 Correct 665 ms 132072 KB Output is correct
41 Correct 632 ms 132068 KB Output is correct
42 Correct 619 ms 132068 KB Output is correct
43 Correct 182 ms 134108 KB Output is correct
44 Correct 604 ms 132072 KB Output is correct
45 Correct 539 ms 132076 KB Output is correct
46 Correct 478 ms 132068 KB Output is correct
47 Correct 346 ms 132068 KB Output is correct
48 Correct 360 ms 132152 KB Output is correct
49 Correct 444 ms 132068 KB Output is correct
50 Correct 525 ms 132076 KB Output is correct
51 Correct 429 ms 132068 KB Output is correct
52 Correct 1122 ms 179248 KB Output is correct
53 Correct 1069 ms 177980 KB Output is correct
54 Correct 824 ms 170940 KB Output is correct
55 Correct 1069 ms 179132 KB Output is correct
56 Correct 1105 ms 177212 KB Output is correct
57 Correct 1099 ms 177852 KB Output is correct
58 Correct 784 ms 170940 KB Output is correct
59 Correct 1002 ms 179308 KB Output is correct
60 Correct 1020 ms 179260 KB Output is correct
61 Correct 1042 ms 178620 KB Output is correct
62 Correct 811 ms 177852 KB Output is correct
63 Correct 881 ms 178624 KB Output is correct
64 Incorrect 1178 ms 179004 KB Output isn't correct
65 Halted 0 ms 0 KB -