Submission #476652

# Submission time Handle Problem Language Result Execution time Memory
476652 2021-09-28T00:14:33 Z thiago_bastos Examination (JOI19_examination) C++17
100 / 100
1444 ms 6804 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 = 500, chunks = 300;

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 1 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 7 ms 716 KB Output is correct
8 Correct 7 ms 568 KB Output is correct
9 Correct 6 ms 588 KB Output is correct
10 Correct 5 ms 588 KB Output is correct
11 Correct 5 ms 588 KB Output is correct
12 Correct 4 ms 460 KB Output is correct
13 Correct 5 ms 588 KB Output is correct
14 Correct 6 ms 588 KB Output is correct
15 Correct 5 ms 596 KB Output is correct
16 Correct 5 ms 588 KB Output is correct
17 Correct 5 ms 552 KB Output is correct
18 Correct 3 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 759 ms 6684 KB Output is correct
2 Correct 753 ms 6684 KB Output is correct
3 Correct 755 ms 6716 KB Output is correct
4 Correct 603 ms 6724 KB Output is correct
5 Correct 365 ms 6724 KB Output is correct
6 Correct 329 ms 6724 KB Output is correct
7 Correct 1287 ms 6692 KB Output is correct
8 Correct 595 ms 6724 KB Output is correct
9 Correct 1002 ms 6716 KB Output is correct
10 Correct 260 ms 6720 KB Output is correct
11 Correct 278 ms 6648 KB Output is correct
12 Correct 151 ms 6692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 759 ms 6684 KB Output is correct
2 Correct 753 ms 6684 KB Output is correct
3 Correct 755 ms 6716 KB Output is correct
4 Correct 603 ms 6724 KB Output is correct
5 Correct 365 ms 6724 KB Output is correct
6 Correct 329 ms 6724 KB Output is correct
7 Correct 1287 ms 6692 KB Output is correct
8 Correct 595 ms 6724 KB Output is correct
9 Correct 1002 ms 6716 KB Output is correct
10 Correct 260 ms 6720 KB Output is correct
11 Correct 278 ms 6648 KB Output is correct
12 Correct 151 ms 6692 KB Output is correct
13 Correct 822 ms 6664 KB Output is correct
14 Correct 837 ms 6708 KB Output is correct
15 Correct 746 ms 6716 KB Output is correct
16 Correct 687 ms 6716 KB Output is correct
17 Correct 365 ms 6728 KB Output is correct
18 Correct 307 ms 6684 KB Output is correct
19 Correct 827 ms 6728 KB Output is correct
20 Correct 846 ms 6724 KB Output is correct
21 Correct 1071 ms 6724 KB Output is correct
22 Correct 257 ms 6716 KB Output is correct
23 Correct 266 ms 6696 KB Output is correct
24 Correct 157 ms 6724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 7 ms 716 KB Output is correct
8 Correct 7 ms 568 KB Output is correct
9 Correct 6 ms 588 KB Output is correct
10 Correct 5 ms 588 KB Output is correct
11 Correct 5 ms 588 KB Output is correct
12 Correct 4 ms 460 KB Output is correct
13 Correct 5 ms 588 KB Output is correct
14 Correct 6 ms 588 KB Output is correct
15 Correct 5 ms 596 KB Output is correct
16 Correct 5 ms 588 KB Output is correct
17 Correct 5 ms 552 KB Output is correct
18 Correct 3 ms 460 KB Output is correct
19 Correct 759 ms 6684 KB Output is correct
20 Correct 753 ms 6684 KB Output is correct
21 Correct 755 ms 6716 KB Output is correct
22 Correct 603 ms 6724 KB Output is correct
23 Correct 365 ms 6724 KB Output is correct
24 Correct 329 ms 6724 KB Output is correct
25 Correct 1287 ms 6692 KB Output is correct
26 Correct 595 ms 6724 KB Output is correct
27 Correct 1002 ms 6716 KB Output is correct
28 Correct 260 ms 6720 KB Output is correct
29 Correct 278 ms 6648 KB Output is correct
30 Correct 151 ms 6692 KB Output is correct
31 Correct 822 ms 6664 KB Output is correct
32 Correct 837 ms 6708 KB Output is correct
33 Correct 746 ms 6716 KB Output is correct
34 Correct 687 ms 6716 KB Output is correct
35 Correct 365 ms 6728 KB Output is correct
36 Correct 307 ms 6684 KB Output is correct
37 Correct 827 ms 6728 KB Output is correct
38 Correct 846 ms 6724 KB Output is correct
39 Correct 1071 ms 6724 KB Output is correct
40 Correct 257 ms 6716 KB Output is correct
41 Correct 266 ms 6696 KB Output is correct
42 Correct 157 ms 6724 KB Output is correct
43 Correct 838 ms 6688 KB Output is correct
44 Correct 836 ms 6724 KB Output is correct
45 Correct 856 ms 6688 KB Output is correct
46 Correct 684 ms 6668 KB Output is correct
47 Correct 413 ms 6692 KB Output is correct
48 Correct 349 ms 6668 KB Output is correct
49 Correct 1444 ms 6712 KB Output is correct
50 Correct 645 ms 6692 KB Output is correct
51 Correct 1068 ms 6804 KB Output is correct
52 Correct 306 ms 6724 KB Output is correct
53 Correct 263 ms 6716 KB Output is correct