Submission #104680

# Submission time Handle Problem Language Result Execution time Memory
104680 2019-04-08T19:11:32 Z tincamatei Two Antennas (JOI19_antennas) C++14
100 / 100
1292 ms 44472 KB
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 200000;
const int MAX_Q = 200000;
const int INF = 1000000001;

int ainta[1+4*MAX_N], aintb[1+4*MAX_N], lazy[1+4*MAX_N];

void updateB(int poz, int val, int l, int r, int nod = 1) {
	if(poz < l || r < poz || r < l) return;

	if(l == r)
		aintb[nod] = val;
	else {
		int mid = (l + r) / 2;
		updateB(poz, val, l, mid, 2 * nod);
		updateB(poz, val, mid + 1, r, 2 * nod + 1);
		aintb[nod] = min(aintb[2 * nod], aintb[2 * nod + 1]);
	}
}

void propagate(int nod, int l, int r) {
	ainta[nod] = max(ainta[nod], lazy[nod] - aintb[nod]);
	if(l < r) {
		lazy[2 * nod] = max(lazy[nod], lazy[2 * nod]);
		lazy[2 * nod + 1] = max(lazy[nod], lazy[2 *  nod + 1]);
	}
	lazy[nod] = 0;
}

void updateA(int i, int j, int val, int l, int r, int nod = 1) {
	propagate(nod, l, r);
	if(j < l || r < i || j < i) return;

	if(i <= l && r <= j) {
		lazy[nod] = max(lazy[nod], val);
		propagate(nod, l, r);
	} else {
		int mid = (l + r) / 2;
		updateA(i, j, val, l, mid, 2 * nod);
		updateA(i, j, val, mid + 1, r, 2 * nod + 1);
		ainta[nod] = max(ainta[2 * nod], ainta[2 * nod + 1]);
	}
}

int queryaint(int i, int j, int l, int r, int nod = 1) {
	propagate(nod, l, r);
	if(r < i || j < l || j < i) return -1;

	if(i <= l && r <= j)
		return ainta[nod];
	else {
		int mid = (l + r) / 2;
		return max(queryaint(i, j, l, mid, 2 * nod),
		           queryaint(i, j, mid + 1, r, 2 * nod + 1));
	}
}

void init(int nod, int l, int r) {
	ainta[nod] = -1;
	aintb[nod] = INF;
	lazy[nod] = 0;

	if(l < r) {
		int mid = (l + r) / 2;
		init(2 * nod, l, mid);
		init(2 * nod + 1, mid + 1, r);
	}
}

struct Tower {
	int h, l, r;
} v[MAX_N];

struct Query {
	int l, r;
	int *rez;
} query[MAX_Q];

int rez[1+MAX_Q];

struct Update {
	int poz, val;
};

vector<Update> updates[MAX_N+1];
vector<Query> queries[MAX_N+1];

void solve(int n, int q) {
	init(1, 0, n - 1);
	
	for(int i = n - 1; i >= 0; --i) {
		updates[i].clear();
		queries[i].clear();

		int st = min(i + v[i].l, n);
		int dr = min(i + v[i].r + 1, n);

		if(st < dr) {
			updates[st].push_back({i, v[i].h});
			updates[dr].push_back({i, INF});
		}
	}

	for(int i = 0; i < q; ++i)
		queries[query[i].r].push_back(query[i]);

	for(int i = 0; i < n; ++i) {
		for(auto it: updates[i]) {
			queryaint(it.poz, it.poz, 0, n - 1);
			updateB(it.poz, it.val, 0, n - 1);
		}

		int st = max(i - v[i].r - 1, -1);
		int dr = max(i - v[i].l, -1);

		if(st < dr)
			updateA(st + 1, dr, v[i].h, 0, n - 1);
	
		for(auto it: queries[i])
			(*it.rez) = max(*it.rez, queryaint(it.l, it.r, 0, n - 1));
	}
}

