HDU 2852 Kiki's k-number (line segment tree + tree array)

Source: Internet
Author: User

Kiki's k-Number Time Limit: 4000/2000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 2603 accepted submission (s): 1202


Problem descriptionfor the k-th number, we all shoshould be very familiar with it. of course, to Kiki it is also simple. now Kiki meets a very similar problem, Kiki wants to design a container, the container is to support the three operations.

Push: Push a given element e to container

Pop: Pop element of a given E from Container

Query: Given two elements A and K, query the kth larger number which greater than a in iner;

Although kiki is very intelligent, she can not think of how to do it, can you help her to solve this problem?

Inputinput some groups of test data, each test data the first number is an integer m (1 <= m <100000), means that the number of operation to do. the next M lines, each line will be an integer p at the beginning, P which has three values:
If P is 0, then there will be an integer e (0 <e <100000), means press Element E into container.

If P is 1, then there will be an integer e (0 <e <100000), indicated that Delete the Element E from the container

If P is 2, then there will be two integers A and K (0 <A <100000, 0 <k <10000), means the inquiries, the element is greater than, and the k-th larger number.

Outputfor Each deletion, if you want to delete the element which does not exist, the output "No elment! ". For each query, output the suitable answers in line. If the number does not exist, the output" not find! ".
Sample Input
 
50 51 20 62 3 22 8 170 20 42 1 12 1 22 1 32 1 4

Sample output
 
No elment! 6not find! 224not find!


The line segment tree almost times out, and it still feels like a tree array, but after all, I have just learned the line segment tree.

# Include <iostream> # include <cstdio> # include <cstdlib> # include <cstring> # include <algorithm> # include <cmath> using namespace STD; const int maxn = 300010; const int maxm = 100001; # define lson L, mid, RT <1 # define rson Mid + 1, R, RT <1 | 1 # define Max int_max # define min int_minstruct node {int left, right; int num;} t [maxm <2]; int ans = 0; void creat (INT left, int right, int ID) // build {T [ID]. left = left; t [ID]. rig Ht = right; t [ID]. num = 0; If (T [ID]. left = T [ID]. right) return; creat (left, (left + right)/2,2 * ID); creat (left + right)/2 + 1, right, 2 * ID + 1);} void updata (int id, int I, Int J) {If (T [ID]. left <= I & T [ID]. right> = I) T [ID]. num + = J; If (T [ID]. left = T [ID]. right) return; if (I> T [ID]. right) return; if (I <t [ID]. left) return; int mid = (T [ID]. left + T [ID]. right)/2; if (I <= mid) updata (ID * 2, I, j); else updata (ID * 2 + 1, I, j );} Void query (int id, int L, int R) {int mid = (T [ID]. left + T [ID]. right)/2; If (T [ID]. left = L & T [ID]. right = r) {ans + = T [ID]. num; return;} If (r <= mid) query (2 * ID, L, R); else if (L> mid) query (2 * ID + 1, l, r); else {query (2 * ID, L, mid); query (2 * ID + 1, Mid + 1, R) ;}} int B _search (INT X) {int low = 1; int high = maxm; int mid, WZ =-1; while (low <= high) {mid = (low + high)/2; ans = 0; query (1, 1, mid); If (ANS> = x) {high = Mid-1; WZ = mid;} else {LOW = Mid + 1;} return WZ;} int main () {int n, a, B, K; while (~ Scanf ("% d", & N) {creat (1,100001, 1); For (INT I = 0; I <n; I ++) {scanf ("% d", & A); if (a = 0) {scanf ("% d", & B); updata (1, B, 1);} else if (a = 1) {scanf ("% d", & B); ans = 0; query (1, B, B); If (! Ans) puts ("No elment! "); Else updata (1, B,-1);} else if (a = 2) {scanf (" % d ", & B, & K ); ans = 0; query (1, 1, B); int TEM = B _search (ANS + k); If (TEM! =-1) printf ("% d \ n", TEM); else puts ("not find! ") ;}}Return 0 ;}

Tree Array

# Include <iostream> # include <cstdio> # include <cstdlib> # include <cstring> # include <algorithm> # include <cmath> # define Init (a) memset (, 0, sizeof (A) using namespace STD; # define Max int_max # define min int_min # define ll _ int64 # define lson l, m, RT <1 # define rson m + 1, R, RT <1 | 1 const int maxn = 300010; const int maxm = 100010; using namespace STD; int C [maxn]; int hash [maxm]; int lowbit (int x) {re Turn X & (-x);} void add (int x, int W) {While (x <= maxn) {C [x] + = W; X + = lowbit (x) ;}} int sum (int I) {int S = 0; while (I> 0) {S + = C [I]; I = I-lowbit (I);} return s;} void B _search (int B, int K) {int TEM = sum (B); int low = B; int high = maxm; int mid; while (low <= high) {mid = (low + high)/2; int ST = sum (MID ); if (hash [Mid]> 0 & St-Hash [Mid]-tem <K & St-tem> = k) {break ;} if (St-tem <k) {LOW = Mid + 1;} els E high = mid-1;} printf ("% d \ n", mid);} bool query (int B, int K) {If (sum (maxn) -sum (B) <k) return 0; B _search (B, k); return 1 ;}int main () {int n, a, B, K; while (scanf ("% d", & N )! = EOF) {Init (c); Init (hash); For (INT I = 0; I <n; I ++) {scanf ("% d ", & A); if (a = 0) {scanf ("% d", & B); add (B, 1); hash [B] ++ ;} else if (a = 1) {scanf ("% d", & B); If (hash [B]> 0) {Add (B,-1 ); hash [B] --;} else puts ("No elment! ");} Else if (a = 2) {scanf (" % d ", & B, & K); bool TEP = query (B, k ); if (! TEP) puts ("not find! ") ;}}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.