NOI 项链工厂
来源:互联网 发布:制作图纸软件 编辑:程序博客网 时间:2024/05/16 14:59
原题:NOI 项链工厂
方法:线断树
program necklace;const maxn=500000;var ls,rs,s,cl,cr,z:array[1..maxn shl 2]of longint; sto,col,cur:longint; rev:boolean;procedure update(const x:longint);begin cl[x]:=cl[ls[x]]; cr[x]:=cr[rs[x]]; s[x]:=s[ls[x]]+s[rs[x]]-ord(cr[ls[x]]=cl[rs[x]]);end;procedure build(var x:longint;const l,r:longint);var mid:longint;begin inc(sto); x:=sto; mid:=(l+r)>>1; if l=r then begin read(cl[x]); cr[x]:=cl[x]; s[x]:=1; exit; end; build(ls[x],l,mid);build(rs[x],mid+1,r); update(x);end;procedure put(const x,cc:longint);begin cl[x]:=cc; cr[x]:=cc; z[x]:=cc; s[x]:=1;end;procedure modify(const x,cc:longint);begin if cc=0 then exit; if ls[x]<>0 then put(ls[x],cc); if rs[x]<>0 then put(rs[x],cc); z[x]:=0;end;function get_c(const x,l,r,k:longint):longint;var mid:longint;begin if z[x]<>0 then exit(z[x]); mid:=(l+r)>>1; if k=l then exit(cl[x]);if k=r then exit(cr[x]); if k<=mid then exit(get_c(ls[x],l,mid,k)); exit(get_c(rs[x],mid+1,r,k));end;function get(const x,l,r,ll,rr:longint):longint;var mid:longint;begin modify(x,z[x]); mid:=(l+r)>>1; if (ll=l)and(rr=r) then exit(s[x]); if rr<=mid then exit(get(ls[x],l,mid,ll,rr)) else if ll>mid then exit(get(rs[x],mid+1,r,ll,rr)) else get:=get(ls[x],l,mid,ll,mid)+get(rs[x],mid+1,r,mid+1,rr)-ord(cr[ls[x]]=cl[rs[x]]);end;procedure change(const x,l,r,k:longint);var mid:longint;begin modify(x,z[x]);mid:=(l+r)>>1; if (l=r) then begin cl[x]:=col; cr[x]:=col; exit; end; if k<=mid then change(ls[x],l,mid,k) else change(rs[x],mid+1,r,k); update(x);end;procedure change(const x,l,r,ll,rr:longint);var mid:longint;begin modify(x,z[x]);mid:=(l+r)>>1; if (l=ll)and(r=rr) then begin put(x,col); exit; end; if rr<=mid then change(ls[x],l,mid,ll,rr) else if ll>mid then change(rs[x],mid+1,r,ll,rr) else beginchange(ls[x],l,mid,ll,mid);change(rs[x],mid+1,r,mid+1,rr); end;update(x);end;var n,m,x,y,i,j:longint;ch:char; tmp_tot:integer;function g(const x:longint):longint;begin g:=cur; if not rev then inc(g,x-1) else inc(g,n-x+1); if g>n then dec(g,n);end;Begin assign(input,'input.txt');reset(input);assign(output,'output.txt');rewrite(output); readln(n,m); build(x,1,n); readln(m); rev:=false;cur:=1; for m:=1 to m do beginrepeat read(ch); until ch in ['R','F','P','C','S'];if ch='C' then begin if eoln then writeln(s[1]-ord(cl[1]=cr[1])+ord(s[1]=1)) else begin read(ch);read(i,j);i:=g(i);j:=g(j); if (not rev)then if (i<=j) then writeln(get(1,1,n,i,j)) else writeln(get(1,1,n,1,j)+get(1,1,n,i,n)-ord(cl[1]=cr[1])) else if (j<=i) then writeln(get(1,1,n,j,i)) else writeln(get(1,1,n,1,i)+get(1,1,n,j,n)-ord(cl[1]=cr[1]));end; end else if ch='R' then begin read(x); if rev then inc(cur,x) else inc(cur,n-x); if cur>n then dec(cur,n);end else if ch='F' then rev:=not revelse if ch='S' then begin readln(i,j);i:=g(i);j:=g(j); x:=get_c(1,1,n,i);y:=get_c(1,1,n,j); if x=y then continue; col:=y;change(1,1,n,i);col:=x;change(1,1,n,j);end else if ch='P' then begin read(i,j,col);i:=g(i);j:=g(j); if (not rev)then if(i<=j) then change(1,1,n,i,j) else begin change(1,1,n,1,j);change(1,1,n,i,n);end else if (j<=i) then change(1,1,n,j,i) else begin change(1,1,n,1,i);change(1,1,n,j,n);end;end; end; close(input);close(output);end.
- NOI 项链工厂
- BZOJ 1493 NOI 2007 项链工厂 Splay
- noi 2007 项链工厂 (bzoj 1493)
- noi 2007 项链工厂 (bzoj 1493)
- 1493: [NOI2007]项链工厂
- [BZOJ1493][NOI2007]项链工厂
- bzoj1493: [NOI2007]项链工厂
- 【线段树】【NOI2007】项链工厂
- 【BZOJ 1493】[NOI2007]项链工厂
- 【bzoj1493】项链工厂【线段树】
- bzoj1493: [NOI2007]项链工厂 splay
- BZOJ1493: [NOI2007]项链工厂 Splay
- [题解]bzoj1493 NOI2007项链工厂
- 【BZOJ】1493 [NOI2007]项链工厂 线段树
- BZOJ 1493 NOI2007 项链工厂 Splay
- 【BZOJ】【P1493】【NOI2007】【项链工厂】【题解】【Treap】
- [NOI2007]项链工厂(线段树)
- bzoj 1493 [NOI2007]项链工厂 线段树
- 《自己动手写操作系统》之ReadSector
- 孔雀东南飞
- 常量的定义
- 离骚
- C语言编译成可执行文件的模板,并放到虚拟机中运行。
- NOI 项链工厂
- svn 的 导入导出
- 机器人问题
- MmMapIoSpace和MmUnMapIoSpace
- Cannot open precompiled header file: 'Debug/****.pch': No such file or directory
- VisionMobile:2012年移动开发者经济报告(二):一、新格局看似尘埃落定(上)
- 存储长度那些事
- swfupload缩略图流程
- 逆波兰表示法