线段树专题 《最值+区间更新+单点更新+区间和+lazy》

A - Balanced Lineup  《最值查询》

 POJ - 3264 

给你一个长度为n的序列a[N] (1 ≤ N ≤ 50000),询问Q(1 ≤ Q ≤ 200000) 次,每次输出【L, R】区间最大值与最小值的差是多少。
so easy~
so easy~
so easy~

Input

多组用例
第一行是两个整数 N,Q
然后是N个数a[i] 保证a[i] 都小于1e9
然后是Q个询问 每次给你L,R 保证(1<=L<=R<= N)

Output输出每次询问【L, R】区间最大值与最小值的差是多少Sample Input

6 3
1
7
3
4
2
5
1 5
4 6
2 2

Sample Output

6
3
0

#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cstdio>
#include <cstdlib>
#define ll long long
#define inf 0x3f3f3f3
using namespace std;
const int mxn = 5e4+10;
#define TLE std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ls now<<1,l,mid
#define rs now<<1|1,mid+1,r
#define lc now<<1
#define rc now<<1|1
int n,m,k,t,mx,mn;
struct node{    int l,r,sum,mx,mn;}rt[mxn<<4];
void build(int now,int l,int r)
{
    rt[now].l = l ,rt[now].r = r ,rt[now].sum = 0;
    if(l==r)
    {
        cin>>rt[now].sum ;
        rt[now].mx = rt[now].sum ;
        rt[now].mn = rt[now].sum ;
        return ;
    }
    int mid = (l+r)>>1;
    build(ls);
    build(rs);
    rt[now].mx = max( rt[lc].mx , rt[rc].mx );
    rt[now].mn = min( rt[lc].mn , rt[rc].mn );
}
void seach(int now,int l,int r)
{
    if(l<=rt[now].l && r>=rt[now].r)
    {
        mx = max(mx,rt[now].mx);
        mn = min(mn,rt[now].mn);
        return ;
    }
    int mid = (rt[now].l+rt[now].r)/2,ans = 0;
    if(l<=mid)
        seach(lc,l,r);
    if(r>mid)
        seach(rc,l,r);
}
int main()
{
    TLE;
    while(cin>>n>>m)
    {
        build(1,1,n);
        int l,r;
        while(m--)
        {
            cin>>l>>r;
            mx = -1 , mn = 1e9+5 ;
            seach(1,l,r);
            cout<<mx-mn<<endl;
        }
    }
    return 0;
}
/*
6 3
1
7
3
4
2
5
1 5
4 6
2 2
*/
View Code

 

B - 敌兵布阵 《区间和 + 单点更新》

 HDU - 1166 

C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了。A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况。由于采取了某种先进的监测手段,所以每个工兵营地的人数C国都掌握的一清二楚,每个工兵营地的人数都有可能发生变动,可能增加或减少若干人手,但这些都逃不过C国的监视。 
中央情报局要研究敌人究竟演习什么战术,所以Tidy要随时向Derek汇报某一段连续的工兵营地一共有多少人,例如Derek问:“Tidy,马上汇报第3个营地到第10个营地共有多少人!”Tidy就要马上开始计算这一段的总人数并汇报。但敌兵营地的人数经常变动,而Derek每次询问的段都不一样,所以Tidy不得不每次都一个一个营地的去数,很快就精疲力尽了,Derek对Tidy的计算速度越来越不满:"你个死肥仔,算得这么慢,我炒你鱿鱼!”Tidy想:“你自己来算算看,这可真是一项累人的工作!我恨不得你炒我鱿鱼呢!”无奈之下,Tidy只好打电话向计算机专家Windbreaker求救,Windbreaker说:“死肥仔,叫你平时做多点acm题和看多点算法书,现在尝到苦果了吧!”Tidy说:"我知错了。。。"但Windbreaker已经挂掉电话了。Tidy很苦恼,这么算他真的会崩溃的,聪明的读者,你能写个程序帮他完成这项工作吗?不过如果你的程序效率不够高的话,Tidy还是会受到Derek的责骂的. 

Input第一行一个整数T,表示有T组数据。 
每组数据第一行一个正整数N(N<=50000),表示敌人有N个工兵营地,接下来有N个正整数,第i个正整数ai代表第i个工兵营地里开始时有ai个人(1<=ai<=50)。 
接下来每行有一条命令,命令有4种形式: 
(1) Add i j,i和j为正整数,表示第i个营地增加j个人(j不超过30) 
(2)Sub i j ,i和j为正整数,表示第i个营地减少j个人(j不超过30); 
(3)Query i j ,i和j为正整数,i<=j,表示询问第i到第j个营地的总人数; 
(4)End 表示结束,这条命令在每组数据最后出现; 
每组数据最多有40000条命令 
Output对第i组数据,首先输出“Case i:”和回车, 
对于每个Query询问,输出一个整数并回车,表示询问的段中的总人数,这个数保持在int以内。 
Sample Input

