Submission #998644

# Submission time Handle Problem Language Result Execution time Memory
998644 2024-06-14T12:07:11 Z VMaksimoski008 Examination (JOI19_examination) C++17
100 / 100
729 ms 91412 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;

struct BIT_2D_Offline {
	int n;
	vector<vector<int> > tree, vals;

	BIT_2D_Offline(int n, vector<pii> &U) : n(n), vals(n+1), tree(n+1) {
		sort(U.begin(), U.end(), [&](pii &a, pii &b) { return a.second < b.second; });
		for(int i=1; i<=n; i++) vals[i].push_back( -1 );

		for(auto &[x, y]: U)
			for(; x<=n; x+=x&-x)
				if(vals[x].back() != y) vals[x].push_back(y);

		for(int i=1; i<=n; i++) tree[i].resize( vals[i].size() );
	}

	int getId(int i, int v) {
		return upper_bound( vals[i].begin(), vals[i].end(), v ) - vals[i].begin() - 1;
	}

	void update(int x, int y, int v) {
		for(; x<=n; x+=x&-x)
			for(int p=getId(x, y); p<tree[x].size(); p+=p&-p) tree[x][p] += v;
	}

	int Q(int x, int y) {
		if(x <= 0 || y <= 0) return 0;
		int ans = 0;
		for(; x; x-=x&-x)
			for(int p=getId(x, y); p; p-=p&-p) ans += tree[x][p];
		return ans;
	}

	int query(int x1, int y1, int x2, int y2) {
		if(x1 > x2 || y1 > y2) return 0;
		return Q(x2, y2) - Q(x1-1, y2) - Q(x2, y1-1) + Q(x1-1, y1-1);
	}
};

int32_t main() {
    ios_base::sync_with_stdio(false);
    cout.tie(0); cin.tie(0);

    int n, q;
    cin >> n >> q;

    vector<pii> v(n);
    set<int> s; s.insert(-1);

    for(pii &x : v) {
        cin >> x.first >> x.second;
        s.insert(x.first);
        s.insert(x.second);
    }

    vector<array<int, 4> > qus(q);
    for(int i=0; i<q; i++) {
        cin >> qus[i][0] >> qus[i][1] >> qus[i][2];
        qus[i][3] = i;
        s.insert(qus[i][0]);
        s.insert(qus[i][1]);
        s.insert(qus[i][2]);
    }

    vector<int> comp(s.begin(), s.end());
    vector<pii> U;
    for(auto &x : v) {
        x.first = lower_bound(comp.begin(), comp.end(), x.first) - comp.begin();
        x.second = lower_bound(comp.begin(), comp.end(), x.second) - comp.begin();
        U.push_back(x);
    }

    for(auto &x : qus) {
        x[0] = lower_bound(comp.begin(), comp.end(), x[0]) - comp.begin();
        x[1] = lower_bound(comp.begin(), comp.end(), x[1]) - comp.begin();
        x[2] = lower_bound(comp.begin(), comp.end(), x[2]) - comp.begin();
    }

    BIT_2D_Offline bit(5*n+5, U);

    sort(qus.begin(), qus.end(), [&](array<int, 4> &a, array<int, 4> &b) {
        return a[2] > b[2];
    });

    sort(v.begin(), v.end(), [&](pii &a, pii &b) {
        return comp[a.first] + comp[a.second] > comp[b.first] + comp[b.second];
    });

    vector<int> ans(q);

    int j=-1;
    for(auto &qu : qus) {
        while(j+1 < n && comp[v[j+1].first] + comp[v[j+1].second] >= comp[qu[2]]) {
            j++;
            bit.update(v[j].first, v[j].second, 1);
        }

        ans[qu[3]] = bit.query(qu[0], qu[1], 5*n, 5*n);
    }

    for(int &x : ans) cout << x << '\n';
    return 0;
}

Compilation message

examination.cpp: In constructor 'BIT_2D_Offline::BIT_2D_Offline(int, std::vector<std::pair<int, int> >&)':
examination.cpp:8:29: warning: 'BIT_2D_Offline::vals' will be initialized after [-Wreorder]
    8 |  vector<vector<int> > tree, vals;
      |                             ^~~~
examination.cpp:8:23: warning:   'std::vector<std::vector<int> > BIT_2D_Offline::tree' [-Wreorder]
    8 |  vector<vector<int> > tree, vals;
      |                       ^~~~
