【求助】Uva 1479 Graph and Queries
  • 板块学术版
  • 楼主2020kanade
  • 当前回复0
  • 已保存回复0
  • 发布时间2021/12/3 20:52
  • 上次更新2023/11/3 23:03:15
查看原帖
【求助】Uva 1479 Graph and Queries
456724
2020kanade楼主2021/12/3 20:52

WA,自己造的数据、样例、uDEBUG找的两组数据全过(最后一行回车符是后来加上的,未改变测评结果) 求HACK或差错,多谢各位神犇

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL N=2e4+10,M=6e4+10;
#define TNP pair<node * ,node * >
#define NULL nullptr
struct node
{
	node *ch[2];
	LL r,v,s;
	node(LL v):v(v) {ch[0]=ch[1]=NULL;s=1;r=rand();}
	//bool operator < (const node* & rhs) const {return this->r<=rhs->r;}
	inline void up()
	{
		this->s=1;
		if(this->ch[0]!=NULL) this->s+=this->ch[0]->s;if(this->ch[1]!=NULL) this->s+=this->ch[1]->s;
	}
};
inline LL sz(node* &rt) {return rt==NULL?0:rt->s;}
inline TNP split_s(node* rt,LL k)
{
	if(rt==NULL) return TNP(NULL,NULL);TNP t;
	if(k<=sz(rt->ch[0])) {t=split_s(rt->ch[0],k);rt->ch[0]=t.second;rt->up();t.second=rt;}
	else {t=split_s(rt->ch[1],k-sz(rt->ch[0])-1);rt->ch[1]=t.first;rt->up();t.first=rt;}
	return t;
}
inline TNP split(node* rt,LL k)
{
	if(rt==NULL) return TNP(NULL,NULL);TNP t;
	if(rt->v<=k) {t=split(rt->ch[1],k);rt->ch[1]=t.first;rt->up();t.first=rt;}
	else {t=split(rt->ch[0],k);rt->ch[0]=t.second;rt->up();t.second=rt;}
	return t;
}
inline node* merge(node* a,node* b)
{
	if(a==NULL) return b;if(b==NULL) return a;
	if(a->r<b->r) {a->ch[1]=merge(a->ch[1],b);a->up();return a;}
	else {b->ch[0]=merge(a,b->ch[0]);b->up();return b;}
}
inline LL getrank(node* rt,LL k)
{
	if(!rt) return 0;
	else if(k<=rt->v) return getrank(rt->ch[0],k);else return getrank(rt->ch[1],k)+sz(rt->ch[0])+1;
}
inline LL getkth(node* &rt,LL k)
{
	//if(!rt) return 0;
	TNP a=split_s(rt,k-1);TNP b=split_s(a.second,1);
	node* t=b.first;rt=merge(a.first,merge(t,b.second));
	return t?t->v:0;
}
inline void insert(node* &rt,LL v)
{
	TNP t=split(rt,v);
	rt=merge(t.first,merge(new node(v),t.second));//rt->up();
}
inline void rem(node* &rt,LL v)
{
	TNP t=split(rt,v-1);
	TNP u=split_s(t.second,1);delete(u.first);
	rt=merge(t.first,u.second);//rt->up();
}
inline void del_T(node* &rt) {if(!rt) return; if(rt->ch[0]) del_T(rt->ch[0]);if(rt->ch[1]) del_T(rt->ch[1]);delete(rt);}
inline LL getlst(node* &rt,LL v) {return getkth(rt,getrank(rt,v));}
inline LL getnxt(node* &rt,LL v) {return getkth(rt,getrank(rt,v+1)+1);}
node *Ts[N];//FHQ Treap
LL uds[N];
inline LL getf(LL u)
{
	return u==uds[u]?uds[u]:uds[u]=getf(uds[u]);
}
inline void uds_withT_merge(LL u,LL v)
{
	LL uf=getf(u),vf=getf(v);LL us=Ts[uf]->s,vs=Ts[vf]->s;
	if(us<vs) {while(Ts[uf]) insert(Ts[vf],getkth(Ts[uf],1)),rem(Ts[uf],getkth(Ts[uf],1));uds[u]=v;}
	else {while(Ts[vf]) insert(Ts[uf],getkth(Ts[vf],1)),rem(Ts[vf],getkth(Ts[vf],1));uds[v]=u;}
}//UDS and Treap's merge
inline void add_e(LL u,LL v)
{
	LL uf=getf(u),vf=getf(v);
	if(uf==vf) return;
	uds_withT_merge(u,v);
}//UDS
struct edge
{
	LL u,v;
}E[M];
inline void add(LL x) {add_e(E[x].u,E[x].v);}
LL nodev[N];bool remed[M];//Graph
struct command
{
	char type;
	LL x,k;
}C[600001];//commands

bool first=1;LL n,m,cnt,kase=1,ansI,cntq;char opt;long double ans;//essential variables
inline void reset(LL &n,LL &m,LL &cnt)
{
	for(auto i=0;i<=n+1;i++) if(Ts[i]) del_T(Ts[i]);for(auto i=0;i<=n+1;i++) Ts[i]=nullptr;
	memset(uds,0,sizeof(uds));memset(remed,0,sizeof(remed));;memset(nodev,0,sizeof(nodev));
	for(auto i=0;i<=cnt+1;i++) C[i]=command{'0',0,0};for(auto i=0;i<=m+1;i++) E[i]=edge{0,0};
	n=0,m=0,cnt=0;ansI=0,ans=0,cntq=0;opt='0';
}//clear multi-test
inline LL qread()
{
    LL a=0,f=1;char ch=getchar();
    while(ch<'0' || ch>'9') {if(ch=='-') f*=-1;ch=getchar();}
    while(ch>='0' && ch<='9') {a=a*10+ch-'0';ch=getchar();}
    return a*f;
}
inline char getsc() {char ch=getchar();getchar();return ch;}//quickreads
int main()
{
	freopen("in.txt","r",stdin);
	for(;scanf("%lld %lld",&n,&m)==2 && (n!=0 && m!=0);kase++)
	{
		for(auto i=1;i<=n;i++) nodev[i]=qread(),uds[i]=i;
		for(auto i=1;i<=m;i++) E[i]=edge{qread(),qread()};
		while((opt=getsc())!='E')
		{
			C[++cnt].type=opt;
			if(opt=='D') {C[cnt].x=qread();remed[C[cnt].x]=1;}
			else if(opt=='Q') {cntq++;C[cnt].x=qread(),C[cnt].k=qread();}
			else if(opt=='C') {C[cnt].x=qread();C[cnt].k=nodev[C[cnt].x];nodev[C[cnt].x]=qread();}
		}
		for(auto i=1;i<=n;i++) insert(Ts[i],nodev[i]);
		for(auto i=1;i<=m;i++) if(!remed[i]) add(i);
		for(auto i=cnt;i>=1;i--)
		{
			if(C[i].type=='D') add(C[i].x),remed[C[i].x]=0;
			else if(C[i].type=='Q') {LL nf=getf(C[i].x);ansI+=Ts[nf]->s+1-C[i].k>0?getkth(Ts[nf],Ts[nf]->s+1-C[i].k):0;}
			else if(C[i].type=='C')
			{
				LL nf=getf(C[i].x);rem(Ts[nf],nodev[C[i].x]);
				nodev[C[i].x]=C[i].k;insert(Ts[nf],C[i].k);
			}
		}
		ans=ansI/(cntq*1.0);
		printf("Case %lld: %6Lf",kase,ans);putchar('\n');
		reset(n,m,cnt);
	}
	return 0;
}
2021/12/3 20:52
加载中...