线段树做法

hezhiqian 魔镜少女 2025-06-12 20:00:57 2025-06-12 20:01:18 10
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define lc p<<1
#define rc p<<1|1
struct tree{
	int l,r;
	int sum,add;
}t[6];
int a[3];
void inline pushup(int p){
	t[p].sum=t[lc].sum+t[rc].sum;
}
void inline build(int p,int l,int r){
	t[p]={l,r,0,0};
	if(l==r){
		t[p].sum=a[l];
		return;
	}
	int mid=l+r>>1;
	build(lc,l,mid);
	build(rc,mid+1,r);
	pushup(p);
}
void inline pushdown(int p){
	t[lc].sum+=(t[lc].r-t[lc].l+1)*t[p].add;
	t[rc].sum+=(t[rc].r-t[rc].l+1)*t[p].add;
	t[lc].add+=t[p].add;
	t[rc].add+=t[p].add;
	t[p].add=0;
}
int inline query(int p,int x,int y){
	if(x<=t[p].l&&t[p].r<=y) return t[p].sum;
	pushdown(p);
	int mid=t[p].l+t[p].r>>1;
	int res=0;
	if(x<=mid) res+=query(lc,x,y);
	if(mid<y) res+=query(rc,x,y);
	return res;
}
void inline doing(){
	cin>>a[1]>>a[2];
	build(1,1,2);
	cout<<query(1,1,2);
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	doing();
	return 0;
}
{{ vote && vote.total.up }}

共 2 条回复

wc02222

@hezhiqian

会种代码都可以去拿CSP-j一等奖了

CPP 刷题王

虽然是个人都不会抄你代码但是还是不要把 AC 代码放讨论区。