Submission #414418

# Submission time Handle Problem Language Result Execution time Memory
414418 2021-05-30T12:12:05 Z hhhhaura New Home (APIO18_new_home) C++14
100 / 100
4401 ms 731200 KB
#define wiwihorz
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma loop-opt(on)

#define rep(i, a, b) for(int i = a; i <= b; i++)
#define rrep(i, a, b) for(int i = b; i >= a; i--)
#define all(x) x.begin(), x.end()
#define INF 1e9
using namespace std;
#define pii pair<int, int>
#ifdef wiwihorz
#define print(a...) kout("[" + string(#a) + "] = ", a)
void vprint(auto L, auto R) { while(L < R) cerr << *L << " \n"[next(L) == R], ++L;}
void kout() { cerr << endl; }
template<class T1, class ... T2> void kout(T1 a, T2 ... e) { cerr << a << " ", kout(e...);}
#else
#define print(...) 0
#define vprint(...) 0
#endif
namespace seg {
	const int P = 5e7;
	int p = 0, root = 0;
	vector<int> lch, rch, seg;
	int get_new() {return ++p;}
	void init_() {
		lch.assign(P, 0);
		rch.assign(P, 0);
		seg.assign(P, INF);
		root = get_new();
	}
	void pull(int nd) {
		seg[nd] = min(seg[lch[nd]], seg[rch[nd]]);
	}
	void modify(int nd, int L, int R, int x, int v) {
		if(L == R) seg[nd] = v;
		else {
			int mid = (L + R) / 2;
			if(!lch[nd]) lch[nd] = get_new();
			if(!rch[nd]) rch[nd] = get_new();
			if(x <= mid) modify(lch[nd], L, mid, x, v);
			else modify(rch[nd], mid + 1, R, x, v);
			pull(nd); 
		}
	}
/*	int query(int nd, int L, int R, int l, int r) {
		if(!nd) return INF;
		else if(l <= L && r >= R) return seg[nd];
		else {
			int mid = (L + R) / 2;
			return min(query(lch[nd], L, mid, l, r),
				query(rch[nd], mid + 1, R, l, r));
		}
	}
*/
	int get_lb(int nd, int L, int R, int x, int cur) {
		int mid = (L + R) / 2;
		if(!nd) {
			int ans = 2 * x - cur;
			if(ans >= INF) return -1;
			else return max(x, ans) - x;
		}
		else if(L == R || !nd) {
			int l = 2 * x - max(x, L);
			if(L == INF || cur < l) return -1;
			else return abs(max(x, L) - x);
		}
		else {
			int mid = (L + R) / 2, l = 2 * x - mid;
			if(min(cur, seg[rch[nd]]) >= l) 
				return get_lb(lch[nd], L, mid, x, min(cur, seg[rch[nd]]));
			else return get_lb(rch[nd], mid + 1, R, x, cur);
		}
	}

/*	int ask(int x) {
		return query(root, 1, INF, x, INF);
	}
*/	int answer(int x) {
		return get_lb(root, 1, INF, x, INF);
	}
	void change(int x, int v) {
		modify(root, 1, INF, x, v);
	}
};
namespace solver {
	int n, k, q;
	struct query {
		int x, tp, id;
		bool operator<(query b) {
			if(x != b.x) return x < b.x;
			else return tp < b.tp;
		}
	};
	vector<map<int, int>> s;
	vector<multiset<pii>> cur;
	vector<pii> ans, stores;
	vector<query> qq;
	vector<int> v;
	void init_(int _n, int _k, int _q) {
		n = _n, k = _k, q = _q;
		s.assign(k + 1, map<int, int>());
		cur.assign(n + q + 2, multiset<pii>());
		ans.assign(q + 1, {0, 0});
		stores.assign(n + k + 1, {0, 0});
		qq.clear(), v.clear();
		seg::init_();	
	}
	void add_store(int id, int l, int tp, int a, int b) {
		stores[id] = {l, tp};
		v.push_back(l);
		qq.push_back({a, 1, id});
		qq.push_back({b + 1, 2, id});
	}
	void add_query(int id, int l, int y) {
		ans[id].first = l;
		v.push_back(l);
		qq.push_back({y, 3, id});
	}
	void update(int x) {
		auto it = cur[x].begin();
		if(it == cur[x].end()) seg::change(v[x], INF);
		else seg::change(v[x], v[it -> first]);
	}
	void add(int id) {
		int tp = stores[id].second, l = stores[id].first;
		s[tp][l] ++;
		if(s[tp][l] > 1) return; 
		int pre, nxt = 0;
		auto it1 = s[tp].upper_bound(l);
		auto it2 = s[tp].lower_bound(l);
		if(it2 == s[tp].begin()) pre = 0;
		else pre = prev(it2) -> first;
		if(it1 != s[tp].end()) {
			nxt = it1 -> first;
			cur[nxt].erase(cur[nxt].find({pre, tp}));
			cur[nxt].insert({l, tp});
			update(nxt);
		} 
		cur[l].insert({pre, tp});
		update(l);
	}
	int query(int x) {
		return seg::answer(x);
	}

/*	int query(int x) {
		int L = -1, R = 1e8;
		while(R - L > 1) {
			int mid = (L + R) / 2;
			int val = seg::ask(x + mid + 1);
			if(val < x - mid) L = mid;
			else R = mid;
		}
		if(R == 1e8) return -1;
		else return R;
	}
*/	void remove(int id) {
		int tp = stores[id].second, l = stores[id].first;
		s[tp][l] --;
		if(s[tp][l] > 0) return;
		int pre, nxt;
		s[tp].erase(s[tp].find(l));
		auto it1 = s[tp].upper_bound(l);
		auto it2 = s[tp].lower_bound(l);
		if(it2 == s[tp].begin()) pre = 0;
		else pre = prev(it2) -> first;
		if(it1 != s[tp].end()) {
			nxt = it1 -> first;
			cur[nxt].erase(cur[nxt].find({l, tp}));
			cur[nxt].insert({pre, tp});
			update(nxt);
		} 
		cur[l].erase({pre, tp});
		update(l);
	}
	void solve() {
		rep(i, 1, k) add_store(n + i, INF, i, -INF, INF);
		v.push_back(-INF);
		sort(all(v)), v.resize(unique(all(v)) - v.begin());
		rep(i, 1, n + k) stores[i].first = lower_bound(all(v), stores[i].first) - v.begin();
		rep(i, 1, q) ans[i].first = lower_bound(all(v), ans[i].first) - v.begin();
		sort(all(qq));
		for(auto i : qq) {
			if(i.tp == 1) add(i.id); 
			else if(i.tp == 2) remove(i.id);
			else ans[i.id].second = query(v[ans[i.id].first]);
		}
		rep(i, 1, q) cout << ans[i].second << "\n";
	}
};
using namespace solver;
signed main() {
	ios::sync_with_stdio(false), cin.tie(0);
	int n, k, q; cin >> n >> k >> q;
	init_(n, k, q);
	rep(i ,1, n) {
		int x, t, a, b;
		cin >> x >> t >> a >> b;
		add_store(i, x, t, a, b);
	}
	rep(i, 1, q) {
		int l, y; cin >> l >> y;
		add_query(i, l, y);
	}
	solve();
	return 0;

}

