[bzoj1180][CROATIAN2009]OTOCI_LCT
OTOCI bzoj-1180 CROATIAN-2009
题目大意:给你n个离散的点,m个操作。支持:两点加边(保证还是森林),修改单点权值,询问两点是否联通,查询联通两点之间路径权值。
注释:$1\le n \le 30,000$,$1\le m \le 300,000$。
想法:显然,又是一道LCT裸题。加边操作直接用link,修改权值直接修改,路径和的话每个点维护子树权值和makeroot(x)+access(y)+splay(y),然后直接查询子树和即可。
最后,附上丑陋的代码... ...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define N 100050 #define mod 51061 #define ls ch[p][0] #define rs ch[p][1] #define get(x) (ch[f[x]][1]==x) // typedef unsigned int int; int n,m,ch[N][2],f[N],rev[N]; int sum[N],mul[N], /* add[N],siz[N], */ val[N]; char s[10]; inline bool isrt( int x) { return ch[f[x]][0]!=x&&ch[f[x]][1]!=x; } void pushdown( int p) { // if(mul[p]!=1) // { // int u=mul[p]; // sum[ls]=sum[ls]*u%mod; mul[ls]=mul[ls]*u%mod; add[ls]=add[ls]*u%mod; val[ls]=val[ls]*u%mod; // sum[rs]=sum[rs]*u%mod; mul[rs]=mul[rs]*u%mod; add[rs]=add[rs]*u%mod; val[rs]=val[rs]*u%mod; // mul[p]=1; // } // if(add[p]) // { // int d=add[p]; // sum[ls]=(sum[ls]+siz[ls]*d%mod)%mod; add[ls]=(add[ls]+d)%mod; val[ls]=(val[ls]+d)%mod; // sum[rs]=(sum[rs]+siz[rs]*d%mod)%mod; add[rs]=(add[rs]+d)%mod; val[rs]=(val[rs]+d)%mod; // add[p]=0; // } if (rev[p]) { swap(ch[ls][0],ch[ls][1]); swap(ch[rs][0],ch[rs][1]); rev[ls]^=1; rev[rs]^=1; rev[p]=0; } } void pushup( int p) { // siz[p]=siz[ls]+siz[rs]+1; sum[p]=(sum[ls]+sum[rs]+val[p]) /* %mod */ ; } void update( int p) { if (!isrt(p)) update(f[p]); pushdown(p); } void rotate( int x) { int y=f[x],z=f[y],k=get(x); if (!isrt(y)) ch[z][ch[z][1]==y]=x; ch[y][k]=ch[x][!k]; f[ch[y][k]]=y; ch[x][!k]=y; f[y]=x; f[x]=z; pushup(y); pushup(x); } void splay( int x) { update(x); for ( int fa;fa=f[x],!isrt(x);rotate(x)) if (!isrt(fa)) rotate(get(fa)==get(x)?fa:x); } void access( int p) { int t=0; while (p) splay(p),rs=t,pushup(p),t=p,p=f[p]; } void makeroot( int p) { access(p); splay(p); swap(ls,rs); rev[p]^=1; } void link( int x, int p) { makeroot(x); f[x]=p; } void cut( int x, int p) { makeroot(x); access(p); splay(p); ls=f[x]=0; } void split( int x, int p) { makeroot(x); access(p); splay(p); } int find( int p) { access(p); splay(p); while (ls) pushdown(p),p=ls; return p; } void fix( int p, int v) { access(p); splay(p);val[p]=v; pushup(p); } int main() { /* int n; */ cin >> n ; for ( int i=1;i<=n;i++) scanf ( "%d" ,&val[i]); scanf ( "%d" ,&m); for ( int x,y,i=1;i<=m;i++) { scanf ( "%s%d%d" ,s,&x,&y); if (s[0]== 'e' ) { if (find(x)!=find(y)) { puts ( "impossible" ); } else { split(x,y); printf ( "%d\n" ,sum[y]); } } else if (s[0]== 'b' ) { if (find(x)==find(y)) puts ( "no" ); else puts ( "yes" ),link(x,y); } else fix(x,y); } } |
小结:LCT的模板生涯结束了... ...
| 欢迎来原网站坐坐! >原文链接<
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步