题解 P3273 【[SCOI2011]棘手的操作】
真的是恶心题。。
建立
对于操作
对于操作
对于操作
对于操作
后三个操作就很简单了。
一开始因为实现常数太大被卡常了。。后面优化了一下操作
这种题尽量一次写好,要不然调起来很难受。
#include<bits/stdc++.h>
#define it multiset<pair<int,int> >::iterator
using namespace std;
multiset<pair<int,int>,greater<pair<int,int> > > p[300005];
multiset<int> mx;
int tag[300005];
int a[300005];
int f[300005];
static char buf[1000000],*p1=buf,*p2=buf,obuf[1000000],*p3=obuf;
#define getchar() p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++
int find(int x)
{
return f[x]==x?x:f[x]=find(f[x]);
}
int read()
{
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9') w=ch=='-'?-1:w,ch=getchar();
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
void merge(int x,int y)
{
int fx=find(x),fy=find(y),k;
if(fx==fy) return;
if(p[fx].size()>p[fy].size())
swap(fx,fy);
f[fx]=fy;
mx.erase(mx.find(min((*p[fx].begin()).first+tag[fx],(*p[fy].begin()).first+tag[fy])));
for(it i=p[fx].begin();i!=p[fx].end();i++)
{
pair<int,int> u=*i;
a[u.second]=u.first-tag[fy]+tag[fx],p[fy].insert({a[u.second],u.second});
}
p[fx].clear();
}
bool t[1000];
void gc(char opt[])
{
int cnt=0;
char ch=getchar();
while(!t[ch]) ch=getchar();
while(t[ch]) opt[cnt++]=ch,ch=getchar();
}
int main()
{
t['U']=t['A']=t['F']=t['1']=t['2']=t['3']=1;
int s=0;
int i,j,n,m,fx,fy,x,y,po;
char opt[5];
n=read();
for(i=1;i<=n;i++)
{
a[i]=read();
p[i].insert({a[i],i});
mx.insert(a[i]);
f[i]=i;
}
m=read();
while(m--)
{
gc(opt);
if(*opt=='U') x=read(),y=read(),merge(x,y);
if(*opt=='A'&&opt[1]=='1')
{
x=read(),y=read();
fx=find(x);
mx.erase(mx.find((*p[fx].begin()).first+tag[fx]));
p[fx].erase(p[fx].find({a[x],x}));
a[x]+=y;
p[fx].insert({a[x],x});
mx.insert((*p[fx].begin()).first+tag[fx]);
}
if(*opt=='A'&&opt[1]=='2')
{
x=read(),y=read();
fx=find(x);
po=(*p[fx].begin()).first+tag[fx];
mx.erase(mx.find(po));
tag[fx]+=y;
mx.insert(po+y);
}
if(*opt=='A'&&opt[1]=='3')
{
y=read();
s+=y;
}
if(*opt=='F'&&opt[1]=='1') x=read(),printf("%d\n",a[x]+tag[find(x)]+s);
if(*opt=='F'&&opt[1]=='2') x=read(),printf("%d\n",(*p[find(x)].begin()).first+tag[find(x)]+s);
if(*opt=='F'&&opt[1]=='3') printf("%d\n",*--mx.end()+s);
}
}