#include "bits/stdc++.h"
using namespace std;
#define for_(i, s, e) for (int i = s; i < (int) e; i++)
#define for__(i, s, e) for (ll i = s; i < e; i++)
typedef long long ll;
typedef vector<int> vi;
typedef array<int, 2> ii;
#define endl '\n'
// Ask for sum 1 -> n for full (one based indexing)
class BIT {
private: vector<int> tree; int n;
int LSOne(int x) {
return x&(-x);
}
public:
void build(int x) {
n = x;
tree.resize(n+1);
}
int sum(int a) {
int sum = 0;
for (; a > 0; a -= LSOne(a)) sum += tree[a];
return sum;
}
int sum(int a, int b) {
return sum(b) - (a == 1 ? 0 : sum(a-1));
}
void update(int p, int v) {
for (; p < n+1; p += LSOne(p)) tree[p] += v;
}
};
int n, q, pt = 1;
vector<array<int, 4>> pts; // {s1, s2, sum, index: 1 -> contestant, else -ve query index}
vi ans;
BIT tree;
map<int, int> mp;
void solve(int l, int r) {
if (l == r) return;
int mid = (l+r)/2;
vector<array<int, 3>> curr; // {s2, sum, index: 1 -> contestant, else -ve query index}
for_(i, mid+1, r+1) if (pts[i][3] != 1) curr.push_back({pts[i][1], pts[i][2], pts[i][3]});
for_(i, l, mid+1) if (pts[i][3] == 1) curr.push_back({pts[i][1], pts[i][2], 1});
sort(curr.rbegin(), curr.rend());
for (auto &i: curr) {
if (i[2] == 1) tree.update(i[1], 1);
else ans[-i[2]] += tree.sum(i[1], pt);
}
for (auto &i: curr) if (i[2] == 1) tree.update(i[1], -1);
solve(l, mid); solve(mid+1, r);
}
int main() {
#ifdef mlocal
freopen("test.in", "r", stdin);
#endif
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> q;
pts.resize(n+q); ans.resize(q);
for_(i, 0, n) {
cin >> pts[i][0] >> pts[i][1];
pts[i][2] = pts[i][0]+pts[i][1]; pts[i][3] = 1;
mp[pts[i][1]] = mp[pts[i][2]] = 0;
}
for_(i, n, n+q) {
cin >> pts[i][0] >> pts[i][1] >> pts[i][2];
pts[i][3] = n-i;
mp[pts[i][1]] = mp[pts[i][2]] = 0;
}
for (auto &i: mp) mp[i.first] = pt++;
for_(i, 0, n+q) {
pts[i][1] = mp[pts[i][1]]; pts[i][2] = mp[pts[i][2]];
}
sort(pts.rbegin(), pts.rend());
tree.build(pt);
solve(0, n+q-1);
for (auto i: ans) cout << i << endl;
cout << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
13 ms |
1132 KB |
Output is correct |
8 |
Correct |
14 ms |
1132 KB |
Output is correct |
9 |
Correct |
14 ms |
1132 KB |
Output is correct |
10 |
Correct |
11 ms |
876 KB |
Output is correct |
11 |
Correct |
13 ms |
1260 KB |
Output is correct |
12 |
Correct |
7 ms |
492 KB |
Output is correct |
13 |
Correct |
12 ms |
1132 KB |
Output is correct |
14 |
Correct |
12 ms |
1132 KB |
Output is correct |
15 |
Correct |
12 ms |
1132 KB |
Output is correct |
16 |
Correct |
9 ms |
1004 KB |
Output is correct |
17 |
Correct |
10 ms |
1004 KB |
Output is correct |
18 |
Correct |
6 ms |
640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
562 ms |
13028 KB |
Output is correct |
2 |
Correct |
558 ms |
12900 KB |
Output is correct |
3 |
Correct |
542 ms |
12900 KB |
Output is correct |
4 |
Correct |
416 ms |
9696 KB |
Output is correct |
5 |
Correct |
429 ms |
11360 KB |
Output is correct |
6 |
Correct |
236 ms |
6756 KB |
Output is correct |
7 |
Correct |
551 ms |
16860 KB |
Output is correct |
8 |
Correct |
504 ms |
14820 KB |
Output is correct |
9 |
Correct |
503 ms |
16096 KB |
Output is correct |
10 |
Correct |
393 ms |
12256 KB |
Output is correct |
11 |
Correct |
394 ms |
11360 KB |
Output is correct |
12 |
Correct |
212 ms |
6600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
562 ms |
13028 KB |
Output is correct |
2 |
Correct |
558 ms |
12900 KB |
Output is correct |
3 |
Correct |
542 ms |
12900 KB |
Output is correct |
4 |
Correct |
416 ms |
9696 KB |
Output is correct |
5 |
Correct |
429 ms |
11360 KB |
Output is correct |
6 |
Correct |
236 ms |
6756 KB |
Output is correct |
7 |
Correct |
551 ms |
16860 KB |
Output is correct |
8 |
Correct |
504 ms |
14820 KB |
Output is correct |
9 |
Correct |
503 ms |
16096 KB |
Output is correct |
10 |
Correct |
393 ms |
12256 KB |
Output is correct |
11 |
Correct |
394 ms |
11360 KB |
Output is correct |
12 |
Correct |
212 ms |
6600 KB |
Output is correct |
13 |
Correct |
727 ms |
17380 KB |
Output is correct |
14 |
Correct |
641 ms |
16356 KB |
Output is correct |
15 |
Correct |
555 ms |
15452 KB |
Output is correct |
16 |
Correct |
502 ms |
13028 KB |
Output is correct |
17 |
Correct |
520 ms |
13536 KB |
Output is correct |
18 |
Correct |
238 ms |
8036 KB |
Output is correct |
19 |
Correct |
707 ms |
17376 KB |
Output is correct |
20 |
Correct |
689 ms |
16608 KB |
Output is correct |
21 |
Correct |
703 ms |
18908 KB |
Output is correct |
22 |
Correct |
398 ms |
12256 KB |
Output is correct |
23 |
Correct |
394 ms |
11360 KB |
Output is correct |
24 |
Correct |
211 ms |
6688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
13 ms |
1132 KB |
Output is correct |
8 |
Correct |
14 ms |
1132 KB |
Output is correct |
9 |
Correct |
14 ms |
1132 KB |
Output is correct |
10 |
Correct |
11 ms |
876 KB |
Output is correct |
11 |
Correct |
13 ms |
1260 KB |
Output is correct |
12 |
Correct |
7 ms |
492 KB |
Output is correct |
13 |
Correct |
12 ms |
1132 KB |
Output is correct |
14 |
Correct |
12 ms |
1132 KB |
Output is correct |
15 |
Correct |
12 ms |
1132 KB |
Output is correct |
16 |
Correct |
9 ms |
1004 KB |
Output is correct |
17 |
Correct |
10 ms |
1004 KB |
Output is correct |
18 |
Correct |
6 ms |
640 KB |
Output is correct |
19 |
Correct |
562 ms |
13028 KB |
Output is correct |
20 |
Correct |
558 ms |
12900 KB |
Output is correct |
21 |
Correct |
542 ms |
12900 KB |
Output is correct |
22 |
Correct |
416 ms |
9696 KB |
Output is correct |
23 |
Correct |
429 ms |
11360 KB |
Output is correct |
24 |
Correct |
236 ms |
6756 KB |
Output is correct |
25 |
Correct |
551 ms |
16860 KB |
Output is correct |
26 |
Correct |
504 ms |
14820 KB |
Output is correct |
27 |
Correct |
503 ms |
16096 KB |
Output is correct |
28 |
Correct |
393 ms |
12256 KB |
Output is correct |
29 |
Correct |
394 ms |
11360 KB |
Output is correct |
30 |
Correct |
212 ms |
6600 KB |
Output is correct |
31 |
Correct |
727 ms |
17380 KB |
Output is correct |
32 |
Correct |
641 ms |
16356 KB |
Output is correct |
33 |
Correct |
555 ms |
15452 KB |
Output is correct |
34 |
Correct |
502 ms |
13028 KB |
Output is correct |
35 |
Correct |
520 ms |
13536 KB |
Output is correct |
36 |
Correct |
238 ms |
8036 KB |
Output is correct |
37 |
Correct |
707 ms |
17376 KB |
Output is correct |
38 |
Correct |
689 ms |
16608 KB |
Output is correct |
39 |
Correct |
703 ms |
18908 KB |
Output is correct |
40 |
Correct |
398 ms |
12256 KB |
Output is correct |
41 |
Correct |
394 ms |
11360 KB |
Output is correct |
42 |
Correct |
211 ms |
6688 KB |
Output is correct |
43 |
Correct |
1040 ms |
31624 KB |
Output is correct |
44 |
Correct |
973 ms |
31584 KB |
Output is correct |
45 |
Correct |
969 ms |
31584 KB |
Output is correct |
46 |
Correct |
627 ms |
19968 KB |
Output is correct |
47 |
Correct |
845 ms |
29540 KB |
Output is correct |
48 |
Correct |
252 ms |
7904 KB |
Output is correct |
49 |
Correct |
989 ms |
33140 KB |
Output is correct |
50 |
Correct |
991 ms |
31620 KB |
Output is correct |
51 |
Correct |
954 ms |
32988 KB |
Output is correct |
52 |
Correct |
737 ms |
24804 KB |
Output is correct |
53 |
Correct |
416 ms |
14096 KB |
Output is correct |