Doom
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 1443 Accepted Submission(s): 378
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 ofn 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 thei -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 radixp ,
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.
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
There is a button on each cell. When the
Mike observes that the number is stored in radix
And now, Mike has got a list of operations. One operation is to push buttons between from
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 integersn,m (1≤n,m≤105 ).
The next line containsn integers ai (0≤ai<p ),
which means the initial values of the n cells.
The nextm lines
describe operations. In each line, there are two integers l,r(1≤l≤r≤n) ,
representing the operation.
For each test case, the first line contains two integers
The next line contains
The next
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.
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
Recommend
参考here
题意:
给出n个数和一个初始值为0的答案。每次操作给出一个区间[l,r],把区间所有的数加到答案中,之后把区间的每个数都平方。每次操作都需要输出答案 mod 9223372034707292160(2 ^ 63 - 2 ^ 31)
思路
这题的做法和 hdu 4027 类似
首先,应该想到这个题可以用线段树来维护区间的和,然后对于(2 ^ 63 - 2 ^ 31)这个数,即任意数的平方MOD此数,重复操作至多29次就会进入一个不变的数。
因此,可以在线段树维护时,用一个标记数组cover来维护。如果发现当前的是叶区间且当前数字的平方去模等于当前数字cover[o] = true。
向上维护cover[o] = cover[ls] & cover[rs]。表示向上维护的区间是否还需要更新。
#include <cstdio> #include <cstring> #include <algorithm> #define ls (o*2) #define rs (o*2+1) using namespace std; typedef unsigned __int64 ll; const int N = 100005; const ll MOD = 9223372034707292160ll; ll sumv[N<<2]; bool cover[N<<2]; int n, q; ll modmul(ll a, ll k) { ll c = 0; while(k) { if(k & 1) c = (c+a) % MOD; a = (a+a) % MOD; k >>= 1; } return c; } void build(int o, int L, int R) { sumv[o] = cover[o] = 0; if(L == R) { scanf("%I64u", &sumv[o]); return ; } int M = (L+R)/2; build(ls, L, M); build(rs, M+1, R); sumv[o] = (sumv[ls] + sumv[rs]) % MOD; } int ql, qr; ll query(int o, int L, int R) { if(ql <= L && R <= qr) { return sumv[o]; } int M = (L+R)/2; ll ret = 0; if(ql <= M) ret = (ret + query(ls, L, M)) % MOD; if(qr > M) ret = (ret + query(rs, M+1, R)) % MOD; return ret; } void modify(int o, int L, int R) { if(cover[o] && ql <= L && R <= qr) return ; if(L == R) { ll tmp = modmul(sumv[o], sumv[o]); if(tmp == sumv[o]) cover[o] = true; sumv[o] = tmp; return ; } int M = (L+R)/2; if(ql <= M) modify(ls, L, M); if(qr > M) modify(rs, M+1, R); cover[o] = cover[ls] & cover[rs]; sumv[o] = (sumv[ls] + sumv[rs]) % MOD; } int main() { int T, cas = 1; scanf("%d", &T); while(T--) { scanf("%d%d", &n, &q); build(1, 1, n); printf("Case #%d: ", cas++); ll ans = 0; while(q--) { scanf("%d%d", &ql, &qr); ans = (ans + query(1, 1, n)) % MOD; printf("%I64u ", ans); modify(1, 1, n); } } return 0; }