概述
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <vector>
using namespace std;
#define REP(i, a, b) for (LL i = (a), _end_ = (b); i <= _end_; ++i)
#define DREP(i, a, b) for (LL i = (a), _begin_ = (b); i >= _begin_; --i)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define mp make_pair
#define x first
#define y second
#define pb push_back
#define SZ(x) (LL((x).size()))
#define ALL(x) (x).begin(), (x).end()
template<typename T> inline bool chkmin(T &a, const T &b){ return a > b ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, const T &b){ return a < b ? a = b, 1 : 0; }
typedef long long LL;
const LL dmax = 100100 << 2, oo = 0x3f3f3f3f;
LL n, m;
LL a[dmax];
LL c[dmax], add[dmax];
#define left x << 1, l, mid
#define right x << 1 | 1, mid + 1, r
inline LL Mid(LL x, LL y){ return (x + y) >> 1; }
inline void push_up(LL x){
c[x] = c[x << 1] + c[x << 1 | 1]; }
inline void push_down(LL x, LL y)
{
if (add[x])
{
add[x << 1] += add[x];
add[x << 1 | 1] += add[x];
c[x << 1] += (y - (y >> 1)) * add[x];
c[x << 1 | 1] += (y >> 1) * add[x];
add[x] = 0;
}
}
void create(LL x, LL l, LL r)
{
if (l == r)
{
scanf("%I64d", &c[x]);
return;
}
LL mid = Mid(l, r);
create(left);
create(right);
push_up(x);
}
void update(LL x, LL l, LL r, LL s, LL t, LL k)
{
if (l >= s && r <= t)
{
add[x] += k;
c[x] += (LL)k * (r - l + 1);
return;
}
LL mid = Mid(l, r);
push_down(x, r - l + 1);
if (s <= mid)
update(left, s, t, k);
if (t > mid)
update(right, s, t, k);
push_up(x);
}
LL query(LL x, LL l, LL r, LL s, LL t)
{
if (l >= s && r <= t)
return c[x];
push_down(x, r - l + 1);
LL mid = Mid(l, r);
LL sum = 0;
if (s <= mid) sum += query(left, s, t);
if (mid < t) sum += query(right, s, t);
return sum;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
scanf("%I64d%I64d", &n, &m);
create(1, 1, n);
getchar();
REP(i, 1, m)
{
LL c = getchar(), x, y;
scanf("%I64d%I64d", &x, &y);
if (c == 'Q')
printf("%I64dn", query(1, 1, n, x, y));
else{
LL z;
scanf("%I64d", &z);
update(1, 1, n, x, y, z);
}
getchar();
}
return 0;
}
最后
以上就是年轻大白为你收集整理的POJ3468 线段树 + Lazy Tag (延迟标记)的全部内容,希望文章能够帮你解决POJ3468 线段树 + Lazy Tag (延迟标记)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复