#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = (int) 1e5 + 7;
const int INF = (int) 1e9;
mt19937 rng;
int gen() {
return uniform_int_distribution<int>(INF)(rng);
}
struct treap {
struct node {
int val;
int sz;
int prio;
node* left;
node* right;
node(int val_ = 0) {
val = val_;
sz = 1;
prio = gen();
left = NULL;
right = NULL;
}
};
int getsize(node* &root) {
if (root == NULL) {
return 0;
} else {
return root->sz;
}
}
void computesize(node* &root) {
root->sz = 1 + getsize(root->left) + getsize(root->right);
}
pair<node*, node*> split(node* &root, int target) {
if (root == NULL) {
return {NULL, NULL};
} else if (root->val <= target) {
pair<node*, node*> sol = split(root->right, target);
root->right = sol.first;
computesize(root);
return {root, sol.second};
} else {
pair<node*, node*> sol = split(root->left, target);
root->left = sol.second;
computesize(root);
return {sol.first, root};
}
}
node* _merge(node* &a, node* &b) {
if (a == NULL && b == NULL) {
return NULL;
} else if (a == NULL) {
return b;
} else if (b == NULL) {
return a;
} else if (a->prio < b->prio) {
a->right = _merge(a->right, b);
computesize(a);
return a;
} else {
b->left = _merge(a, b->left);
computesize(b);
return b;
}
}
node* root;
treap() {
root = NULL;
}
void _insert(int val) {
pair<node*, node*> sol = split(root, val);
node* newnode = new node(val);
sol.first = _merge(sol.first, newnode);
root = _merge(sol.first, sol.second);
}
int _query(int val) {
pair<node*, node*> sol = split(root, val - 1);
int ret = getsize(sol.second);
root = _merge(sol.first, sol.second);
return ret;
}
};
struct T {
int type;
int x, y, z;
int id;
};
bool operator < (const T &a, const T &b) {
if (a.x != b.x) {
return a.x > b.x;
} else {
return a.type < b.type;
}
}
vector<T> events;
int sol[N];
int n, q;
struct segtree {
vector<treap> segt;
segtree(int n) {
segt.resize(4 * n + 1);
}
void update(int v, int tl, int tr, int x, int val) {
segt[v]._insert(val);
if (tl == tr) {
return;
}
int tm = (tl + tr) / 2;
if (x <= tm) {
update(2 * v, tl, tm, x, val);
} else {
update(2 * v + 1, tm + 1, tr, x, val);
}
}
int query(int v, int tl, int tr, int x, int y, int val) {
if (x <= tl && tr <= y) {
return segt[v]._query(val);
} else {
int tm = (tl + tr) / 2;
if (x <= tm & y <= tm) {
return query(2 * v, tl, tm, x, y, val);
} else if (tm + 1 <= x && tm + 1 <= y) {
return query(2 * v + 1, tm + 1, tr, x, y, val);
} else {
return query(2 * v, tl, tm, x, tm, val) + query(2 * v + 1, tm + 1, tr, tm + 1, y, val);
}
}
}
};
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// freopen("input", "r", stdin);
cin >> n >> q;
for (int i = 1; i <= n; i++) {
int x, y;
cin >> x >> y;
events.push_back({1, x, y, x + y, i});
}
for (int i = 1; i <= q; i++) {
int x, y, z;
cin >> x >> y >> z;
events.push_back({2, x, y, z, i});
}
sort(events.begin(), events.end());
{
vector<int> vals;
for (auto &e : events) {
vals.push_back(e.x);
vals.push_back(e.y);
vals.push_back(e.z);
}
sort(vals.begin(), vals.end());
map<int, int> mp;
int cnt = 0;
for (auto &i : vals) {
if (mp[i] == 0) {
mp[i] = ++cnt;
}
}
for (auto &e : events) {
e.x = mp[e.x];
e.y = mp[e.y];
e.z = mp[e.z];
}
n = cnt;
}
segtree tree(n);
for (auto &e : events) {
if (e.type == 1) {
tree.update(1, 1, n, e.y, e.z);
} else {
sol[e.id] = tree.query(1, 1, n, e.y, n, e.z);
}
}
for (int i = 1; i <= q; i++) {
cout << sol[i] << " ";
}
return 0;
}
Compilation message
examination.cpp: In member function 'int segtree::query(int, int, int, int, int, int)':
examination.cpp:139:13: warning: suggest parentheses around comparison in operand of '&' [-Wparentheses]
139 | if (x <= tm & y <= tm) {
| ~~^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
17 ms |
3212 KB |
Output is correct |
8 |
Correct |
17 ms |
3220 KB |
Output is correct |
9 |
Correct |
19 ms |
3308 KB |
Output is correct |
10 |
Correct |
13 ms |
2924 KB |
Output is correct |
11 |
Correct |
15 ms |
2904 KB |
Output is correct |
12 |
Correct |
7 ms |
1260 KB |
Output is correct |
13 |
Correct |
14 ms |
3184 KB |
Output is correct |
14 |
Correct |
14 ms |
3168 KB |
Output is correct |
15 |
Correct |
15 ms |
3088 KB |
Output is correct |
16 |
Correct |
13 ms |
2628 KB |
Output is correct |
17 |
Correct |
12 ms |
2952 KB |
Output is correct |
18 |
Correct |
3 ms |
876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1043 ms |
96688 KB |
Output is correct |
2 |
Correct |
1103 ms |
96788 KB |
Output is correct |
3 |
Correct |
1098 ms |
96676 KB |
Output is correct |
4 |
Correct |
566 ms |
93092 KB |
Output is correct |
5 |
Correct |
1396 ms |
92692 KB |
Output is correct |
6 |
Correct |
301 ms |
32172 KB |
Output is correct |
7 |
Correct |
1113 ms |
96516 KB |
Output is correct |
8 |
Correct |
1157 ms |
96540 KB |
Output is correct |
9 |
Correct |
1006 ms |
96076 KB |
Output is correct |
10 |
Correct |
1324 ms |
91640 KB |
Output is correct |
11 |
Correct |
510 ms |
94008 KB |
Output is correct |
12 |
Correct |
92 ms |
19828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1043 ms |
96688 KB |
Output is correct |
2 |
Correct |
1103 ms |
96788 KB |
Output is correct |
3 |
Correct |
1098 ms |
96676 KB |
Output is correct |
4 |
Correct |
566 ms |
93092 KB |
Output is correct |
5 |
Correct |
1396 ms |
92692 KB |
Output is correct |
6 |
Correct |
301 ms |
32172 KB |
Output is correct |
7 |
Correct |
1113 ms |
96516 KB |
Output is correct |
8 |
Correct |
1157 ms |
96540 KB |
Output is correct |
9 |
Correct |
1006 ms |
96076 KB |
Output is correct |
10 |
Correct |
1324 ms |
91640 KB |
Output is correct |
11 |
Correct |
510 ms |
94008 KB |
Output is correct |
12 |
Correct |
92 ms |
19828 KB |
Output is correct |
13 |
Correct |
1374 ms |
99344 KB |
Output is correct |
14 |
Correct |
1197 ms |
97216 KB |
Output is correct |
15 |
Correct |
1072 ms |
96692 KB |
Output is correct |
16 |
Correct |
717 ms |
93624 KB |
Output is correct |
17 |
Correct |
1566 ms |
93396 KB |
Output is correct |
18 |
Correct |
295 ms |
32232 KB |
Output is correct |
19 |
Correct |
1387 ms |
99516 KB |
Output is correct |
20 |
Correct |
1334 ms |
98284 KB |
Output is correct |
21 |
Correct |
1580 ms |
99240 KB |
Output is correct |
22 |
Correct |
1267 ms |
91516 KB |
Output is correct |
23 |
Correct |
537 ms |
94020 KB |
Output is correct |
24 |
Correct |
96 ms |
19912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
17 ms |
3212 KB |
Output is correct |
8 |
Correct |
17 ms |
3220 KB |
Output is correct |
9 |
Correct |
19 ms |
3308 KB |
Output is correct |
10 |
Correct |
13 ms |
2924 KB |
Output is correct |
11 |
Correct |
15 ms |
2904 KB |
Output is correct |
12 |
Correct |
7 ms |
1260 KB |
Output is correct |
13 |
Correct |
14 ms |
3184 KB |
Output is correct |
14 |
Correct |
14 ms |
3168 KB |
Output is correct |
15 |
Correct |
15 ms |
3088 KB |
Output is correct |
16 |
Correct |
13 ms |
2628 KB |
Output is correct |
17 |
Correct |
12 ms |
2952 KB |
Output is correct |
18 |
Correct |
3 ms |
876 KB |
Output is correct |
19 |
Correct |
1043 ms |
96688 KB |
Output is correct |
20 |
Correct |
1103 ms |
96788 KB |
Output is correct |
21 |
Correct |
1098 ms |
96676 KB |
Output is correct |
22 |
Correct |
566 ms |
93092 KB |
Output is correct |
23 |
Correct |
1396 ms |
92692 KB |
Output is correct |
24 |
Correct |
301 ms |
32172 KB |
Output is correct |
25 |
Correct |
1113 ms |
96516 KB |
Output is correct |
26 |
Correct |
1157 ms |
96540 KB |
Output is correct |
27 |
Correct |
1006 ms |
96076 KB |
Output is correct |
28 |
Correct |
1324 ms |
91640 KB |
Output is correct |
29 |
Correct |
510 ms |
94008 KB |
Output is correct |
30 |
Correct |
92 ms |
19828 KB |
Output is correct |
31 |
Correct |
1374 ms |
99344 KB |
Output is correct |
32 |
Correct |
1197 ms |
97216 KB |
Output is correct |
33 |
Correct |
1072 ms |
96692 KB |
Output is correct |
34 |
Correct |
717 ms |
93624 KB |
Output is correct |
35 |
Correct |
1566 ms |
93396 KB |
Output is correct |
36 |
Correct |
295 ms |
32232 KB |
Output is correct |
37 |
Correct |
1387 ms |
99516 KB |
Output is correct |
38 |
Correct |
1334 ms |
98284 KB |
Output is correct |
39 |
Correct |
1580 ms |
99240 KB |
Output is correct |
40 |
Correct |
1267 ms |
91516 KB |
Output is correct |
41 |
Correct |
537 ms |
94020 KB |
Output is correct |
42 |
Correct |
96 ms |
19912 KB |
Output is correct |
43 |
Correct |
1611 ms |
123880 KB |
Output is correct |
44 |
Correct |
1659 ms |
123912 KB |
Output is correct |
45 |
Correct |
1518 ms |
123816 KB |
Output is correct |
46 |
Correct |
840 ms |
113348 KB |
Output is correct |
47 |
Correct |
1581 ms |
113108 KB |
Output is correct |
48 |
Correct |
346 ms |
38048 KB |
Output is correct |
49 |
Correct |
1706 ms |
123840 KB |
Output is correct |
50 |
Correct |
1575 ms |
123908 KB |
Output is correct |
51 |
Correct |
1697 ms |
123724 KB |
Output is correct |
52 |
Correct |
1535 ms |
108248 KB |
Output is correct |
53 |
Correct |
541 ms |
102940 KB |
Output is correct |