线段树

敌兵布阵

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 154223    Accepted Submission(s): 64006

Problem Description
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<stdio.h>
using namespace std;
const int maxn = 50005;
int sum[maxn*4];

void PushUP(int re){
    sum[re] = sum[re*2]+sum[2*re+1];
}

void creat(int re,int left,int right){
    if(left == right){
        scanf("%d",&sum[re]);
        return ;
    } 
    int mid = (left+right)>>1;
    creat(2*re,left,mid);
    creat(2*re+1,mid+1,right);
    PushUP(re);
}

int query(int qL,int qR,int re,int left,int right){
    if(qR>=right&&qL<=left){
        return  sum[re];
    }
    int ans = 0;
    int mid = (left+right)>>1;
    if(qL<=mid){
        ans +=query(qL,qR,2*re,left,mid);
    }
    if(qR>mid){
        ans += query(qL,qR,2*re+1,mid+1,right);
    }
    return ans;
}

void update(int pos,int number,int re,int left,int right){
    if(left==right){
        sum[re]+=number;
        return ;
    } 
    int mid = (left+right)>>1;
    if(pos<=mid)
        update(pos,number,2*re,left,mid);
    else if(pos>mid)
        update(pos,number,2*re+1,mid+1,right);
        
    PushUP(re);
}

