线段树

P3372 【模板】线段树 1

思路
区间修改+区间查询,考虑使用懒标记取记录区间和。
代码

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define lc u<<1
#define rc u<<1|1
const int N = 1e5+5;
int a[N];
struct tree{
    int l, r;
    int sum, tag;
}tr[N << 2];
void pushup(int u){
    tr[u].sum = tr[lc].sum + tr[rc].sum; 
}
void pushdown(int u){
    if(tr[u].tag){
        tr[lc].sum+=(tr[lc].r-tr[lc].l+1)*tr[u].tag;
        tr[rc].sum+=(tr[rc].r-tr[rc].l+1)*tr[u].tag;
        tr[lc].tag+=tr[u].tag;
        tr[rc].tag+=tr[u].tag;
        tr[u].tag=0;
    }
}
void build(int u,int l,int r){
    tr[u]={l,r,a[l],0};
    if(l==r) return;
    int m=l+r>>1;
    build(lc,l,m);
    build(rc,m+1,r);
    pushup(u);
}
void update(int u,int x,int y,int k){
    if(x>tr[u].r || y<tr[u].l) return;
    if(x<=tr[u].l && tr[u].r<=y){
        tr[u].sum+=(tr[u].r-tr[u].l+1)*k;
        tr[u].tag+=k;
        return;
    }
    pushdown(u);
    update(lc,x,y,k);
    update(rc,x,y,k);
    pushup(u);
}
int query(int u,int x,int y){
    if(x>tr[u].r || y<tr[u].l) return 0;
    if(x<=tr[u].l && tr[u].r<=y) return tr[u].sum;
    pushdown(u);
    return query(lc,x,y)+query(rc,x,y);
}

signed main(){
//	freopen(".in", "r", stdin);
//	freopen(".out", "w", stdout);
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int n, m;
	cin >> n >> m;
	for(int i=1; i<=n; ++i){
		cin >> a[i];
	}
	build(1,1,n);
	while(m--){
		int ops, x, y;
		int k;
		cin >> ops >> x >> y;
		if(ops==1){
			cin >> k;
			update(1,x,y,k);
		}
		else{
			cout << query(1,x,y) << '\n';
		}
	}
	return 0;
}

P3870 [TJOI2009] 开关

思路
区间查询,区间修改。考虑用懒标记去记录区间有多少个还开着的灯,每次修改的时候只需要用区间开着灯的数量用区间的长度去减即可。
代码

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define lc u<<1
#define rc u<<1|1
const int N = 1e5+5;
struct node{
	int l, r;
	int open;
	bool tag;
}tr[N<<2];
void pushup(int u){
	tr[u].open = tr[lc].open + tr[rc].open;
}
void pushdown(int u){
	if(tr[u].tag){
		tr[lc].open = (tr[lc].r - tr[lc].l + 1) - tr[lc].open;
		tr[rc].open = (tr[rc].r - tr[rc].l + 1) - tr[rc].open;
		tr[lc].tag ^= 1;
		tr[rc].tag ^= 1;
		tr[u].tag = 0;
	}
}
void build(int u, int l, int r){
	tr[u].l = l, tr[u].r = r, tr[u].open = 0;
	if(l==r)return;
	int mid = l + r >> 1;
	build(lc,l,mid);
	build(rc,mid+1,r);
	pushup(mid);
}
void update(int u, int l, int r){
	if(tr[u].r < l || tr[u]. l > r)return;
	if(l <= tr[u].l && tr[u].r <= r){
		tr[u].open = (tr[u].r - tr[u].l + 1) - tr[u].open;
		tr[u].tag ^=1;
		return;
	}
	pushdown(u);
	update(lc, l, r);
	update(rc, l, r);
	pushup(u);
}
ll query(int u, int l, int r){
	if(tr[u].r < l || tr[u].l > r)return 0;
	if(l <= tr[u].l && tr[u].r <= r)return tr[u].open;
	pushdown(u);
	return query(lc,l,r) + query(rc,l,r);
}
int main(){
//	freopen(".in", "r", stdin);
//	freopen(".out", "w", stdout);
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int n, m;
	cin >> n >> m;
	build(1,1,n);
	while(m--){
		int c, a, b;
		cin >> c >> a >> b;
		if(c==0){
			update(1,a,b);
		}
		else{
			cout << query(1,a,b) << '\n';
		}
	}
	return 0;
}
posted @ 2026-09-21 21:44  williamYcY  阅读(8)  评论(0)    收藏  举报
/* */ /* */ /* */ /* */ /* */ /* */