POJ 2104 K-th Number(主席樹)

来源:http://www.cnblogs.com/zwfymqz/archive/2017/07/08/7137467.html
-Advertisement-
Play Games

K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 57427 Accepted: 19856 Case Time Limit: 2000MS Description You are working for ...


K-th Number
Time Limit: 20000MS   Memory Limit: 65536K
Total Submissions: 57427   Accepted: 19856
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     主席樹的模板題。  
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<queue>
 6 #include<algorithm>
 7 using namespace std;
 8 const int MAXN=10000001;
 9 void read(int &n)
10 {
11     char c='+';int x=0;bool flag=0;
12     while(c<'0'||c>'9')
13     {c=getchar();if(c=='-')flag=1;}
14     while(c>='0'&&c<='9')
15     {x=x*10+c-48,c=getchar();}
16     flag==1?n=-x:n=x;
17 }
18 int n,m;
19 int a[MAXN];
20 int hash[MAXN];
21 int root[MAXN/2];
22 int now,tot;// 所有節點的總出現次數 
23 int ls[MAXN],rs[MAXN],cnt[MAXN];
24 void build(int &cur,int l,int r)
25 {
26     cur=tot++;// 總的節點數量 
27     cnt[cur]=0; 
28     if(l!=r)
29     {
30         int mid=(l+r)/2;
31          build(ls[cur],l,mid);
32          build(rs[cur],mid+1,r);    
33     }
34 }
35 void update(int pre,int pos,int &cur,int l,int r)
36 {
37     cur=tot++;
38     cnt[cur]=cnt[pre]+1;
39     ls[cur]=ls[pre];rs[cur]=rs[pre];
40     if(l==r)
41         return ;
42     int mid=(l+r)/2;
43     if(pos<=mid)
44         update(ls[pre],pos,ls[cur],l,mid);
45     else    
46         update(rs[pre],pos,rs[cur],mid+1,r);
47 }
48 int query(int lt,int rt,int l,int r,int k)
49 {
50     if(l==r)
51         return l;
52     int now=cnt[ls[rt]]-cnt[ls[lt]];
53     int mid=(l+r)/2;
54     if(k<=now)
55         return query(ls[lt],ls[rt],l,mid,k);
56     else 
57         return query(rs[lt],rs[rt],mid+1,r,k-now);
58 }
59 int main()
60 {
61     while(scanf("%d%d",&n,&m)==2)
62     {
63         for(int i=1;i<=n;i++)
64         {
65             read(a[i]);
66             hash[i]=a[i];
67         }
68         sort(hash+1,hash+n+1);
69         int size=unique(hash+1,hash+n+1)-hash-1;
70         for(int i=1;i<=n;i++)
71             a[i]=lower_bound(hash+1,hash+size+1,a[i])-hash;// 排序+離散化 
72         
73         tot=0;
74         build(root[0],1,size);
75         
76         for(int i=1;i<=n;i++)
77             update(root[i-1],a[i],root[i],1,size);// 建出所有的樹 
78         
79         for(int i=1;i<=m;i++)
80         {
81             int x,y,z;
82             read(x);read(y);read(z);
83             printf("%d\n",hash[query(root[x-1],root[y],1,size,z)]);
84         }    // 查詢 
85     }
86     
87     return 0;
88 }

 

 
您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 數據結構:是指通過某種方式組織在一起的數據元素的集合 主要包括: 序列:主要介紹列表、元組、字元串 映射:字典 集合(set) 接下來,會對列表進行介紹 ...
  • 上一篇文章中不知道大家發現端倪木有,兩張照片對比很明顯發現第一張是信息很明顯的,第二張是亂碼的。 為什麼會出現這種情況?細心的童鞋可能發現是我們發送給伺服器的請求連接的數據不同: 第一張圖的信息是{"roomid":98284,"uid":271298361556770} 第二張圖的信息是{"uid ...
  • google已經將kotlin作為android開發的首選語言,然而我並不是android開發者,也不是java開發者,那麼我為什麼要學kotlin呢? 也許是心血來潮,也許是因為JB家出的編程語言必定會火,也許我只是JB家的忠實粉絲而已,不管怎麼樣吧,在此立一個flag,開啟我的kotlin學習之 ...
  • 一 概述 1.EL Expression Language,表達式語言,一種不同於編程語言的語言,用於訪問對象或者為對象賦值,取代JSP頁面中嵌套的java代碼,使頁面風格統一。 2.語法格式 expression既可以是屬性,也可以是字面值,還可以是算術表達式、關係表達式、邏輯表達式、條件表達式等 ...
  • 今天第一次接觸java的網頁編程,因為我之前是學習php的。所以學習jsp的話感覺是很相似的。第一次入門的程式是混編的形式,和php的用法相識,java規定的嵌入語言是用<% %>來表示是java的代碼,而php的話是使用<?php ?>來表示php的代碼塊。 這次的學習的效果如下: 代碼如下: 在 ...
  • 題目背景 很多學校流行一種比較的習慣。老師們很喜歡詢問,從某某到某某當中,分數最高的是多少。這讓很多學生很反感。 題目描述 不管你喜不喜歡,現在需要你做的是,就是按照老師的要求,寫一個程式,模擬老師的詢問。當然,老師有時候需要更新某位同學的成績 輸入輸出格式 輸入格式: 第一行,有兩個正整數 N 和 ...
  • 動態代理案例1:/*要求:運用Proxy動態代理來增強方法題目: 1.定義介面Fruit,其中有addFruit方法 2.定義實現類FruitImpl,實現Fruit介面 3.定義測試類,利用動態代理類的方式,增強addFruit方法*/ 1 import java.lang.reflect.Pro... ...
  • 文件內容如下: 現在看如何處理並轉成列表! 輸出結果如下: ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...