标签:nbsp namespace ems stream -- turn 线段树 oid else
#include <iostream> #include<string.h> #include<algorithm> using namespace std; const int maxn=100000; int a[maxn]; struct segtree { int mi,ma; }segt[maxn<<2]; void build(int root,int beg,int en) { if(beg==en) { segt[root].mi=a[beg]; segt[root].ma=a[beg]; return ; } else { int mid=(beg+en)/2; build(root*2,beg,mid); build(root*2+1,mid+1,en); } segt[root].mi=min(segt[root*2].mi,segt[root*2+1].mi); segt[root].ma=max(segt[root*2].ma,segt[root*2+1].ma); //改这里 } int query1(int root,int rst,int ren,int beg,int en) { if(rst>en||ren<beg) return -1; if(rst==beg&&ren==en) return segt[root].mi; int mid=(rst+ren)/2; if(en<=mid) return query1(root*2,rst,mid,beg,en); else if(beg>mid) return query1(root*2+1,mid+1,ren,beg,en); else return min(query1(root*2,rst,mid,beg,mid),query1(root*2+1,mid+1,ren,mid+1,en)); //改这里 } int query2(int root,int rst,int ren,int beg,int en) { if(rst>en||ren<beg) return -1; if(rst==beg&&ren==en) return segt[root].ma; int mid=(rst+ren)/2; if(en<=mid) return query2(root*2,rst,mid,beg,en); else if(beg>mid) return query2(root*2+1,mid+1,ren,beg,en); else return max(query2(root*2,rst,mid,beg,mid),query2(root*2+1,mid+1,ren,mid+1,en)); //改这里 } int main() { memset(segt,0,sizeof(segt)); int n,k; cin>>n>>k; k--; for(int i=1;i<=n;i++) { cin>>a[i]; } build(1,1,n); for(int i=1;i<=n-k;i++) { printf("%d ",query1(1,1,n,i,i+k)); } printf("\n"); for(int i=1;i<=n-k;i++) { printf("%d ",query2(1,1,n,i,i+k)); } printf("\n"); return 0; }
标签:nbsp namespace ems stream -- turn 线段树 oid else
原文地址:https://www.cnblogs.com/AAAzhuo/p/11269246.html