动态开点权值线段树

动态开点权值线段树其实不用离散化,也不用关注负数,因为和负数没关系了

//#define LOCAL
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mem(a, b) memset(a,b,sizeof(a))
#define sz(a) (int)a.size()
#define INF 0x3f3f3f3f
#define DNF 0x7f7f7f7f
#define DBG printf("this is a input\n")
#define fi first
#define se second
#define PiL pair <ll , ll>
#define PLL pair <ll , ll>
#define Pii pair <int , int>
#define mk(a, b) make_pair(a,b)
#define pb push_back
#define LF putchar('\n')
#define SP putchar(' ')
#define p_queue priority_queue
#define CLOSE ios::sync_with_stdio(0); cin.tie(0)

template<typename T>
void read(T &x) {x = 0;char ch = getchar();ll f = 1;while(!isdigit(ch)){if(ch == '-')f *= -1;ch = getchar();}while(isdigit(ch)){x = x * 10 + ch - 48; ch = getchar();}x *= f;}
template<typename T, typename... Args>
void read(T &first, Args& ... args) {read(first);read(args...);}
template<typename T>
void write(T arg) {T x = arg;if(x < 0) {putchar('-'); x =- x;}if(x > 9) {write(x / 10);}putchar(x % 10 + '0');}
template<typename T, typename ... Ts>
void write(T arg, Ts ... args) {write(arg);if(sizeof...(args) != 0) {putchar(' ');write(args ...);}}
using namespace std;
int t;
const int N = 3000050;
int p = 1e7 + 10;
struct Segement_Tree
{
    int root , tot , sum[N], lc[N] , rc[N];
    void push_up (int rt) {
        sum[rt] = sum[lc[rt]] + sum[rc[rt]] ;
    }
    void update (int &rt , int l , int r , int x, int k)
    {
        if (!rt) rt = ++ tot;
        if (!root) root = 1;
        if (l == r)
        {
            sum[rt] += k;
            return ;
        }
        int mid = (l + r) >> 1;
        if (x <= mid)
            update(lc[rt], l, mid, x, k);
        else
            update(rc[rt], mid + 1, r, x, k);
        push_up(rt);
        return ;
    }
    int query (int rt , int l , int r, int ql , int qr)
    {
        if (!rt) return 0;
        if (ql <= l && r <= qr) return sum[rt];
        int mid = (l + r) >> 1 , ans = 0;
        if (ql <= mid) ans += query (lc[rt] , l , mid , ql , qr);
        if (mid <  qr) ans += query (rc[rt] , mid + 1 , r , ql , qr);
        return ans;
    }
    int Kth (int rt , int l , int r, int k)
    {
        if (!rt) return -1;
        if (l == r) return l;
        int mid = (l + r) >> 1;
        if (k <= sum[lc[rt]])
            return Kth (lc[rt], l , mid , k);
        else
            return Kth (rc[rt], mid + 1 , r, k - sum[lc[rt]]);
    }
}tree;
int main()
{
    read (t);
    while (t --)
    {
        int op , x;
        read (op , x);
        if (op == 1)
            tree.update(tree.root, -1e7 , 1e7, x , 1);
        if (op == 2)
            tree.update(tree.root, -1e7 , 1e7, x , -1);
        if (op == 3)
            write(tree.query(tree.root,-1e7,1e7,-1e7,x-1)+1), LF;
        if (op == 4)
            write(tree.Kth(tree.root, -1e7, 1e7, x)), LF;
        if (op == 5)
        {
            int cnt = tree.query(tree.root, -1e7 , 1e7 , -1e7, x - 1);
            write(tree.Kth(tree.root, -1e7 , 1e7 , cnt)), LF;
        }
        if (op == 6)
        {
            int cnt = tree.query(tree.root, -1e7 , 1e7 , -1e7, x) + 1;
            write(tree.Kth(tree.root, -1e7 , 1e7 , cnt)), LF;
        }
    }
}

移位版

//#define LOCAL
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mem(a, b) memset(a,b,sizeof(a))
#define sz(a) (int)a.size()
#define INF 0x3f3f3f3f
#define DNF 0x7f7f7f7f
#define DBG printf("this is a input\n")
#define fi first
#define se second
#define PiL pair <ll , ll>
#define PLL pair <ll , ll>
#define Pii pair <int , int>
#define mk(a, b) make_pair(a,b)
#define pb push_back
#define LF putchar('\n')
#define SP putchar(' ')
#define p_queue priority_queue
#define CLOSE ios::sync_with_stdio(0); cin.tie(0)

template<typename T>
void read(T &x) {x = 0;char ch = getchar();ll f = 1;while(!isdigit(ch)){if(ch == '-')f *= -1;ch = getchar();}while(isdigit(ch)){x = x * 10 + ch - 48; ch = getchar();}x *= f;}
template<typename T, typename... Args>
void read(T &first, Args& ... args) {read(first);read(args...);}
template<typename T>
void write(T arg) {T x = arg;if(x < 0) {putchar('-'); x =- x;}if(x > 9) {write(x / 10);}putchar(x % 10 + '0');}
template<typename T, typename ... Ts>
void write(T arg, Ts ... args) {write(arg);if(sizeof...(args) != 0) {putchar(' ');write(args ...);}}
using namespace std;
int t;
const int N = 5000010;
int p = 1e7 + 10;
struct Segement_Tree
{
    int root , tot, sum[N], lc[N] , rc[N];
    void push_up (int i) {sum[i] = sum[lc[i]] + sum[rc[i]] ;}
    void update (int &rt , int l , int r , int x, int k)
    {
        if (!rt) rt = ++ tot;
        if (!root) root = rt;
        if (l == r)
        {
            sum[rt] += k;
            return ;
        }
        int mid = (l + r) >> 1;
        if (x <= mid) update (lc[rt] , l , mid , x, k);
        else update (rc[rt], mid + 1, r , x , k);
        push_up(rt);
    }
    int query (int rt , int l , int r, int ql , int qr)
    {
        if (!rt) return 0;
        if (ql <= l && r <= qr) return sum[rt];
        int mid = (l + r) >> 1 , ans = 0;
        if (ql <= mid) ans += query (lc[rt] , l , mid , ql , qr);
        if (mid <  qr) ans += query (rc[rt] , mid + 1 , r , ql , qr);
        return ans;
    }
    int Kth (int rt , int l , int r, int k)
    {
        if (l == r) return l - p;
        int mid = (l + r) >> 1;
        if (k <= sum[lc[rt]])
            return Kth (lc[rt], l , mid , k);
        else
            return Kth (rc[rt], mid + 1 , r, k - sum[lc[rt]]);
    }
}tree;
int main()
{
    read (t);
    while (t --)
    {
        int op , x;
        read (op , x);
        x += p;
        if (op == 1)
            tree.update(tree.root, 1 , 3e7, x , 1);
        if (op == 2)
            tree.update(tree.root, 1 , 3e7, x , -1);
        if (op == 3)
            write(tree.query(tree.root, 1, 3e7 , 1, x - 1) + 1), LF;
        if (op == 4)
            write(tree.Kth(tree.root, 1, 3e7, x - p)), LF;
        if (op == 5)
        {
            int cnt = tree.query(tree.root, 1 , 3e7 , 1, x - 1);
            write(tree.Kth(tree.root, 1 , 3e7 , cnt)), LF;
        }
        if (op == 6)
        {
            int cnt = tree.query(tree.root, 1 , 3e7 , 1, x) + 1;
            write(tree.Kth(tree.root, 1 , 3e7 , cnt)), LF;
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值