当前位置: 代码迷 >> 综合 >> A Simple Problem with Integers//POJ - 3468//线段树
  详细解决方案

A Simple Problem with Integers//POJ - 3468//线段树

热度:47   发布时间:2024-01-10 06:48:16.0

A Simple Problem with Integers//POJ - 3468//线段树


题目

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.
题意
求区间和,中间会有改变
链接https://vjudge.net/contest/352439#problem/B

思路

线段树模板题

代码

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
using namespace std;
typedef long long ll;
ll n,m;
int a[100100],val;
void pushdown(int node);struct seg{
    ll sum,lazy;int l,r;
}segtree[100100*4];void build(int node,int l,int r){
    segtree[node].l=l;segtree[node].r=r;segtree[node].lazy=0;if(l==r){
    segtree[node].sum=a[l];return;}else{
    int mid=(l+r)>>1;build(node<<1,l,mid);build(node<<1|1,mid+1,r);segtree[node].sum = segtree[node<<1].sum + segtree[node<<1|1].sum;}
}ll query(int node,int x,int y){
             //求和if(segtree[node].l == x && segtree[node].r == y){
    return segtree[node].sum;}pushdown(node);int mid = segtree[node].l + segtree[node].r;ll res = 0;mid >>= 1;if(mid >= y)res += query(node<<1,x,y);else if(mid < x)res += query(node<<1|1,x,y);else{
    res += query(node<<1,x,mid);res += query(node<<1|1,mid+1,y);}return res;
}void pushdown(int node){
    if(segtree[node].lazy != 0){
    segtree[node<<1].lazy += segtree[node].lazy;segtree[node<<1|1].lazy += segtree[node].lazy;segtree[node<<1].sum += segtree[node].lazy * (segtree[node<<1].r - segtree[node<<1].l + 1);segtree[node<<1|1].sum += segtree[node].lazy * (segtree[node<<1|1].r - segtree[node<<1|1].l + 1);;segtree[node].lazy = 0;}
}void update(int node,int x,int y){
    					//更新if(segtree[node].l == x && segtree[node].r == y){
    segtree[node].lazy += val;segtree[node].sum += val * (y - x + 1);return;}if(segtree[node].l == segtree[node].r)return;pushdown(node);int mid = segtree[node].l + segtree[node].r;mid >>= 1;if(mid >= y)update(node<<1,x,y);else if(mid < x)update(node<<1|1,x,y);else{
    update(node<<1,x,mid);update(node<<1|1,mid+1,y);}segtree[node].sum = segtree[node<<1].sum + segtree[node<<1|1].sum;
}int main()
{
    char ch[5];scanf("%lld%lld",&n,&m);for(ll i=1;i<=n;i++)scanf("%lld",&a[i]);build(1,1,n);for(ll i=1;i<=m;i++){
    scanf("%s",ch);if(ch[0]=='C'){
    ll x,y;scanf("%lld%lld%lld",&x,&y,&val);update(1,x,y);}else{
    ll x,y;scanf("%lld%lld",&x,&y);ll ans=query(1,x,y);printf("%lld\n",ans);}}return 0;
}

注意

  相关解决方案