HDU5239.Doom——线段树

http://acm.hdu.edu.cn/showproblem.php?pid=5239

转:http://blog.csdn.net/zava_1087/article/details/46040747
任意数的平方,最多29次模上p,都会不变

#include <bits/stdc++.h>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

const int maxn = 110000;

using namespace std;
typedef unsigned long long LL;
const LL mod = 9223372034707292160LL;
int n,q,cnt[maxn<<2];
LL sum[maxn<<2];
LL mulmod(LL x,LL y,LL mod)
{
    LL ret = 0;
    __asm__("movq %1,%%rax\n imulq %2\n idivq %3\n":"=d"(ret):"m"(x),"m"(y),"m"(mod):"%rax");
    return ret;
}
void push_up(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("%I64d",&sum[rt]);
        cnt[rt]=0;
        return ;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    push_up(rt);
}
void update(int L,int R,int l,int r,int rt)
{
    if(cnt[rt]==30) return ;
    if(l==r){
        sum[rt]=mulmod(sum[rt],sum[rt],mod);
//        sum[rt]=(sum[rt]*sum[rt])%mod;
        cnt[rt]++;
        return ;
    }
    int m=(l+r)>>1;
    if(L<=m) update(L,R,lson);
    if(m<R) update(L,R,rson);
    push_up(rt);
}
LL query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&r<=R){
        return sum[rt];
    }
    int m=(l+r)>>1;
    LL ret=0;
    if(L<=m)
        ret=(ret+query(L,R,lson))%mod;
    if(m<R)
        ret=(ret+query(L,R,rson))%mod;
    return ret;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif // ONLINE_JUDGE
    int T,cas=1;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&q);
        build(1,n,1);
        LL sum=0;
        printf("Case #%d:\n", cas++);
        while(q--){
            int l,r;
            scanf("%d%d",&l,&r);
            sum=(sum+query(l,r,1,n,1))%mod;
            update(l,r,1,n,1);
            printf("%I64d\n",sum);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值