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

本文介绍了一道涉及区间操作的问题,通过使用线段树来高效处理一系列区间内的数值变化及求和操作,并讨论了如何利用特殊模数特性简化运算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
  
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:\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;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI蜗牛之家

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值