poj1698线段树区间修改区间查询

Just a Hook

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 49371    Accepted Submission(s): 23351


 

Problem Description

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.

 

 

Source

2008 “Sunline Cup” National Invitational Contest

这题是区间修改,加上1到n求和

代码&模板:

#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
#define N 100003
int a[N];
struct node
{
    int lazy;
    int val;//区间的最大值或者和之类的
} no[4*N];
int cc=1;
int t;
int n;
void push(int id)
{
    no[id].val=no[id<<1].val+no[id<<1|1].val;
    return;
}
void build(int id,int l,int r)
{
    if(l==r)
    {
        no[id].val=a[l];
        no[id].lazy=0;//lazy初始化
        return;
    }
    int mid=(l+r)/2;
    no[id].lazy=0;//lazy初始化
    build(id<<1,l,mid);
    build(id<<1|1,mid+1,r);
    push(id);
    return;
}
//这里的id,l,r代表着树中一个点的信息,编号以及代表的闭区间范围,他们三个在递归的过程中始终改变
void push_down(int id,int l,int r)//将修改传递下去
{
    if(no[id].lazy!=0)//这个区间是否全体有过修改
    {

        int mid=(l+r)/2;
        no[id<<1].val=(mid-l+1)*no[id].lazy;//左半部分长度乘上修改成的值也就是lazy的值就是这部分区间更新后的和
        //注意如果是集体增加或减少则这里=就变成+=因为增加就有累加效应
        no[id<<1].lazy=no[id].lazy;
        //把区间都进行的操做也就是lazy赋值给下一层区间,这里同样注意如果是集体增加或减少则这里=就变成+=因为增加就有累加效应 也就是no[id<<1].lazy+=no[id].lazy;
        no[id<<1|1].val=(r-mid)*no[id].lazy;
        no[id<<1|1].lazy=no[id].lazy;
        no[id].lazy=0;
        //这一层修改已经传递下去了,就把lazy改回0
    }
    return;
}
//区间更新
//这里的id,l,r代表着树中一个点的信息,编号以及代表的闭区间范围,他们三个在递归的过程中始终改变,而后面的参数一般是要修改或查询的相关参数不改变
void update_section(int id,int l,int r,int x,int y,int key)
{
    if(x<=l&&y>=r)
    {
        no[id].val=((r-l+1)*key);
        no[id].lazy=key;
        return;
    }
    push_down(id,l,r);
    int mid=(l+r)/2;
    if(x<=mid)
    {
        update_section(id<<1,l,mid,x,y,key);
    }
    if(y>mid)//注意这里不能写else 因为两边都要更新
    {
        update_section(id<<1|1,mid+1,r,x,y,key);
    }
    push(id);
    return;
}
//单点更新
void update_id(int id,int l,int r,int x,int y)
{
    if(l==r)
    {
        no[id].val=y;
        return;
    }
    int mid=(l+r)/2;
    if(x<=mid)
    {
        update_id(id<<1,l,mid,x,y);
    }
    if (x>mid)
    {
        update_id(id<<1|1,mid+1,r,x,y);
    }
    push(id);
    return;
}
//区间求和,其实就是把l,r不断地分隔,最终拼接出x,y的区间
int query(int id,int l,int r,int x,int y)
{
    //当分割出的区间在x,y内也就是是x,y的一部分,那么就可以返回了,不需要再往下找了,这也是比遍历快的地方
    if(x<=l&&y>=r)
    {
        return no[id].val;
    }
    //如果这个点lazy不等于0,也就是更新过,证明这个区间整个每个数都一起更改过,那么当要往下查询的时候要把这种更改传递下去传递一层(只传一层目的是节约时间)
    push_down(id,l,r);
    int mid=(l+r)/2;
    int sum=0;
    if(x<=mid)
    {
        sum+=query(id<<1,l,mid,x,y);
    }
    if(y>mid)
    {
        sum+=query(id<<1|1,mid+1,r,x,y);
    }
    return sum;
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);

        a[0]=0;
        for(int i=1; i<=n; i++)
        {
            a[i]=1;
        }
        build(1,1,n);
        int f;
        int x,y;
        scanf("%d",&f);
        int key;
        while(f--)
        {
            scanf("%d%d%d",&x,&y,&key);
            update_section(1,1,n,x,y,key);

        }
        printf("Case %d: The total value of the hook is %d.\n",cc++,query(1,1,n,1,n));//查询1-n的和

    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值