hust 5239 Doom(线段树 规律OR数论 待整理 )

来源:互联网 发布:广州淘宝拍照的地方 编辑:程序博客网 时间:2024/06/10 19:16

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 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
24 42 3 4 51 22 33 41 41 321 11 11 1
 

Sample Output
Case #1:51839405Case #2:2622
 

Source
The 2015 ACM-ICPC China Shanghai Metropolitan Programming Contest
 

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:\n", cas++);        ll ans = 0;        while(q--) {            scanf("%d%d", &ql, &qr);            ans = (ans + query(1, 1, n)) % MOD;            printf("%I64u\n", ans);            modify(1, 1, n);        }    }    return 0;}





0 0
原创粉丝点击