【nowcoder】Tree Recovery(线段树-区间更新 / 前缀和与差分)

链接:https://www.nowcoder.com/acm/contest/77/H
来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 131072K,其他语言262144K
64bit IO Format: %lld
题目描述
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.
输入描述:
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.
输出描述:
You need to answer all Q commands in order. One answer in a line.
示例1
输入
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
输出
4
55
9
15

分析:题目数据比较弱,可以直接暴力求解
另外可用一维前缀和与差分 复杂度 O(n)
当数据较大,可用线段树的区间更新求和 模板

【前缀和与差分】代码:
/**
给定序列a,对[l,r] 的所有元素都加上k,求[L,R]的ai和
用前缀和求解 O(n)
///p[i]=a[i]-a[i-1]
对【l,r】内元素加k 转化为 p[l]+=k,p[r+1]-=k;
然后对修改后的数组a求和
一旦n过大,还是不适用

**/

#include <bits/stdc++.h>
using namespace std;

#define mem(a,n) memset(a,n,sizeof(a))
#define memc(a,b) memcpy(a,b,sizeof(b))
#define rep(i,a,n) for(int i=a;i<n;i++) ///[a,n)
#define dec(i,n,a) for(int i=n;i>=a;i--)///[n,a]
#define pb push_back
#define fi first
#define se second
#define IO ios::sync_with_stdio(false)
#define fre freopen("in.txt","r",stdin)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef long long ll;
typedef unsigned long long ull;
const double PI=acos(-1.0);
const double E=2.718281828459045;
const double eps=1e-3;
const int INF=0x3f3f3f3f;
const int MOD=258280327;
const int N=1e5+5;
const ll maxn=1e6+5;
const int dir[4][2]= {-1,0,1,0,0,-1,0,1};
int a[N],p[N];
int n,q;
void add(int l,int r,int k)
{
    p[l]+=k,p[r+1]-=k;
}
void get_a()
{
    rep(i,1,n+1) a[i]=a[i-1]+p[i];
}
int main()
{
    while(~scanf("%d%d",&n,&q))
    {
        mem(a,0);
        mem(p,0);
        rep(i,1,n+1)
        {
            scanf("%d",&a[i]);
            p[i]=a[i]-a[i-1];
        }
        while(q--)
        {
            char op;
            scanf(" %c",&op);
            if(op=='C')
            {
                int l,r,k;
                scanf("%d%d%d",&l,&r,&k);
                add(l,r,k);
                get_a();
            }
            else
            {
                int l,r;
                scanf("%d%d",&l,&r);
                ll sum=0;
                rep(i,l,r+1) sum+=a[i];
                printf("%lld\n",sum);
            }
        }
    }
    return 0;
}

【线段树 区间更新模板】

#include <bits/stdc++.h>
using namespace std;

#define mem(a,n) memset(a,n,sizeof(a))
#define memc(a,b) memcpy(a,b,sizeof(b))
#define rep(i,a,n) for(int i=a;i<n;i++) ///[a,n)
#define dec(i,n,a) for(int i=n;i>=a;i--)///[n,a]
#define pb push_back
#define fi first
#define se second
#define IO ios::sync_with_stdio(false)
#define fre freopen("in.txt","r",stdin)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef long long ll;
typedef unsigned long long ull;
const double PI=acos(-1.0);
const double E=2.718281828459045;
const double eps=1e-3;
const int INF=0x3f3f3f3f;
const int MOD=258280327;
const int N=1e5+5;
const ll maxn=1e6+5;
const int dir[4][2]= {-1,0,1,0,0,-1,0,1};
ll sum[N<<2],a[N],MAX[N<<2],ad[N<<2];
void pushup(ll rt)
{
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
    // MAX[rt]=max(MAX[rt<<1],MAX[rt<<1|1]);
}
void pushdown(int rt,int m)
{
    if(ad[rt])
    {
        ad[rt<<1]+=ad[rt];
        ad[rt<<1|1]+=ad[rt];
        sum[rt<<1]+=ad[rt]*(m-(m>>1));
        sum[rt<<1|1]+=ad[rt]*(m>>1);
        ad[rt]=0; ///标记清除!!!
    }
}
void build(ll l,ll r,ll rt)
{
    ad[rt]=0;
    if(l==r)
    {
        scanf("%lld",&sum[rt]);
        return ;
    }
    ll m=(l+r)>>1;
    build(lson);
    build(rson);
    pushup(rt);
}
void update(ll L,ll R,ll c,ll l,ll r,ll rt)
{
    if(L<=l&&R>=r)
    {
        ad[rt]+=c;
        sum[rt]+=c*(r-l+1);
        return ;
    }
    pushdown(rt,r-l+1);
    ll m=(l+r)>>1;
    if(L<=m) update(L,R,c,lson);
    if(R>m) update(L,R,c,rson);
    pushup(rt);
}
ll query(ll L,ll R,ll l,ll r,ll rt)
{
    if(L<=l&&R>=r)
    {
        return sum[rt];
    }
    pushdown(rt,r-l+1);
    ll m=(l+r)>>1;
    ll ret=0;
    if(L<=m) ret+=query(L,R,lson);
    if(R>m) ret+=query(L,R,rson);
    return ret;
}
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        build(1,n,1);
        for(int i=0; i<m; i++)
        {
            char op;
            ll a,b,c;
            scanf(" %c",&op);
            if(op=='Q')
            {
                scanf("%lld%lld",&a,&b);
                printf("%lld\n",query(a,b,1,n,1));

            }
            else
            {
                scanf("%lld%lld%lld",&a,&b,&c);
                update(a,b,c,1,n,1);
            }
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值