1. 程式人生 > >HDU2665 Kth number(主席樹,離散化)

HDU2665 Kth number(主席樹,離散化)

Problem Description

Give you a sequence and ask you the kth big number of a inteval.

Input

The first line is the number of the test cases. For each test case,
the first line contain two integer n and m (n, m <= 100000), indicates
the number of integers in the sequence and the number of the quaere.
The second line contains n integers, describe the sequence. Each of
following m lines contains three integers s, t, k. [s, t] indicates
the interval and k indicates the kth big number in interval [s, t]

Output

For each test case, output m lines. Each line contains the kth big
number.

Sample Input

1 
10 1 
1 4 2 3 5 6 7 8 9 0 
1 3 2 

Sample Output

2

思路

普通的主席樹求給定區間第k大,如果不會的去看我以前的部落格。。

程式碼

#include <cstdio>
#include <cstring>
#include <cctype>
#include <stdlib.h>
#include <string> #include <map> #include <iostream> #include <sstream> #include <set> #include <stack> #include <cmath> #include <queue> #include <vector> #include <algorithm> #include <list> using namespace std; #define mem(a, b) memset(a, b, sizeof(a))
typedef long long ll; const int N = 2e5 + 10; const int inf = 0x3f3f3f3f; int node_cnt, n, m; int sum[N << 5], rt[N], lc[N << 5], rc[N << 5]; int a[N], b[N], p; //原序列和離散序列和修改點 void build(int &t, int l, int r) { t = ++node_cnt; if (l == r) return; int mid = (l + r) >> 1; build(lc[t], l, mid); build(rc[t], mid + 1, r); } int modify(int o, int l, int r) { int oo = ++node_cnt; lc[oo] = lc[o]; rc[oo] = rc[o]; sum[oo] = sum[o] + 1; if (l == r) return oo; int mid = (l + r) >> 1; if (p <= mid) lc[oo] = modify(lc[oo], l, mid); else rc[oo] = modify(rc[oo], mid + 1, r); return oo; } int query(int u, int v, int l, int r, int k) //求u,v這兩棵線段樹的差的樹中的第k大 { int mid = (l + r) >> 1, ans; int x = sum[lc[v]] - sum[lc[u]]; if (l == r) return l; if (x >= k) ans = query(lc[u], lc[v], l, mid, k); else ans = query(rc[u], rc[v], mid + 1, r, k - x); return ans; } void init() { node_cnt = 0; } int main() { //freopen("in.txt", "r", stdin); int t; scanf("%d", &t); while (t--) { scanf("%d%d", &n, &m); init(); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); b[i] = a[i]; } sort(b + 1, b + n + 1); int q = unique(b + 1, b + n + 1) - (b + 1); build(rt[0], 1, q); for (int i = 1; i <= n; i++) { p = lower_bound(b + 1, b + q + 1, a[i]) - b; rt[i] = modify(rt[i - 1], 1, q); } int l, r, k; while (m--) { scanf("%d%d%d", &l, &r, &k); int ans = query(rt[l - 1], rt[r], 1, q, k); printf("%d\n", b[ans]); } } return 0; }

相關推薦

HDU2665 Kth number(主席離散)

Problem Description Give you a sequence and ask you the kth big number of a inteval. Input The first line is the number

hdu 2665 Kth number 主席

Kth number Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 12712&n

LuoguP3834 【模板】可持久化線段 1(主席)|| 離散

name node urn con upd i++ cstring get pro 題目:【模板】可持久化線段樹 1(主席樹) 不知道說啥。 1 #include<cstdio> 2 #include<cstring> 3 #include&

HDU2665 Kth number主席

Kth number Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 16583    Accepted Su

POJ-2299 Ultra-QuickSort (狀數組離散C++)

test tle print integer += log operator 有意思 produce Problem Description In this problem, you have to analyze a particular sorting algorith

HDU2665 Kth number(歸併模板)

Kth number 傳送門1傳送門2 Give you a sequence and ask you the kth big number of a inteval. Input The

