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
 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值