1. 程式人生 > >POJ2104(主席樹求區間第K大)

POJ2104(主席樹求區間第K大)

模板題,模板來自B站UESTCACM

#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <sstream>
#include <cmath>
#include <stack> #define LL long long #define mod 1000000007 #define INF 0x3f3f3f3f using namespace std; const int maxn = 1e6 + 5; vector<int> v; int a[maxn], root[maxn]; int cnt; int getid(int x){ return lower_bound(v.begin(),v.end(),x)-v.begin()+1; } struct node{ int l,r,sum; }T[maxn*40
]; void update(int l, int r, int &x, int y, int pos){ T[++cnt] = T[y]; T[cnt].sum++; x = cnt; if(l == r) return; int m = (l+r)/2; if(m >= pos) update(l,m,T[x].l,T[y].l,pos); else update(m+1,r,T[x].r,T[y].r,pos); } int query(int l, int r, int x, int y, int k){ if
(l == r) return l; int m = (l + r)/2; int sum = T[T[y].l].sum - T[T[x].l].sum; if(sum >= k) return query(l,m,T[x].l,T[y].l,k); else return query(m+1,r,T[x].r,T[y].r,k-sum); } int main(){ int n,q; scanf("%d%d",&n,&q); for(int i=1; i<=n; i++){ scanf("%d",&a[i]); v.push_back(a[i]); } sort(v.begin(),v.end()); v.erase(unique(v.begin(),v.end()),v.end()); for(int i=1; i<=n; i++) update(1,n,root[i],root[i-1],getid(a[i])); while(q--){ int x,y,k; scanf("%d%d%d",&x,&y,&k); printf("%d\n",v[query(1,n,root[x-1],root[y],k)-1]); } return 0; }

相關推薦

POJ2104主席區間K

模板題,模板來自B站UESTCACM #include <iostream> #include <algorithm> #include <queue> #in

HDU 2665 Kth number主席靜態區間K題解

可持久化 unique algorithm using 主席樹 可持久化線段樹 long spa 靜態區 題意:問你區間第k大是誰 思路:主席樹就是可持久化線段樹,他是由多個歷史版本的權值線段樹(不是普通線段樹)組成的。 具體可以看q學姐的B站視頻 代碼:

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

POJ 2104 K-th Number 主席(區間k)

主席書資料 題意:給出n個數,m次詢問,[x,y]內第k小的數時多少?n<=1e5,m<=5000 主席樹:對原序列的每個字首i都建立一個線段樹 維護值域[l,r]中的每個數,在字首i的

poj 2104主席區間k

區間 ++ cto ast http lan air algorithm while POJ - 2104 題意:求區間第k小 思路:無修改主席樹 AC代碼: #include "iostream" #include "iomanip" #include "string.

poj2104 (線段區間k)

題目連結: 題意: 給n個數, 每次詢問一個區間, 讓你輸出區間中的第k大的數。 做了這道題,我對線段樹的能解決的問題得認識又有提升。 這個是讓求區間內的第k大, 詢問比較多,暴力顯然不行。 我們想用線段樹解題,那麼線段樹應該儲存什麼呢? 一開始更本沒想到, 看了別人的部落

[poj 2104]主席+靜態區間k

include end 區間 得到 name int 題目 tar tdi 題目鏈接:http://poj.org/problem?id=2104 主席樹入門題目,主席樹其實就是可持久化權值線段樹,rt[i]維護了前i個數中第i大(小)的數出現次數的信息,通過查詢兩棵樹的差

hdu 5919--Sequence II主席--區間不同數個數+區間k

positions minus -s ima date rst itl 主席樹 技術 題目鏈接 Problem Description Mr. Frog has an integer sequence of length n, which can be denot

主席入門詳解一學習筆記例題POJ-2104 區間k

學習主席樹,在網上搜了很多教程(都好簡短啊,直接就是幾行字就上程式碼,看不懂啊有木有~~),最後才很艱難的學會了最基礎的部分。下面就是我在學習的過程中的產生的疑惑和解決的辦法。 學習主席樹需要的前置技能:線段樹。 參考資料 1. B站上的視訊講解(話說B站真的啥都有啊)

hdu2665 區間k小?主席or可持久化線段or函式式線段

題目大意:感覺題目表述得不明不白的,給一堆不知道我也不知道什麼資料範圍的數,然後給你M個區間,輸出每個區間的第k大的數(這裡出現嚴重的問題!!!) 題目說得kth bigger 難道不是第k大?結果我WA了一堆之後,翻了幾篇別人的部落格程式碼,結果發現別人

HDU2665 區間K 主席

題目連結 http://acm.hdu.edu.cn/showproblem.php?pid=2665   程式碼: //#include<bits/stdc++.h> #include<iostream> #include<cmath> #incl

HDOJ2665 Kth number --- 劃分區間k小數

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 c

區間k的數

1175 區間中第K大的數 題目連結:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1175&judgeId=649231 主席樹與普通線段樹的建樹方法有點不同,他只儲存他的

spoj COT - Count on a tree(主席 +lca,樹上K)

分享圖片 query 很多 include pac view 一行 struct oid 您將獲得一個包含N個節點的樹。樹節點的編號從1到?。每個節點都有一個整數權重。 我們會要求您執行以下操作: uvk:詢問從節點u到節點v的路徑上的第k個最小權重 輸入 在第一行中有

區間k黑匣子_NOI導刊2010提高06

https://www.luogu.org/problemnew/show/P1801 可以用multiset水過,發現有超多資料結構可以處理這道題。== 線段樹:https://blog.csdn.net/weishengmingerfendou/article/details/47144

A - K-th Number POJ - 2104 -主席第一彈-K的數

感謝:http://www.cnblogs.com/zyf0163/p/4749042.html https://blog.csdn.net/qq_24451605/article/details/49031123 裸題多次查詢給定區間L—R內第K大的數 #include&

2665 Kth number 靜態區間k

Give you a sequence and ask you the kth big number of a inteval. InputThe first line is the numbe

poj2104區間k小,靜態主席入門模板

看了很久的主席樹,最後看https://blog.csdn.net/williamsun0122/article/details/77871278這篇終於看懂了 #include <stdio.h> #include<algorithm> using namespace s

區間k主席

區間第k小 題目描述 如題,給定NNN個正整數構成的序列,將對於指定的閉區間查詢其區間內的第KKK小值。 輸入格式 第一行包含兩個正整數NNN、MMM,分別表示序列的長度和查詢的個數。 第二行包含NN

區間k靜態——主席

Description 給定一個長度為n的序列,m個詢問,每個詢問的形式為:L,r,k表示在[L,r]間中的第k大元素。 Input 第1行:2個數,n,m表示序列的長度和詢問的個數 第2行:n個數,表示n個數的大小 第3-m+2行:每行3個數,L,r,k表示詢問在[L