HDU 5239 Doom

思路:打个表之后发现最多操作29次,之后区间的值就不会再改变,然后就是一个线段树了


#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define maxn 100005 + 10
#define mod 9223372034707292160ULL
#define lson i<<1,l,m
#define rson (i<<1)|1,m+1,r
ULL a[maxn << 2];
int cnt[maxn << 2];
ULL sum[maxn << 2];

ULL quick_add(ULL a, ULL n){
    if(n == 0)
        return 0;
    ULL sum = quick_add(a, n/2);
    sum = (sum+sum);
    if(sum >= mod)
        sum -= mod;
    if(n & 1)
        sum = (sum + a);
    if(sum >= mod)
        sum -= mod;
    return sum;
}

void push_up(int i){
    sum[i] = (sum[i << 1] + sum[i << 1 | 1]) % mod;
    cnt[i] = min(cnt[i << 1], cnt[i << 1 | 1]);
}

void build(int i,int l,int r)
{
    if(l == r)
	{
        cnt[i] = 0;
        sum[i] = a[l];
        return ;
    }
    int m = (l + r) >> 1;
    build(lson);
    build(rson);
    push_up(i);
}

ULL query(int ql,int qr,int i,int l,int r)
{
    if(ql <= l && qr >= r)
        return sum[i];
    int m = (l + r) >> 1;
    ULL s = 0;
    if(ql <= m) s = (s + query(ql,qr,lson));
    if(s >= mod) s -= mod;
    if(qr > m)  s = (s + query(ql,qr,rson));
    if(s >= mod) s -= mod;
    return s;
}

void update(int ql,int qr,int i,int l,int r)
{
    if(cnt[i] >= 30)
        return ;
    if(l == r)
	{
        sum[i] = quick_add(sum[i], sum[i]);
        cnt[i]++;
        return ;
    }
    int m = (l + r) >> 1;
    if(ql <= m)
        update(ql,qr,lson);
    if(qr> m)
        update(ql,qr,rson);
    push_up(i);
}

int main ()
{
    int t;
    int cas = 1;
    scanf("%d", &t);
    while(t--)
	{
        int n, q;
        scanf("%d%d", &n, &q);
        for(int i = 1; i <= n; i++)
            scanf("%llu", &a[i]);
        build(1, 1,n);
        ULL s = 0;
        printf("Case #%d:\n", cas++);
        while(q--)
		{
            int l, r;
            scanf("%d%d", &l, &r);
            s += query(l,r,1,1,n);
            if(s >= mod) s -= mod;
            printf("%llu\n", s);
            update(l,r,1,1,n);
        }
    }
    return 0;
}


Description

THE END IS COMINGGGGGG! 

Mike has got stuck on a mystery machine. If he cannot solve this problem, he will go to his doom. 

This machine is consist of $n$ cells, and a screen. The $i$-th cell contains a number $a_i(1 \leq i \leq n)$. The screen also contains a number $s$, which is initially $0$. 

There is a button on each cell. When the $i$-th is pushed, Mike observes that, the number on the screen will be changed to $s + a_i$, where $s$ is the original number. and the number on the $i$-th cell will be changed to $a_i^2$. 

Mike observes that the number is stored in radix $p$, where $p = 9223372034707292160$. In other words  , the operation is under modulo $p$. 

And now, Mike has got a list of operations. One operation is to push buttons between from $l$-th to $r$-th (both included), and record the number on the screen. He is tired of this stupid work, so he asks for your help. Can you tell him, what are the numbers recorded. 

 

Input

The first line contains an integer $T$($T \le 5$), denoting the number of test cases. 

For each test case, the first line contains two integers $n, m$($1 \leq n, m\leq 10^5$). 

The next line contains $n$ integers $a_i$($0 \leq a_i < p$), which means the initial values of the $n$ cells. 

The next $m$ lines describe operations. In each line, there are two integers $l, r (1 \leq l \leq r \leq n)$, representing the operation. 

 

Output

For each test case, output ''Case #t:'', to represent this is the $t$-th case. And then output the answer for each query operation, one answer in a line. 

For more details you can take a look at the example. 
 

Sample Input

        
        
        
        
2 4 4 2 3 4 5 1 2 2 3 3 4 1 4 1 3 2 1 1 1 1 1 1
 

Sample Output

        
        
        
        
Case #1: 5 18 39 405 Case #2: 2 6 22
 


你可能感兴趣的:(HDU 5239 Doom)