#include"bits/stdc++.h"
using namespace std;
#define LL long long
#define MP(a,b) make_pair(a,b)
//RSQ セグメント木
struct SegmentTree{
private:
int n;
vector<long long> nodes;
public:
void init(int N){ //初期化する O(N)
nodes.clear();
n = 1;
while(n < N) n *= 2;
n = 2 * n -1;
for(int i=0; i<n; i++) nodes.push_back(0);
}
void add(int i, long long x){ //値を変更する O(log N)
i += n / 2;
nodes[i] += x;
while(i > 0){
i = (i-1)/2; //親の取得は(i-1)/2
nodes[i] = nodes[i*2+1] + nodes[i*2+2]; //子の取得はi*2+1,i*2+2
}
}
long long sum(int a, int b, int k=0, int l=0, int r=-1){ //[a,b)の最小値を求める O(log N)
if(r == -1) r = n/2+1;
if(r <= a || b <= l) return 0; //交差する場合
if(a <= l && r <= b) return nodes[k]; //完全に含む場合
long long valueL = sum(a, b, k*2+1, l, (l+r)/2);
long long valueR = sum(a, b, k*2+2, (l+r)/2, r);
return valueL + valueR;
}
};
int N,Q;
vector<pair<int,int> > v,q;
vector<int> zaatuB;
vector<pair<pair<int,int>, int> > all;
multiset<pair<int,int> > s;
SegmentTree st;
int ans[200000];
int main(){
cin >> N >> Q;
for(int i=0; i<N; i++){
int A,B;
cin >> A >> B;
v.push_back(MP(A,B));
zaatuB.push_back(B);
}
for(int i=0; i<Q; i++){
int a,b;
cin >> a >> b;
q.push_back(MP(a,b));
zaatuB.push_back(b);
}
sort(zaatuB.begin(), zaatuB.end());
for(int i=0; i<N; i++){
all.push_back(MP(MP(v[i].first, lower_bound(zaatuB.begin(), zaatuB.end(), v[i].second)-zaatuB.begin()), Q));
swap(all.back().first.second, all.back().second);
}
for(int i=0; i<Q; i++){
all.push_back(MP(MP(q[i].first, lower_bound(zaatuB.begin(), zaatuB.end(), q[i].second)-zaatuB.begin()), i));
swap(all.back().first.second, all.back().second);
}
sort(all.begin(), all.end());
reverse(all.begin(), all.end());
for(int i=0; i<all.size(); i++){
swap(all[i].first.second, all[i].second);
}
st.init(zaatuB.size());
multiset<pair<int,int> > s2;
for(int i=0; i<all.size(); i++){
if(i != 0 && all[i-1].first.first != all[i].first.first){
for(auto itr=s2.begin(); itr!=s2.end(); itr++) s.insert(*itr);
s2.clear();
}
//cout << all[i].first.first << " " << all[i].first.second << " " << all[i].second << endl;
if(all[i].second == Q){
auto itr = s.lower_bound(MP(all[i].first.second+1,-1));
while(s.size() != 0 && itr != s.end()){
if(itr->first > all[i].first.second/* && itr->second > all[i].first.first*/){
st.add(itr->first, -1);
s.erase(itr);
break;
}
itr++;
}
s2.insert(MP(all[i].first.second, all[i].first.first));
st.add(all[i].first.second, 1);
}
else{
ans[all[i].second] = st.sum(0, all[i].first.second+1);
}
}
for(int i=0; i<Q; i++) cout << ans[i] << endl;
return 0;
}
Compilation message
matryoshka.cpp: In function 'int main()':
matryoshka.cpp:70:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<all.size(); i++){
~^~~~~~~~~~~
matryoshka.cpp:76:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<all.size(); i++){
~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
3 ms |
256 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
3 ms |
256 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
3 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
256 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
256 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
3 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
256 KB |
Output is correct |
20 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
3 ms |
256 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
3 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
256 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
256 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
3 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
256 KB |
Output is correct |
20 |
Correct |
2 ms |
384 KB |
Output is correct |
21 |
Correct |
3 ms |
384 KB |
Output is correct |
22 |
Correct |
7 ms |
512 KB |
Output is correct |
23 |
Correct |
10 ms |
640 KB |
Output is correct |
24 |
Correct |
12 ms |
640 KB |
Output is correct |
25 |
Correct |
13 ms |
640 KB |
Output is correct |
26 |
Correct |
12 ms |
640 KB |
Output is correct |
27 |
Correct |
12 ms |
640 KB |
Output is correct |
28 |
Correct |
11 ms |
640 KB |
Output is correct |
29 |
Correct |
12 ms |
640 KB |
Output is correct |
30 |
Correct |
11 ms |
640 KB |
Output is correct |
31 |
Correct |
12 ms |
716 KB |
Output is correct |
32 |
Correct |
12 ms |
640 KB |
Output is correct |
33 |
Correct |
11 ms |
640 KB |
Output is correct |
34 |
Correct |
15 ms |
640 KB |
Output is correct |
35 |
Correct |
11 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
3 ms |
256 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
3 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
256 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
256 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
3 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
256 KB |
Output is correct |
20 |
Correct |
2 ms |
384 KB |
Output is correct |
21 |
Correct |
3 ms |
384 KB |
Output is correct |
22 |
Correct |
7 ms |
512 KB |
Output is correct |
23 |
Correct |
10 ms |
640 KB |
Output is correct |
24 |
Correct |
12 ms |
640 KB |
Output is correct |
25 |
Correct |
13 ms |
640 KB |
Output is correct |
26 |
Correct |
12 ms |
640 KB |
Output is correct |
27 |
Correct |
12 ms |
640 KB |
Output is correct |
28 |
Correct |
11 ms |
640 KB |
Output is correct |
29 |
Correct |
12 ms |
640 KB |
Output is correct |
30 |
Correct |
11 ms |
640 KB |
Output is correct |
31 |
Correct |
12 ms |
716 KB |
Output is correct |
32 |
Correct |
12 ms |
640 KB |
Output is correct |
33 |
Correct |
11 ms |
640 KB |
Output is correct |
34 |
Correct |
15 ms |
640 KB |
Output is correct |
35 |
Correct |
11 ms |
640 KB |
Output is correct |
36 |
Correct |
1110 ms |
28888 KB |
Output is correct |
37 |
Correct |
793 ms |
28108 KB |
Output is correct |
38 |
Correct |
355 ms |
12792 KB |
Output is correct |
39 |
Correct |
825 ms |
26684 KB |
Output is correct |
40 |
Correct |
914 ms |
27144 KB |
Output is correct |
41 |
Correct |
1022 ms |
28204 KB |
Output is correct |
42 |
Correct |
843 ms |
25804 KB |
Output is correct |
43 |
Correct |
710 ms |
31344 KB |
Output is correct |
44 |
Correct |
1232 ms |
31476 KB |
Output is correct |
45 |
Correct |
1205 ms |
31540 KB |
Output is correct |
46 |
Correct |
1154 ms |
31388 KB |
Output is correct |
47 |
Correct |
1122 ms |
31352 KB |
Output is correct |
48 |
Correct |
1185 ms |
31664 KB |
Output is correct |
49 |
Correct |
992 ms |
31112 KB |
Output is correct |
50 |
Correct |
985 ms |
31516 KB |
Output is correct |