Submission #654312

# Submission time Handle Problem Language Result Execution time Memory
654312 2022-10-30T23:24:07 Z resting Radio Towers (IOI22_towers) C++17
40 / 100
2617 ms 538472 KB
#include <bits/stdc++.h>

using namespace std;
#define ll long long

#define mx 100005

struct tree {
	struct node {
		node(int l, int r) : l(l), r(r) {}
		int l = 0; int r = 0; int v = 0;
		node* lc = nullptr;
		node* rc = nullptr;
	};
	vector<node*> rt;
	vector<int> nums;

	tree() {
		nums.push_back(numeric_limits<int>::min());
		rt.push_back(build(0, mx));
	}
	void upd(int idx, int val, int v) {
		//cout << "index: " << idx << "," << "value: " << val << "," << "time: " << v << endl;
		nums.push_back(v); // v = time
		rt.push_back(upd2(rt.back(), idx, val));
	}

	node* build(int l, int r) {
		node* rt = new node(l, r);
		if (l == r) return rt;
		int m = (l + r) / 2;
		rt->lc = build(l, m);
		rt->rc = build(m + 1, r);
		return rt;
	}

	node* upd2(node* rt, int idx, int v) {
		//return the root
		if (rt->l > idx || rt->r < idx) return rt;
		node* tr = new node(*rt); //copy?
		if (tr->l == tr->r) {
			tr->v = v;
			return tr;
		}
		tr->lc = upd2(tr->lc, idx, v);
		tr->rc = upd2(tr->rc, idx, v);
		tr->v = tr->lc->v + tr->rc->v;
		return tr;
	}
	
	array<int, 3> query(int l, int r, int t) {
		node* vv = rt.at((upper_bound(nums.begin(), nums.end(), t) - nums.begin())-1);
		return { queryL(vv, l), queryR(vv, r), queryS(vv, l, r)};
	}

	int queryS(node* rt, int l, int r) {
		if (rt->l > r || rt->r < l)return 0;
		if (rt->l >= l && rt->r <= r) return rt->v;
		return queryS(rt->lc, l, r) + queryS(rt->rc, l, r);
	}

	int queryL(node* rt, int l) { //query left most with index >= l and value >= v
		if (rt->r < l) return -1;
		if (!rt->v) return -1;
		if (rt->l == rt->r) return rt->l;
		int res = queryL(rt->lc, l);
		return res == -1 ? queryL(rt->rc, l) : res;
	}

	int queryR(node* rt, int r) { //query right most with index <= r
		if (rt->l > r) return -1;
		if (!rt->v) return -1;
		if (rt->l == rt->r) return rt->l;
		int res = queryR(rt->rc, r);
		return res == -1 ? queryR(rt->lc, r) : res;
	}

} open, close;

vector<int> h;

//struct mintree { //ehh should be sparse tablebut wtv
//	struct node {
//		node(int l, int r) : l(l), r(r) {};
//		int v = numeric_limits<int>::max();
//		int l, r;
//		node *lc = 0, *rc = 0;
//	};
//	node* rt;
//	mintree() {
//		rt = build(0, mx);
//	}
//	node* build(int l, int r) {
//		node* rt = new node(l, r);
//		if (l == r) return rt;
//		int m = (l + r) / 2;
//		rt->lc = build(l, m);
//		rt->rc = build(m + 1, r);
//		return rt;
//	}
//	void upd(int i, int v) {
//		upd2(rt, i, v);
//	}
//	void upd2(node* rt, int i, int v) {
//		if (i < rt->l || i > rt->r) return;
//		if (rt->l == rt->r) {
//			rt->v = v;
//			return;
//		}
//		upd2(rt->lc, i, v); upd2(rt->rc, i, v);
//		rt->v = min(rt->lc->v, rt->rc->v);
//	}
//	int query(int l, int r) {
//		return query2(rt, l, r);
//	}
//	int query2(node* rt, int l, int r) {
//		if (l > rt->r || r < rt->l) return numeric_limits<int>::max();
//		if (rt->l >= l && rt->r <= r) return rt->v;
//		return min(query2(rt->lc, l, r), query2(rt->rc, l, r));
//	}
//} ac2;

