kuangbin 专题七 线段树(POJ 3468)A Simple Problem with Integers

A Simple Problem with Integers

Time limit5000 ms
Case time limit2000ms
Memory limit131072 kB
OSLinux

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. 

你有n个整数,A1,A2,…An。你需要处理两种操作,一种操作是在给定的区间内为每个数字添加一个给定的数字,另一种是求给定区间内的数字之和。

  • 输入
第一行包含两个数字n和q.         1 ≤ n,q ≤ 100000
第二行包含n个数字,A1,A2,…An.        -100000000 ≤ AI ≤ 100000000
下一个q行中的每一行表示一个操作
 “C a b c”是指在Aa、Aa+1,…,Ab.        -10000 ≤ c ≤ 10000
 “Q a b”表示查询区间Aa,Aa+1,…Ab的和. 
  • 输出
您需要按顺序回答所有Q命令, 一行一个答案。 
  • 提示
总和可能超过32位整数的范围。 

仍仍仍然是一道板子题
我天…这两天就在疯狂练打模板
一道区间修改的题

Time2891ms
Memory9248kB
Length2257
LangG++
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
const ll maxa = 0x3f3f3f;
int num[100005];

struct Btree {
  ll l, r, sum, lazy;
}tree[400005];

inline void pushup(int k) {
  tree[k].sum = max(tree[k << 1].sum , tree[k << 1 | 1].sum);
}

void pushdown(int now) {
  if(tree[now].l == tree[now].r) {
    tree[now].lazy = 0;
    return;
  }
  tree[now << 1].sum += (tree[now << 1].r - tree[now << 1].l + 1) * tree[now].lazy;
  tree[now << 1 | 1].sum += (tree[now << 1 | 1].r - tree[now << 1 | 1].l + 1) * tree[now].lazy;
  tree[now << 1].lazy += tree[now].lazy;
  tree[now << 1 | 1].lazy += tree[now].lazy;
  tree[now].lazy = 0;
}

void build(int now, int l, int r) {
  tree[now].lazy = 0;
  tree[now].l = l, tree[now].r = r;
  if(l == r) {
    tree[now].sum = num[l];
    return;
  }
  int mid = (l + r) >> 1;
  build(now << 1, l, mid);
  build(now << 1 | 1, mid + 1, r);
  pushup(now);
}

void updateSeg(int now, int l, int r, int x){
  if (tree[now].l == l && tree[now].r == r) {
    tree[now].sum += 1ll * (tree[now].r - tree[now].l + 1) * x;
    tree[now].lazy += x;
    return;
  }
  int mid = (tree[now].l + tree[now].r) >> 1;
  if(tree[now].lazy) pushdown(now);
  if(r <= mid) updateSeg(now << 1, l, r, x);
  else if(l > mid) updateSeg(now << 1 | 1, l, r, x);
  else{
    updateSeg(now << 1, l, mid, x);
    updateSeg(now << 1 | 1, mid + 1, r, x);
  }
  pushup(now);
}

ll query(int now, int l, int r){
  if(l == tree[now].l && r == tree[now].r) return tree[now].sum;
  if(tree[now].lazy) pushdown(now);
  int mid = (tree[now].r + tree[now].l) >> 1;
  if(r <= mid) return query(now << 1, l, r);
  else if(l > mid) return query(now << 1 | 1, l, r);
  else return query(now << 1, l, mid) + query(now << 1 | 1, mid + 1, r);
}

int main(int argc, char const *argv[]) {
  int n, q, i, j, k;
  char s;
  while (~scanf("%d %d", &n, &q)) {
    for (int h = 1; h <= n; h++) {
      scanf("%d", &num[h]);
    }
    build(1, 1, n);
    for (int g = 0; g < q; g++) {
      scanf(" %c", &s);
      if(s == 'Q') {
        scanf("%d %d", &i, &j);
        printf("%lld\n", query(1, i, j));
      }
      if(s == 'C') {
        scanf("%d %d %d", &i, &j, &k);
        updateSeg(1, i, j, k);
      }
    }
  }
  return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值