POJ 2104 K-th Number主席區間第K大的數)

Description You are working for Macrohard company in data structures department. After failing your previous task about key insertion you

HDU 4605 Magic Ball Game(可持續化線段狀陣列離散

When the magic ball game turns up, Kimi immediately falls in it. The interesting game is made up of N balls, each with a weight of w[i]. These N balls for

HDU 4417 —— Super Mario(狀陣列離散離線處理)

意思比較簡單,就是給N個數(下標從0開始),然後q個詢問,三個引數,L,R,H,詢問序列中在【L,R】這個區間上小於等於H的個數。 挺綜合的一道題目。因為數字最多100000個,數值最多達10^9,所以首先要對數值進行離散化。 像這種區間查詢問題,用S(X,H)表示從0開

hdu1542 線段+掃描線+離散

ack cas explore amp mar struct %d for ase 僅僅想說題目給的欲實際不服 還是這類型的水題吧 建議看之前我寫的那個 #include<stdio.h> #include<stri

POJ 2104 K-th Number (主席)

std +++ esp space ctype == string uniq upd 題意:給定一個序列,然後有 q 個詢問,每次詢問 l - r 區間內的第 k 大的值。 析:很明顯的主席樹,而且還是裸的主席樹,先進行離散化,然後用主席樹進行查詢就好。 代碼如下: #p

【BZOJ2006】超級鋼琴(主席優先隊列)

題解 tin char std tor getchar 每次 隊列 ace 【BZOJ2006】超級鋼琴(主席樹,優先隊列) 題面 BZOJ 題解 既然是一段區間 首先就要變成單點 所以求一個前綴和 這個時候貪心很明顯了: 枚舉每一個點和可以和它組成一段的可行的點 全部丟進

poj 2528 線段+特殊離散

cto 1-1 there sync ali after hat tor memory Mayor‘s posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions:

主席喵~

str hdu4417 ffi pac 直接 發現 names 很多 Go 稍微總結一下主席樹吧 Too Difficult!搞了一天搞出一大堆怎麽令人悲傷的辣雞代碼。總之先總結一下吧,以後碰到這種問題直接拿去毒害隊友好了。 Lv.1 最基本的操作 區間k大值 區間內有

Poj 2104 K-th Number 主席模版題

OS tdi pda sig signed begin ostream air color 題意:離線詢問[l,r]區間第k大 題解:模版題,入門題 #include <iostream> #include <cstdio> #inclu

【ZOJ4053】Couleur(主席set啟發式)

oid bool operator tor std 計算 每次 per return 題意: 有n個位置,每個位置上的數字是a[i],現在有強制在線的若幹個單點刪除操作,每次刪除的位置都不同,要求每次刪除之後求出最大的連續區間逆序對個數 n<=1e5,1<=a[

【CodeForces - 255C】Almost Arithmetical Progression (dp離散

題幹: Gena loves sequences of numbers. Recently, he has discovered a new type of sequences which he called an almost arithmetical progression. A seq

(二分尺取離散)1686 第K大區間

1686 第K大區間 1 秒   131,072 KB   40 分   4 級題 定義一個區間的值為其眾數出現的次數。 現給出n個數,求將所有區間的值排序後,第K大的值為多少。 眾數(統計學/數學名詞)_百度百

LeetCode 715. Range Module / 57. Insert Interval(區間查詢更新離散

A Range Module is a module that tracks ranges of numbers. Your task is to design and implement the following interfaces in an efficient manner.

洛谷P3241 [HNOI2015]開店 [鏈剖分主席lca]

又是一道黑題,不容易啊。。。 連結 首先,不管年齡的限制,問題即可簡化為:給定一個點,求其他所有點到當前點的距離 回想一下樹上兩點距離公式:,兩點距離等於兩點深度相加減去lca的深度乘二 點的深度可以一次O(n)的dfs解決,問題轉化為求對於一個點u,,字好小啊。。。 回想 [L