您的位置:首页 > 其它

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

2017-05-05 16:36 253 查看

Doom

[b]Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)

Total Submission(s): 1488    Accepted Submission(s): 397
[/b]

[align=left]Problem Description[/align]
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 froml-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.

 

[align=left]Input[/align]
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<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.

 

[align=left]Output[/align]
For each test case, output ''Case #t:'', to represent this is thet-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.

 

[align=left]Sample Input[/align]

2
4 4
2 3 4 5
1 2
2 3
3 4
1 4
1 3
2
1 1
1 1
1 1

 

[align=left]Sample Output[/align]

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
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  线段树 hdu