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的更多相关文章

  1. codevs 线段树练习ⅠⅡⅢ

    1080 线段树练习  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description 一行N个方格,开始每个格子里都有 ...

  2. codevs 1082 线段树练习 3(区间维护)

    codevs 1082 线段树练习 3  时间限制: 3 s  空间限制: 128000 KB  题目等级 : 大师 Master 题目描述 Description 给你N个数,有两种操作: 1:给区 ...

  3. codevs 1576 最长上升子序列的线段树优化

    题目:codevs 1576 最长严格上升子序列 链接:http://codevs.cn/problem/1576/ 优化的地方是 1到i-1 中最大的 f[j]值,并且A[j]<A[i] .根 ...

  4. codevs 1080 线段树点修改

    先来介绍一下线段树. 线段树是一个把线段,或者说一个区间储存在二叉树中.如图所示的就是一棵线段树,它维护一个区间的和. 蓝色数字的是线段树的节点在数组中的位置,它表示的区间已经在图上标出,它的值就是这 ...

  5. codevs 1082 线段树区间求和

    codevs 1082 线段树练习3 链接:http://codevs.cn/problem/1082/ sumv是维护求和的线段树,addv是标记这歌节点所在区间还需要加上的值. 我的线段树写法在运 ...

  6. codevs 1080 线段树练习

    链接:http://codevs.cn/problem/1080/ 先用树状数组水一发,再用线段树水一发 树状数组代码:84ms #include<cstdio> #include< ...

  7. 线段树练习 codevs 1080

    /* codevs 1080 线段树练习 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Description 一行N个方格,开 ...

  8. codevs 1080 线段树练习 CDQ分治

    codevs 1080 线段树练习 http://codevs.cn/problem/1080/  时间限制: 1 s  空间限制: 128000 KB   题目描述 Description 一行N个 ...

  9. 4163 hzwer与逆序对 (codevs + 权值线段树 + 求逆序对)

    题目链接:http://codevs.cn/problem/4163/ 题目:

随机推荐

  1. java 简单操作HDFS

    创建java 项目 package com.yw.hadoop273; import org.apache.hadoop.conf.Configuration; import org.apache.h ...

  2. 单选按钮(CheckBox)

    import React, { useState, useEffect } from 'react' import PropTypes from 'prop-types' import _ from ...

  3. zabbix的离线安装方法----孙祎晨,如需转载请注明出处,谢谢配合。

    ------------------------zabbix的离线安装步骤--------------------------------------------------------------- ...

  4. Hadoop HA 搭建

    Hadoop HA 什么是 HA HA是High Available缩写,是双机集群系统简称,指高可用性集群,是保证业务连续性的有效解决方案,一般有两个或两个以上的节点,且分为活动节点及备用节点.通常 ...

  5. linux驱动开发随手记【1】

    1.一个patch只完成一项任务 2.用vim编辑多个文件,想同时关闭所有文件时候,可以用 :qall 命令 .vim用户手册08 3.mmc dev 0.mmc为U-boot命令,这句话的含义是设置 ...

  6. centos7创建共享文件夹

    0.检查是否已经安装samba rpm -qi samba 1.未安装,安装samba, 如果已安装,请忽略: yum -y install samba samba-client 2.共享一个目录,使 ...

  7. await在forEach不起作用解决【await is a reserved word】

    原文链接:https://blog.csdn.net/ssbb1995/article/details/82084800 1.await 只能在 async中使用,如: async function ...

  8. c# 处理串行化对象的版本变化

  9. Vue框架之组件与过滤器的使用

    一.组件的使用 局部组件的使用 ​ 打油诗: 1.声子 2.挂子 3.用 var App = { tempalte:` <div class='app'></div>` }; ...

  10. HDU 6187 Destroy Walls (思维,最大生成树)

    HDU 6187 Destroy Walls (思维,最大生成树) Destroy Walls *Time Limit: 8000/4000 MS (Java/Others) Memory Limit ...