zoukankan      html  css  js  c++  java
  • hust 5239 Doom(线段树 规律OR数论 待整理 )

    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:
    ", 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;
    }





  • 相关阅读:
    php数组的使用
    php数组的定义、php数组的类型
    小米手机安卓手机微信里出现意外的边框线,border:0也没用
    php实现WEB在线文件管理器
    安装了https ssl证书,但浏览器显示并非完全安全(此页面内容部分不安全)
    thinkphp无限分类模块实现
    常见的移动web问题,终端触摸交互,各种bug坑如何解决
    现代都市风 移动端可折叠导航菜单
    电器类电商网站分类大菜单,配色超舒服~
    帮助中心模板框架--简约小清新风格
  • 原文地址:https://www.cnblogs.com/zswbky/p/6792858.html
Copyright © 2011-2022 走看看