上海大都會 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:\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;
}

轉載于:https://www.cnblogs.com/sciorz/p/9428114.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/388911.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/388911.shtml
英文地址,請注明出處:http://en.pswp.cn/news/388911.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

探索性數據分析入門_入門指南:R中的探索性數據分析

探索性數據分析入門When I started on my journey to learn data science, I read through multiple articles that stressed the importance of understanding your data. It didn’t make sense to me. I was naive enough to think that we are handed over data which we p…

用Javascript代碼實現瀏覽器菜單命令(以下代碼在 Windows XP下的瀏覽器中調試通過

每當我們看到別人網頁上的打開、打印、前進、另存為、后退、關閉本窗口、禁用右鍵等實現瀏覽器命令的鏈接&#xff0c;而自己苦于不能實現時&#xff0c;是不是感到很遺憾&#xff1f;是不是也想實現&#xff1f;如果能在網頁上能實現瀏覽器的命令&#xff0c;將是多么有意思的…

mysql程序設計教程_MySQL教程_編程入門教程_牛客網

MySQL 索引MySQL索引的建立對于MySQL的高效運行是很重要的&#xff0c;索引可以大大提高MySQL的檢索速度。打個比方&#xff0c;如果合理的設計且使用索引的MySQL是一輛蘭博基尼的話&#xff0c;那么沒有設計和使用索引的MySQL就是一個人力三輪車。拿漢語字典的目錄頁(索引)打比…

學習筆記整理之StringBuffer與StringBulider的線程安全與線程不安全

關于線程和線程不安全&#xff1a; 概述 編輯 如果你的代碼所在的進程中有多個線程在同時運行&#xff0c;而這些線程可能會同時運行這段代碼。如果每次運行結果和單線程運行的結果是一樣的&#xff0c;而且其他的變量的值也和預期的是一樣的&#xff0c;就是線程安全的。或者說…

python web應用_為您的應用選擇最佳的Python Web爬網庫

python web應用Living in today’s world, we are surrounded by different data all around us. The ability to collect and use this data in our projects is a must-have skill for every data scientist.生活在當今世界中&#xff0c;我們周圍遍布著不同的數據。 在我們的…

NDK-r14b + FFmpeg-release-3.4 linux下編譯FFmpeg

下載資源 官網下載完NDK14b 和 FFmpeg 下載之后&#xff0c;更改FFmpeg 目錄下configure問價如下&#xff1a; SLIBNAME_WITH_MAJOR$(SLIBPREF)$(FULLNAME)-$(LIBMAJOR)$(SLIBSUF) LIB_INSTALL_EXTRA_CMD$$(RANLIB)"$(LIBDIR)/$(LIBNAME)" SLIB_INSTALL_NAME$(SLI…

C# WebBrowser自動填表與提交

C# WebBrowser自動填表與提交 默認分類 2007-04-18 15:47:17 閱讀57 評論0 字號&#xff1a;大中小 訂閱 要使我們的WebBrowser具有自動填表、甚至自動提交的功能&#xff0c;并不困難。   假設有一個最簡單的登錄頁面&#xff0c;輸入用戶名密碼&#xff0c;點“登錄”…

html中列表導航怎么和圖片對齊_HTML實戰篇:html仿百度首頁

本篇文章主要給大家介紹一下如何使用htmlcss來制作百度首頁頁面。1)制作頁面所用的知識點我們首先來分析一下百度首頁的頁面效果圖百度首頁由頭部的一個文字導航&#xff0c;中間的一個按鈕和一個輸入框以及下邊的文字簡介和導航組成。我們這里主要用到的知識點就是列表標簽(ul…

C# 依賴注入那些事兒

原文地址&#xff1a;http://www.cnblogs.com/leoo2sk/archive/2009/06/17/1504693.html 里面有一個例子差了些代碼&#xff0c;補全后貼上。 3.1.3 依賴獲取 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml;//定義…

asp.net core Serilog的使用

