「luogu2617」Dynamic Rankings
「luogu2617」Dynamic Rankings
传送门
树套树直接上树状数组套主席树,常数很大就是了。
树套树参考代码:
/*--------------------------------
Code name: DynamicRanking.cpp
Author: The Ace Bee
This code is made by The Ace Bee
--------------------------------*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#define rg register
using namespace std;
const int MAXN = 500010;
inline int lowbit(int x) { return x & -x; }
inline int read() {
int s = 0; bool f = false; char c = getchar();
while (c < '0' || c > '9') f |= (c == '-'), c = getchar();
while (c >= '0' && c <= '9') s = (s << 3) + (s << 1) + (c ^ 48), c = getchar();
return f ? -s : s;
}
int n, m, a[MAXN], len, b[MAXN], rt[MAXN];
int tot, num[MAXN << 5], lc[MAXN << 5], rc[MAXN << 5];
int cnt[2], tmp[2][20];
struct node{ bool f; int l, r, k; int pos, key; }p[MAXN];
inline void update(int& p, int l, int r, int x, int v) {
if (!p) p = ++tot;
num[p] += v;
if (l == r) return;
int mid = (l + r) >> 1;
if (x <= mid) update(lc[p], l, mid, x, v);
else update(rc[p], mid + 1, r, x, v);
}
inline void HJTupdate(int x, int v) {
int k = lower_bound(b + 1, b + len + 1, a[x]) - b;
for (rg int i = x; i <= n; i += lowbit(i)) update(rt[i], 1, len, k, v);
}
inline int query(int l, int r, int k) {
if (l == r) return l;
int mid = (l + r) >> 1, sum = 0;
for (rg int i = 1; i <= cnt[1]; ++i) sum += num[lc[tmp[1][i]]];
for (rg int i = 1; i <= cnt[0]; ++i) sum -= num[lc[tmp[0][i]]];
if (k <= sum) {
for (rg int i = 1; i <= cnt[1]; ++i) tmp[1][i] = lc[tmp[1][i]];
for (rg int i = 1; i <= cnt[0]; ++i) tmp[0][i] = lc[tmp[0][i]];
return query(l, mid, k);
} else {
for (rg int i = 1; i <= cnt[1]; ++i) tmp[1][i] = rc[tmp[1][i]];
for (rg int i = 1; i <= cnt[0]; ++i) tmp[0][i] = rc[tmp[0][i]];
return query(mid + 1, r, k - sum);
}
}
inline int HJTquery(int l, int r, int k) {
memset(tmp, 0, sizeof tmp), cnt[0] = cnt[1] = 0;
for (rg int i = r; i; i -= lowbit(i)) tmp[1][++cnt[1]] = rt[i];
for (rg int i = l; i; i -= lowbit(i)) tmp[0][++cnt[0]] = rt[i];
return query(1, len, k);
}
int main() {
n = read(), m = read();
for (rg int i = 1; i <= n; ++i) a[i] = read(), b[++len] = a[i];
for (rg int i = 1; i <= m; ++i) {
char s[5]; scanf("%s", s);
p[i].f = (s[0] == 'Q');
if (p[i].f)
p[i].l = read(), p[i].r = read(), p[i].k = read();
else
p[i].pos = read(), p[i].key = read(), b[++len] = p[i].key;
}
sort(b + 1, b + len + 1);
len = unique(b + 1, b + len + 1) - b - 1;
for (rg int i = 1; i <= n; ++i) HJTupdate(i, 1);
for (rg int i = 1; i <= m; ++i) {
if (p[i].f)
printf("%d\n", b[HJTquery(p[i].l - 1, p[i].r, p[i].k)]);
else {
HJTupdate(p[i].pos, -1);
a[p[i].pos] = p[i].key;
HJTupdate(p[i].pos, 1);
}
}
return 0;
}
整体二分的话就把修改看做减去开始的再加上后来的,跑得比树套树快远了~
整体二分参考代码:
#include <algorithm>
#include <cstdio>
#define rg register
#define file(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout)
using namespace std;
template < class T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while ('0' > c || c > '9') f |= c == '-', c = getchar();
while ('0' <= c && c <= '9') s = s * 10 + c - 48, c = getchar();
s = f ? -s : s;
}
const int _ = 2e5 + 5, INF = 1e9;
int n, m, q, a[_], tr[_], res[_];
int num; struct node { int opt, id, l, r, k; } t[_ << 1], tt1[_ << 1], tt2[_ << 1];
inline void update(int x, int v) { for (rg int i = x; i <= n; i += i & -i) tr[i] += v; }
inline int query(int x) { int res = 0; for (rg int i = x; i >= 1; i -= i & -i) res += tr[i]; return res; }
inline void solve(int ql, int qr, int l, int r) {
if (ql > qr || l > r) return ;
if (l == r) { for (rg int i = ql; i <= qr; ++i) if (t[i].opt == 1) res[t[i].id] = l; return ; }
int mid = (l + r) >> 1, p1 = 0, p2 = 0;
for (rg int i = ql; i <= qr; ++i) {
if (t[i].opt == 0) {
if (t[i].l <= mid) update(t[i].r, t[i].k), tt1[++p1] = t[i]; else tt2[++p2] = t[i];
} else {
int cnt = query(t[i].r) - query(t[i].l - 1);
if (cnt >= t[i].k) tt1[++p1] = t[i]; else t[i].k -= cnt, tt2[++p2] = t[i];
}
}
for (rg int i = 1; i <= p1; ++i) if (tt1[i].opt == 0) update(tt1[i].r, -tt1[i].k);
for (rg int i = 1; i <= p1; ++i) t[ql + i - 1] = tt1[i];
for (rg int i = 1; i <= p2; ++i) t[ql + p1 + i - 1] = tt2[i];
solve(ql, ql + p1 - 1, l, mid), solve(ql + p1, qr, mid + 1, r);
}
int main() {
read(n), read(m);
for (rg int i = 1; i <= n; ++i) read(a[i]), t[++num] = (node) { 0, 0, a[i], i, 1 };
char s[5];
for (rg int l, r, k, i = 1; i <= m; ++i) {
scanf("%s", s);
if (s[0] == 'Q') read(l), read(r), read(k), t[++num] = (node) { 1, ++q, l, r, k };
else read(l), read(r), t[++num] = (node) { 0, 0, a[l], l, -1 }, t[++num] = (node) { 0, 0, a[l] = r, l, 1 };
}
solve(1, num, -INF, INF);
for (rg int i = 1; i <= q; ++i) printf("%d\n", res[i]);
return 0;
}
「luogu2617」Dynamic Rankings的更多相关文章
- iOS 9,为前端世界都带来了些什么?「译」 - 高棋的博客
2015 年 9 月,Apple 重磅发布了全新的 iPhone 6s/6s Plus.iPad Pro 与全新的操作系统 watchOS 2 与 tvOS 9(是的,这货居然是第 9 版),加上已经 ...
- 「译」JUnit 5 系列:条件测试
原文地址:http://blog.codefx.org/libraries/junit-5-conditions/ 原文日期:08, May, 2016 译文首发:Linesh 的博客:「译」JUni ...
- 「译」JUnit 5 系列:扩展模型(Extension Model)
原文地址:http://blog.codefx.org/design/architecture/junit-5-extension-model/ 原文日期:11, Apr, 2016 译文首发:Lin ...
- BZOJ 1901: Zju2112 Dynamic Rankings[带修改的主席树]【学习笔记】
1901: Zju2112 Dynamic Rankings Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 7143 Solved: 2968[Su ...
- JavaScript OOP 之「创建对象」
工厂模式 工厂模式是软件工程领域一种广为人知的设计模式,这种模式抽象了创建具体对象的过程.工厂模式虽然解决了创建多个相似对象的问题,但却没有解决对象识别的问题. function createPers ...
- 「C++」理解智能指针
维基百科上面对于「智能指针」是这样描述的: 智能指针(英语:Smart pointer)是一种抽象的数据类型.在程序设计中,它通常是经由类型模板(class template)来实做,借由模板(tem ...
- [bzoj1901][zoj2112][Dynamic Rankings] (整体二分+树状数组 or 动态开点线段树 or 主席树)
Dynamic Rankings Time Limit: 10 Seconds Memory Limit: 32768 KB The Company Dynamic Rankings has ...
- 「JavaScript」四种跨域方式详解
超详细并且带 Demo 的 JavaScript 跨域指南来了! 本文基于你了解 JavaScript 的同源策略,并且了解使用跨域跨域的理由. 1. JSONP 首先要介绍的跨域方法必然是 JSON ...
- 「2014-5-31」Z-Stack - Modification of Zigbee Device Object for better network access management
写一份赏心悦目的工程文档,是很困难的事情.若想写得完善,不仅得用对工具(use the right tools),注重文笔,还得投入大把时间,真心是一件难度颇高的事情.但,若是真写好了,也是善莫大焉: ...
随机推荐
- lca最近公共祖先与树上倍增。
https://vjudge.net/contest/295298#problem/A lca 的题目 求任意两点的距离. A题是在线算法,用st表rmq来实现. https://blog.csdn. ...
- Comparable
为什么会有Comparable与Comparator接口? 引入策略模式 目录 引入 Comparable接口的来龙去脉 引入Comparator接口 什么是策略模式? 使用了策略模式有什么好处? ...
- layui-表格宽度自适应
不设置表格宽度,表格默认全屏 可以通过以下方式设置表格宽度
- 树状数组(fenwick tree)
树状数组又称芬威克树,概念上是树状,实际上是使用数组实现的,表现为一种隐式数据结构,balabala...详情请见:https://en.wikipedia.org/wiki/Fenwick_tree ...
- C语言:判断t所指字符串中的字母是否由连续递增字母组成。-判断一个输入的任何整数n,是否等于某个连续正整数序列之和。-将一副扑克牌编号为1到54,以某种方式洗牌,这种方式是将这副牌分成两半,然后将他们交叉,并始终保持编号1的牌在最上方。
//判断t所指字符串中的字母是否由连续递增字母组成. #include <stdio.h> #include <string.h> void NONO(); int fun( ...
- unittest如何制作测试套件Testsuite 按method,class ,module,按命令行执行unittest
cmd 执行unittest case : python -m unitest test.py test2.py python -m unittest testfile.testclass.tes ...
- 通过命令创建Django项目
本人是使用window10操作系统进行讲解Django框架,Linux系统和windows版本几乎一致,可以自行学习就可以解决. 首先在系统上创建了虚拟环境,如果不会创建,可以根据这篇文章学习:htt ...
- LLC半桥谐振变换器调试记录
1.判断二极管是否击穿 2.判断mos管是否烧坏 直接用声音档,发出响声说明击穿了 3.测试二极管的正负极方法 将万用表调到二极管档 1.信号发生芯片周围的电阻 2.反馈部分的电阻 3.实验准备部分: ...
- ISE主备注册和同步
Synchronize Primary and Secondary Cisco ISE Nodes You can make configuration changes to Cisco ISE on ...
- 在Visual Studio 2017中,.NET(C#)通过Oracle.ManagedDataAccess来连接Oracle数据库
C#如何通过Oracle.ManagedDataAccess来连接Oracle数据库 1.先创建一个项目,鼠标右击项目中的引用,再点击管理NuGet程序包(也可以先下载dll文件,再选添加引用),在搜 ...