Submission #476651

# Submission time Handle Problem Language Result Execution time Memory
476651 2021-09-28T00:10:41 Z thiago_bastos Examination (JOI19_examination) C++17
100 / 100
1983 ms 11716 KB
#include "bits/stdc++.h"
 
using namespace std;

using i64 = long long;
using u64 = unsigned long long;
using i32 = int;
using u32 = unsigned;
using i16 = short;
using u16 = unsigned short;
using ld = long double;
using ii = pair<int, int>;

template<class T>
struct BIT {
	vector<T> bit;
	
	BIT(){}
	
	BIT(int n) {
		bit.assign(n + 1, 0);
	}
	
	void upd(int k, T x) {
		for(++k; k < int(bit.size()); k += k & -k) bit[k] += x;
	}
	
	T query(int k) {
		T ans {};
		for(++k; k > 0; k -= k & -k) ans += bit[k];
		return ans;
	}
	
	T query(int l, int r) {
		if(l > r) return (T)0;
		return query(r) - query(l - 1);
	}
};

using T = tuple<int, int, int>;

const int chunk_sz = 300, chunks = 340;

BIT<int> ordered_set[chunks];
vector<int> S[chunks];
vector<T> a;
int n, q;

bool cmp(T a, T b) {
	return get<0>(a) + get<1>(a) < get<0>(b) + get<1>(b);
}

void upd(int x, int y, int z) {
	int i = lower_bound(a.begin(), a.end(), make_tuple(x, y, z)) - a.begin();
	int j = lower_bound(S[i / chunk_sz].begin(), S[i / chunk_sz].end(), y) - S[i / chunk_sz].begin();
	ordered_set[i / chunk_sz].upd(j, 1);
}

int query(int x, int y, int z) {
	int k = lower_bound(a.begin(), a.end(), make_tuple(x, -1, -1)) - a.begin();
	int j = k / chunk_sz, ans = 0;
	
	for(int i = k; i < min(n, (j + 1) * chunk_sz); ++i) {
		auto [s, t, _] = a[i];
		if(s + t >= z && t >= y) ++ans;
	}
	
	int c = (n + chunk_sz - 1) / chunk_sz;
	
	for(++j; j < c; ++j) {
		int i = lower_bound(S[j].begin(), S[j].end(), y) - S[j].begin();
		ans += ordered_set[j].query(i, (int)S[j].size() - 1);
	}
	
	return ans;
}

void solve() {

	cin >> n >> q;
	
	vector<T> b(n);
	vector<int> ans(q);
	vector<tuple<int, int, int, int>> Q(q);
	
	a.resize(n);
	
	for(int i = 0; i < n; ++i) {
		int s, t;
		cin >> s >> t;
		b[i] = a[i] = {s, t, i};
	}
	
	sort(a.begin(), a.end());
	
	for(int i = 0; i < n; ++i) S[i / chunk_sz].push_back(get<1>(a[i]));
	
	for(int i = 0; i < chunks; ++i) {
		ordered_set[i] = BIT<int>(size(S[i]));
		sort(S[i].begin(), S[i].end());
	}
	
	for(int i = 0; i < q; ++i) {
		int x, y, z;
		cin >> x >> y >> z;
		Q[i] = {z, x, y, i};
	}
	
	b.emplace_back(-1, -1, -1);
	sort(b.rbegin(), b.rend(), cmp);
	sort(Q.rbegin(), Q.rend());
	
	int i = 0, j = 0;
	
	while(j < q) {
		auto [s, t, k] = b[i];
		auto [z, x, y, pos] = Q[j];
		if(s + t >= z) {
			upd(s, t, k);
			++i;
		} else {
			ans[pos] = query(x, y, z);
			++j;
		}
	}
	
	for(int x : ans) cout << x << '\n';
}