Compilation message

new_home.cpp:4: warning: ignoring '#pragma loop ' [-Wunknown-pragmas]
    4 | #pragma loop-opt(on)
      | 
new_home.cpp:14:13: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   14 | void vprint(auto L, auto R) { while(L < R) cerr << *L << " \n"[next(L) == R], ++L;}
      |             ^~~~
new_home.cpp:14:21: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   14 | void vprint(auto L, auto R) { while(L < R) cerr << *L << " \n"[next(L) == R], ++L;}
      |                     ^~~~
new_home.cpp: In function 'int seg::get_lb(int, int, int, int, int)':
new_home.cpp:57:7: warning: unused variable 'mid' [-Wunused-variable]
   57 |   int mid = (L + R) / 2;
      |       ^~~
# Verdict Execution time Memory Grader output
1 Correct 389 ms 587308 KB Output is correct
2 Correct 358 ms 587336 KB Output is correct
3 Correct 353 ms 587408 KB Output is correct
4 Correct 331 ms 587356 KB Output is correct
5 Correct 336 ms 587516 KB Output is correct
6 Correct 334 ms 587416 KB Output is correct
7 Correct 339 ms 587716 KB Output is correct
8 Correct 335 ms 587580 KB Output is correct
9 Correct 347 ms 587588 KB Output is correct
10 Correct 378 ms 587468 KB Output is correct
11 Correct 342 ms 587408 KB Output is correct
12 Correct 361 ms 587496 KB Output is correct
13 Correct 332 ms 587464 KB Output is correct
14 Correct 337 ms 587444 KB Output is correct
15 Correct 333 ms 587500 KB Output is correct
16 Correct 328 ms 587524 KB Output is correct
17 Correct 336 ms 587484 KB Output is correct
18 Correct 339 ms 587520 KB Output is correct
19 Correct 355 ms 587532 KB Output is correct
20 Correct 332 ms 587452 KB Output is correct
21 Correct 352 ms 587652 KB Output is correct
22 Correct 335 ms 587588 KB Output is correct
23 Correct 334 ms 587460 KB Output is correct
24 Correct 329 ms 587420 KB Output is correct
25 Correct 336 ms 587440 KB Output is correct
26 Correct 330 ms 587468 KB Output is correct
27 Correct 334 ms 587620 KB Output is correct
28 Correct 364 ms 587452 KB Output is correct
29 Correct 343 ms 587480 KB Output is correct
30 Correct 336 ms 587564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 389 ms 587308 KB Output is correct
2 Correct 358 ms 587336 KB Output is correct
3 Correct 353 ms 587408 KB Output is correct
4 Correct 331 ms 587356 KB Output is correct
5 Correct 336 ms 587516 KB Output is correct
6 Correct 334 ms 587416 KB Output is correct
7 Correct 339 ms 587716 KB Output is correct
8 Correct 335 ms 587580 KB Output is correct
9 Correct 347 ms 587588 KB Output is correct
10 Correct 378 ms 587468 KB Output is correct
11 Correct 342 ms 587408 KB Output is correct
12 Correct 361 ms 587496 KB Output is correct
13 Correct 332 ms 587464 KB Output is correct
14 Correct 337 ms 587444 KB Output is correct
15 Correct 333 ms 587500 KB Output is correct
16 Correct 328 ms 587524 KB Output is correct
17 Correct 336 ms 587484 KB Output is correct
18 Correct 339 ms 587520 KB Output is correct
19 Correct 355 ms 587532 KB Output is correct
20 Correct 332 ms 587452 KB Output is correct
21 Correct 352 ms 587652 KB Output is correct
22 Correct 335 ms 587588 KB Output is correct
23 Correct 334 ms 587460 KB Output is correct
24 Correct 329 ms 587420 KB Output is correct
25 Correct 336 ms 587440 KB Output is correct
26 Correct 330 ms 587468 KB Output is correct
27 Correct 334 ms 587620 KB Output is correct
28 Correct 364 ms 587452 KB Output is correct
29 Correct 343 ms 587480 KB Output is correct
30 Correct 336 ms 587564 KB Output is correct
31 Correct 824 ms 605608 KB Output is correct
32 Correct 427 ms 598740 KB Output is correct
33 Correct 809 ms 602080 KB Output is correct
34 Correct 810 ms 602188 KB Output is correct
35 Correct 841 ms 605656 KB Output is correct
36 Correct 825 ms 605368 KB Output is correct
37 Correct 655 ms 600456 KB Output is correct
38 Correct 634 ms 600264 KB Output is correct
39 Correct 581 ms 600192 KB Output is correct
40 Correct 597 ms 600196 KB Output is correct
41 Correct 619 ms 600544 KB Output is correct
42 Correct 622 ms 600476 KB Output is correct
43 Correct 442 ms 599964 KB Output is correct
44 Correct 624 ms 600456 KB Output is correct
45 Correct 602 ms 600572 KB Output is correct
46 Correct 608 ms 600480 KB Output is correct
47 Correct 525 ms 599988 KB Output is correct
48 Correct 529 ms 599816 KB Output is correct
49 Correct 632 ms 600004 KB Output is correct
50 Correct 566 ms 600220 KB Output is correct
51 Correct 546 ms 600060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3256 ms 682392 KB Output is correct
2 Correct 3484 ms 672420 KB Output is correct
3 Correct 3898 ms 730836 KB Output is correct
4 Correct 3350 ms 695408 KB Output is correct
5 Correct 3495 ms 674908 KB Output is correct
6 Correct 3462 ms 675320 KB Output is correct
7 Correct 3126 ms 730836 KB Output is correct
8 Correct 2560 ms 695368 KB Output is correct
9 Correct 2227 ms 682952 KB Output is correct
10 Correct 2542 ms 676468 KB Output is correct
11 Correct 1689 ms 674536 KB Output is correct
12 Correct 1747 ms 676196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3440 ms 672680 KB Output is correct
2 Correct 692 ms 642156 KB Output is correct
3 Correct 3706 ms 675228 KB Output is correct
4 Correct 4291 ms 728740 KB Output is correct
5 Correct 3390 ms 686180 KB Output is correct
6 Correct 3679 ms 693248 KB Output is correct
7 Correct 3678 ms 674724 KB Output is correct
8 Correct 3770 ms 675076 KB Output is correct
9 Correct 3874 ms 729920 KB Output is correct
10 Correct 3169 ms 690196 KB Output is correct
11 Correct 2951 ms 679912 KB Output is correct
12 Correct 3322 ms 676228 KB Output is correct
13 Correct 1657 ms 673028 KB Output is correct
14 Correct 1634 ms 672168 KB Output is correct
15 Correct 1838 ms 674260 KB Output is correct
16 Correct 1984 ms 675596 KB Output is correct
17 Correct 2031 ms 673520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 389 ms 587308 KB Output is correct
2 Correct 358 ms 587336 KB Output is correct
3 Correct 353 ms 587408 KB Output is correct
4 Correct 331 ms 587356 KB Output is correct
5 Correct 336 ms 587516 KB Output is correct
6 Correct 334 ms 587416 KB Output is correct
7 Correct 339 ms 587716 KB Output is correct
8 Correct 335 ms 587580 KB Output is correct
9 Correct 347 ms 587588 KB Output is correct
10 Correct 378 ms 587468 KB Output is correct
11 Correct 342 ms 587408 KB Output is correct
12 Correct 361 ms 587496 KB Output is correct
13 Correct 332 ms 587464 KB Output is correct
14 Correct 337 ms 587444 KB Output is correct
15 Correct 333 ms 587500 KB Output is correct
16 Correct 328 ms 587524 KB Output is correct
17 Correct 336 ms 587484 KB Output is correct
18 Correct 339 ms 587520 KB Output is correct
19 Correct 355 ms 587532 KB Output is correct
20 Correct 332 ms 587452 KB Output is correct
21 Correct 352 ms 587652 KB Output is correct
22 Correct 335 ms 587588 KB Output is correct
23 Correct 334 ms 587460 KB Output is correct
24 Correct 329 ms 587420 KB Output is correct
25 Correct 336 ms 587440 KB Output is correct
26 Correct 330 ms 587468 KB Output is correct
27 Correct 334 ms 587620 KB Output is correct
28 Correct 364 ms 587452 KB Output is correct
29 Correct 343 ms 587480 KB Output is correct
30 Correct 336 ms 587564 KB Output is correct
31 Correct 824 ms 605608 KB Output is correct
32 Correct 427 ms 598740 KB Output is correct
33 Correct 809 ms 602080 KB Output is correct
34 Correct 810 ms 602188 KB Output is correct
35 Correct 841 ms 605656 KB Output is correct
36 Correct 825 ms 605368 KB Output is correct
37 Correct 655 ms 600456 KB Output is correct
38 Correct 634 ms 600264 KB Output is correct
39 Correct 581 ms 600192 KB Output is correct
40 Correct 597 ms 600196 KB Output is correct
41 Correct 619 ms 600544 KB Output is correct
42 Correct 622 ms 600476 KB Output is correct
43 Correct 442 ms 599964 KB Output is correct
44 Correct 624 ms 600456 KB Output is correct
45 Correct 602 ms 600572 KB Output is correct
46 Correct 608 ms 600480 KB Output is correct
47 Correct 525 ms 599988 KB Output is correct
48 Correct 529 ms 599816 KB Output is correct
49 Correct 632 ms 600004 KB Output is correct
50 Correct 566 ms 600220 KB Output is correct
51 Correct 546 ms 600060 KB Output is correct
52 Correct 911 ms 616196 KB Output is correct
53 Correct 874 ms 612436 KB Output is correct
54 Correct 831 ms 608836 KB Output is correct
55 Correct 700 ms 605604 KB Output is correct
56 Correct 730 ms 608176 KB Output is correct
57 Correct 649 ms 601704 KB Output is correct
58 Correct 713 ms 605452 KB Output is correct
59 Correct 766 ms 608092 KB Output is correct
60 Correct 661 ms 601840 KB Output is correct
61 Correct 629 ms 615800 KB Output is correct
62 Correct 894 ms 616060 KB Output is correct
63 Correct 848 ms 609908 KB Output is correct
64 Correct 784 ms 607156 KB Output is correct
65 Correct 713 ms 602424 KB Output is correct
66 Correct 607 ms 600648 KB Output is correct
67 Correct 582 ms 600448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 389 ms 587308 KB Output is correct
2 Correct 358 ms 587336 KB Output is correct
3 Correct 353 ms 587408 KB Output is correct
4 Correct 331 ms 587356 KB Output is correct
5 Correct 336 ms 587516 KB Output is correct
6 Correct 334 ms 587416 KB Output is correct
7 Correct 339 ms 587716 KB Output is correct
8 Correct 335 ms 587580 KB Output is correct
9 Correct 347 ms 587588 KB Output is correct
10 Correct 378 ms 587468 KB Output is correct
11 Correct 342 ms 587408 KB Output is correct
12 Correct 361 ms 587496 KB Output is correct
13 Correct 332 ms 587464 KB Output is correct
14 Correct 337 ms 587444 KB Output is correct
15 Correct 333 ms 587500 KB Output is correct
16 Correct 328 ms 587524 KB Output is correct
17 Correct 336 ms 587484 KB Output is correct
18 Correct 339 ms 587520 KB Output is correct
19 Correct 355 ms 587532 KB Output is correct
20 Correct 332 ms 587452 KB Output is correct
21 Correct 352 ms 587652 KB Output is correct
22 Correct 335 ms 587588 KB Output is correct
23 Correct 334 ms 587460 KB Output is correct
24 Correct 329 ms 587420 KB Output is correct
25 Correct 336 ms 587440 KB Output is correct
26 Correct 330 ms 587468 KB Output is correct
27 Correct 334 ms 587620 KB Output is correct
28 Correct 364 ms 587452 KB Output is correct
29 Correct 343 ms 587480 KB Output is correct
30 Correct 336 ms 587564 KB Output is correct
31 Correct 824 ms 605608 KB Output is correct
32 Correct 427 ms 598740 KB Output is correct
33 Correct 809 ms 602080 KB Output is correct
34 Correct 810 ms 602188 KB Output is correct
35 Correct 841 ms 605656 KB Output is correct
36 Correct 825 ms 605368 KB Output is correct
37 Correct 655 ms 600456 KB Output is correct
38 Correct 634 ms 600264 KB Output is correct
39 Correct 581 ms 600192 KB Output is correct
40 Correct 597 ms 600196 KB Output is correct
41 Correct 619 ms 600544 KB Output is correct
42 Correct 622 ms 600476 KB Output is correct
43 Correct 442 ms 599964 KB Output is correct
44 Correct 624 ms 600456 KB Output is correct
45 Correct 602 ms 600572 KB Output is correct
46 Correct 608 ms 600480 KB Output is correct
47 Correct 525 ms 599988 KB Output is correct
48 Correct 529 ms 599816 KB Output is correct
49 Correct 632 ms 600004 KB Output is correct
50 Correct 566 ms 600220 KB Output is correct
51 Correct 546 ms 600060 KB Output is correct
52 Correct 3256 ms 682392 KB Output is correct
53 Correct 3484 ms 672420 KB Output is correct
54 Correct 3898 ms 730836 KB Output is correct
55 Correct 3350 ms 695408 KB Output is correct
56 Correct 3495 ms 674908 KB Output is correct
57 Correct 3462 ms 675320 KB Output is correct
58 Correct 3126 ms 730836 KB Output is correct
59 Correct 2560 ms 695368 KB Output is correct
60 Correct 2227 ms 682952 KB Output is correct
61 Correct 2542 ms 676468 KB Output is correct
62 Correct 1689 ms 674536 KB Output is correct
63 Correct 1747 ms 676196 KB Output is correct
64 Correct 3440 ms 672680 KB Output is correct
65 Correct 692 ms 642156 KB Output is correct
66 Correct 3706 ms 675228 KB Output is correct
67 Correct 4291 ms 728740 KB Output is correct
68 Correct 3390 ms 686180 KB Output is correct
69 Correct 3679 ms 693248 KB Output is correct
70 Correct 3678 ms 674724 KB Output is correct
71 Correct 3770 ms 675076 KB Output is correct
72 Correct 3874 ms 729920 KB Output is correct
73 Correct 3169 ms 690196 KB Output is correct
74 Correct 2951 ms 679912 KB Output is correct
75 Correct 3322 ms 676228 KB Output is correct
76 Correct 1657 ms 673028 KB Output is correct
77 Correct 1634 ms 672168 KB Output is correct
78 Correct 1838 ms 674260 KB Output is correct
79 Correct 1984 ms 675596 KB Output is correct
80 Correct 2031 ms 673520 KB Output is correct
81 Correct 911 ms 616196 KB Output is correct
82 Correct 874 ms 612436 KB Output is correct
83 Correct 831 ms 608836 KB Output is correct
84 Correct 700 ms 605604 KB Output is correct
85 Correct 730 ms 608176 KB Output is correct
86 Correct 649 ms 601704 KB Output is correct
87 Correct 713 ms 605452 KB Output is correct
88 Correct 766 ms 608092 KB Output is correct
89 Correct 661 ms 601840 KB Output is correct
90 Correct 629 ms 615800 KB Output is correct
91 Correct 894 ms 616060 KB Output is correct
92 Correct 848 ms 609908 KB Output is correct
93 Correct 784 ms 607156 KB Output is correct
94 Correct 713 ms 602424 KB Output is correct
95 Correct 607 ms 600648 KB Output is correct
96 Correct 582 ms 600448 KB Output is correct
97 Correct 4401 ms 730672 KB Output is correct
98 Correct 690 ms 642636 KB Output is correct
99 Correct 3576 ms 659548 KB Output is correct
100 Correct 4160 ms 712956 KB Output is correct
101 Correct 3700 ms 695240 KB Output is correct
102 Correct 3711 ms 677336 KB Output is correct
103 Correct 2602 ms 653536 KB Output is correct
104 Correct 2602 ms 653040 KB Output is correct
105 Correct 1693 ms 653144 KB Output is correct
106 Correct 1792 ms 653128 KB Output is correct
107 Correct 2851 ms 678424 KB Output is correct
108 Correct 3186 ms 691780 KB Output is correct
109 Correct 2399 ms 659588 KB Output is correct
110 Correct 2995 ms 677860 KB Output is correct
111 Correct 3273 ms 691448 KB Output is correct
112 Correct 2457 ms 659068 KB Output is correct
113 Correct 1880 ms 729404 KB Output is correct
114 Correct 4282 ms 731200 KB Output is correct
115 Correct 3778 ms 700040 KB Output is correct
116 Correct 3574 ms 686800 KB Output is correct
117 Correct 3213 ms 662908 KB Output is correct
118 Correct 2474 ms 655148 KB Output is correct
119 Correct 2403 ms 652984 KB Output is correct
120 Correct 1235 ms 649796 KB Output is correct
121 Correct 1318 ms 651880 KB Output is correct
122 Correct 1285 ms 651784 KB Output is correct
123 Correct 1399 ms 652704 KB Output is correct
124 Correct 1494 ms 653444 KB Output is correct
125 Correct 1412 ms 653248 KB Output is correct
126 Correct 1588 ms 653620 KB Output is correct