更新于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; }
原文:http://www.cnblogs.com/Lazers/p/6701604.html