线段树入门三道题

xiaoxiao2021-02-28  68

//POJ 3264 #include <iostream> #include <cstdio> #include <cmath> using namespace std; #define FOR( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define Mid ((l+r)>>1) //括号! #define lson rt<<1,l,Mid #define rson rt<<1|1,Mid+1,r const int maxn= 50005; const int inf=0x3f3f3f3f; int Max[maxn<<2],Min[maxn<<2]; int ans1,ans2; void build(int rt,int l,int r){ if(l==r){ scanf("%d",&Max[rt]); Min[rt] = Max[rt]; }else{ build(lson); build(rson); Max[rt] = max( Max[rt<<1], Max[rt<<1|1]); Min[rt] = min( Min[rt<<1], Min[rt<<1|1]); } } void query(int rt,int l,int r,int L,int R){ if(L <= l && r <= R){ ans1 = max(ans1,Max[rt]); ans2 = min(ans2,Min[rt]); }else{ if( L <= Mid) query(lson,L,R); if( R > Mid) query(rson,L,R); } } int main(){ int n,m,L,R; while(scanf("%d%d",&n,&m)!=EOF){ build(1,1,n); while(m--){ ans1 = -inf,ans2 = inf; scanf("%d%d",&L,&R); query(1,1,n,L,R); printf("%d\n", ans1-ans2); } } return 0; } //HDU1754 #include <iostream> #include <cstdio> #include <cmath> using namespace std; #define FOR( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define Mid ((l+r)>>1) #define lson rt<<1,l,Mid #define rson rt<<1|1,Mid+1,r const int maxn= 200000+5; const int inf=0x3f3f3f3f; int Max[maxn<<2]; int ans1,ans2; void build(int rt,int l,int r){ if(l==r) scanf("%d",&Max[rt]); else{ build(lson); build(rson); Max[rt] = max( Max[rt<<1], Max[rt<<1|1]); } } void update(int rt,int l,int r,int pos,int num){ if(l == r && r == pos) Max[rt] = num; else{ if( pos <= Mid) update(lson,pos,num); if( pos > Mid) update(rson,pos,num); Max[rt] = max( Max[rt<<1], Max[rt<<1|1]); } } int query(int rt,int l,int r,int L,int R){ if(L <= l && r <= R) return Max[rt]; else{ int tmp = -1; if( L <= Mid) tmp = max(tmp,query(lson,L,R)); if( R > Mid) tmp = max(tmp,query(rson,L,R)); return tmp; } } int main(){ int n,m,L,R; char op; while(scanf("%d%d",&n,&m)!=EOF){ build(1,1,n); getchar(); while(m--){ scanf("%c%d%d%*c",&op,&L,&R); if(op=='Q') printf("%d\n",query(1,1,n,L,R)); else update(1,1,n,L,R); } } return 0; } //HDU1166 #include <iostream> #include <cstdio> #include <cmath> using namespace std; #define FOR( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define Mid ((l+r)>>1) #define lson rt<<1,l,Mid #define rson rt<<1|1,Mid+1,r const int maxn= 50000+5; const int inf=0x3f3f3f3f; int sum[maxn<<2]; int ans1,ans2; void build(int rt,int l,int r){ if(l==r) scanf("%d",&sum[rt]); else{ build(lson); build(rson); sum[rt] = sum[rt<<1] + sum[rt<<1|1]; } } void update(int rt,int l,int r,int pos,int num){ if(l == r && r == pos) sum[rt] += num; else{ if( pos <= Mid) update(lson,pos,num); else update(rson,pos,num); sum[rt] = sum[rt<<1] + sum[rt<<1|1]; } } int query(int rt,int l,int r,int L,int R){ if(L <= l && r <= R) return sum[rt]; else{ int tmp = 0; if( L <= Mid) tmp += query(lson,L,R); if( R > Mid) tmp += query(rson,L,R); return tmp; } } int main(){ int n,m,L,R,t; char op[10]; scanf("%d",&t); for(int cas=1;cas<=t;cas++){ scanf("%d",&n); build(1,1,n); printf("Case %d:\n",cas); while(scanf("%s",op),op[0]!='E'){ scanf("%d%d",&L,&R); if(op[0]=='Q') printf("%d\n", query(1,1,n,L,R)); else if(op[0]=='A') update(1,1,n,L,R); else update(1,1,n,L,-R); } } return 0; }
转载请注明原文地址: https://www.6miu.com/read-50858.html

最新回复(0)