wyh的天鹅-NC15449(平衡线段树)+(vector二分优化)

wyh的天鹅

题号:NC15449
时间限制:C/C++ 3秒,其他语言6秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述 

你们wyh学长小时候住在河边,因为周围的生态环境非常好,所以经常会有天鹅浮在湖面上,每只天鹅都长得不一样,它们偶尔排成一排,偶尔分散开,偶尔也会去其他河畔,wyh学长为了统计它们的个数,编了一个程序赋予它们一个“萌”值,但是这些天鹅很不听话,一会儿会从别的地方游过来一两只,一会儿又会在统计过程中游走一两只,现在请你帮他完成统计任务。

输入描述:

共有T(T<=10)组数据,每组数据第一行为两个数 N, M (N,M <= 500000),代表有N只天鹅和M次操作,接下来一行是N个数字,下面M行首先会输入一个字符串S,接着会有三类操作,如果S是“insert”,接着输入一个正整数a,代表插入一只“萌”值为a的天鹅,如果S是“delete”,接着输入一个正整数a,代表删除一只“萌”值为a的天鹅,如果S是“query”,接着输入一个正整数k,代表查询“萌”值第k大的天鹅。
萌值为[1,1000000000],并且保证一定存在第k大

输出描述:

对应每次询问,输出询问结果。

示例1

输入

复制

1
5 4
6 4 2 9 1
query 2
insert 7
delete 6
query 2

输出

复制

6
7
/*
*@Author:   GuoJinlong
*@Language: C++
*/
//#include <bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<list>
#include<set>
#include<iomanip>
#include<cstring>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<cassert>
#include<sstream>
#include<algorithm>
using namespace std;
const int mod=1e9+7;
typedef long long  ll;
#define ls (rt<<1)
#define rs (rt<<1|1)
#define mid (l+r)/2
#define mms(x, y) memset(x, y, sizeof x)
#define over(i,s,t) for(register long long i=s;i<=t;++i)
#define lver(i,t,s) for(register long long i=t;i>=s;--i)
const int MAXN = 305;
const int INF = 0x3f3f3f3f;
const int N=5e4+7;
const int maxn=1e5+5;
const double EPS=1e-10;
const double Pi=3.1415926535897;
//inline double max(double a,double b){
//    return a>b?a:b;
//}
//inline double min(double a,double b){
//    return a<b?a:b;
//}
 
