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 时间线段树分治 背包的更多相关文章

  1. 2019.01.13 loj#6515. 贪玩蓝月(线段树分治+01背包)

    传送门 题意简述:有一个初始为空的双端队列,每次可以在队首和队尾插入或弹出一个二元组(wi,vi)(w_i,v_i)(wi​,vi​),支持询问从当前队列中选取若干个元素是的他们的和对 MODMODM ...

  2. LOJ 121 「离线可过」动态图连通性——LCT维护删除时间最大生成树 / 线段树分治

    题目:https://loj.ac/problem/121 离线,LCT维护删除时间最大生成树即可.注意没有被删的边的删除时间是 m+1 . 回收删掉的边的节点的话,空间就可以只开 n*2 了. #i ...

  3. LOJ 2585 「APIO2018」新家 ——线段树分治+二分答案

    题目:https://loj.ac/problem/2585 算答案的时候要二分! 这样的话,就是对于询问位置 x ,二分出一个最小的 mid 使得 [ x-mid , x+mid ] 里包含所有种类 ...

  4. LOJ#121. 「离线可过」动态图连通性(线段树分治)

    题意 板子题,题意很清楚吧.. Sol 很显然可以直接上LCT.. 但是这题允许离线,于是就有了一个非常巧妙的离线的做法,好像叫什么线段树分治?? 此题中每条边出现的位置都可以看做是一段区间. 我们用 ...

  5. LOJ 2312(洛谷 3733) 「HAOI2017」八纵八横——线段树分治+线性基+bitset

    题目:https://loj.ac/problem/2312 https://www.luogu.org/problemnew/show/P3733 原本以为要线段树分治+LCT,查了查发现环上的值直 ...

  6. 【线段树分治 01背包】loj#6515. 「雅礼集训 2018 Day10」贪玩蓝月

    考试时候怎么就是没想到线段树分治呢? 题目描述 <贪玩蓝月>是目前最火爆的网页游戏.在游戏中每个角色都有若干装备,每件装备有一个特征值 $w$ 和一个战斗力 $v$ .在每种特定的情况下, ...

  7. UOJ46 【清华集训2014】玄学 【时间线段树】

    题目链接:UOJ 这题的时间线段树非常的妙. 对时间建立线段树,修改的时候在后面加,每当填满一个节点之后就合并进它的父亲. 对于一个节点维护序列,发现这是一个分段函数,合并就是归并排序.于是就形成了差 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. shell基础 -- 基本语法

    本文介绍一下 shell 的语法. 一.变量 在 shell 里,使用变量之前通常并不需要事先为他们做出声明,需要使用的时候直接创建就行了.默认情况下,所有变量都被看做字符串并以字符串来存储,即使它们 ...

  2. 学习使用Git 版本控制 代码管理

    title: 学习使用Git 版本控制 代码管理 notebook: 经验累积 tags:Git --- Git 版本控制 学习教程 Git版本控制器,可以作为程序员.计算机科学和软件工程的研究人员在 ...

  3. JavaScript 之 ajax

    1. AJAX 的概念 AJAX,即 Asynchronous JavaScript and XML(异步的 JavaScript 和 XML) 同步:前面的代码不执行完毕,后面的代码无法执行 异步: ...

  4. 如何把node更新到最新的稳定版本

    先装n,再用n把node升级到最新稳定版 $ npm install -g n $ n stable

  5. Scrum立会报告+燃尽图(十一月二十一日总第二十九次):β阶段第二周分配任务

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2284 项目地址:https://git.coding.net/zhang ...

  6. mysql 数据库名含“-”

    跨数据库操作时,用反引号即可: insert into `tmi-ds`.knn_test(imagedata) select imagedata from tmidb.imagetable wher ...

  7. WITH HINDSIGHT

    设想和目标 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 我们是要做一个基于文件同步展示的语音软件:感谢之前的两次项目审核,我们定义与描述得很清楚: 我们达到目标 ...

  8. HDU 5855 Less Time, More profit 最大权闭合子图

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5855 Less Time, More profit Time Limit: 2000/1000 MS ...

  9. Java中的网络编程-1

    计算机网络:将分布在不同地区的计算机与专门的外部设备用通信线路互连成一个规模大.功能强的网络系统, 从而使众多计算机 可以方便的互相传递信息, 共享硬件.软件.数据信息等资源. 计算机网络的主要功能: ...

  10. Spring源码解析 – AnnotationConfigApplicationContext容器创建过程

    Spring在BeanFactory基础上提供了一些列具体容器的实现,其中AnnotationConfigApplicationContext是一个用来管理注解bean的容器,从AnnotationC ...