poj --3264

又手贱的用cin  结果t掉;

感觉自己的代码风格太差了

 

 http://poj.org/problem?id=3264

 

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<algorithm>
  4 using namespace std;
  5 const int INF = 0xffffff0;
  6 
  7 int minV = INF;
  8 int maxV = -INF;
  9 
 10 typedef struct _NODE_
 11 {
 12 
 13     int L,R;
 14     int minV,maxV;
 15     int Mid()
 16     {
 17         return (L+R)/2;
 18     }
 19 }NODE;
 20 
 21 NODE tree[800000 + 10];
 22 
 23 
 24 void BuildTree(int root,int L,int R)
 25 {
 26 
 27     tree[root].L = L;
 28     tree[root].R = R;
 29     tree[root].minV = INF;
 30     tree[root].maxV = -INF;
 31     if (L!=R)
 32     {
 33 
 34         BuildTree(2*root+1,L,(L+R)/2);
 35         BuildTree(2*root+2,(L+R)/2+1,R);
 36     }
 37 }
 38 
 39 
 40 void Insert(int root,int i,int v)
 41 {
 42 
 43     if (tree[root].L==tree[root].R)
 44     {
 45         tree[root].minV = tree[root].maxV = v;
 46         return ;
 47     }
 48     tree[root].minV = min(tree[root].minV,v);
 49     tree[root].maxV = max(tree[root].maxV,v);
 50     if (i<=tree[root].Mid())
 51     {
 52         Insert(2*root+1,i,v);
 53     }
 54     else
 55     {
 56         Insert(2*root+2,i,v);
 57     }
 58 }
 59 
 60 void Query(int root,int s,int e)
 61 {
 62 
 63     if (tree[root].minV>=minV&&tree[root].maxV<=maxV)
 64     {
 65         return ;
 66     }
 67     if (tree[root].L == s&&tree[root].R == e)
 68     {
 69         minV = min(minV,tree[root].minV);
 70         maxV = max(maxV,tree[root].maxV);
 71         return ;
 72     }
 73     if (e<=tree[root].Mid())
 74     {
 75         Query(2*root+1,s,e);
 76     }
 77     else if (s>tree[root].Mid())
 78     {
 79         Query(2*root+2,s,e);
 80     }
 81     else
 82     {
 83         Query(2*root+1,s,tree[root].Mid());
 84         Query(2*root+2,tree[root].Mid()+1,e);
 85     }
 86 }
 87 
 88 void work(int n ,int q)
 89 {
 90     BuildTree(0,1,n);
 91     for (int i = 1;i<=n;i++)
 92     {
 93         int h;
 94         scanf("%d",&h);
 95         Insert(0,i,h);
 96     }
 97     for (int i = 0;i<q;i++)
 98     {
 99         int s,e;
100         scanf("%d%d",&s,&e);
101         minV = INF;
102         maxV = -INF;
103         Query(0,s,e);
104         cout << maxV-minV << endl;
105     }
106 }
107 
108 int main()
109 {
110 
111     int n,q,h;
112     int i,j,k;
113     scanf("%d%d",&n,&q);
114     work(n,q);
115     return 0;
116 
117 }
代码君

 

posted on 2015-06-10 19:09  yifi  阅读(166)  评论(0编辑  收藏  举报

导航