XTUOJ 1238 Segment Tree
Segment Tree
Accepted : 3 Submit : 21
Time Limit : 9000 MS Memory Limit : 65536 KB
Problem Description:
A contest is not integrity without problems about data structure.
There is an array a[1],a[2],…,a[n]. And q questions of the following 4 types:
1 l r c - Update a[k] with a[k]+c for all l≤k≤r
2 l r c - Update a[k] with min{a[k],c} for all l≤k≤r;
3 l r c - Update a[k] with max{a[k],c} for all l≤k≤r;
4 l r - Ask for min{a[k]:l≤k≤r} and max{a[k]:l≤k≤r}.
Input
The first line contains a integer T(no more than 5) which represents the number of test cases.
For each test case, the first line contains 2 integers n,q (1≤n,q≤200000).
The second line contains n integers a1,a2,…,an which indicates the initial values of the array (|ai|≤).
Each of the following q lines contains an integer t which denotes the type of i-th question. If t=1,2,3, 3 integers l,r,c follows. If t=4, 2 integers l,r follows. (1≤ti≤4,1≤li≤ri≤n)
If t=1, |ci|≤2000;
If t=2,3, |ci|≤10^9.
Output
For each question of type 4, output two integers denote the minimum and the maximum.
Sample Input
1
1 1
1
4 1 1
Sample Output
1 1
解题:如其名,线段树!关键在于如何解决矛盾,既要相加,又要进行区间重置?那么这样搞,如何进行lazy呢?只要设置一个重置标志就好了。
BB is cheap!
#include <bits/stdc++.h>
using namespace std;
const int maxn = ;
struct node {
int lt,rt,theMin,theMax,add,lazy;
bool reset;
} tree[maxn<<];
void pushup(int v) {
tree[v].theMax = max(tree[v<<].theMax,tree[v<<|].theMax);
tree[v].theMin = min(tree[v<<].theMin,tree[v<<|].theMin);
}
void pushdown(int v) {
if(tree[v].reset){
tree[v].reset = false;
tree[v<<].reset = tree[v<<|].reset = true;
tree[v<<].lazy = tree[v<<|].lazy = tree[v].lazy;
tree[v<<].theMin = tree[v<<].theMax = tree[v].lazy;
tree[v<<|].theMin = tree[v<<|].theMax = tree[v].lazy;
tree[v<<].add = tree[v<<|].add = ;
//cout<<tree[v].lt<<" "<<tree[v].rt<<" "<<tree[v].lazy<<" nmb"<<endl;
}
if(tree[v].add){
tree[v<<].add += tree[v].add;
tree[v<<|].add += tree[v].add;
tree[v<<].theMax += tree[v].add;
tree[v<<].theMin += tree[v].add;
tree[v<<|].theMax += tree[v].add;
tree[v<<|].theMin += tree[v].add;
tree[v].add = ;
}
}
void build(int lt,int rt,int v) {
tree[v].lt = lt;
tree[v].rt = rt;
tree[v].reset = false;
tree[v].add = ;
if(lt == rt) {
scanf("%d",&tree[v].theMin);
tree[v].theMax = tree[v].theMin;
return;
}
int mid = (lt + rt)>>;
build(lt,mid,v<<);
build(mid+,rt,v<<|);
pushup(v);
}
int queryMax(int lt,int rt,int v) {
if(lt <= tree[v].lt && rt >= tree[v].rt) return tree[v].theMax;
pushdown(v);
int theMax = INT_MIN;
if(lt <= tree[v<<].rt) theMax = max(theMax,queryMax(lt,rt,v<<));
if(rt >= tree[v<<|].lt) theMax = max(theMax,queryMax(lt,rt,v<<|));
pushup(v);
return theMax;
}
int queryMin(int lt,int rt,int v) {
if(lt <= tree[v].lt && rt >= tree[v].rt) return tree[v].theMin;
pushdown(v);
int theMin = INT_MAX;
if(lt <= tree[v<<].rt) theMin = min(theMin,queryMin(lt,rt,v<<));
if(rt >= tree[v<<|].lt) theMin = min(theMin,queryMin(lt,rt,v<<|));
pushup(v);
return theMin;
}
void add(int lt,int rt,int val,int v) {
if(lt <= tree[v].lt && rt >= tree[v].rt) {
tree[v].add += val;
tree[v].theMax += val;
tree[v].theMin += val;
return;
}
pushdown(v);
if(lt <= tree[v<<].rt) add(lt,rt,val,v<<);
if(rt >= tree[v<<|].lt) add(lt,rt,val,v<<|);
pushup(v);
}
void updateMax(int lt,int rt,int val,int v) {
if(lt <= tree[v].lt && rt >= tree[v].rt && tree[v].theMax <= val) {
tree[v].reset = true;
tree[v].theMax = tree[v].theMin = val;
tree[v].lazy = val;
tree[v].add = ;
return;
}else if(lt <= tree[v].lt && rt >= tree[v].rt && val <= tree[v].theMin) return;
pushdown(v);
if(lt <= tree[v<<].rt) updateMax(lt,rt,val,v<<);
if(rt >= tree[v<<|].lt) updateMax(lt,rt,val,v<<|);
pushup(v);
}
void updateMin(int lt,int rt,int val,int v) {
if(lt <= tree[v].lt && rt >= tree[v].rt && tree[v].theMin >= val){
tree[v].add = ;
tree[v].reset = true;
tree[v].theMax = tree[v].theMin = val;
tree[v].lazy = val;
return;
}else if(lt <= tree[v].lt && rt >= tree[v].rt && tree[v].theMax <= val) return;
pushdown(v);
if(lt <= tree[v<<].rt) updateMin(lt,rt,val,v<<);
if(rt >= tree[v<<|].lt) updateMin(lt,rt,val,v<<|);
pushup(v);
}
int main() {
int n,q,op,x,y,c,T;
scanf("%d",&T);
while(T--){
scanf("%d %d",&n,&q);
build(,n,);
while(q--){
scanf("%d%d%d",&op,&x,&y);
switch(op){
case :scanf("%d",&c);add(x,y,c,);break;
case :scanf("%d",&c);updateMin(x,y,c,);break;
case :scanf("%d",&c);updateMax(x,y,c,);break;
case :printf("%d %d\n",queryMin(x,y,),queryMax(x,y,));break;
default:;
}
}
}
return ;
}
XTUOJ 1238 Segment Tree的更多相关文章
- BestCoder#16 A-Revenge of Segment Tree
Revenge of Segment Tree Problem Description In computer science, a segment tree is a tree data struc ...
- [LintCode] Segment Tree Build II 建立线段树之二
The structure of Segment Tree is a binary tree which each node has two attributes startand end denot ...
- [LintCode] Segment Tree Build 建立线段树
The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...
- Segment Tree Modify
For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in thi ...
- Segment Tree Query I & II
Segment Tree Query I For an integer array (index from 0 to n-1, where n is the size of this array), ...
- Segment Tree Build I & II
Segment Tree Build I The structure of Segment Tree is a binary tree which each node has two attribut ...
- Lintcode: Segment Tree Query II
For an array, we can build a SegmentTree for it, each node stores an extra attribute count to denote ...
- Lintcode: Segment Tree Modify
For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in thi ...
- Lintcode: Segment Tree Query
For an integer array (index from 0 to n-1, where n is the size of this array), in the corresponding ...
随机推荐
- VS2008 集成Lua解释器
1. 登陆官网下载源代码 -> www.lua.org -> get started -> installing 选择系统类型(这里是Windows的,所下面载 luaDist) ...
- 在PyCharm中以root权限运行和调试python代码
python中有的代码可能需要su权限,如 import os os.mkdir('/media/xxx/disk_a') 如果在交互式环境中使用,需要以sudo的方式启动python.而在PyCha ...
- border:none与border:0的区别
border:none与border:0的区别体现为两点:一是理论上的性能差异,二是浏览器兼容性的差异. 性能差异: [border:0;]把border设为“0”像素效果等于border-width ...
- Copying
Aliasing can make program difficult to read because changes made in one place might have unexpected ...
- 【DNN引用包】
<%@ Register TagPrefix="dnn" TagName="address" Src="~/controls/address.a ...
- CUDA学习笔记(五)
终于实质分析线程的内容了:按照SIMD的方式,每32个线程称为一个线程束,这些线程都执行同一指令,且每个线程都使用私有寄存器进行这一操作请求. 忽然觉得,做CUDA的程序就像是去北京上班:写MPI之后 ...
- TPC-C测试
TPC发布的测试标准之一,是专门针对联机事务处理系统(OLTP)的测试标准.1992年发布1.0版本.最新版本5.11,2010年发布. 测试规范中模拟了一个比较复杂并具有代表意义的OLTP应用环境, ...
- HDU 1240 Asteroids!【BFS】
题意:给出一个三维的空间,给出起点和终点,问是否能够到达终点 和上一题一样,只不过这一题的坐标是zxy输入的, 因为题目中说的是接下来的n行中分别是由n*n的矩形组成的,所以第一个n该是Z坐标,n*n ...
- 51Nod 最长的循环节(打表预处理)
正整数k的倒数1/k,写为10进制的小数如果为无限循环小数,则存在一个循环节,求<=n的数中,倒数循环节长度最长的那个数,假如存在多个最优的答案,输出所有答案中最大的那个数. 1/6= ...
- url链接打开本地应用(测试通过)
基于windows!! 类比mailto://XXXX 主要参考: https://www.cnblogs.com/snow365/p/6428212.html 应用 1.在网页上本地办公 网页应用越 ...