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了, ...
随机推荐
- java.sql.SQLException: Incorrect string value: '\xE6\x88\x91\xE7\x9A\x84...' for column 'groupName'
java.sql.SQLException: Incorrect string value: '\xE6\x88\x91\xE7\x9A\x84...' for column 'groupName' ...
- Emmet:HTML/CSS代码快速编写神器--20150422
Emmet的前身是大名鼎鼎的Zen coding,如果你从事Web前端开发的话,对该插件一定不会陌生.它使用仿CSS选择器的语法来生成代码,大大提高了HTML/CSS代码编写的速度,比如下面的演示: ...
- linux运维中常用的指令
一.终端中常用的快捷键 man界面中的快捷键: ?keyword 向上搜索关键词keyword,n向下搜索,N继续向上搜索 /keyword 向下搜索关键词keyw ...
- GIMP选择区域Selection Editor
如图我要选择该图的衣服部分和这个球的部分, 选择Select下的Selection Editor工具,然后点击魔法棒工具(Fuzzy Select Tool),选择衣服: 需要注意以下白色部分是选择的 ...
- angular 列表渲染机制
watchCollection:监听集合元素的变化,而不能监听到集合元素内部的属性变化,只要集合中元素的引用没有发生变化,则认为无变化.用这个api也可以监听普通对象的第一层属性变化. watch:监 ...
- 如何用纯 CSS 创作一个雷达扫描动画
效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/VdbGvr 可交互视频 ...
- return_url notify_url
return_url对返回订单状态进行更新和判断,notify_url为异步调动页面,需要先判断notify_url里是否对订单数据做过处理,避免重复更新数据,然后如果用户付款成功直接关闭页面,会造成 ...
- 关于面试总结-linux篇
前言 现在做测试的出去面试,都会被问到linux,不会几个linux指令都不好意思说自己是做测试的了,本篇收集了几个被问的频率较高的linux面试题 常用指令 1.说出10个linux常用的指令 ls ...
- Java 正则表达式详解---https://www.jb51.net/article/16829.htm
一.正则表达式基础知识 我们先从简单的开始.假设你要搜索一个包含字符“cat”的字符串,搜索用的正则表达式就是“cat”.如果搜索对大小写不敏感,单词“catalog”.“Catherine”.“so ...
- HDU 2435 There is a war
There is a war Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ...