嘟嘟嘟

带修改区间第k大。

然而某谷把数据扩大到了1e5,所以用分块现在只能得50分。

分块怎么做呢?很暴力的。

基本思想还是块内有序,块外暴力统计。

对于修改,直接重排修改的数所在块,时间复杂度O(√nlogn√n)。

对于询问,二分答案,然后在每个块内再二分统计小于mid的数有几个,块外暴力统计,时间复杂度O(m * log1e9 * √nlog√n),所以只能过1e4。

 #include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-;
const int maxn = 1e5 + ;
const int maxb = ;
inline ll read()
{
ll ans = ;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) {last = ch; ch = getchar();}
while(isdigit(ch)) {ans = (ans << ) + (ans << ) + ch - ''; ch = getchar();}
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < ) x = -x, putchar('-');
if(x >= ) write(x / );
putchar(x % + '');
} int n, q, a[maxn];
char c[]; int S, Cnt = , blo[maxn], lb[maxn], rb[maxn];
int b[maxb][maxb];
void init()
{
S = sqrt(n);
Cnt = n % S ? n / S + : n / S;
for(rg int i = ; i <= Cnt; ++i) lb[i] = rb[i - ] + , rb[i] = lb[i] + S - ;
rb[Cnt] = n;
for(rg int i = , j = ; i <= n; ++i) blo[i] = j, j += (i == rb[j]);
for(rg int i = , cb = ; i <= Cnt; ++i, cb = )
{
for(rg int j = lb[i]; j <= rb[i]; ++j) b[i][++cb] = a[j];
sort(b[i] + , b[i] + cb + );
}
}
inline void update(const int& x, const int& k)
{
a[x] = k;
int t = blo[x], cb = ;
for(rg int i = lb[t]; i <= rb[t]; ++i) b[t][++cb] = a[i];
sort(b[t] + , b[t] + cb + );
}
inline int judge(const int& L, const int& R, const int& x, const int& k)
{
int l = blo[L], r = blo[R], ret = ;
if(l == r)
{
for(rg int i = L; i <= R; ++i) ret += (a[i] < x);
return ret < k;
}
for(rg int i = l + ; i < r; ++i)
{
int tp = lower_bound(b[i] + , b[i] + rb[i] - lb[i] + , x) - b[i] - ;
if(tp < ) tp = ;
if(tp > rb[i] - lb[i]) tp = rb[i] - lb[i] + ;
ret += tp;
}
for(rg int i = L; i <= rb[l]; ++i) ret += (a[i] < x);
for(rg int i = lb[r]; i <= R; ++i) ret += (a[i] < x);
return ret < k;
} int main()
{
n = read(), q = read();
for(rg int i = ; i <= n; ++i) a[i] = read();
init();
for(rg int i = ; i <= q; ++i)
{
scanf("%s", c);
if(c[] == 'C')
{
int x = read(), y = read();
update(x, y);
}
else
{
int L = read(), R = read(), k = read();
int l = , r = 1e9;
while(l < r)
{
int mid = (l + r + ) >> ;
if(judge(L, R, mid, k)) l = mid;
else r = mid - ;
}
write(l), enter;
}
}
return ;
}

