HDU 2852-kiki ' s K-number (segment tree)

Source: Internet
Author: User

KiKi ' s K-numberTime limit:$ MS Memory Limit:32768KB 64bit IO Format:%i64d &%i64u SubmitStatusPracticeHDU 2852Appoint Description:Leolin (2011-08-22)System Crawler (2015-04-16)

Description

For the k-th number, we all should is 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 are to support the three operations.

Push:push a given element e to container

Pop:pop element of a given e from container

Query:given elements A and K, query the kth larger number which greater than a in container;

Although Kiki is very intelligent, she can isn't think of how to does it, can you help hers to solve this problem?

Input

Input Some groups of test data, each test data the first number is a integer m (1 <= m <100000), means that the NUM ber of operation to do. The next m lines, each line would be a integer p at the beginning, p which have three values:
If p is 0 and then there'll be a integer e (0 <e <100000), means press element e into Container.

If p is 1 and then there'll be a integer e (0 <e <100000), indicated that delete the element e from the container

If P is 2, then there'll be integers a and K (0 <a <100000, 0 <k <10000), means the inquiries, the Elemen T is greater than a, and the k-th larger number.

Output

For each deletion, if want to delete the element which does not exist, the output "No elment!". For each query, the output of the suitable answers in line. If the number is does not exist, the output is "not find!".

Sample Input

Sample Output

No Elment!6not Find!224not find!

#include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include < iostream> #include <sstream> #include <algorithm> #include <set> #include <queue> #include <stack> #include <map>using namespace std;typedef long long ll;const int inf=0x3f3f3f3f;const double pi= ACOs ( -1.0); #define Lson l,mid,rt<<1#define rson mid+1,r,rt<<1|1const int maxn=100010;int sum[maxn<<2]; void pushup (int rt) {sum[rt]=sum[rt<<1]+sum[rt<<1|1];}        void Update (int p, int x, int l, int r, int rt) {if (l==r) {sum[rt]+=p;    return;    } int mid= (L+R) >>1;    if (x<=mid) Update (p, X, Lson);    Else Update (p, X, Rson); Pushup (RT);}    int Query (int ll, int rr, int l, int r, int rt) {if (ll<=l&&rr>=r) {return SUM[RT];    } int mid=l+r>>1;    int ans=0;    if (ll<=mid) ans+=query (Ll,rr,lson); if (rr>mid) ans+=query (Ll,rr,rson);    return ans;}    int binsearch (int k) {int high=100010, low =1, mid;    int ans=-1;    int res;        while (Low<=high) {mid= (Low+high) >>1;        Res=query (1,mid,1,100010,1);            if (res>=k) {high=mid-1;        Ans=mid;        } else if (res<k) {low=mid+1; }} return ans;    int main () {int n,i;    int p,a,k;        while (~SCANF ("%d", &n)) {memset (sum,0,sizeof (sum));            while (n--) {scanf ("%d", &p);                if (p==0) {scanf ("%d", &a);            Update (1,a,1,100010,1);                } else if (p==1) {scanf ("%d", &a); if (!                    Query (a,a,1,100010,1)) {printf ("No elment!\n");                Continue;            } Update ( -1,a,1,100010,1);                } else {scanf ("%d%d", &a,&k);                int S=query (1,a,1,100010,1);                int Ans=binsearch (S+K); if (ans==-1)                   printf ("Not find!\n");            else printf ("%d\n", ans); }}} return 0;}


HDU 2852-kiki ' s K-number (segment tree)

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.