好题,先离线把连通块变成连续的区间
每次连通块合并就相当于两个区间合并
这样就轻易的用线段树解决了

 type node=record
wh:string[];
x,y:longint;
end; var lazy,tree:array[..*] of longint;
q:array[..] of node;
a,b,c,last,next,fa:array[..] of longint;
i,n,m,x,y,t:longint;
ch:char; procedure swap(var a,b:longint);
var c:longint;
begin
c:=a;
a:=b;
b:=c;
end; function max(a,b:longint):longint;
begin
if a>b then exit(a) else exit(b);
end; function getf(x:longint):longint;
begin
if fa[x]<>x then fa[x]:=getf(fa[x]);
exit(fa[x]);
end; procedure push(i:longint);
begin
inc(lazy[i*],lazy[i]);
inc(tree[i*],lazy[i]);
inc(lazy[i*+],lazy[i]);
inc(tree[i*+],lazy[i]);
lazy[i]:=;
end; function ask(i,l,r,x,y:longint):longint;
var m,s:longint;
begin
if (x<=l) and (y>=r) then exit(tree[i])
else begin
if lazy[i]<> then push(i);
m:=(l+r) shr ;
s:=-;
if x<=m then s:=ask(i*,l,m,x,y);
if y>m then s:=max(s,ask(i*+,m+,r,x,y));
exit(s);
end;
end; procedure add(i,l,r,x,y,z:longint);
var m:longint;
begin
if (x<=l) and (y>=r) then
begin
inc(tree[i],z);
inc(lazy[i],z);
end
else begin
if lazy[i]<> then push(i);
m:=(l+r) shr ;
if x<=m then add(i*,l,m,x,y,z);
if y>m then add(i*+,m+,r,x,y,z);
tree[i]:=max(tree[i*],tree[i*+]);
end;
end; procedure build(i,l,r:longint);
var m:longint;
begin
if l=r then tree[i]:=a[c[l]]
else begin
m:=(l+r) shr ;
build(i*,l,m);
build(i*+,m+,r);
tree[i]:=max(tree[i*],tree[i*+]);
end;
end; begin
readln(n);
for i:= to n do
begin
read(a[i]);
fa[i]:=i;
last[i]:=i;
end;
readln(m);
for i:= to m do
begin
read(ch);
q[i].wh:=ch;
read(ch);
q[i].wh:=q[i].wh+ch;
if q[i].wh='U ' then
begin
readln(q[i].x,q[i].y);
x:=getf(q[i].x);
y:=getf(q[i].y);
if x=y then continue;
fa[y]:=x;
next[last[x]]:=y;
last[x]:=last[y];
end
else if (q[i].wh='A1') or (q[i].wh='A2') then
readln(q[i].x,q[i].y)
else if (q[i].wh='F3') then readln
else readln(q[i].x);
end; for i:= to n do
if fa[i]=i then
begin
x:=i;
while x<> do
begin
inc(t);
b[x]:=t;
c[t]:=x;
x:=next[x];
end;
end; build(,,n);
for i:= to n do
begin
fa[i]:=i;
last[i]:=i;
end; for i:= to m do
if q[i].wh='U ' then
begin
x:=getf(q[i].x);
y:=getf(q[i].y);
if x=y then continue;
fa[y]:=x;
last[x]:=last[y];
end
else if q[i].wh='A1' then
add(,,n,b[q[i].x],b[q[i].x],q[i].y)
else if q[i].wh='A2' then
begin
x:=getf(q[i].x);
y:=last[x];
add(,,n,b[x],b[y],q[i].y);
end
else if q[i].wh='A3' then
begin
inc(tree[],q[i].x);
inc(lazy[],q[i].x);
end
else if q[i].wh='F1' then
writeln(ask(,,n,b[q[i].x],b[q[i].x]))
else if q[i].wh='F2' then
begin
x:=getf(q[i].x);
y:=last[x];
writeln(ask(,,n,b[x],b[y]));
end
else writeln(tree[]);
end.

最新文章

  1. 在Visual Studio 2015 中添加SharePoint 2016 开发模板
  2. C#语言基础——集合(ArrayList集合)
  3. 前端开发神器sublime Text
  4. 諾基亞定制的Android系統名為 Z Launcher
  5. Spring Batch学习笔记三:JobRepository
  6. pip UnicodeDecodeError: &#39;ascii&#39; codec can&#39;t decode byte
  7. 关于ORACLE中配置文件的问题
  8. Boost_udp错误
  9. BZOJ3720 Gty的妹子树
  10. VS 解决方案目录结构设置
  11. ruby中实例变量、类变量等等的区别和联系
  12. Selenium WebDriver 中鼠标和键盘事件分析及扩展(转)
  13. Linux系统启动过程介绍
  14. HTML系列(六):划分文档结构
  15. 27. Remove Element【leetcode】
  16. 201521123116 《java程序设计》第十二周学习总结
  17. 理解defineProperty以及getter、setter
  18. Optaplanner终于支持多线程并行运行 - Multithreaded incremental solving
  19. 在Eclipse中导入web项目时的问题总结
  20. Java高级应用简笔

热门文章

  1. Help View修复
  2. 学习笔记_Java_day12_设计模式MVC(13).JavaWeb的三层框架(14)
  3. java新手笔记22 接口示例2
  4. java新手笔记9 类的封装示例
  5. [USACO1.1.4]坏掉的项链Broken Necklace
  6. 使用宏定义来减少JNI的繁琐
  7. c#中创建类(更新中)
  8. express的基本配置项
  9. [转]CentOS Yum 命令详解
  10. VS2015安装开发ios android