hihocoder1070、1077线段树更新点查询区间

题目链接:http://hihocoder.com/problemset/problem/1070

    http://hihocoder.com/problemset/problem/1077

我的代码:

 1 #include <cstdio>
 2 #include <algorithm>
 3 
 4 using namespace std;
 5 
 6 #define MAXN 1000005
 7 
 8 int w[MAXN];
 9 
10 struct segNode
11 {
12     int left, right, minw;
13 };
14 
15 struct segTree
16 {
17     segNode t[4*MAXN];
18     void build(int i, int l, int r)
19     {
20         t[i].left = l;
21         t[i].right = r;
22         if(l<r)
23         {
24             int m = (l+r)/2;
25             build(2*i, l, m);
26             build(2*i+1, m+1, r);
27             t[i].minw = min(t[2*i].minw, t[2*i+1].minw);
28         }
29         else t[i].minw = w[l];
30     }
31     int query(int i, int l, int r)
32     {
33         if(t[i].left==l&&t[i].right==r) return t[i].minw;
34         int m = (t[i].left+t[i].right)/2;
35         if(l>m) return query(2*i+1, l, r);
36         if(r<=m) return query(2*i, l, r);
37         return min(query(2*i, l, m), query(2*i+1, m+1, r));
38     }
39     void update(int i, int id, int v)
40     {
41         if(t[i].left==t[i].right) t[i].minw = v;
42         else
43         {
44             int m = (t[i].left+t[i].right)/2;
45             if(id<=m) update(2*i, id, v);
46             else update(2*i+1, id, v);
47             t[i].minw = min(t[2*i].minw, t[2*i+1].minw);
48         }
49     }
50 }tree;
51 
52 int main()
53 {
54     int n, q;
55     while(scanf("%d", &n)!=EOF)
56     {
57         for(int i=1; i<=n; ++i) scanf("%d", &w[i]);
58         tree.build(1, 1, n);
59         scanf("%d", &q);
60         while(q--)
61         {
62             int op, a, b;
63             scanf("%d%d%d", &op, &a, &b);
64             if(op)  tree.update(1, a, b);
65             else    printf("%d\n", tree.query(1, a, b));
66         }
67     }
68     return 0;
69 }

 

posted @ 2015-02-22 11:51  __brthls  阅读(190)  评论(0编辑  收藏  举报