D. The Child and Sequence
time limit per test

4 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite sequence of Picks.

Fortunately, Picks remembers how to repair the sequence. Initially he should create an integer array a[1], a[2], ..., a[n]. Then he should perform a sequence of m operations. An operation can be one of the following:

  1. Print operation l, r. Picks should write down the value of .
  2. Modulo operation l, r, x. Picks should perform assignment a[i] = a[imod x for each i (l ≤ i ≤ r).
  3. Set operation k, x. Picks should set the value of a[k] to x (in other words perform an assignment a[k] = x).

Can you help Picks to perform the whole sequence of operations?

Input

The first line of input contains two integer: n, m (1 ≤ n, m ≤ 105). The second line contains n integers, separated by space: a[1], a[2], ..., a[n] (1 ≤ a[i] ≤ 109) — initial value of array elements.

Each of the next m lines begins with a number type .

  • If type = 1, there will be two integers more in the line: l, r (1 ≤ l ≤ r ≤ n), which correspond the operation 1.
  • If type = 2, there will be three integers more in the line: l, r, x (1 ≤ l ≤ r ≤ n; 1 ≤ x ≤ 109), which correspond the operation 2.
  • If type = 3, there will be two integers more in the line: k, x (1 ≤ k ≤ n; 1 ≤ x ≤ 109), which correspond the operation 3.
Output

For each operation 1, please print a line containing the answer. Notice that the answer may exceed the 32-bit integer.

Sample test(s)
Input
5 5 1 2 3 4 5 2 3 5 4 3 3 5 1 2 5 2 1 3 3 1 1 3
Output
8 5
Input
10 10 6 9 6 7 6 1 10 10 9 5 1 3 9 2 7 10 9 2 5 10 8 1 4 7 3 3 7 2 7 9 9 1 2 4 1 6 6 1 5 9 3 1 10
Output
49 15 23 1 9
Note

Consider the first testcase:

  • At first, a = {1, 2, 3, 4, 5}.
  • After operation 1, a = {1, 2, 3, 0, 1}.
  • After operation 2, a = {1, 2, 5, 0, 1}.
  • At operation 3, 2 + 5 + 0 + 1 = 8.
  • After operation 4, a = {1, 2, 2, 0, 1}.
  • At operation 5, 1 + 2 + 2 = 5.

注意:剪枝:若x<mod,x=x;

 #include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map> #define N 100010
#define M 15
//#define mod 1000000007
//#define mod2 100000000
#define ll long long
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int n,q;
int x[N];
int type;
int a,b,c; struct node
{
ll sum;
int ma;
}tree[*N];
/*
void update(int l,int r,int i)
{
if(!tree[i].mark) return;
int mid=(l+r)/2;
tree[2*i].sum+=tree[i].mark*(ll)(mid-l+1);
tree[2*i+1].sum+=tree[i].mark*(ll)(r-mid);
tree[2*i].mark+=tree[i].mark;
tree[2*i+1].mark+=tree[i].mark;
tree[i].mark=0; }*/ ll query(int tl,int tr,int l,int r,int i)
{
if(tl>r || tr<l)
return ;
if(tl<=l && r<=tr)
return tree[i].sum;
// update(l,r,i);
int mid=(l+r)/; return query(tl,tr,l,mid,*i)+query(tl,tr,mid+,r,*i+);
}
/*
void addd(int tl,int tr,int l,int r,int i,int val)
{
if(tl>r || tr<l)
return;
if(tl<=l && tr>=r){
tree[i].sum+=val*(ll)(r-l+1);
tree[i].mark+=val;
return;
}
update(l,r,i);
int mid=(l+r)/2;
addd(tl,tr,l,mid,2*i,val);
addd(tl,tr,mid+1,r,2*i+1,val);
tree[i].sum=tree[2*i].sum+tree[2*i+1].sum;
}*/ void modefiyMod(int tl,int tr, int l, int r,int i,int mod)
{
if(tree[i].ma<mod) return;
if(l==r){
tree[i].sum=tree[i].ma=tree[i].sum%mod;
return;
}
else{
int mid=(l+r)/;
if(tr<=mid){
modefiyMod(tl,tr,l,mid,i*,mod);
}
else if(tl>mid){
modefiyMod(tl,tr,mid+,r,i*+,mod);
}
else{
modefiyMod(tl,tr,l,mid,i*,mod);
modefiyMod(tl,tr,mid+,r,i*+,mod);
}
tree[i].sum=tree[*i].sum+tree[*i+].sum;
tree[i].ma=max(tree[*i].ma,tree[*i+].ma);
}
} void setVal(int i, int l, int r,int x,int v)
{
if(l==r){
tree[i].sum=tree[i].ma=v;
return;
}
else{
int mid=(l+r)/;
if(x<=mid){
setVal(i*,l,mid,x,v);
}
else{
setVal(i*+,mid+,r,x,v);
}
tree[i].sum=tree[*i].sum+tree[*i+].sum;
tree[i].ma=max(tree[*i].ma,tree[*i+].ma);
}
} void build_tree(int l,int r,int i)
{
if(l==r){
tree[i].sum=x[l];
tree[i].ma=x[l];
return;
}
int mid=(l+r)/;
build_tree(l,mid,*i);
build_tree(mid+,r,*i+);
tree[i].sum=tree[*i].sum+tree[*i+].sum;
tree[i].ma=max(tree[*i].ma,tree[*i+].ma);
} int main()
{
int i;
//freopen("data.in","r",stdin);
//scanf("%d",&T);
//for(int cnt=1;cnt<=T;cnt++)
//while(T--)
while(scanf("%d%d",&n,&q)!=EOF)
{
for(i=;i<=n;i++) scanf("%d",&x[i]);
memset(tree,,sizeof(tree));
build_tree(,n,);
while(q--){
scanf("%d",&type);
if(type==){
scanf("%d%d",&a,&b);
ll ans=query(a,b,,n,);
printf("%I64d\n",ans); }
else if(type==){
scanf("%d%d%d",&a,&b,&c);
modefiyMod(a, b, , n, , c);
}
else{
scanf("%d%d",&a,&b);
setVal(, , n, a, b);
}
}
} return ;
}

CodeForces 438D 线段树 剪枝的更多相关文章

  1. Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论

    Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论 题意 给你一段数,然后小明去猜某一区间内的gcd,这里不一定是准确值,如果在这个区间内改变 ...

  2. 对权值线段树剪枝的误解--以HDU6703为例

    引子 对hdu6703,首先将问题转化为"询问一个排列中大于等于k的值里,下标超过r的最小权值是多少" 我们采用官方题解中的做法:权值线段树+剪枝 对(a[i],i)建线段树,查询 ...

  3. Codeforces 444 C. DZY Loves Colors (线段树+剪枝)

    题目链接:http://codeforces.com/contest/444/problem/C 给定一个长度为n的序列,初始时ai=i,vali=0(1≤i≤n).有两种操作: 将区间[L,R]的值 ...

  4. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem E (Codeforces 831E) - 线段树 - 树状数组

    Vasily has a deck of cards consisting of n cards. There is an integer on each of the cards, this int ...

  5. HDOJ:6356-Glad You Came(线段树剪枝)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6356 解题心得: 现在深深的知道了算法复杂度的重要了,这个题算复杂度的时候还要把一些常数也算出来,不然 ...

  6. LibreOJ #6190. 序列查询(线段树+剪枝)

    莫队貌似是过不了的,这题是我没见过的科技... 首先区间按右端点排序,然后一个扫描线,扫到某个区间右端点时候计算答案,线段树上节点的信息并不需要明确定义,我们只要求线段树做到当前扫到now时,查询[L ...

  7. HDU4391(线段树+剪枝)

    Paint The Wall Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. Codeforces 938G 线段树分治 线性基 可撤销并查集

    Codeforces 938G Shortest Path Queries 一张连通图,三种操作 1.给x和y之间加上边权为d的边,保证不会产生重边 2.删除x和y之间的边,保证此边之前存在 3.询问 ...

  9. codeforces 1136E 线段树

    codeforces 1136E: 题意:给你一个长度为n的序列a和长度为n-1的序列k,序列a在任何时候都满足如下性质,a[i+1]>=ai+ki,如果更新后a[i+1]<ai+ki了, ...

随机推荐

  1. initWithNibName/awakeFromNib/initWithCoder

    转自: http://leeyin.iteye.com/blog/1040362 每个ios开发者对loadView和viewDidLoad肯定都很熟悉,虽然这两个函数使用上真的是非常简单,但是和类似 ...

  2. 2018 北京区域赛 I - Palindromes (找规律)

    题目 HihoCoder - 1878 题目大意 给出k,让求出第k个回文数(k的“长度”不超过1e5) 题解 之前做过类似的题,是统计各阶段的数找到第K个回文数,但这里K太大,需要寻找新的方法. 打 ...

  3. 并查集+思维——The Door Problem

    一.问题描述(题目链接) 有n个门和m个开关,每个开关可以控制任意多的门,每个门严格的只有两个开关控制,问能否通过操作某些开关使得所有门都打开.(给出门的初始状态). 二.问题分析 大部分开关问题首先 ...

  4. 解决微信小程序要求的TLS版本必须大于等于1.2的问题(windows2008服务器)

    开始->运行->输入 PowerShell 复制这段代码粘入弹出的dos窗口内 # Enables TLS 1.2 on windows Server 2008 R2 and Window ...

  5. 官方webupload上传多个文件或者图片的方法

    文件上传 页面代码: <!--引入CSS--> <link rel="stylesheet" type="text/css" href=&qu ...

  6. ext笔记

    命名   The top-level namespaces and the actual class names should be CamelCased. Everything else shoul ...

  7. quartz测试类

    package demo.mytest; import java.text.ParseException; import org.quartz.CronTrigger;import org.quart ...

  8. nginx目录结构和配置文件

    nginx软件功能模块说明 Nginx软件之所以强大,是因为它具有众多的功能模块,下面列出了企业常用的重要模块. (1) Nginx核心功能模块(Core functionality)nginx核心功 ...

  9. VC++中char和TCHAR之间转换

    char:计算机编程语言(c.c++.java等)中可容纳单个字符的一种基本数据类型. TCHAR:为了满足Unicode编码,对char的扩展,即_T(“str”)表示TCHAR类型 C++支持两种 ...

  10. python图像插值

    最近邻:选择离它所映射到的位置最近的输入像素的灰度值为插值结果. 最临近插值 图像的缩放很好理解,就是图像的放大和缩小.传统的绘画工具中,有一种叫做“放大尺”的绘画工具,画家常用它来放大图画.当然,在 ...