zoukankan      html  css  js  c++  java
  • zoj 2112 Dynamic Rankings(k大数水过)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1112

      用第k大数8s水过的题....如果计算复杂度,理论上是不应该可以通过的...这题正解是树套树,用线段树嵌套平衡树。(树套树版本已写,详见另一篇随笔)

    记录一下水过去的代码:

    View Code
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <cmath>
     4 #include <algorithm>
     5 #include <cstdlib>
     6 
     7 #define lson l, m, rt << 1
     8 #define rson m + 1, r, rt << 1 | 1
     9 #define root 0, mx - 1, 1
    10 
    11 using namespace std;
    12 
    13 #define debug 0
    14 
    15 int min2(int _a, int _b){return _a < _b ? _a : _b;}
    16 int max2(int _a, int _b){return _a > _b ? _a : _b;}
    17 
    18 const int maxn = 50001;
    19 int tmp[maxn], s[maxn];
    20 
    21 int k_elem(int n, int *a, int k){//二分搜索第k大数
    22     int t, key;
    23     int i, j, l = 0, r = n - 1;
    24     while (l < r){
    25         for (key = a[((i = l - 1) + (j = r + 1)) >> 1]; i < j; ){
    26             for (j--; key < a[j]; j--);
    27             for (i++; a[i] < key; i++);
    28             if (i < j) t = a[i], a[i] = a[j], a[j] = t;
    29         }
    30         if (k > j) l = j + 1;
    31         else r = j;
    32     }
    33     return a[k];
    34 }
    35 
    36 int main(){
    37     int T, n, m;
    38     int l, r, k;
    39     char in[3];
    40 
    41     scanf("%d", &T);
    42     while (T--){
    43         scanf("%d%d", &n, &m);
    44         for (int i = 1; i <= n; i++){
    45             scanf("%d", &s[i]);
    46         }
    47         while (m-- && scanf("%s", in)){
    48             if (in[0] == 'Q'){
    49                 scanf("%d%d%d", &l, &r, &k);
    50                 for (int i = l; i <= r; i++){
    51                     tmp[i - l] = s[i];
    52                 }
    53                 printf("%d\n", k_elem(r - l + 1, tmp, k - 1));
    54             }
    55             else{
    56                 scanf("%d%d", &l, &k);
    57                 s[l] = k;
    58             }
    59         }
    60     }
    61 
    62     return 0;
    63 }

    ——written by Lyon

  • 相关阅读:
    javaoop初级入门继承和封装和接口和多态和抽象类
    java基础的几种 函数
    sql server 2008子查询的学习小纪(一)
    中文价码filter
    $http.jsonp和$scope.watch
    普通版选项卡
    面向对象版选项卡
    angular版选项卡
    微信H5--手机键盘弹起导致页面变形
    controller控制器
  • 原文地址:https://www.cnblogs.com/LyonLys/p/zoj_2112_Lyon_unformal.html
Copyright © 2011-2022 走看看