• 线段树一些基本的操作;


    更新于2017 4 12 

    目前学的线段树的操作;

    1.某段区间+v;

    2。替换某个值(这个代码没涉及0.0)

    3.查询区间和,最大值,最小值;

    部分代码如下

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cstdio>
    #inlcude<algorithm>
    #include<queue>
    #include<vector>
    priority_queue<int,vector <int> ,greater<int> >s;
    using namespace std;
    int x,y,v;
    struct{
        int dalta,sum,max,min;
    }tree[1000000];
    void getdown(int left,int right,int root)
    {
        int dalta=tree[root].dalta;
        //tree[root<<1].max+=dalta;
        //tree[root<<1].min+=dalta;    
        //tree[root<<1].sum+=(right-left+1)*dalat;    
        //tree[root<<1+1].max+=dalta;
        //tree[root<<1+1].min+=dalta;    
        //tree[root<<1+1].sum+=(right-left+1)*dalat;
        tree[root].dalta=0;    
    }
    void getnow(int root)
    {
        //tree[root].max=max(tree[root*2].max,tree[root*2+1].max);
        //tree[root].min=min(tree[root*2].min,tree[root*2+1].min);
        //tree[root].sum=tree[root*2].sum+tree[root*2+1].sum;
        return ;
    }
    void updata(int left,int right,int root)
    {    
        if(x>right||y<left)return ;
        if(x<=left&&y>=right)
        {
            //tree[root].min+=v;
            //tree[root].max+=v;
            //tree[root].sum+=(right-left+1)*v;
            //tree[root].dalta+=v;
            return ;
        }
        getdown(left,right,root);
        int mid=(left+right)>>1;
        updata(left,mid,root*2);
        updata(mid+1,right,root*2+1);
        getnow(root);
    }
    int search(int left,int right,int root)
    {
        if(x>right||y<left)
        {    //return 0;return -222222222;return 22222222}
        if(x<=left&&y>=right)
        {
            //return tree[root].sum;
            //return tree[root].max;
            //return tree[root].min;
        }
        int mid=(right+left)>>1;
        search(left,mid,root*2);
        search(mid+1,right,root*2+1);
        //return max(tree[root*2].max,tree[root*2+1].max);
        //return min(tree[root*2].min,tree[root*2+1].min)'
        //return tree[root*2].sum+tree[root*2+1].sum;
    }
    View Code
  • 相关阅读:
    ui自动化-则神第一天04-学习方法
    PHP unserialize()
    路径中 斜杠/和反斜杠 的区别
    PhpStorm 克隆下来的项目改动,版本控制不起效果
    PhpStorm 回到上次编辑位置的快捷键
    mysql max_allowed_packet查询和修改
    PHP大批量插入数据库的3种方法和速度对比
    mysql5.7.23安装详细过程
    JS 放大镜
    aviary 图片编辑器
  • 原文地址:https://www.cnblogs.com/Lazers/p/6701604.html
Copyright © 2020-2023  润新知