HDU 5023 A Corrupt Mayor ' s performance Art (segment tree interval update)

Source: Internet
Author: User

Problem Descriptioncorrupt 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?
Inputthere 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.
Outputfor 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: P: Change the interval color. Q: Statistic interval color type. is a simple interval update. However, because there are 30 colors, and to count the color types, we useBinary represents each color. Then the statistical output can be.
#include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <string > #include <iostream> #include <queue> #include <cmath> #include <map> #include <stack>  #include <bitset>using namespace std; #define REPF (I, A, b) for (int i = A; I <= B; + + i) #define REP (i, n ) for (int i = 0; i < n; + + i) #define CLEAR (A, X) memset (A, x, sizeof a) typedef long long Ll;typedef pair& lt;int,int>pil;const int mod = 1000000007;const int maxn=1000000+10;const int maxm=100000+10;int add[maxn<<2], Sum[maxn<<2];int n,m,ans[maxn];void pushup (int rs) {sum[rs]= (sum[rs<<1]|sum[rs<<1|1]);}        void Pushdown (int rs) {if (Add[rs]) {add[rs<<1]=add[rs];        add[rs<<1|1]=add[rs];        sum[rs<<1]=add[rs];        sum[rs<<1|1]=add[rs];    add[rs]=0;    }}void Build (int rs,int l,int r) {add[rs]=0;        if (l==r) {sum[rs]=2;    return; } inT mid= (l+r) >>1;    Build (Rs<<1,l,mid);    Build (Rs<<1|1,mid+1,r); Pushup (RS);} void update (int x,int y,int c,int l,int r,int rs) {if (l>=x&&r<=y) {add[rs]= (LL) (1<< (c-1)        );        Sum[rs]= (LL) (1<< (c-1));    return;    } pushdown (RS);    int mid= (L+R) >>1;    if (x<=mid) update (X,Y,C,L,MID,RS&LT;&LT;1);    if (y>mid) update (X,Y,C,MID+1,R,RS&LT;&LT;1|1); Pushup (RS);}    int query (int x,int y,int l,int r,int rs) {if (l>=x&&r<=y) {return sum[rs];    } pushdown (RS);    int res=0;    int mid= (L+R) >>1;    if (x<=mid) res|=query (x,y,l,mid,rs<<1);    if (y>mid) res|=query (x,y,mid+1,r,rs<<1|1); return res;}   int main () {char str[2];   int x,y,w;       while (~SCANF ("%d%d", &n,&m) && (n+m)) {build (1,1,n);           while (m--) {scanf ("%s", str);       if (str[0]== ' P ') {scanf ("%d%d%d", &x,&y,&w);        Update (x,y,w,1,n,1);               } else {scanf ("%d%d", &x,&y);               int Tt=query (x,y,1,n,1);               int l=0;               for (int i=0;i<30;i++) if (tt& (1<<i)) ans[l++]=i+1; for (int i=0;i<l;i++) printf (i==l-1? ")           %d\n ":"%d ", ans[i]); }}} return 0;}


HDU 5023 A Corrupt Mayor ' s performance Art (segment tree interval update)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.