BZOJ 2333 棘手的操作(离线+线段树+带权并查集)
这题搞了我一天啊。。。拍不出错原来是因为极限数据就RE了啊,竟然返回WA啊。我的线段树要开8倍才能过啊。。。
首先可以发现除了那个加边操作,其他的操作有点像线段树啊。如果我们把每次询问的联通块都放在一个区间的话,那么就可以用线段树维护了啊。
于是我们只需要用带权并查集把联通块串成一条链的形式。就可以用区间表示出来了啊。。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# include <cstdio> # include <cstring> # include <cstdlib> # include <iostream> # include <vector> # include <queue> # include <stack> # include <map> # include <set> # include <cmath> # include <algorithm> using namespace std; # define lowbit(x) ((x)&(-x)) # define pi acos(-1.0) # define eps 1e-3 # define MOD 1000000000 # define INF 1000000000 # define mem(a,b) memset(a,b,sizeof(a)) # define FOR(i,a,n) for(int i=a; i<=n; ++i) # define FO(i,a,n) for(int i=a; i<n; ++i) # define bug puts("H"); # define lch p<<1,l,mid # define rch p<<1|1,mid+1,r # define mp make_pair # define pb push_back typedef pair<int,int> PII; typedef vector<int> VI; # pragma comment(linker, "/STACK:1024000000,1024000000") typedef long long LL; int Scan() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } void Out(int a) { if(a<0) {putchar('-'); a=-a;} if(a>=10) Out(a/10); putchar(a%10+'0'); } const int N=300005; //Code begin... struct Node{char s[5]; int x, y;}node[N]; struct Q{int l, r;}q[N]; int seg[N<<3], tag[N<<3], a[N], fa[N], to[N], tail[N], suc[N], F[N]; int find(int x){ int tmp; if (fa[x]!=x) tmp=find(fa[x]), fa[x]=tmp; return fa[x]; } void union_set(int u, int v){fa[u]=v; suc[tail[v]]=u; tail[v]=tail[u];} void union_set1(int x, int y){ int l=min(q[x].l,q[y].l), r=max(q[x].r,q[y].r); fa[x]=y, q[y].l=l, q[y].r=r; } void push_up(int p){seg[p]=max(seg[p<<1],seg[p<<1|1]);} void push_down(int p){ if (!tag[p]) return ; seg[p]+=tag[p]; tag[p<<1]+=tag[p]; tag[p<<1|1]+=tag[p]; tag[p]=0; } void init(int p, int l, int r){ if (l<r) { int mid=(l+r)>>1; init(lch); init(rch); push_up(p); } else seg[p]=a[F[l]]; } int query(int p, int l, int r, int L, int R){ push_down(p); if (L>r||R<l) return -INF; if (L<=l&&R>=r) return seg[p]; int mid=(l+r)>>1; return max(query(lch,L,R),query(rch,L,R)); } void update(int p, int l, int r, int L, int R, int val){ push_down(p); if (L>r||R<l) return ; if (L<=l&&R>=r) tag[p]=val, push_down(p); else { int mid=(l+r)>>1; update(lch,L,R,val); update(rch,L,R,val); push_up(p); } } int main () { int n, m, u, v; scanf("%d",&n); FOR(i,1,n) fa[i]=tail[i]=i; FOR(i,1,n) scanf("%d",a+i); scanf("%d",&m); FOR(i,1,m) { scanf("%s",node[i].s); if (!strcmp(node[i].s,"U")||!strcmp(node[i].s,"A1")||!strcmp(node[i].s,"A2")) scanf("%d%d",&node[i].x,&node[i].y); else if(!strcmp(node[i].s,"F3")) continue; else scanf("%d",&node[i].x); } FOR(i,1,m) if(node[i].s[0]=='U') { u=find(node[i].x), v=find(node[i].y); if (u!=v) union_set(u,v); } int now=0; FOR(i,1,n) if (fa[i]==i) { to[i]=++now; F[now]=i; int tmp=i; while (suc[tmp]) tmp=suc[tmp], to[tmp]=++now, F[now]=tmp; } //debug //FOR(i,1,n) printf(" %d",to[i]); putchar('\n'); //debug init(1,1,n); FOR(i,1,n) fa[i]=q[i].l=q[i].r=i; FOR(i,1,m) { if (!strcmp(node[i].s,"U")) { u=find(to[node[i].x]); v=find(to[node[i].y]); if (u!=v) union_set1(u,v); } else if (!strcmp(node[i].s,"A1")) update(1,1,n,to[node[i].x],to[node[i].x],node[i].y); else if (!strcmp(node[i].s,"A2")) u=find(to[node[i].x]), update(1,1,n,q[u].l,q[u].r,node[i].y); else if (!strcmp(node[i].s,"A3")) update(1,1,n,1,n,node[i].x); else if (!strcmp(node[i].s,"F1")) printf("%d\n",query(1,1,n,to[node[i].x],to[node[i].x])); else if (!strcmp(node[i].s,"F2")) u=find(to[node[i].x]), printf("%d\n",query(1,1,n,q[u].l,q[u].r)); else printf("%d\n",query(1,1,n,1,n)); } return 0; }