Description
Corrupt governors always find ways to get dirty money. Paint something, then sell the worthless painting at a-high-price-to-someone who wants-bribe him/her on an auction, thi S seemed a safe-on-the-mayor for the X to make money.
Because A lot of people praised Mayor X ' s painting (of course, X is a mayor), Mayor X believed more and more that he was a Very talented painter. Soon Mayor X is not satisfied with only making money. He wanted to be a famous painter. So he joined the local painting associates. Other painters had to elect him as the chairman of the Associates. Then he painting sold at better.
The local middle school from which Mayor X graduated, wanted to beat Mayor X ' s horse fart (in Chinese 中文版, beating one ' s horse fart means flattering one hard). They built a wall, and invited Mayor X to paint on it. Mayor X was very happy. But he really had no idea on what to paint because he could only paint very abstract paintings which nobody really unde Rstand. Mayor X ' s secretary suggested that he could make this thing isn't only a painting, but also a performance art work.
This is the Secretary ' s idea:
The wall was divided to N segments and the width of each segment were one cun (Cun is a Chinese length unit). All segments were numbered from 1 to N There were kinds of colors mayor X could use to paint the wall. They named those colors as color 1, color 2 .... color 30. The wall ' s original color was color 2. Every time mayor X would paint some consecutive segments with a certain kind of color, and he does this for many times. Trying to make his performance art fancy, mayor X declared so at any moment, if someone asked how many kind of colors we Re there on any consecutive segments, he could give the number immediately without counting.
But Mayor X didn ' t know what to give the right answer. Your friend, Mr W is an secret officer of anti-corruption Bureau, he helped mayor X on this problem and gained his trust . Do you know how Mr. Q does this?
Input
There is several test cases.
For each test case:
The first line contains-integers, n and M, meaning that the wall is divided to N segments and there are M operations (0 < N <= 1,000,000; 0<m<=100,000)
Then M. lines follow, each representing an operation. There is kinds of operations, as described below:
1) P a b C
A, B and C are integers. This operation means the mayor X painted all segments from segment A to segment B with color C (0 < a<=b <= N, 0 < C <= 30).
2) Q a B
A and B are integers. This is a query operation. It means that someone asked what many kinds of colors were there from segment A to segment B (0 < a<=b <= N) .
Please note that the operations is given in time sequence.
The input ends with M = 0 and N = 0.
Output
For each query operation, print all kinds of color on the queried segments. For color 1, print 1, for color 2, print 2 ... etc. And this color sequence must is in ascending order.
Sample Input
5 10P 1 2 3P 2 3 4Q 2 3Q 1 3P 3 5 4P 1 2 7Q 1 3Q 3 4P 5 5 8Q 1 50 0
Sample Output
43 44 744) 7 8
Test instructions: Give the point [1,n] and update the number of queries M,p is on the interval [a, b] Dianran color c,q is the query interval [a, b] what color, from small to large output.
Idea: Build, use INS to update the interval and lazy mark, and then use Sea to query. Since there are 30 colors, you can store them with the binary of int.
Note: When making a small contribution to the tle, instead of the MLE or re, it's been a long struggle.
AC Code:
1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5#include <queue>6#include <vector>7#include <map>8 #definell Long Long9 using namespacestd;Ten Const intmaxn=1000000+Ten; One structNote A { - intL,r,ans; - BOOLT; the} a[maxn<<2];//Note: maxn<<2 - intBuildintLintRintk) - { -a[k].l=l,a[k].r=r,a[k].ans=1<<2, a[k].t=0; + if(l==R) - { + return 0; A } at intMid= (l+r) >>1; -Build (l,mid,k<<1); -Build (mid+1,r,k<<1|1); - return 0; - } - intPushdown (intk) in { -a[k<<1].ans=a[k<<1|1].ans=A[k].ans; toa[k].t=0; +a[k<<1].t=a[k<<1|1].t=1; - return 0; the } * intInsintLintRintKintd) $ {Panax Notoginseng if(a[k].l==l&&a[k].r==R) - { thea[k].t=1; +a[k].ans=1<<D; A return 0; the } + if(a[k].t) pushdown (k); - intMid= (A[K].L+A[K].R) >>1; $ if(a[k<<1].r>=r) Ins (l,r,k<<1, d); $ Else if(a[k<<1|1].l<=l) Ins (l,r,k<<1|1, d); - Else - { theINS (l,mid,k<<1, d); -INS (mid+1,r,k<<1|1, d);Wuyi } thea[k].ans=a[k<<1].ans|a[k<<1|1].ans; - return 0; Wu } - intSeaintLintRintk) About { $ if(l==a[k].l&&r==A[K].R) - { - returnA[k].ans; - } A if(a[k].t) pushdown (k); + intMid= (A[K].L+A[K].R) >>1, ans; the if(a[k<<1].R>=R) Ans=sea (l,r,k<<1); - Else if(a[k<<1|1].l<=l) Ans=sea (l,r,k<<1|1); $ Else the { theAns=sea (l,mid,k<<1) |sea (mid+1,r,k<<1|1); the } the returnans; - } in intMain () the { the intn,m,a,b,c; About Chars[Ten]; the while(~SCANF ("%d%d",&n,&m)) the { the if(n==0&&m==0) + Break; -Build1N1); the while(m--)Bayi { thescanf"%s", s); the if(s[0]=='P') - { -scanf"%d%d%d",&a,&b,&c); theIns (A, B,1, c); the } the Else the { - intans,i; thescanf"%d%d",&a,&b); theAns=sea (A, B,1); the for(i=1; i<= -; i++)//Note: i<=3094 if((ans>>i) &1) the { theprintf"%d", i); the Break;98 } About for(i++;i<= -; i++) - if((ans>>i) &1)101printf"%d", i);102printf"\ n");103 }104 } the }106 return 0;107}
View Code
hdu--5023 A corrupt Mayor ' s performance Art (segment tree + interval update + bitwise operation)