看训练指南做的这道题,参考了书上的代码,不过还是说一些自己的一点理解。
首先为何要离线弄?为什么不能按顺序来,这里书上没有讲,其实并不难想,主要在于,首先一个联通块要用一个名次树,然后删除一条边的话可能变成2个联通块,但是又可能还是联通的,这里的判断并不容易得到(而且还要用到树的分裂)。但是如果是反过来操作,那么就变成了加一条边,这里可以用并查集很容易的知道在加边之前是否就属于一个联通块,如果不属于才需要合并2颗树。这就是反过来弄的好处。
其余的2个操作改x值为v就变为改v为x,查第k大还是查第k大,细节处书上也处理的特别清楚就不说了。
AC代码:
//#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<ctype.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<cstdlib>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<string.h>
#include<string>
#include<sstream>
#include<bitset>
using namespace std;
#define ll long long
#define ull unsigned long long
#define eps 1e-8
#define NMAX 201000
#define MOD 1000000007
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1)
template<class T>
inline void scan_d(T &ret)
{
char c;
int flag = 0;
ret=0;
while(((c=getchar())<'0'||c>'9')&&c!='-');
if(c == '-')
{
flag = 1;
c = getchar();
}
while(c>='0'&&c<='9') ret=ret*10+(c-'0'),c=getchar();
if(flag) ret = -ret;
}
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;}
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;
}
int find(Node* o, int x)
{
while(o != NULL)
{
int d = o->cmp(x);
if(d == -1) return 1;
else o = o->ch[d];
}
return 0;
}
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->r < o->ch[d]->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]->r > o->ch[1]->r) ? 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();
}
const int maxn = 20000+10,maxm = 60000+10;
struct query
{
char type;
int x,p;
// query(char type, int x, int p):type(type),x(x),p(p){}
};
query que[500005];
int w[maxn],pa[maxn],from[maxm],to[maxm],del[maxm];
Node* root[maxn];
int findset(int x)
{
return pa[x] != x ? pa[x] = findset(pa[x]) : x;
}
void removetree(Node* &x)
{
if(x == NULL) return;
removetree(x->ch[0]);
removetree(x->ch[1]);
delete x;
x = NULL;
}
void mergeto(Node* &x, Node* &dest)
{
if(x == NULL) return;
mergeto(x->ch[0],dest);
mergeto(x->ch[1],dest);
insert(dest,x->v);
delete x;
x = NULL;
}
void add_edge(int x)
{
int a = findset(from[x]),b = findset(to[x]);
if(a != b)
{
if(root[a]->s > root[b]->s)
{
pa[b] = a;
mergeto(root[b],root[a]);
}
else
{
pa[a] = b;
mergeto(root[a],root[b]);
}
}
}
int dfs(Node* o, int k)
{
if(k > o->s || k <= 0) return 0;
int ge = (o->ch[1] == NULL) ? 0 : o->ch[1]->s;
if(k == ge+1) return o->v;
if(k <= ge) return dfs(o->ch[1],k);
return dfs(o->ch[0],k-ge-1);
}
int main()
{
#ifdef GLQ
freopen("input.txt","r",stdin);
// freopen("o3.txt","w",stdout);
#endif // GLQ
srand(time(NULL));
int n,m,cas = 1;
while(~scanf("%d%d",&n,&m) && n+m)
{
printf("Case %d: ",cas++);
for(int i = 1; i <= n; i++)
pa[i] = i;
for(int i = 1; i <= n; i++)
scanf("%d",&w[i]);
for(int i = 1; i <= m; i++)
scanf("%d %d",&from[i],&to[i]);
memset(del,0,sizeof(del));
char c;
int nct = 0;
while(scanf(" %c",&c) && c != 'E')
{
int x,p=0;
if(c == 'D')
{
scanf("%d",&x);
del[x] = 1;
}
if(c == 'Q') scanf("%d%d",&x,&p);
if(c == 'C')
{
scanf("%d%d",&x,&p);
int tmp = w[x];
w[x] = p;
p = tmp;
}
que[nct++] = (query){c,x,p};
}
// for(int i = 0; i < nct; i++)
// cout<<que[i].type<<" "<<que[i].x<<" "<<que[i].p<<endl;
for(int i = 1; i <= n; i++)
{
if(root[i] != NULL) removetree(root[i]);
root[i] = new Node(w[i]);
}
// cout<<root[1]->v<<endl;
// cout<<nct<<" "<<que[nct-1].type<<" "<<que[nct-1].x<<" "<<que[nct-1].p<<endl;
for(int i = 1; i <= m; i++) if(!del[i])
add_edge(i);
ll ans = 0,cnt = 0;
for(int i = nct-1; i >= 0; i--)
{
if(que[i].type == 'D') add_edge(que[i].x);
else if(que[i].type == 'Q')
{
int fa = findset(que[i].x);
ans += dfs(root[fa],que[i].p);
cnt++;
}
else
{
int fa = findset(que[i].x);
remove(root[fa],w[que[i].x]);
insert(root[fa],que[i].p);
w[que[i].x] = que[i].p;
}
}
printf("%.6lf\n",(double)ans/(double)cnt);
}
return 0;
}