这个题离散化有点蛋疼。。。
代码:
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<algorithm> #include<string> #include<iomanip> #pragma comment(linker, "/STACK:1024000000,1024000000") #include<vector> #include<set> #include<map> #include<queue> #include<list> using namespace std; typedef long long LL; typedef unsigned long long ULL; #define rep(i,k,n) for(int i=(k);i<=(n);i++) #define red(i,k,n) for(int i=(k);i>=(n);i--) #define sqr(x) ((x)*(x)) #define clr(x,y) memset((x),(y),sizeof(x)) #define mod 1000000007 #define MAX(a,b) ((a)>(b)?(a):(b)) #define MIN(a,b) ((a)>(b)?(b):(a)) const int maxn = 400010; struct opp { bool type; int de; } op[200010]; int a[maxn],n,N,acnt; int r[maxn]; bool cmp(int x,int y) { return a[x]<a[y]; } void dis() { rep(i,1,acnt)r[i]=i; sort(r+1,r+1+acnt,cmp); int pre=a[r[1]]; N=a[r[1]]=1; rep(i,2,acnt) { if(a[r[i]]==pre)a[r[i]]=a[r[i-1]]; else { pre=a[r[i]]; N=a[r[i]]=a[r[i-1]]+1; } } } struct Bit { int bit[maxn]; void cl() { clr(bit,0); } void add(int k,int v) { for(int i=k; i<=N; i+=i&-i)bit[i]+=v; } int sum(int k) { int ret=0; for(int i=k; i>0; i-=i&-i)ret+=bit[i]; return ret; } } BL,BR; int main() { //#define LOCAL #ifdef LOCAL freopen("e:\\read.txt","r",stdin); //freopen("e:\\write.txt","w",stdout); #endif int ii=0; while(~scanf("%d",&n)) { acnt=0; rep(i,1,n) { int t; scanf("%d",&t); op[i].type=t; if(t) { scanf("%d",&op[i].de); } else { acnt+=2; scanf("%d",&t); a[acnt-1]=t; a[acnt]=t+acnt/2; } } dis(); BL.cl(); BR.cl(); int s=1; printf("Case #%d:\n",++ii); rep(i,1,n) { if(op[i].type) { int k=op[i].de; BL.add(a[k*2-1],-1); BR.add(a[k*2],-1); } else { printf("%d\n",BR.sum(a[2*s])-BL.sum(a[2*s-1]-1)); BL.add(a[2*s-1],1); BR.add(a[2*s],1); s++; } } } return 0; }