程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDU 4973 A simple simulation problem.(線段樹)

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

編輯:C++入門知識

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


題目大意: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 Description There 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?
Input The 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)
Output For 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
1
5 5
D 5 5
Q 5 6
D 2 3
D 1 2
Q 1 7

Sample Output
Case #1:
2
3
#include 
#include 
#include 
#include 
#include 
#define LL long long


using 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));
        }
    }
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved