codevs 1082 线段树练习 3 区间更新+延迟标记

题目描述 Description

给你N个数,有两种操作:

1:给区间[a,b]的所有数增加X

2:询问区间[a,b]的数的和。

输入描述 Input Description

第一行一个正整数n,接下来n行n个整数,

再接下来一个正整数Q,每行表示操作的个数,

如果第一个数是1,后接3个正整数,

表示在区间[a,b]内每个数增加X,如果是2,

表示操作2询问区间[a,b]的和是多少。

pascal选手请不要使用readln读入

输出描述 Output Description

对于每个询问输出一行一个答案

样例输入 Sample Input

3

1

2

3

2

1 2 3 2

2 2 3

样例输出 Sample Output

9

数据范围及提示 Data Size & Hint

数据范围

1<=n<=200000

1<=q<=200000

思路:线段树+延迟标记

#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
#define true ture
#define false flase
using namespace std;
#define ll long long
int scan()
{
    int res = 0 , ch ;
    while( !( ( ch = getchar() ) >= ‘0‘ && ch <= ‘9‘ ) )
    {
        if( ch == EOF )  return 1 << 30 ;
    }
    res = ch - ‘0‘ ;
    while( ( ch = getchar() ) >= ‘0‘ && ch <= ‘9‘ )
        res = res * 10 + ( ch - ‘0‘ ) ;
    return res ;
}
struct is
{
    int l,r;
    ll num;
    int lazy;
}tree[200010*3];
void build_tree(int l,int r,int pos)
{
    tree[pos].l=l;
    tree[pos].r=r;
    tree[pos].lazy=0;
    if(l==r)
    {
        //tree[pos].num=1;
        scanf("%lld",&tree[pos].num);
        return;
    }
    int mid=(l+r)/2;
    build_tree(l,mid,pos*2);
    build_tree(mid+1,r,pos*2+1);
    tree[pos].num=tree[pos*2].num+tree[pos*2+1].num;
}
void update(int l,int r,int change,int pos)
{
    if(tree[pos].l==l&&tree[pos].r==r)
    {
        tree[pos].lazy+=change;
        tree[pos].num+=(tree[pos].r-tree[pos].l+1)*change;
        return;
    }
    if(tree[pos].lazy)
    {
        tree[pos*2].num+=(tree[pos*2].r+1-tree[pos*2].l)*tree[pos].lazy;
        tree[pos*2+1].num+=(tree[pos*2+1].r+1-tree[pos*2+1].l)*tree[pos].lazy;
        tree[pos*2].lazy+=tree[pos].lazy;
        tree[pos*2+1].lazy+=tree[pos].lazy;
        tree[pos].lazy=0;
    }
    int mid=(tree[pos].l+tree[pos].r)/2;
    if(r<=mid)
    update(l,r,change,pos*2);
    else if(l>mid)
    update(l,r,change,pos*2+1);
    else
    {
        update(l,mid,change,pos*2);
        update(mid+1,r,change,pos*2+1);
    }
    tree[pos].num=tree[pos*2].num+tree[pos*2+1].num;
}
ll query(int l,int r,int pos)
{
    //cout<<l<<" "<<r<<" "<<pos<<endl;
    if(tree[pos].l==l&&tree[pos].r==r)
    return tree[pos].num;
    if(tree[pos].lazy)
    {
        tree[pos*2].num+=(tree[pos*2].r+1-tree[pos*2].l)*tree[pos].lazy;
        tree[pos*2+1].num+=(tree[pos*2+1].r+1-tree[pos*2+1].l)*tree[pos].lazy;
        tree[pos*2].lazy+=tree[pos].lazy;
        tree[pos*2+1].lazy+=tree[pos].lazy;
        tree[pos].lazy=0;
    }
    int mid=(tree[pos].l+tree[pos].r)/2;
    if(l>mid)
    return query(l,r,pos*2+1);
    else if(r<=mid)
    return query(l,r,pos*2);
    else
    return query(l,mid,pos*2)+query(mid+1,r,pos*2+1);
}
int main()
{
    int x,q,i,t;
    while(~scanf("%d",&x))
    {
        build_tree(1,x,1);
        scanf("%d",&q);
        while(q--)
        {
            int flag,change,l,r;
            scanf("%d%d%d",&flag,&l,&r);
            if(flag==1)
            {
                scanf("%d",&change);
                update(l,r,change,1);
            }
            else
            printf("%lld\n",query(l,r,1));
        }
    }
    return 0;
}

