线段树学习

来源:互联网 发布:华为it 编辑:程序博客网 时间:2024/05/18 02:37

线段树的根节点从1开始。它的左子树的编号是2 * n,右子树为2* n + 1;

经典例题:

hdu1166 敌兵布阵

#include<bits/stdc++.h>using namespace std;const int maxn = 50000 + 10;typedef long long ll;struct Tree{int l;int r;ll sum;}tree[maxn * 4];int a[maxn];void build(int root,int l,int r){tree[root].l = l;tree[root].r = r;if(tree[root].l == tree[root].r){tree[root].sum = a[l];return;}int mid = (l + r) / 2;build(root* 2, l, mid);build(root * 2 + 1,mid + 1,r);tree[root].sum = tree[root * 2].sum + tree[root * 2 + 1].sum;}int getsum(int root,int L,int R){ll ret = 0;if(L <= tree[root].l  && tree[root].r <= R){ret += tree[root].sum;return ret;}int mid = (tree[root].l + tree[root].r) / 2;if(L <= mid)ret += getsum(root*2,L,R);if(R >= mid + 1)ret += getsum(root * 2 + 1,L,R);return ret;}void update(int root,int val,int pos){if(tree[root].l == tree[root].r){tree[root].sum = val;return ;}int mid =   ( tree[root].l + tree[root].r) / 2;if(pos <= mid)update(root * 2,val,pos);else update(root*2 + 1,val,pos);    tree[root].sum = tree[root * 2].sum + tree[root * 2 + 1].sum;}void Print(int root){    cout << root << " " << tree[root].l << " " << tree[root].r << " "<< tree[root].sum << endl;    if(tree[root].l == tree[root].r)        return;    Print(root * 2);    Print(root * 2 + 1);}int main(){int Tcase;scanf("%d",&Tcase);for(int ii = 1; ii <= Tcase; ii ++){int n;scanf("%d",&n);for(int i = 1; i <= n; i ++)scanf("%d",&a[i]);        build(1,1,n);//        cout << getsum(1,2,7) << endl;//        cout << endl;//        Print(1);cout << "Case " <<ii << ":" << endl;char s[10];int x,y;while(scanf("%s",s) && s[0] != 'E'){scanf("%d%d",&x,&y);if(s[0] == 'Q'){if(x > y){int t = x;x = y;y = t;}cout << getsum(1,x,y) << endl;}else if(s[0] == 'A'){    a[x] += y;update(1,a[x],x);//Print(1);}else if(s[0] == 'S'){    a[x] -= y;update(1,a[x],x);}}}return 0;}


hdu 1754 I hate it

#include<bits/stdc++.h>using namespace std;const int maxn = 200000 + 10;typedef long long ll;struct Tree{int l;int r;int maxs;}tree[maxn * 4];int a[maxn];void build(int root,int l,int r){tree[root].l = l;tree[root].r = r;if(tree[root].l == tree[root].r){tree[root].maxs = a[l];return;}int mid = (l + r) / 2;build(root* 2, l, mid);build(root * 2 + 1,mid + 1,r);tree[root].maxs = max(tree[root * 2].maxs , tree[root * 2 + 1].maxs);}int getsum(int root,int L,int R){int ret = 0;if(L <= tree[root].l  && tree[root].r <= R){return tree[root].maxs;}int mid = (tree[root].l + tree[root].r) / 2;if(L <= mid)ret = max(ret,getsum(root*2,L,R));if(R >= mid + 1)ret = max(ret,getsum(root * 2 + 1,L,R));return ret;}void update(int root,int val,int pos){if(tree[root].l == tree[root].r){tree[root].maxs = val;return ;}int mid =   ( tree[root].l + tree[root].r) / 2;if(pos <= mid)update(root * 2,val,pos);else update(root*2 + 1,val,pos);    tree[root].maxs = max(tree[root * 2].maxs , tree[root * 2 + 1].maxs);}//void Print(int root)//{//    cout << root << " " << tree[root].l << " " << tree[root].r << " "<< tree[root].maxs << endl;//    if(tree[root].l == tree[root].r)//        return;////    Print(root * 2);//    Print(root * 2 + 1);//}int main(){//int Tcase;//scanf("%d",&Tcase);//for(int ii = 1; ii <= Tcase; ii ++)    int n,m;    while( ~ scanf("%d%d",&n,&m) ){for(int i = 1; i <= n; i ++)scanf("%d",&a[i]);        build(1,1,n);//        Print(1);//cout << "Case " <<ii << ":" << endl;char s[10];int x,y;//scanf("%d",&m);//while(scanf("%s",s) && s[0] != 'E')        while(m --){scanf("%s%d%d",s,&x,&y);if(s[0] == 'Q'){if(x > y){int t = x;x = y;y = t;}cout << getsum(1,x,y) << endl;}else if(s[0] == 'U'){update(1,y,x);}}}return 0;}



0 0
原创粉丝点击