ZOJ 1112 Dynamic Rankings【动态区间第K大,整体二分】
题目链接:
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1112
题意:
求动态区间第K大。
分析:
把修改操作看成删除与增加,对所有操作进行整体二分。
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
#define pr(x) cout << #x << ": " << x << " "
#define pl(x) cout << #x << ": " << x << endl;
const int maxn = 50000 + 5, maxq = 20000 + 5, oo = 0x3f3f3f3f;
int n, m, tot;
int a[maxn];
struct Query{
int x, y, k;
int id, type;
}q[maxn + maxq], q1[maxn + maxq], q2[maxn + maxq];
int bit[maxn];
int ans[maxq];
void add(int i, int x)
{
while(i <= n){
bit[i] += x;
i += i & -i;
}
}
int sum(int i)
{
int ans = 0;
while(i > 0){
ans += bit[i];
i -= i & -i;
}
return ans;
}
void solve(int ql, int qr, int l, int r)
{
if(ql > qr) return ;
if(l == r){
for(int i = ql; i <= qr; i++){
if(q[i].type == 2) ans[q[i].id] = l;
}
return ;
}
int t1 = 0, t2 = 0;
int mid = l + r >> 1;
// pl(mid);
for(int i = ql; i <= qr; i++){
if(q[i].type == 1){
if(q[i].x <= mid){
add(q[i].id, q[i].y);
q1[t1++] = q[i];
}else q2[t2++] = q[i];
}else{
int tmp = sum(q[i].y) -sum(q[i].x - 1);
if(tmp >= q[i].k) q1[t1++] = q[i];
else {
q[i].k -= tmp;
q2[t2++] = q[i];
}
}
}
for(int i = 0; i < t1; i++){
if(q1[i].type == 1) add(q1[i].id, -q1[i].y);
}
for(int i = 0; i < t1; i++) q[ql + i] = q1[i];
for(int i = 0; i < t2; i++) q[t1 + i + ql] = q2[i];
solve(ql, ql + t1 - 1, l, mid);
solve(ql + t1, qr, mid + 1, r);
}
int main (void)
{
int x;scanf("%d", &x);
while(x--){
scanf("%d%d", &n, &m);
tot = 0;
memset(bit, 0, sizeof(bit));
for(int i = 1; i <= n; i++){
scanf("%d", &a[i]);
q[++tot] = (Query){a[i], 1, oo, i, 1};
}
char s[2];
int x, y, k;
int cnt = 0;
for(int i = 1; i <= m; i++){
scanf("%s%d%d", s, &x, &y);
if(s[0] == 'Q'){
scanf("%d", &k);
q[++tot] = (Query){x, y, k, ++cnt, 2};
}else{
q[++tot] = (Query){a[x], -1, oo, x, 1};
q[++tot] = (Query){y, 1, oo, x, 1};
}
}
solve(1, tot, 0, oo);
for(int i = 1; i <= cnt; i++){
printf("%d\n", ans[i]);
}
}
return 0;
}
ZOJ 1112 Dynamic Rankings【动态区间第K大,整体二分】的更多相关文章
- 51nod 1175 区间第k大 整体二分
题意: 一个长度为N的整数序列,编号0 - N - 1.进行Q次查询,查询编号i至j的所有数中,第K大的数是多少. 分析: 仅仅就是一道整体二分的入门题而已,没听说过整体二分? 其实就是一个分治的函数 ...
- ZOJ2112 Dynamic Rankings 动态区间第K最值 平方分割
有了上一题的经验(POJ的静态区间第K最值)再解决这道题就轻松多了 空间5256KB,时间3330ms,如果把动态开点的平衡树换成数组模拟的话应该会更快 之所以选择了平方分割而不是树套树,不仅是所谓趁 ...
- ZOJ2112--Dynamic Rankings (动态区间第k大)
Dynamic Rankings Time Limit: 10 Seconds Memory Limit: 32768 KB The Company Dynamic Rankings has ...
- ZOJ 2112 Dynamic Rankings(动态区间第 k 大+块状链表)
题目大意 给定一个数列,编号从 1 到 n,现在有 m 个操作,操作分两类: 1. 修改数列中某个位置的数的值为 val 2. 询问 [L, R] 这个区间中第 k 大的是多少 n<=50,00 ...
- hdu5412(动态区间第k大)
CRB and Queries Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
- zoj 2112 Dynamic Rankings 动态第k大 线段树套Treap
Dynamic Rankings Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/show ...
- 【BZOJ】1901: Zju2112 Dynamic Rankings(区间第k小+树套树)
http://www.lydsy.com/JudgeOnline/problem.php?id=1901 这题调了我相当长的时间,1wa1a,我是第一次写树套树,这个是树状数组套splay,在每个区间 ...
- 动态区间第K大
整体二分. 主要需要注意的一点是,对于每个删除操作,若删除操作被算入贡献,则最开始的插入操作也一定会被算入,所以不必担心删除删错. #include<cstdio> #include< ...
- 【XSY2720】区间第k小 整体二分 可持久化线段树
题目描述 给你你个序列,每次求区间第\(k\)小的数. 本题中,如果一个数在询问区间中出现了超过\(w\)次,那么就把这个数视为\(n\). 强制在线. \(n\leq 100000,a_i<n ...
随机推荐
- 前台解析json的方法
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,是理想的数据交换格式.同时,JSON是 JavaScript 原生格式,这意 ...
- SQLyog连接数据库 提示错误plugin caching_sha2_password could not be loaded
1.打开mysql cmd 2.执行语句 ALTER USER 'root'@'localhost' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER; # ...
- python virtualenv学习
补充:在开发Python应用程序的时候,系统安装的Python3只有一个版本:3.4.所有第三方的包都会被pip安装到Python3的site-packages目录下. virtualenv就是 ...
- c++ 计算彩票中奖概率
操作方法: 输入两个数字,第一个数字是备选总数,第二个数字是选择总数,然后返回中将概率. 可以投注多次,结束的时候返回总的中将概率. #include <iostream> using n ...
- [LUOGU] 1892 团伙
题目描述 1920年的芝加哥,出现了一群强盗.如果两个强盗遇上了,那么他们要么是朋友,要么是敌人.而且有一点是肯定的,就是: 我朋友的朋友是我的朋友: 我敌人的敌人也是我的朋友. 两个强盗是同一团伙的 ...
- Python 模块和包的概念
模块&包(* * * * *) 模块(modue)的概念: 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护. 为了编写可维护的代码,我们把很多函 ...
- Codeforces C. Sonya and Problem Wihtout a Legend(DP)
Description Sonya was unable to think of a story for this problem, so here comes the formal descript ...
- 牛客网暑期ACM多校训练营(第六场) J Heritage of skywalkert(数论, eth_element)
链接: https://www.nowcoder.com/acm/contest/144/J 题意: 给定一个函数, 求它n次结果中任意两次的lcm最大值 分析: 首先要看出这个函数并没有什么含义, ...
- BZOJ 4809: 皇后
题目大意: n皇后问题,有些格子不能放. 题解: 直接暴力,并不用加优化就能过. 代码: #include<cstdio> using namespace std; int cc,n,an ...
- Java-获取一个类的父类
如何使用代码获取一个类的父类 package com.tj; public class MyClass implements Cloneable { public static void main(S ...