int main() {
	ios_base :: sync_with_stdio(false);
	cin.tie(0);
	int t = 1;
	//cin >> t;
	while(t--) solve();
 	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 328 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 6 ms 588 KB Output is correct
8 Correct 6 ms 588 KB Output is correct
9 Correct 6 ms 588 KB Output is correct
10 Correct 5 ms 588 KB Output is correct
11 Correct 6 ms 588 KB Output is correct
12 Correct 4 ms 460 KB Output is correct
13 Correct 5 ms 660 KB Output is correct
14 Correct 5 ms 588 KB Output is correct
15 Correct 5 ms 588 KB Output is correct
16 Correct 5 ms 616 KB Output is correct
17 Correct 4 ms 588 KB Output is correct
18 Correct 3 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1063 ms 9368 KB Output is correct
2 Correct 1070 ms 9280 KB Output is correct
3 Correct 1025 ms 9284 KB Output is correct
4 Correct 848 ms 8648 KB Output is correct
5 Correct 427 ms 8652 KB Output is correct
6 Correct 402 ms 7828 KB Output is correct
7 Correct 1841 ms 9240 KB Output is correct
8 Correct 784 ms 9288 KB Output is correct
9 Correct 1334 ms 9156 KB Output is correct
10 Correct 358 ms 8596 KB Output is correct
11 Correct 341 ms 8600 KB Output is correct
12 Correct 188 ms 7832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1063 ms 9368 KB Output is correct
2 Correct 1070 ms 9280 KB Output is correct
3 Correct 1025 ms 9284 KB Output is correct
4 Correct 848 ms 8648 KB Output is correct
5 Correct 427 ms 8652 KB Output is correct
6 Correct 402 ms 7828 KB Output is correct
7 Correct 1841 ms 9240 KB Output is correct
8 Correct 784 ms 9288 KB Output is correct
9 Correct 1334 ms 9156 KB Output is correct
10 Correct 358 ms 8596 KB Output is correct
11 Correct 341 ms 8600 KB Output is correct
12 Correct 188 ms 7832 KB Output is correct
13 Correct 1125 ms 9876 KB Output is correct
14 Correct 1143 ms 9664 KB Output is correct
15 Correct 1048 ms 9284 KB Output is correct
16 Correct 951 ms 8980 KB Output is correct
17 Correct 492 ms 8896 KB Output is correct
18 Correct 409 ms 7876 KB Output is correct
19 Correct 1172 ms 9876 KB Output is correct
20 Correct 1120 ms 9756 KB Output is correct
21 Correct 1461 ms 9668 KB Output is correct
22 Correct 343 ms 8516 KB Output is correct
23 Correct 370 ms 8596 KB Output is correct
24 Correct 202 ms 7832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 328 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 6 ms 588 KB Output is correct
8 Correct 6 ms 588 KB Output is correct
9 Correct 6 ms 588 KB Output is correct
10 Correct 5 ms 588 KB Output is correct
11 Correct 6 ms 588 KB Output is correct
12 Correct 4 ms 460 KB Output is correct
13 Correct 5 ms 660 KB Output is correct
14 Correct 5 ms 588 KB Output is correct
15 Correct 5 ms 588 KB Output is correct
16 Correct 5 ms 616 KB Output is correct
17 Correct 4 ms 588 KB Output is correct
18 Correct 3 ms 460 KB Output is correct
19 Correct 1063 ms 9368 KB Output is correct
20 Correct 1070 ms 9280 KB Output is correct
21 Correct 1025 ms 9284 KB Output is correct
22 Correct 848 ms 8648 KB Output is correct
23 Correct 427 ms 8652 KB Output is correct
24 Correct 402 ms 7828 KB Output is correct
25 Correct 1841 ms 9240 KB Output is correct
26 Correct 784 ms 9288 KB Output is correct
27 Correct 1334 ms 9156 KB Output is correct
28 Correct 358 ms 8596 KB Output is correct
29 Correct 341 ms 8600 KB Output is correct
30 Correct 188 ms 7832 KB Output is correct
31 Correct 1125 ms 9876 KB Output is correct
32 Correct 1143 ms 9664 KB Output is correct
33 Correct 1048 ms 9284 KB Output is correct
34 Correct 951 ms 8980 KB Output is correct
35 Correct 492 ms 8896 KB Output is correct
36 Correct 409 ms 7876 KB Output is correct
37 Correct 1172 ms 9876 KB Output is correct
38 Correct 1120 ms 9756 KB Output is correct
39 Correct 1461 ms 9668 KB Output is correct
40 Correct 343 ms 8516 KB Output is correct
41 Correct 370 ms 8596 KB Output is correct
42 Correct 202 ms 7832 KB Output is correct
43 Correct 1124 ms 11660 KB Output is correct
44 Correct 1106 ms 11716 KB Output is correct
45 Correct 1109 ms 11588 KB Output is correct
46 Correct 949 ms 10116 KB Output is correct
47 Correct 470 ms 10180 KB Output is correct
48 Correct 430 ms 7872 KB Output is correct
49 Correct 1983 ms 11588 KB Output is correct
50 Correct 834 ms 11668 KB Output is correct
51 Correct 1452 ms 11416 KB Output is correct
52 Correct 387 ms 10260 KB Output is correct
53 Correct 334 ms 9360 KB Output is correct