1
10
1 2 3 4 5 6 7 8 9 10
Query 1 3
Add 3 6
Query 2 7
Sub 10 2
Add 6 3
Query 3 10
End 

Sample Output

Case 1:
6
33
59
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#define ll long long
#define inf 0x3f3f3f3
using namespace std;
const int mxn = 50000+10;
#define TLE std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ls now<<1,l,mid
#define rs now<<1|1,mid+1,r
#define lc now<<1
#define rc now<<1|1
#define upsum(now) rt[now].sum =rt[now<<1].sum + rt[now<<1|1].sum ;
#define upmx(now) rt[now].mx = max(rt[now<<1].mx , rt[now<<1|1].mx) ;
ll n,m,k,t,mx,mn,l,r,dp[mxn];
string str;
struct node{    ll lazy,l,r,sum,mx,mn;}rt[mxn<<4];
int key[mxn] ;
int lowbit(int x) {return (x&(-x));}
void alter_node(int in , int val )  /// 在 in 位置加上 val
{
    while(in<=n)
    {
        key[in]+=val;
        in += lowbit(in) ;
    }
}
int ask_sum(int i)                  ///  求 i 以前的和(包括 i )
{
    int ans = 0 ;
    while(i>0)
    {
        ans += key[i] ;
        i -= lowbit(i);
    }
    return ans ;
}
int main()
{
    TLE;
    cin>>t;
    for(int tt=1;tt<=t;tt++)
    {
        cout<<"Case "<<tt<<":"<<endl;
        cin>>n;
        memset(key,0,sizeof(key));
        for(int i=1;i<=n;i++)
        {
            cin>>k;
            alter_node(i,k);
        }
        while(cin>>str && str[0]!='E')
        {
            if(str[0]=='Q')
            {
                cin>>l>>r;
                cout<<ask_sum(r)-ask_sum(l-1)<<endl;
            }
            else if(str[0]=='A')
            {
                cin>>l>>r;
                alter_node(l,r);
            }
            else
            {
                cin>>l>>r;
                alter_node(l,-r);
            }
        }
    }
}
树状数组

线段树
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cstdio>
#include <cstdlib>
#define ll long long
#define inf 0x3f3f3f3
using namespace std;
const int mxn = 5e4+10;
#define TLE std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ls now<<1,l,mid
#define rs now<<1|1,mid+1,r
#define lc now<<1
#define rc now<<1|1
#define up(now) rt[now].sum =rt[now<<1].sum + rt[now<<1|1].sum ;
int n,m,k,t,mx,mn;
string str;
struct node{    int l,r,sum,mx,mn;}rt[mxn<<4];
void build(int now,int l,int r)
{
    rt[now].l = l ,rt[now].r = r , rt[now].sum = 0 ;
    if(l==r)
    {
        cin>>rt[now].sum ;
        return ;
    }
    int mid = (l+r)>>1;
    build(ls) ; build(rs);
    up(now);
}
int seach(int now,int l,int r)
{
    if(l<=rt[now].l && r>=rt[now].r)
    {
        return rt[now].sum ;
    }
    int mid = (rt[now].l+rt[now].r)>>1 , ans = 0 ;
    if(l<=mid) ans += seach(lc,l,r);
    if(r>mid) ans+= seach(rc,l,r);
    return ans;
}
void updata(int now,int l,int r)
{
    if(rt[now].l==rt[now].r)
    {
        rt[now].sum += r ;
        return ;
    }
    int mid = (rt[now].l+rt[now].r)>>1;
    if(l<=mid) updata(lc,l,r);
    if(l>mid) updata(rc,l,r);
    up(now);
}
int main()
{
    TLE;
    cin>>t;
    for(int tt=1;tt<=t;tt++)
    {
        cin>>n;
        build(1,1,n);
        cout<<"Case "<<tt<<":"<<endl;
        int l,r;
        while(cin>>str)
        {
            if(str[0]=='E') break;
            if(str[0]=='Q')
            {
                cin>>l>>r;
                cout<<seach(1,l,r)<<endl;
            }
            else if(str[0]=='A')
            {
                cin>>l>>r;
                updata(1,l,r);
            }
            else if(str[0]=='S')
            {
                cin>>l>>r;
                updata(1,l,-1*r);
            }
        }

    }
    return 0;
}
/*
6 3
1
7
3
4
2
5
1 5
4 6
2 2
*/

 

C - I Hate It  《最值查询 + 单点更新》

 HDU - 1754 

很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。 
这让很多学生很反感。 

