G - KiKi's K-Number (Fenwick tree request interval k-th largest)

For the k-th number, we all should 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 container;

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 a, 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
5
0 5
1 2
0 6
2 3 2
2 8 1
7
0 2
0 2
0 4
2 1 1
2 1 2
2 1 3
2 1 4
Sample Output
! Elment NO 
. 6 
Not the Find! 
2 
2 
. 4 
Not the Find! 
Title meaning: There are three modes of operation: 0 The pressed into the container to the element. 1. The elements to be removed from the container, if there is no output No Elment! 2. Get the number k is larger than the first of a large number (using a bipartite approximation)

#include<iostream>
#include<cstring>
#include<vector>
#include<string>
#include<stdio.h>
using namespace std;
typedef long long ll;
int lowbit(int x){return x&-x;}
const int maxn=100010;
int c[maxn+10];
int  n,m;
void update(int x,int v)
{
    for(int i=x;i<=maxn;i+=lowbit(i))
        c[i]+=v;
}
int sum(int x)
{
    int ans=0;
    for(int i=x;i>=1;i-=lowbit(i))
        ans+=c[i];
    return ans;
}


int main()
{
    ios::sync_with_stdio(0);
    int m;
    while(cin>>m){
        memset(c,0,sizeof(c));
        while(m--){
            int op,x,k;
            cin>>op;
            if(op==0){
                cin>>x;
                update(x,1);
            }
            else if(op==1){
                cin>>x;
                if(sum(x)-sum(x-1)==0)cout<<"No Elment!"<<endl;
                else update(x,-1);
            }
            else{ 
                CIN >> X >> K;
                 int L = X + . 1 , R & lt = 100010 , ANS = - . 1 ;
                 the while (L <= R & lt) {
                     int MID = (L + R & lt) / 2 ;
                     IF (SUM (MID) - SUM (x)> = k) // if [x + 1, mid] x is larger than the interval exceeds the k, the answer may be smaller described 
                        ANS = MID, R & lt mid- = . 1 ;
                     the else L = MID + . 1 ; // otherwise, the answer to the need for greater 
                } 
                IF (ANS == - 1 ) cout << " ! Not the Find " <<endl;
                else cout<<ans<<endl;
            }
        }
    }
    return 0;
}

 2: tree line version (not simply use half of the need to directly write a recursive function, a bit like a tree line weights mean)

#include<iostream>
#include<cstring>
#include<vector>
#include<string>
#include<stdio.h>
using namespace std;
typedef long long ll;
int lowbit(int x){return x&-x;}
const int maxn=100010;
int tree[maxn<<2];
void update(int l,int r,int x,int v,int rt)
{
    if(l==r){
        if(v==1)tree[rt]++;
        else{
            if(tree[rt]==0)
                cout<<"No Elment!"<<endl;
            else
                tree[rt]--;
        }
        return ;
    }
    int mid=(l+r)/2;
    if(x<=mid)update(l,mid,x,v,rt*2);
    else update(mid+1,r,x,v,rt*2+1);
    tree[rt]=tree[rt*2]+tree[rt*2+1];
}
int querysum(int l,int r,int L,int R,int rt)
{
    if(L<=l&&R>=r)return tree[rt];
    int mid=(l+r)/2;
    int ans=0;
    if(L<=mid)ans+=querysum(l,mid,L,R,rt*2);
    if(R>=mid+1)ans+=querysum(mid+1,r,L,R,rt*2+1);
    return ans;
}
int query(int l,int r,int k,int rt)
{
    if(l==r)return l;
    int mid=(l+r)/2;
    if(k<=tree[rt*2])return query(l,mid,k,rt*2);
    else return query(mid+1,r,k-tree[rt*2],rt*2+1);
}
int main()
{
    int m;
    int n=100010;
    while(scanf("%d", &m) != EOF){
        memset(tree,0,sizeof(tree));
        while(m--){
            int op,x,k;
            scanf("%d",&op);
            if(op==0){
                 scanf("%d",&x);
                update(1,n,x,1,1);
            }
            else if(op==1){
                scanf("%d",&x);
                update(1,n,x,-1,1);
            }
            else{
                scanf("%d%d",&x,&k);
                int pos=querysum(1,n,1,x,1);
                int ans=query(1,n,pos+k,1);
                if(ans>=maxn)cout<<"Not Find!"<<endl;
                else cout<<ans<<endl;
            }
        }
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/cherish-lin/p/10961135.html