数据结构&算法学习笔记: 线段树

  • 单点更新、区间求和以及求区间求最大值
#define lson rt<<1, left, m
#define rson rt<<1|1, m + 1, right
const int maxn = 1e5+5;
using namespace std;
struct Node
{
    int value;
    int left,right;
    int mid()
    {
        return (left + right) >> 1;
    }
}node[maxn<<2];
int lindex[maxn];//保存叶子节点在线段树中的下标
void PushUp(int rt)
{
    node[rt].value = node[rt<<1].value + node[rt<<1|1].value;
    //node[rt].value = max(node[rt<<1].value, node[rt<<1|1].value);
}
void BuildTree(int rt, int left, int right)
{
    node[rt].left = left;
    node[rt].right = right;
    node[rt].value = 0;
    if(left == right)
    {
        scanf("%d",&node[rt].value);
        lindex[left] = rt;
        return ;
    }
    int m = node[rt].mid();
    BuildTree(lson);
    BuildTree(rson);
    PushUp(rt);
}
void Update(int rt)
{
    if(rt == 1)return ;
    int f = rt >> 1;
    int a = node[f<<1].value;
    int b = node[f<<1|1].value;
    node[f].value = a + b;//区间求和
    //node[f].value = max(a,b);//区间求最大值
    Update(f);
}
int ans;
void Query(int rt, int left, int right)
{
    if(node[rt].left == left && node[rt].right == right)
    {
        ans += node[rt].value;//区间求和
        //ans = max(ans, node[rt].value);//区间求最大值
        return ;
    }
    int m = node[rt].mid();
    if(right <= m) Query(rt<<1, left, right);
    else if(left > m) Query(rt<<1|1, left, right);
    else{
        Query(lson);
        Query(rson);
    }
}

习题练习
HDU 1754 ( 传送门 )
I Hate It
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K
(Java/Others)
Problem Description
很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。
这让很多学生很反感。
不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。
Input
本题目包含多组测试,请处理到文件结束。
在每个测试的第一行,有两个正整数 N 和 M ( 0 < N <= 200000,0 < M < 5000 ),分别代表学生的数目和操作的数目。
学生ID编号分别从1编到N。
第二行包含N个整数,代表这N个学生的初始成绩,其中第i个数代表ID为i的学生的成绩。
接下来有M行。每一行有一个字符 C (只取’Q’或’U’) ,和两个正整数A,B。
当C为’Q’的时候,表示这是一条询问操作,它询问ID从A到B(包括A,B)的学生当中,成绩最高的是多少。
当C为’U’的时候,表示这是一条更新操作,要求把ID为A的学生的成绩更改为B。
Output
对于每一次询问操作,在一行里面输出最高成绩。
Sample Input
5 6
1 2 3 4 5
Q 1 5
U 3 6
Q 3 4
Q 4 5
U 2 9
Q 1 5
Sample Output
5
6
5
9

Hint Huge input,the C function scanf() will work better than cin

#include<iostream>
#include<cstdio>
#define lson rt<<1, left, m
#define rson rt<<1|1, m + 1, right
const int maxn = 2e5+5;
using namespace std;
struct Node
{
    int value;
    int left,right;
    int mid()
    {
        return (left + right) >> 1;
    }
}node[maxn<<2];
int lindex[maxn];
void PushUp(int rt)
{
    node[rt].value = max(node[rt<<1].value, node[rt<<1|1].value);
}
void BuildTree(int rt, int left, int right)
{
    node[rt].left = left;
    node[rt].right = right;
    node[rt].value = 0;
    if(left == right)
    {
        cin>>node[rt].value;
        lindex[left] = rt;
        return ;
    }
    int m = node[rt].mid();
    BuildTree(lson);
    BuildTree(rson);
    PushUp(rt);
}
void Update(int rt)
{
    if(rt == 1)return ;
    int f = rt >> 1;
    int a = node[f<<1].value;
    int b = node[f<<1|1].value;
    node[f].value = max(a,b);
    Update(f);
}
int ans;
void Query(int rt, int left, int right)
{
    if(node[rt].left == left && node[rt].right == right)
    {
        ans = max(ans, node[rt].value);
        return ;
    }
    int m = node[rt].mid();
    if(right <= m) Query(rt<<1, left, right);
    else if(left > m) Query(rt<<1|1, left, right);
    else{
        Query(lson);
        Query(rson);
    }
}
int main()
{
    ios::sync_with_stdio(false);
    int n, m;
    while(cin>>n>>m)
    {
        BuildTree(1, 1, n);
        while(m--)
        {
            string s;
            int a, b;
            cin>>s>>a>>b;
            if(s[0] == 'Q')
            {
                ans = 0;
                Query(1, a, b);
                cout<<ans<<endl;
            }
            else
            {
                if(s[0] == 'U')
                {
                    node[lindex[a]].value = b;
                    Update(lindex[a]);
                }
            }
        }
    }
    return 0;
}

