线段树例题

news2024/9/25 23:24:42

目录

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.coloradmin.cn/o/1705798.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

httpsok-v1.12.0支持LB证书自动部署

&#x1f525;httpsok-v1.12.0支持LB证书自动部署 介绍 httpsok 是一个便捷的 HTTPS 证书自动续签工具&#xff0c;基于全新的设计理念&#xff0c;专为 Nginx 、OpenResty 服务器设计。已服务众多中小企业&#xff0c;稳定、安全、可靠。 一行命令&#xff0c;一分钟轻松搞…

console.log——NPM库

前期回顾 Vue3 TS 项目实战 - 后台管理系统 - 按钮权限_vue3ts后台管理-CSDN博客 目录 &#x1f6a9;不使用NPM插件的方式 第一步&#xff1a;创建log函数-源码 第二步&#xff1a;注册到window上 第三步&#xff1a;扩展Window接口 第四步&#xff1a;确保类型文件…

leetcode 530.二叉搜索树的最小绝对差 、501.二叉搜索树中的众数 、236. 二叉树的最近公共祖先

leetcode 530.二叉搜索树的最小绝对差 、501.二叉搜索树中的众数 、236. 二叉树的最近公共祖先 leetcode 530.二叉搜索树的最小绝对差 题目链接&#xff1a;https://leetcode.cn/problems/maximum-binary-tree/description/ 题目&#xff1a; 给你一个二叉搜索树的根节点 r…

【Qt】Qt定时器类QTimer

在进行窗口程序的处理过程中, 经常要周期性的执行某些操作, 或者制作一些动画效果&#xff0c;看似比较复杂的问题使用定时器就可以完美的解决这些问题&#xff0c; Qt中提供了两种定时器方式一种是使用Qt中的事件处理函数这个在后续章节会给大家做细致的讲解&#xff0c;本节主…

常用目标检测预训练模型大小及准确度比较

目标检测是计算机视觉领域中的一项重要任务&#xff0c;旨在检测和定位图像或者视频中的目标对象。当人类观看图像或视频时&#xff0c;我们可以在瞬间识别和定位感兴趣的对象。目标检测的目标是使用计算机复制这种智能。 近年来&#xff0c;目标检测网络的发展日益成熟&#…

设计模式基础——设计原则介绍

1.概述 ​ 对于面向对象软件系统的设计而言&#xff0c;如何同时提高一个软件系统的可维护性、可复用性、可拓展性是面向对象设计需要解决的核心问题之一。面向对象设计原则应运而生&#xff0c;这些原则你会在设计模式中找到它们的影子&#xff0c;也是设计模式的基础。往往判…

力扣刷题--1528. 重新排列字符串【简单】

题目描述 给你一个字符串 s 和一个 长度相同 的整数数组 indices 。 请你重新排列字符串 s &#xff0c;其中第 i 个字符需要移动到 indices[i] 指示的位置。 返回重新排列后的字符串。 示例 1&#xff1a; 输入&#xff1a;s “codeleet”, indices [4,5,6,7,0,2,1,3] 输…

OpenMV的VisionBoard视觉识别开发板学习记录

此篇博客仅用于对VisionBoard的开发板的学习研究记录&#xff0c;没有教学内容。 一、资料来源 开发板资料链接 开发板环境搭建手册 开发板视频教程 板子的资料网站 openmv官方的网站 目录 一、资料来源二、针对 VisionBoard的目标识别和定位总结1. 目标识别功能1.1 物体检测…

大模型时代:生活将如何被重塑?

大模型时代&#xff1a;生活将如何被重塑&#xff1f; &#x1f604;生命不息&#xff0c;写作不止 &#x1f525; 继续踏上学习之路&#xff0c;学之分享笔记 &#x1f44a; 总有一天我也能像各位大佬一样 &#x1f3c6; 博客首页 怒放吧德德 To记录领地 &#x1f31d;分享…

数据挖掘与机器学习——回归分析

目录 回归分析定义&#xff1a; 案例&#xff1a; 线性回归 预备知识&#xff1a; 定义&#xff1a; 一元线性回归&#xff1a; 如何找出最佳的一元线性回归模型&#xff1a; 案例&#xff1a; python实现&#xff1a; 多元线性回归 案例&#xff1a; 线性回归的优缺…

