hdu 1698 poj 3468 线段树 成段更新(lazy标记)

 

Just a Hook

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


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.


题意:

给出一个系列,初始化为每一个的值为1,有十万个更新区间的操作,更新为相应的 1 或 2 或 3,最后求总值


题解:

线段树的成段更新..

  用到了lazy[] 表示懒惰标志..

  懒惰标记:

    就是每次更新不更新到最后..而是更新到包含了区间的最大的节点..

    然后如果下次更新的时候更新到了上次已经更新到的节点..

    那先把上次更新暂停的节点往下更新..直到这次更新的区间最大的节点没有被标记..

最后输出总值sum【1】就可以了


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

#define MAXN 100010
#define lson l,mid,p<<1
#define rson mid+1,r,p<<1|1
int sum[MAXN<<2],lazy[MAXN<<2];

void pushUp(int p)
{
    sum[p]=sum[p<<1]+sum[p<<1|1];
}
void pushDown(int p,int num)
{
    if(lazy[p]!=-1)
    {
        lazy[p<<1]=lazy[p<<1|1]=lazy[p];
        sum[p<<1]=(num-(num>>1))*lazy[p];
        sum[p<<1|1]=(num>>1)*lazy[p];
        lazy[p]=-1;
    }
}
void build(int l,int r,int p)
{
    lazy[p]=-1;
    sum[p]=1;
    if(l==r)
        return;

    int mid=(l+r)>>1;
    build(lson);
    build(rson);
    pushUp(p);
}
void update(int L,int R,int num,int l,int r,int p)
{
    if(L<=l&&r<=R){
        lazy[p]=num;
        sum[p]=num*(r-l+1);
        return;
    }
    pushDown(p,r-l+1);
    int mid=(l+r)>>1;
    if(mid>=L)
        update(L,R,num,lson);
    if(mid<R)
        update(L,R,num,rson);
    pushUp(p);
}
int main()
{
    int cases=1;
    int n,m,T;
    //freopen("in.txt","r",stdin);
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        build(1,n,1);
        while(m--)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            update(a,b,c,1,n,1);
        }
        printf("Case %d: The total value of the hook is %d.\n",cases++,sum[1]);
    }
    return 0;
}




A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 111524 Accepted: 34729
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.

Source




#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

#define MAXN 100010
#define lson l,mid,p<<1
#define rson mid+1,r,p<<1|1
#define LL long long
LL sum[MAXN<<2],lazy[MAXN<<2];

void pushUp(int p)
{
    sum[p]=sum[p<<1]+sum[p<<1|1];
}
void pushDown(int p,int r,int l)
{
    if(lazy[p])
    {
        int mid=(r+l)>>1;
        lazy[p<<1]+=lazy[p];
        lazy[p<<1|1]+=lazy[p];
        sum[p<<1]+=(mid-l+1)*lazy[p];
        sum[p<<1|1]+=(r-mid)*lazy[p];
        lazy[p]=0;
    }
}
void build(int l,int r,int p)
{
    lazy[p]=0;
    if(l==r){
        scanf("%lld",&sum[p]);
        return;
    }

    int mid=(l+r)>>1;
    build(lson);
    build(rson);
    pushUp(p);
}
void update(int l,int r,int p,int L,int R,LL num)
{
    if(L<=l&&r<=R){
        lazy[p]+=num;
        sum[p]+=num*(r-l+1);
        return;
    }

    pushDown(p,r,l);
    int mid=(l+r)>>1;
    if(mid>=L)
        update(lson,L,R,num);
    if(mid<R)
        update(rson,L,R,num);
    pushUp(p);
}
LL query(int l,int r,int p,int i,int j)
{
    if(l>=i&&r<=j){
        return sum[p];
    }
    LL ans=0;

    pushDown(p,r,l);
    int mid=(l+r)>>1;
    if(mid<j) ans+=query(rson,i,j);
    if(mid>=i) ans+=query(lson,i,j);
    return ans;
}

int main()
{
    int n,m;
    //freopen("in.txt","r",stdin);
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        LL c;
        int a,b;
        char ch[5];
        build(1,n,1);
        while(m--)
        {
            scanf("%s",ch);
            if(ch[0]=='C'){
                scanf("%d%d%lld",&a,&b,&c);
                update(1,n,1,a,b,c);
            }
            else{
                scanf("%d%d",&a,&b);
                LL ans=query(1,n,1,a,b);
                printf("%lld\n",ans);
            }
        }
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值