hdu 5239__Doom

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
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
 

Source
 


題意:一共有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|1
const 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;
}



發佈了51 篇原創文章 · 獲贊 1 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章