HDU 4973 A simple simulation problem.(線段樹)

來源:互聯網
上載者:User

標籤:des   style   color   os   java   io   strong   ar   for   

題目大意:D表示在區間x,y內所有的元素擴充一倍;Q表示查詢在這個下表以內的數字最多的個數為多少。

如:1,2,3.D 1 3 之後就變成了 1 1 2 2 3 3.Q 1 4 輸出 2.

解題思路:每個節點記錄兩個資訊:最大值的個數以及這個點一共有多少個數字。

A simple simulation problem.Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 580    Accepted Submission(s): 227


Problem DescriptionThere are n types of cells in the lab, numbered from 1 to n. These cells are put in a queue, the i-th cell belongs to type i. Each time I can use mitogen to double the cells in the interval [l, r]. For instance, the original queue is {1 2 3 3 4 5}, after using a mitogen in the interval [2, 5] the queue will be {1 2 2 3 3 3 3 4 4 5}. After some operations this queue could become very long, and I can’t figure out maximum count of cells of same type. Could you help me? 
InputThe first line contains a single integer t (1 <= t <= 20), the number of test cases.

For each case, the first line contains 2 integers (1 <= n,m<= 50000) indicating the number of cell types and the number of operations.

For the following m lines, each line represents an operation. There are only two kinds of operations: Q and D. And the format is:

“Q l r”, query the maximum number of cells of same type in the interval [l, r];
“D l r”, double the cells in the interval [l, r];

(0 <= r – l <= 10^8, 1 <= l, r <= the number of all the cells) 
OutputFor each case, output the case number as shown. Then for each query "Q l r", print the maximum number of cells of same type in the interval [l, r].

Take the sample output for more details.
 
Sample Input
15 5D 5 5Q 5 6D 2 3D 1 2Q 1 7
 
Sample Output
Case #1:23
 
#include <stdio.h>#include <string.h>#include <math.h>#include <algorithm>#include <iostream>#define LL long longusing namespace std;const int maxn = 101000;LL num[maxn<<2];LL Max[maxn<<2];void Bulid(int l, int r, int site){    Max[site] = 1LL;    if(l == r)    {        num[site] = 1LL;        return;    }    int mid = (l+r)>>1;    Bulid(l, mid, site<<1);    Bulid(mid+1, r, site<<1|1);    num[site] = num[site<<1]+num[site<<1|1];}void Push_Up(int site){    Max[site] = max(Max[site<<1], Max[site<<1|1]);    num[site] = num[site<<1]+num[site<<1|1];}void Update(int l, int r, LL ll, LL rr, int site){    if(l == r)    {        num[site] += (rr-ll+1);        Max[site] = num[site];        return;    }    LL tmp = num[site<<1];    int mid = (l+r)>>1;    if(rr <= tmp) Update(l, mid, ll, rr, site<<1);    else if(ll > tmp) Update(mid+1, r, ll-tmp, rr-tmp, site<<1|1);    else    {        Update(l, mid, ll, tmp, site<<1);        Update(mid+1, r, 1, rr-tmp, site<<1|1);    }    Push_Up(site);}LL Query(int l, int r, LL ll, LL rr, int site){    if(num[site] == (rr-ll+1)) return Max[site];    if(l == r) return (rr-ll+1);    LL tmp = num[site<<1];    int mid = (l+r)>>1;    if(rr <= tmp) return Query(l, mid, ll, rr, site<<1);    else if(ll > tmp) return Query(mid+1, r, ll-tmp, rr-tmp, site<<1|1);    else return max(Query(l, mid, ll, tmp, site<<1), Query(mid+1, r, 1, rr-tmp, site<<1|1));}int main(){    int T;    int Case = 1;    cin >>T;    char str[110];    while(T--)    {        printf("Case #%d:\n", Case++);        int n, m;        scanf("%d %d",&n, &m);        Bulid(1, n, 1);        LL x, y;        while(m--)        {            scanf("%s",str);            scanf("%I64d %I64d",&x, &y);            if(str[0] == 'D') Update(1, n, x, y, 1);            else printf("%I64d\n",Query(1, n, x, y, 1));        }    }}


HDU 4973 A simple simulation problem.(線段樹)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.