题解 CF1140F 【Extending Set of Points】

· · 题解

题解:CF1140F

吐槽:

为啥同机房的奆佬的题都只有2700,然后我2600。。。

万里长征刚刚开始,这是第一篇。

题意:

维护一个平面上点的集合,支持插入,删除,查询扩展集合的大小。

扩展集合的定义:反复进行如下操作,直到不能加入点为止。

将符合以下条件的(x2,y2)加入S(条件:存在x1,y1使得(x1,y1),(x1,y2),(x2,y1)属于S)。

思路:

我们先考虑给出一个点集,如何计算它拓展集合的大小。

上面是最初的集合。

红色是第一次加入的点。

绿色是第二次加入的点。

橙色是第三次加入的点。

蓝色是第四次加入的点。

我们发现其实它拓展成了一个矩形。

我们再画一些图。

观察到也是拓展成一个矩阵。

而且所有的点横竖连边的话图就会联通。

也就是说,一个点会和它同横坐标竖坐标的点联通,而观察上述二例,其集合大小就是一个连通块中不同横坐标点的个数乘上不同纵坐标点的个数,于是我们用并查集维护连通块,对于横纵坐标分别维护集合大小,答案就是其乘积之和。加入一个点就相当于横坐标和纵坐标连边,然后,没了?

停停停,还有删除来着,嗯嗯并查集似乎不支持删除吧,但并查集看上去是那么的可行诶。

于是,线段树分治登场,它能将带删除操作的题目以一个log的代价转化为不带删除的题目。

线段树分治:

我们考虑一个点,它加入了又删除,很容易用map求出它覆盖的时间段,当然,这需要一个点删除后对答案的影响消失。

很容易想到开一颗维护时间的线段树,将一个操作覆盖的时间段上的节点打上这个操作,查询时对线段树进行dfs就可以在叶子节点得到答案了。

别忙,回溯怎么办,不还是要删点吗?嗯,好好想想,它只需要撤销前一个操作啊,可撤销并查集啊。

于是我们快乐地搞定了这道题。

代码和实现中的细节问题:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#include<string>
#include<cstring>
#include<ctime>
#include<stack>
#include<vector>
#define next nxt
#define itn int
#define y1 y3647
#define LL long long
using namespace std;
inline void read(int &x)
{
    x=0;
    int f=1;
    char ch=getchar();
    while((ch<'0'||ch>'9')&&ch!='-')
    ch=getchar();
    if(ch=='-')
    {
        f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    x*=f;
}
const int N=3e5+10;
int i,j,k,m,n,s,t,top,tc;
map <pair<int,int>,int> mp;
map <pair<int,int>,int> ::iterator it;
vector <pair<int,int> > a[N<<2];
struct bc{
    int v,fv,u,s1u,s2u;
}sta[N<<6];
int fa[N<<1],size1[N<<1],size2[N<<1],root[N<<1];
int find(int x)
{
    if(fa[x]==x)
    return x;
    return find(fa[x]);
}
void updata(int l,int r,int rt,int x,int y,pair<int,int> op)
{
    if(x<=l&&y>=r)
    {
        a[rt].push_back(op);//vector真的很香
        return;
    }
    int mid=(l+r)/2;
    if(x<=mid)
    updata(l,mid,rt<<1,x,y,op);
    if(y>mid)
    updata(mid+1,r,rt<<1|1,x,y,op);
}
long long put(int u,int v,long long c)
{
    u=find(u),v=find(v);
    if(u==v)
    return c;
    tc++;
    if(size1[u]+size2[u]>size1[v]+size2[v])
    swap(u,v);
    sta[++top]=(bc){u,fa[u],v,size1[v],size2[v]};//改变的有以下:u的fa,v的size1,size2。
    fa[u]=v;
    c-=(long long)size1[v]*size2[v];
    c-=(long long)size1[u]*size2[u];
    size1[v]+=size1[u];
    size2[v]+=size2[u];
    c+=(long long)size1[v]*size2[v];//第一发WA没开longlong
    return c;
}
void dfs(int l,int r,int rt,long long ans)
{
    tc=0;
    for(int i=0;i<a[rt].size();i++)
    ans=put(a[rt][i].first,a[rt][i].second,ans);
    int cc=tc;
    if(l==r)
    {
        printf("%lld ",ans);
        for(int i=0;i<cc;i++)
        {
            fa[sta[top].v]=sta[top].fv;
            size1[sta[top].u]=sta[top].s1u;
            size2[sta[top].u]=sta[top].s2u;
            top--;
        }
        return;
    }
    int mid=(l+r)/2;
    dfs(l,mid,rt<<1,ans);
    dfs(mid+1,r,rt<<1|1,ans);
    for(int i=0;i<cc;i++)
    {
        fa[sta[top].v]=sta[top].fv;
        size1[sta[top].u]=sta[top].s1u;
        size2[sta[top].u]=sta[top].s2u;
        top--;
    }
}
int main()
{
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
    //freopen(".ans","w",stdout);
    read(n);
    for(i=1;i<=n;i++)
    {
        int x,y;
        read(x),read(y);
        int tmp=mp[make_pair(x,y)];
        if(tmp)
        {
            updata(1,n,1,tmp,i-1,make_pair(x,y+3e5));//注意是i-1因为在i处已经删去了这个点
            mp.erase(make_pair(x,y));
        }
        else
        mp[make_pair(x,y)]=i;
    }
    for(it=mp.begin();it!=mp.end();it++)
    updata(1,n,1,(*it).second,n,make_pair(it->first.first,it->first.second+3e5));//在最后的时间把所有操作加入。
    for(i=1;i<=3e5;i++)
    {
        fa[i]=i;
        size1[i]=1;
    }
    for(i=3e5+1;i<=6e5;i++)
    {
        fa[i]=i;
        size2[i]=1;
    }
    dfs(1,n,1,0);
    return 0;
}

各位看官看到这里就可以了,下面是给自己看的。

某大牛代码:

px[top]=x; py[top]=y; fa[y]=x;
sz1[x]+=sz1[y]; sz2[x]+=sz2[y];
------------------------------
int x=px[top],y=py[top];
fa[y]=y;
sz1[x]-=sz1[y];
sz2[x]-=sz2[y];

我自己的对应部分:

sta[++top]=(bc){u,fa[u],v,size1[v],size2[v]};
--------------------------------------------
for(int i=0;i<cc;i++)
{
    fa[sta[top].v]=sta[top].fv;
    size1[sta[top].u]=sta[top].s1u;
    size2[sta[top].u]=sta[top].s2u;
    top--;
}

显然维护的信息有些冗余,这又是空间复杂度瓶颈,于是。。。

反思:

这种题要多画图想一想,其实最开始我就考虑了线段树分治,但是我似乎不会静态问题。

可撤销并查集实现还有些不熟练。