此题就是1227 的弱化版。
画个图或者稍微证明一下就能够知道,一定不会超过一次变换。
那么我们只需要统计有多少个白点会变黑,换句话说就是有多少个白点上下左右都有黑点。
离散化横坐标,因为没有黑点在的列是没有任何意义的,对答案也没有贡献。
然后处理每一行,对于每一行,维护一个BIT也就是哪些点会产生贡献,这个BIT最多只会有n次修改,n次查询。
所以时间复杂度为O(nlogn).
# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <set>
# include <cmath>
# include <algorithm>
using namespace std;
# define lowbit(x) ((x)&(-x))
# define pi 3.1415926535
# define eps 1e-
# define MOD
# define INF
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define bug puts("H");
# define lch p<<,l,mid
# define rch p<<|,mid+,r
# define mp make_pair
# define pb push_back
typedef pair<int,int> PII;
typedef vector<int> VI;
# pragma comment(linker, "/STACK:1024000000,1024000000")
typedef long long LL;
int Scan() {
int res=, flag=;
char ch;
if((ch=getchar())=='-') flag=;
else if(ch>=''&&ch<='') res=ch-'';
while((ch=getchar())>=''&&ch<='') res=res*+(ch-'');
return flag?-res:res;
}
void Out(int a) {
if(a<) {putchar('-'); a=-a;}
if(a>=) Out(a/);
putchar(a%+'');
}
const int N=;
//Code begin... struct Node{int x, y;}node[N];
VI v, vv;
int col[N], vis[N], tree[N], n; bool comp(Node a, Node b){
if (a.y==b.y) return a.x<b.x;
return a.y>b.y;
}
void add(int x, int val){while (x<=n) tree[x]+=val, x+=lowbit(x);}
int query(int x){
int res=;
while (x) res+=tree[x], x-=lowbit(x);
return res;
}
int main ()
{
LL ans=;
scanf("%d",&n);
FOR(i,,n) scanf("%d%d",&node[i].x,&node[i].y), v.pb(node[i].x);
sort(v.begin(),v.end());
int siz=unique(v.begin(),v.end())-v.begin();
sort(node+,node+n+,comp);
FOR(i,,n) {
node[i].x=lower_bound(v.begin(),v.begin()+siz,node[i].x)-v.begin()+;
++col[node[i].x];
}
int now=;
while (now<=n) {
vv.clear(); vv.pb(node[now].x);
++now;
while (now<=n&&node[now].y==node[now-].y) vv.pb(node[now].x), ++now;
siz=vv.size();
FO(i,,siz) ans+=(query(vv[i]-)-query(vv[i-]));
FO(i,,siz) {
++vis[vv[i]];
if (vis[vv[i]]==&&col[vv[i]]>) add(vv[i],);
else if (vis[vv[i]]==col[vv[i]]&&col[vv[i]]>) add(vv[i],-);
}
}
printf("%lld\n",ans+n);
return ;
}