[模板] 线段树
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int amn=1e5+5; 5 int num[amn]; 6 struct Segment_Tree{ 7 ll l,r,sum,lazy,len; 8 #define ls rt<<1 9 #define rs rt<<1|1 10 #define trl(rt) tr[rt].l 11 #define trr(rt) tr[rt].r 12 #define trsum(rt) tr[rt].sum 13 #define trlz(rt) tr[rt].lz 14 #define trlen(rt) tr[rt].len 15 #define Mid int mid=(l+r)>>1 16 }tr[amn]; 17 void push_up(int rt){ 18 trsum(rt)=trsum(ls)+trsum(rs); 19 } 20 void bulid(int rt,int l,int r){ 21 trl(rt)=l,trr(rt)=r; 22 trlz(rt)=0; 23 trlen(rt)=r-l+1; 24 if(l==r){ 25 trsum(rt)=num[l]; 26 } 27 Mid; 28 build(ls,l,mid); 29 build(rs,mid+1,r); 30 push_up(rt); 31 } 32 void push_down(int rt,int l,int r){ 33 if(trlz(rt)){ 34 Mid; 35 trsum(ls)=trlz(rt)*(mid-l+1); 36 trsum(rs)=trlz(rt)*(r-mid); 37 trlz(ls)=trlz(rs)=trlz(rt); 38 trlz(rt)=0; 39 } 40 return ; 41 } 42 ll query(int rt,int l,int r,int L,int R){ 43 if(L<=l&&r<=R)return trsum(rt); 44 Mid; 45 push_down(rt); 46 if(L<=mid)query(ls,l,mid,L,R); 47 if(mid+1<=R)query(rs,mid+1,L,R); 48 } 49 void updata(int rt,int l,int r,int L,int R,int val){ 50 if(L<=l&&r<=R){ 51 trlz(rt)=val; 52 trsum(rt)=trlz(rt)*trlen(rt); 53 return ; 54 } 55 Mid; 56 push_down(rt,l,r); 57 if(L<=mid)updata(ls,l,mid,L,R,val); 58 if(mid+1<=R)updata(rs,mid+1,r,L,R,val); 59 push_up(rt); 60 } 61 int main(){ 62 63 }