#include <bits/stdc++.h>
#define MAXN 202020
using namespace std;
typedef pair<int,int> pi;
typedef tuple<int,int,int> ti;
typedef pair<vector<int>,vector<int> > pvi;
typedef long long int ll;
int N;
pi arr[MAXN];
ll ans;
vector<ti> dnc(int s,int e){
vector<ti> ret;
if(s==e){
ret.push_back(ti(arr[s].first,arr[s].second,-1));
return ret;
}
int m = (s+e)>>1;
vector<ti> A = dnc(s,m);
vector<ti> B = dnc(m+1,e);
vector<pi> vect;
int a = 0,b = 0;
while(a < A.size()&&b < B.size()){
if(get<1>(A[a]) < get<1>(B[b])){
while(!vect.empty()&&vect.back().second < get<0>(A[a])) vect.pop_back();
vect.push_back(pi(get<1>(A[a]),get<0>(A[a])));
ret.push_back(A[a]);
a++;
}
else{
int p1 = lower_bound(vect.begin(),vect.end(),pi(get<2>(B[b]),-1)) - vect.begin();
ans += vect.size() - p1;
auto ptr = vect.end();
int temp = get<2>(B[b]);
if(!vect.empty()){
ptr--;
temp = max(get<2>(B[b]),ptr->first);
}
B[b] = ti(get<0>(B[b]),get<1>(B[b]),temp);
ret.push_back(B[b]);
b++;
}
}
while(a < A.size()){
while(!vect.empty()&&vect.back().first < get<0>(A[a])) vect.pop_back();
vect.push_back(pi(get<0>(A[a]),get<1>(A[a])));
ret.push_back(A[a]);
a++;
}
while(b < B.size()){
int p1 = lower_bound(vect.begin(),vect.end(),pi(get<2>(B[b]),-1)) - vect.begin();
ans += vect.size() - p1;
auto ptr = vect.end();
int temp = get<2>(B[b]);
if(!vect.empty()){
ptr--;
temp = max(get<2>(B[b]),ptr->first);
}
B[b] = ti(get<0>(B[b]),get<1>(B[b]),temp);
ret.push_back(B[b]);
b++;
}
return ret;
}
int main(){
scanf("%d",&N);
for(int i=0;i<N;i++){
scanf("%d %d",&arr[i].first,&arr[i].second);
}
sort(arr,arr+N);
dnc(0,N-1);
printf("%lld\n",ans);
return 0;
}
Compilation message
scarecrows.cpp: In function 'std::vector<std::tuple<int, int, int> > dnc(int, int)':
scarecrows.cpp:23:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(a < A.size()&&b < B.size()){
~~^~~~~~~~~~
scarecrows.cpp:23:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(a < A.size()&&b < B.size()){
~~^~~~~~~~~~
scarecrows.cpp:44:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(a < A.size()){
~~^~~~~~~~~~
scarecrows.cpp:50:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(b < B.size()){
~~^~~~~~~~~~
scarecrows.cpp: In function 'int main()':
scarecrows.cpp:66:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&N);
~~~~~^~~~~~~~~
scarecrows.cpp:68:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&arr[i].first,&arr[i].second);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
668 KB |
Output is correct |
2 |
Correct |
7 ms |
640 KB |
Output is correct |
3 |
Correct |
6 ms |
768 KB |
Output is correct |
4 |
Correct |
6 ms |
640 KB |
Output is correct |
5 |
Correct |
6 ms |
640 KB |
Output is correct |
6 |
Correct |
7 ms |
768 KB |
Output is correct |
7 |
Correct |
7 ms |
640 KB |
Output is correct |
8 |
Correct |
6 ms |
640 KB |
Output is correct |
9 |
Correct |
7 ms |
640 KB |
Output is correct |
10 |
Correct |
7 ms |
628 KB |
Output is correct |
11 |
Correct |
7 ms |
640 KB |
Output is correct |
12 |
Correct |
5 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
173 ms |
8836 KB |
Output is correct |
2 |
Correct |
242 ms |
8228 KB |
Output is correct |
3 |
Correct |
212 ms |
9612 KB |
Output is correct |
4 |
Correct |
176 ms |
8188 KB |
Output is correct |
5 |
Correct |
196 ms |
8220 KB |
Output is correct |
6 |
Correct |
204 ms |
8104 KB |
Output is correct |
7 |
Correct |
218 ms |
8256 KB |
Output is correct |
8 |
Correct |
238 ms |
8256 KB |
Output is correct |
9 |
Correct |
194 ms |
9072 KB |
Output is correct |
10 |
Correct |
210 ms |
9576 KB |
Output is correct |
11 |
Correct |
216 ms |
8324 KB |
Output is correct |
12 |
Correct |
234 ms |
8256 KB |
Output is correct |
13 |
Correct |
234 ms |
8180 KB |
Output is correct |
14 |
Correct |
198 ms |
9064 KB |
Output is correct |
15 |
Correct |
225 ms |
8128 KB |
Output is correct |
16 |
Correct |
216 ms |
8080 KB |
Output is correct |
17 |
Correct |
135 ms |
5260 KB |
Output is correct |
18 |
Correct |
222 ms |
8236 KB |
Output is correct |
19 |
Correct |
218 ms |
8124 KB |
Output is correct |
20 |
Correct |
221 ms |
8168 KB |
Output is correct |