全输出0求条
查看原帖
全输出0求条
1208546
AnotherDream楼主2025/1/12 15:52
// Problem: C - 线段树 1
// Contest: Virtual Judge - 第2章:进阶数据结构2(线段树、树状数组)
// URL: https://vjudge.net/contest/629231#problem/C
// Memory Limit: 125 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
using namespace std;
#define debug cerr<<"The code runs successfully.\n";
#define endl '\n'
#define TRACE 1
#define tcout TRACE && cout
#define fst ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define int long long
#define mid ((l+r)>>1)
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
const int P = 998244353; 
const int Base = 3221225477;
const int INF = 0x3f3f3f3f3f3f3f3f; 
const int N = 1e5 + 10, M = 2e6 + 10;
int a[N],s[N<<2],t[N<<2],n,q;
inline void calc(int p,int l,int r,int x) {
	s[p]+=(r-l+1)*x,t[p]+=x;
}
inline void pushup(int x) {
	s[x]=s[ls(x)]+s[rs(x)];
}
inline void pushdown(int p,int l,int r) {
	if(!t[p]) return;
	calc(ls(p),l,mid,t[p]);
	calc(rs(p),mid+1,r,t[p]);
	t[p]=0;
}
inline void build(int p,int l,int r) {
	if(l==r) {
		s[p]=a[l];
		return;
	}
	build(ls(p),l,mid);
	build(rs(p),mid+1,r);
	pushup(p);
}
//单点
/*
inline void add(int p,int l,int r,int x,int y) {
	if(l==r) {
		s[p]+=y;
		return ;
	}
	x<=mid?add(ls(p),l,mid,x,y):add(rs(p),mid+1,r,x,y);
	pushup(p);
}
*/
//区间
void add(int p,int l,int r,int L,int R,int x) {
	if(r<L||R>l) return;
	if(L<=l&&r<=R) calc(p,l,r,x);
	pushdown(p,l,r);
	add(ls(p),l,mid,L,R,x);
	add(rs(p),mid+1,r,L,R,x);
	pushup(p);
}
inline int ask(int p,int l,int r,int L,int R) {
	if(r<L||R>l) return 0;
	if(L<=l&&r<=R) return s[p];
	pushdown(p,l,r);
	return ask(ls(p),l,mid,L,R)+ask(rs(p),mid+1,r,L,R);
}
signed main() {
	fst;
	cin>>n>>q;
	for(int i=1;i<=n;i++) {
		cin>>a[i];
	}
	build(1,1,n);
	while(q--) {
		int op;
		cin>>op;
		if(op==1) {
			int l,r,x;
			cin>>l>>r>>x;
			add(1,1,n,l,r,x);
		}
		else {
			int l,r;
			cin>>l>>r;
			cout<<ask(1,1,n,l,r)<<endl;
		}
	}
	return 0;
}

2025/1/12 15:52
加载中...