#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct BIT{
int n;
int val[1000000];
int get(int x){
int res = 0;
while(x){
res += val[x];
x -= x & -x;
}
return res;
}
void add(int x, int vrd){
while(x <= n){
val[x] += vrd;
x += x & -x;
}
}
};
struct query{
int x, y, z, ind;
};
bool cmp1(query a, query b){
if(a.x == b.x) return a.ind > b.ind;
return a.x < b.x;
}
bool cmp2(query a, query b){
if(a.z == b.z) return a.ind > b.ind;
return a.z < b.z;
}
pair <int, int> niz[100005];
map <int, int> compr;
int tcomp;
query queries1[200005], queries2[200005];
int res[100005];
int main(){
ios_base::sync_with_stdio(false);
int n, q;
cin >> n >> q;
vector <int> compvec;
for(int i=1; i<=n; i++){
cin >> niz[i].first >> niz[i].second;
compvec.push_back(niz[i].first);
compvec.push_back(niz[i].second);
compvec.push_back(niz[i].first+niz[i].second);
}
int q1 = 0, q2 = 0;
for(int i=1; i<=q; i++){
int x, y, z;
cin >> x >> y >> z;
compvec.push_back(x);
compvec.push_back(y);
compvec.push_back(z);
query tq;
tq.x = x, tq.y = y, tq.z = z, tq.ind = i;
if(z <= x+y) queries1[++q1] = tq;
else queries2[++q2] = tq;
}
for(int i=1; i<=n; i++){
query tq;
tq.x = niz[i].first;
tq.y = niz[i].second;
tq.ind = tq.z = 0;
queries1[++q1] = tq;
}
sort(compvec.begin(), compvec.end());
for(auto c : compvec){
if(!compr[c]) compr[c] = ++tcomp;
}
sort(queries1+1, queries1+1+q1, cmp1);
int bilo = 0;
BIT sam;
sam.n = 1000000;
for(int i=q1; i>=1; i--){
if(queries1[i].ind == 0){
bilo++;
sam.add(compr[queries1[i].y], 1);
}
else{
res[queries1[i].ind] += bilo - sam.get(compr[queries1[i].y]-1);
}
}
for(int i=1; i<=n; i++){
query tq;
tq.x = niz[i].first;
tq.y = niz[i].second;
tq.z = niz[i].first + niz[i].second;
tq.ind = 0;
queries2[++q2] = tq;
}
sort(queries2+1, queries2+1+q2, cmp2);
BIT prvo, drugo;
bilo = 0;
prvo.n = drugo.n = 1000000;
for(int i=q2; i>=1; i--){
if(queries2[i].ind == 0){
bilo++;
prvo.add(compr[queries2[i].x], 1);
drugo.add(compr[queries2[i].y], 1);
}
else{
res[queries2[i].ind] += bilo - prvo.get(compr[queries2[i].x]-1) - drugo.get(compr[queries2[i].y]-1);
}
}
for(int i=1; i<=q; i++) cout << res[i] << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
12 ms |
1900 KB |
Output is correct |
8 |
Correct |
13 ms |
1900 KB |
Output is correct |
9 |
Correct |
15 ms |
1900 KB |
Output is correct |
10 |
Correct |
10 ms |
1516 KB |
Output is correct |
11 |
Correct |
9 ms |
1516 KB |
Output is correct |
12 |
Correct |
4 ms |
932 KB |
Output is correct |
13 |
Correct |
11 ms |
1772 KB |
Output is correct |
14 |
Correct |
10 ms |
1772 KB |
Output is correct |
15 |
Correct |
11 ms |
1772 KB |
Output is correct |
16 |
Correct |
7 ms |
1132 KB |
Output is correct |
17 |
Correct |
8 ms |
1408 KB |
Output is correct |
18 |
Correct |
4 ms |
876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
382 ms |
18140 KB |
Output is correct |
2 |
Correct |
406 ms |
19460 KB |
Output is correct |
3 |
Correct |
374 ms |
19292 KB |
Output is correct |
4 |
Correct |
215 ms |
15912 KB |
Output is correct |
5 |
Correct |
287 ms |
16220 KB |
Output is correct |
6 |
Correct |
119 ms |
10460 KB |
Output is correct |
7 |
Correct |
388 ms |
19036 KB |
Output is correct |
8 |
Correct |
365 ms |
19164 KB |
Output is correct |
9 |
Correct |
347 ms |
18780 KB |
Output is correct |
10 |
Correct |
272 ms |
15580 KB |
Output is correct |
11 |
Correct |
218 ms |
15324 KB |
Output is correct |
12 |
Correct |
101 ms |
10076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
382 ms |
18140 KB |
Output is correct |
2 |
Correct |
406 ms |
19460 KB |
Output is correct |
3 |
Correct |
374 ms |
19292 KB |
Output is correct |
4 |
Correct |
215 ms |
15912 KB |
Output is correct |
5 |
Correct |
287 ms |
16220 KB |
Output is correct |
6 |
Correct |
119 ms |
10460 KB |
Output is correct |
7 |
Correct |
388 ms |
19036 KB |
Output is correct |
8 |
Correct |
365 ms |
19164 KB |
Output is correct |
9 |
Correct |
347 ms |
18780 KB |
Output is correct |
10 |
Correct |
272 ms |
15580 KB |
Output is correct |
11 |
Correct |
218 ms |
15324 KB |
Output is correct |
12 |
Correct |
101 ms |
10076 KB |
Output is correct |
13 |
Correct |
423 ms |
21084 KB |
Output is correct |
14 |
Correct |
450 ms |
19804 KB |
Output is correct |
15 |
Correct |
391 ms |
19312 KB |
Output is correct |
16 |
Correct |
268 ms |
16704 KB |
Output is correct |
17 |
Correct |
296 ms |
16732 KB |
Output is correct |
18 |
Correct |
124 ms |
10460 KB |
Output is correct |
19 |
Correct |
439 ms |
20956 KB |
Output is correct |
20 |
Correct |
404 ms |
20316 KB |
Output is correct |
21 |
Correct |
430 ms |
20956 KB |
Output is correct |
22 |
Correct |
273 ms |
15580 KB |
Output is correct |
23 |
Correct |
211 ms |
15384 KB |
Output is correct |
24 |
Correct |
100 ms |
10076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
12 ms |
1900 KB |
Output is correct |
8 |
Correct |
13 ms |
1900 KB |
Output is correct |
9 |
Correct |
15 ms |
1900 KB |
Output is correct |
10 |
Correct |
10 ms |
1516 KB |
Output is correct |
11 |
Correct |
9 ms |
1516 KB |
Output is correct |
12 |
Correct |
4 ms |
932 KB |
Output is correct |
13 |
Correct |
11 ms |
1772 KB |
Output is correct |
14 |
Correct |
10 ms |
1772 KB |
Output is correct |
15 |
Correct |
11 ms |
1772 KB |
Output is correct |
16 |
Correct |
7 ms |
1132 KB |
Output is correct |
17 |
Correct |
8 ms |
1408 KB |
Output is correct |
18 |
Correct |
4 ms |
876 KB |
Output is correct |
19 |
Correct |
382 ms |
18140 KB |
Output is correct |
20 |
Correct |
406 ms |
19460 KB |
Output is correct |
21 |
Correct |
374 ms |
19292 KB |
Output is correct |
22 |
Correct |
215 ms |
15912 KB |
Output is correct |
23 |
Correct |
287 ms |
16220 KB |
Output is correct |
24 |
Correct |
119 ms |
10460 KB |
Output is correct |
25 |
Correct |
388 ms |
19036 KB |
Output is correct |
26 |
Correct |
365 ms |
19164 KB |
Output is correct |
27 |
Correct |
347 ms |
18780 KB |
Output is correct |
28 |
Correct |
272 ms |
15580 KB |
Output is correct |
29 |
Correct |
218 ms |
15324 KB |
Output is correct |
30 |
Correct |
101 ms |
10076 KB |
Output is correct |
31 |
Correct |
423 ms |
21084 KB |
Output is correct |
32 |
Correct |
450 ms |
19804 KB |
Output is correct |
33 |
Correct |
391 ms |
19312 KB |
Output is correct |
34 |
Correct |
268 ms |
16704 KB |
Output is correct |
35 |
Correct |
296 ms |
16732 KB |
Output is correct |
36 |
Correct |
124 ms |
10460 KB |
Output is correct |
37 |
Correct |
439 ms |
20956 KB |
Output is correct |
38 |
Correct |
404 ms |
20316 KB |
Output is correct |
39 |
Correct |
430 ms |
20956 KB |
Output is correct |
40 |
Correct |
273 ms |
15580 KB |
Output is correct |
41 |
Correct |
211 ms |
15384 KB |
Output is correct |
42 |
Correct |
100 ms |
10076 KB |
Output is correct |
43 |
Correct |
851 ms |
47016 KB |
Output is correct |
44 |
Correct |
836 ms |
46920 KB |
Output is correct |
45 |
Correct |
800 ms |
47580 KB |
Output is correct |
46 |
Correct |
400 ms |
32476 KB |
Output is correct |
47 |
Correct |
502 ms |
34268 KB |
Output is correct |
48 |
Correct |
127 ms |
10460 KB |
Output is correct |
49 |
Correct |
824 ms |
47532 KB |
Output is correct |
50 |
Correct |
850 ms |
46952 KB |
Output is correct |
51 |
Correct |
820 ms |
47708 KB |
Output is correct |
52 |
Correct |
471 ms |
28948 KB |
Output is correct |
53 |
Correct |
246 ms |
21852 KB |
Output is correct |