3110: [Zjoi2013]K大数查询

https://lydsy.com/JudgeOnline/problem.php?id=3110

分析:

  整体二分+线段树。

  两种操作:区间加入一个数,区间询问第k大值。

  如果只有一种操作,我们可以二分答案x,然后把大于x的都加入到线段树中去(区间[l,r]整体+1),然后查询这次询问的区间有多少数(区间[l,r]求和)。

  多种操作的话整体二分就行了,注意到有时间顺序,所以可以按照时间顺序加入和查询。

  注意一下要开longlong。

代码:

 #include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<cctype>
#include<set>
#include<queue>
#include<vector>
#include<map>
#define Root 1, n, 1
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ; int ans[N], n, m;
struct Que{
int ty, l, r, v, id;
bool operator < (const Que &A) const {
return id < A.id;
}
}A[N], tl[N], tr[N];
struct SegmentTree{
LL sum[N << ], tag[N << ];
void pushup(int rt) { sum[rt] = sum[rt << ] + sum[rt << | ]; }
void pushdown(int rt,int len) {
sum[rt << ] += 1ll * (len - len / ) * tag[rt];
sum[rt << | ] += 1ll * (len / ) * tag[rt];
tag[rt << ] += tag[rt];
tag[rt << | ] += tag[rt];
tag[rt] = ;
}
void update(int l,int r,int rt,int L,int R,int v) {
if (L <= l && r <= R) {
tag[rt] += v, sum[rt] += (r - l + ) * v; return ;
}
if (tag[rt]) pushdown(rt, r - l + );
int mid = (l + r) >> ;
if (L <= mid) update(lson, L, R, v);
if (R > mid) update(rson, L, R, v);
pushup(rt);
}
LL query(int l,int r,int rt,int L,int R) {
if (L <= l && r <= R) return sum[rt];
if (tag[rt]) pushdown(rt, r - l + );
int mid = (l + r) >> ; LL res = ;
if (L <= mid) res += query(lson, L, R);
if (R > mid) res += query(rson, L, R);
return res;
}
}T; void solve(int l,int r,int Head,int Tail) {
if (Head > Tail) return ;
if (l == r) {
for (int i = Head; i <= Tail; ++i)
if (A[i].ty == ) ans[A[i].id] = l;
return ;
}
int mid = (l + r + ) >> , cl = , cr = ;
for (int i = Head; i <= Tail; ++i) {
if (A[i].ty == ) {
if (A[i].v >= mid) T.update(Root, A[i].l, A[i].r, ), tr[++cr] = A[i];
else tl[++cl] = A[i];
}
else {
LL t = T.query(Root, A[i].l, A[i].r);
if (t >= A[i].v) tr[++cr] = A[i];
else A[i].v -= t, tl[++cl] = A[i];
}
}
for (int i = Head; i <= Tail; ++i) if (A[i].ty == && A[i].v >= mid) T.update(Root, A[i].l, A[i].r, -);
for (int i = ; i <= cl; ++i) A[i + Head - ] = tl[i];
for (int i = ; i <= cr; ++i) A[i + Head + cl - ] = tr[i];
solve(l, mid - , Head, Head + cl - );
solve(mid, r, Head + cl, Tail);
}
int main() {
n = read(), m = read(); int Q = ;
for (int i = ; i <= m; ++i) {
A[i].ty = read(), A[i].l = read(), A[i].r = read(), A[i].v = read(), A[i].id = ;
if (A[i].ty == ) A[i].id = ++Q;
}
solve(, n, , m);
for (int i = ; i <= Q; ++i) printf("%d\n",ans[i]);
return ;
}

3110: [Zjoi2013]K大数查询的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

  7. BZOJ 3110 [Zjoi2013]K大数查询 (CDQ分治+树状数组)

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

  8. [BZOJ 3110] [Zjoi2013] K大数查询 【树套树】

    题目链接: BZOJ - 3110 题目分析 这道题是一道树套树的典型题目,我们使用线段树套线段树,一层是区间线段树,一层是权值线段树.一般的思路是外层用区间线段树,内层用权值线段树,但是这样貌似会很 ...

  9. bzoj 3110 [Zjoi2013]K大数查询——线段树套线段树(标记永久化)

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3110 第一道线段树套线段树! 第一道标记永久化! 为什么为什么写了两个半小时啊…… 本想线段 ...

随机推荐

  1. Python常用库之二:Pandas

    Pandas是用于数据操纵和分析,建立在Numpy之上的.Pandas为Python带来了两种新的数据结构:Pandas Series和Pandas DataFrame,借助这两种数据结构,我们能够轻 ...

  2. 通过iframe标签绕过csp

    iframe.php代码如下: allow-popups开启时,window.open就可以打开新的窗口. 看csp规则,默认是在当前域内,如果这是一个ctf题的话,就很简单了,window.loca ...

  3. 20145216史婧瑶《Java程序设计》第五次实验报告

    20145216 实验五<Java网络编程> 实验内容 1.掌握Socket程序的编写 2.掌握密码技术的使用 3.设计安全传输系统 实验要求 1.基于Java Socket实现安全传输 ...

  4. JNI由浅入深_10_JNI 综合开发

    1.使用ndk-build时如果找不到某个类,可以使用下面两种方法解决: 1.1 进入src目录 D:\project3\JNIAndroid\src>set classpath=D:\proj ...

  5. Node 192.168.248.12:7001 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.

    [root@node00 src]# ./redis-trib.rb add-node --slave --master-id4f6424e47a2275d2b7696bfbf8588e8c4c3a5 ...

  6. Lua库-string库

    string.len(s) string.rep(s,n) string.lower(s) string.upper(s) string.sub(s,i);//截取s第i个开始的后缀 string.s ...

  7. 【CSS-移动端响应式布局详解】

    背景 移动端响应式布局开发主要方案有: 基于rem开发 基于媒体查询 基于弹性盒 基础概念 在讨论响应式布局知识前,先了解下移动端常用基础概念. 逻辑像素(CSS pixels) 浏览器使用的抽象单位 ...

  8. ZooKeeper系列(2)--基于ZooKeeper实现简单的配置中心

    ZooKeeper节点的类型分为以下几类:  1. 持久节点:节点创建后就一直存在,直到有删除操作来主动删除该节点 2. 临时节点:临时节点的生命周期和创建该节点的客户端会话绑定,即如果客户端会话失效 ...

  9. No active profile set, falling back to default profiles: default

    No active profile set, falling back to default profiles: default 这个错误是由于idea没有设置默认启动环境,设置即可

  10. 在阿里云的CentOS 6.5 上面安装 timidity++ 和 ffmpeg(含libmp3lame) 实现命令行将midi转换为mp3

    首先使用yum安装需要的组件 yum install -y automake autoconf libtool gcc gcc-c++ yasm yasm-devel 然后从sourceforge下载 ...