#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct Query{
int x, y, idx;
Query(){}
Query(int x, int y, int idx): x(x), y(y), idx(idx){}
bool operator<(const Query &r)const{
return y!=r.y ? y<r.y : x<r.x;
}
};
struct Fenwick {
int tree[1000002];
int n = 1000000;
void add(int x, int v){
while(x<=n){
tree[x] += v;
x += x&-x;
}
}
int sum(int x){
int ret=0;
while(x){
ret += tree[x];
x -= x&-x;
}
return ret;
}
} tree;
int n, q;
pair<int, int> arr[200002];
Query query[200002];
set<pair<int, int> > st;
int ans[200002];
void renumber(){
vector<int> vec;
for(int i=1; i<=n; i++) vec.push_back(arr[i].first);
for(int i=1; i<=q; i++) vec.push_back(query[i].x);
sort(vec.begin(), vec.end());
vec.erase(unique(vec.begin(), vec.end()), vec.end());
for(int i=1; i<=n; i++) arr[i].first = lower_bound(vec.begin(), vec.end(), arr[i].first) - vec.begin() + 1;
for(int i=1; i<=q; i++) query[i].x = lower_bound(vec.begin(), vec.end(), query[i].x) - vec.begin() + 1;
}
int main(){
scanf("%d %d", &n, &q);
for(int i=1; i<=n; i++){
scanf("%d %d", &arr[i].first, &arr[i].second);
}
for(int i=1; i<=q; i++){
scanf("%d %d", &query[i].x, &query[i].y);
query[i].idx = i;
}
renumber();
sort(arr+1, arr+n+1, [&](pair<int, int> x, pair<int, int> y){
return x.second != y.second ? x.second < y.second : x.first > y.first;
});
sort(query+1, query+q+1);
int pnt = 1;
for(int i=1; i<=q; i++){
while(pnt <= n && query[i].y >= arr[pnt].second){
tree.add(arr[pnt].first, 1);
auto it = st.lower_bound(make_pair(arr[pnt].first, -1));
if(it == st.begin()){
st.insert(make_pair(arr[pnt].first, pnt));
pnt++;
continue;
}
--it;
tree.add(it->first, -1);
st.insert(make_pair(arr[pnt].first, pnt));
st.erase(it);
++pnt;
}
ans[query[i].idx] = tree.sum(1000000) - tree.sum(query[i].x - 1);
}
for(int i=1; i<=q; i++) printf("%d\n", ans[i]);
}
Compilation message
matryoshka.cpp: In function 'int main()':
matryoshka.cpp:55:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
55 | scanf("%d %d", &n, &q);
| ~~~~~^~~~~~~~~~~~~~~~~
matryoshka.cpp:57:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
57 | scanf("%d %d", &arr[i].first, &arr[i].second);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
matryoshka.cpp:60:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
60 | scanf("%d %d", &query[i].x, &query[i].y);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
312 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
316 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
312 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
316 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
316 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
312 KB |
Output is correct |
15 |
Correct |
0 ms |
312 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
308 KB |
Output is correct |
18 |
Correct |
0 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
312 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
316 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
316 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
312 KB |
Output is correct |
15 |
Correct |
0 ms |
312 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
308 KB |
Output is correct |
18 |
Correct |
0 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
308 KB |
Output is correct |
21 |
Correct |
0 ms |
312 KB |
Output is correct |
22 |
Correct |
2 ms |
332 KB |
Output is correct |
23 |
Correct |
2 ms |
460 KB |
Output is correct |
24 |
Correct |
3 ms |
460 KB |
Output is correct |
25 |
Correct |
2 ms |
448 KB |
Output is correct |
26 |
Correct |
3 ms |
460 KB |
Output is correct |
27 |
Correct |
3 ms |
460 KB |
Output is correct |
28 |
Correct |
3 ms |
460 KB |
Output is correct |
29 |
Correct |
2 ms |
576 KB |
Output is correct |
30 |
Correct |
2 ms |
460 KB |
Output is correct |
31 |
Correct |
3 ms |
456 KB |
Output is correct |
32 |
Correct |
3 ms |
444 KB |
Output is correct |
33 |
Correct |
3 ms |
460 KB |
Output is correct |
34 |
Correct |
3 ms |
460 KB |
Output is correct |
35 |
Correct |
2 ms |
448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
312 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
316 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
316 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
312 KB |
Output is correct |
15 |
Correct |
0 ms |
312 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
308 KB |
Output is correct |
18 |
Correct |
0 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
308 KB |
Output is correct |
21 |
Correct |
0 ms |
312 KB |
Output is correct |
22 |
Correct |
2 ms |
332 KB |
Output is correct |
23 |
Correct |
2 ms |
460 KB |
Output is correct |
24 |
Correct |
3 ms |
460 KB |
Output is correct |
25 |
Correct |
2 ms |
448 KB |
Output is correct |
26 |
Correct |
3 ms |
460 KB |
Output is correct |
27 |
Correct |
3 ms |
460 KB |
Output is correct |
28 |
Correct |
3 ms |
460 KB |
Output is correct |
29 |
Correct |
2 ms |
576 KB |
Output is correct |
30 |
Correct |
2 ms |
460 KB |
Output is correct |
31 |
Correct |
3 ms |
456 KB |
Output is correct |
32 |
Correct |
3 ms |
444 KB |
Output is correct |
33 |
Correct |
3 ms |
460 KB |
Output is correct |
34 |
Correct |
3 ms |
460 KB |
Output is correct |
35 |
Correct |
2 ms |
448 KB |
Output is correct |
36 |
Correct |
264 ms |
11828 KB |
Output is correct |
37 |
Correct |
246 ms |
11832 KB |
Output is correct |
38 |
Correct |
89 ms |
5168 KB |
Output is correct |
39 |
Correct |
207 ms |
9344 KB |
Output is correct |
40 |
Correct |
198 ms |
10312 KB |
Output is correct |
41 |
Correct |
263 ms |
11120 KB |
Output is correct |
42 |
Correct |
158 ms |
9628 KB |
Output is correct |
43 |
Correct |
157 ms |
17356 KB |
Output is correct |
44 |
Correct |
276 ms |
15124 KB |
Output is correct |
45 |
Correct |
285 ms |
15128 KB |
Output is correct |
46 |
Correct |
261 ms |
15132 KB |
Output is correct |
47 |
Correct |
273 ms |
14904 KB |
Output is correct |
48 |
Correct |
303 ms |
15616 KB |
Output is correct |
49 |
Correct |
257 ms |
14876 KB |
Output is correct |
50 |
Correct |
276 ms |
15576 KB |
Output is correct |