hsd_tandongqin @ 2024-10-09 20:39:09
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5 + 5;
int n,m;
int a[N *4];
struct node{
int l,r,val;
}tr[N*4];
void build(int u,int l,int r)
{
tr[u].l = l;
tr[u].r = r;
if(l == r)
{
tr[u].val = a[l];
return;
}
int mid = (l + r) >> 1;
build(u * 2, l, mid);
build(u * 2 + 1, mid + 1, r);
tr[u].val = tr[u*2].val + tr[u*2+1].val;
}
void add(int u,int x,int y,int k)
{
if(tr[u].l == tr[u].r && tr[u].l >= x&&tr[u].r <= y)
{
tr[u].val += k;
return;
}
int mid = (tr[u].l + tr[u].r) >> 1;
if(mid >= x) add(u * 2, x , y , k);
if(mid + 1<=y) add(u * 2 + 1, x ,y,k);
tr[u].val = tr[u*2].val + tr[u*2+1].val;
}
int q_sum(int u,int x,int y)
{
int vall = 0;
if(tr[u].l == tr[u].r&&tr[u].l >= x&&tr[u].r<=y)
{
return tr[u].val;
}
int mid = (tr[u].l + tr[u].r) >> 1;
if(mid >= x) vall += q_sum(u *2, x , y);
if(mid + 1 <= y) vall +=q_sum(u * 2 + 1, x , y);
return vall;
}
signed main()
{
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin>>n>>m;
for(int i = 1;i <= n;i++) cin>>a[i];
build(1 , 1 , n);
for(int i = 1;i <= m;i++)
{
int op,x,y,k;
cin>>op>>x>>y;
if(op == 1)
{
cin>>k;
add(1 , x, y,k);
}
else if(op == 2)
{
cout<<q_sum(1 , x , y)<<"\n";
}
}
return 0;
}
by Liu123666 @ 2024-10-09 20:52:23
if(tr[u].l == tr[u].r && tr[u].l >= x&&tr[u].r <= y)
布什哥们,你这些的是那派线段树啊,不成暴力了?
by 48WangYanJi @ 2024-10-23 17:37:41
@Liu123666 不优雅的暴力(doge)