题目链接: BZOJ - 3110

题目分析

这道题是一道树套树的典型题目,我们使用线段树套线段树,一层是区间线段树,一层是权值线段树。一般的思路是外层用区间线段树,内层用权值线段树,但是这样貌似会很难写。多数题解都使用了外层权值线段树,内层区间线段树,于是我就这样写了。每次插入会在 logn 棵线段树中一共建 log^2(n) 个结点,所以空间应该开到 O(nlog^2(n)) 。由于这道题查询的是区间第 k 大,所以我们存在线段树中的数值是输入数值的相反数(再加上 n 使其为正数),这样查第 k 小就可以了。在查询区间第 k 大值的时候,我们用类似二分的方法,一层一层地逼近答案。

写代码的时候出现的错误:在每一棵区间线段树中修改数值的时候,应该调用的是像 Insert(Lc[x], 1, n, l, r) 这样子,但我经常写成 Insert(x << 1, s, t, l, r) 之类的。注意!

代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm> using namespace std; const int MaxN = 100000 + 5, MaxM = 100000 * 16 * 16 + 5; int n, m, f, a, b, c, Index, Ans;
int Root[MaxN * 4], Lc[MaxM], Rc[MaxM], Sum[MaxM], Lazy[MaxM]; inline int gmin(int a, int b) {
return a < b ? a : b;
}
inline int gmax(int a, int b) {
return a > b ? a : b;
} int Get(int x, int s, int t, int l, int r) {
if (l <= s && r >= t) return Sum[x];
int p = 0, q = 0, m = (s + t) >> 1;
if (l <= m) p = Get(Lc[x], s, m, l, r);
if (r >= m + 1) q = Get(Rc[x], m + 1, t, l, r);
return (p + q + Lazy[x] * (gmin(t, r) - gmax(s, l) + 1));
} int GetKth(int l, int r, int k) {
int s = 1, t = n * 2, m, x = 1, Temp;
while (s != t) {
m = (s + t) >> 1;
if ((Temp = Get(Root[x << 1], 1, n, l, r)) >= k) {
t = m; x = x << 1;
}
else {
s = m + 1; x = x << 1 | 1; k -= Temp;
}
}
return s;
} void Insert(int &x, int s, int t, int l, int r) {
if (x == 0) x = ++Index;
if (l <= s && r >= t) {
Sum[x] += t - s + 1;
++Lazy[x];
return;
}
int m = (s + t) >> 1;
if (l <= m) Insert(Lc[x], s, m, l, r);
if (r >= m + 1) Insert(Rc[x], m + 1, t, l, r);
Sum[x] = Sum[Lc[x]] + Sum[Rc[x]] + Lazy[x] * (t - s + 1);
} void Add(int l, int r, int Num) {
int s = 1, t = n * 2, m, x = 1;
while (s != t) {
Insert(Root[x], 1, n, l, r);
m = (s + t) >> 1;
if (Num <= m) {
t = m;
x = x << 1;
}
else {
s = m + 1;
x = x << 1 | 1;
}
}
Insert(Root[x], 1, n, l, r);
} int main()
{
scanf("%d%d", &n, &m);
Index = 0;
for (int i = 1; i <= m; ++i) {
scanf("%d%d%d%d", &f, &a, &b, &c);
if (f == 1) {
c = -c + n + 1;
Add(a, b, c);
}
else {
Ans = GetKth(a, b, c);
Ans = -Ans + n + 1;
printf("%d\n", Ans);
}
}
return 0;
}

  

