The Stream of Corning 2(线段树)


#pragma GCC optimize(2)
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,m;
const int maxn = 1e6+1000;
int t[maxn];
struct node{
    int t;
    int val;
    int type;
};
node p[maxn];
char ss[1<<17],*A=ss,*B=ss;
inline char gc(){if(A==B){B=(A=ss)+fread(ss,1,1<<17,stdin);if(A==B)return EOF;}return*A++;}
template<class T>inline void cinn(T&x){
    static char c;static int y;
    for(c=gc(),x=0,y=1;c<48||57<c;c=gc())if(c=='-')y=-1;
    for(;48<=c&&c<=57;c=gc())x=((x+(x<<2))<<1)+(c^'0');
    x*=y;
}
//void init_hash(){
//    for(int i = 1;i<=n;++i){
//        t[i] = p[i].val;
//    }
//    sort(t+1,t+n+1);
//    m = unique(t+1,t+n+1)-(t+1);
//}
//int hashs(int x){
//    return lower_bound(t+1,t+1+m,x)-t;
//}
int c[maxn<<4];
void build(int k,int l,int r){
    if(l==r){
        c[k] = 0;
        return;
    }
    int mid = (l+r)>>1;
    build(k<<1,l,mid);
    build(k<<1|1,mid+1,r);
    c[k] = 0;
}
bool cmp(const node &a, const node &b){
    if(a.t==b.t){
        return a.type<b.type;
    }
    return a.t<b.t;
}
void update(int k,int l,int r,int pos,int val){
    if(l==r){
       // cout<<"l"<<l<<endl;
        c[k]+=val;
        return;
    }
    int mid = (l+r)>>1;
    if(pos<=mid){
        update(k<<1,l,mid,pos,val);
    }
    else{
        update(k<<1|1,mid+1,r,pos,val);
    }
    c[k] = c[k<<1]+c[k<<1|1];
}
int query(int k,int l,int r,int num){
    if(num>c[k])return -1;
    if(l==r&&c[k]>=num){
        //cout<<"l"<<num<<endl;
        return l;
    }
    int mid = (l+r)>>1;
    if(num<=c[k<<1]){
   //     cout<<l<<" "<<r<<endl;
        return query(k<<1,l,mid,num);
    }
    else{
       // cout<<l<<" "<<r<<endl;
        return query(k<<1|1,mid+1,r,num-c[k<<1]);
    }
}
int main(){
    int t;
    cinn(t);
    int cas = 1;
    while(t--){
        int q;
        n = 0;
        cinn(q);
        int maxs = 0;
        for(int i = 0;i<q;++i){
            int op;
            int st,val = 0,ed;
            int k;
            cinn(op);
            if(op==1){
                cinn(st);
                cinn(val);cinn(ed);
                p[++n].t = st;
                p[n].val = val;
                p[n].type = 1;
                p[++n].t = ed;
                p[n].val = val;
                p[n].type = 3;
            }
            else{
                cinn(st);cinn(k);
                p[++n].t = st;
                p[n].val = k;
                p[n].type = 2;
            }
            maxs = max(maxs,val);
        }
//        init_hash();
        build(1,1,maxs);
        sort(p+1,p+1+n,cmp);
        printf("Case %d:\n",cas++);
        //cout<<1<<":"<<endl;
        for(int i = 1;i<=n;++i){
            int pos = 0;
            //cout<<p[i].t<<" "<<p[i].val<<" "<<p[i].type<<endl;

            if(p[i].type==1){
//                pos = hashs(p[i].val);
                //cout<<pos<<endl;
                update(1,1,maxs,p[i].val,1);
            }
            if(p[i].type==3){
//                pos = hashs(p[i].val);
                update(1,1,maxs,p[i].val,-1);
            }
            if(p[i].type==2){
                printf("%d\n",query(1,1,maxs,p[i].val));
            }
            //cout<<"all"<<c[1]<<endl;
        }

    }
}

/*
1
10
1 1 10 7 1 2 9 4 2 4 1 1 5 2 6 2 6 2
2 7 2
1 8 2 20 1 9 1 15 1 10 3 13 2 11 3

 1
11
 1 1 1 3
1 1 10 7 1 2 9 4 2 4 1 1 5 2 6 2 6 2
2 7 2
1 8 2 20 1 9 1 15 1 10 3 13 2 11 3

 */

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值