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

  1. BestCoder#16 A-Revenge of Segment Tree

    Revenge of Segment Tree Problem Description In computer science, a segment tree is a tree data struc ...

  2. [LintCode] Segment Tree Build II 建立线段树之二

    The structure of Segment Tree is a binary tree which each node has two attributes startand end denot ...

  3. [LintCode] Segment Tree Build 建立线段树

    The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...

  4. Segment Tree Modify

    For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in thi ...

  5. 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), ...

  6. Segment Tree Build I & II

    Segment Tree Build I The structure of Segment Tree is a binary tree which each node has two attribut ...

  7. Lintcode: Segment Tree Query II

    For an array, we can build a SegmentTree for it, each node stores an extra attribute count to denote ...

  8. Lintcode: Segment Tree Modify

    For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in thi ...

  9. 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 ...

随机推荐

  1. 【甘道夫】Sqoop1.99.3基础操作--导入Oracle的数据到HDFS

    第一步:进入clientShell fulong@FBI008:~$ sqoop.sh client Sqoop home directory: /home/fulong/Sqoop/sqoop-1. ...

  2. HDOJ 5296 Annoying problem LCA+数据结构

    dfs一遍得到每一个节点的dfs序,对于要插入的节点x分两种情况考虑: 1,假设x能够在集合中的某些点之间,找到左边和右边距离x近期的两个点,即DFS序小于x的DFS序最大点,和大于x的DFS序最小的 ...

  3. 基于深度学习的病毒检测技术无需沙箱环境,直接将样本文件转换为二维图片,进而应用改造后的卷积神经网络 Inception V4 进行训练和检测

    话题 3: 基于深度学习的二进制恶意样本检测 分享主题:全球正在经历一场由科技驱动的数字化转型,传统技术已经不能适应病毒数量飞速增长的发展态势.而基于沙箱的检测方案无法满足 APT 攻击的检测需求,也 ...

  4. C#-汉字转拼音缩写

    /// 〈summary〉 /// 汉字转拼音缩写 /// Code By MuseStudio@hotmail.com /// 2004-11-30 /// 〈/summary〉 /// 〈para ...

  5. manacherO(n)求最长回文子串 hihocoder1032

    原文地址:https://segmentfault.com/a/1190000003914228   http://blog.csdn.net/synapse7/article/details/189 ...

  6. Hadoop集群配置搭建

    环境:Centos 6.9,Hadoop 2.7.1,JDK 1.8.0_161,Maven 3.3.9 前言: 1.配置一台master服务器,两台或多台slave服务器.    2.master可 ...

  7. 学习Go语言之使用原子访问或互斥锁解决竞态问题

    使用原子访问或互斥锁 // 解决竞态问题 package main import ( "fmt" "sync" "sync/atomic" ...

  8. JavaScript笔记(1)

    JS前导: ECMA欧洲计算机制造者协会 ECMA-262 (ES5规范) ECMA-404(Json规范) wsc定义HTML.CSS.DOM规范 计算机程序分为: cpu密集(用于计算) I/O密 ...

  9. JDBC连接SQL Server遇到的问题

    需要使用到微软的JDBC sql server的驱动类,去官网下载jar包 使用的URL模式:"jdbc:sqlserver:地址:端口//;databaseName=YourDatabas ...

  10. Rman备份异机恢复

    最后更新时间:2018/12/29 前置条件 已准备一台安装好Centos6+oracle11gr2 软件的服务器; 只安装了 oracle 数据库软件,需要手工创建以下目录: #环境变量 expor ...