Codeforces Round 948 (Div. 2) D. XORificator(哈希)

题目 n*m(n*m<3e5)的矩阵&#xff0c; 实际为t(t<1e4)组样例&#xff0c;但保证sum n*m不超过3e5 你可以选一行把所有01翻转&#xff0c;问最多可以让多少列只有一个1&#xff0c;然后把你翻转的行输出 思路来源 其实题还挺裸的&#xff0c;教了一下潘老师&#xff0…

8D和FMEA的关系是什么?——FMEA软件

免费试用FMEA软件-免费版-SunFMEA 在质量管理领域中&#xff0c;8D和FMEA是两个非常重要的工具。它们各自有着独特的作用&#xff0c;但同时又存在着紧密的联系。本文旨在深入探讨8D和FMEA之间的关系&#xff0c;以及它们如何协同工作以提高产品质量和客户满意度。 8D&#x…

【JavaEE精炼宝库】多线程(3)线程安全 | synchronized

目录 一、线程安全 1.1 经典线程不安全案例&#xff1a; 1.2 线程安全的概念&#xff1a; 1.3 线程不安全的原因&#xff1a; 1.3.1 案例刨析: 1.3.2 线程不安全的名词解释&#xff1a; 1.3.3 Java 内存模型 (JMM)&#xff1a; 1.3.4 解决线程不安全问题&#xff1a; 二…

紫光展锐前沿探索 | 满足未来6G多差异化应用场景的技术体系思考

在6G架构/系统设计中&#xff0c;紫光展锐提出了未来6G空口“一体多翼”的技术体系概念&#xff0c;即“Big-Lite Multi-RAT”。本文将详细对该技术体系展开介绍。 “一体多翼”技术体系通过 “体”&#xff08;Big RAT&#xff09;和“翼”&#xff08;Lite RAT&#xff09;的…

数据结构:树(2)【堆排序】【堆排序的时间复杂度】【topk】

一.堆排序 关于排序我们还是挺熟悉的&#xff0c;像是冒泡排序&#xff0c;快速排序等等。这里多介绍一种也挺牛的排序&#xff0c;叫做堆排序。在我的上一篇博客里我们了解到了关于堆的概念&#xff0c;堆又分为大堆和小堆。那么如果我们在进行堆排序的过程中&#xff0c;我们…

cs与msf权限传递,mimikatz抓取明文密码

目录 一、cs与msf权限传递 二、mimikatz抓取明文密码 一、cs与msf权限传递 cs传到msf&#xff1a; 创建foreign监听器-->msf监听模块设置端口-->cs执行新建会话选择创建的监听器 1.创建监听器&#xff1a; 2.msf监听设置端口&#xff1a; use exploit/multi/hander s…

计算机网络学习

文章目录 第一章信息时代的计算机网络因特网概述电路交换&#xff0c;分组交换&#xff0c;报文交换计算机网络的定义和分类计算机网络的性能指标常见的三种计算机网络体系计算机网络体系结构分层的必要性计算机网络体系结构分层思想举例计算机网络体系结构中的专用术语 第二章…

类别型特征

#机器学习 #深度学习 #基础知识 #特征工程 #数据编码 背景 在现实生活中,我们面对的数据类型有很多,其中有的数据天然为数值类型具备数值意义,那么可以很自然地和算法结合,但是大部分数据他没有天然的数值意义,那么将他们送入到算法前,就需要对数据进行编码处理,将其转换为数…

php -v在cmd中正常显示 在vscode中却报错

效果展示 原因 在vscode中 终端是 PowerShell PowerShell 默认情况下它不会继承系统的PATH环境变量 解决方案 使用CMD作为终端 打开VSCode设置&#xff08;File > Preferences > Settings 或 Ctrl,&#xff09;。搜索 terminal.integrated.shell.windows。更改其值…

vue3主题切换按钮与功能实现

代码: <template><div class"slideThree"><label class"theme-switch"><inputtype"checkbox"class"checkbox"v-model"isChecked"change"setTheme"id"slideThree"name"check…