线段树例题

2024-05-28 14:36
文章标签 例题 线段

本文主要是介绍线段树例题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

1.Sequence

2.Peach Conference

3.Permutation Subsequence


1.Sequence

题目描述:

Given an array a consisting of n integers, on which you are to perform m operations of two types.

1.Given two integers x,y, replace the number of index x with number y. That is ax:=y.

2.Given one integer x, print the number of consecutive subsequences of a, whose minimum value equals to ax.

It's guaranteed that there are no duplicated value in array a at any moment.

输入描述:

The first line contains two intergers n,m(1≤n,m≤10^5),where n is the size of the array and m is the number of operations to perform.

The second line contains n integer, the ith integer is ai (1≤ai≤2^31-1)

Then,m lines follow, describing m operation you are to perform in order.
Each line start with an integer opt∈[1,2], meaning the type of operation to perform.

If opt=1,two integers x, y (1≤x≤n,1≤y≤2^31-1) follows,mentioned above.

If opt=2,one integers x (1≤x≤n) follows, mentioned above.

输出描述:
For each operation of type 2, print one integer on one line as the answer.

输入样例:

10 5
8 3 6 2 10 9 5 7 1 4 
2 2
1 9 11
1 5 12
2 4
1 8 18

输出样例:

4
28

思路: 用线段树进行op=1的单点修改,用二分+区间查询进行op=2的查找Ax左边最后一个小于Ax的位置ans1,然后查找Ax右边第一个小于Ax的位置ans2,答案就是(x-ans1+1)*(ans2-x+1),这里的二分还是有许多细节需要注意,这里就不多说了,大家看代码理解吧