不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。

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 <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cstdio>
#include <cstdlib>
#define ll long long
#define inf 0x3f3f3f3
using namespace std;
const int mxn = 200000+10;
#define TLE std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ls now<<1,l,mid
#define rs now<<1|1,mid+1,r
#define lc now<<1
#define rc now<<1|1
#define upsum(now) rt[now].sum =rt[now<<1].sum + rt[now<<1|1].sum ;
#define upmx(now) rt[now].mx = max(rt[now<<1].mx , rt[now<<1|1].mx) ;
int n,m,k,t,mx,mn;
string str;
struct node{    int l,r,sum,mx,mn;}rt[mxn<<4];
void build(int now , int l, int r)
{
    rt[now].l = l , rt[now].r = r ;
    if(l==r)
    {
        cin>>rt[now].mx;
        return ;
    }
    int mid = (l+r)>>1;
    build(ls); build(rs);
    upmx(now);
}
void ask(int now,int l,int r)
{
    if(l<=rt[now].l && r>=rt[now].r)
    {
        mx = max(rt[now].mx,mx);
        return ;
    }
    int mid = (rt[now].l + rt[now].r)>>1;
    if( l<=mid )
        ask(lc,l,r);
    if(r>mid)
        ask(rc,l,r);

}
void alter(int now,int l,int r)
{
    if(rt[now].l==l && l==rt[now].r)
    {
        rt[now].mx = r ;
        return ;
    }
    int mid = (rt[now].l+rt[now].r)>>1;
    if(mid>=l) alter(lc,l,r);
    else alter(rc,l,r);
    upmx(now);
}
int main()
{
    TLE;
    while(cin>>n>>m)
    {
        build(1,1,n);
        while(m--)
        {
            char ch;int l,r;
            cin>>ch>>l>>r;
            if(ch=='Q')
            {
                mx = -1;
                ask(1,l,r);
                cout<<mx<<endl;
            }
            else
                alter(1,l,r);
        }
    }
}
/*
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
*/
View Code

 

D - Just a Hook《lazy值更新 + 区间更新》

 HDU - 1698 

In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length. 



Now Pudge wants to do some operations on the hook. 

Let us number the consecutive metallic sticks of the hook from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver sticks or golden sticks. 
The total value of the hook is calculated as the sum of values of N metallic sticks. More precisely, the value for each kind of stick is calculated as follows: 

For each cupreous stick, the value is 1. 
For each silver stick, the value is 2. 
For each golden stick, the value is 3. 

Pudge wants to know the total value of the hook after performing the operations. 
You may consider the original hook is made up of cupreous sticks. 

InputThe input consists of several test cases. The first line of the input is the number of the cases. There are no more than 10 cases. 
For each case, the first line contains an integer N, 1<=N<=100,000, which is the number of the sticks of Pudge’s meat hook and the second line contains an integer Q, 0<=Q<=100,000, which is the number of the operations. 
Next Q lines, each line contains three integers X, Y, 1<=X<=Y<=N, Z, 1<=Z<=3, which defines an operation: change the sticks numbered from X to Y into the metal kind Z, where Z=1 represents the cupreous kind, Z=2 represents the silver kind and Z=3 represents the golden kind. 
OutputFor each case, print a number in a line representing the total value of the hook after the operations. Use the format in the example. 
Sample Input

1
10
2
1 5 2
5 9 3

Sample Output

Case 1: The total value of the hook is 24.

#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cstdio>
#include <cstdlib>
#define ll long long
#define inf 0x3f3f3f3
using namespace std;
const int mxn = 200000+10;
#define TLE std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ls now<<1,l,mid
#define rs now<<1|1,mid+1,r
#define lc now<<1
#define rc now<<1|1
#define upsum(now) rt[now].sum =rt[now<<1].sum + rt[now<<1|1].sum ;
#define upmx(now) rt[now].mx = max(rt[now<<1].mx , rt[now<<1|1].mx) ;
int n,m,k,t,mx,mn,l,r;
string str;
struct node{    int lazy,l,r,sum,mx,mn;}rt[mxn<<4];
void updown(int now)
{
    if(rt[now].lazy)
    {
        rt[lc].lazy = rt[now].lazy ;
        rt[rc].lazy = rt[now].lazy ;
        rt[lc].sum = (rt[lc].r-rt[lc].l+1)*rt[now].lazy;
        rt[rc].sum = (rt[rc].r-rt[rc].l+1)*rt[now].lazy;
        rt[now].lazy = 0 ;
    }
}
void build(int now,int l,int r)
{
    rt[now].l = l , rt[now].r = r ,rt[now].lazy = 0;
    if(l==r)
    {
        rt[now].sum = 1 ;
        return ;
    }
    int mid = (l+r)>>1;
    build(ls) ;   build(rs) ;    upsum(now);
}
int ask(int now,int l,int r)
{
    if(l<=rt[now].l && r>=rt[now].r)
        return rt[now].sum;
    updown(now);
    int mid = (rt[now].l+rt[now].r)>>1;
    if(l<=mid)
        ask(lc,l,r);
    if(r>mid)
        ask(rc,l,r);
}
void alter(int now,int l,int r,int val)
{
    if(l<=rt[now].l && r>=rt[now].r)
    {
        rt[now].lazy = val;
        rt[now].sum = (rt[now].r-rt[now].l+1)*val;
        return ;
    }
    updown(now);
    int mid = (rt[now].l+rt[now].r)>>1;
    if(l<=mid)
        alter(lc,l,r,val);
    if(r>mid)
        alter(rc,l,r,val);
    upsum(now);
}
int main()
{
    TLE;
    cin>>t;
    for(int tt = 1 ;tt<=t;tt++)
    {
        cin>>n;
        build(1,1,n);
        cin>>m;
        while(m--)
        {
            cin>>l>>r>>k;
            alter(1,l,r,k);
        }
        cout<<"Case "<<tt<<": The total value of the hook is "<<ask(1,1,n)<<"."<<endl;
    }
}
/*
10
1 5 2
9 3
*/
View Code



