hdu 5239__Doom

来源:互联网 发布:windows无法访问\\ 编辑:程序博客网 时间:2024/04/28 10:57

Doom

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 705    Accepted Submission(s): 171


Problem 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 ai(1in). 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+ai, where s is the original number. and the number on the i-th cell will be changed to a2i.

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(T5), denoting the number of test cases.

For each test case, the first line contains two integers n,m(1n,m105).

The next line contains n integers ai(0ai<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(1lrn), 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
24 42 3 4 51 22 33 41 41 321 11 11 1
 

Sample Output
Case #1:51839405Case #2:2622
 

Source
The 2015 ACM-ICPC China Shanghai Metropolitan Programming Contest
 


题意:一共有n个门,每个门都有一个权值,每次能打开从 l 到 r 的门,有个屏幕能在打开这些门之后把这些门里的值累计起来。每打开一扇门,这个门的权值就变为自身的平方。所有的值对p=9223372034707292160取余即是最后答案。

想法:利用线段树来更新区间的值并求和。看了题解才发现一个数自身平方到29次对p的取余就会不变。


代码如下:

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1const int maxn=100000+100;typedef unsigned long long ll;const ll mod=9223372034707292160;ll sum[maxn<<2];int cnt[maxn<<2];void pushup(int rt){    sum[rt]=(sum[rt<<1]+sum[rt<<1|1])%mod;    cnt[rt]=min(cnt[rt<<1],cnt[rt<<1|1]);}void build(int l,int r,int rt){    if(l==r) {        scanf("%I64u",&sum[rt]);        cnt[rt]=0;        return ;    }    int m=(l+r)>>1;    build(lson);    build(rson);    pushup(rt);}ll query(int L,int R,int l,int r,int rt){    if(L<=l&&r<=R) {        return sum[rt];    }    ll cnt=0;    int m=(l+r)>>1;    if(L<=m) cnt+=query(L,R,lson);    if(cnt>mod) cnt-=mod;    if(R>m)  cnt+=query(L,R,rson);    if(cnt>mod) cnt-=mod;    return cnt;}ll quick_add(ll a,ll n){    if(n==0)        return 0;    ll 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 update(int L,int R,int l,int r,int rt){    if(cnt[rt]>=30) {        return ;    }    if(l==r) {        sum[rt]=quick_add(sum[rt],sum[rt]);        cnt[rt]++;        return ;    }    int m=(l+r)>>1;    if(L<=m) {        update(L,R,lson);    }    if(R>m) {        update(L,R,rson);    }    pushup(rt);}int main(){    int T,L,R,n,m,cas=1;    scanf("%d",&T);    while(T--) {        scanf("%d%d",&n,&m);        build(1,n,1);        ll ans=0;        printf("Case #%d:\n",cas++);        for(int i=0;i<m;i++) {            scanf("%d%d",&L,&R);            ans+=query(L,R,1,n,1);            if(ans>=mod)                ans-=mod;            printf("%I64u\n",ans);            update(L,R,1,n,1);        }    }    return 0;}



0 0