#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, q;
pair<ll, int> X[100002], Y[100002], XY[100002];
ll x[100002], y[100002], xy[100002];
struct test1
{
ll A, B, C; int i;
test1() : test1(0, 0, 0, 0) {}
test1(ll _A, ll _B, ll _C, int _i) : A(_A), B(_B), C(_C), i(_i) {}
bool operator < (const test1 &O) const
{
return C < O.C;
}
};
struct test2
{
ll A, B; int i;
test2() : test2(0, 0, 0) {}
test2(ll _A, ll _B, int _i) :A(_A), B(_B), i(_i) {}
bool operator < (const test2 &O) const
{
return A < O.A;
}
};
ll S[100002], T[100002];
vector<test1> vec1;
vector<test2> vec2;
ll tree[400002][3];
ll ans[100002];
void add(int i, int l, int r, int idx, int type)
{
if(l == r) { tree[i][type]++; return; }
int m = (l + r)/2;
if(idx <= m) add(i*2, l, m, idx, type);
else add(i*2+1, m+1, r, idx, type);
tree[i][type] = tree[i*2][type] + tree[i*2+1][type];
}
ll query(int i, int l, int r, int s, int e, int type)
{
if(s > e) return 0;
if(e < l || r < s) return 0;
if(s <= l && r <= e) return tree[i][type];
int m = (l + r)/2;
return query(i*2, l, m, s, e, type) + query(i*2+1, m+1, r, s, e, type);
}
int main()
{
ios::sync_with_stdio(false); cin.tie(NULL);
cin >> n >> q;
for(int i=1;i<=n;i++)
{
cin >> S[i] >> T[i];
X[i] = {S[i], i};
Y[i] = {T[i], i};
XY[i] = {S[i] + T[i], i};
}
sort(X+1, X+n+1);
sort(Y+1, Y+n+1);
sort(XY+1, XY+n+1);
for(int i=1;i<=n;i++)
{
x[i] = X[i].first;
y[i] = Y[i].first;
xy[i] = XY[i].first;
}
for(int i=1;i<=q;i++)
{
ll A, B, C; cin >> A >> B >> C;
if(A + B > C) vec2.push_back(test2(A, B, i));
else vec1.push_back(test1(A, B, C, i));
}
sort(vec1.begin(), vec1.end());
sort(vec2.begin(), vec2.end());
int M1 = vec1.size(), M2 = vec2.size();
int now = n;
for(int i=M1-1;i>=0;i--)
{
while(now > 0 && XY[now].first >= vec1[i].C)
{
int id = lower_bound(x+1, x+n+1, S[XY[now].second]) - x;
add(1, 1, n, id, 0);
id = lower_bound(y+1, y+n+1, T[XY[now].second]) - y;
add(1, 1, n, id, 1);
now--;
}
int ia = lower_bound(x+1, x+n+1, vec1[i].A) - x;
int ib = lower_bound(y+1, y+n+1, vec1[i].B) - y;
ans[vec1[i].i] = query(1, 1, n, ia, n, 0) - query(1, 1, n, 1, ib-1, 1);
}
now = n;
for(int i=M2-1;i>=0;i--)
{
while(now > 0 && X[now].first >= vec2[i].A)
{
int id = lower_bound(y+1, y+n+1, T[X[now].second]) - y;
add(1, 1, n, id, 2);
now--;
}
int ia = lower_bound(y+1, y+n+1, vec2[i].B) - y;
ans[vec2[i].i] = query(1, 1, n, ia, n, 2);
}
for(int i=1;i<=q;i++)
{
cout << ans[i] << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
7 ms |
1004 KB |
Output is correct |
8 |
Correct |
7 ms |
972 KB |
Output is correct |
9 |
Correct |
7 ms |
972 KB |
Output is correct |
10 |
Correct |
6 ms |
972 KB |
Output is correct |
11 |
Correct |
6 ms |
972 KB |
Output is correct |
12 |
Correct |
5 ms |
972 KB |
Output is correct |
13 |
Correct |
5 ms |
1152 KB |
Output is correct |
14 |
Correct |
5 ms |
1100 KB |
Output is correct |
15 |
Correct |
5 ms |
1112 KB |
Output is correct |
16 |
Correct |
4 ms |
1100 KB |
Output is correct |
17 |
Correct |
5 ms |
976 KB |
Output is correct |
18 |
Correct |
5 ms |
832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
219 ms |
18856 KB |
Output is correct |
2 |
Correct |
208 ms |
21492 KB |
Output is correct |
3 |
Correct |
201 ms |
21452 KB |
Output is correct |
4 |
Correct |
136 ms |
14828 KB |
Output is correct |
5 |
Correct |
184 ms |
20664 KB |
Output is correct |
6 |
Correct |
169 ms |
14056 KB |
Output is correct |
7 |
Correct |
191 ms |
21268 KB |
Output is correct |
8 |
Correct |
196 ms |
21388 KB |
Output is correct |
9 |
Correct |
181 ms |
21192 KB |
Output is correct |
10 |
Correct |
168 ms |
20376 KB |
Output is correct |
11 |
Correct |
160 ms |
20496 KB |
Output is correct |
12 |
Correct |
117 ms |
13536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
219 ms |
18856 KB |
Output is correct |
2 |
Correct |
208 ms |
21492 KB |
Output is correct |
3 |
Correct |
201 ms |
21452 KB |
Output is correct |
4 |
Correct |
136 ms |
14828 KB |
Output is correct |
5 |
Correct |
184 ms |
20664 KB |
Output is correct |
6 |
Correct |
169 ms |
14056 KB |
Output is correct |
7 |
Correct |
191 ms |
21268 KB |
Output is correct |
8 |
Correct |
196 ms |
21388 KB |
Output is correct |
9 |
Correct |
181 ms |
21192 KB |
Output is correct |
10 |
Correct |
168 ms |
20376 KB |
Output is correct |
11 |
Correct |
160 ms |
20496 KB |
Output is correct |
12 |
Correct |
117 ms |
13536 KB |
Output is correct |
13 |
Correct |
345 ms |
22208 KB |
Output is correct |
14 |
Correct |
309 ms |
22888 KB |
Output is correct |
15 |
Correct |
196 ms |
21392 KB |
Output is correct |
16 |
Correct |
219 ms |
21408 KB |
Output is correct |
17 |
Correct |
250 ms |
21368 KB |
Output is correct |
18 |
Correct |
165 ms |
14404 KB |
Output is correct |
19 |
Correct |
349 ms |
22200 KB |
Output is correct |
20 |
Correct |
342 ms |
22312 KB |
Output is correct |
21 |
Correct |
356 ms |
23048 KB |
Output is correct |
22 |
Correct |
172 ms |
20476 KB |
Output is correct |
23 |
Correct |
161 ms |
20376 KB |
Output is correct |
24 |
Correct |
167 ms |
13524 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
7 ms |
1004 KB |
Output is correct |
8 |
Correct |
7 ms |
972 KB |
Output is correct |
9 |
Correct |
7 ms |
972 KB |
Output is correct |
10 |
Correct |
6 ms |
972 KB |
Output is correct |
11 |
Correct |
6 ms |
972 KB |
Output is correct |
12 |
Correct |
5 ms |
972 KB |
Output is correct |
13 |
Correct |
5 ms |
1152 KB |
Output is correct |
14 |
Correct |
5 ms |
1100 KB |
Output is correct |
15 |
Correct |
5 ms |
1112 KB |
Output is correct |
16 |
Correct |
4 ms |
1100 KB |
Output is correct |
17 |
Correct |
5 ms |
976 KB |
Output is correct |
18 |
Correct |
5 ms |
832 KB |
Output is correct |
19 |
Correct |
219 ms |
18856 KB |
Output is correct |
20 |
Correct |
208 ms |
21492 KB |
Output is correct |
21 |
Correct |
201 ms |
21452 KB |
Output is correct |
22 |
Correct |
136 ms |
14828 KB |
Output is correct |
23 |
Correct |
184 ms |
20664 KB |
Output is correct |
24 |
Correct |
169 ms |
14056 KB |
Output is correct |
25 |
Correct |
191 ms |
21268 KB |
Output is correct |
26 |
Correct |
196 ms |
21388 KB |
Output is correct |
27 |
Correct |
181 ms |
21192 KB |
Output is correct |
28 |
Correct |
168 ms |
20376 KB |
Output is correct |
29 |
Correct |
160 ms |
20496 KB |
Output is correct |
30 |
Correct |
117 ms |
13536 KB |
Output is correct |
31 |
Correct |
345 ms |
22208 KB |
Output is correct |
32 |
Correct |
309 ms |
22888 KB |
Output is correct |
33 |
Correct |
196 ms |
21392 KB |
Output is correct |
34 |
Correct |
219 ms |
21408 KB |
Output is correct |
35 |
Correct |
250 ms |
21368 KB |
Output is correct |
36 |
Correct |
165 ms |
14404 KB |
Output is correct |
37 |
Correct |
349 ms |
22200 KB |
Output is correct |
38 |
Correct |
342 ms |
22312 KB |
Output is correct |
39 |
Correct |
356 ms |
23048 KB |
Output is correct |
40 |
Correct |
172 ms |
20476 KB |
Output is correct |
41 |
Correct |
161 ms |
20376 KB |
Output is correct |
42 |
Correct |
167 ms |
13524 KB |
Output is correct |
43 |
Correct |
361 ms |
24168 KB |
Output is correct |
44 |
Correct |
364 ms |
24140 KB |
Output is correct |
45 |
Correct |
321 ms |
24788 KB |
Output is correct |
46 |
Correct |
230 ms |
22572 KB |
Output is correct |
47 |
Correct |
287 ms |
22512 KB |
Output is correct |
48 |
Correct |
216 ms |
15236 KB |
Output is correct |
49 |
Correct |
365 ms |
23960 KB |
Output is correct |
50 |
Correct |
372 ms |
24884 KB |
Output is correct |
51 |
Correct |
353 ms |
24480 KB |
Output is correct |
52 |
Correct |
251 ms |
22432 KB |
Output is correct |
53 |
Correct |
118 ms |
15100 KB |
Output is correct |