zoukankan      html  css  js  c++  java
  • BZOJ2223 [Coci 2009]PATULJCI

    求区间内个数大于rank的一个数

    主席树求一下就好啦!

     1 /**************************************************************
     2     Problem: 2223
     3     User: rausen
     4     Language: C++
     5     Result: Accepted
     6     Time:704 ms
     7     Memory:54712 kb
     8 ****************************************************************/
     9  
    10 #include <cstdio>
    11 #include <algorithm>
    12  
    13 using namespace std;
    14 const int N = 3e5 + 5;
    15 const int Cnt_chair = 4500000;
    16  
    17 struct chair_node {
    18     chair_node *ls, *rs;
    19     int cnt;
    20 } *root[N], mempool[Cnt_chair], *cnt_chair = mempool, *null;
    21  
    22 int n, mx, ans;
    23  
    24 inline int read() {
    25     int x = 0;
    26     char ch = getchar();
    27     while (ch < '0' || '9' < ch)
    28         ch = getchar();
    29     while ('0' <= ch && ch <= '9') {
    30         x = x * 10 + ch - '0';
    31         ch = getchar();
    32     }
    33     return x;
    34 }
    35  
    36 #define mid (l + r >> 1)
    37 void chair_insert(chair_node *p, chair_node *&q, int l, int r, int x) {
    38     (q = ++cnt_chair) -> cnt = p -> cnt + 1;
    39     if (l == r) return;
    40     if (x <= mid) 
    41         q -> rs = p -> rs, chair_insert(p -> ls, q -> ls, l, mid, x);
    42     else
    43         q -> ls = p -> ls, chair_insert(p -> rs, q -> rs, mid + 1, r, x);
    44 }
    45  
    46 int chair_query(chair_node *p, chair_node *q, int l, int r, int rank) {
    47     if (l == r) return l;
    48     if (q -> ls -> cnt - p -> ls -> cnt > rank)
    49         return chair_query(p -> ls, q -> ls, l, mid, rank);
    50     if (q -> rs -> cnt - p -> rs -> cnt > rank)
    51         return chair_query(p -> rs, q -> rs, mid + 1, r, rank);
    52     return 0;
    53 }
    54 #undef mid
    55  
    56 int main() {
    57     int Q, i, l, r;
    58     n = read(), mx = read();
    59     null = cnt_chair;
    60     null -> cnt = 0, null -> ls = null -> rs = null;
    61     root[0] = null;
    62     for (i = 1; i <= n; ++i)
    63         chair_insert(root[i - 1], root[i], 1, mx, read());
    64     Q = read();
    65     while (Q--) {
    66         l = read(), r = read();
    67         if (l > r) swap(l, r);
    68         ans = chair_query(root[l - 1], root[r], 1, mx, (r - l + 1) >> 1);
    69         if (ans == 0) printf("no
    ");
    70         else printf("yes %d
    ", ans);
    71     }
    72     return 0;
    73 }
    View Code

    (p.s. 数组开小了连WA两发。。。555)

    By Xs酱~ 转载请说明 博客地址:http://www.cnblogs.com/rausen
  • 相关阅读:
    迷宫寻找路径数
    136. 只出现一次的数字
    48. 旋转图像
    283. 移动零
    面试题 01.06. 字符串压缩
    位运算符
    367. 有效的完全平方数
    868. 二进制间距
    SpringAOP表达式
    Mybatis常见错误及纠错
  • 原文地址:https://www.cnblogs.com/rausen/p/4296633.html
Copyright © 2011-2022 走看看