区间加、区间查询
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1e5 + 10;
struct SegmentTree{
int l, r;
long long sum, add;
}t[N * 4];
int a[N], n, m;
void pushup(int p) {
t[p].sum = t[p << 1].sum + t[p << 1 | 1].sum;
}
void build(int p, int l, int r){
t[p].l = l;
t[p].r = r;
if(l == r){
t[p].sum = a[l];
return;
}
int mid = l + r >> 1;
build(p << 1, l, mid);
build (p << 1 | 1, mid + 1, r);
pushup(p);
}
void pushdown(int p) {
if(t[p].add) {
int ls = p << 1;
int rs = p << 1 | 1;
t[ls].sum += t[p].add * (t[ls].r - t[ls].l + 1);
t[rs].sum += t[p].add * (t[rs].r - t[rs].l + 1);
t[ls].add += t[p].add;
t[rs].add += t[p].add;
t[p].add = 0;
}
}
void change(int p, int l, int r, int d) {
if(l <= t[p].l && r >= t[p].r) {
t[p].sum += (long long)d * (t[p].r - t[p].l + 1);
t[p].add += d;
return;
}
pushdown(p);
int mid = t[p].l + t[p].r >> 1;
if(l <= mid) change(p << 1, l, r, d);
if(r > mid) change(p << 1 | 1, l, r, d);
pushup(p);
}
long long ask(int p, int l, int r) {
if(l <= t[p].l && r >= t[p].r) return t[p].sum;
pushdown(p);
int mid = t[p].l + t[p].r >> 1;
long long val = 0;
if(l <= mid) val += ask(p << 1, l, r);
if(r > mid) val += ask(p << 1 | 1, l, r);
return val;
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
for(int i = 1; i <= n ; i ++)
scanf("%d", &a[i]);
build(1, 1, n);
int op, x, y, k;
for(int i = 1; i <= m ; i ++) {
scanf("%d%d%d", &op, &x, &y);
if(op == 1){
scanf("%d", &k);
change(1, x, y, k);
}
else{
printf("%lld
", ask(1, x, y));
}
}
return 0;
}
区间加、区间乘、区间查询
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define LL long long
const int N = 1e5 + 10;
int n, p, m;
int a[N];
struct TREE{
int l, r;
LL sum, add, mul;
}tr[N * 4];
void pushup(int u) {
tr[u].sum = (tr[u << 1].sum + tr[u << 1 | 1].sum) % p;
}
void eval(TREE &t, int add, int mul) {
t.sum = ((LL)t.sum * mul + (LL)(t.r - t.l + 1) * add) % p;
t.mul = (LL)t.mul * mul % p;
t.add = ((LL)t.add * mul + add) % p;
}
void pushdown(int u) {
eval(tr[u << 1], tr[u].add, tr[u].mul);
eval(tr[u << 1 | 1], tr[u].add, tr[u].mul);
tr[u].add = 0;
tr[u].mul = 1;
}
void build(int u, int l, int r) {
tr[u].l = l;
tr[u].r = r;
if(l == r){
tr[u].sum = a[r];
tr[u].add = 0;
tr[u].mul = 1;
return;
}
tr[u].sum = 0;
tr[u].add = 0;
tr[u].mul = 1;
int mid = l + r >> 1;
build(u << 1, l, mid);
build(u << 1 | 1, mid + 1, r);
pushup(u);
}
void modify(int u, int l, int r, int add, int mul) {
if(tr[u].l >= l && tr[u].r <= r) eval(tr[u], add, mul);
else {
pushdown(u);
int mid = tr[u].l + tr[u].r >> 1;
if(l <= mid) modify(u << 1, l, r, add, mul);
if(r > mid) modify(u << 1 | 1, l, r, add, mul);
pushup(u);
}
}
int query(int u, int l, int r) {
if(tr[u].l >= l && tr[u].r <= r) return tr[u].sum;
pushdown(u);
int mid = tr[u].l + tr[u].r >> 1;
int sum = 0;
if(l <= mid) sum = query(u << 1, l, r);
if(r > mid) sum = (sum + query(u << 1 | 1, l, r)) % p;
return sum;
}
int main() {
scanf("%d%d%d", &n, &m, &p);
for(int i = 1; i <= n; i ++)
scanf("%d", &a[i]);
build(1, 1, n);
while(m --) {
int t, l, r, d;
scanf("%d%d%d", &t, &l, &r);
if(t == 1){
scanf("%d", &d);
modify(1, l, r, 0, d);
}
else if(t == 2) {
scanf("%d", &d);
modify(1, l, r, d, 1);
}
else printf("%lld
", query(1, l, r));
}
return 0;
}