Submission #625230

# Submission time Handle Problem Language Result Execution time Memory
625230 2022-08-09T16:30:20 Z QwertyPi New Home (APIO18_new_home) C++14
47 / 100
5000 ms 526800 KB
#include <bits/stdc++.h>
#pragma optimize("unroll-loops")
#pragma optimize("Ofast")
const int N = 300013;
using namespace std;

struct Store{
	int x, ty, a, b;
};

struct Query{
	int t, ty, x, id;
};

struct Pair{
	Pair() = default;
	Pair(int _x, int _y, int _d) : x(_x), y(_y), d(_d){};
	int x, y, d;
};

struct DS{
	vector<int> sp[N * 2], bits[N * 2];
	string name;
	DS() = default;
	DS(string _n) : name(_n){}
	void insert(Pair p){
		int x = p.x + 1, y = p.y + 1;
		for(int i = x; i < N * 2; i += i & -i){
			sp[i].push_back(y);
		}
	}
	void parse(){
		for(int i = 0; i < N * 2; i++) sp[i].push_back(0);
		for(int i = 0; i < N * 2; i++){
			sort(sp[i].begin(), sp[i].end());
			sp[i].resize(unique(sp[i].begin(), sp[i].end()) - sp[i].begin());
			bits[i].resize(sp[i].size());
		}
	}
	void add(Pair p){
		int x = p.x + 1, y = p.y + 1;
		for(int i = x; i < N * 2; i += i & -i){
			int idx = lower_bound(sp[i].begin(), sp[i].end(), y) - sp[i].begin();
			for(int j = idx; j < sp[i].size(); j += j & -j){
				bits[i][j] += p.d;
			}
		}
	}
	int qry(int x, int y){
		x++; y++;
		int ret = 0;
		for(int i = x; i; i -= i & -i){
			int idx = upper_bound(sp[i].begin(), sp[i].end(), y) - 1 - sp[i].begin();
			for(int j = idx; j; j -= j & -j){
				ret += bits[i][j];
			}
		}
		return ret;
	}
	int qry(int x1, int x2, int y1, int y2){
		return qry(x2, y2) + qry(x1 - 1, y1 - 1) - qry(x2, y1 - 1) - qry(x1 - 1, y2);
	}
} ds;

struct BIT{
	int bit[N];
	void add(int p, int v){
		p++;
		for(int i = p; i < N; i += i & -i){
			bit[i] += v;
		}
	}
	int qry(int p){
		int ret = 0;
		p++;
		for(int i = p; i; i -= i & -i){
			ret += bit[i];
		}
		return ret;
	}
	int qry(int l, int r){
		return qry(r) - qry(l - 1);
	}
} bit;

vector<Store> stores;
vector<Query> Q;
vector<Pair> C[N * 3], D[N * 3];
int m;
int p[N * 2];
int ans[N];

int n, k, q;

bool check(int x1, int x2){
	int l = lower_bound(p, p + m, x1) - p;
	int r = upper_bound(p, p + m, x2) - 1 - p;
	int c1 = bit.qry(l, r);
	int c2 = ds.qry(l, r, l, r);
	assert(c1 - c2 >= 0 && c1 - c2 <= k);
	return c1 - c2 == k;
}

