题意:选任意底长的框把若干点框起来,有多少种方法。
思路:
看到这种X-Y还带有计数的,想到cdq分治(二维偏序)。
每次我就看 x j < = x i x_j <= x_i xj<=xi&& y j > = y i y_j>=y_i yj>=yi的点有多少,分别统计这个点左边有多少个、右边有多少个。然后左右的数量相乘就是以当前i为底的贡献。
考虑到有y相同的,这里会产生重复,就需要统一特判一下所有y相同的,相邻两个间独立计数。
AC代码:
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define endl '\n'
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 5e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0', ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };
typedef struct Pos
{
ll x;
ll y;
bool operator < (const Pos &a) const{
if(y!=a.y) return y > a.y;
return x < a.x;
}
}P;
P a[maxn];
ll sum1[maxn];
ll sum2[maxn];
ll up = 1;
inline void add(ll pos, ll y)
{
for(ll i=pos; i<=up; i += lowbit(i)) sum1[i] += y, sum2[i] += pos*y;
}
void add_range(ll l, ll r, ll x)
{
add(l,x),add(r+1,-x);
}
ll ask(ll p)
{
ll ans = 0;
for(ll i=p;i>=1;i -= lowbit(i)) ans += ((p+1)*sum1[i] - sum2[i]);
return ans;
}
ll L[maxn];
ll vis[maxn];
ll vis1[maxn];
int main()
{
int kase;
kase = 1;
while(kase--)
{
ll n = read();
rep(i,1,n)
{
a[i].x = read(), a[i].y = read();
L[i] = a[i].x;
}
sort(a+1,a+1+n);
sort(L+1,L+1+n);
rep(i,1,n) a[i].x = (lower_bound(L+1,L+1+n,a[i].x) - L) + 1, up = max(up,a[i].x);
ll ans = 0;
rep(i,1,n)
{
int p = i;
int len = 0;
while(p<=n&&a[p].y==a[i].y) p++;
rep(j,i,p-1)
{
ll l = ask(a[j].x-1);
ll r = ask(j==p-1?up:a[j+1].x-1) - ask(a[j].x);
ans += (l+1)*(r+1);
if(!vis[a[j].x])
add_range(a[j].x,a[j].x,1), vis[a[j].x] = 1;
}
i = p-1;
}
cout<<ans<<endl;
}
return 0;
}