CodeForces 438D 线段树 剪枝
4 seconds
256 megabytes
standard input
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:
- Print operation l, r. Picks should write down the value of .
- Modulo operation l, r, x. Picks should perform assignment a[i] = a[i] mod x for each i (l ≤ i ≤ r).
- 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?
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.
For each operation 1, please print a line containing the answer. Notice that the answer may exceed the 32-bit integer.
5 5 1 2 3 4 5 2 3 5 4 3 3 5 1 2 5 2 1 3 3 1 1 3
8 5
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
49 15 23 1 9
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 线段树 剪枝的更多相关文章
- Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论
Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论 题意 给你一段数,然后小明去猜某一区间内的gcd,这里不一定是准确值,如果在这个区间内改变 ...
- 对权值线段树剪枝的误解--以HDU6703为例
引子 对hdu6703,首先将问题转化为"询问一个排列中大于等于k的值里,下标超过r的最小权值是多少" 我们采用官方题解中的做法:权值线段树+剪枝 对(a[i],i)建线段树,查询 ...
- Codeforces 444 C. DZY Loves Colors (线段树+剪枝)
题目链接:http://codeforces.com/contest/444/problem/C 给定一个长度为n的序列,初始时ai=i,vali=0(1≤i≤n).有两种操作: 将区间[L,R]的值 ...
- 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 ...
- HDOJ:6356-Glad You Came(线段树剪枝)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6356 解题心得: 现在深深的知道了算法复杂度的重要了,这个题算复杂度的时候还要把一些常数也算出来,不然 ...
- LibreOJ #6190. 序列查询(线段树+剪枝)
莫队貌似是过不了的,这题是我没见过的科技... 首先区间按右端点排序,然后一个扫描线,扫到某个区间右端点时候计算答案,线段树上节点的信息并不需要明确定义,我们只要求线段树做到当前扫到now时,查询[L ...
- HDU4391(线段树+剪枝)
Paint The Wall Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Codeforces 938G 线段树分治 线性基 可撤销并查集
Codeforces 938G Shortest Path Queries 一张连通图,三种操作 1.给x和y之间加上边权为d的边,保证不会产生重边 2.删除x和y之间的边,保证此边之前存在 3.询问 ...
- codeforces 1136E 线段树
codeforces 1136E: 题意:给你一个长度为n的序列a和长度为n-1的序列k,序列a在任何时候都满足如下性质,a[i+1]>=ai+ki,如果更新后a[i+1]<ai+ki了, ...
随机推荐
- 特别困的学生 UVa12108(模拟题)
一.题目 课堂上有n个学生(n<=10).每个学生都有一个“睡眠-清醒”周期,其中第i个学生醒Ai分钟后睡Bi分钟,然后重复(1<=Ai,Bi<=5),初始第i个同学处于他的周期的C ...
- 如何写好一个vue组件,老夫的一年经验全在这了【转】 v-bind="$attrs" 和 v-on="$listeners"
如何写好一个vue组件,老夫的一年经验全在这了 一个适用性良好的组件,一种是可配置项很多,另一种就是容易覆写,从而扩展功能 Vue 组件的 API 来自三部分——prop.事件和插槽: prop 允许 ...
- 1991: C语言实验——大小写转换
1991: C语言实验——大小写转换 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 183 Solved: 109[Submit][Status][We ...
- JavaScript中对象的属性:如何遍历属性
for/in 语句循环遍历对象的属性. js中获取key得到某对象中相对应的value的方法:obj.key js中根据动态key得到某对象中相对应的value的方法有二: 一.var key = & ...
- 92.背包问题(lintcode)
注意j-A[i-1]必须大于等于0,只大于0会报错 class Solution { public: /** * @param m: An integer m denotes the size of ...
- iOS开发各种证书问题
引言 写在前面 一.App ID(bundle identifier) 二.设备(Device) 三.开发证书(Certificates) 四.供应配置文件(Provisioning ...
- PWN题搭建
0x00.准备题目 例如:level.c #include <stdio.h> #include <unistd.h> int main(){ char buffer[0x10 ...
- shell脚本,逻辑结构题练习。
awk '/5/{a=1}!a' file2结果:1234解释:第一行 /5/不匹配跳过{a=1},继续!a,此时a没有值属于假取反为真,故输出第一行 第二行 /5/不匹配跳过{a=1},继续!a,此 ...
- hihoCoder-1093-SPFA
SPFA的卓越之处就在于处理多点稀疏图,因为点太多的话,我们直接用矩阵来存图的话是存不下的. 所以当我们用邻接矩阵来存图的话,我们就可以用SPFA来解决这类问题,spfa就是优化版的bellman-f ...
- HDU-2018-奶牛的故事
这题找到递推式就好写了,递推式大致是: f=n (n<=4) f=f(n-1)+f(n-3) (n>4) 其实这题的题意,我觉得是有很大的问题的,它前后说的每年年初的意思都不一样,敬请参考 ...