提议:有一张n个节点个m条边的无向图,每个点有权值,有若干操作: D X :删除ID为X的边,Q X K:计算与节点X联通的节点中,第K大的权值,不存在则当作0,C X V:把节点X的权值改为V。输出所有Q指令计算结果的平均值。
思路:白书名次树的例题,以前看到这题庞大的代码量就不敢学了,今天学了后发现比后缀数组容易理解多了,代码量大不一定就难懂,有个删除边的操作,因此直接用Treap不太好弄,所以可以把操作顺序反过来,首先建立好Treap,之前要删除边的,那么就不把这两个联通块连接就好了,之前需要改的点权值,就都改了,遍历倒序操作时,如果是删除边,就变成连接两个联通块,可用并查集连接,把容量小的Treap连接到容量大的Treap,省复杂度,如果是改权值,那就改回到之前的权值,然后就直接套用模板了。
#include<cstdio>
#include<cstring>
#include<vector>
#include<cstdlib>
using namespace std;
struct Node{
Node *ch[2];
int r;
int v;
int s;
Node(int v):v(v){ch[0]=ch[1]=NULL;r=rand();s=1;}
bool operator<(const Node& rhs)const{
return r<rhs.r;
}
int cmp(int x)const{
if(x==v)return -1;
return x<v?0:1;
}
void maintain()
{
s=1;
if(ch[0]!=NULL)s+=ch[0]->s;
if(ch[1]!=NULL)s+=ch[1]->s;
}
};
void rotate(Node* &o,int d)
{
Node* k=o->ch[d^1];o->ch[d^1]=k->ch[d];k->ch[d]=o;
o->maintain();k->maintain();o=k;
}
void insert(Node* &o,int x)
{
if(o==NULL)o=new Node(x);
else
{
int d=(x<o->v?0:1);
insert(o->ch[d],x);
if(o->ch[d]->r>o->r)rotate(o,d^1);
}
o->maintain();
}
void remove(Node* &o,int x)
{
int d=o->cmp(x);
if(d==-1)
{
Node* u=o;
if(o->ch[0]!=NULL&&o->ch[1]!=NULL)
{
int d2=(o->ch[0]>o->ch[1]?1:0);
rotate(o,d2);remove(o->ch[d2],x);
}else{
if(o->ch[0]==NULL)o=o->ch[1];else o=o->ch[0];
delete u;
}
}
else
remove(o->ch[d],x);
if(o!=NULL)
o->maintain();
}
/* Treap树模板 */
const int maxc=5e5+10;
struct Command{
char type;
int x,p;
}commands[maxc];
const int maxn=2e4+10;
const int maxm=6e4+10;
int n,m,weight[maxn],from[maxm],to[maxm],removed[maxm];
int pa[maxn];
int findset(int x){return pa[x]!=x?pa[x]=findset(pa[x]):x;}
Node* root[maxn];
int kth(Node* o,int k)
{
if(o==NULL||o->s<k||k<=0)return 0;
int s=(o->ch[1]==NULL?0:o->ch[1]->s);
if(k==s+1)return o->v;
else if(k<=s)return kth(o->ch[1],k);
else return kth(o->ch[0],k-s-1);
}
void mergeto(Node* &src,Node* &dest)
{
if(src->ch[0]!=NULL)mergeto(src->ch[0],dest);
if(src->ch[1]!=NULL)mergeto(src->ch[1],dest);
insert(dest,src->v);
delete src;
src=NULL;
}
void removetree(Node* &o)
{
if(o->ch[0]!=NULL)removetree(o->ch[0]);
if(o->ch[1]!=NULL)removetree(o->ch[1]);
delete o;
o=NULL;
}
void add_edge(int x)
{
int u=findset(from[x]),v=findset(to[x]);
if(u!=v)
{
if(root[u]->s<root[v]->s){pa[u]=v;mergeto(root[u],root[v]);}
else {pa[v]=u;mergeto(root[v],root[u]);};
}
}
int query_cnt;
long long query_tot;
void query(int x,int k)
{
query_cnt++;
query_tot+=kth(root[findset(x)],k);
}
void change_weight(int x,int v)
{
int u=findset(x);
remove(root[u],weight[x]);
insert(root[u],v);
weight[x]=v;
}
int main()
{
int kase=0;
while(~scanf("%d%d",&n,&m)&&n)
{
for(int i=1;i<=n;i++)scanf("%d",&weight[i]);
for(int i=1;i<=m;i++)scanf("%d%d",&from[i],&to[i]);
memset(removed,0,sizeof(removed));
int c=0;
while(1)
{
char type;
int x,p=0,v=0;
scanf(" %c",&type);
if(type=='E')break;
scanf("%d",&x);
if(type=='D')removed[x]=1;
if(type=='Q')scanf("%d",&p);
if(type=='C')
{
scanf("%d",&v);
p=weight[x];
weight[x]=v;
}
commands[c++]=(Command){type,x,p};
}
for(int i=1;i<=n;i++)
{
pa[i]=i;if(root[i]!=NULL)removetree(root[i]);
root[i]=new Node(weight[i]);
}
for(int i=1;i<=m;i++)
if(!removed[i])
add_edge(i);
query_cnt=query_tot=0;
for(int i=c-1;i>=0;i--)
{
if(commands[i].type=='D')add_edge(commands[i].x);
if(commands[i].type=='Q')query(commands[i].x,commands[i].p);
if(commands[i].type=='C')change_weight(commands[i].x,commands[i].p);
}
printf("Case %d: %.6lf\n",++kase,query_tot/(double)query_cnt);
}
}