zoukankan      html  css  js  c++  java
  • 【POJ2104】【整体二分+树状数组】区间第k大

    Description

    You are working for Macrohard company in data structures department. After failing your previous task about key insertion you were asked to write a new data structure that would be able to return quickly k-th order statistics in the array segment.
    That is, given an array a[1...n] of different integer numbers, your program must answer a series of questions Q(i, j, k) in the form: "What would be the k-th number in a[i...j] segment, if this segment was sorted?"
    For example, consider the array a = (1, 5, 2, 6, 3, 7, 4). Let the question be Q(2, 5, 3). The segment a[2...5] is (5, 2, 6, 3). If we sort this segment, we get (2, 3, 5, 6), the third number is 5, and therefore the answer to the question is 5.

    Input

    The first line of the input file contains n --- the size of the array, and m --- the number of questions to answer (1 <= n <= 100 000, 1 <= m <= 5 000).
    The second line contains n different integer numbers not exceeding 109 by their absolute values --- the array for which the answers should be given.
    The following m lines contain question descriptions, each description consists of three numbers: i, j, and k (1 <= i <= j <= n, 1 <= k <= j - i + 1) and represents the question Q(i, j, k).

    Output

    For each question output the answer to it --- the k-th number in sorted a[i...j] segment.

    Sample Input

    7 3
    1 5 2 6 3 7 4
    2 5 3
    4 4 1
    1 7 3

    Sample Output

    5
    6
    3

    Hint

    This problem has huge input,so please use c-style input(scanf,printf),or you may got time limit exceed.

    Source

    Northeastern Europe 2004, Northern Subregion
    【分析】
    比上一道题目还简单..
      1 /*
      2 宋代郑思肖
      3 《画菊》
      4 花开不并百花丛,独立疏篱趣未穷。
      5 宁可枝头抱香死,何曾吹落北风中。
      6 */
      7 #include <iostream>
      8 #include <cstdio>
      9 #include <algorithm>
     10 #include <cstring>
     11 #include <vector>
     12 #include <utility>
     13 #include <iomanip>
     14 #include <string>
     15 #include <cmath>
     16 #include <queue>
     17 #include <assert.h>
     18 #include <map>
     19 #include <ctime>
     20 #include <cstdlib>
     21 #include <stack>
     22 #define LOCAL
     23 const int MAXN = 100000 + 10;
     24 const int MAXM = 5000 + 10;
     25 const int INF = 0x7fffffff;
     26 const int SIZE = 450;
     27 const int maxnode =  250005 + 10;
     28 using namespace std;
     29 typedef long long ll;
     30 using namespace std;
     31 struct DATA{
     32        int val, x;//x代表位置 
     33        bool operator < (const DATA &b)const{
     34             return val < b.val;
     35        }
     36 }data[MAXN]; 
     37 struct QUESTION{
     38        int l, r;
     39        int k;
     40 }q[MAXM];
     41 int c[MAXN], id[MAXN], Ans[MAXN];
     42 int tmp[MAXN];
     43 bool mark[MAXN];
     44 int Max = -INF, Min = INF, pos, n, m;
     45 //树状数组 
     46 inline int lowbit(int x){return x&-x;}
     47 int sum(int x){
     48     int tmp = 0;
     49     while (x > 0){
     50           tmp += c[x];
     51           x -= lowbit(x);
     52     }
     53     return tmp;
     54 }
     55 void add(int x, int val){
     56      while (x <= n){
     57            c[x] += val;
     58            x += lowbit(x);
     59      }
     60      return;
     61 }
     62 
     63 void init(){
     64      memset(mark, 0, sizeof(mark));
     65      memset(c, 0, sizeof(c)); 
     66      scanf("%d%d", &n, &m);
     67      for (int i = 1; i <= n; i++){
     68          scanf("%d", &data[i].val);
     69          data[i].x = i;
     70          Max = max(Max, data[i].val);
     71          Min = min(Min, data[i].val);
     72      }
     73      //排序 
     74      sort(data + 1, data + 1 + n);
     75 }
     76 void solve(int l, int r, int L, int R){
     77       if (l > r || L == R) return;
     78       int mid = (L + R) >> 1;
     79       while (data[pos + 1].val <= mid && pos < n){
     80             add(data[pos + 1].x, 1);
     81             pos++;
     82       } 
     83       while (data[pos].val > mid){
     84             add(data[pos].x, -1);
     85             pos--;
     86       }
     87       int cnt = 0;//记录找到答案的回答个数 
     88       for (int i = l; i <= r; i++){
     89           if (sum(q[id[i]].r) - sum(q[id[i]].l - 1) > q[id[i]].k - 1){
     90              Ans[id[i]] = mid;
     91              mark[i] = 1;
     92              cnt++;
     93           }else mark[i] = 0;
     94       } 
     95       int l1 = l, l2 = l + cnt;
     96       for (int i = l; i <= r; i++)
     97       if (mark[i]) tmp[l1++] = id[i];
     98       else tmp[l2++] = id[i];
     99       
    100       for (int i = l; i <= r; i++) id[i] = tmp[i];
    101       solve(l, l1 - 1, L, mid);
    102       solve(l1, l2 - 1, mid + 1, R);
    103 }
    104 void work(){
    105      pos = 0;//初始化即data中的下标 
    106      for (int i = 1; i <= m; i++){
    107          scanf("%d%d%d", &q[i].l, &q[i].r, &q[i].k);
    108      }
    109      for (int i = 1; i <= m; i++) id[i] = i;
    110      solve(1, m, Min, Max + 1);
    111      for (int i = 1; i <= m; i++) printf("%d
    ", Ans[i]);
    112 }
    113 
    114 int main(){
    115    
    116     init();
    117     work(); 
    118     return 0;
    119 }
    View Code
  • 相关阅读:
    2019省赛训练组队赛4.9周二 2017浙江省赛
    #Leetcode# 49. Group Anagrams
    #Leetcode# 57. Insert Interval
    POJ 2195 Going Home
    HDU 2255 奔小康赚大钱
    HDU 1083 Courses
    HDU 2063 过山车
    POJ 3041 Asteroids
    指针的妙处
    jzoj 6273. 2019.8.4【NOIP提高组A】欠钱 (money)
  • 原文地址:https://www.cnblogs.com/hoskey/p/4338312.html
Copyright © 2011-2022 走看看