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 ...
随机推荐
- hibernate动态表名映射--仅仅有想不到,没有做不到
近期的一个项目有一个需求,有N个考核单位,要对每一个考核单位生成一张考核情况表.这样做的目的是横切数据库,这这个需求的实现中,我的组员遇到了一个技术问题,我将我的解决的方法和整个思考过程与大家分享, ...
- Python: PS 滤镜--高反差保留 (High pass)
本文用 Python 实现 PS 滤镜中的 高反差保留 特效,具体的算法原理和图像效果可以参考之前的博客: http://blog.csdn.net/matrix_space/article/deta ...
- OpenGL编程逐步深入(七)旋转变换
准备知识 这一节我们来看一下旋转变换.旋转变换指的是给我们一个指点的点和角度,我们需要绕着过该点的轴线將对象旋转对应的角度.这里我们只改变X/Y/Z中的两个分量,第三个分量保持不变.这意味着我们的图形 ...
- 鼠标点击textarea后,在光标后追加内容
$("#insertMsg").on("click",function(){ //获取下拉选项框的值 var textFeildValue = $(" ...
- 四 numpy操作数组输出图片
一.读取一张图片,修改颜色通道后输出 # -*- coding=GBK -*- import cv2 as cv import numpy as np #numpy数组操作 def access_pi ...
- k近邻法(k-nearest neighbor, k-NN)
一种基本分类与回归方法 工作原理是:1.训练样本集+对应标签 2.输入没有标签的新数据,将新的数据的每个特征与样本集中数据对应的特征进行比较,然后算法提取样本最相似数据(最近邻)的分类标签. 3.一般 ...
- bzoj1457: 棋盘游戏 SG函数 Nim
Code: #include<cstdio> #include<cstring> using namespace std; #define maxn 1003 #define ...
- numpy基础篇-简单入门教程3
np import numpy as np np.__version__ print(np.__version__) # 1.15.2 numpy.arange(start, stop, step, ...
- [USACO15JAN]草鉴定Grass Cownoisseur(分层图+tarjan)
[USACO15JAN]草鉴定Grass Cownoisseur 题目描述 In an effort to better manage the grazing patterns of his cows ...
- 09-breack语句