时间: 2024-08-06 10:10:12

codevs 1082 线段树练习 3 区间更新+延迟标记的相关文章

codevs 1081 线段树练习 2 区间更新 单点查询 无lazy

题目描述 Description 给你N个数,有两种操作 1:给区间[a,b]的所有数都增加X 2:询问第i个数是什么? 输入描述 Input Description 第一行一个正整数n,接下来n行n个整数,再接下来一个正整数Q,表示操作的个数. 接下来Q行每行若干个整数.如果第一个数是1,后接3个正整数a,b,X,表示在区间[a,b]内每个数增加X,如果是2,后面跟1个整数i, 表示询问第i个位置的数是多少. 输出描述 Output Description 对于每个询问输出一行一个答案 样例输

HDOJ--4893--Wow! Such Sequence!【线段树+单点、区间更新】

链接:http://acm.hdu.edu.cn/showproblem.php?pid=4893 题意:给你一个长度n的数列,初始都为0,有三种操作,第一种给第k个位置的数加d,第二种是查询区间 [l , r] 的总和,第三种是使区间 [l , r] 的值改为离它最近的那个斐波那契数的值. 我刚开始用sum数组存储节点的值,第三种操作是个区间更新,但是区间更新的值不一样,我就想当然的搜到最底部的节点来处理更新,果断T了.后来想了想,其实可以在节点上再加一个信息,就是这个值下次进行第三种操作要变

codevs 1082 线段树练习3

1082 线段树练习 3 时间限制: 3 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 给你N个数,有两种操作: 1:给区间[a,b]的所有数增加X 2:询问区间[a,b]的数的和. 输入描述 Input Description 第一行一个正整数n,接下来n行n个整数, 再接下来一个正整数Q,每行表示操作的个数, 如果第一个数是1,后接3个正整数, 表示在区间[a,b]内每个数增加X,如果是2, 表示操作2询问区间[a,b]的和是多少.

codevs 1082 线段树联系3

1082 线段树练习 3 时间限制: 3 s 空间限制: 128000 KB 题目等级 : 大师 Master 题解 题目描述 Description 给你N个数,有两种操作: 1:给区间[a,b]的所有数增加X 2:询问区间[a,b]的数的和. 输入描述 Input Description 第一行一个正整数n,接下来n行n个整数, 再接下来一个正整数Q,每行表示操作的个数, 如果第一个数是1,后接3个正整数, 表示在区间[a,b]内每个数增加X,如果是2, 表示操作2询问区间[a,b]的和是多

codevs 1082 线段树练习 3

1082 线段树练习 3 时间限制: 3 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 给你N个数,有两种操作: 1:给区间[a,b]的所有数增加X 2:询问区间[a,b]的数的和. 输入描述 Input Description 第一行一个正整数n,接下来n行n个整数, 再接下来一个正整数Q,每行表示操作的个数, 如果第一个数是1,后接3个正整数, 表示在区间[a,b]内每个数增加X,如果是2, 表示操作2询问区间[a,b]的和是多少.

【树状数组区间修改区间求和】codevs 1082 线段树练习 3

http://codevs.cn/problem/1082/ [AC] 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int maxn=2e5+2; 5 int n; 6 ll a[maxn]; 7 ll c1[maxn]; 8 ll c2[maxn]; 9 int lowbit(int x) 10 { 11 return x&-x; 12 } 13 void add(l

codevs 1082 线段树练习 3 --分块练习

时间限制: 3 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 给你N个数,有两种操作: 1:给区间[a,b]的所有数增加X 2:询问区间[a,b]的数的和. 输入描述 Input Description 第一行一个正整数n,接下来n行n个整数, 再接下来一个正整数Q,每行表示操作的个数, 如果第一个数是1,后接3个正整数, 表示在区间[a,b]内每个数增加X,如果是2, 表示操作2询问区间[a,b]的和是多少. pascal选手请不要使用

杭电 HDU ACM 1698 Just a Hook(线段树 区间更新 延迟标记)

欢迎"热爱编程"的高考少年--报考杭州电子科技大学计算机学院 Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 20889    Accepted Submission(s): 10445 Problem Description In the game of DotA, Pudge's meat hook

HDU 1698 Just a Hook (区间更新+延迟标记)

Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 31096    Accepted Submission(s): 15313 Problem Description In the game of DotA, Pudge's meat hook is actually the most horrible thing