#define ll int
struct difftree { //what the fk am i doing
	struct node {
		node(int l, int r) : l(l), r(r) {};
		ll mn = std::numeric_limits<int>::max()/2;
		ll ma = std::numeric_limits<int>::min()/2;
		ll incdif = 0, decdif = 0;
		int l, r;
		node* lc = 0, * rc = 0;
	};
	node* rt = 0;
	difftree() {
		rt = build(0, mx);
		//cout << rt->l << "," << rt->r << endl;
	}
	node* build(int l, int r) {
		node* rt = new node(l, r);
		if (l == r) return rt;
		int m = (l + r) / 2;
		rt->lc = build(l, m);
		rt->rc = build(m + 1, r);
		return rt;
	}
	void upd(int i, int v) {
		upd2(rt, i, v);
	}
	void upd2(node* rt, int i, int v) {
		if (i < rt->l || i > rt->r) return;
		if (rt->l == rt->r) {
			rt->mn = rt->ma = v;
			return;
		}
		upd2(rt->lc, i, v); upd2(rt->rc, i, v);
		rt->mn = min(rt->lc->mn, rt->rc->mn);
		rt->ma = max(rt->lc->ma, rt->rc->ma);
		rt->incdif = max(max(rt->lc->incdif, rt->rc->incdif), rt->rc->ma - rt->lc->mn);
		rt->decdif = max(max(rt->lc->decdif, rt->rc->decdif), rt->lc->ma - rt->rc->mn);
	}
	int incdif(int l, int r) {
		node amogus = query2(rt, l, r);
		int tmp = amogus.incdif;
		return tmp;
	}
	int decdif(int l, int r) {
		node amogus = query2(rt, l, r);
		int tmp = amogus.decdif;
		return tmp;
	}
	node query2(node* rt, int l, int r) {
		//cout << "gonna kms" << endl;
		//cout << l << "," << rt->l << "," << r << "," << rt->r << endl;
		if (l > rt->r || r < rt->l) return node(-1,-1);
		if (rt->l >= l && rt->r <= r) return *rt;
		node owo(-1, -1);
		node lc = query2(rt->lc, l, r);
		node rc = query2(rt->rc, l, r);
		owo.mn = min(lc.mn, rc.mn);
		owo.ma = max(lc.ma, rc.ma);
		owo.incdif = max(max(lc.incdif, rc.incdif), rc.ma - lc.mn);
		owo.decdif = max(max(lc.decdif, rc.decdif), lc.ma - rc.mn);
		return owo;
	}
} diff;
//
//struct dsu {
//	vector<int> p, r;
//	dsu() {
//		p.resize(mx, -1);
//		r.resize(mx, 0);
//	}
//	int par(int x) {
//		return p.at(x) == -1 ? x : p.at(x) = par(p.at(x));
//	}
//	void merge(int a, int b) {
//		a = par(a), b = par(b); 
//		if (a == b) return;
//		//attaching b to a, so r.at(a) must be bigger
//		if (r.at(a) < r.at(b)) swap(a, b);
//		if (r.at(a) == r.at(b)) r.at(a)++;
//		p.at(b) = a;
//	}
//};


