zoukankan      html  css  js  c++  java
  • 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
    ", tree.query(1, a, b));
    66         }
    67     }
    68     return 0;
    69 }
  • 相关阅读:
    Reverse Bits
    Number of 1 Bits
    Single Number,Single Number II
    Repeated DNA Sequences
    Fraction to Recurring Decimal
    Isomorphic Strings
    Valid Sudoku
    Count Primes
    Bulls and Cows
    高性能Web服务器Nginx的配置与部署研究(12)应用模块之Memcached做文件缓存时压缩引起的问题
  • 原文地址:https://www.cnblogs.com/pczhou/p/4297382.html
Copyright © 2011-2022 走看看