HDU-4973-A simple simulation problem. (Binary + tree array)

Source: Internet
Author: User
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 3 4 4 5 }. after some operations this queue cocould become V Ery 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
 
Source2014 multi-university training contest 10
Idea: Perform binary search directly when finding a location. The two arrays Save the interval and the single point count respectively.
#include <stdio.h>#define max(A,B)(A>B?A:B)long long node[50005],cnt[50005];int n,m;long long sum(int x){    long long res=0;    while(x>=1)    {        res+=node[x];        x-=x&-x;    }    return res;}void add(int x,long long val){    while(x<=n)    {        node[x]+=val;        x+=x&-x;    }}int getpos(long long val){    int l=1,r=n,mid,res;    while(l<=r)    {        mid=(l+r)>>1;        if(sum(mid)>=val)        {            res=mid;            r=mid-1;        }        else l=mid+1;    }    return res;}int main(){    int T,i,l,r,cases=1;    long long a,b,tempa,tempb,ans;    char s[5];    scanf("%d",&T);    while(T--)    {        scanf("%d%d",&n,&m);        printf("Case #%d:\n",cases++);        for(i=1;i<=n;i++) node[i]=0,cnt[i]=1;        for(i=1;i<=n;i++) add(i,1);        while(m--)        {            scanf("%s%I64d%I64d",s,&a,&b);            if(s[0]=='D')            {                l=getpos(a);                r=getpos(b);                if(l==r)                {                    cnt[l]+=b-a+1;                    add(l,b-a+1);                }                else                {                    tempa=sum(l)-a+1;                    tempb=b-sum(r-1);                    cnt[l]+=tempa;                    add(l,tempa);                    cnt[r]+=tempb;                    add(r,tempb);                    for(i=l+1;i<=r-1;i++)                    {                        add(i,cnt[i]);                        cnt[i]+=cnt[i];                    }                }            }            else            {                l=getpos(a);                r=getpos(b);                if(l==r) printf("%I64d\n",b-a+1);                else                {                    ans=max(sum(l)-a+1,b-sum(r-1));                    for(i=l+1;i<=r-1;i++) ans=max(ans,cnt[i]);                    printf("%I64d\n",ans);                }            }        }    }}


HDU-4973-A simple simulation problem. (Binary + tree array)

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.