题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3727
题意:输入n,再输入n个操作,操作有四种
Insert X:插入x到序列末尾
query1 L R X:在当前序列中的[l,r]区间找第x小的数。
query2 X:在当前序列中,输出X是第几小的数。
query3 X:找到当前序列中第X小的数是几。
然后输出的是3种query的和。
解题思路:离线主席树。
/* ***********************************************
┆ ┏┓ ┏┓ ┆
┆┏┛┻━━━┛┻┓ ┆
┆┃ ┃ ┆
┆┃ ━ ┃ ┆
┆┃ ┳┛ ┗┳ ┃ ┆
┆┃ ┃ ┆
┆┃ ┻ ┃ ┆
┆┗━┓ 马 ┏━┛ ┆
┆ ┃ 勒 ┃ ┆
┆ ┃ 戈 ┗━━━┓ ┆
┆ ┃ 壁 ┣┓┆
┆ ┃ 的草泥马 ┏┛┆
┆ ┗┓┓┏━┳┓┏┛ ┆
┆ ┃┫┫ ┃┫┫ ┆
┆ ┗┻┛ ┗┻┛ ┆
************************************************ */
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <bitset>
using namespace std;
#define rep(i,a,b) for (int i=(a),_ed=(b);i<=_ed;i++)
#define per(i,a,b) for (int i=(b),_ed=(a);i>=_ed;i--)
#define pb push_back
#define mp make_pair
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define mod 1000000007
#define LL long long
#define ULL unsigned long long
#define MS0(X) memset((X), 0, sizeof((X)))
#define SelfType int
SelfType Gcd(SelfType p,SelfType q){return q==0?p:Gcd(q,p%q);}
SelfType Pow(SelfType p,SelfType q){SelfType ans=1;while(q){if(q&1)ans=ans*p;p=p*p;q>>=1;}return ans;}
#define Sd(X) int (X); scanf("%d", &X)
#define Sdd(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define Sddd(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define reunique(v) v.resize(std::unique(v.begin(), v.end()) - v.begin())
#define all(a) a.begin(), a.end()
#define mem(x,v) memset(x,v,sizeof(x))
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
typedef vector<long long> vll;
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")
const int N = 1e5 + 10;
struct data
{
int id;
int l,r,x;
}data[N*2];
vi v;
inline int getid(int x)
{
return lower_bound(v.begin(),v.end(),x) - v.begin() + 1;
}
void input(int n)
{
char s[10];
for(int i=0;i<n;i++)
{
scanf("%s",s);
if(s[0]=='I')
{
int x = read();
data[i].id = 0;
data[i].x = x;
v.pb(x);
}
else if(s[6]=='1')
{
int a,b,c;
a = read(), b = read(), c = read();
data[i].id = 1;
data[i].l = a, data[i].r = b, data[i].x = c;
}
else if(s[6]=='2')
{
int x = read();
data[i].id = 2;
data[i].x = x;
}
else
{
int x = read();
data[i].id = 3;
data[i].x = x;
}
}
}
struct node
{
int l,r,sum;
}tree[N*20];
int tot,rt[N];
void build(int l,int r,int &x)
{
x = ++tot;
tree[x].sum = 0;
if(l==r) return;
int m = (l+r) >> 1;
build(l,m,tree[x].l);
build(m+1,r,tree[x].r);
}
void update(int l,int r,int &x,int y,int k)
{
x = ++tot;
tree[x] = tree[y]; tree[x].sum++;
if(l==r) return;
int m = (l+r) >> 1;
if(k<=m) update(l,m,tree[x].l,tree[y].l,k);
else update(m+1,r,tree[x].r,tree[y].r,k);
}
int query1(int l,int r,int x,int y,int k) //查找区间第k小的数
{
if(l==r) return l;
int mid = (l+r) >> 1;
int sum = tree[tree[y].l].sum - tree[tree[x].l].sum;
if(k<=sum) return query1(l,mid,tree[x].l,tree[y].l,k);
else return query1(mid+1,r,tree[x].r,tree[y].r,k-sum);
}
int query2(int l,int r,int x,int k) //在当前序列中,输出X是第几小的数。
{
if(l==r) return 1;
int mid = (l+r) >> 1;
if(k<=mid) return query2(l,mid,tree[x].l,k);
else
{
int sum = tree[tree[x].l].sum;
return sum += query2(mid+1,r,tree[x].r,k);
}
}
int query3(int l,int r,int x,int k) //找到当前序列中第X小的数是几
{
if(l==r) return l;
int mid = (l+r) >> 1;
int sum = tree[tree[x].l].sum;
if(sum>=k) return query3(l,mid,tree[x].l,k);
else return query3(mid+1,r,tree[x].r,k-sum);
}
char s[10];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);
int n;
int cas = 1;
while(~scanf("%d",&n))
{
v.clear();
tot = 0;
LL ans1 = 0, ans2 = 0, ans3 = 0;
input(n);
sort(all(v));
v.erase(unique(all(v)),v.end());
int cnt = v.size();
build(1,cnt,rt[0]);
int now = 1;
for(int i=0;i<n;i++)
{
if(data[i].id==0)
{
update(1,cnt,rt[now],rt[now-1],getid(data[i].x));
now++;
}
else if(data[i].id==1)
{
int l = data[i].l, r = data[i].r, x = data[i].x;
ans1 += v[query1(1,cnt,rt[l-1],rt[r],x)-1];
}
else if(data[i].id==2)
{
ans2 += query2(1,cnt,rt[now-1],getid(data[i].x));
}
else
{
ans3 += v[query3(1,cnt,rt[now-1],data[i].x)-1];
}
}
printf("Case %d:\n",cas++);
printf("%I64d\n%I64d\n%I64d\n",ans1,ans2,ans3);
}
return 0;
}