int qry(int x){
	int l = 0, r = 1e9;
	while(l != r){
		int mid = (l + r) / 2;
		if(check(p[x] - mid, p[x] + mid)){
			r = mid;
		}else{
			l = mid + 1;
		}
	}
	return l >= 9e8 ? -1 : l;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin >> n >> k >> q;
	vector<int> sp;
	for(int i = 0; i < n; i++){
		int x, ty, a, b;
		cin >> x >> ty >> a >> b;
		stores.push_back({x, ty, a, b + 1}); 
		sp.push_back(a);
		sp.push_back(b + 1);
	}
	for(int i = 0; i < q; i++){
		int x, t;
		cin >> x >> t;
		Q.push_back({t, 2, x, i});
	}
	sort(sp.begin(), sp.end());
	sp.resize(unique(sp.begin(), sp.end()) - sp.begin());
	{
		map<int, int> M;
		for(int i = 0; i < sp.size(); i++){
			M[sp[i]] = i;
		}
		for(auto& st : stores){
			st.a = M[st.a];
			st.b = M[st.b];
		}
		for(auto& q : Q){
			q.t = upper_bound(sp.begin(), sp.end(), q.t) - sp.begin() - 1; 
		}
	}
	{
		vector<int> xs;
		for(auto st : stores){
			xs.push_back(st.x);
		}
		for(auto q : Q){
			xs.push_back(q.x);
		}
		sort(xs.begin(), xs.end());
		xs.resize(unique(xs.begin(), xs.end()) - xs.begin());
		m = xs.size();
		for(int i = 0; i < m; i++){
			p[i] = xs[i];
		}
		map<int, int> M;
		for(int i = 0; i < m; i++){
			M[xs[i]] = i;
		}
		for(auto& st : stores){
			st.x = M[st.x];
		}
		for(auto& q : Q){
			q.x = M[q.x];
		}
	}
	for(auto st : stores){
		Q.push_back({st.a, 1, st.x, st.ty});
		Q.push_back({st.b, 0, st.x, st.ty});
	}
	
	sort(Q.begin(), Q.end(), [](Query a, Query b){
		return a.t < b.t || a.t == b.t && a.ty < b.ty;
	});
	multiset<int> vset[N];
	for(int i = 0; i < Q.size(); i++){
		Query q = Q[i];
		auto ptr = vset[q.id].begin();
		switch(q.ty){
			case 0:
				ptr = vset[q.id].lower_bound(q.x);
				assert(ptr != vset[q.id].end());
				if(ptr != vset[q.id].begin()) C[i].push_back({*prev(ptr), *ptr, -1});
				if(next(ptr) != vset[q.id].end()) C[i].push_back({*ptr, *next(ptr), -1});
				if(ptr != vset[q.id].begin() && next(ptr) != vset[q.id].end()) C[i].push_back({*prev(ptr), *next(ptr), 1});
				D[i].push_back({q.x, -1, -1});
				vset[q.id].erase(ptr);
				break;
			case 1:
				ptr = vset[q.id].lower_bound(q.x);
				if(ptr != vset[q.id].end() && ptr != vset[q.id].begin()) C[i].push_back({*prev(ptr), *ptr, -1});
				if(ptr != vset[q.id].end()) C[i].push_back({q.x, *ptr, 1});
				if(ptr != vset[q.id].begin()) C[i].push_back({*prev(ptr), q.x, 1});
				D[i].push_back({q.x, -1, 1});
				vset[q.id].insert(q.x);
				break;
			case 2:
				break;
			default:
				assert(false);
		}
	}
	for(int i = 0; i < Q.size(); i++){
		for(auto p : C[i]){
			ds.insert(p);
		}
	}
	ds.parse();
	for(int i = 0; i < Q.size(); i++){
		for(auto p : C[i]){
			ds.add(p);
		}
		for(auto p : D[i]){
			bit.add(p.x, p.d);
		}
		Query q = Q[i];
		switch(q.ty){
			case 0:
				break;
			case 1:
				break;
			case 2: ans[q.id] = qry(q.x); break;
			default: assert(false);
		}
	}
	for(int i = 0; i < q; i++){
		cout << ans[i] << '\n';
	}
}

Compilation message

new_home.cpp:2: warning: ignoring '#pragma optimize ' [-Wunknown-pragmas]
    2 | #pragma optimize("unroll-loops")
      | 
new_home.cpp:3: warning: ignoring '#pragma optimize ' [-Wunknown-pragmas]
    3 | #pragma optimize("Ofast")
      | 
