zoukankan      html  css  js  c++  java
  • 上海大都会 H.A Simple Problem with Integers


    题目描述

    You have N integers A1, A2, ... , AN. You are asked to write a program to receive and execute two kinds of instructions:

    1. C a b means performing Ai = (Ai2 mod 2018) for all Ai such that a ≤ i ≤ b.
    2. Q a b means query the sum of Aa, Aa+1, ..., Ab. Note that the sum is not taken modulo 2018.

    输入描述:

    The first line of the input is T(1≤ T ≤ 20), which stands for the number of test cases you need to solve.
    The first line of each test case contains N (1 ≤ N ≤ 50000).The second line contains N numbers, the initial values of A1, A2, ..., An.  0 ≤ Ai < 2018. The third line contains the number of operations Q (0 ≤ Q ≤ 50000). The following Q lines represents an operation having the format "C a b" or "Q a b", which has been described above. 1 ≤ a ≤ b ≤ N.

    输出描述:

    For each test case, print a line "Case #t:" (without quotes, t means the index of the test case) at the beginning.
    You need to answer all Q commands in order. One answer in a line.

    分析

    刚开始打表算错了周期。。。又坑队友了
    每个元素平方最大周期为6,且6是其他所有周期的公倍数。我们可以在每个节点维护一个大小为6的数组,同时维护一个代表从数组中取哪个元素的指针。因为有的数在进入循环节前需要经过几次修改,打表发现进入循环节前的修改次数都不超过5,所以可以在前五次暴力更新节点,之后才开始利用线段树的lazy标记。

    代码

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <vector>
    #include <stack>
    #include <algorithm>
    using namespace std;
    typedef long long ll;
    const int mod=2018;
    const int maxn=50050;
    int cnt[maxn*4],sum[maxn*4][7],lazy[maxn*4],p[maxn*4];
    void build(int l,int r,int id) {
        cnt[id]=p[id]=lazy[id]=0;
        if(l==r) {
            scanf("%d", &sum[id][0]);
        }
        else {
            int mid=(l+r)>>1;
            build(l,mid,id<<1);
            build(mid+1,r,id<<1|1);
            sum[id][0]=sum[id<<1][0]+sum[id<<1|1][0];
        }
    }
    void pushUp(int id) {
        cnt[id]=min(cnt[id<<1],cnt[id<<1|1]);
        if(cnt[id]>=5) {
            p[id]=0;
            for (int i = 0; i < 6; ++i)
                sum[id][i]=sum[id<<1][(p[id<<1]+i)%6]+sum[id<<1|1][(p[id<<1|1]+i)%6];
        }
        else sum[id][0]=sum[id<<1][p[id<<1]]+sum[id<<1|1][p[id<<1|1]];
    }
    void pushDown(int id) {
        p[id<<1]=(p[id<<1]+lazy[id])%6,lazy[id<<1]+=lazy[id];
        p[id<<1|1]=(p[id<<1|1]+lazy[id])%6,lazy[id<<1|1]+=lazy[id];
        lazy[id]=0;
    }
    void modify(int x,int y,int l,int r,int id) {
        if(l>r||l>y||r<x) return;
        if(l==r) {
            cnt[id]++;
            if(cnt[id]<5) {
                sum[id][0]=sum[id][0]*sum[id][0]%mod;
            }
            else if(cnt[id]==5) {
                p[id]=0;
                sum[id][0]=sum[id][0]*sum[id][0]%mod;
                for (int i = 1; i < 6; ++i)
                {
                    sum[id][i]=sum[id][i-1]*sum[id][i-1]%mod;
                }
            }
            else {
                p[id]=(p[id]+1)%6;
            }
            return;
        }
        if(lazy[id]) pushDown(id);
        if(x<=l&&y>=r) {
            int mid=(l+r)>>1;
            if(cnt[id]<5) {
                modify(x,y,l,mid,id<<1);
                modify(x,y,mid+1,r,id<<1|1);
                pushUp(id);
            }
            else {
                lazy[id]++;
                p[id]=(p[id]+1)%6;
            }
            return;
        }
        int mid=(l+r)>>1;
        if(x<=mid) modify(x,y,l,mid,id<<1);
        if(y>mid) modify(x,y,mid+1,r,id<<1|1);
        pushUp(id);
    }
    int query(int x,int y,int l,int r,int id) {
        if(l!=r && lazy[id]) pushDown(id);
        if(l>=x&&r<=y) {
            return sum[id][p[id]%6];
        }
        int mid=(l+r)>>1;
        int ans=0;
        if(x<=mid) ans+=query(x,y,l,mid,id<<1);
        if(y>mid) ans+=query(x,y,mid+1,r,id<<1|1);
        return ans;
    }
    int main(int argc, char const *argv[])
    {
        int t,Case=0;
        scanf("%d", &t);
        while(t--)
        {
            int n;
            scanf("%d", &n);
            build(1,n,1);
            int q;
            scanf("%d", &q);
            printf("Case #%d:
    ", ++Case);
            while(q--)
            {
                char str[3];
                int l,r;
                scanf("%s%d%d", str,&l,&r);
                if(str[0]=='Q') {
                    printf("%d
    ", query(l,r,1,n,1));
                }
                else {
                    modify(l,r,1,n,1);
                }
            }
        }
        return 0;
    }
    
  • 相关阅读:
    PostMan-NewMan运行参数
    shell脚本学习简单记录笔记
    android开发okhttp-4.9.1源码大致流程解读
    android开发获取键盘高度以及判断键盘是否显示(兼容分屏模式)
    Android开发The style on this component requires your app theme to be Theme.AppCompat (or a descendant)的解决方法
    Linux开发Ubuntu安装man手册
    Android开发源码解读四大组件源码解读简单梳理
    Android开发涉及到的AMS类和ActivityThread类源码解读
    Android开发为什么主线程可以一直运行而不会退出来
    前端CryptoJS加密、后端解密代码实现参考
  • 原文地址:https://www.cnblogs.com/sciorz/p/9428114.html
Copyright © 2011-2022 走看看