[codevs]线段树练习5
http://codevs.cn/problem/4927/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string> using namespace std;
const int N = 1e5 + ; #define oo 1423333339
#define LL long long
#define gg 465432477 struct Node {
LL l, r, w, f, mx, mi, fg;
bool qs;
} T[N << ];
LL n, m, answer, maxn, minn; inline LL read() {
LL x = , f = ;
char c = getchar();
while(c < '' || c > '') { if(c == '-')f = -; c = getchar();}
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * f;
} void imp(LL jd) {
T[jd].w = T[jd << ].w + T[jd << | ].w;
T[jd].mx = max(T[jd << ].mx, T[jd << | ].mx);
T[jd].mi = min(T[jd << ].mi, T[jd << | ].mi);
} void down(LL jd) {
if(T[jd].qs) {
T[jd << ].f = ;
T[jd << ].qs = ;
T[jd << ].fg = T[jd].fg;
T[jd << ].w = (T[jd << ].r - T[jd << ].l + ) * T[jd].fg; T[jd << | ].f = ;
T[jd << | ].qs = ;
T[jd << | ].fg = T[jd].fg;
T[jd << | ].w = (T[jd << | ].r - T[jd << | ].l + ) * T[jd].fg; T[jd << ].mi = T[jd << ].mx = T[jd << | ].mi= T[jd << | ].mx=T[jd].fg; T[jd].fg = ;
T[jd].qs = ;
}
if(T[jd].f) {
T[jd << ].f += T[jd].f;
T[jd << ].w += (T[jd << ].r - T[jd << ].l + ) * T[jd].f;
T[jd << ].mi += T[jd].f;
T[jd << ].mx += T[jd].f; T[jd << | ].f += T[jd].f;
T[jd << | ].w += (T[jd << | ].r - T[jd << | ].l + ) * T[jd].f;
T[jd << | ].mi += T[jd].f;
T[jd << | ].mx += T[jd].f; T[jd].f = ;
}
return ;
} void build_tree(LL l, LL r, LL jd) {
T[jd].l = l;
T[jd].r = r;
if(l == r) {
T[jd].w = read();
T[jd].mx = T[jd].w;
T[jd].mi = T[jd].w;
return ;
}
LL mid = (l + r) >> ;
build_tree(l, mid, jd << );
build_tree(mid + , r, jd << | );
imp(jd);
} void Sec_g(LL l, LL r, LL jd, LL x, LL y, LL yj) {
if(x <= l && r <= y) {
T[jd].f += yj;
T[jd].w += (T[jd].r - T[jd].l + ) * yj;
T[jd].mi += yj;
T[jd].mx += yj;
return ;
}
if(T[jd].f || T[jd].qs)
down(jd);
LL mid = (l + r) >> ;
if(x <= mid)
Sec_g(l, mid, jd << , x, y, yj);
if(y > mid)
Sec_g(mid + , r, jd << | , x, y, yj);
imp(jd);
} void Sec_set(LL l, LL r, LL jd, LL x, LL y, LL k) {
if(x <= l && r <= y) {
T[jd].fg = k;
T[jd].qs = ;
T[jd].w = (T[jd].r - T[jd].l + ) * T[jd].fg;
T[jd].mx = k;
T[jd].mi = k;
T[jd].f = ;
return ;
}
LL mid = (l + r ) >> ;
if(T[jd].f || T[jd].qs)
down(jd);
if(x <= mid)
Sec_set(l, mid, jd << , x, y, k);
if(y > mid)
Sec_set(mid + , r, jd << | , x, y, k);
imp(jd);
} void Sec_calc(LL l, LL r, LL jd, LL x, LL y) {
if(x <= l && r <= y) {
answer += T[jd].w;
return;
}
if(T[jd].f || T[jd].qs)
down(jd);
LL mid = (l + r) >> ;
if(x <= mid)
Sec_calc(l, mid, jd << , x, y);
if(y > mid)
Sec_calc(mid + , r, jd << | , x, y);
} void Sec_min(LL l, LL r, LL jd, LL x, LL y) {
if(x <= l && r <= y) {
minn = min(minn, T[jd].mi);
return ;
}
if(T[jd].f || T[jd].qs)
down(jd);
LL mid = (l + r) >> ;
if(x <= mid)
Sec_min(l, mid, jd << , x, y);
if(y > mid)
Sec_min(mid + , r, jd << | , x, y);
} void Sec_max(LL l, LL r, LL jd, LL x, LL y) {
if(x <= l && r <= y) {
maxn = max(maxn, T[jd].mx);
return ;
}
if(T[jd].f || T[jd].qs)
down(jd);
LL mid = (l + r) >> ;
if(x <= mid)
Sec_max(l, mid, jd << , x, y);
if(y > mid)
Sec_max(mid + , r, jd << | , x, y);
} int main() {
freopen("gg.in", "r", stdin);
n = read();
m = read();
build_tree(, n, );
for(LL i = ; i <= m; i ++) {
string s;
cin >> s;
LL x = read();
LL y = read();
if(s == "add") {
LL k = read();
Sec_g(, n, , x, y, k);
continue;
}
if(s == "set") {
LL k = read();
Sec_set(, n, , x, y, k);
continue;
}
if(s == "sum") {
answer = ;
Sec_calc(, n, , x, y);
printf("%lld\n", answer);
continue;
}
if(s == "min") {
minn = oo;
Sec_min(, n, , x, y);
printf("%lld\n", minn);
continue;
}
if(s == "max") {
maxn = -oo;
Sec_max(, n, , x, y);
printf("%lld\n", maxn);
continue;
}
}
return ;
}
/*
10 6
3 9 2 8 1 7 5 0 4 6
add 4 9 4
set 2 6 2
sum 2 10
max 1 7
min 3 6
*/
[codevs]线段树练习5的更多相关文章
- codevs 线段树练习ⅠⅡⅢ
1080 线段树练习 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description 一行N个方格,开始每个格子里都有 ...
- codevs 1082 线段树练习 3(区间维护)
codevs 1082 线段树练习 3 时间限制: 3 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 给你N个数,有两种操作: 1:给区 ...
- codevs 1576 最长上升子序列的线段树优化
题目:codevs 1576 最长严格上升子序列 链接:http://codevs.cn/problem/1576/ 优化的地方是 1到i-1 中最大的 f[j]值,并且A[j]<A[i] .根 ...
- codevs 1080 线段树点修改
先来介绍一下线段树. 线段树是一个把线段,或者说一个区间储存在二叉树中.如图所示的就是一棵线段树,它维护一个区间的和. 蓝色数字的是线段树的节点在数组中的位置,它表示的区间已经在图上标出,它的值就是这 ...
- codevs 1082 线段树区间求和
codevs 1082 线段树练习3 链接:http://codevs.cn/problem/1082/ sumv是维护求和的线段树,addv是标记这歌节点所在区间还需要加上的值. 我的线段树写法在运 ...
- codevs 1080 线段树练习
链接:http://codevs.cn/problem/1080/ 先用树状数组水一发,再用线段树水一发 树状数组代码:84ms #include<cstdio> #include< ...
- 线段树练习 codevs 1080
/* codevs 1080 线段树练习 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Description 一行N个方格,开 ...
- codevs 1080 线段树练习 CDQ分治
codevs 1080 线段树练习 http://codevs.cn/problem/1080/ 时间限制: 1 s 空间限制: 128000 KB 题目描述 Description 一行N个 ...
- 4163 hzwer与逆序对 (codevs + 权值线段树 + 求逆序对)
题目链接:http://codevs.cn/problem/4163/ 题目:
随机推荐
- 使用babel es6 转 es5
安装 //Webpack 接入 Babel 必须依赖的模块 npm i -D babel-core babel-loader //preset,告诉babel编译的文件中用到了哪些语法env包含当前所 ...
- MySQL 索引机制
MySQL 原理篇 MySQL 索引机制 MySQL 体系结构及存储引擎 MySQL 语句执行过程详解 MySQL 执行计划详解 MySQL InnoDB 缓冲池 MySQL InnoDB 事务 My ...
- Gogs + Drone 实现CI/CD(CD)
前文已经实现CI部分,本文继续以Asp.Net Core实现CD部分. 创建gogs仓库 首先在gogs创建一个空项目drone-ci-demo,本地新建一个asp.net core项目,并且在与.c ...
- dotnet core2.2 通过虚拟机发布到CentOS上
自从.net core出现的时候,就知道c#的代码居然能后运行到Linux上面,以前都没想过居然这么牛逼,所以很早就想学习怎样部署上去,直到现在.net core都出现2.2了,才花时间去接触,说实话 ...
- centos安装rocketMQ
1.下载安装包 http://rocketmq.apache.org/release_notes/ 这里选择 4.4.0 版本,点击进去 可以选择源码包或者二进制文件,这里选择二进制文件(ps:如果选 ...
- SqlServer2008 / SqlServer2012 禁用windows登录,sa忘记密码或密码过期如何登陆
以管理员身份运行cmd 1.cmd 下 停止SqlServer服务,net stop mssqlserver: 2.新建windows账号test,加入administrators组里,授予管理员权 ...
- 后端参数校验器v1.0(调用一个方法校验所有参数并得到校验结果,且包括错误原因)
一:介绍 在写后端时,面对多个参数,比如手机号码.密码等我们常常需要写验证逻辑,当需要验证的参数较多的时候我们会需要写很多的判断语句,这就造成了大量的代码冗余.因此我开发了一套参数验证器,只需要调用参 ...
- 关于Echarts柱状图点击事件的实现方法
开发过程中,我们经常会碰到这样的需求:在柱状图上,点击某条柱形,调用相应的方法或跳转相应的界面 接下来就详细介绍如何实现柱状图的点击事件,其中maChart是绘图对象 一.简单的点击事件 myChar ...
- laravel withCount 统计关联数量
roleModel定义关联 hasmany public function users(){ return $this->hasMany('App\Models\Users', 'role_i ...
- 【动态规划】ZZNU-OJ- 2054 : 油田
2054 : 油田 (一个神奇的功能:点击上方文字进入相应页面) 时间限制:1 Sec 内存限制:32 MiB提交:49 答案正确:6 提交 状态 讨论区 题目描述 在太平洋的一片海域,发现了大量的油 ...