[poj 3468] A Simple Problem with Integers(裸线段树)
来源:互联网 发布:淘宝广州模特招聘骗局 编辑:程序博客网 时间:2024/06/15 23:51
水题,只要注意结果是long long就行了。
#include<iostream>
using namespace std;
#define maxn 100010
struct node
{
int l;
int r;
long long add;
long long sum;
}tree[maxn * 4];
long long num[maxn];
void down(int num)
{
tree[num<<1].sum += tree[num].add*(tree[num<<1].r - tree[num<<1].l + 1);
tree[num<<1|1].sum += tree[num].add*(tree[num<<1|1].r - tree[num<<1|1].l + 1);
tree[num<<1].add += tree[num].add;
tree[num<<1|1].add += tree[num].add;
tree[num].add = 0;
}
void build(int l, int r, int root)
{
tree[root].l = l;
tree[root].r = r;
tree[root].add = 0;
if(l == r)
{
tree[root].sum = num[l];
return;
}
int mid = (l + r) >> 1;
build(l, mid, root<<1);
build(mid + 1, r, root<<1|1);
tree[root].sum = tree[root<<1].sum + tree[root<<1|1].sum;
}
void update(int l, int r, int num, int root)
{
if(l <= tree[root].l && tree[root].r <= r)
{
tree[root].add += num;
tree[root].sum += num*(tree[root].r - tree[root].l + 1);
return;
}
if(tree[root].add)
down(root);
int mid = (tree[root].l + tree[root].r) >> 1;
if(l <= mid)
update(l, r, num, root<<1);
if(r > mid)
update(l, r, num, root<<1|1);
tree[root].sum = tree[root<<1].sum + tree[root<<1|1].sum;
}
long long query(int l, int r, int root)
{
if(l <= tree[root].l && tree[root].r <= r)
{
return tree[root].sum;
}
if(tree[root].add)
down(root);
int mid = (tree[root].l + tree[root].r) >> 1;
long long ans1 = 0, ans2 = 0;
if(l <= mid)
ans1 = query(l, r, root<<1);
if(r > mid)
ans2 = query(l, r, root<<1|1);
return ans1 + ans2;
}
int main()
{
int n, q;
char ch;
int a, b, c;
while(~scanf("%d%d", &n, &q))
{
for(int i = 1; i <= n; i++)
scanf("%lld", &num[i]);
build(1, n, 1);
while(q--)
{
getchar();
scanf("%c%d%d", &ch, &a, &b);
if(ch == 'C')
{
scanf("%d", &c);
update(a, b, c, 1);
}
else
{
printf("%lld\n", query(a, b, 1));
}
}
}
return 0;
}
- [poj 3468] A Simple Problem with Integers(裸线段树)
- BZOJ-3212 Pku3468 A Simple Problem with Integers 裸线段树区间维护查询
- poj 3468 -- A Simple Problem with Integers ( 线段树 , 段更新 , 段求和 )
- poj 3468 A Simple Problem with Integers(线段树|成段更新,区间查询)
- poj 3468 A Simple Problem with Integers(线段树,成段更新)
- POJ 3468 A Simple Problem with Integers (线段树—成段覆盖)
- poj 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
- POJ 3468 A Simple Problem with Integers 线段树 (成段更新)
- [ACM] poj 3468 A Simple Problem with Integers(线段树,成段更新,懒惰标记)
- 线段树(成段更新) 之 poj 3468 A Simple Problem with Integers
- POJ 3468-A Simple Problem with Integers(线段树:成段更新,区间求和)
- POJ 3468 A Simple Problem with Integers(线段树 成段更新)
- POJ - 3468 - A Simple Problem with Integers (线段树 - 成段更新)
- POJ 3468 A Simple Problem with Integers(线段树 成段增减,区间求和)
- POJ 3468 A Simple Problem with Integers 线段树(成段更新)
- POJ 3468 A Simple Problem with Integers 成段更新
- poj 3468A Simple Problem with Integers(成段更新)
- POJ 3468 A SIMPLE PROBLEM WITH INTEGERS(线段树)
- hdu 1598 find the most comfortable road
- poj 2823 Sliding Window (单调队列 or 线段树)
- poj1001_BigNumMultipy
- [hrbust 2029] 二十世纪八十年代(状态压缩)
- [hrbust 2030] 成语接龙(状态压缩 + dfs)
- [poj 3468] A Simple Problem with Integers(裸线段树)
- [hdu 1181] 变形课(传递闭包)
- [hdu 1016] Prime Ring Problem(剪枝dfs)
- [hdu 3829] Cat VS Dog(二分图最大独立集)
- [转载] 有向图强连通分量的Tarjan算法 ——byvoid
- [iOS] 关于 self = [super init];
- Oracle中的AS和IS的不同
- 谈谈网络中的连接设备
- 2-sat概括性总结