#include<iostream>
#include<limits.h>
#define int long long
using namespace std;
const int N=1e5+5;
int n,m,w[N];struct node{int l,r;int mind;
}tr[4*N];void pushup(int u)
{tr[u].mind=min(tr[u<<1].mind,tr[u<<1|1].mind);
}void build(int u,int l,int r)
{if(l==r)  tr[u]={l,l,w[l]};else{tr[u]={l,r};int mid=(l+r)>>1;build(u<<1,l,mid),build(u<<1|1,mid+1,r);pushup(u);}
}int query(int u,int l,int r)
{if(tr[u].l>=l&&tr[u].r<=r) return tr[u].mind;else{int mid=(tr[u].l+tr[u].r)>>1;int v=INT64_MAX;if(mid>=l) v=min(query(u<<1,l,r),v);if(mid<r) v=min(v,query(u<<1|1,l,r));return v;}
}void modify(int u,int x,int y)
{if(tr[u].l==x&&tr[u].r==x) tr[u]={x,x,y};else{int mid=(tr[u].l+tr[u].r)>>1;if(mid>=x) modify(u<<1,x,y);else modify(u<<1|1,x,y);pushup(u);}
}int32_t main()
{cin>>n>>m;for(int i=1;i<=n;i++) cin>>w[i];build(1,1,n);while(m--){int type;cin>>type;if(type==1){int x,y;cin>>x>>y;w[x]=y;modify(1,x,y);}else{int x;cin>>x;int a=w[x]; int l1=1,r1=x-1,ans1,ans2;while(l1<=r1)//先二分x的左边{int mid=(l1+r1+1)>>1;if(query(1,mid,x-1)<a) l1=mid+1;//如果mid~x-1这个区间存在比a更小的值,则区间向右边移动一个单位,缩小范围else r1=mid-1;//反之则扩大范围,向左边移动}ans1=l1;//取离x更近的值int l2=x+1,r2=n;while(l2<=r2)//二分x的右边{int mid=(r2+l2)>>1;if(query(1,x+1,mid)<a) r2=mid-1;//如果x+1~mid这个范围存在比a更小的值,则区间向左移动一个单位,缩小范围else l2=mid+1;//反之则扩大范围,向右边移动}ans2=r2;cout<<(x-ans1+1)*(ans2-x+1)<<endl;}}
}

2.Peach Conference

题目描述:

Sun Wukong was honored as the great saint of Qitian. He was very happy, so he held a peach meeting for the monkeys (ID numbered 1 to N). To make it more interesting, Sun Wukong decided to throw the dice. The conference will roll the dice Q times, forming Q instructions, each in the form of ’m a b’, where m is an integer label, and a and b are monkey’s ID. The meaning of each instruction is as follows:

1. If m > 0, send m peaches to each monkey in ID interval [a, b];

2. If m < 0, remove |m| peaches from each monkey in the ID interval [a, b] (if the number of peaches from any monkey is less than |m|, remove all peaches of the monkey);

3. If m = 0, calculate the sum of peaches of each monkey in the interval [a, b]; now you are invited to preside over the peach conference, can you complete the task according to the requirements of Sun Wukong?

输入描述:

The fifirst line contains two positive integers N and Q (1 ≤ N, Q ≤ 100000), representing N monkeys and Q instructions, respectively.

Next, there are Q lines, and each line corresponds to one instruction. Each instruction is composed of three integers m (−10000 ≤ m ≤ 10000), a and b (1 ≤ a ≤ b ≤ N), which respectively represent the label m and the ID interval of monkey.

输出描述:


Output each instruction with label m = 0 as an integer in order per line, that is, the sum of peaches of each monkey in the interval [a, b].

输入样例:

10 8
1 1 10
0 4 6
2 3 6
0 4 5
-2 5 8
0 4 7
-2 4 5
0 3 5

输出样例:

3
6
5
4

思路: 线段树+懒标记,如果在区间[l,r]中最小值减m会>=0,则不用进行标记,如果最大值减m<=0,则要进行标记,具体请看代码

#include<iostream>
#include<cstring>
#define int long long
using namespace std;
const int N=1e5+5;int n,q,w[N];
struct node{int l,r;int sum,mind,maxd,add,clear;
}tr[4*N];void pushdown(int u)
{node &left=tr[u<<1],&right=tr[u<<1|1],&root=tr[u];if(root.clear){left.sum=left.mind=left.maxd=left.add=0;left.clear=1;right.sum=right.mind=right.maxd=right.add=0;right.clear=1;root.clear=0;}if(root.add){left.sum+=(left.r-left.l+1)*root.add;left.mind+=root.add,left.maxd+=root.add;left.add+=root.add;right.sum+=(right.r-right.l+1)*root.add;right.mind+=root.add,right.maxd+=root.add;right.add+=root.add;root.add=0;return ;}}void pushup(int u)
{tr[u].sum=tr[u<<1].sum+tr[u<<1|1].sum;tr[u].maxd=max(tr[u<<1].maxd,tr[u<<1|1].maxd);tr[u].mind=min(tr[u<<1].mind,tr[u<<1|1].mind);}void Build(int u,int l,int r)
{if(l==r) tr[u]={l,l};else{tr[u]={l,r};int mid=(l+r)>>1;Build(u<<1,l,mid),Build(u<<1|1,mid+1,r);pushup(u);}
}void modify(int u,int l,int r,int d)
{if(tr[u].l>=l&&tr[u].r<=r) {if(tr[u].mind+d>=0){tr[u].sum+=(tr[u].r-tr[u].l+1)*d;tr[u].add+=d;tr[u].maxd+=d;tr[u].mind+=d;return ;}if(tr[u].maxd+d<=0){tr[u].sum=tr[u].mind=tr[u].maxd=tr[u].add=0;tr[u].clear=1;return ;}}pushdown(u);int mid=(tr[u].l+tr[u].r)>>1;if(mid>=l) modify(u<<1,l,r,d);if(mid<r) modify(u<<1|1,l,r,d);pushup(u);}int query(int u,int l,int r)
{if(tr[u].l>=l&&tr[u].r<=r)return tr[u].sum;else{pushdown(u);int mid=(tr[u].l+tr[u].r)>>1;int v=0;if(mid>=l)  v=query(u<<1,l,r);if(mid<r) v+=query(u<<1|1,l,r);return v;}
}int32_t main()
{cin>>n>>q;Build(1,1,n);while(q--){int m,l,r;cin>>m>>l>>r;if(m==0) cout<<query(1,l,r)<<endl;else  modify(1,l,r,m);}
}

3.Permutation Subsequence

注意 :要用线段树来优化找区间最值,不然会超时

代码:

#include<iostream>
#include<cmath>
using namespace std;
const int N=2e5+5;
int a[N],book[N],n,k;
struct node{int l, r;int maxd ,mind;
}tr[4*N];void pushup(int u)
{tr[u].maxd=max(tr[u<<1].maxd,tr[u<<1|1].maxd);tr[u].mind=min(tr[u<<1].mind,tr[u<<1|1].mind);
}void build(int u,int l,int r)
{if(l==r) tr[u]={l,l,book[l],book[l]};else {int mid=(l+r)>>1;tr[u]={l,r};build(u<<1,l,mid),build(u<<1|1,mid+1,r);pushup(u);}
}int query(int u,int l,int r,int type)
{if(tr[u].l>=l&&tr[u].r<=r) {if(type==1) return tr[u].mind;else return tr[u].maxd;}else{int mid=(tr[u].l+tr[u].r)>>1;if(type==1){int min_=0x3f3f3f3f;if(mid>=l) min_=query(u<<1,l,r,type);if(mid<r) min_=min(min_,query(u<<1|1,l,r,type));return min_;}else{int max_=0;if(mid>=l) max_=query(u<<1,l,r,type);if(mid<r) max_=max(max_,query(u<<1|1,l,r,type));return max_;}}
}
int main()
{cin>>n>>k;for(int i=1;i<=n;i++) {cin>>a[i];book[a[i]]=i;}int min_=0x3f3f3f3f;build(1,1,n);for(int i=1;i<=n-k+1;i++){int max_=query(1,i,i+k-1,2);int mi_=query(1,i,i+k-1,1);min_=min(min_,abs(max_-mi_));}cout<<min_;
}

这篇关于线段树例题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1010839

相关文章

poj3468(线段树成段更新模板题)

题意:包括两个操作:1、将[a.b]上的数字加上v;2、查询区间[a,b]上的和 下面的介绍是下解题思路: 首先介绍  lazy-tag思想:用一个变量记录每一个线段树节点的变化值,当这部分线段的一致性被破坏我们就将这个变化值传递给子区间,大大增加了线段树的效率。 比如现在需要对[a,b]区间值进行加c操作,那么就从根节点[1,n]开始调用update函数进行操作,如果刚好执行到一个子节点,

hdu1394(线段树点更新的应用)

题意:求一个序列经过一定的操作得到的序列的最小逆序数 这题会用到逆序数的一个性质,在0到n-1这些数字组成的乱序排列,将第一个数字A移到最后一位,得到的逆序数为res-a+(n-a-1) 知道上面的知识点后,可以用暴力来解 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#in

hdu1689(线段树成段更新)

两种操作:1、set区间[a,b]上数字为v;2、查询[ 1 , n ]上的sum 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<queue>#include<set>#include<map>#include<stdio.h>#include<stdl

hdu 1754 I Hate It(线段树,单点更新,区间最值)

题意是求一个线段中的最大数。 线段树的模板题,试用了一下交大的模板。效率有点略低。 代码: #include <stdio.h>#include <string.h>#define TREE_SIZE (1 << (20))//const int TREE_SIZE = 200000 + 10;int max(int a, int b){return a > b ? a :

hdu 1166 敌兵布阵(树状数组 or 线段树)

题意是求一个线段的和,在线段上可以进行加减的修改。 树状数组的模板题。 代码: #include <stdio.h>#include <string.h>const int maxn = 50000 + 1;int c[maxn];int n;int lowbit(int x){return x & -x;}void add(int x, int num){while

poj 1127 线段相交的判定

题意: 有n根木棍,每根的端点坐标分别是 px, py, qx, qy。 判断每对木棍是否相连,当他们之间有公共点时,就认为他们相连。 并且通过相连的木棍相连的木棍也是相连的。 解析: 线段相交的判定。 首先,模板中的线段相交是不判端点的,所以要加一个端点在直线上的判定; 然后,端点在直线上的判定这个函数是不判定两个端点是同一个端点的情况的,所以要加是否端点相等的判断。 最后

HDU4737线段树

题目大意:给定一系列数,F(i,j)表示对从ai到aj连续求或运算,(i<=j)求F(i,j)<=m的总数。 const int Max_N = 100008 ;int sum[Max_N<<2] , x[Max_N] ;int n , m ;void push_up(int t){sum[t] = sum[t<<1] | sum[t<<1|1] ;}void upd

zoj 1721 判断2条线段(完全)相交

给出起点,终点,与一些障碍线段。 求起点到终点的最短路。 枚举2点的距离,然后最短路。 2点可达条件:没有线段与这2点所构成的线段(完全)相交。 const double eps = 1e-8 ;double add(double x , double y){if(fabs(x+y) < eps*(fabs(x) + fabs(y))) return 0 ;return x + y ;

圆与线段的交点

poj 3819  给出一条线段的两个端点,再给出n个圆,求出这条线段被所有圆覆盖的部分占了整条线段的百分比。 圆与线段的交点 : 向量AB 的参数方程  P = A + t * (B - A)      0<=t<=1 ; 将点带入圆的方程即可。  注意: 有交点 0 <= t <= 1 ; 此题求覆盖的部分。 则 若求得 t  满足 ; double ask(d

Codeforces 482B 线段树

求是否存在这样的n个数; m次操作,每次操作就是三个数 l ,r,val          a[l] & a[l+1] &......&a[r] = val 就是区间l---r上的与的值为val 。 也就是意味着区间[L , R] 每个数要执行 | val 操作  最后判断  a[l] & a[l+1] &......&a[r] 是否= val import ja