首页 > 代码库 > BZOJ 1818 内部白点(离散化+树状数组)

BZOJ 1818 内部白点(离散化+树状数组)

此题就是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-9
# define MOD 9999973
# define INF 1000000000
# 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<<1,l,mid
# define rch p<<1|1,mid+1,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=0, flag=0;
    char ch;
    if((ch=getchar())==-) flag=1;
    else if(ch>=0&&ch<=9) res=ch-0;
    while((ch=getchar())>=0&&ch<=9)  res=res*10+(ch-0);
    return flag?-res:res;
}
void Out(int a) {
    if(a<0) {putchar(-); a=-a;}
    if(a>=10) Out(a/10);
    putchar(a%10+0);
}
const int N=100005;
//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=0;
    while (x) res+=tree[x], x-=lowbit(x);
    return res;
}
int main ()
{
    LL ans=0;
    scanf("%d",&n);
    FOR(i,1,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+1,node+n+1,comp);
    FOR(i,1,n) {
        node[i].x=lower_bound(v.begin(),v.begin()+siz,node[i].x)-v.begin()+1;
        ++col[node[i].x];
    }
    int now=1;
    while (now<=n) {
        vv.clear(); vv.pb(node[now].x);
        ++now;
        while (now<=n&&node[now].y==node[now-1].y) vv.pb(node[now].x), ++now;
        siz=vv.size();
        FO(i,1,siz) ans+=(query(vv[i]-1)-query(vv[i-1]));
        FO(i,0,siz) {
            ++vis[vv[i]];
            if (vis[vv[i]]==1&&col[vv[i]]>1) add(vv[i],1);
            else if (vis[vv[i]]==col[vv[i]]&&col[vv[i]]>1) add(vv[i],-1);
        }
    }
    printf("%lld\n",ans+n);
    return 0;
}
View Code

 

BZOJ 1818 内部白点(离散化+树状数组)