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. ssh跳板登陆太麻烦,使用expect每次自动登录 利用expect 模拟键盘动作,在闲置时间之内模拟地给个键盘响应

    #!/usr/bin/expect -f #设置超时时间 set timeout #这里设置了跳板机的密码 set password "你的跳板机密码" #连接跳板机 spawn ...

  2. nyoj--1009--So Easy[Ⅰ](数学)

    So Easy[Ⅰ] 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 给出任意一个三角形的三个边a,b,c. 要求:求出这个三角形的外接圆半径. 输入 输入数据有多组. ...

  3. [POJ2728] Desert King 解题报告(最优比率生成树)

    题目描述: David the Great has just become the king of a desert country. To win the respect of his people ...

  4. Kali linux 2016.2(Rolling)里Metasploit的常用模块

    端口扫描 auxiliary/scanner/portscanscanner/portscan/ack ACK防火墙扫描scanner/portscan/ftpbounce FTP跳端口扫描scann ...

  5. 请问这个git上开源的node项目怎样才能在windows用Npm跑起来

    这个项目https://github.com/wechaty/we...以前都是用人家弄好的手脚架搞得es6,搞了2天搞起了es6还报错,错误信息在下面,然后我想请教大神:1我到底应该怎么弄才能在wi ...

  6. 射击的乐趣:WIN32诠释打飞机游戏

    一楼留给链接http://blog.csdn.net/crocodile__/article/details/11860129 楼上神贴,膜拜片刻...... 一.游戏玩法和已经实现的功能 1.打开游 ...

  7. 三分钟明白 Activiti工作流 -- java运用_转载

    一. 什么是工作流 以请假为例,现在大多数公司的请假流程是这样的 员工打电话(或网聊)向上级提出请假申请——上级口头同意——上级将请假记录下来——月底将请假记录上交公司——公司将请假录入电脑 采用工作 ...

  8. Java调用Python遇到的一系列问题与解决方案

    首先,百度了几个方法 1.用jython里的一个jar包,jython.jar,里面封装了一个专门调用Python的类, 但是不知道为什么我用Java一调用就报错,因此放弃.   2.用runtime ...

  9. C#中使用Dictionary实现Map数据结构——VC编程网

    转载自: http://blog.51cto.com/psnx168 在VC中使用过CMap以及在Java中使用过Map的朋友应该很熟悉,使用Map可以方便实现基于键值对数据的处理,在C#中,你就需要 ...

  10. javascript面向对象编程,带你认识封装、继承和多态

    原文链接:点我 周末的时候深入的了解了下javascript的面向对象编程思想,收获颇丰,感觉对面向对象编程有了那么一丢丢的了解了~很开森 什么是面向对象编程 先上一张图,可以对面向对象有一个大致的了 ...