【题解】【LibreOJ Round #6】花团 LOJ 534 时间线段树分治 背包
Prelude
题目链接:萌萌哒传送门(/≧▽≦)/
Solution
如果完全离线的话,可以直接用时间线段树分治来做,复杂度\(O(qv \log q)\)。
现在在线了怎么办呢?
这其实是个假在线,因为每个物品的删除时间已经给你了,所以还是直接用时间线段树分治来做。
其实我是重点想谈一下复杂度的,\(O(n^{2} \log n)\)的复杂度居然都可以出到\(15000\),而且居然还跑的飞快?

Code
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef long double ldouble;
typedef pair<int,int> pii;
typedef vector<pii>::iterator viter;
const int MAXN = 15010;
const int LOGN = 17;
const int INF = 0x3f3f3f3f;
int _w;
inline void bmin( int &a, int b ) {
a = b < a ? b : a;
}
inline void bmax( int &a, int b ) {
a = b > a ? b : a;
}
int q, maxv, T, lastans;
struct Knapsack {
int f[MAXN];
void init() {
for( int i = 0; i <= maxv; ++i )
f[i] = -INF;
f[0] = 0;
}
void insert( int v, int w ) {
for( int i = maxv-v; i >= 0; --i )
bmax( f[i+v], f[i]+w );
}
const int &operator[]( int i ) const {
return f[i];
}
int &operator[]( int i ) {
return f[i];
}
};
vector<pii> item[MAXN<<2];
Knapsack f[LOGN];
int qv[MAXN];
pii ins;
int ql, qr;
void insert( int o, int L, int R ) {
if( L >= ql && R <= qr ) {
item[o].push_back(ins);
} else {
int M = (L+R)>>1, lc = o<<1, rc = lc|1;
if( ql <= M ) insert(lc, L, M);
if( qr > M ) insert(rc, M+1, R);
}
}
void query( int i, int d ) {
if( qv[i] != -1 ) {
int v = qv[i];
if( f[d][v] < 0 ) {
puts("0 0");
lastans = 0;
} else {
printf( "1 %d\n", f[d][v] );
lastans = T * (f[d][v] ^ 1);
}
}
if( i == q ) return;
int op;
_w = scanf( "%d", &op );
if( op == 1 ) {
int v, w, e;
_w = scanf( "%d%d%d", &v, &w, &e );
v -= lastans, w -= lastans, e -= lastans;
ins = pii(v, w), ql = i+1, qr = e;
insert(1, 0, q);
} else {
_w = scanf( "%d", qv+i+1 );
qv[i+1] -= lastans;
}
}
void solve( int o, int L, int R, int d ) {
for( viter it = item[o].begin(); it != item[o].end(); ++it )
f[d].insert(it->first, it->second);
if( L == R ) {
query(L, d);
} else {
int M = (L+R)>>1, lc = o<<1, rc = lc|1;
f[d+1] = f[d];
solve(lc, L, M, d+1);
f[d+1] = f[d];
solve(rc, M+1, R, d+1);
}
}
int main() {
_w = scanf( "%d%d%d", &q, &maxv, &T );
f[0].init();
memset(qv, -1, sizeof qv);
solve(1, 0, q, 0);
return 0;
}
【题解】【LibreOJ Round #6】花团 LOJ 534 时间线段树分治 背包的更多相关文章
- 2019.01.13 loj#6515. 贪玩蓝月(线段树分治+01背包)
传送门 题意简述:有一个初始为空的双端队列,每次可以在队首和队尾插入或弹出一个二元组(wi,vi)(w_i,v_i)(wi,vi),支持询问从当前队列中选取若干个元素是的他们的和对 MODMODM ...
- LOJ 121 「离线可过」动态图连通性——LCT维护删除时间最大生成树 / 线段树分治
题目:https://loj.ac/problem/121 离线,LCT维护删除时间最大生成树即可.注意没有被删的边的删除时间是 m+1 . 回收删掉的边的节点的话,空间就可以只开 n*2 了. #i ...
- LOJ 2585 「APIO2018」新家 ——线段树分治+二分答案
题目:https://loj.ac/problem/2585 算答案的时候要二分! 这样的话,就是对于询问位置 x ,二分出一个最小的 mid 使得 [ x-mid , x+mid ] 里包含所有种类 ...
- LOJ#121. 「离线可过」动态图连通性(线段树分治)
题意 板子题,题意很清楚吧.. Sol 很显然可以直接上LCT.. 但是这题允许离线,于是就有了一个非常巧妙的离线的做法,好像叫什么线段树分治?? 此题中每条边出现的位置都可以看做是一段区间. 我们用 ...
- LOJ 2312(洛谷 3733) 「HAOI2017」八纵八横——线段树分治+线性基+bitset
题目:https://loj.ac/problem/2312 https://www.luogu.org/problemnew/show/P3733 原本以为要线段树分治+LCT,查了查发现环上的值直 ...
- 【线段树分治 01背包】loj#6515. 「雅礼集训 2018 Day10」贪玩蓝月
考试时候怎么就是没想到线段树分治呢? 题目描述 <贪玩蓝月>是目前最火爆的网页游戏.在游戏中每个角色都有若干装备,每件装备有一个特征值 $w$ 和一个战斗力 $v$ .在每种特定的情况下, ...
- UOJ46 【清华集训2014】玄学 【时间线段树】
题目链接:UOJ 这题的时间线段树非常的妙. 对时间建立线段树,修改的时候在后面加,每当填满一个节点之后就合并进它的父亲. 对于一个节点维护序列,发现这是一个分段函数,合并就是归并排序.于是就形成了差 ...
- hdu 5195 DZY Loves Topological Sorting BestCoder Round #35 1002 [ 拓扑排序 + 优先队列 || 线段树 ]
传送门 DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131 ...
- Codeforces Round #603 (Div. 2) E. Editor 线段树
E. Editor The development of a text editor is a hard problem. You need to implement an extra module ...
随机推荐
- 拉格朗日乘子法与KKT条件 && SVM中为什么要用对偶问题
参考链接: 拉格朗日乘子法和KKT条件 SVM为什么要从原始问题变为对偶问题来求解 为什么要用对偶问题 写在SVM之前——凸优化与对偶问题 1. 拉格朗日乘子法与KKT条件 2. SVM 为什么要从原 ...
- 深度学习-tensorflow学习笔记(2)-MNIST手写字体识别
深度学习-tensorflow学习笔记(2)-MNIST手写字体识别超级详细版 这是tf入门的第一个例子.minst应该是内置的数据集. 前置知识在学习笔记(1)里面讲过了 这里直接上代码 # -*- ...
- 如何让QT程序以管理员权限运行(UAC)
方案一:(仅适用于使用msvc编译器) 在PRO文件中添加一行指令即可, QMAKE_LFLAGS += /MANIFESTUAC:"level='requireAdministrator' ...
- Centos 7 zabbix 实战应用
实际需求:公司已经有了100台服务器,现在需要使用zabbix全部监控起来. 先出个方案(规划) 常规监控:cpu,内存,磁盘,网卡 问题:怎样快速添加100台机器 方法1:使用克隆的 ...
- Beta发布——视频博客
1.视频链接 视频上传至优酷自频道,地址链接:http://v.youku.com/v_show/id_XMzkzNzAxNDk2OA==.html?spm=a2hzp.8244740.0.0 2.视 ...
- 随机生成30道四则运算题NEW
代码: #include <iostream> #include <time.h> using namespace std; void main() { srand((int) ...
- 第二章 script元素
<script>元素 async:可选.表示应该立即下载脚本,但不应妨碍页面中的其他操作,比如下载其他资源或等待加载其他脚本.只对外部脚本文件有效. charset:可选.表示通过 ...
- 虚拟机Centos设置静态IP
首先确保虚拟网卡(VMware Network Adapter VMnet8)是开启的,然后在windows的命令行里输入“ipconfig /all”,找到VMware Network Adapte ...
- 为何php curl post模式发送数据速度变慢了?我来说说原因
事例: 今天要向一台服务器上传文件,原版是curl的get模式,现在改用了post模式,按照原本的思想,代码如下 <?php $post['c'] = 'config'; $post['t'] ...
- Spring MVC @RequestParam @RequestHeader @CookieValue用法
摘要: package com.hust.springmvc1; import org.springframework.stereotype.Controller; import org.spring ...