码迷,mamicode.com
首页 > 其他好文 > 详细

线段树一些基本的操作;

时间:2017-04-12 23:33:08      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:color   区间   int   tree   clu   algo   替换   ring   div   

更新于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

 

线段树一些基本的操作;

标签:color   区间   int   tree   clu   algo   替换   ring   div   

原文地址:http://www.cnblogs.com/Lazers/p/6701604.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!