POJ3468 A Simple Problem with Integers---樹狀陣列(區間問題)
POJ3468 A Simple Problem with Integers
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.
Code
#include<iostream>
#include<cstring>
using namespace std;
const int maxn = 1e5 + 3;
typedef long long ll;
// 思路是通過公式演變過來的,這裡不多講
// 第一個陣列存範圍變化的字首和
// 第二個陣列存 i * tree[0][i]的字首和
// 求解 get(l, r) = (sum[r] + (r + 1) * get(0, r) - get(1, r))
// - (sum[l - 1] + l * get(0, l - 1) - get(1, l - 1))
char op;
ll sum[maxn], tree[2][maxn], n, q, l, r, v, ans;
inline ll lowbit(ll x) { return x & -x; }
void add(ll f, ll x, ll v) { for (; x <= n; x += lowbit(x)) tree[f][x] += v; }
ll get(ll f, ll x) {
ll ans = 0;
for (; x; x -= lowbit(x)) ans += tree[f][x];
return ans;
}
inline ll read() {
ll f = 1, x = 0; char ch = getchar();
while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') x = x * 10 + ch - 48, ch = getchar();
return f * x;
}
int main() {
memset(tree, 0, sizeof tree);
memset(sum, 0, sizeof sum);
n = read(), q = read();
for (ll i = 1; i <= n; i++) sum[i] = sum[i - 1] + read();
while (q--) {
op = getchar(), l = read(), r = read();
if (op == 'Q') {
ans = (sum[r] + (r + 1) * get(0, r) - get(1, r)) - (sum[l - 1] + l * get(0, l - 1) - get(1, l - 1));
cout << ans << endl;
}
else {
v = read();
add(0, l, v);
add(0, r + 1, -v);
add(1, l, v * l);
add(1, r + 1, -v * (r + 1));
}
}
return 0;
}
相關文章
- (poj3468)A Simple Problem with Integers(區間更新)
- 【poj3468】A Simple Problem with Integers
- 樹狀陣列模板題 & (樹狀陣列 1:單點修改,區間查詢)陣列
- POJ 3468 A Simple Problem with Integers(線段樹區間操作)
- POJ 3468 A Simple Problem with Integers (線段樹 區間更新)
- 【樹狀陣列 區間更新區間查詢】code陣列
- POJ 3468 A Simple Problem with Integers (線段樹 區間共加)
- POJ 3468-A Simple Problem with Integers(區間更新線段樹)
- 樹狀陣列的區間查詢與區間修改陣列
- D 區間求和 [數學 樹狀陣列]陣列
- POJ 3468 【區間修改+區間查詢 樹狀陣列 | 線段樹 | 分塊】陣列
- 樹狀陣列單點更新和區間查詢陣列
- 求區間不同數的個數【樹狀陣列求解】陣列
- 變化的區間樹狀陣列,單點查詢陣列
- 最大子陣列問題(Maximum subarray problem)陣列
- 樹狀陣列陣列
- 樹狀陣列模板+習題集陣列
- HDU 1556【區間更新+單點查詢 樹狀陣列】陣列
- 【樹狀陣列 區間修改,單點求值】1556 Color the ball陣列
- 【Leetcode每日一題】327. 區間和的個數(線段樹/樹狀陣列)LeetCode每日一題陣列
- 解析樹狀陣列陣列
- 【樹狀陣列 單點修改,區間求值】hdu 1166 敵兵佈陣陣列
- POJ 1195-Mobile phones(二維樹狀陣列-區間更新區間查詢)陣列
- 樹狀陣列詳解陣列
- 樹狀陣列基礎陣列
- poj 2481 樹狀陣列陣列
- hdu 3874 樹狀陣列陣列
- 二維樹狀陣列陣列
- HDU 1556-Color the ball(樹狀陣列-區間修改 單點查詢)陣列
- HDU1166 敵兵佈陣【樹狀陣列 單點修改+區間查詢】陣列
- 洛谷題單指南-二叉堆與樹狀陣列-P3368 【模板】樹狀陣列 2陣列
- 樹狀陣列和逆序對陣列
- hdu 5147 樹狀陣列陣列
- 【筆記/模板】樹狀陣列筆記陣列
- 樹狀陣列快速入門陣列
- 關於區間操作查詢(字首和與差分)+樹狀陣列基礎陣列
- POJ 2155-Matrix(二維樹狀陣列-區間修改 單點查詢)陣列
- 樹狀陣列3種基本操作陣列