NYOJ 116 ( 传送门 )
士兵杀敌(二)
时间限制:1000 ms | 内存限制:65535 KB
难度:5
描述
南将军手下有N个士兵,分别编号1到N,这些士兵的杀敌数都是已知的。

小工是南将军手下的军师,南将军经常想知道第m号到第n号士兵的总杀敌数,请你帮助小工来回答南将军吧。

南将军的某次询问之后士兵i可能又杀敌q人,之后南将军再询问的时候,需要考虑到新增的杀敌数。

输入
只有一组测试数据
第一行是两个整数N,M,其中N表示士兵的个数(1 < N < 1000000),M表示指令的条数。(1 < M < 100000)
随后的一行是N个整数,ai表示第i号士兵杀敌数目。(0<=ai<=100)
随后的M行每行是一条指令,这条指令包含了一个字符串和两个整数,首先是一个字符串,如果是字符串QUERY则表示南将军进行了查询操作,后面的两个整数m,n,表示查询的起始与终止士兵编号;如果是字符串ADD则后面跟的两个整数I,A(1 <= I <= N,1 <= A <= 100),表示第I个士兵新增杀敌数为A.
输出
对于每次查询,输出一个整数R表示第m号士兵到第n号士兵的总杀敌数,每组输出占一行
样例输入
5 6
1 2 3 4 5
QUERY 1 3
ADD 1 2
QUERY 1 3
ADD 2 3
QUERY 1 2
QUERY 1 5
样例输出
6
8
8
20

#include<iostream>
#include<cstdio>
#define lson rt<<1, left, m
#define rson rt<<1|1, m + 1, right
const int maxn = 1e6+5;
using namespace std;
struct Node
{
    int value;
    int left,right;
    int mid()
    {
        return (left + right) >> 1;
    }
}node[maxn<<2];
int lindex[maxn];
void PushUp(int rt)
{
    node[rt].value = node[rt<<1].value + node[rt<<1|1].value;
}
void BuildTree(int rt, int left, int right)
{
    node[rt].left = left;
    node[rt].right = right;
    node[rt].value = 0;
    if(left == right)
    {
        cin>>node[rt].value;
        lindex[left] = rt;
        return ;
    }
    int m = node[rt].mid();
    BuildTree(lson);
    BuildTree(rson);
    PushUp(rt);
}
void Update(int rt)
{
    if(rt == 1)return ;
    int f = rt >> 1;
    int a = node[f<<1].value;
    int b = node[f<<1|1].value;
    node[f].value = a + b;
    Update(f);
}
int ans;
void Query(int rt, int left, int right)
{
    if(node[rt].left == left && node[rt].right == right)
    {
        ans += node[rt].value;
        return ;
    }
    int m = node[rt].mid();
    if(right <= m) Query(rt<<1, left, right);
    else if(left > m) Query(rt<<1|1, left, right);
    else{
        Query(lson);
        Query(rson);
    }
}
int main()
{
    int n, m;
    ios::sync_with_stdio(false);
    while(cin>>n>>m)
    {
        BuildTree(1, 1, n);
        string s;
        int a, b;
        for(int i = 0; i < m; i++)
        {
            ans = 0;
            string s;
            cin>>s>>a>>b;
            if(s[0] == 'Q'){
                Query(1, a, b);
                cout<<ans<<endl;
            }
            else{
                if(s[0] == 'A')
                {
                    node[lindex[a]].value += b;
                    Update(lindex[a]);
                }
            }
        }
    }
    return 0;
}

HDU 1166( 传送门 ) 与上述题目类似。

  • 区间修改,需要使用lazy标志,从上到下,遇到子区间,修改停止不再向下修改。若下次修改涉及它的子树则将lazy 标志分解到它的左右子树。
