• POJ2104 K-th Number —— 区间第k小 整体二分


    题目链接:https://vjudge.net/problem/POJ-2104

    K-th Number
    Time Limit: 20000MS   Memory Limit: 65536K
    Total Submissions: 64110   Accepted: 22556
    Case Time Limit: 2000MS

    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

    题解:

    查询区间第k小(不带修改),整体二分。

    代码如下:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <vector>
     6 #include <cmath>
     7 #include <queue>
     8 #include <stack>
     9 #include <map>
    10 #include <string>
    11 #include <set>
    12 using namespace std;
    13 typedef long long LL;
    14 const int INF = 2e9;
    15 const LL LNF = 9e18;
    16 const int MOD = 1e9+7;
    17 const int MAXN = 2e5+100;
    18 
    19 struct node
    20 {
    21     int x, y, k, type, id;
    22 };
    23 node q[MAXN];
    24 
    25 int n, m, c[MAXN];
    26 int lowbit(int x) {return x&(-x);}
    27 void add(int x, int val) {for(int i=x;i<=n;i+=lowbit(i)) c[i]+=val;}
    28 int sum(int x) {int ret=0; for(int i=x;i>0;i-=lowbit(i))ret+=c[i]; return ret;}
    29 
    30 int ans[MAXN];
    31 node q1[MAXN], q2[MAXN];    //两个桶
    32 void solve(int l, int r, int ql, int qr)    //二分答案
    33 {
    34     if(ql>qr) return;   //!!
    35     if(l==r)        //当l==r时,即答案已明确
    36     {
    37         for(int i = ql; i<=qr; i++)
    38             if(q[i].type==1) ans[q[i].id] = l;
    39         return;
    40     }
    41 
    42     int mid = (l+r)>>1;     //写成 (l+r)/2会runtime error,不知为何
    43     int t1 = 0, t2 = 0;
    44     for(int i = ql; i<=qr; i++)     //枚举操作
    45     {
    46         if(q[i].type==0)
    47         {
    48             if(q[i].y<=mid)
    49             {
    50                 add(q[i].x, 1);
    51                 q1[++t1] = q[i];
    52             }else q2[++t2] = q[i];
    53         }
    54         else
    55         {
    56             int pre = sum(q[i].y)-sum(q[i].x-1);
    57             if(pre>=q[i].k) q1[++t1] = q[i];
    58             else
    59             {
    60                 q[i].k -= pre;
    61                 q2[++t2] = q[i];
    62             }
    63         }
    64     }
    65     for(int i = 1; i<=t1; i++)  //撤回对线段树的操作
    66         if(q1[i].type==0) add(q1[i].x, -1);
    67 
    68     for(int i = 1; i<=t1; i++) q[ql+i-1] = q1[i];
    69     for(int i = 1; i<=t2; i++) q[ql+t1+i-1] = q2[i];
    70     solve(l, mid, ql, ql+t1-1);
    71     solve(mid+1, r, ql+t1, qr);
    72 }
    73 
    74 int main()
    75 {
    76     while(scanf("%d%d",&n,&m)==2)
    77     {
    78         int tot = 0;
    79         for(int i = 1; i<=n; i++)
    80         {
    81             ++tot;
    82             scanf("%d", &q[tot].y);
    83             q[tot].x = i; q[tot].type = 0;
    84         }
    85         for(int i = 1; i<=m; i++)
    86         {
    87             ++tot;
    88             scanf("%d%d%d", &q[tot].x,&q[tot].y,&q[tot].k);
    89             q[tot].id = i; q[tot].type = 1;
    90         }
    91 
    92         memset(c, 0, sizeof(c));
    93         solve(-MOD,MOD, 1,tot);
    94         for(int i = 1; i<=m; i++)
    95             printf("%d
    ", ans[i]);
    96     }
    97 }
    View Code
  • 相关阅读:
    ORACLE中dba,user,v$等开头的常用表和视图
    rman restore spfile from backup
    oracle11g设置归档模式和非归档模式
    Oracle数据文件改名
    Linux下rz/sz安装及使用方法
    oracle数据库rman备份计划及恢复
    高级数据查询SQL语法
    关系数据库SQL之基本数据查询:子查询、分组查询、模糊查询
    关系数据库常用SQL语句语法大全
    微服务化的基石——持续集成【转】
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/8647420.html
Copyright © 2020-2023  润新知