#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
using namespace std;
struct pt{int x,y;}pos[200005];
int cmp1(pt a, pt b){return a.x<b.x;}
int cmp2(pt a, pt b){return a.y<b.y;}
struct x{int key, sub, type;};
bool operator<(x a, x b){return a.key == b.key ? (a.type > b.type) : (a.key < b.key);}
int a[200005],n;
x heap[270000];
int lim, tree[270000];
void add(int x){
while (x <= lim) {
tree[x]++;
x += x & -x;
}
}
int sum(int x){
int res = 0;
while (x) {
res += tree[x];
x -= x & -x;
}
return res;
}
long long f(int s, int e){
long long res = 0;
if(e - s < 400){
for (int i=s; i<=e; i++) {
int upper=1e9;
for (int j=i+1; j<=e; j++) {
if(a[i] < a[j] && a[j] < upper){
upper = a[j];
res++;
}
}
}
return res;
}
int m = (s+e)/2;
res = f(s,m) + f(m+1,e);
memset(tree,0,sizeof(tree));
set<int> s_left, s_right;
set<int> ::iterator it;
int lim, piv = 0;
for (int i=m; i>=s; i--) {
it = s_left.upper_bound(a[i]);
if(it == s_left.end()) lim = n+1;
else lim = *it - 1;
s_left.insert(a[i]);
heap[piv] = {a[i],lim,0};
piv++;
}
for (int i=m+1; i<=e; i++) {
it = s_right.upper_bound(a[i]);
if(it == s_right.begin()) lim = -1;
else lim = *--it;
heap[piv] = {lim,a[i],1};
piv++;
s_right.insert(a[i]);
}
sort(heap,heap+piv);
for (int i=0; i<piv; i++) {
x c = heap[i];
if(c.type) add(c.sub);
else res += sum(c.sub) - sum(c.key - 1);
}
return res;
}
int main(){
scanf("%d",&n);
for (int i=0; i<n; i++) {
scanf("%d %d",&pos[i].x,&pos[i].y);
}
sort(pos,pos+n,cmp2);
for (int i=0; i<n; i++) {
pos[i].y = i+1;
}
sort(pos,pos+n,cmp1);
for (int i=0; i<n; i++) {
a[i] = pos[i].y;
}
for(lim = 1; lim <= n+1; lim <<= 1);
long long r = f(0,n-1);
printf("%lld",r);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
7776 KB |
Output is correct |
2 |
Correct |
0 ms |
7776 KB |
Output is correct |
3 |
Correct |
0 ms |
7776 KB |
Output is correct |
4 |
Correct |
0 ms |
7776 KB |
Output is correct |
5 |
Correct |
0 ms |
7776 KB |
Output is correct |
6 |
Correct |
0 ms |
7776 KB |
Output is correct |
7 |
Correct |
0 ms |
7776 KB |
Output is correct |
8 |
Correct |
0 ms |
7776 KB |
Output is correct |
9 |
Correct |
0 ms |
7776 KB |
Output is correct |
10 |
Correct |
0 ms |
7776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
7908 KB |
Output is correct |
2 |
Correct |
12 ms |
7908 KB |
Output is correct |
3 |
Correct |
12 ms |
7908 KB |
Output is correct |
4 |
Correct |
8 ms |
7908 KB |
Output is correct |
5 |
Correct |
8 ms |
7908 KB |
Output is correct |
6 |
Correct |
16 ms |
7908 KB |
Output is correct |
7 |
Correct |
16 ms |
7908 KB |
Output is correct |
8 |
Correct |
8 ms |
7908 KB |
Output is correct |
9 |
Correct |
12 ms |
7908 KB |
Output is correct |
10 |
Correct |
12 ms |
7908 KB |
Output is correct |
11 |
Correct |
16 ms |
7908 KB |
Output is correct |
12 |
Correct |
8 ms |
7908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
832 ms |
17148 KB |
Output is correct |
2 |
Correct |
1296 ms |
17148 KB |
Output is correct |
3 |
Correct |
976 ms |
17148 KB |
Output is correct |
4 |
Correct |
760 ms |
17148 KB |
Output is correct |
5 |
Correct |
1004 ms |
17148 KB |
Output is correct |
6 |
Correct |
1124 ms |
17148 KB |
Output is correct |
7 |
Correct |
1248 ms |
17148 KB |
Output is correct |
8 |
Correct |
1280 ms |
17148 KB |
Output is correct |
9 |
Correct |
840 ms |
17148 KB |
Output is correct |
10 |
Correct |
932 ms |
17148 KB |
Output is correct |
11 |
Correct |
1152 ms |
17148 KB |
Output is correct |
12 |
Correct |
1252 ms |
17148 KB |
Output is correct |
13 |
Correct |
1296 ms |
17148 KB |
Output is correct |
14 |
Correct |
728 ms |
17148 KB |
Output is correct |
15 |
Correct |
1148 ms |
17148 KB |
Output is correct |
16 |
Correct |
1296 ms |
17148 KB |
Output is correct |
17 |
Correct |
704 ms |
13848 KB |
Output is correct |
18 |
Correct |
1252 ms |
17148 KB |
Output is correct |
19 |
Correct |
1220 ms |
17148 KB |
Output is correct |
20 |
Correct |
1200 ms |
17148 KB |
Output is correct |