// AC one more times
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define endl '\n'
#define all(x) (x).begin(), (x).end()
typedef pair<int, int> pii;
const int N = 1e5 + 10;
struct segtree
{
int s;
}seg[N * 4];
int n;
vector<int> vx;
pii a[N];
multiset<int> s;
void update(int id)
{
seg[id].s = seg[id * 2].s + seg[id * 2 + 1].s;
}
void change(int id, int l, int r, int pos, int val)
{
if(l == r)
{
seg[id].s = seg[id].s + val;
return;
}
int mid = (l + r) >> 1;
if(pos <= mid)
change(id * 2, l, mid, pos, val);
else
change(id * 2 + 1, mid + 1, r, pos, val);
update(id);
}
int query1(int id, int l, int r, int ql, int qr)
{
if(ql > qr) return 0;
if(l == ql && r == qr)
{
return seg[id].s;
}
int mid = (l + r) >> 1;
if(qr <= mid)
return query1(id * 2, l, mid, ql, qr);
else if(ql > mid)
return query1(id * 2 + 1, mid + 1, r, ql, qr);
else
return query1(id * 2, l, mid, ql, mid) +
query1(id * 2 + 1, mid + 1, r, mid + 1, qr);
}
int query2(int id, int l, int r, int x)
{
if(l == r)
{
return l;
}
int mid = (l + r) >> 1;
if(seg[id * 2].s >= x)
return query2(id * 2, l, mid, x);
else
return query2(id * 2 + 1, mid + 1, r, x - seg[id * 2].s);
}
void solve()
{
cin>>n;
for(int i = 1; i <= n; i++)
{
cin>>a[i].fi>>a[i].se;
if(a[i].fi != 4)
vx.pb(a[i].se);
}
sort(all(vx));
vx.erase(unique(vx.begin(), vx.end()), vx.end());
for(int i = 1; i <= n; i++)
{
int opt = a[i].fi, x = a[i].se;
x = lower_bound(vx.begin(), vx.end(), x) -vx.begin() + 1;
if(opt == 1)
{
change(1, 1, n, x, 1);
s.insert(vx[x - 1]);
}
else if(opt == 2)
{
change(1, 1, n, x, -1);
s.erase(s.find(vx[x - 1]));
}
else if(opt == 3)
cout<<query1(1, 1, n, 1, x - 1) + 1<<endl;
else if(opt == 4)
cout<<vx[query2(1, 1, n, a[i].se) - 1]<<endl;
else if(opt == 5)
cout<<*(--s.lower_bound(vx[x - 1]))<<endl;
else
cout<<*(s.upper_bound(vx[x - 1]))<<endl;
}
}
int main()
{
std::ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr);
int TC = 1;
//cin >> TC;
for(int tc = 1; tc <= TC; tc++)
{
//cout << "Case #" << tc << ": ";
solve();
}
return 0;
}
标签:return,int,线段,mid,seg,权值,vx,id,模板
From: https://www.cnblogs.com/magicat/p/17360427.html