Hdu4006 (the kth great number) priority queue/SBT

Source: Internet
Author: User
/*************************************** * ********* The subject is as follows: output the K-th large number for each query. algorithm idea: (1) According to the question, you only need to keep the first K large numbers and sort them in descending order; that is to say, each time a number is added, the location of the number is found. Then, the number beyond K elements is deleted. The priority queue can be used to do well. (2) SBT or tree array solution; **************************************** * ********/# include <iostream> # include <algorithm> # include <cmath> # include <cstdio> using namespace std; const int N = 1000010; int l [N], r [N], s [N]; int key [N]; int node; void left_rotate (int & t) {int k = r [T]; r [t] = l [k]; l [k] = t; s [k] = s [t]; s [t] = s [l [t] + s [r [t] + 1; t = k;} void right_rotate (int & t) {int k = l [t]; l [t] = r [k]; r [k] = t; s [k] = s [t]; s [t] = s [l [t] + s [r [t] + 1; t = k;} void maintain (int & t, bool flag) {if (flag = false) {if (s [l [l [t]> s [r [t]) right_rotate (t ); else if (s [l [r [t]> s [r [t]) {left_rotate (t); right_rotate (t);} else return ;} else {if (s [r [r [t]> s [l [t]) left_rotate (t ); else if (s [r [l [t]> s [L [t]) {right_rotate (t); left_rotate (t);} else return;} maintain (l [t], false); maintain (r [t], true); maintain (t, false); maintain (t, true);} void insert (int & t, int k) {if (! T) {s [t = ++ node] = 1; l [t] = r [t] = 0; key [t] = k ;} else {++ s [t]; if (key [t]> k) insert (l [t], k); else insert (r [t], k ); maintain (t, k> = key [t]) ;}} int select (int t, int k) {int v = s [l [t] + 1; if (k = v) return key [t]; else if (k <v) return select (l [t], k); else return select (r [t], k-v);} int main () {// freopen ("C: \ Users \ Administrator \ Desktop \ kd.txt", "r", stdin ); int n, k; while (~ Scanf ("% d", & n, & k) {int u = node = s [0] = 0; while (n --) {char c; int x; scanf ("% c", & c); if (c = 'I') {scanf ("% d", & x); insert (u, x);} else printf ("% d \ n", select (u, s [u] + 1-k) ;}} return 0 ;}

 

Priority queue:

#include<iostream>#include<algorithm>#include<cmath>#include<cstdio>#include<queue>using namespace std;/*struct node{int tmp;friend bool operator<(const node a,const node b){return a.tmp>b.tmp;}};*/int main(){//freopen("C:\\Users\\Administrator\\Desktop\\kd.txt","r",stdin);int n,k;while(~scanf("%d%d",&n,&k)){//priority_queue<node>Q;priority_queue<int,vector<int>,greater<int> > Q;for(int i=1;i<=n;i++){char c;scanf(" %c",&c);if(c=='I'){int x;scanf("%d",&x);Q.push(x);if(Q.size()>k)Q.pop();}elseprintf("%d\n",Q.top());}}return 0;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.