int main(){
    int t;
    cin>>t;
    for(int cas=1;cas<=t;cas++){
        printf("Case %d:
",cas);
        int n;
        scanf("%d",&n);
        creat(1,1,n);
        string s;
        while(cin>>s&&s[0]!='E'){
            int a,b;
            scanf("%d%d",&a,&b);
            if(s[0]=='Q'){
                printf("%d
",query(a,b,1,1,n));
            }
            else if(s[0]=='A'){
                update(a,b,1,1,n);
            }
            else
                update(a,-b,1,1,n);
        }
    }
    return 0;
} 

I Hate It

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

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

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<stdio.h>
using namespace std;
const int maxn = 200005;
int stu[4*maxn];
void PushUP(int re){
    stu[re] = max(stu[2*re],stu[2*re+1]);
}

void create(int re,int left,int right){
    if(left==right){
        scanf("%d",&stu[re]);
        return ;//必须写 
    }
    int mid = (left+right)>>1;
    create(2*re,left,mid);
    create(2*re+1,mid+1,right);
    PushUP(re);
}

int query(int qL,int qR,int re,int a,int b){
    if(a<=qL&&b>=qR)
        return stu[re];
    int mid = (qL+qR)>>1;
    int ans = 0;
    if(a<=mid)
        ans= max(ans,query(qL,mid,2*re,a,b));
    if(b>mid)
        ans = max(ans,query(mid+1,qR,2*re+1,a,b));
    return ans;
}

void update(int left,int right,int re,int a,int b){
    if(left==right){
        stu[re] = b;
        return ;
    }
    int mid = (left+right)>>1;
    if(a<=mid)
        update(left,mid,2*re,a,b);
    else
        update(mid+1,right,2*re+1,a,b);
    PushUP(re);
} 

int main(){
    int n,m;
    while(cin>>n>>m){
        create(1,1,n);
        for(int ca=1;ca<=m;ca++){
            char c;
            int a,b;
            getchar();
            scanf("%c%d%d",&c,&a,&b);
            if(c=='Q'){
                printf("%d
",query(1,n,1,a,b));
            }
            else if(c=='U'){
                update(1,n,1,a,b);
            }
        }
    }
    return 0;
}

Just a Hook

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.
Time limit
2000 ms
Memory limit
32768 kB
OS
Windows
Source
 

代码:

#include<iostream>
#include<stdio.h>
using namespace std;
const int maxn =1e5+10;
//lazy的作用是如果查询的区间正好找到,那么我就不更新我的子区间,直接返回,然后就节约时间
//如果下次查询的时候正好查到lazy=1的区间的话,那么我就把这个被标记的区间进行更新,更新的值是我
//我原来的值。 
struct node{
    int l,r;
    int lazy,tag;
    int sum;
}a[maxn*4];

void Build(int i,int l,int r){
    a[i].l = l;
    a[i].r = r;
    a[i].lazy = 0;
    a[i].tag = 0;
    if(l==r){
        a[i].sum = 1;
        return ;
    }
    int mid = (l+r)>>1;
    Build(2*i,l,mid);
    Build(2*i+1,mid+1,r);
    a[i].sum = a[2*i].sum + a[2*i+1].sum; 
}

void update(int i,int l,int r,int v){
    if(a[i].l==l&&a[i].r==r){
        a[i].lazy = 1;
        a[i].sum = (r-l+1)*v;
        a[i].tag = v;
        return ;
    }
    int mid = (a[i].l+a[i].r)>>1;
    if(a[i].lazy == 1){
        a[i].lazy = 0;
        update(2*i,a[i].l,mid,a[i].tag);
        update(2*i+1,mid+1,a[i].r,a[i].tag);
        
    }
    
    if(r<=mid)
        update(2*i,l,r,v);
    else if(l>mid){
        update(2*i+1,l,r,v);
    }
    else{
        update(2*i,l,mid,v);
        update(2*i+1,mid+1,r,v);
    }
    a[i].sum = a[2*i].sum + a[2*i+1].sum;
}

int main(){
    int x,y,z;
    int n,m,t;
    cin>>t;
    for(int i=1;i<=t;i++){
        scanf("%d%d",&n,&m);
        Build(1,1,n);
        while(m--){
            scanf("%d%d%d",&x,&y,&z);
            update(1,x,y,z);
        }
        printf("Case %d: The total value of the hook is %d.
",i,a[1].sum);
    }
    return 0;
}

A Simple Problem with Integers

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

Time limit
5000 ms
Case time limit
2000 ms
Memory limit
131072 kB

代码:
#include<iostream>
#include<stdio.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5+10;
struct node{
    int l,r;
    ll lazy;
    ll sum;
}a[4*maxn];

void PushDown(int u){
    if(a[u].lazy){
        a[2*u].lazy+=a[u].lazy ;
        a[2*u+1].lazy +=a[u].lazy;
        a[2*u].sum +=a[u].lazy*(a[2*u].r-a[2*u].l+1);
        a[2*u+1].sum +=a[u].lazy*(a[2*u+1].r-a[2*u+1].l+1);
        a[u].lazy = 0; 
    }
}
void Build(int re,int left,int right){
    a[re].l = left;
    a[re].r = right;
    a[re].lazy =0;
    if(left==right){
        scanf("%lld",&a[re].sum);
        return ;
    }
    int mid = (left+right)>>1;
    Build(2*re,left,mid);
    Build(2*re+1,mid+1,right);
    a[re].sum = a[2*re].sum + a[2*re+1].sum;
}

ll query(int ll,int rr,int re){
    if(a[re].l==ll&&a[re].r==rr){
        return    a[re].sum;
    }
    PushDown(re);
    int mid=(a[re].l+a[re].r)>>1;
    if(rr<=mid)
        return query(ll,rr,2*re);
    else if(ll>mid)
        return query(ll,rr,2*re+1);
    else{
        return query(ll,mid,2*re)+query(mid+1,rr,2*re+1);//这里是加号 
    }
    
}

void update(int aa,int bb,int c,int i){
    if(a[i].l==aa&&a[i].r==bb){
        a[i].lazy += c;
        a[i].sum +=(bb-aa+1)*c;
        return ;
    }
    if(a[i].l==a[i].r) return ;
    PushDown(i);
    int mid = (a[i].l+a[i].r)>>1;
    
    if(bb<=mid){
        update(aa,bb,c,2*i);
    }
    else if(aa>mid){
        update(aa,bb,c,2*i+1);
    }
    else{
        update(aa,mid,c,2*i);
        update(mid+1,bb,c,2*i+1);
    }
    a[i].sum = a[2*i].sum + a[2*i+1].sum; 
}

int main(){
    int n,q;
    cin>>n>>q;
    Build(1,1,n);
    for(int i=1;i<=q;i++){
        char s;
        int a,b,c;
        getchar();
        scanf("%c",&s);
        if(s=='Q'){
            scanf("%d%d",&a,&b);
            cout<<query(a,b,1)<<endl;
        }
        else{
            scanf("%d%d%d",&a,&b,&c);
            update(a,b,c,1);
        }
    }
    return 0;
} 





代码:
#include <iostream>
#include <cstdio>
#include <stack>
#define ll long long
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
const int maxn = 3e6+5;
struct Tree {
    int l, r;
    ll maxx, minn;
}e[maxn<<2];
 
ll a[maxn], b[maxn], sum[maxn], ans;
 
int l[maxn], r[maxn];
 
void pushup(int cur) {
    e[cur].maxx = max(e[cur<<1].maxx, e[cur<<1|1].maxx);
    e[cur].minn = min(e[cur<<1].minn, e[cur<<1|1].minn);
}
 
void build(int l, int r, int cur) {
    e[cur].l = l;
    e[cur].r = r;
    if(l==r) {
        e[cur].maxx = sum[l];
        e[cur].minn = sum[l];
        return ;
    }
    int mid = l + r >> 1;
    build(l, mid, cur<<1);
    build(mid+1, r, cur<<1|1);
    pushup(cur);
}
 
ll querymax(int l, int r, int cur) {
    if(l <= e[cur].l && e[cur].r <= r) {
        return e[cur].maxx;
    }
    ll ans = -INF;
    int mid = e[cur].l + e[cur].r >> 1;
    if(l <= mid) ans = max(ans, querymax(l, r, cur<<1));
    if(r > mid) ans = max(ans, querymax(l, r, cur<<1|1));
    return ans;
}
 
ll querymin(int l, int r, int cur) {
    if(l <= e[cur].l && e[cur].r <= r) {
        return e[cur].minn;
    }
    ll ans = INF;
    int mid = e[cur].l + e[cur].r >> 1;
    if(l <= mid) ans = min(ans, querymin(l, r, cur<<1));
    if(r > mid) ans = min(ans, querymin(l, r, cur<<1|1));
    return ans;
}
 
int main()
{
    int n;
    scanf("%d", &n);
    sum[0] = 0;
    for(int i = 1; i <= n; i++) {
        scanf("%lld", &a[i]);
    }
    for(int i = 1; i <= n; i++) {
        scanf("%lld", &b[i]);
        sum[i] = sum[i - 1] + b[i];
    }
    build(0, n, 1);
    stack<int> s1, s2;
    for(int i = 1; i <= n; i++) {
        while(!s1.empty() && a[i] <= a[s1.top()]) s1.pop();
        if(s1.empty()) l[i] = 1;
        else l[i] = s1.top() + 1;
        s1.push(i);
    }
    for(int i = n; i >= 1; i--) {
        while(!s2.empty() && a[i] <= a[s2.top()]) s2.pop();
        if(s2.empty()) r[i] = n;
        else r[i] = s2.top() - 1;
        s2.push(i);
    }
    for(int i = 1; i <= n; i++) {
        if(a[i] < 0) {
            ans = max(ans, a[i] * (querymin(i, r[i], 1) - querymax(l[i] - 1, i - 1, 1)));
        }
        else
            ans = max(ans, a[i] * (querymax(i, r[i], 1) - querymin(l[i] - 1, i - 1, 1)));
    }
    printf("%lld
", ans);
    return 0;
}
 
原文地址:https://www.cnblogs.com/lusiqi/p/11650889.html