线段树的点修改+区间查询+区间修改

D - 敌兵布阵

题目描述

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的责骂的.

输入

第一行一个整数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条命令

输出

对第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<map>
#include<queue>
#include<stack>
#include<string.h>
#include<string>
#include<cmath>
#include<stdio.h>
#define ll long long
using namespace std;
const int maxn=50005;
int str[maxn<<2];//保存节点的区段和(有可能是个区间,也可能是个点)
int arr[50005];//单个点的值
void Build(int node,int st,int ed){//构造线段树
    if(st==ed){//找到叶子节点赋值
        str[node]=arr[st];
    }
    else{
        int mid=(st+ed)/2;
        int lefts=2*node+1;//节点的左子树的节点
        int rights=2*node+2;//节点的有子树节点
        Build(lefts,st,mid);
        Build(rights,mid+1,ed);
        str[node]=str[lefts]+str[rights];//返回节点左右节点的和作为该节点的值
    }

}
void Update(int node,int r,int st,int ed){//node代表是节点,r该表的是修改的的是哪个,st代表区间的开始,ed代表是区间的结尾位置
    if(st==ed){
        str[node]=arr[st];//找到单个节点
    }
    else{
        int mid=(st+ed)/2;
        int lefts=2*node+1;
        int rights=2*node+2;
        if(r>=st&&r<=mid){//判断是进入左子树还是右子树
            Update(lefts,r,st,mid);
        }
        else{
            Update(rights,r,mid+1,ed);
        }
        str[node]=str[lefts]+str[rights];//左后修改值
    }

}
int checkpeople(int i,int j,int node,int st,int ed){//区间查询,i,j代表修改的是要查询的区间
    if(j<st||i>ed){//所查询的区间根本就不再[st,ed]中,直接返回0,没必要继续下去
        return 0;
    }
    else if(i<=st&&j>=ed){//[i,j]区间包含[st,ed]区间,之间返回区间的值
        return str[node];
    }
    else if(st==ed){//如果硬是查到了叶子节点,返回叶子节点代表的值
        return str[st];
    }//上述情况都不成立,之间再进入左右子树进行查询
    int mids=(st+ed)/2;
    int lefts=2*node+1;
    int rights=2*node+2;
    int leftnum=checkpeople(i,j,lefts,st,mids);
    int rightsnum=checkpeople(i,j,rights,mids+1,ed);
    return rightsnum+leftnum;

}
int main(){
    int T,n,k,num=0;
    char name[10];
    int i,j;
    scanf("%d",&T);
    while(T--){
        printf("Case %d:\n",++num);
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d",&arr[i]);
        }
        Build(0,1,n);//开始预处理
        while(1){
            scanf("%s",name);
            if(strcmp(name,"End")==0){
                break;
            }
            else if(strcmp(name,"Query")==0){
                scanf("%d %d",&i,&j);
                cout<<checkpeople(i,j,0,1,n)<<endl;
            }
            else if(strcmp(name,"Add")==0){
                scanf("%d %d",&i,&j);
                arr[i]+=j;
                Update(0,i,1,n);
            }
            else if(strcmp(name,"Sub")==0){
                scanf("%d %d",&i,&j);
                arr[i]-=j;
                Update(0,i,1,n);
            }
        }
    }
}

这道题目涉及到线段树的点的修改+区间查询
查看代码,解释都在代码里面

E - 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.

Input

The 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.

Output

For 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<map>
#include<queue>
#include<stack>
#include<string.h>
#include<string>
#include<cmath>
#include<stdio.h>
#define ll long long
using namespace std;
const int maxn=1e6+5;
int str[maxn<<2];
int lazy[maxn<<2];//进行一个懒惰标记
int arr[maxn];

void PushUp(int node)//就是一个求左右节点和
{
    int lefts=2*node+1;
    int rights=2*node+2;
    str[node]=str[lefts]+str[rights];
}

void Pushdown(int node,int st,int ed)//下传懒惰标记更新子区间
{
    if(!lazy[node])return;
    int mid=(st+ed)/2;
    int lefts = 2*node+1;
    int rights=2*node+2;
    int t = lazy[node];//原先的值要下传
    str[lefts]=(mid-st+1)*t;
    lazy[lefts]=t;
    str[rights]=(ed-mid)*t;
    lazy[rights]=t;
    lazy[node]= 0;
}

void Build(int node,int st,int ed){//构造线段树
    if(st==ed){
        str[node]=arr[st];
    }
    else{
        int mid=(st+ed)/2;
        int lefts=2*node+1;
        int rights=2*node+2;
        Build(lefts,st,mid);
        Build(rights,mid+1,ed);
        str[node]=str[lefts]+str[rights];
    }

}
void Update(int x,int y,int z,int node,int st,int ed){//把[x,y]区间里的值每一个都修改成z
    if(st>=x&&ed<=y){//[st,ed]在[x,y]中间(包含关系)
        lazy[node]=z;
        str[node]=(ed-st+1)*z;
        return;
    }
    if(y<st||x>ed)return;//[x,y]和[st,ed]不相交
    Pushdown(node,st,ed);//更新之前lazy标记的点(下沉)
    int mid=(st+ed)/2;
    int lefts=2*node+1;
    int rights=2*node+2;
    Update(x,y,z,lefts,st,mid);   
    Update(x,y,z,rights,mid+1,ed);
    PushUp(node);
}
int checkpeople(int i,int j,int node,int st,int ed){//区间修改的关键:lazy-tag,交错和包含
    if(j<st||i>ed){
        return 0;
    }
    else if(i<=st&&j>=ed){
        return str[node];
    }
    else if(st==ed){
        return str[st];
    }
    int mids=(st+ed)/2;
    int lefts=2*node+1;
    int rights=2*node+2;
    int leftnum=checkpeople(i,j,lefts,st,mids);
    int rightsnum=checkpeople(i,j,rights,mids+1,ed);
    return rightsnum+leftnum;

}
int main(){
    int T,n,k,num=0,Q,x,y,z;
    char name[10];
    int i,j;
    scanf("%d",&T);
    while(T--){
        memset(lazy,0,sizeof(lazy));
        scanf("%d",&n);
        for(int i=1;i<=maxn;i++){
            arr[i]=1;
        }
        Build(0,1,n);
        //cout<<str[0]<<endl;
        scanf("%d",&Q);
        for(int i=0;i<Q;i++){
            scanf("%d %d %d",&x,&y,&z);
            Update(x,y,z,0,1,n);
        }
        printf("Case %d: The total value of the hook is %d.\n",++num,str[0]);
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值