BZOJ3110【线段树】

写的是区间线段树套权值线段树.似乎比反过来写要麻烦.SAD.

为了节省内存.内层的线段树要动态开点.

/* I will wait for you */
 
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <string>
#define make make_pair
#define fi first
#define se second
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef map<int, int> mii;
 
const int maxn = 100010;
const int maxm = 1010;
const int maxs = 26;
const int inf = 0x3f3f3f3f;
const int P = 1000000007;
const double eps = 1e-6;
 
inline ll read()
{
    ll x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch > '9')
          f = (ch == '-' ? -1 : 1), ch = getchar();
    while (ch >= '0' && ch <= '9')
          x = x * 10 + ch - '0', ch = getchar();
    return x * f;
}
 
struct Tree
{
    int sum; Tree *ls, *rs;
} *null, *_A[4 * maxn], *_C[4 * maxn]; 
 
vector<pair<Tree*, int> > _vector;
 
int n, m; 
 
void init()
{
    null = new Tree(), null -> sum = 0;
    null -> ls = null -> rs = null;
     
    for (int i = 1; i <= n << 2; i++)
        _A[i] = _C[i] = null;
}
 
Tree *_insert(Tree *o, int l, int r, int cnt, int num)
{
    if (o == null) o = new Tree(), *o = *null;
     
    if (l != r) {
        int mid = (l + r) >> 1;
        if (num <= mid)
            o -> ls = _insert(o -> ls, l, mid, cnt, num);
        else
            o -> rs = _insert(o -> rs, mid + 1, r, cnt, num);
    }
     
    o -> sum += cnt;
    return o;
}
 
void insert(int o, int l, int r, int x, int y, int num)
{
    if (x == l && y == r)
        _C[o] = _insert(_C[o], 1, n, 1, num);
    else {
        int mid = (l + r) >> 1;
        _A[o] = _insert(_A[o], 1, n, (y - x + 1), num);
         
        if (x <= mid) 
            insert(2 * o, l, mid, x, min(mid, y), num);
        if (y > mid)
            insert(2 * o + 1, mid + 1, r, max(mid + 1, x), y, num);
    }
}
 
void _query(int o, int l, int r, int x, int y)
{
    _vector.push_back(make(_C[o], y - x + 1));
     
    if(l == x && r == y) 
        _vector.push_back(make(_A[o], 1));
    else {
        int mid = (l + r) >> 1;
        if (x <= mid)
            _query(2 * o, l, mid, x, min(mid, y));
        if (y > mid)
            _query(2 * o + 1, mid + 1, r, max(mid + 1, x), y);
    }
}
 
int query(int l, int r, int k)
{
    _vector.clear(), _query(1, 1, n, l, r); 
    int size = _vector.size(), _l = 1, _r = n;
     
    while (_l != _r) {
        int tmp = 0, mid = (_l + _r) / 2;
        for (int i = 0; i < size; i++) {
            Tree *_tree = _vector[i].fi;
            int mul = _vector[i].se;
            tmp += mul * _tree -> rs -> sum;
        }
         
        for (int i = 0; i < size; i++) {
            Tree *&_tree = _vector[i].fi;
            if (k <= tmp) 
                _tree = _tree -> rs, _l = mid + 1;
            else
                _tree = _tree -> ls, _r = mid;
        }
        if (k > tmp) k -= tmp;
    }
     
    return (_l + _r) >> 1;
}
 
int main()
{
    n = read(), m = read(), init();
     
    for (int i = 1; i <= m; i++) {
        int t = read(), a = read();
        int b = read(), c = read(); 
        if (t == 1) insert(1, 1, n, a, b, c);
        if (t == 2) printf("%d\n", query(a, b, c));
    }
 
    return 0;
}

你可能感兴趣的:(BZOJ3110【线段树】)