void init(int N, vector<int> H) {
	h = H;
	h.insert(h.begin(), numeric_limits<int>::max()); N++;
	set<int> ranges;
	for (int i = 0; i < N; i++)
		ranges.insert(i);
	vector<int> mn = h;
	vector<bool> exists(N, 1);
	set<pair<int, int>> q;
	for (int i = 0; i < N; i++) {
		//ac2.upd(i, h.at(i));
		diff.upd(i, h.at(i));
		open.upd(i, 1, numeric_limits<int>::min());
		close.upd(i+1, 1, numeric_limits<int>::min());
		int v = numeric_limits<int>::max();
		if (i) v = min(v, h.at(i) - mn.at(i));
		if (i < N - 1)v = min(v, h.at(i + 1) - mn.at(i));
		q.insert({ v, i });
	}
	int time = numeric_limits<int>::min();
	while (q.size()) {
		auto [a, b] = *q.begin();
		time = max(time, a);
		q.erase(q.begin());
		if (!exists.at(b)) continue;

		auto x = ranges.find(b);
		if (b && h.at(b) - mn.at(b) <= a) {
			//deletus
			auto x2 = x; x2--;
			if (exists.at(*x2)) {
				exists.at(*x) = 0;
				open.upd(*x, 0, time);
				auto x3 = x; x3++;
				if (x3 == ranges.end()) close.upd(N, 0, time);
				else close.upd(*x3, 0, time);
				continue;
			}
			int tmp = mn.at(b);
			exists.at(*x) = 0; 
			exists.at(*x2) = 1;
			open.upd(*x, 0, time);
			open.upd(*x2, 1, time);
			ranges.erase(x--);
			mn.at(*x) = min(mn.at(*x), tmp);
		}
		else {
			auto x2 = x; x2++;
			//assert(x2 != ranges.end()); //troll
			if (x2 == ranges.end()) {
				continue;
			}
			if (exists.at(*x2)) {
				open.upd(*x, 0, time);
				close.upd(*x2, 0, time);
				exists.at(*x) = 0;
				continue;
			}
			if (x2 != ranges.end() && h.at(*x2) - mn.at(*x) <= a) {
				mn.at(*x) = min(mn.at(*x), mn.at(*x2));
				exists.at(*x2) = 0; 
				close.upd(*x2, 0, time);

				auto x3 = x2; x3++;
				if(x3 != ranges.end())close.upd(*x3, 1, time);
				else close.upd(N, 1, time);
				ranges.erase(x2);
			}
			else {
				break;
			}
		}
		//x is result

		b = *x; int v = numeric_limits<int>::max();
		if (b) v = min(v, h.at(b) - mn.at(b));
		x++;
		if (x != ranges.end()) v = min(v, h.at(*x) - mn.at(b));
		q.insert({ v, b });

		//yeahhhhhhh......
	}
	h = H;
}

int max_towers(int L, int R, int D) {
	L++, R++; D--;
	//this is so sus
	auto [a, b, c] = open.query(L, R, D);
	auto [e, f, g] = close.query(L, R, D);


	//auto .at(x, y, z) = open.query(0, 7, numeric_limits<int>::min());
	//cout << x << "," << y << "," << z << endl;
	//cout << c << "," << g << endl;

	if (c == 0 || g == 0) {
		//AMONGUS
		int l = L, r = R, m = 0;
		int inc = 0, dec = 0;
		while (l < r) {
			m = (l + r) / 2;
			 inc = diff.incdif(L, m);
			 dec = diff.decdif(m, R);
			//cout <<L<<","<<R<<","<<m << "," << inc << "," << dec << endl;
			if (min(inc, dec) >= D+1) break;
			if (max(inc, dec) < D + 1)break;
 			if (inc < dec)
				l = m + 1;
			else 
				r = m - 1;
		}
		//cout << "." << endl;
		if (min(inc, dec) >= D+1) {
			return 2;
		}
		else {
			return 1;
		}
	}
	//cout << a << "," << b << "," << c << endl;
	//cout << e << "," << f << "," << g << endl;
	//assert(a != -1);
	//assert(b != -1);
	//assert(e != -1);
	//assert(f != -1);

	
	if (e <= a) {
		g--;
	}
	if (b >= f) {
		c--;
	}
	//assert(c == g);
	//cout << diff.incdif(L, a) << "," << L << "," << a << endl;
	if (diff.incdif(L, a) >= D+1) c++;
	if (diff.decdif(f, R) >= D+1) c++;
	return c;
}

//
//int main() {
//	int N, Q;
//	assert(2 == scanf("%d %d", &N, &Q));
//	std::vector<int> H(N);
//	for (int i = 0; i < N; ++i) {
//		assert(1 == scanf("%d", &H.at(i)));
//	}
//	init(N, H);
//
//	for (int i = 0; i < Q; ++i) {
//		int L, R, D;
//		assert(3 == scanf("%d %d %d", &L, &R, &D));
//		printf("%d\n", max_towers(L, R, D));
//	}
//	return 0;
//}

Compilation message

towers.cpp:123: warning: "ll" redefined
  123 | #define ll int
      | 
towers.cpp:4: note: this is the location of the previous definition
    4 | #define ll long long
      |
