hdu5239线段树区间更新,区间平方,取模

Doom

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


 

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(1≤i≤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+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(T≤5), denoting the number of test cases.

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

The next line contains n integers ai(0≤ai
The next m lines describe operations. In each line, there are two integers l,r(1≤l≤r≤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

 

 

Source

The 2015 ACM-ICPC China Shanghai Metropolitan Programming Contest

 

 

Recommend

We have carefully selected several similar problems for you:  6577 6576 6575 6574 6573 

 

一个数一直平方,在平方到一定的次数以后,这个数对模数取模的值就不会改变了,做一个标记记录即可

#include
#include
#include
#include
#include
using namespace std;
#define maxn 1000005

#define lson i*2,l,m
#define rson i*2+1,m+1,r
typedef unsigned long long ll;
const ll mod=9223372034707292160ull;

ll sum[maxn*4];
int flag[maxn*4];
ll a[maxn];
int t;
int n;

int m;
void pushup(int i)
{
    sum[i]=(sum[i*2+1]%mod+sum[i*2]%mod)%mod;
    flag[i]=flag[i*2+1]&flag[i*2];
}
void build(int i,int l,int r)
{
    if(l==r)
    {
        sum[i]=a[l];
        flag[i]=0;
        return;
    }
    int m=(l+r)/2;
    build(lson);
    build(rson);
    pushup(i);
}
ll mul(ll a,ll b)
{
    ll ans=0;
    while(b)
    {
        if(b&1)
        {
            ans=(ans+a)%mod;
            b--;
        }

    b/=2;
    a=(a+a)%mod;

}
return ans;
}

ll query(int ql,int qr,int i,int l,int r)
{
    if(ql<=l&&r<=qr)
    return sum[i];
    int m=(l+r)/2;
    ll res=0;
    if(ql<=m)
    res=(res%mod+query(ql,qr,lson)%mod)%mod;
    if(m

 

你可能感兴趣的:(线段树)