//此处的区间修改是【a,b】的区间每个值增加c
#define lson rt<<1, left, m
#define rson rt<<1|1, m + 1, right
#define maxn 100004
using namespace std;
struct Node{
    int left, right;
    int value;
    int add;//lazy标志
    int mid()
    {
        return (left + right) >> 1;
    }
}node[maxn<<2];
void PushUp(int rt)
{
    node[rt].value = node[rt<<1].value + node[rt<<1|1].value;
}
void BuildTree(int rt, int left, int right)
{
    node[rt].left = left;
    node[rt].right = right;
    node[rt].add = 0;
    if(left == right)
    {
        scanf("%d",&node[rt].value);
        return ;
    }
    int m = node[rt].mid();
    BuildTree(lson);
    BuildTree(rson);
    PushUp(rt);
}
void PushDown(int rt, int len)//向下打压lazy标志
{//len是区间长度
    if(node[rt].add)
    {
        node[rt<<1].add += node[rt].add;
        node[rt<<1|1].add += node[rt].add;
        node[rt<<1].value += node[rt].add * (len - (len >> 1));//len - (len >> 1)是左线段区间的长度
        node[rt<<1|1].value += node[rt].add * (len >> 1);//len>>1是右线段区间的长度
        node[rt].add = 0;
    }
}
void Update(int rt, int left, int right, int c)
{
    if(node[rt].left == left && node[rt].right == right)
    {
        node[rt].value += (right - left + 1) * c;
        node[rt].add += c;
        return ;
    }
    PushDown(rt, node[rt].right - node[rt].left + 1);
    int m = node[rt].mid();
    if(right <= m)Update(rt<<1,left,right, c);
    else if(left > m)Update(rt<<1|1,left,right, c);
    else {
        Update(lson, c);
        Update(rson, c);
    }
    PushUp(rt);
}
void Query(int rt, int left, int right)
{
    if(node[rt].left == left && node[rt].right == right)
    {
        ans += node[rt].value;
        return ;
    }
    PushDown(rt, node[rt].right - node[rt].left + 1);
    int m = node[rt].mid();
    if(right <= m) Query(rt<<1,left,right);
    else if(left > m) Query(rt<<1|1,left,right);
    else{
        Query(lson);
        Query(rson);
    }
}

习题
Poj 3468 ( 传送门 )
A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072K
Case Time Limit: 2000MS
Description

You have N integers, A1, A2, … , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.

Input

The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, … , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
“C a b c” means adding c to each of Aa, Aa+1, … , Ab. -10000 ≤ c ≤ 10000.
“Q a b” means querying the sum of Aa, Aa+1, … , Ab.

Output

You need to answer all Q commands in order. One answer in a line.

Sample Input

10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4
Sample Output

4
55
9
15
Hint

The sums may exceed the range of 32-bit integers.

#include<iostream>
#include<cstdio>
#define lson rt<<1, left, m
#define rson rt<<1|1, m + 1, right
#define maxn 100004
using namespace std;
struct Node{
    int left, right;
    long long sum;
    long long add;
    int mid()
    {
        return (left + right) >> 1;
    }
}node[maxn<<2];

void PushUp(int rt)
{
    node[rt].sum = node[rt<<1].sum + node[rt<<1|1].sum;
}

void PushDown(int rt, int len)
{
    if(node[rt].add)
    {
        node[rt<<1].add += node[rt].add;
        node[rt<<1|1].add += node[rt].add;
        node[rt<<1].sum += node[rt].add * (len - (len >> 1));//
        node[rt<<1|1].sum += node[rt].add * (len >> 1);//
        node[rt].add = 0;
    }
}

void BuildTree(int rt, int left, int right)
{
    node[rt].left = left;
    node[rt].right = right;
    node[rt].add = 0;
    if(left == right)
    {
        scanf("%I64d",&node[rt].sum);
        return ;
    }
    int m = node[rt].mid();
    BuildTree(lson);
    BuildTree(rson);
    PushUp(rt);
}

void Update(int rt, int left, int right, long long c)
{
    if(node[rt].left == left && node[rt].right == right)
    {
        node[rt].sum += (right - left + 1) * c;
        node[rt].add += c;
        return ;
    }
    PushDown(rt, node[rt].right - node[rt].left + 1);
    int m = node[rt].mid();
    if(right <= m)Update(rt<<1,left,right, c);
    else if(left > m)Update(rt<<1|1,left,right, c);
    else {
        Update(lson, c);
        Update(rson, c);
    }
    PushUp(rt);
}

long long ans;
void Query(int rt, int left, int right)
{
    if(node[rt].left == left && node[rt].right == right)
    {
        ans += node[rt].sum;
        return ;
    }
    PushDown(rt, node[rt].right - node[rt].left + 1);
    int m = node[rt].mid();
    if(right <= m) Query(rt<<1,left,right);
    else if(left > m) Query(rt<<1|1,left,right);
    else{
        Query(lson);
        Query(rson);
    }
}
int main()
{
    int n,q;
    while(~scanf("%d%d",&n, &q))
    {
        BuildTree(1,1,n);
        while(q--)
        {
            char s[3];
            int a, b;
            long long c;
            scanf(" %s", s);
            if(s[0] == 'Q')
            {
                ans = 0;
                scanf("%d%d",&a,&b);
                Query(1,a,b);
                printf("%lld\n",ans);
            }
            else
            {
                scanf("%d%d%I64d",&a,&b,&c);
                Update(1,a,b,c);
            }
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值