0,WA+MLE求条

P3372 【模板】线段树 1

fried_chicken @ 2023-11-26 00:12:08

开ll了

#include<bits/stdc++.h>
using namespace std;
#define file(filename) freopen(filename".in","r",stdin),freopen(filename".out","w",stdout)
#define endl "\n"
#define INF 0x3f3f3f3f
#define int long long//开long long了
#define ull unsigned long long
#define mem(a,num) memset(a,num,sizeof(a))
#define keep(n) fixed<<setprecision(n)
#define lowbit(x) x&(-x)
#define pch putchar
#define LF putchar('\n')
#define SP putchar(' ') 
#define lc p>>1
#define rc p<<1|1
const int N=100005;
int w[N];
struct node {
    int l,r,sum,add;//add表示lazy标记
} tr[N*4];
void pushup(int p) { //向上更新
    tr[p].sum=tr[lc].sum+tr[rc].sum;
}
void pushdown(int p) { //向下更新
    if(tr[p].add) {
        tr[lc].sum+=tr[p].add*(tr[lc].r-tr[lc].l+1),tr[rc].sum+=tr[p].add*(tr[rc].r-tr[rc].l+1),tr[lc].add+=tr[p].add,tr[rc].add+=tr[p].add,tr[p].add=0;
    }
}
void build(int p,int l,int r) { //建树
    tr[p]= {l,r,w[l],0};
    if(l==r) return;
    int m=(l+r)>>1;
    build(lc,l,m);
    build(rc,m+1,r);
    pushup(p);
}
void change(int p,int l,int r,int k) { //区间修改
    if(l<=tr[p].l&&tr[p].r<=r) {//覆盖则修改
        tr[p].sum+=(tr[p].r-tr[p].l+1)*k;
        tr[p].add+=k;
        return;
    }int m=(tr[p].l+tr[p].r)>>1;//不覆盖则裂开
    pushdown(p);
    if(l<=m) change(lc,l,r,k);
    if(r>m) change(rc,l,r,k);
    pushup(p);
}
int query(int p,int l,int r) { //区间查询
    if(l<=tr[p].l && tr[p].r<=r) return tr[p].sum;
    int m=(tr[p].l+tr[p].r)>>1;
    pushdown(p);
    int sum=0;
    if(l<=m) sum+=query(lc,l,r);
    if(r>m) sum+=query(rc,l,r);
    return sum;
}
int n,m,sum,ans;
signed main() {
  //file("");
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin>>n>>m;
  for(int i=1;i<=n;i++) cin>>w[i];
  build(1,1,n);
  int op,x,y,k;
  for(int i=1;i<=m;i++){
    cin>>op>>x>>y;
    if(op==1) {cin>>k;
        change(1,x,y,k);
    }else{
        cout<<query(1,x,y)<<endl;
    }
  }
  return 0;
}

by uid_310801 @ 2023-11-26 00:21:43

@fried_chicken #define lc p<<1


by fried_chicken @ 2023-11-26 00:52:40

@Spouter_27已AC,thx


|