CodeForces 438D The Child and Sequence (线段树 暴力)
题目大意:
给你一个序列,要求在序列上维护三个操作:
1)区间求和
2)区间取模
3)单点修改
这里的操作二很讨厌,取模必须模到叶子节点上,否则跑出来肯定是错的。没有操作二就是线段树水题了。
既然必须模到叶子节点,那我们就模咯。
显然,若$b<c$,则$b%c=b$。
因此我们同时维护一个区间最大值,若某区间内最大值小于模数,就把该分支剪掉。
若$a=b%c$,那么肯定有$a \leq \frac{b}{2}$成立。
也就是说,一个数最多被模$\log_2 x$次。总的时间复杂度为$O(n \log n)$,可以接受(就算取模$log$次以后单点修改将某点的值改大,因为有上述剪枝存在,时间复杂度并不会上升太多)。
代码:
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cctype>
#define foru(i,x,y) for(LL i=x;i<=y;i++)
#define ford(i,x,y) for(LL i=x;i>=y;i--)
#define re(x) x=read()
#define max(a,b) ((aaa=a)>(bbb=b))?aaa:bbb
using namespace std;
typedef long long LL;
typedef double db;
const LL inf=1e9;
const LL N=2e6+; struct node{LL s,m;}t[N];
LL n,m,aaa,bbb; LL read(){
static LL f,x;static char ch;
x=f=;ch=getchar();
while(!isdigit(ch)){f=(ch=='-');ch=getchar();}
while(isdigit(ch)){x=x*+ch-'';ch=getchar();}
return f?-x:x;
} #define mid ((L+R)>>1)
#define ls (k<<1)
#define rs ((k<<1)+1) void upd(LL k,LL L,LL R,LL p,LL x){
if(p<L||p>R)return;
if(L==R){t[k]=(node){x,x};return;}
upd(ls,L,mid,p,x);upd(rs,mid+,R,p,x);
t[k].s=t[ls].s+t[rs].s;
t[k].m=max(t[ls].m,t[rs].m);
} LL qrys(LL k,LL L,LL R,LL l,LL r){
if(r<L||l>R)return ;
if(l<=L&&R<=r)return t[k].s;
return qrys(ls,L,mid,l,r)+qrys(rs,mid+,R,l,r);
} LL qrym(LL k,LL L,LL R,LL l,LL r){
if(r<L||l>R)return ;
if(l<=L&&R<=r)return t[k].m;
return max(qrym(ls,L,mid,l,r),qrym(rs,mid+,R,l,r));
} void mo(LL k,LL L,LL R,LL l,LL r,LL x){
if(r<L||l>R||t[k].m<x)return;
if(L==R){t[k].s%=x;t[k].m=t[k].s;return;}
mo(ls,L,mid,l,r,x);mo(rs,mid+,R,l,r,x);
t[k].s=t[ls].s+t[rs].s;
t[k].m=max(t[ls].m,t[rs].m);
} int main(){
LL l,r,x;
//freopen("mod.in","r",stdin);freopen("mod.out","w",stdout);
re(n);re(m);
foru(i,,n){
re(x);
upd(,,n,i,x);
}
foru(i,,m){
re(x);re(l);re(r);
if(x==){
printf("%I64d\n",qrys(,,n,l,r));
}else if(x==){
re(x);
mo(,,n,l,r,x);
}else{
upd(,,n,l,r);
}
}
return ;
}
CodeForces 438D The Child and Sequence (线段树 暴力)的更多相关文章
- 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 ...
- CF(438D) The Child and Sequence(线段树)
题意:对数列有三种操作: Print operation l, r. Picks should write down the value of . Modulo operation l, r, x. ...
- 2018.07.23 codeforces 438D. The Child and Sequence(线段树)
传送门 线段树维护区间取模,单点修改,区间求和. 这题老套路了,对一个数来说,每次取模至少让它减少一半,这样每次单点修改对时间复杂度的贡献就是一个log" role="presen ...
- 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 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
题面 D. The Child and Sequence time limit per test 4 seconds memory limit per test 256 megabytes input ...
- cf250D. The Child and Sequence(线段树 均摊复杂度)
题意 题目链接 单点修改,区间mod,区间和 Sol 如果x > mod ,那么 x % mod < x / 2 证明: 即得易见平凡, 仿照上例显然, 留作习题答案略, 读者自证不难. ...
- Codeforces Round #250 (Div. 1) D. The Child and Sequence (线段树)
题目链接:http://codeforces.com/problemset/problem/438/D 给你n个数,m个操作,1操作是查询l到r之间的和,2操作是将l到r之间大于等于x的数xor于x, ...
- Codeforces 438D The Child and Sequence
题意:给定一个n个数的序列,完成以下3个操作: 1.给定区间求和 2.给定区间对x取模 3.单点修改 对一个数取模,这个数至少折半.于是我们记一个最大值max,如果x>max则不做处理. #in ...
随机推荐
- matlab中自带的sobol的函数提供的sobol序列
clc; clear all; close all; M=;% 维度,几个参数 nPop=; VarMin=[0.6, 0.10, 0.002, 0.02, 0.17, 0.0, 0.17, 0.0, ...
- GTK入门
环境准备 官网下载 GTK 源码包,因为本机 GLib 版本不够,下载一个非最新版的 GTK3.8.0 先学习用 直接阅读 "/gtk+-3.8.0/docs/reference/gtk/h ...
- Python String startswith() Method
一,摘自官方API https://docs.python.org/3/library/stdtypes.html#methods str.startswith(prefix[, start[, e ...
- 连接数据库方法2-DBCP
DBCP(连接池): 解决对数据库建立以及关闭连接时消耗大量资源的解决方案. 程序创建和关闭对数据库连接时会消耗大量的资源,连接池技术帮我们 在程序运行的开始时就预先创建大量的连接,这些连接组成一个池 ...
- Centos6.5 安装zabbix3(收藏,非原创)
1.安装PHP Zabbix 3.0对PHP的要求最低为5.4,而CentOS6默认为5.3.3,完全不满足要求,故需要利用第三方源,将PHP升级到5.4以上,注意,不支持PHP7 rpm -ivh ...
- Unity获取游戏对象详解
我觉得Unity里面的Transform 和 GameObject就像两个双胞胎兄弟一样,这俩哥们很要好,我能直接找到你,你也能直接找到我.我看很多人喜欢在类里面去保存GameObject对象.解决G ...
- .equal()和==的区别
1.首先,equal和==最根本的区别在于equal是一个方法,而==是一个运算符. 2.一般来说,==运算符比较的是在内存中的物理地址,.equal()比较的是哈希算法值是否相等(即hashcode ...
- springboot rabbitmq消息同步用作接口调用
1.引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- PAT Advanced 1066 Root of AVL Tree (25) [平衡⼆叉树(AVL树)]
题目 An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child ...
- POJ 1655 Balancing Act【树的重心模板题】
传送门:http://poj.org/problem?id=1655 题意:有T组数据,求出每组数据所构成的树的重心,输出这个树的重心的编号,并且输出重心删除后得到的最大子树的节点个数,如果个数相同, ...