先貼上關于使用這個日志組件的一些使用方法&#xff0c;等有時間了在吧官方的文檔翻譯一下吧&#xff0c;現在真是沒時間。 Serilog在使用上主要分為兩大塊&#xff1a; 第一塊是主庫&#xff0c;包括Serilog以及Serilog.AspNetCore&#xff0c;如果導入后一個的話會自動導入前…

在FAANG面試中破解堆算法

In FAANG company interview, Candidates always come across heap problems. There is one question they do like to ask — Top K. Because these companies have a huge dataset and they can’t always go through all the data. Finding tope data is always a good opti…

android webView的緩存機制和資源預加載

android 原生使用WebView嵌入H5頁面 Hybrid開發 一、性能問題 android webview 里H5加載速度慢網絡流量大 1、H5頁面加載速度慢 渲染速度慢 js解析效率 js本身的解析過程復雜、解析速度不快&#xff0c;前端頁面設計較多的js代碼文件 手機硬件設備的性能 機型多&#xff0c;…

mysql springboot 緩存_Spring Boot 整合 Redis 實現緩存操作

摘要: 原創出處 www.bysocket.com 「泥瓦匠BYSocket 」歡迎轉載&#xff0c;保留摘要&#xff0c;謝謝&#xff01;『 產品沒有價值&#xff0c;開發團隊再優秀也無濟于事 – 《啟示錄》 』本文提綱一、緩存的應用場景二、更新緩存的策略三、運行 springboot-mybatis-redis 工程…

http壓力測試工具及使用說明

http壓力測試工具及使用說明 轉 說明&#xff1a;介紹幾款簡單、易使用http壓測工具&#xff0c;便于研發同學&#xff0c;壓測服務&#xff0c;明確服務臨界值&#xff0c;尋找服務瓶頸點。 壓測時候可重點以下指標&#xff0c;關注并發用戶數、TPS&#xff08;每秒事務數量&a…

itchat 道歉_人類的“道歉”

itchat 道歉When cookies were the progeny of “magic cookies”, they were seemingly innocuous packets of e-commerce data that stored a user’s partial transaction state on their computer. It wasn’t disclosed that you were playing a beneficial part in a muc…

使用Kubespray部署生產可用的Kubernetes集群(1.11.2)

Kubernetes的安裝部署是難中之難&#xff0c;每個版本安裝方式都略有區別。筆者一直想找一種支持多平臺 、相對簡單 、適用于生產環境 的部署方案。經過一段時間的調研&#xff0c;有如下幾種解決方案進入筆者視野&#xff1a; 部署方案優點缺點Kubeadm官方出品部署較麻煩、不夠…

android webView 與 JS交互方式

webView 與JS交互 Android調用JS代碼的方法有&#xff1a; 通過WebView的loadUrl&#xff08;&#xff09;通過WebView的evaluateJavascript&#xff08;&#xff09; 對于JS調用Android代碼的方法有3種&#xff1a; 通過WebView的addJavascriptInterface&#xff08;&…

matlab軟件imag函數_「復變函數與積分變換」基本計算代碼

使用了Matlab代碼&#xff0c;化簡平時遇到的計算問題&#xff0c;也可以用于驗算結果來自211工科專業2學分復變函數與積分變換課程求復角主值sym(angle(待求復數))%公式 sym(angle(1sqrt(3)*i))%舉例代入化簡將 代入關于z的函數f(z)中并化解&#xff0c;用于公式法計算無窮遠點…

數據科學 python_為什么需要以數據科學家的身份學習Python的7大理由

數據科學 pythonAs a new Data Scientist, you know that your path begins with programming languages you need to learn. Among all languages that you can select from Python is the most popular language for all Data Scientists. In this article, I will cover 7 r…

[luoguP4142]洞穴遇險

https://www.zybuluo.com/ysner/note/1240792 題面 戳我 解析 這種用來拼接的奇形怪狀的東西&#xff0c;要不就是輪廓線\(DP\)&#xff0c;要不就是網絡流。 為了表示奇數點&#xff08;即\((xy)\%21\)&#xff09;的危險值&#xff0c;把該點拆為兩個點&#xff0c;連一條邊長…