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. Centos7+httpd+fastcgi+rails安装

    搭建的环境: centos7 Apache/2.4.6 fastcgi2.4.6 rails4 在安装fastcgi的时候遇到了问题: 问题: .... .. In file included fro ...

  2. 13.MongoDB 连接命令格式

    转自:https://www.linuxidc.com/Linux/2016-03/129456.htm 使用用户 admin 使用密码 123456 连接到本地的 MongoDB 服务上.输出结果如 ...

  3. javascript中的那些宽度和高度

    window.outerHeight和window.outerWidth  表示整个浏览器窗体的大小,包括任务栏等.       IE9及以上 window.innerHeight和window.in ...

  4. 【C#Windows 服务】 《二》INI配置文件

    一.工具: VS2015+NET Framework4.5. 二.操作: 1.创建INIHelp帮助类 2.丰富帮助类操作 3.windows实例调用 三.代码: 1.INI帮助类: 1 2 3 4 ...

  5. 爬虫--pyquery使用

    强大又灵活的网页解析库. 初始化   字符串初始化 html = ''' <div> <ul> <li class="item-0">first ...

  6. [洛谷P1939]【模板】矩阵加速(数列)

    题目大意:给你一个数列a,规定$a[1]=a[2]=a[3]=1$,$a[i]=a[i-1]+a[i-3](i>3)$求$a[n]\ mod\ 10^9+7$的值. 解题思路:这题看似是很简单的 ...

  7. 参考《深度学习之PyTorch实战计算机视觉》PDF

    计算机视觉.自然语言处理和语音识别是目前深度学习领域很热门的三大应用方向. 计算机视觉学习,推荐阅读<深度学习之PyTorch实战计算机视觉>.学到人工智能的基础概念及Python 编程技 ...

  8. free---显示内存

    free命令可以显示当前系统未使用的和已使用的内存数目,还可以显示被内核使用的内存缓冲区. 语法 free(选项) 选项 -b:以Byte为单位显示内存使用情况: -k:以KB为单位显示内存使用情况: ...

  9. PHP -Casbin: 支持 ACL、RBAC、ABAC 多种模型的 PHP 权限管理框架

    PHP-Casbin 是一个用 PHP 语言打造的轻量级开源访问控制框架( https://github.com/php-casbin... ),目前在 GitHub 开源.PHP-Casbin 采用 ...

  10. P4555 [国家集训队]最长双回文串(回文树)

    题目描述 顺序和逆序读起来完全一样的串叫做回文串.比如acbca是回文串,而abc不是(abc的顺序为abc,逆序为cba,不相同). 输入长度为 n 的串 S ,求 S 的最长双回文子串 T ,即可 ...