hdu 5239 Doom - 线段树 +特殊取模


Doom

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


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


题意

给你n个按钮 m个操作
首先给出一行n个按钮的初始值。
然后m行操作 ql qr 表示从第ql个按钮按到qr,结果加上按钮上的值 % 9223372034707292160,操作完成之后相应按钮的值变为原来的平方

解题思路
明显的线段树,但是复杂度太高没法维护?
9223372034707292160  (2 ^ 63 - 2 ^ 31) 是一个特殊的数字,即任意数的平方MOD此数,重复操作至多29次就会进入一个不变的数。
所以只需要增加一个标记变量 change  如果平方后取模不再改变,说明以后都不会改变了 cnage = 1;
这里longlong WA了好久,全改成unsigned long long 就过了 = =  

代码

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
typedef unsigned long long ll;
const ll INF = 0x3f3f3f3f;
const ll maxn = 100000+10;
const ll mod = 9223372034707292160;
struct node{
    ll l,r;
    ll sum;
    bool change;
}tree[maxn*4];

ll val[maxn];

ll  mod_mul(ll x,ll y,ll mod){
    ll res = 0;
    while(y>0){
        if(y&1){res = (res+x) %mod;}
        y>>=1;
        x=(x+x)%mod;
    }
    return res;
}

void build(ll node,ll b,ll e){
    ll mid  = (b+e)/2;
    tree[node].l = b;
    tree[node].r = e;
    tree[node].change = 0;
    if(b == e) {
        tree[node].sum = val[b];
        return;
    }
    if(b <= mid) build(node*2,b,mid);
    if(e > mid) build(node*2+1,mid+1,e);
    tree[node].sum  = (tree[node*2+1].sum + tree[node*2].sum)%mod;

}

void update(ll node,ll ql,ll qr){
    if(tree[node].change ){
        return;
    }
    if(tree[node].l==tree[node].r ){
        ll temp = mod_mul(tree[node].sum,tree[node].sum,mod);
        if(tree[node].sum == temp) tree[node].change = 1;
        tree[node].sum = temp;
        return;
    }
    ll mid = (tree[node].l+tree[node].r)/2;
    if(ql <= mid) update(node*2,ql,qr);
    if(qr > mid) update(node*2+1,ql,qr);
    tree[node].sum = (tree[node*2].sum + tree[node*2+1].sum)%mod;
    tree[node].change = tree[node*2+1].change & tree[node*2].change;
}

ll query(ll node ,ll ql,ll qr){
    if(tree[node].l >= ql && tree[node].r<= qr){

        return tree[node].sum%mod;
    }
    ll mid  = (tree[node].l+tree[node].r)/2;
    ll  ans1 = 0 ,ans2 = 0;
    if(ql <= mid)  ans1 = query(node*2,ql,qr)%mod;
    if(qr > mid) ans2 = query(node*2+1,ql,qr)%mod;
    return (ans1+ans2)%mod;
}

ll n,m;

int main(){
    ll t,ql,qr;
    scanf("%llu",&t);
    for(ll icase = 1; icase <= t;++icase){
        ll ans = 0;
        scanf("%llu%llu",&n,&m);
        for(ll i = 1;i<=n;++i){
            scanf("%llu",&val[i]);
        }
        build(1,1,n);
        printf("Case #%llu:\n",icase);
        for(ll i = 0;i<m;++i){
            scanf("%llu%llu",&ql,&qr);
            ans =(ans + query(1,ql,qr))%mod;
            printf("%llu\n",ans);
            update(1,ql,qr);
        }
    }
    return 0;
}
/*
2
4 4
2 3 4 5
1 2
2 3
3 4
1 4
1 3
2
1 1
1 1
1 1
*/

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值