Twinkling lights iiitime limit:8000msmemory limit:131072kbthis problem would be judged onFzu. Original id:1968
64-bit integer IO format: %i64d Java class name: Main
Twinkling lights has always been a fun game. Perhaps you remember FZU1069 twinkling lights and FZU1420 twinkling lights ii. Now, Bluewind changed the rules of the game, the game will become more fun.
n Lamp line, number 1. N, at first, all the lights were open. The Bluewind will perform m operations, and the operation is divided into five types:
C x y, turn off the lights from X to Y, and the lights that were turned off remain the same.
S x y, the lights that numbered from X to Y are open, the original lights still open.
A x y, so that the number from X to Y lights are changed state, that the original lights off, the original turned off the lights.
Q x y, the number of lights that are open from X to Y in the query number.
L x y, the number of the longest continuously open lights in the query number from X to Y.
Input
The first line of two integers n,m (1<=n,m<=500,000) indicates that there are N lights, M operations.
Next m lines, each row gives an action in the specified format, where (1<=x<=y<=n).
Output
For each Q query operation and l query operation, output the corresponding results.
Sample Input
5C 2 8S 5 7A 1 10Q 1 10L 1 10
Sample Output
43
SourceContest for lecture II Problem solving: Segment tree ... The problem hdu 3397 Sequence operation very much alike.
1#include <iostream>2#include <cstdio>3 using namespacestd;4 Const intMAXN =500010;5 structNode {6 intLt,rt,cover;7} tree[maxn<<2];8 intret;9 voidBuildintLintRintv) {Tentree[v].lt =L; OneTree[v].rt =R; ATree[v].cover =1; - if(L = = R)return; - intMid = (L + R) >>1; theBuild (l,mid,v<<1); -Build (mid+1,r,v<<1|1); - } -InlinevoidPushup (intv) { + if(tree[v<<1].cover = = tree[v<<1|1].cover) -Tree[v].cover = tree[v<<1].cover; + ElseTree[v].cover =-1; A } atInlinevoidPushdown (intv) { - if(Tree[v].cover >=0) { -tree[v<<1].cover = tree[v<<1|1].cover =Tree[v].cover; -Tree[v].cover =-1; - } - } in voidUpdateintLtintRtintValBOOLSelintv) { - if(sel && Tree[v].cover = = val)return; to if(LT <= tree[v].lt && rt >= tree[v].rt && (sel | |!sel && tree[v].cover >=0)) { + if(SEL) Tree[v].cover =Val; - ElseTree[v].cover ^=1; the return; * } $ Pushdown (v);Panax Notoginseng if(LT <= tree[v<<1].RT) Update (lt,rt,val,sel,v<<1); - if(Rt >= tree[v<<1|1].lt) Update (lt,rt,val,sel,v<<1|1); the Pushup (v); + } A voidQueryintLtintRtint&ans,int&r,BOOLSelintv) { the if(!tree[v].cover)return; + if(LT <= tree[v].lt && rt >= tree[v].rt && tree[v].cover >0) { - if(sel) ans + = tree[v].rt-tree[v].lt +1; $ Else { $ if(R +1= = tree[v].lt) ans + = tree[v].rt-tree[v].lt +1; - ElseAns = tree[v].rt-tree[v].lt +1; - } theR =Tree[v].rt; -RET =Max (Ans,ret);Wuyi return; the } - Pushdown (v); Wu if(LT <= tree[v<<1].RT) Query (lt,rt,ans,r,sel,v<<1); - if(Rt >= tree[v<<1|1].lt) Query (lt,rt,ans,r,sel,v<<1|1); About Pushup (v); $ } - intMain () { - intN,m,x,y,ans,r; - Charop[3]; A while(~SCANF ("%d%d",&n,&m)) { +Build1N1); the while(m--) { -scanf"%s%d%d",op,&x,&y); $ Switch(op[0]) { the Case 'C': theUpdate (x, Y,0,true,1); the Break; the Case 'S': -Update (x, Y,1,true,1); in Break; the Case 'A': theUpdate (x, Y,0,false,1); About Break; the Case 'Q': theret = ans = r =0; theQuery (X,y,ans,r,true,1); +printf"%d\n", ret); - Break; the Case 'L':Bayiret = ans = r =0; theQuery (X,y,ans,r,false,1); theprintf"%d\n", ret); - Break; - } the } the } the return 0; the}View Code
Fzu 1968 twinkling Lights III