# Verdict Execution time Memory Grader output
1 Incorrect 1257 ms 332252 KB 18th lines differ - on the 1st token, expected: '1', found: '0'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 30 ms 30568 KB Output is correct
2 Correct 49 ms 38596 KB Output is correct
3 Correct 44 ms 38536 KB Output is correct
4 Correct 38 ms 38724 KB Output is correct
5 Correct 39 ms 38564 KB Output is correct
6 Correct 44 ms 38552 KB Output is correct
7 Correct 39 ms 38624 KB Output is correct
8 Correct 44 ms 38600 KB Output is correct
9 Correct 40 ms 38484 KB Output is correct
10 Correct 38 ms 38552 KB Output is correct
11 Correct 39 ms 38600 KB Output is correct
12 Correct 34 ms 28360 KB Output is correct
13 Correct 52 ms 38628 KB Output is correct
14 Correct 43 ms 38612 KB Output is correct
15 Correct 38 ms 38508 KB Output is correct
16 Correct 39 ms 38600 KB Output is correct
17 Correct 40 ms 38552 KB Output is correct
18 Correct 51 ms 38600 KB Output is correct
19 Correct 38 ms 38580 KB Output is correct
20 Correct 37 ms 38600 KB Output is correct
21 Correct 39 ms 38600 KB Output is correct
22 Correct 38 ms 38600 KB Output is correct
23 Correct 39 ms 38512 KB Output is correct
24 Correct 36 ms 38600 KB Output is correct
25 Correct 33 ms 32964 KB Output is correct
26 Correct 46 ms 38512 KB Output is correct
27 Correct 39 ms 38680 KB Output is correct
28 Correct 39 ms 38524 KB Output is correct
29 Correct 40 ms 38584 KB Output is correct
30 Correct 40 ms 38576 KB Output is correct
31 Correct 40 ms 38524 KB Output is correct
32 Correct 38 ms 38608 KB Output is correct
33 Correct 39 ms 38532 KB Output is correct
34 Correct 38 ms 38572 KB Output is correct
35 Correct 43 ms 38592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 30568 KB Output is correct
2 Correct 49 ms 38596 KB Output is correct
3 Correct 44 ms 38536 KB Output is correct
4 Correct 38 ms 38724 KB Output is correct
5 Correct 39 ms 38564 KB Output is correct
6 Correct 44 ms 38552 KB Output is correct
7 Correct 39 ms 38624 KB Output is correct
8 Correct 44 ms 38600 KB Output is correct
9 Correct 40 ms 38484 KB Output is correct
10 Correct 38 ms 38552 KB Output is correct
11 Correct 39 ms 38600 KB Output is correct
12 Correct 34 ms 28360 KB Output is correct
13 Correct 52 ms 38628 KB Output is correct
14 Correct 43 ms 38612 KB Output is correct
15 Correct 38 ms 38508 KB Output is correct
16 Correct 39 ms 38600 KB Output is correct
17 Correct 40 ms 38552 KB Output is correct
18 Correct 51 ms 38600 KB Output is correct
19 Correct 38 ms 38580 KB Output is correct
20 Correct 37 ms 38600 KB Output is correct
21 Correct 39 ms 38600 KB Output is correct
22 Correct 38 ms 38600 KB Output is correct
23 Correct 39 ms 38512 KB Output is correct
24 Correct 36 ms 38600 KB Output is correct
25 Correct 33 ms 32964 KB Output is correct
26 Correct 46 ms 38512 KB Output is correct
27 Correct 39 ms 38680 KB Output is correct
28 Correct 39 ms 38524 KB Output is correct
29 Correct 40 ms 38584 KB Output is correct
30 Correct 40 ms 38576 KB Output is correct
31 Correct 40 ms 38524 KB Output is correct
32 Correct 38 ms 38608 KB Output is correct
33 Correct 39 ms 38532 KB Output is correct
34 Correct 38 ms 38572 KB Output is correct
35 Correct 43 ms 38592 KB Output is correct
36 Correct 691 ms 358420 KB Output is correct
37 Correct 1104 ms 535656 KB Output is correct
38 Correct 1099 ms 535624 KB Output is correct
39 Correct 1032 ms 535536 KB Output is correct
40 Correct 1150 ms 535824 KB Output is correct
41 Correct 1110 ms 535516 KB Output is correct
42 Correct 1159 ms 535684 KB Output is correct
43 Correct 680 ms 538464 KB Output is correct
44 Correct 1082 ms 535560 KB Output is correct
45 Correct 654 ms 532024 KB Output is correct
46 Correct 1004 ms 535584 KB Output is correct
47 Correct 1020 ms 535572 KB Output is correct
48 Correct 1010 ms 535700 KB Output is correct
49 Correct 1048 ms 535484 KB Output is correct
50 Correct 990 ms 535668 KB Output is correct
51 Correct 652 ms 538472 KB Output is correct
52 Correct 1006 ms 535508 KB Output is correct
53 Correct 1002 ms 535524 KB Output is correct
54 Correct 1008 ms 535700 KB Output is correct
55 Correct 953 ms 535584 KB Output is correct
56 Correct 636 ms 534692 KB Output is correct
57 Correct 940 ms 518288 KB Output is correct
58 Correct 1010 ms 535696 KB Output is correct
59 Correct 983 ms 535508 KB Output is correct
60 Correct 1043 ms 535652 KB Output is correct
61 Correct 1025 ms 535584 KB Output is correct
62 Correct 994 ms 535608 KB Output is correct
63 Correct 1022 ms 535648 KB Output is correct
64 Correct 631 ms 538408 KB Output is correct
65 Correct 966 ms 535564 KB Output is correct
66 Correct 630 ms 536264 KB Output is correct
67 Correct 951 ms 535736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2053 ms 532032 KB Output is correct
2 Correct 2611 ms 535592 KB Output is correct
3 Correct 2581 ms 535552 KB Output is correct
4 Incorrect 2617 ms 535528 KB 12441st lines differ - on the 1st token, expected: '1', found: '0'
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 625 ms 150016 KB Output is correct
2 Correct 2412 ms 535580 KB Output is correct
3 Correct 2223 ms 535644 KB Output is correct
4 Correct 2399 ms 535804 KB Output is correct
5 Correct 2394 ms 535684 KB Output is correct
6 Correct 2596 ms 535624 KB Output is correct
7 Correct 2409 ms 535496 KB Output is correct
8 Correct 1580 ms 538408 KB Output is correct
9 Correct 2064 ms 535608 KB Output is correct
10 Correct 1664 ms 538448 KB Output is correct
11 Correct 1932 ms 535692 KB Output is correct
12 Correct 958 ms 535480 KB Output is correct
13 Correct 1014 ms 535660 KB Output is correct
14 Correct 989 ms 535528 KB Output is correct
15 Correct 981 ms 535668 KB Output is correct
16 Correct 654 ms 534576 KB Output is correct
17 Correct 984 ms 518252 KB Output is correct
18 Correct 1004 ms 535492 KB Output is correct
19 Correct 986 ms 535656 KB Output is correct
20 Correct 1048 ms 535584 KB Output is correct
21 Correct 1022 ms 535580 KB Output is correct
22 Correct 1022 ms 535672 KB Output is correct
23 Correct 1006 ms 535504 KB Output is correct
24 Correct 626 ms 538468 KB Output is correct
25 Correct 943 ms 535580 KB Output is correct
26 Correct 663 ms 536280 KB Output is correct
27 Correct 949 ms 535688 KB Output is correct
28 Correct 37 ms 38536 KB Output is correct
29 Correct 38 ms 38600 KB Output is correct
30 Correct 38 ms 38516 KB Output is correct
31 Correct 39 ms 38600 KB Output is correct
32 Correct 36 ms 38484 KB Output is correct
33 Correct 30 ms 32968 KB Output is correct
34 Correct 38 ms 38600 KB Output is correct
35 Correct 39 ms 38588 KB Output is correct
36 Correct 41 ms 38540 KB Output is correct
37 Correct 39 ms 38600 KB Output is correct
38 Correct 41 ms 38620 KB Output is correct
39 Correct 40 ms 38528 KB Output is correct
40 Correct 36 ms 38560 KB Output is correct
41 Correct 39 ms 38612 KB Output is correct
42 Correct 37 ms 38584 KB Output is correct
43 Correct 40 ms 38484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 30568 KB Output is correct
2 Correct 49 ms 38596 KB Output is correct
3 Correct 44 ms 38536 KB Output is correct
4 Correct 38 ms 38724 KB Output is correct
5 Correct 39 ms 38564 KB Output is correct
6 Correct 44 ms 38552 KB Output is correct
7 Correct 39 ms 38624 KB Output is correct
8 Correct 44 ms 38600 KB Output is correct
9 Correct 40 ms 38484 KB Output is correct
10 Correct 38 ms 38552 KB Output is correct
11 Correct 39 ms 38600 KB Output is correct
12 Correct 34 ms 28360 KB Output is correct
13 Correct 52 ms 38628 KB Output is correct
14 Correct 43 ms 38612 KB Output is correct
15 Correct 38 ms 38508 KB Output is correct
16 Correct 39 ms 38600 KB Output is correct
17 Correct 40 ms 38552 KB Output is correct
18 Correct 51 ms 38600 KB Output is correct
19 Correct 38 ms 38580 KB Output is correct
20 Correct 37 ms 38600 KB Output is correct
21 Correct 39 ms 38600 KB Output is correct
22 Correct 38 ms 38600 KB Output is correct
23 Correct 39 ms 38512 KB Output is correct
24 Correct 36 ms 38600 KB Output is correct
25 Correct 33 ms 32964 KB Output is correct
26 Correct 46 ms 38512 KB Output is correct
27 Correct 39 ms 38680 KB Output is correct
28 Correct 39 ms 38524 KB Output is correct
29 Correct 40 ms 38584 KB Output is correct
30 Correct 40 ms 38576 KB Output is correct
31 Correct 40 ms 38524 KB Output is correct
32 Correct 38 ms 38608 KB Output is correct
33 Correct 39 ms 38532 KB Output is correct
34 Correct 38 ms 38572 KB Output is correct
35 Correct 43 ms 38592 KB Output is correct
36 Correct 691 ms 358420 KB Output is correct
37 Correct 1104 ms 535656 KB Output is correct
38 Correct 1099 ms 535624 KB Output is correct
39 Correct 1032 ms 535536 KB Output is correct
40 Correct 1150 ms 535824 KB Output is correct
41 Correct 1110 ms 535516 KB Output is correct
42 Correct 1159 ms 535684 KB Output is correct
43 Correct 680 ms 538464 KB Output is correct
44 Correct 1082 ms 535560 KB Output is correct
45 Correct 654 ms 532024 KB Output is correct
46 Correct 1004 ms 535584 KB Output is correct
47 Correct 1020 ms 535572 KB Output is correct
48 Correct 1010 ms 535700 KB Output is correct
49 Correct 1048 ms 535484 KB Output is correct
50 Correct 990 ms 535668 KB Output is correct
51 Correct 652 ms 538472 KB Output is correct
52 Correct 1006 ms 535508 KB Output is correct
53 Correct 1002 ms 535524 KB Output is correct
54 Correct 1008 ms 535700 KB Output is correct
55 Correct 953 ms 535584 KB Output is correct
56 Correct 636 ms 534692 KB Output is correct
57 Correct 940 ms 518288 KB Output is correct
58 Correct 1010 ms 535696 KB Output is correct
59 Correct 983 ms 535508 KB Output is correct
60 Correct 1043 ms 535652 KB Output is correct
61 Correct 1025 ms 535584 KB Output is correct
62 Correct 994 ms 535608 KB Output is correct
63 Correct 1022 ms 535648 KB Output is correct
64 Correct 631 ms 538408 KB Output is correct
65 Correct 966 ms 535564 KB Output is correct
66 Correct 630 ms 536264 KB Output is correct
67 Correct 951 ms 535736 KB Output is correct
68 Correct 2053 ms 532032 KB Output is correct
69 Correct 2611 ms 535592 KB Output is correct
70 Correct 2581 ms 535552 KB Output is correct
71 Incorrect 2617 ms 535528 KB 12441st lines differ - on the 1st token, expected: '1', found: '0'
72 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1257 ms 332252 KB 18th lines differ - on the 1st token, expected: '1', found: '0'
2 Halted 0 ms 0 KB -