HDU 5239 Doom(线段树)

Doom
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 1547 Accepted Submission(s): 426
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
he 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).
he next line contains n integers ai(0≤ai < 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≤l≤r≤n), representing the operation.
OutputFor 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
2
题意:有一个长度为n的序列。每次询问输出区间l,r内数的平方和。然后将数平方。
题解:mod数是关键,发现一个数经过多次平方后,%mod将不再变化。所以线段树正常维护的同时再加一个标记来标记是否数字不再变化。
代码:

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<algorithm>
#include<map>
#include<time.h>
#include<math.h>
//#define pb push_back
//#define mp make_pair
#define INF 0x3f3f3f3f
using namespace std;
typedef unsigned __int64 ll;
typedef pair<int,int>pp;
const int N=1e5+100;
const ll mod=9223372034707292160ll;
int read()
{
    int x=0;
    char ch = getchar();
    while('0'>ch||ch>'9')ch=getchar();
    while('0'<=ch&&ch<='9')
    {
        x=(x<<3)+(x<<1)+ch-'0';
        ch=getchar();
    }
    return x;
}
/***********************************************************/
ll sum[N<<2];
int vis[N<<2];
int t,n,m,x,y;
void pushup(int rt)
{
    sum[rt]=(sum[rt<<1]+sum[rt<<1|1])%mod;
    vis[rt]=vis[rt<<1]&vis[rt<<1|1];
}
ll qmul(ll a,ll b)
{
    ll res = 0;
    while(b){
        if(b&1) res=(res+a)%mod;
        b>>=1,a=(a+a)%mod;
    }
    return res;
}
void build(int l,int r,int rt)
{
    if(l==r)
    {
        scanf("%I64u",&sum[rt]);
        vis[rt]=0;
        return ;
    }
    int mid=(r+l)>>1;
    build(l,mid,rt<<1);
    build(mid+1,r,rt<<1|1);
    pushup(rt);
}
void update(int l,int r,int L,int R,int rt)
{
    if(vis[rt]&&L<=l&&R>=r) return ;
    if(l==r)
    {
        ll tmp=qmul(sum[rt],sum[rt]);
        if(tmp==sum[rt]) vis[rt]=1;
        sum[rt]=tmp;
        return ;
    }
    int mid=(r+l)>>1;
    if(L<=mid) update(l,mid,L,R,rt<<1);
    if(R>mid) update(mid+1,r,L,R,rt<<1|1);
    pushup(rt);
}
ll query(int l,int r,int L,int R,int rt)
{
    if(L<=l&&R>=r)
    {
        return sum[rt];
    }
    int mid=(r+l)>>1;
    ll ret=0;
    if(L<=mid) ret=(ret+query(l,mid,L,R,rt<<1))%mod;
    if(R>mid) ret=(ret+query(mid+1,r,L,R,rt<<1|1))%mod;
    return ret;
}
int main()
{
    scanf("%d",&t);
    int cas=1;
    while(t--)
    {
        memset(vis,0,sizeof(vis));
        memset(sum,0,sizeof(sum));
        scanf("%d%d",&n,&m);
        build(1,n,1);
        printf("Case #%d:\n",cas++);
        ll ans=0;
        while(m--)
        {
            scanf("%d%d",&x,&y);
            ans=(ans+query(1,n,x,y,1))%mod;
            printf("%llu\n",ans);
            update(1,n,x,y,1);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值