日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

上海大都会 H.A Simple Problem with Integers

發(fā)布時(shí)間:2023/11/30 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 上海大都会 H.A Simple Problem with Integers 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目描述

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

  • C a b means performing Ai = (Ai2 mod 2018) for all Ai such that a ≤ i ≤ b.
  • 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.

    分析

    剛開(kāi)始打表算錯(cuò)了周期。。。又坑隊(duì)友了
    每個(gè)元素平方最大周期為6,且6是其他所有周期的公倍數(shù)。我們可以在每個(gè)節(jié)點(diǎn)維護(hù)一個(gè)大小為6的數(shù)組,同時(shí)維護(hù)一個(gè)代表從數(shù)組中取哪個(gè)元素的指針。因?yàn)橛械臄?shù)在進(jìn)入循環(huán)節(jié)前需要經(jīng)過(guò)幾次修改,打表發(fā)現(xiàn)進(jìn)入循環(huán)節(jié)前的修改次數(shù)都不超過(guò)5,所以可以在前五次暴力更新節(jié)點(diǎn),之后才開(kāi)始利用線段樹(shù)的lazy標(biāo)記。

    代碼

    #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:\n", ++Case);while(q--){char str[3];int l,r;scanf("%s%d%d", str,&l,&r);if(str[0]=='Q') {printf("%d\n", query(l,r,1,n,1));}else {modify(l,r,1,n,1);}}}return 0; }

    轉(zhuǎn)載于:https://www.cnblogs.com/sciorz/p/9428114.html

    創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

    總結(jié)

    以上是生活随笔為你收集整理的上海大都会 H.A Simple Problem with Integers的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。