int xd[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int yd[8] = {1, 0, -1, 0, -1, 1, -1, 1};
 
//void Fire(){
//    queue<node> p;
//    p.push({fx,fy,0});
//    memset(fire, -1, sizeof(fire));
//    fire[fx][fy]=0;
//    while(!p.empty()){
//        node temp=p.front();
//        p.pop();
//        for(int i=0;i<8;i++){
//            int x=temp.x+xd[i];
//            int y=temp.y+yd[i];
//            if(x<0||x>=n||y<0||y>=m||fire[x][y]!=-1){
//                continue;
//            }
//            fire[x][y]=temp.val+1;
//            p.push({x,y,temp.val+1});
//        }
//    }
//}
//int bfs(){
//    queue<node> p;
//    memset(vis, 0, sizeof(vis));
//    p.push({sx,sy,0});
//    while (!p.empty()) {
//        node temp=p.front();
//        vis[temp.x][temp.y]=1;
//        p.pop();
//        for(int i=0;i<4;i++){
//            int x=temp.x+xd[i];
//            int y=temp.y+yd[i];
//            if(x<0||x>=n||y<0||y>=m)  continue;
//            if(x==ex&&y==ey&&temp.val+1<=fire[x][y]) return temp.val+1;
//            if(vis[x][y]||temp.val+1>=fire[x][y]||a[x][y]=='#') continue;
//            p.push({x,y,temp.val+1});
//        }
//    }
//    return -1;
//}

//一维哈希
//int n;
//string s;
//int bas=131;
//typedef unsigned long long ull;
//const ull mod1=100001651;
//ull a[100010];
//ull Hash(string s){
//    ll ans=0;
//    for(int i=0;i<s.size();i++){
//        ans*=bas;
//        ans+=int(s[i]);
//        ans%=mod1;
//    }
//    return ans;
//}

//二维哈希
//using lom=unsigned long long ;
//const lom bas1=131,bas2=233;
//const int M=505;
//int n,m;
//char a[M][M];
//lom _hash[M][M];
//lom p1[M],p2[M];
//
//
//void init(){
//    p1[0]=1;
//    p2[0]=1;
//    for(int i=1;i<=505;i++){
//        p1[i]=p1[i-1]*bas1;
//        p2[i]=p2[i-1]*bas2;
//
//    }
//}
//void Hash(){
//    _hash[0][0]=_hash[0][1]=_hash[1][0]=0;
//    for(int i=1;i<=n;i++){    //前缀和
//        for(int j=1;j<=m;j++){
//            _hash[i][j]=_hash[i][j-1]*bas1+a[i][j]-'a';
//        }
//    }
//    for(int i=1;i<=n;i++){   //二维前缀和
//        for(int j=1;j<=m;j++){
//            _hash[i][j]+=_hash[i-1][j]*bas2;
//        }
//    }
//
//}


//int pre[1010];
//int in[1010];
//int post[1010];
//int k;
//struct node{
//    int value;
//    node *l,*r;
//    node (int value=0,node *l=NULL,node *r=NULL):value(value),l(l),r(r){}
//};
//void builttree(int l,int r,int &t,node * &root){
//    int flag=-1;
//    for(int i=l;i<=r;i++){
//        if(in[i]==pre[t]){
//            flag=i;
//            break;
//        }
//    }
//    if(flag==-1) return;
//    root=new node(in[flag]);
//    t++;
//    if(flag>l) builttree(l,flag-1,t,root->l);
//    if(flag<r) builttree(flag+1,r,t,root->r);
//
//}
//void preorder(node *root){
//    if(root!=NULL)
//    {
//        post[k++]=root->value;
//        preorder(root->l);
//        preorder(root->r);
//
//    }
//}
//void inorder(node *root){
//    if(root!=NULL)
//    {
//        inorder(root->l);
//        post[k++]=root->value;
//        inorder(root->r);
//
//    }
//}
//void postorder(node *root){
//    if(root!=NULL)
//    {
//        postorder(root->l);
//        postorder(root->r);
//        post[k++]=root->value;
//    }
//}


#define MAX 1000000
int tree[MAX<<2];
void pushup(int rt){ //向上维护结点
    tree[rt]=tree[rt<<1]+tree[rt<<1|1];
}
void update(int rt,int i,int val,int l,int r)
{
    if(r==l) {
        tree[rt]+=val;
        return;
    }
    if(i<=mid){
        update(ls,i,val,l,mid);
    }
    else {
        update(rs,i,val,mid+1,r);
    }
    pushup(rt);
}
int query(int rt,int l,int r,int k){
    if(l==r) return l;
    if(tree[rt<<1|1]>=k)
        return query(rs,mid+1,r,k);
    else return query(ls,l,mid,k-tree[rt<<1|1]);
}
int main(){
    int t;
    cin>>t;
    while (t--) {
        int n,m;
        cin>>n>>m;
        mms(tree,0);
        for(int i=1;i<=n;i++){
            int x;
            cin>>x;
            update(1,x,1,1,MAX);
        }
        while (m--) {
            string s;
            int x;
            cin>>s>>x;
            if(s[0]=='q') {
                cout<<query(1,1,MAX,x)<<endl;
            }
            else if(s[0]=='i'){
                update(1,x,1,1,MAX);
            }
            else {
                update(1,x,-1,1,MAX);
            }
        }
    }
}
/*
*@Author:   GuoJinlong
*@Language: C++
*/
//#include <bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<list>
#include<set>
#include<iomanip>
#include<cstring>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<cassert>
#include<sstream>
#include<algorithm>
using namespace std;
const int mod=1e9+7;
typedef long long  ll;
#define ls (rt<<1)
#define rs (rt<<1|1)
#define mid (l+r)/2
#define mms(x, y) memset(x, y, sizeof x)
#define over(i,s,t) for(register long long i=s;i<=t;++i)
#define lver(i,t,s) for(register long long i=t;i>=s;--i)
const int MAXN = 305;
const int INF = 0x3f3f3f3f;
const int N=5e4+7;
const int maxn=1e5+5;
const double EPS=1e-10;
const double Pi=3.1415926535897;
//inline double max(double a,double b){
//    return a>b?a:b;
//}
//inline double min(double a,double b){
//    return a<b?a:b;
//}
 
int xd[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int yd[8] = {1, 0, -1, 0, -1, 1, -1, 1};
 
//void Fire(){
//    queue<node> p;
//    p.push({fx,fy,0});
//    memset(fire, -1, sizeof(fire));
//    fire[fx][fy]=0;
//    while(!p.empty()){
//        node temp=p.front();
//        p.pop();
//        for(int i=0;i<8;i++){
//            int x=temp.x+xd[i];
//            int y=temp.y+yd[i];
//            if(x<0||x>=n||y<0||y>=m||fire[x][y]!=-1){
//                continue;
//            }
//            fire[x][y]=temp.val+1;
//            p.push({x,y,temp.val+1});
//        }
//    }
//}
//int bfs(){
//    queue<node> p;
//    memset(vis, 0, sizeof(vis));
//    p.push({sx,sy,0});
//    while (!p.empty()) {
//        node temp=p.front();
//        vis[temp.x][temp.y]=1;
//        p.pop();
//        for(int i=0;i<4;i++){
//            int x=temp.x+xd[i];
//            int y=temp.y+yd[i];
//            if(x<0||x>=n||y<0||y>=m)  continue;
//            if(x==ex&&y==ey&&temp.val+1<=fire[x][y]) return temp.val+1;
//            if(vis[x][y]||temp.val+1>=fire[x][y]||a[x][y]=='#') continue;
//            p.push({x,y,temp.val+1});
//        }
//    }
//    return -1;
//}

//一维哈希
//int n;
//string s;
//int bas=131;
//typedef unsigned long long ull;
//const ull mod1=100001651;
//ull a[100010];
//ull Hash(string s){
//    ll ans=0;
//    for(int i=0;i<s.size();i++){
//        ans*=bas;
//        ans+=int(s[i]);
//        ans%=mod1;
//    }
//    return ans;
//}

//二维哈希
//using lom=unsigned long long ;
//const lom bas1=131,bas2=233;
//const int M=505;
//int n,m;
//char a[M][M];
//lom _hash[M][M];
//lom p1[M],p2[M];
//
//
//void init(){
//    p1[0]=1;
//    p2[0]=1;
//    for(int i=1;i<=505;i++){
//        p1[i]=p1[i-1]*bas1;
//        p2[i]=p2[i-1]*bas2;
//
//    }
//}
//void Hash(){
//    _hash[0][0]=_hash[0][1]=_hash[1][0]=0;
//    for(int i=1;i<=n;i++){    //前缀和
//        for(int j=1;j<=m;j++){
//            _hash[i][j]=_hash[i][j-1]*bas1+a[i][j]-'a';
//        }
//    }
//    for(int i=1;i<=n;i++){   //二维前缀和
//        for(int j=1;j<=m;j++){
//            _hash[i][j]+=_hash[i-1][j]*bas2;
//        }
//    }
//
//}


//int pre[1010];
//int in[1010];
//int post[1010];
//int k;
//struct node{
//    int value;
//    node *l,*r;
//    node (int value=0,node *l=NULL,node *r=NULL):value(value),l(l),r(r){}
//};
//void builttree(int l,int r,int &t,node * &root){
//    int flag=-1;
//    for(int i=l;i<=r;i++){
//        if(in[i]==pre[t]){
//            flag=i;
//            break;
//        }
//    }
//    if(flag==-1) return;
//    root=new node(in[flag]);
//    t++;
//    if(flag>l) builttree(l,flag-1,t,root->l);
//    if(flag<r) builttree(flag+1,r,t,root->r);
//
//}
//void preorder(node *root){
//    if(root!=NULL)
//    {
//        post[k++]=root->value;
//        preorder(root->l);
//        preorder(root->r);
//
//    }
//}
//void inorder(node *root){
//    if(root!=NULL)
//    {
//        inorder(root->l);
//        post[k++]=root->value;
//        inorder(root->r);
//
//    }
//}
//void postorder(node *root){
//    if(root!=NULL)
//    {
//        postorder(root->l);
//        postorder(root->r);
//        post[k++]=root->value;
//    }
//}


int t,n,m,x;
string str;
vector<int>vt;

signed main(){
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin>>t;
    while(t--){
        vt.clear();
        cin>>n>>m;
        for(int i=1;i<=n;i++){
            cin>>x;
            vt.push_back(x);
        }
        sort(vt.begin(),vt.end());
        for(int i=1;i<=m;i++){
            cin>>str>>x;
            if(str[0]=='q'){
                printf("%d\n",vt[vt.size()-x]);
            }
            if(str[0]=='i'){
                vt.insert(lower_bound(vt.begin(),vt.end(),x),x);
            }
            if(str[0]=='d') vt.erase(lower_bound(vt.begin(),vt.end(),x));
        }
    }
    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郭晋龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值