The Child and Sequence
Codeforces Round #250 (Div. 1)D:http://codeforces.com/problemset/problem/438/D
题意:给你一个序列,然后有3种操作 1x y.表示查询[x,y]之间的区间和,2 x y z表示把[x y]内的数%z,3x y,表示把第x个数变成y。
题解:肯定是用线段树来维护,但是一开始想不到维护什么统计量,对于区间取模,没办法用lazy标记,更新到第的话,肯定会T。后来,认为既然没办法用lazy,那么只能用别的方法来优化更新。发现每次取模之后,数都会变小,如果要取模的数比当前模数小的话,就不用取模,于是可以维护区间最大值,如果区间最大值都小于模数的话,这个区间肯定不用更新,这样来减少更新。这样的方法,其实以前也做过,就是对于一个区间内的数进行开平方操作,每个数会越开越小,到了1的时候就可以直接不开了。因此,对于线段树的区间更新来说:1如果能找到好的lazy可以标记的话,就使用lazy标记;2如果找不到就要想办法优化区间更新,让单点更新的次数变少。另外此题,自己用线段树省空间的写法,结果不熟练,一个地方写错,最后wa几发。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=1e5+;
int n,m;
long long sum[N*],maxn[N*];
void pushup(int rt){
sum[rt]=sum[rt<<]+sum[rt<<|];
maxn[rt]=max(maxn[rt<<],maxn[rt<<|]);
}
void build(int l,int r,int rt){
if(l==r){
scanf("%I64d",&maxn[rt]);
sum[rt]=maxn[rt];
return;
}
int mid=(l+r)/;
build(l,mid,rt<<);
build(mid+,r,rt<<|);
pushup(rt);
}
void update(int l,int r,int rt,int from,int to,long long mod){
if(maxn[rt]<mod)return;
if(l==r){
maxn[rt]%=mod;
sum[rt]=maxn[rt];
return;
}
int mid=(l+r)/;
if(mid>=to)update(l,mid,rt<<,from,to,mod);
else if(mid<from)update(mid+,r,rt<<|,from,to,mod);
else{
update(l,mid,rt<<,from,mid,mod);
update(mid+,r,rt<<|,mid+,to,mod);
}
pushup(rt);
}
void update2(int l,int r,int rt,int pos,long long val){
if(l==r){
maxn[rt]=val;
sum[rt]=val;
return;
}
int mid=(l+r)/;
if(mid>=pos)update2(l,mid,rt<<,pos,val);
else update2(mid+,r,rt<<|,pos,val);
pushup(rt);
}
long long query(int l,int r,int rt,int from,int to){
if(l==from&&r==to){
return sum[rt];
}
int mid=(l+r)/;
if(mid>=to)return query(l,mid,rt<<,from,to);
else if(mid<from)return query(mid+,r,rt<<|,from,to);
else {
return query(l,mid,rt<<,from,mid)+query(mid+,r,rt<<|,mid+,to); }
}
int t,t1,t4;
long long t2,t3;
int main(){
while(~scanf("%d%d",&n,&m)){
memset(sum,,sizeof(sum));
memset(maxn,,sizeof(maxn));
build(,n,);
for(int i=;i<=m;i++){
scanf("%d%d",&t,&t1);
if(t==){
scanf("%d",&t4);
printf("%I64d\n",query(,n,,t1,t4));
}
else if(t==){
scanf("%d%I64d",&t4,&t2);
update(,n,,t1,t4,t2);
}
else{
scanf("%I64d",&t2);
update2(,n,,t1,t2);
}
}
}
}
The Child and Sequence的更多相关文章
- Codeforce 438D-The Child and Sequence 分类: Brush Mode 2014-10-06 20:20 102人阅读 评论(0) 收藏
D. The Child and Sequence time limit per test 4 seconds memory limit per test 256 megabytes input st ...
- Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间取摸
D. The Child and Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...
- 题解——CodeForces 438D The Child and Sequence
题面 D. The Child and Sequence time limit per test 4 seconds memory limit per test 256 megabytes input ...
- Codeforces Round #250 (Div. 1) D. The Child and Sequence(线段树)
D. The Child and Sequence time limit per test 4 seconds memory limit per test 256 megabytes input st ...
- Codeforces Round #250 (Div. 1) D. The Child and Sequence
D. The Child and Sequence time limit per test 4 seconds memory limit per test 256 megabytes input st ...
- AC日记——The Child and Sequence codeforces 250D
D - The Child and Sequence 思路: 因为有区间取模操作所以没法用标记下传: 我们发现,当一个数小于要取模的值时就可以放弃: 凭借这个来减少更新线段树的次数: 来,上代码: # ...
- 438D - The Child and Sequence
D. The Child and Sequence time limit per test 4 seconds memory limit per test 256 megabytes input st ...
- Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间求和+点修改+区间取模
D. The Child and Sequence At the children's day, the child came to Picks's house, and messed his h ...
- Codeforces 438D The Child and Sequence - 线段树
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at ...
- 线段树【CF620E】The Child and Sequence
Description At the children's day, the child came to Picks's house, and messed his house up. Picks w ...
随机推荐
- BA/PM Competency Module
No Competency Description BA Weight% PM Weight% 1 Business Analysis:BA Track Maps process flows, ...
- [Flux] 1. Development Environment Setup
Install packages: { "name": "reactflux", "version": "1.0.0", ...
- QSplashScreen类实现Qt程序启动画面
QSplashScreen类实现Qt程序启动画面 收藏人:zwsj 2013-09-13 | 阅:569 转:6 | 来源 | 分享 程序启动 ...
- java按照集合中元素的属性进行排序示例代码
public class Student { private String name; private int age; private int id; public Student() { sup ...
- cordova 消息推送,告别,消息推送服务器,和 苹果推送证书
cordova plugin add org.apache.cordova.vibration cordova plugin add https://github.com/katzer/cordova ...
- 运行yum报错:Error: Cannot retrieve metalink for repository: epel. Please verify its path
Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again 当我们安装第三方扩 ...
- java返回参数中几种常见的方法
1.有参数有返回值 public class text_1 { 1)创建add方法 public int add(int i, int j) { int res = i + j; ...
- Weex 初始
1.一旦数据和模板绑定,数据的变化会立即体现在前台的变化 <template> <container> <text style="font-size: {{si ...
- 了解php面向对象
php 三大特性:封装.继承.多态,一直以来只知道其字,却不大了解其意思和具体使用,只是对继承有大概的了 解,优点是代码的重用性,oop概念,记得有一次我去面试,人家问我什么是oop,然后我答了很多什 ...
- servlet 项目 ,,启动没问题,,但是,一请求也面就报错误。。。。求解决。。。。。。。。。。。。。各种百度,都没解决了啊。。。。。急急急急急急急急急急急急急急急急急急
信息: Server startup in 1674 mslog4j:WARN No appenders could be found for logger (com.mchange.v2.log.M ...