1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
   | #include<iostream> #include<cstring> #include<cstdio> #include<cmath> #include<algorithm> #include<malloc.h>  #define ll long long #define INF 0x7fffffff #define re register
  using namespace std;
  int read() {     register int x = 0,f = 1;register char ch;     ch = getchar();     while(ch > '9' || ch < '0'){if(ch == '-') f = -f;ch = getchar();}     while(ch <= '9' && ch >= '0'){x = x * 10 + ch - 48;ch = getchar();}     return x * f; }
  struct tree{     int maxx,minn;     tree *lson,*rson; }*root = (tree*)malloc(sizeof(tree));
  struct node{     int a,b; }ans[1000005];
  void build(tree *tre,int l,int r) {     if(l == r)     {         tre -> maxx = read();         tre -> minn = tre -> maxx;         return;     }     int mid = (l + r) >> 1;     tree *son1 = (tree*)malloc(sizeof(tree));     tree *son2 = (tree*)malloc(sizeof(tree));     tre -> lson = son1;     tre -> rson = son2;     build(tre -> lson,l,mid);     build(tre -> rson,mid + 1,r);     tre -> maxx = max(tre -> lson -> maxx,tre -> rson -> maxx);     tre -> minn = min(tre -> lson -> minn,tre -> rson -> minn); }
  node query(tree *tre,int l,int r,int x,int y) {     if(l == r) return (node){tre -> maxx,tre -> minn};     int mid = (l + r) >> 1;     node t1,t2;     t1.a = -INF; t1.b = INF; t2.a = -INF; t2.b = INF;     if(x <= mid) t1 = query(tre -> lson,l,mid,x,y);     if(y > mid) t2 = query(tre -> rson,mid + 1,r,x,y);     return (node){max(t1.a,t2.a),min(t1.b,t2.b)}; }
  int main() {     int n,k;     n = read(); k = read();     build(root,1,n);     for(int i = 1; i <= n - k + 1; i++) ans[i] = query(root,1,n,i,i + k - 1);     for(int i = 1; i <= n - k + 1; i++) printf("%d ",ans[i].b); printf("\n");     for(int i = 1; i <= n - k + 1; i++) printf("%d ",ans[i].a); printf("\n");     return 0; }
   |