zoukankan      html  css  js  c++  java
  • bzoj 1180: [CROATIAN2009]OTOCI【LCT】

    一道几乎是板子的LCT,但是沉迷数学很久时候突然1A了这道题还是挺开心的

    #include<iostream>
    #include<cstdio>
    using namespace std;
    const int N=30005;
    int n,q,sum[N],v[N],s[N],top;
    char c[20];
    struct qwe
    {
    	int c[2],f,sum,lz;
    }t[N];
    int read()
    {
    	int r=0,f=1;
    	char p=getchar();
    	while(p>'9'||p<'0')
    	{
    		if(p=='-')
    			f=-1;
    		p=getchar();
    	}
    	while(p>='0'&&p<='9')
    	{
    		r=r*10+p-48;
    		p=getchar();
    	}
    	return r*f;
    }
    bool srt(int x)
    {
    	return t[t[x].f].c[0]!=x&&t[t[x].f].c[1]!=x;
    }
    void pd(int x)
    {
    	if(t[x].lz)
    	{
    		t[x].lz=0;
    		t[t[x].c[0]].lz^=1;
    		t[t[x].c[1]].lz^=1;
    		swap(t[x].c[0],t[x].c[1]);
    	}
    }
    void ud(int x)
    {
    	t[x].sum=t[t[x].c[0]].sum+t[t[x].c[1]].sum+v[x];
    }
    void zhuan(int x)
    {
    	int y=t[x].f,z=t[y].f,l=t[y].c[0]!=x,r=l^1;
    	if(!srt(y))
    		t[z].c[t[z].c[0]!=y]=x;
    	t[x].f=z;
    	t[y].c[l]=t[x].c[r];
    	t[t[x].c[r]].f=y;
    	t[x].c[r]=y;
    	t[y].f=x;
    	ud(y);
    	ud(x);
    }
    void splay(int x)
    {
    	top=0;
    	s[++top]=x;
    	for(int i=x;!srt(i);i=t[i].f)
    		s[++top]=t[i].f;
    	for(int i=top;i>=1;i--)
    		pd(s[i]);
    	while(!srt(x))
    	{
    		int y=t[x].f,z=t[y].f;
    		if(!srt(y))
    		{
    			if((t[z].c[0]==y)^(t[y].c[0]==x))
    				zhuan(x);
    			else
    				zhuan(y);
    		}
    		zhuan(x);
    	}
    }
    void acc(int x)
    {
    	for(int i=0;x;i=x,x=t[x].f)
    	{
    		splay(x);
    		t[x].c[1]=i;
    		ud(x);
    	}
    }
    void mkrt(int x)
    {
    	acc(x);
    	splay(x);
    	t[x].lz^=1;
    }
    void lk(int x,int y)
    {
    	mkrt(x);
    	t[x].f=y;
    }
    int getrt(int x)
    {
    	acc(x);
    	splay(x);
    	while(t[x].c[0])
    		x=t[x].c[0];
    	return x;
    }
    int main()
    {
    	n=read();
    	for(int i=1;i<=n;i++)
    		t[i].sum=v[i]=read();
    	q=read();
    	while(q--)
    	{
    		scanf("%s",c);
    		int x=read(),y=read();
    		//cout<<c<<endl;
    		if(c[0]=='b')
    		{
    			if(getrt(x)==getrt(y))
    				puts("no");
    			else
    			{
    				puts("yes");
    				lk(x,y);
    			}
    		}
    		else if(c[0]=='p')
    		{
    			mkrt(x);
    			v[x]=y;
    			ud(x);
    		}
    		else
    		{
    			if(getrt(x)!=getrt(y))
    				puts("impossible");
    			else
    			{
    				mkrt(x);
    				acc(y);
    				splay(y);
    				printf("%d
    ",t[y].sum);
    			}
    		}
    	}
    	return 0;
    }
    /*
    5
    4 2 4 5 6
    10
    excursion 1 1
    excursion 1 2
    bridge 1 2
    excursion 1 2
    bridge 3 4
    bridge 3 5
    excursion 4 5
    bridge 1 3
    excursion 2 4
    excursion 2 5
    */
    
  • 相关阅读:
    读《大道至简》第七、八章有感
    跨域AJAX
    简单的变长数组
    约瑟夫环问题
    BZOJ 1050: [HAOI2006]旅行comf
    BZOJ 1061: [Noi2008]志愿者招募
    BZOJ 1016: [JSOI2008]最小生成树计数
    20155326刘美岑 2016-2017-2 《Java程序设计》第5周学习总结
    6月13日云栖精选夜读:数梦工场完成A轮7.5亿融资 三个维度构建“新型互联网”
    Java使用POI实现数据导出excel报表
  • 原文地址:https://www.cnblogs.com/lokiii/p/8337784.html
Copyright © 2011-2022 走看看