https://www.luogu.org/problemnew/show/P3372
1 #include <bits/stdc++.h> 2 #define lowbit(a) (a&-a) 3 4 using namespace std; 5 typedef long long LL; 6 7 const LL N = 100010; 8 LL n, m, q, c[N], c2[N], opt, x, y, k; 9 inline LL Sum(LL *a, LL x){ 10 LL ans = 0; 11 for(LL i = x; i; i -= lowbit(i)) ans += a[i]; 12 return ans; 13 } 14 15 inline void add(LL *a, LL x, LL q){ 16 for(LL i = x; i <= n; i += lowbit(i)) a[i] += q; 17 } 18 19 inline void read(LL &x){ 20 register LL c = getchar(), f = 1;x = 0; 21 while(!isdigit(c)) {if (c == ‘-‘) f = -1; c = getchar();} 22 while(isdigit(c)) x = (x<<3)+(x<<1)+(c^48), c = getchar(); 23 x *= f; 24 } 25 26 int main(void){ 27 LL now, tmp = 0; 28 read(n); read(m); 29 for(register LL i = 1; i <= n; ++i){ 30 read(now); 31 add(c, i, now-tmp); 32 add(c2, i, (i-1)*(now-tmp)); 33 tmp = now; 34 } 35 while(m--){ 36 LL opt; read(opt); 37 if (opt == 1){ 38 read(x), read(y), read(k); 39 add(c, x, k), add(c, y+1, -k), add(c2, x, (x-1)*k), add(c2, y+1, -k*y); 40 } 41 else { 42 read(x), read(y); 43 LL sum1 = (x-1)*Sum(c,x-1)-Sum(c2,x-1), 44 sum2 = y*Sum(c,y)-Sum(c2,y); 45 printf("%lld\n", sum2-sum1); 46 } 47 } 48 return 0; 49 }
树状数组
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 #include <cmath> 6 #define lson (o<<1) 7 #define rson (o<<1|1) 8 #define midd int mid = (l+r)>>1 9 using namespace std; 10 const int N = 100010; 11 12 int a[N], n, m; 13 14 inline int read() { 15 register int n, ch; 16 n = 0, ch = getchar(); 17 while (!isdigit(ch)) ch = getchar(); 18 while (isdigit(ch)) n = (n << 3) + (n << 1) + ch - ‘0‘, ch = getchar(); 19 return n; 20 } 21 struct Node{ 22 long long sum, add; 23 }t[N<<2]; 24 inline void pushup(int o){t[o].sum=t[lson].sum+t[rson].sum;} 25 inline void pushdown(int o, int l, int r){ 26 int ls = lson, rs = rson, mid = (l+r)>>1; 27 t[ls].add+=t[o].add; 28 t[rs].add+=t[o].add; 29 t[ls].sum+=(mid-l+1)*t[o].add; 30 t[rs].sum+=(r-mid)*t[o].add; 31 t[o].add=0; 32 } 33 inline void secadd(int o,int l, int r, int ql, int qr, int v){ 34 if (ql<=l&&r<=qr){ 35 t[o].add+=v; 36 t[o].sum+=(r-l+1)*v; 37 return ; 38 } 39 int mid = (l+r)>>1; 40 if (t[o].add) pushdown(o, l, r); 41 if (ql<=mid) secadd(lson,l,mid,ql,qr,v); 42 if (qr>mid) secadd(rson,mid+1,r,ql,qr,v); 43 pushup(o); 44 } 45 inline long long query(int o,int l, int r, int ql, int qr){ 46 if (ql<=l&&r<=qr) 47 return t[o].sum; 48 if (t[o].add) pushdown(o,l,r);int mid = (l+r)>>1;long long ans = 0; 49 if (ql<=mid) ans+=query(lson,l,mid,ql,qr); 50 if (qr>mid) ans += query(rson,mid+1,r,ql,qr); 51 return ans; 52 } 53 inline void build(int o, int l, int r){ 54 if (l==r){t[o].sum=a[l];return ;} 55 int mid = (l+r)>>1; 56 build(lson,l,mid), build(rson,mid+1,r); 57 pushup(o); 58 } 59 60 int main(void){ 61 n=read(), m=read(); 62 for(register int i = 1; i <= n; ++i){ 63 a[i] = read(); 64 } 65 build(1,1,n); 66 while(m--){ 67 register int bs = read(), x = read(), y = read(), k; 68 if (bs == 1){k = read(); secadd(1,1,n,x,y,k);} 69 else printf("%lld\n", query(1,1,n,x,y)); 70 } 71 return 0; 72 }
线段树
原文地址:https://www.cnblogs.com/Ycrpro/p/8452481.html
时间: 2024-10-12 16:23:30