[BZOJ 3110] [Zjoi2013] K大数查询 【树套树】的更多相关文章

  1. BZOJ.3110.[ZJOI2013]K大数查询(整体二分 树状数组/线段树)

    题目链接 BZOJ 洛谷 整体二分求的是第K小(利用树状数组).求第K大可以转为求第\(n-K+1\)小,但是这样好像得求一个\(n\). 注意到所有数的绝对值\(\leq N\),将所有数的大小关系 ...

  2. BZOJ 3110: [Zjoi2013]K大数查询 [树套树]

    3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 6050  Solved: 2007[Submit][Sta ...

  3. 树套树专题——bzoj 3110: [Zjoi2013] K大数查询 &amp; 3236 [Ahoi2013] 作业 题解

    [原题1] 3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec  Memory Limit: 512 MB Submit: 978  Solved: 476 Descri ...

  4. bzoj 3110: [Zjoi2013]K大数查询 树状数组套线段树

    3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 1384  Solved: 629[Submit][Stat ...

  5. BZOJ 3110: [Zjoi2013]K大数查询( 树状数组套主席树 )

    BIT+(可持久化)权值线段树, 用到了BIT的差分技巧. 时间复杂度O(Nlog^2(N)) ---------------------------------------------------- ...

  6. BZOJ 3110([Zjoi2013]K大数查询-区间第k大[段修改,在线]-树状数组套函数式线段树)

    3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec   Memory Limit: 512 MB Submit: 418   Solved: 235 [ Submit][ ...

  7. BZOJ 3110 [Zjoi2013]K大数查询(整体二分)

    3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 11654  Solved: 3505[Submit][St ...

  8. bzoj 3110 [Zjoi2013]K大数查询【树套树||整体二分】

    树套树: 约等于是个暴力了.以区间线段树的方式开一棵权值线段树,在权值线段树的每一个点上以动态开点的方式开一棵区间线段树. 结果非常惨烈(时限20s) #include<iostream> ...

  9. BZOJ 3110 [Zjoi2013]K大数查询 ——树套树

    [题目分析] 外层区间线段树,内层是动态开点的权值线段树. SY神犇说树套树注重的是内外层的数据结构的选择问题,果然很重要啊. 动态开点的实现方法很好. [代码] #include <cstdi ...

  10. BZOJ 3110: [Zjoi2013]K大数查询 [整体二分]

    有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c如果是2 a b c形式,表示询问从第a个位置到第b个位置,第C大的数是多少. N ...

随机推荐

  1. [CSS] Animating SVG

    <!DOCTYPE> <html lang='en'> <head> <meta charset='utf-8'> <title>Cospl ...

  2. Android开发报错系列(一),java.lang.NullPointerException,at android.widget.ListView.setupChild

    问题描述:运行代码是报空指针错误,java.lang.NullPointerException,at Android.widget.ListView.setupChild 问题定位:listview控 ...

  3. Maven学习总结——聚合与继承

    一.聚合 如果我们想一次构建多个项目模块,那我们就需要对多个项目模块进行聚合 1.1.聚合配置代码 1 <modules> 2 <module>模块一</module&g ...

  4. inverse 相关设置

    <set name="students" table = "student" inverse="true"> <!-- 指 ...

  5. bootstrap3学习1:响应式布局layout

    在去年的这个时候写过关于bootstrap的相关文章(见:bootstrap2学习1:基本CSS样式),然后就搁置了,原因是因为当时对bootstrap的了解不深,并且当时v2版本对响应式设计的不是非 ...

  6. jquery---点击弹出层

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. vim字符串替换

    vi/vim 中可以使用 :s 命令来替换字符串.以前只会使用一种格式来全文替换,今天发现该命令有很多种写法(vi 真是强大啊,还有很多需要学习),记录几种在此,方便以后查询. :s/vivian/s ...

  8. jquery 银行卡号验证

    具体参考:https://github.com/jondavidjohn/payform 插件js: jquery.payform.js 具体操作 alert($.payform.validateCa ...

  9. Linux软件

    网上下载:Chrome Browser for Linux; sqlite;  WPS; symbol-fonts; 软件中心:Terminator; Code::Blocks IDE;  新立得软件 ...

  10. 【转】iOS-Core-Animation-Advanced-Techniques(三)

    原文: http://www.cocoachina.com/ios/20150105/10827.html 专用图层 复杂的组织都是专门化的--Catharine R. Stimpson 到目前为止, ...