new_home.cpp: In member function 'void DS::add(Pair)':
new_home.cpp:44:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   44 |    for(int j = idx; j < sp[i].size(); j += j & -j){
      |                     ~~^~~~~~~~~~~~~~
new_home.cpp: In function 'int main()':
new_home.cpp:138:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  138 |   for(int i = 0; i < sp.size(); i++){
      |                  ~~^~~~~~~~~~~
new_home.cpp: In lambda function:
new_home.cpp:180:34: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
  180 |   return a.t < b.t || a.t == b.t && a.ty < b.ty;
      |                       ~~~~~~~~~~~^~~~~~~~~~~~~~
new_home.cpp: In function 'int main()':
new_home.cpp:183:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Query>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  183 |  for(int i = 0; i < Q.size(); i++){
      |                 ~~^~~~~~~~~~
new_home.cpp:210:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Query>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  210 |  for(int i = 0; i < Q.size(); i++){
      |                 ~~^~~~~~~~~~
new_home.cpp:216:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Query>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  216 |  for(int i = 0; i < Q.size(); i++){
      |                 ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 99 ms 122384 KB Output is correct
2 Correct 95 ms 122368 KB Output is correct
3 Correct 95 ms 122372 KB Output is correct
4 Correct 116 ms 122372 KB Output is correct
5 Correct 117 ms 122840 KB Output is correct
6 Correct 103 ms 122716 KB Output is correct
7 Correct 98 ms 122544 KB Output is correct
8 Correct 128 ms 122564 KB Output is correct
9 Correct 98 ms 122504 KB Output is correct
10 Correct 119 ms 122812 KB Output is correct
11 Correct 99 ms 122600 KB Output is correct
12 Correct 104 ms 122656 KB Output is correct
13 Correct 112 ms 122580 KB Output is correct
14 Correct 103 ms 122624 KB Output is correct
15 Correct 103 ms 122540 KB Output is correct
16 Correct 100 ms 122588 KB Output is correct
17 Correct 104 ms 122572 KB Output is correct
18 Correct 104 ms 122568 KB Output is correct
19 Correct 97 ms 122508 KB Output is correct
20 Correct 109 ms 122772 KB Output is correct
21 Correct 97 ms 122452 KB Output is correct
22 Correct 105 ms 122444 KB Output is correct
23 Correct 110 ms 122492 KB Output is correct
24 Correct 108 ms 122596 KB Output is correct
25 Correct 105 ms 122700 KB Output is correct
26 Correct 120 ms 122668 KB Output is correct
27 Correct 110 ms 122612 KB Output is correct
28 Correct 105 ms 122612 KB Output is correct
29 Correct 105 ms 122572 KB Output is correct
30 Correct 116 ms 122544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 99 ms 122384 KB Output is correct
2 Correct 95 ms 122368 KB Output is correct
3 Correct 95 ms 122372 KB Output is correct
4 Correct 116 ms 122372 KB Output is correct
5 Correct 117 ms 122840 KB Output is correct
6 Correct 103 ms 122716 KB Output is correct
7 Correct 98 ms 122544 KB Output is correct
8 Correct 128 ms 122564 KB Output is correct
9 Correct 98 ms 122504 KB Output is correct
10 Correct 119 ms 122812 KB Output is correct
11 Correct 99 ms 122600 KB Output is correct
12 Correct 104 ms 122656 KB Output is correct
13 Correct 112 ms 122580 KB Output is correct
14 Correct 103 ms 122624 KB Output is correct
15 Correct 103 ms 122540 KB Output is correct
16 Correct 100 ms 122588 KB Output is correct
17 Correct 104 ms 122572 KB Output is correct
18 Correct 104 ms 122568 KB Output is correct
19 Correct 97 ms 122508 KB Output is correct
20 Correct 109 ms 122772 KB Output is correct
21 Correct 97 ms 122452 KB Output is correct
22 Correct 105 ms 122444 KB Output is correct
23 Correct 110 ms 122492 KB Output is correct
24 Correct 108 ms 122596 KB Output is correct
25 Correct 105 ms 122700 KB Output is correct
26 Correct 120 ms 122668 KB Output is correct
27 Correct 110 ms 122612 KB Output is correct
28 Correct 105 ms 122612 KB Output is correct
29 Correct 105 ms 122572 KB Output is correct
30 Correct 116 ms 122544 KB Output is correct
31 Correct 4471 ms 163640 KB Output is correct
32 Correct 607 ms 168824 KB Output is correct
33 Correct 4217 ms 163060 KB Output is correct
34 Correct 4562 ms 162560 KB Output is correct
35 Correct 4648 ms 163984 KB Output is correct
36 Correct 4176 ms 163720 KB Output is correct
37 Correct 3575 ms 162432 KB Output is correct
38 Correct 3336 ms 162216 KB Output is correct
39 Correct 2910 ms 161244 KB Output is correct
40 Correct 2977 ms 161200 KB Output is correct
41 Correct 1822 ms 148248 KB Output is correct
42 Correct 1380 ms 148040 KB Output is correct
43 Correct 321 ms 145164 KB Output is correct
44 Correct 1757 ms 148288 KB Output is correct
45 Correct 2015 ms 148328 KB Output is correct
46 Correct 2526 ms 148280 KB Output is correct
47 Correct 1437 ms 143004 KB Output is correct
48 Correct 1683 ms 144728 KB Output is correct
49 Correct 1894 ms 145656 KB Output is correct
50 Correct 1578 ms 144956 KB Output is correct
51 Correct 2197 ms 146216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 2988 ms 506336 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5121 ms 526800 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 99 ms 122384 KB Output is correct
2 Correct 95 ms 122368 KB Output is correct
3 Correct 95 ms 122372 KB Output is correct
4 Correct 116 ms 122372 KB Output is correct
5 Correct 117 ms 122840 KB Output is correct
6 Correct 103 ms 122716 KB Output is correct
7 Correct 98 ms 122544 KB Output is correct
8 Correct 128 ms 122564 KB Output is correct
9 Correct 98 ms 122504 KB Output is correct
10 Correct 119 ms 122812 KB Output is correct
11 Correct 99 ms 122600 KB Output is correct
12 Correct 104 ms 122656 KB Output is correct
13 Correct 112 ms 122580 KB Output is correct
14 Correct 103 ms 122624 KB Output is correct
15 Correct 103 ms 122540 KB Output is correct
16 Correct 100 ms 122588 KB Output is correct
17 Correct 104 ms 122572 KB Output is correct
18 Correct 104 ms 122568 KB Output is correct
19 Correct 97 ms 122508 KB Output is correct
20 Correct 109 ms 122772 KB Output is correct
21 Correct 97 ms 122452 KB Output is correct
22 Correct 105 ms 122444 KB Output is correct
23 Correct 110 ms 122492 KB Output is correct
24 Correct 108 ms 122596 KB Output is correct
25 Correct 105 ms 122700 KB Output is correct
26 Correct 120 ms 122668 KB Output is correct
27 Correct 110 ms 122612 KB Output is correct
28 Correct 105 ms 122612 KB Output is correct
29 Correct 105 ms 122572 KB Output is correct
30 Correct 116 ms 122544 KB Output is correct
31 Correct 4471 ms 163640 KB Output is correct
32 Correct 607 ms 168824 KB Output is correct
33 Correct 4217 ms 163060 KB Output is correct
34 Correct 4562 ms 162560 KB Output is correct
35 Correct 4648 ms 163984 KB Output is correct
36 Correct 4176 ms 163720 KB Output is correct
37 Correct 3575 ms 162432 KB Output is correct
38 Correct 3336 ms 162216 KB Output is correct
39 Correct 2910 ms 161244 KB Output is correct
40 Correct 2977 ms 161200 KB Output is correct
41 Correct 1822 ms 148248 KB Output is correct
42 Correct 1380 ms 148040 KB Output is correct
43 Correct 321 ms 145164 KB Output is correct
44 Correct 1757 ms 148288 KB Output is correct
45 Correct 2015 ms 148328 KB Output is correct
46 Correct 2526 ms 148280 KB Output is correct
47 Correct 1437 ms 143004 KB Output is correct
48 Correct 1683 ms 144728 KB Output is correct
49 Correct 1894 ms 145656 KB Output is correct
50 Correct 1578 ms 144956 KB Output is correct
51 Correct 2197 ms 146216 KB Output is correct
52 Correct 591 ms 132628 KB Output is correct
53 Correct 547 ms 132204 KB Output is correct
54 Correct 1201 ms 144236 KB Output is correct
55 Correct 1611 ms 142736 KB Output is correct
56 Correct 1504 ms 140520 KB Output is correct
57 Correct 1797 ms 146804 KB Output is correct
58 Correct 1443 ms 142668 KB Output is correct
59 Correct 1209 ms 140180 KB Output is correct
60 Correct 1557 ms 146612 KB Output is correct
61 Correct 239 ms 132148 KB Output is correct
62 Correct 647 ms 132784 KB Output is correct
63 Correct 1128 ms 139472 KB Output is correct
64 Correct 1276 ms 141948 KB Output is correct
65 Correct 1563 ms 146468 KB Output is correct
66 Correct 2040 ms 148260 KB Output is correct
67 Correct 488 ms 148996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 99 ms 122384 KB Output is correct
2 Correct 95 ms 122368 KB Output is correct
3 Correct 95 ms 122372 KB Output is correct
4 Correct 116 ms 122372 KB Output is correct
5 Correct 117 ms 122840 KB Output is correct
6 Correct 103 ms 122716 KB Output is correct
7 Correct 98 ms 122544 KB Output is correct
8 Correct 128 ms 122564 KB Output is correct
9 Correct 98 ms 122504 KB Output is correct
10 Correct 119 ms 122812 KB Output is correct
11 Correct 99 ms 122600 KB Output is correct
12 Correct 104 ms 122656 KB Output is correct
13 Correct 112 ms 122580 KB Output is correct
14 Correct 103 ms 122624 KB Output is correct
15 Correct 103 ms 122540 KB Output is correct
16 Correct 100 ms 122588 KB Output is correct
17 Correct 104 ms 122572 KB Output is correct
18 Correct 104 ms 122568 KB Output is correct
19 Correct 97 ms 122508 KB Output is correct
20 Correct 109 ms 122772 KB Output is correct
21 Correct 97 ms 122452 KB Output is correct
22 Correct 105 ms 122444 KB Output is correct
23 Correct 110 ms 122492 KB Output is correct
24 Correct 108 ms 122596 KB Output is correct
25 Correct 105 ms 122700 KB Output is correct
26 Correct 120 ms 122668 KB Output is correct
27 Correct 110 ms 122612 KB Output is correct
28 Correct 105 ms 122612 KB Output is correct
29 Correct 105 ms 122572 KB Output is correct
30 Correct 116 ms 122544 KB Output is correct
31 Correct 4471 ms 163640 KB Output is correct
32 Correct 607 ms 168824 KB Output is correct
33 Correct 4217 ms 163060 KB Output is correct
34 Correct 4562 ms 162560 KB Output is correct
35 Correct 4648 ms 163984 KB Output is correct
36 Correct 4176 ms 163720 KB Output is correct
37 Correct 3575 ms 162432 KB Output is correct
38 Correct 3336 ms 162216 KB Output is correct
39 Correct 2910 ms 161244 KB Output is correct
40 Correct 2977 ms 161200 KB Output is correct
41 Correct 1822 ms 148248 KB Output is correct
42 Correct 1380 ms 148040 KB Output is correct
43 Correct 321 ms 145164 KB Output is correct
44 Correct 1757 ms 148288 KB Output is correct
45 Correct 2015 ms 148328 KB Output is correct
46 Correct 2526 ms 148280 KB Output is correct
47 Correct 1437 ms 143004 KB Output is correct
48 Correct 1683 ms 144728 KB Output is correct
49 Correct 1894 ms 145656 KB Output is correct
50 Correct 1578 ms 144956 KB Output is correct
51 Correct 2197 ms 146216 KB Output is correct
52 Runtime error 2988 ms 506336 KB Execution killed with signal 6
53 Halted 0 ms 0 KB -