E - A Simple Problem with Integers 《区间和 + lazy值更新》

 POJ - 3468 

给出了一个序列,你需要处理如下两种询问。

"C a b c"表示给[a, b]区间中的值全部增加c (-10000 ≤ c ≤ 10000)。

"Q a b" 询问[a, b]区间中所有值的和。

Input

第一行包含两个整数N, Q。1 ≤ N,Q ≤ 100000.

第二行包含n个整数,表示初始的序列A (-1000000000 ≤ Ai ≤ 1000000000)。

接下来Q行询问,格式如题目描述。

Output

对于每一个Q开头的询问,你需要输出相应的答案,每个答案一行。

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
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cstdio>
#include <cstdlib>
#define ll long long
#define inf 0x3f3f3f3
using namespace std;
const int mxn = 200000+10;
#define TLE std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ls now<<1,l,mid
#define rs now<<1|1,mid+1,r
#define lc now<<1
#define rc now<<1|1
#define upsum(now) rt[now].sum =rt[now<<1].sum + rt[now<<1|1].sum ;
#define upmx(now) rt[now].mx = max(rt[now<<1].mx , rt[now<<1|1].mx) ;
ll n,m,k,t,mx,mn,l,r;
string str;
struct node{    ll lazy,l,r,sum,mx,mn;}rt[mxn<<4];
void updown(int now)
{
    if(rt[now].lazy)
    {
        rt[lc].lazy += rt[now].lazy ;
        rt[rc].lazy += rt[now].lazy ;
        rt[lc].sum += (rt[lc].r-rt[lc].l+1)*rt[now].lazy;
        rt[rc].sum += (rt[rc].r-rt[rc].l+1)*rt[now].lazy;
        rt[now].lazy = 0 ;
    }
}
void build(int now,int l,int r)
{
    rt[now].l = l , rt[now].r = r ,rt[now].lazy = 0;
    if(l==r)
    {
        cin>>rt[now].sum ;
        return ;
    }
    int mid = (l+r)>>1;
    build(ls) ;   build(rs) ;    upsum(now);
}
ll ask(int now,int l,int r)
{
    if(l<=rt[now].l && r>=rt[now].r)
        return rt[now].sum ;
    updown(now);
    int mid = (rt[now].l+rt[now].r)>>1;
    ll ans = 0;
    if(l<=mid)
        ans+=ask(lc,l,r);
    if(r>mid)
        ans+=ask(rc,l,r);
    return ans;
}
void alter(int now,int l,int r,int val)
{
    if(l<=rt[now].l && r>=rt[now].r)
    {
        rt[now].lazy += val;
        rt[now].sum += (rt[now].r-rt[now].l+1)*val;
        return ;
    }
    updown(now);
    int mid = (rt[now].l+rt[now].r)>>1;
    if(l<=mid)
        alter(lc,l,r,val);
    if(r>mid)
        alter(rc,l,r,val);
    upsum(now);
}
int main()
{
    TLE;
    while(cin>>n>>m)
    {
        char ch;
        build(1,1,n);
        while(m--)
        {
            cin>>ch>>l>>r;
            if(ch=='Q')
            {
                cout<<ask(1,l,r)<<endl;
            }
            else
            {
                cin>>k;
                alter(1,l,r,k);
            }

        }
    }
}
/*
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
*/
View Code

 





所遇皆星河
原文地址:https://www.cnblogs.com/Shallow-dream/p/12046690.html