int main() {
	int n, q;

	scanf("%d", &n);
	for(int i = 0; i < n; ++i)
		scanf("%d%d%d", &v[i].h, &v[i].l, &v[i].r);
	scanf("%d", &q);
	for(int i = 0; i < q; ++i) {
		scanf("%d%d", &query[i].l, &query[i].r);
		query[i].rez = &rez[i];
		rez[i] = -1;
		query[i].l--;
		query[i].r--;
	}

	solve(n, q);

	for(int i = 0; i < q; ++i) {
		query[i].l = n - query[i].l - 1;
		query[i].r = n - query[i].r - 1;
		swap(query[i].l, query[i].r);
	}
	reverse(v, v + n);
	
	solve(n, q);
  
	for(int i = 0; i < q; ++i)
		printf("%d\n", rez[i]);
  
	return 0;
}

Compilation message

antennas.cpp: In function 'int main()':
antennas.cpp:130:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
antennas.cpp:132:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%d", &v[i].h, &v[i].l, &v[i].r);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
antennas.cpp:133:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &q);
  ~~~~~^~~~~~~~~~
antennas.cpp:135:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &query[i].l, &query[i].r);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 12 ms 9728 KB Output is correct
2 Correct 11 ms 9856 KB Output is correct
3 Correct 13 ms 9728 KB Output is correct
4 Correct 11 ms 9728 KB Output is correct
5 Correct 10 ms 9856 KB Output is correct
6 Correct 13 ms 9856 KB Output is correct
7 Correct 11 ms 9728 KB Output is correct
8 Correct 11 ms 9856 KB Output is correct
9 Correct 11 ms 9728 KB Output is correct
10 Correct 14 ms 9836 KB Output is correct
11 Correct 11 ms 9856 KB Output is correct
12 Correct 12 ms 9728 KB Output is correct
13 Correct 12 ms 9856 KB Output is correct
14 Correct 13 ms 9856 KB Output is correct
15 Correct 12 ms 9728 KB Output is correct
16 Correct 12 ms 9884 KB Output is correct
17 Correct 12 ms 9856 KB Output is correct
18 Correct 11 ms 9856 KB Output is correct
19 Correct 12 ms 9828 KB Output is correct
20 Correct 13 ms 9856 KB Output is correct
21 Correct 14 ms 9856 KB Output is correct
22 Correct 14 ms 9728 KB Output is correct
23 Correct 12 ms 9856 KB Output is correct
24 Correct 11 ms 9728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 9728 KB Output is correct
2 Correct 11 ms 9856 KB Output is correct
3 Correct 13 ms 9728 KB Output is correct
4 Correct 11 ms 9728 KB Output is correct
5 Correct 10 ms 9856 KB Output is correct
6 Correct 13 ms 9856 KB Output is correct
7 Correct 11 ms 9728 KB Output is correct
8 Correct 11 ms 9856 KB Output is correct
9 Correct 11 ms 9728 KB Output is correct
10 Correct 14 ms 9836 KB Output is correct
11 Correct 11 ms 9856 KB Output is correct
12 Correct 12 ms 9728 KB Output is correct
13 Correct 12 ms 9856 KB Output is correct
14 Correct 13 ms 9856 KB Output is correct
15 Correct 12 ms 9728 KB Output is correct
16 Correct 12 ms 9884 KB Output is correct
17 Correct 12 ms 9856 KB Output is correct
18 Correct 11 ms 9856 KB Output is correct
19 Correct 12 ms 9828 KB Output is correct
20 Correct 13 ms 9856 KB Output is correct
21 Correct 14 ms 9856 KB Output is correct
22 Correct 14 ms 9728 KB Output is correct
23 Correct 12 ms 9856 KB Output is correct
24 Correct 11 ms 9728 KB Output is correct
25 Correct 180 ms 19904 KB Output is correct
26 Correct 33 ms 11128 KB Output is correct
27 Correct 281 ms 23788 KB Output is correct
28 Correct 234 ms 23548 KB Output is correct
29 Correct 164 ms 20088 KB Output is correct
30 Correct 157 ms 19448 KB Output is correct
31 Correct 197 ms 22296 KB Output is correct
32 Correct 228 ms 23544 KB Output is correct
33 Correct 213 ms 23416 KB Output is correct
34 Correct 126 ms 16504 KB Output is correct
35 Correct 274 ms 24240 KB Output is correct
36 Correct 257 ms 23588 KB Output is correct
37 Correct 163 ms 17400 KB Output is correct
38 Correct 264 ms 22520 KB Output is correct
39 Correct 44 ms 11768 KB Output is correct
40 Correct 268 ms 22748 KB Output is correct
41 Correct 176 ms 19704 KB Output is correct
42 Correct 248 ms 22364 KB Output is correct
43 Correct 95 ms 14328 KB Output is correct
44 Correct 257 ms 22520 KB Output is correct
45 Correct 50 ms 12152 KB Output is correct
46 Correct 263 ms 22540 KB Output is correct
47 Correct 71 ms 13180 KB Output is correct
48 Correct 275 ms 22396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 582 ms 22968 KB Output is correct
2 Correct 547 ms 23656 KB Output is correct
3 Correct 383 ms 21460 KB Output is correct
4 Correct 536 ms 23532 KB Output is correct
5 Correct 216 ms 18416 KB Output is correct
6 Correct 573 ms 28140 KB Output is correct
7 Correct 456 ms 26548 KB Output is correct
8 Correct 548 ms 28364 KB Output is correct
9 Correct 66 ms 12408 KB Output is correct
10 Correct 589 ms 28180 KB Output is correct
11 Correct 314 ms 20464 KB Output is correct
12 Correct 599 ms 28204 KB Output is correct
13 Correct 423 ms 28340 KB Output is correct
14 Correct 366 ms 27976 KB Output is correct
15 Correct 376 ms 26948 KB Output is correct
16 Correct 350 ms 27496 KB Output is correct
17 Correct 434 ms 28756 KB Output is correct
18 Correct 389 ms 27372 KB Output is correct
19 Correct 444 ms 27312 KB Output is correct
20 Correct 478 ms 27768 KB Output is correct
21 Correct 488 ms 27968 KB Output is correct
22 Correct 398 ms 28012 KB Output is correct
23 Correct 412 ms 27820 KB Output is correct
24 Correct 323 ms 26424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 9728 KB Output is correct
2 Correct 11 ms 9856 KB Output is correct
3 Correct 13 ms 9728 KB Output is correct
4 Correct 11 ms 9728 KB Output is correct
5 Correct 10 ms 9856 KB Output is correct
6 Correct 13 ms 9856 KB Output is correct
7 Correct 11 ms 9728 KB Output is correct
8 Correct 11 ms 9856 KB Output is correct
9 Correct 11 ms 9728 KB Output is correct
10 Correct 14 ms 9836 KB Output is correct
11 Correct 11 ms 9856 KB Output is correct
12 Correct 12 ms 9728 KB Output is correct
13 Correct 12 ms 9856 KB Output is correct
14 Correct 13 ms 9856 KB Output is correct
15 Correct 12 ms 9728 KB Output is correct
16 Correct 12 ms 9884 KB Output is correct
17 Correct 12 ms 9856 KB Output is correct
18 Correct 11 ms 9856 KB Output is correct
19 Correct 12 ms 9828 KB Output is correct
20 Correct 13 ms 9856 KB Output is correct
21 Correct 14 ms 9856 KB Output is correct
22 Correct 14 ms 9728 KB Output is correct
23 Correct 12 ms 9856 KB Output is correct
24 Correct 11 ms 9728 KB Output is correct
25 Correct 180 ms 19904 KB Output is correct
26 Correct 33 ms 11128 KB Output is correct
27 Correct 281 ms 23788 KB Output is correct
28 Correct 234 ms 23548 KB Output is correct
29 Correct 164 ms 20088 KB Output is correct
30 Correct 157 ms 19448 KB Output is correct
31 Correct 197 ms 22296 KB Output is correct
32 Correct 228 ms 23544 KB Output is correct
33 Correct 213 ms 23416 KB Output is correct
34 Correct 126 ms 16504 KB Output is correct
35 Correct 274 ms 24240 KB Output is correct
36 Correct 257 ms 23588 KB Output is correct
37 Correct 163 ms 17400 KB Output is correct
38 Correct 264 ms 22520 KB Output is correct
39 Correct 44 ms 11768 KB Output is correct
40 Correct 268 ms 22748 KB Output is correct
41 Correct 176 ms 19704 KB Output is correct
42 Correct 248 ms 22364 KB Output is correct
43 Correct 95 ms 14328 KB Output is correct
44 Correct 257 ms 22520 KB Output is correct
45 Correct 50 ms 12152 KB Output is correct
46 Correct 263 ms 22540 KB Output is correct
47 Correct 71 ms 13180 KB Output is correct
48 Correct 275 ms 22396 KB Output is correct
49 Correct 582 ms 22968 KB Output is correct
50 Correct 547 ms 23656 KB Output is correct
51 Correct 383 ms 21460 KB Output is correct
52 Correct 536 ms 23532 KB Output is correct
53 Correct 216 ms 18416 KB Output is correct
54 Correct 573 ms 28140 KB Output is correct
55 Correct 456 ms 26548 KB Output is correct
56 Correct 548 ms 28364 KB Output is correct
57 Correct 66 ms 12408 KB Output is correct
58 Correct 589 ms 28180 KB Output is correct
59 Correct 314 ms 20464 KB Output is correct
60 Correct 599 ms 28204 KB Output is correct
61 Correct 423 ms 28340 KB Output is correct
62 Correct 366 ms 27976 KB Output is correct
63 Correct 376 ms 26948 KB Output is correct
64 Correct 350 ms 27496 KB Output is correct
65 Correct 434 ms 28756 KB Output is correct
66 Correct 389 ms 27372 KB Output is correct
67 Correct 444 ms 27312 KB Output is correct
68 Correct 478 ms 27768 KB Output is correct
69 Correct 488 ms 27968 KB Output is correct
70 Correct 398 ms 28012 KB Output is correct
71 Correct 412 ms 27820 KB Output is correct
72 Correct 323 ms 26424 KB Output is correct
73 Correct 1079 ms 39672 KB Output is correct
74 Correct 673 ms 29672 KB Output is correct
75 Correct 1063 ms 40856 KB Output is correct
76 Correct 1256 ms 44364 KB Output is correct
77 Correct 650 ms 30320 KB Output is correct
78 Correct 1050 ms 39696 KB Output is correct
79 Correct 1038 ms 43116 KB Output is correct
80 Correct 1292 ms 44392 KB Output is correct
81 Correct 484 ms 27124 KB Output is correct
82 Correct 923 ms 36816 KB Output is correct
83 Correct 887 ms 36564 KB Output is correct
84 Correct 1224 ms 44472 KB Output is correct
85 Correct 803 ms 37608 KB Output is correct
86 Correct 958 ms 43120 KB Output is correct
87 Correct 568 ms 29292 KB Output is correct
88 Correct 915 ms 42420 KB Output is correct
89 Correct 888 ms 40368 KB Output is correct
90 Correct 1120 ms 42584 KB Output is correct
91 Correct 587 ms 33072 KB Output is correct
92 Correct 984 ms 43164 KB Output is correct
93 Correct 466 ms 31148 KB Output is correct
94 Correct 1025 ms 43340 KB Output is correct
95 Correct 530 ms 32324 KB Output is correct
96 Correct 845 ms 41520 KB Output is correct