BZOJ 2049 & LCT又一模板

题意:

  自己看...加边删边问联通...

SOL:

  就加了一个findroot而已...

  然而时间还是惨不忍睹...优化全开也才1700ms...膜seter...

Code:

  

/*==========================================================================
# Last modified: 2016-03-17 18:33
# Filename: 2049.cpp
# Description:
==========================================================================*/
#define me AcrossTheSky
#include <cstdio>
#include <cmath>
#include <ctime>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm> #include <set>
#include <map>
#include <stack>
#include <queue>
#include <vector> #define lowbit(x) (x)&(-x)
#define FOR(i,a,b) for((i)=(a);(i)<=(b);(i)++)
#define FORP(i,a,b) for(int i=(a);i<=(b);i++)
#define FORM(i,a,b) for(int i=(a);i>=(b);i--)
#define ls(a,b) (((a)+(b)) << 1)
#define rs(a,b) (((a)+(b)) >> 1)
#define getlc(a) ch[(a)][0]
#define getrc(a) ch[(a)][1] #define maxn 100000
#define maxm 100000
#define pi 3.1415926535898
#define _e 2.718281828459
#define INF 1070000000
using namespace std;
typedef long long ll;
typedef unsigned long long ull; template<class T> inline
void read(T& num) {
bool start=false,neg=false;
char c;
num=0;
while((c=getchar())!=EOF) {
if(c=='-') start=neg=true;
else if(c>='0' && c<='9') {
start=true;
num=num*10+c-'0';
} else if(start) break;
}
if(neg) num=-num;
}
/*==================split line==================*/
int ch[maxn][2],rev[maxn],nxt[maxn],fa[maxn],pre[maxn],s[maxn];
int sta[maxn];
int n,m;
inline void pushup(int x){
s[x]=1+s[ch[x][0]]+s[ch[x][1]];
}
inline void pushdown(int x){
if (rev[x]){
swap(ch[x][0],ch[x][1]);
rev[ch[x][0]]^=1;
rev[ch[x][1]]^=1;
rev[x]=0;
}
}
inline void setx(int x){
if (ch[x][1]) {
fa[ch[x][1]]=0; pre[ch[x][1]]=x; ch[x][1]=0; pushup(x);
}
}
inline void rotate(int x){
int p=fa[x],q=fa[p],d=ch[p][1]==x;
fa[ch[p][d]=ch[x][d^1]]=p; pushup(p);
fa[ch[x][d^1]=p]=x; pushup(x);
fa[x]=q;
if (q){
if (ch[q][1]==p) ch[q][1]=x;
else ch[q][0]=x;
}
else pre[x]=pre[p];
}
inline void splay(int x){
int top=0;
for (int i=x;i;i=fa[i]) sta[++top]=i;
for (;top;top--) pushdown(sta[top]);
for (int y;(y=fa[x])!=0;rotate(x))
if (fa[y]) rotate((getlc(y)==x)==(getlc(fa[y])==y)?y:x);
}
inline void access(int x){
splay(x); setx(x); int v;
while(v=pre[x]){
splay(v); setx(v);
ch[v][1]=x; fa[x]=v; pushup(v);
x=v;
}
}
inline void beroot(int x){
access(x);
splay(x);
rev[x]^=1;
}
inline void link(int x,int y){
beroot(x);
pre[x]=y;
//access(x); splay(x);
}
inline void cut(int x,int y){
beroot(x); access(y); splay(y);
ch[y][0]=fa[x]=pre[x]=0;
pushup(y);
}
inline int findroot(int x){
while (ch[x][0]) x=ch[x][0];
return x;
}
inline void query(int x,int y){
beroot(y);
access(x);
splay(x);
if (findroot(x)==y) printf("Yes\n");
else printf("No\n");
}
int main(){
read(n); read(m);
FORP(i,1,n) s[i]=1;
FORP(i,1,m){
char temp[10]; int x,y;
scanf("%s",temp); read(x); read(y);
if (x<y) swap(x,y);
if (temp[0]=='Q') query(x,y);
else if (temp[0]=='C') link(x,y);
else cut(x,y);
}
}
上一篇:python函数4种类型及函数生成帮助文档


下一篇:【转载】RabbitMQ基础知识