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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-09-08 21:51:57

BZOJ3110【线段树】的相关文章

【BZOJ-3110】K大数查询 整体二分 + 线段树

3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 6265  Solved: 2060[Submit][Status][Discuss] Description 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c如果是2 a b c形式,表示询问从第a个位置到第b个位置,第C大的数是多少. Input 第一行N,M接下来M行,每行形如1 a

BZOJ3110 ZJOI2013 K大数查询 线段树套线段树

题意:给定一个数列,维护:1.在a和b之间插入c  2.询问[a,b]中的第c大 题解: 权值线段树套区间线段树 外层的权值线段树中每个节点如果维护[L,R]这个区间,那么该节点所对应的线段树维护的就是[L,R]这些数在每个区间里出现了几次,也就是说如果外层线段树的某个节点维护[L,R],其所对应的内层线段树中某个节点[l,r]维护的值就是[L,R]这些数在[l,r]这个区间中出现的次数. 最后吐槽一下动态内存+指针版线段树MLE……尼玛我写指针版完全习惯了根本就不会写数组版了QAQ,自己拿数据

【BZOJ3110】【Zjoi2013】K大数查询 树套树 权值线段树套区间线段树

#include <stdio.h> int main() { puts("转载请注明出处谢谢"); puts("http://blog.csdn.net/vmurder/article/details/43020009"); } 题解: 外层权值线段树,内层区间线段树可解. 权值都是1~n,就不用离散化了. 我写了标记永久化. 其它心得神马的: 天生对树形数据结构无爱. 第一次写树套树,终于知道是怎么回事了. (只针对本题) 就是外层每个点都表示了一段

【BZOJ3110】【整体二分+树状数组区间修改/线段树】K大数查询

Description 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c 如果是2 a b c形式,表示询问从第a个位置到第b个位置,第C大的数是多少. Input 第一行N,M 接下来M行,每行形如1 a b c或2 a b c Output 输出每个询问的结果 Sample Input 2 5 1 1 2 1 1 1 2 2 2 1 1 2 2 1 1 1 2 1 2 3 Sample Output 1 2 1 HINT

【BZOJ3110】【codevs1616】K大数查询,权值线段树套普通线段树

Time:2016.05.09 Author:xiaoyimi 转载注明出处谢谢 传送门1 传送门2 思路: 之前没怎么接触过权值线段树(非主席树),这次就当学习了一下吧.一开始还把题意理解错了,我的天啊-- 起初思考了好久,发现不知道怎么处理负数的情况,不过数据里并没有负数? 权值线段树的每个节点表示一个区间[L,R],存储原序列中权值为[L,R]的元素的信息,所以这里的权值线段树每个节点上都是一棵普通线段树,就是负责统计原序列中权值为[L,R]的元素的个数. 每次插入一个相同的值x时就相当于

[poj2104]可持久化线段树入门题(主席树)

解题关键:离线求区间第k小,主席树的经典裸题: 对主席树的理解:主席树维护的是一段序列中某个数字出现的次数,所以需要预先离散化,最好使用vector的erase和unique函数,很方便:如果求整段序列的第k小,我们会想到离散化二分和线段树的做法, 而主席树只是保存了序列的前缀和,排序之后,对序列的前缀分别做线段树,具有差分的性质,因此可以求任意区间的第k小,如果主席树维护索引,只需要求出某个数字在主席树中的位置,即为sort之后v中的索引:若要求第k大,建树时反向排序即可 1 #include

【BZOJ4942】[Noi2017]整数 线段树+DFS(卡过)

[BZOJ4942][Noi2017]整数 题目描述去uoj 题解:如果只有加法,那么直接暴力即可...(因为1的数量最多nlogn个) 先考虑加法,比较显然的做法就是将A二进制分解成log位,然后依次更新这log位,如果最高位依然有进位,那么找到最高位后面的第一个0,将中间的所有1变成0,那个0变成1.这个显然要用到线段树,但是复杂度是nlog2n的,肯定过不去. 于是我在考场上yy了一下,这log位是连续的,我们每次都要花费log的时间去修改一个岂不是很浪费?我们可以先在线段树上找到这段区间

bzoj1798: [Ahoi2009]Seq 维护序列seq 线段树

题目传送门 这道题就是线段树 先传乘法标记再传加法 #include<cstdio> #include<cstring> #include<algorithm> #define LL long long using namespace std; const int M=400010; LL read(){ LL ans=0,f=1,c=getchar(); while(c<'0'||c>'9'){if(c=='-') f=-1; c=getchar();}

Vijos P1066 弱弱的战壕【多解,线段树,暴力,树状数组】

弱弱的战壕 描述 永恒和mx正在玩一个即时战略游戏,名字嘛~~~~~~恕本人记性不好,忘了-_-b. mx在他的基地附近建立了n个战壕,每个战壕都是一个独立的作战单位,射程可以达到无限(“mx不赢定了?!?”永恒[email protected][email protected]). 但是,战壕有一个弱点,就是只能攻击它的左下方,说白了就是横纵坐标都不大于它的点(mx:“我的战壕为什么这么菜”ToT).这样,永恒就可以从别的地方进攻摧毁战壕,从而消灭mx的部队. 战壕都有一个保护范围,同它的攻击