[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/ 题目:
随机推荐
- netty--处理器
编解码器的基类: 编码:MessageToByteEncode 解码:ByteToMessageDecoder
- Linux磁盘管理系列 — 磁盘配额管理
一.磁盘管理的概念 Linux系统是多用户任务操作系统,在使用系统时,会出现多用户共同使用一个磁盘的情况,如果其中少数几个用户占用了大量的磁盘空间,势必压缩其他用户的磁盘的空间和使用权限.因此,系统管 ...
- dB分贝计算
1. 定义 dB-表征相对值的大小的单位,即两个电.声功率之比或者电流.电压.音量之比,是一种测相对大小的单位. 1.1 电.声功率之比——10lg(x/y) x.y分别表示两个欲比较的功率值. 例如 ...
- puppet工作原理及部署redis主从篇
一.简介 1.国际惯例什么是puppet puppet是一种Linux.Unix.windows平台的集中配置管理系统,使用自有的puppet描述语言,可管理配置文件.用户.cron任务.软件包.系统 ...
- core直接获取报异常数据
报异常直接跳转到/Home/Error [ResponseCache(Duration = , Location = ResponseCacheLocation.None, NoStore = tru ...
- mysql 系统变量
show variables; ---------------------------------+-------------------------------------------------- ...
- Linux 知识
linux下如何查看某软件是否已安装 因为linux安装软件的方式比较多,所以没有一个通用的办法能查到某些软件是否安装了.总结起来就是这样几类: 1.rpm包安装的,可以用rpm -qa看到,如果要查 ...
- C#-DBHelper
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Python requests.post嵌套多层json参数调用接口
#coding:utf-8 import requests,json #第一行注解的#coding:utf-8表示可以支持中文,不然代码里面有中文会报错 url = "http://xxx& ...
- DNS服务——智能域名解析、镜像Web站点、直接域名泛域名
智能域名解析 智能域名解析只有Linux DNS服务器才有.下面给出2种智能域名解析应用场景. 应用场景1 整个互联网由众多ISP组成,在中国就是联通.电信.移动等等 各家ISP内部网络四通八达,速度 ...