我自己的小小线段树模板
#include<iostream>
#include<cstdio>
using namespace std;
#define maxn 100005
#define mid m=(l+r)>>1
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int c[maxn << 2];
int mark[maxn << 2];
void pushup(int rt)
{
c[rt] = c[rt << 1] + c[rt << 1 | 1];
}
void pushdown(int rt, int m)
{
if (mark[rt])
{
mark[rt << 1] += mark[rt];
mark[rt << 1 | 1] += mark[rt];
c[rt << 1] += (m - (m >> 1))*mark[rt];
c[rt << 1 | 1] += (m >> 1)*mark[rt];
mark[rt] = 0;
}
}
void build(int l, int r, int rt)
{
mark[rt] = 0;
if (l == r)
{
scanf("%intd", &c[rt]);
return;
}
int mid;
build(lson);
build(rson);
pushup(rt);
}
void see(int l, int r, int rt)
{
cout << l << " " << r << " " << c[rt] << " " << mark[rt] << endl;
if (l == r)
{
return;
}
int mid;
see(lson);
see(rson);
}
void updata(int L, int R, int d, int l, int r, int rt)
{
if (L <= l && r <= R)
{
c[rt] += (r - l + 1)*d;
mark[rt] += d;
return;
}
pushdown(rt, r - l + 1);
int mid;
if (L <= m)
{
updata(L, R, d, lson);
}
if (R > m)
{
updata(L, R, d, rson);
}
pushup(rt);
}
int query(int L, int R, int l, int r, int rt)
{
if (L <= l&&r <= R)
{
return c[rt];
}
int ans = 0, mid;
pushdown(rt, r - l + 1);
if (L <= m)
{
ans += query(L, R, lson);
}
if (R > m)
{
ans += query(L, R, rson);
}
return ans;
}
int main()
{
int n, m;
scanf("%intd%intd", &n, &m);
build(1, n, 1);
while (m--)
{
char q;
cin >> q;
if (q == 'Q')
{
int a, b;
scanf("%d%d", &a, &b);
cout << query(a, b, 1, n, 1) << endl;
}
else
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
updata(a, b, c, 1, n, 1);
//see(1,n,1);
}
}
}