examination.cpp:10:2: warning:   when initialized here [-Wreorder]
   10 |  BIT_2D_Offline(int n, vector<pii> &U) : n(n), vals(n+1), tree(n+1) {
      |  ^~~~~~~~~~~~~~
examination.cpp: In member function 'void BIT_2D_Offline::update(int, int, int)':
examination.cpp:27:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   27 |    for(int p=getId(x, y); p<tree[x].size(); p+=p&-p) tree[x][p] += v;
      |                           ~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 11 ms 2908 KB Output is correct
8 Correct 10 ms 2908 KB Output is correct
9 Correct 12 ms 2908 KB Output is correct
10 Correct 7 ms 2496 KB Output is correct
11 Correct 8 ms 2812 KB Output is correct
12 Correct 5 ms 2140 KB Output is correct
13 Correct 9 ms 2908 KB Output is correct
14 Correct 10 ms 2908 KB Output is correct
15 Correct 10 ms 2908 KB Output is correct
16 Correct 7 ms 2908 KB Output is correct
17 Correct 7 ms 2652 KB Output is correct
18 Correct 3 ms 2140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 457 ms 71712 KB Output is correct
2 Correct 453 ms 71628 KB Output is correct
3 Correct 454 ms 71628 KB Output is correct
4 Correct 232 ms 64452 KB Output is correct
5 Correct 285 ms 72900 KB Output is correct
6 Correct 114 ms 59344 KB Output is correct
7 Correct 391 ms 71852 KB Output is correct
8 Correct 411 ms 71624 KB Output is correct
9 Correct 373 ms 71372 KB Output is correct
10 Correct 275 ms 72904 KB Output is correct
11 Correct 183 ms 63432 KB Output is correct
12 Correct 94 ms 58900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 457 ms 71712 KB Output is correct
2 Correct 453 ms 71628 KB Output is correct
3 Correct 454 ms 71628 KB Output is correct
4 Correct 232 ms 64452 KB Output is correct
5 Correct 285 ms 72900 KB Output is correct
6 Correct 114 ms 59344 KB Output is correct
7 Correct 391 ms 71852 KB Output is correct
8 Correct 411 ms 71624 KB Output is correct
9 Correct 373 ms 71372 KB Output is correct
10 Correct 275 ms 72904 KB Output is correct
11 Correct 183 ms 63432 KB Output is correct
12 Correct 94 ms 58900 KB Output is correct
13 Correct 481 ms 73648 KB Output is correct
14 Correct 469 ms 71592 KB Output is correct
15 Correct 437 ms 71620 KB Output is correct
16 Correct 257 ms 64712 KB Output is correct
17 Correct 324 ms 73416 KB Output is correct
18 Correct 112 ms 59340 KB Output is correct
19 Correct 487 ms 73680 KB Output is correct
20 Correct 470 ms 72924 KB Output is correct
21 Correct 459 ms 73676 KB Output is correct
22 Correct 262 ms 72896 KB Output is correct
23 Correct 188 ms 63428 KB Output is correct
24 Correct 90 ms 58832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 11 ms 2908 KB Output is correct
8 Correct 10 ms 2908 KB Output is correct
9 Correct 12 ms 2908 KB Output is correct
10 Correct 7 ms 2496 KB Output is correct
11 Correct 8 ms 2812 KB Output is correct
12 Correct 5 ms 2140 KB Output is correct
13 Correct 9 ms 2908 KB Output is correct
14 Correct 10 ms 2908 KB Output is correct
15 Correct 10 ms 2908 KB Output is correct
16 Correct 7 ms 2908 KB Output is correct
17 Correct 7 ms 2652 KB Output is correct
18 Correct 3 ms 2140 KB Output is correct
19 Correct 457 ms 71712 KB Output is correct
20 Correct 453 ms 71628 KB Output is correct
21 Correct 454 ms 71628 KB Output is correct
22 Correct 232 ms 64452 KB Output is correct
23 Correct 285 ms 72900 KB Output is correct
24 Correct 114 ms 59344 KB Output is correct
25 Correct 391 ms 71852 KB Output is correct
26 Correct 411 ms 71624 KB Output is correct
27 Correct 373 ms 71372 KB Output is correct
28 Correct 275 ms 72904 KB Output is correct
29 Correct 183 ms 63432 KB Output is correct
30 Correct 94 ms 58900 KB Output is correct
31 Correct 481 ms 73648 KB Output is correct
32 Correct 469 ms 71592 KB Output is correct
33 Correct 437 ms 71620 KB Output is correct
34 Correct 257 ms 64712 KB Output is correct
35 Correct 324 ms 73416 KB Output is correct
36 Correct 112 ms 59340 KB Output is correct
37 Correct 487 ms 73680 KB Output is correct
38 Correct 470 ms 72924 KB Output is correct
39 Correct 459 ms 73676 KB Output is correct
40 Correct 262 ms 72896 KB Output is correct
41 Correct 188 ms 63428 KB Output is correct
42 Correct 90 ms 58832 KB Output is correct
43 Correct 700 ms 90564 KB Output is correct
44 Correct 729 ms 90636 KB Output is correct
45 Correct 705 ms 90568 KB Output is correct
46 Correct 364 ms 75212 KB Output is correct
47 Correct 408 ms 89284 KB Output is correct
48 Correct 120 ms 59084 KB Output is correct
49 Correct 676 ms 90824 KB Output is correct
50 Correct 683 ms 91412 KB Output is correct
51 Correct 632 ms 90820 KB Output is correct
52 Correct 387 ms 90312 KB Output is correct
53 Correct 241 ms 69576 KB Output is correct