luogu P2617 Dynamic Rankings(分块,n <= 1e4)的更多相关文章

  1. luogu P2617 Dynamic Rankings && bzoj 1901 (带修改区间第k大)

    链接:https://www.luogu.org/problemnew/show/P2617 思路: 如果直接在主席树上修改的话,每次修改都会对后面所有的树造成影响,一次修改的复杂度就会变成 : n* ...

  2. Luogu P2617 Dynamic Rankings

    带修主席树的模板,因为状态不好所以敲了很长时间,不过写完感觉能更好地理解主席树了. 核心其实就是树状数组套主席树,维护方法不再是以前的那种一步一修改,而是对于树状数组上的每一个点建立一棵权值线段树,然 ...

  3. luogu P2617 Dynamic Rankings(主席树)

    嘟嘟嘟 一句话题意:带修改区间第\(k\)小. 不修改都会,主席树板子.但是有修改就要比较深入的理解主席树了. 众所周知,主席树中以\(i\)为根的线段树维护的是\([1, i]\)这个前缀的权值,因 ...

  4. Luogu P2617 Dynamic Rankings(整体二分)

    题目 动态区间第K小模板题. 一个非常可行的办法是BIT套动态开点权值SegTree,但是它跑的实在太慢了. 然后由于这题并没有强制在线,所以我们可以使用整体二分来吊打树套树. 当然如果强制在线的话就 ...

  5. LUOGU P2617 Dynamic Rankings(树状数组套主席树)

    传送门 解题思路 动态区间第\(k\)大,树状数组套主席树模板.树状数组的每个位置的意思的是每棵主席树的根,维护的是一个前缀和.然后询问的时候\(log\)个点一起做前缀和,一起移动.时空复杂度\(O ...

  6. [luogu P2617] Dynamic Rankings 带修主席树

    带修改的主席树,其实这种,已经不能算作主席树了,因为这个没有维护可持久化的... 主席树直接带修改的话,由于这种数据结构是可持久化的,那么要相应改动,这个节点以后所有的主席树,这样单次修改,就达到n* ...

  7. P2617 Dynamic Rankings(树状数组套主席树)

    P2617 Dynamic Rankings 单点修改,区间查询第k大 当然是无脑树套树了~ 树状数组套主席树就好辣 #include<iostream> #include<cstd ...

  8. 2018.07.01洛谷P2617 Dynamic Rankings(带修主席树)

    P2617 Dynamic Rankings 题目描述 给定一个含有n个数的序列a[1],a[2],a[3]--a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i ...

  9. 洛谷P2617 Dynamic Rankings (主席树)

    洛谷P2617 Dynamic Rankings 题目描述 给定一个含有n个数的序列a[1],a[2],a[3]--a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a ...

随机推荐

  1. js定义一个处理字符串的函数

    //定义一个处理字符串的方法 function StringBuffer(str){ var arr = []; str = str || ''; arr.push(str); //追加字符串 thi ...

  2. zookeeper【4】master选举

    考虑7*24小时向外提供服务的系统,不能有单点故障,于是我们使用集群,采用的是Master+Slave.集群中有一台主机和多台备机,由主机向外提 供服务,备机监听主机状态,一旦主机宕机,备机必需迅速接 ...

  3. 二 Channel

    Java NIO的通道类似流,但又有些不同 既可以从通道中读取数据,也可以写数据到通道.但是流的读写通常是单向的 通道可以异步读写 通道中的数据通常总是要先读到一个Buffer,或者总是从Buffer ...

  4. linux chkconfig 使用说明

    原文 chkconfig是管理系统服务(service)的命令行工具.所谓系统服务(service),就是随系统启动而启动,随系统关闭而关闭的程序. chkconfig可以更新(启动或停止)和查询系统 ...

  5. PAT 1048. Find Coins

    two sum题目,算是贪婪吧 #include <cstdio> #include <cstdlib> #include <vector> #include &l ...

  6. H5,API的pushState(),replaceState()和popstate()用法

    pushState和replaceState是H5的API中新添加的两个方法.通过window.history方法来对浏览器历史记录的读写. pushState和replaceState 在 HTML ...

  7. Java Struts2 (四)

    一.contextMap中的数据操作 root根:List 元素1 元素2 元素3 元素4 元素5 contextMap:Map key value application Map key value ...

  8. [转]ggplot2用法简单介绍

    简介 ggplot2包是基于Wilkinson在<Grammar of Graphics>一书中所提出的图形语法的具体实现, 这套图形语法把绘图过程归纳为data, transformat ...

  9. 查看postgre都有哪些语句占用CPU

    查看占用CPU最多的几个postgresql ps aux | grep postgres | sort -n -r -k 3 | head -10 | awk '{print $2, $3}' 查看 ...

  10. UFW Essentials: Common Firewall Rules and Commands

    Introduction UFW is a firewall configuration tool for iptables that is included with Ubuntu by defau ...