Solution -「JOISC 2021」「LOJ #3489」饮食区
\(\mathcal{Description}\)
Link.
呐……不想概括题意,自己去读叭~
\(\mathcal{Solution}\)
如果仅有 1. 3. 操作,能不能做?
——简单整体二分。
如果仅有 1. 2. 操作,能不能实时维护每个位置还剩下多少人?累计走了多少人?
——吉司机线段树。
所以,离线下来,把上两个重工业揉在一起就能粗暴地过掉这道题√ 复杂度 \(\mathcal O(n\log^2n)\)(\(n,m,q\) 同阶)。
有 \(\mathcal O(n\log n)\) 而且短得多的算法欸,问 Tiw 嘛 qwq。
\(\mathcal{Code}\)
/* Clearink */
#include <cstdio>
#include <vector>
#define rep( i, l, r ) for ( int i = l, rep##i = r; i <= rep##i; ++i )
#define per( i, r, l ) for ( int i = r, per##i = l; i >= per##i; --i )
typedef long long LL;
#define int LL
template<typename Tp = int>
inline Tp rint() {
Tp x = 0; int s = getchar();
for ( ; s < '0' || '9' < s; s = getchar() );
for ( ; '0' <= s && s <= '9'; s = getchar() ) x = x * 10 + ( s ^ '0' );
return x;
}
template<typename Tp>
inline void wint( Tp x ) {
if ( x < 0 ) putchar( '-' ), x = -x;
if ( 9 < x ) wint( x / 10 );
putchar( x % 10 ^ '0' );
}
inline LL lmin( const LL a, const LL b ) { return a < b ? a : b; }
const int MAXN = 2.5e5;
const LL LINF = 1ll << 60;
int n, m, q;
std::vector<int> allq;
struct Event {
int a, b, c; LL d;
inline void read() {
if ( int op = rint(); op == 1 ) {
a = rint(), b = rint(), c = rint(), d = rint();
} else if ( op == 2 ) {
a = -1, b = rint(), c = rint(), d = rint();
} else {
a = b = -2, c = rint(), d = rint<LL>();
}
}
} evt[MAXN + 5];
struct SegmentTree {
LL tag[MAXN << 2];
inline void clear( const int u, const int l, const int r ) {
tag[u] = 0;
if ( l == r ) return ;
int mid = l + r >> 1;
clear( u << 1, l, mid ), clear( u << 1 | 1, mid + 1, r );
}
inline void modify( const int u, const int l, const int r,
const int ml, const int mr, const LL v ) {
if ( ml <= l && r <= mr ) return void( tag[u] += v );
int mid = l + r >> 1;
if ( ml <= mid ) modify( u << 1, l, mid, ml, mr, v );
if ( mid < mr ) modify( u << 1 | 1, mid + 1, r, ml, mr, v );
}
inline LL query( const int u, const int l, const int r, const int x ) {
if ( l == r ) return tag[u];
int mid = l + r >> 1;
if ( x <= mid ) return tag[u] + query( u << 1, l, mid, x );
else return tag[u] + query( u << 1 | 1, mid + 1, r, x );
}
} sgt; // It's for both Init and Solve.
namespace Init {
struct JiSegmentTree {
LL tag1[MAXN << 2], tag2[MAXN << 2], mnv[MAXN << 2], smn[MAXN << 2];
inline void clear( const int u, const int l, const int r ) {
smn[u] = LINF;
if ( l == r ) return ;
int mid = l + r >> 1;
clear( u << 1, l, mid ), clear( u << 1 | 1, mid + 1, r );
}
inline void pushad( const int u, const LL v1, const LL v2 ) {
tag1[u] += v1, mnv[u] += v1;
if ( smn[u] != LINF ) smn[u] += v2, tag2[u] += v2;
}
inline void pushdn( const int u ) {
int a = mnv[u << 1], b = mnv[u << 1 | 1];
if ( a <= b ) pushad( u << 1, tag1[u], tag2[u] );
else pushad( u << 1, tag2[u], tag2[u] );
if ( b <= a ) pushad( u << 1 | 1, tag1[u], tag2[u] );
else pushad( u << 1 | 1, tag2[u], tag2[u] );
tag1[u] = tag2[u] = 0;
}
inline void pushup( const int u ) {
mnv[u] = lmin( mnv[u << 1], mnv[u << 1 | 1] );
smn[u] = LINF;
if ( mnv[u] < mnv[u << 1] ) smn[u] = lmin( smn[u], mnv[u << 1] );
if ( mnv[u] < mnv[u << 1 | 1] )
smn[u] = lmin( smn[u], mnv[u << 1 | 1] );
if ( mnv[u] < smn[u << 1] ) smn[u] = lmin( smn[u], smn[u << 1] );
if ( mnv[u] < smn[u << 1 | 1] )
smn[u] = lmin( smn[u], smn[u << 1 | 1] );
}
inline void modify( const int u, const int l, const int r,
const int ml, const int mr, const LL v ) {
if ( ml <= l && r <= mr ) return pushad( u, v, v );
int mid = l + r >> 1; pushdn( u );
if ( ml <= mid ) modify( u << 1, l, mid, ml, mr, v );
if ( mid < mr ) modify( u << 1 | 1, mid + 1, r, ml, mr, v );
pushup( u );
}
inline void upto( const int u, const int l, const int r,
const int ul, const int ur, const LL v ) {
if ( mnv[u] >= v ) return ;
if ( ul <= l && r <= ur && v < smn[u] )
return pushad( u, v - mnv[u], 0 );
int mid = l + r >> 1; pushdn( u );
if ( ul <= mid ) upto( u << 1, l, mid, ul, ur, v );
if ( mid < ur ) upto( u << 1 | 1, mid + 1, r, ul, ur, v );
pushup( u );
}
inline LL query( const int u, const int l, const int r, const int x ) {
if ( l == r ) return mnv[u];
int mid = l + r >> 1; pushdn( u );
if ( x <= mid ) return query( u << 1, l, mid, x );
else return query( u << 1 | 1, mid + 1, r, x );
}
} jsgt;
inline void init() {
jsgt.clear( 1, 1, n );
rep ( i, 1, q ) {
if ( evt[i].a >= 0 ) {
sgt.modify( 1, 1, n, evt[i].a, evt[i].b, evt[i].d );
jsgt.modify( 1, 1, n, evt[i].a, evt[i].b, evt[i].d );
} else if ( evt[i].a == -1 ) {
jsgt.modify( 1, 1, n, evt[i].b, evt[i].c, -evt[i].d );
jsgt.upto( 1, 1, n, evt[i].b, evt[i].c, 0 );
} else {
allq.push_back( i );
evt[i].d += sgt.query( 1, 1, n, evt[i].c )
- jsgt.query( 1, 1, n, evt[i].c );
}
}
}
} // namespace Init.
namespace Solve {
int ans[MAXN + 5];
inline void divide( const int l, const int r, const std::vector<int>& qvec ) {
if ( qvec.empty() ) return ;
if ( l == r ) {
for ( int q: qvec ) ans[q] = l;
return ;
}
int mid = l + r >> 1;
rep ( i, l, mid ) if ( evt[i].a >= 0 ) {
sgt.modify( 1, 1, n, evt[i].a, evt[i].b, evt[i].d );
}
std::vector<int> qlef, qrig;
for ( int q: qvec ) {
if ( sgt.query( 1, 1, n, evt[q].c ) >= evt[q].d ) qlef.push_back( q );
else qrig.push_back( q );
}
divide( mid + 1, r, qrig );
rep ( i, l, mid ) if ( evt[i].a >= 0 ) {
sgt.modify( 1, 1, n, evt[i].a, evt[i].b, -evt[i].d );
}
divide( l, mid, qlef );
}
inline void solve() {
sgt.clear( 1, 1, n );
divide( 1, q + 1, allq );
rep ( i, 1, q ) if ( evt[i].a == -2 ) {
wint( ans[i] > i ? 0 : evt[ans[i]].c ), putchar( '\n' );
}
}
} // namespace Solve.
signed main() {
n = rint(), m = rint(), q = rint();
rep ( i, 1, q ) evt[i].read();
Init::init();
Solve::solve();
return 0;
}
Solution -「JOISC 2021」「LOJ #3489」饮食区的更多相关文章
- Solution -「JOISC 2021」「LOJ #3495」聚会 2
\(\mathcal{Description}\) Link. 给定一棵含 \(n\) 个结点的树.称点集 \(S\) 到结点 \(u\) 的会合距离为 \(\sum_{v\in S}\ope ...
- Solution -「JOISC 2021」「LOJ #3491」道路建设
\(\mathcal{Description}\) Link. 平面上有 \(n\) 个互不重合的点 \((x_{1..n},y_{1..n})\),求其两两曼哈顿距离的前 \(m\) 小值. ...
- Solution -「JOISC 2021」古老的机器
\(\mathcal{Description}\) Link. 这是一道通信题. 对于长度为一个 \(n\),仅包含字符 X, Y, Z 的字符串 \(s\),将其中 \(n\) 个字符按 ...
- Loj #2731 「JOISC 2016 Day 1」棋盘游戏
Loj 2731 「JOISC 2016 Day 1」棋盘游戏 JOI 君有一个棋盘,棋盘上有 \(N\) 行 \(3\) 列 的格子.JOI 君有若干棋子,并想用它们来玩一个游戏.初始状态棋盘上至少 ...
- 【LOJ】#3036. 「JOISC 2019 Day3」指定城市
LOJ#3036. 「JOISC 2019 Day3」指定城市 一个点的可以dp出来 两个点也可以dp出来 后面的就是在两个点的情况下选一条最长的链加进去,用线段树维护即可 #include < ...
- 【LOJ】#3034. 「JOISC 2019 Day2」两道料理
LOJ#3034. 「JOISC 2019 Day2」两道料理 找出最大的\(y_{i}\)使得\(sumA_{i} + sumB_{y_i} \leq S_{i}\) 和最大的\(x_{j}\)使得 ...
- 【LOJ】#3032. 「JOISC 2019 Day1」馕
LOJ#3032. 「JOISC 2019 Day1」馕 处理出每个人把馕切成N段,每一段快乐度相同,我们选择第一个排在最前的人分给他的第一段,然后再在未选取的的人中选一个第二个排在最前的切一下,并把 ...
- 【LOJ】#3033. 「JOISC 2019 Day2」两个天线
LOJ#3033. 「JOISC 2019 Day2」两个天线 用后面的天线更新前面的天线,线段树上存历史版本的最大值 也就是线段树需要维护历史版本的最大值,后面的天线的标记中最大的那个和最小的那个, ...
- 【LOJ】#3031. 「JOISC 2019 Day1」聚会
LOJ#3031. 「JOISC 2019 Day1」聚会 听说随机可过? 我想了很久想了一个不会被卡的做法,建出前\(u - 1\)个点的虚树,然后找第\(u\)个点的插入位置,就是每次找一条最长链 ...
随机推荐
- 拓展 Array 方法
为 Array 对象扩展了一个迭代器之后,就可以利用这个法代器进一步拓展 Array 的方法,使其能够完成更多的实用功能. Array.prototype.each = function( f ) { ...
- 第10组 Beta冲刺 (1/5)(组长)
1.1基本情况 ·队名:今晚不睡觉 ·组长博客:https://www.cnblogs.com/cpandbb/p/14012521.html ·作业博客:https://edu.cnblogs.co ...
- Hive的分析函数的使用
原文: https://www.toutiao.com/i6769120000578945544/?group_id=6769120000578945544 我们先准备数据库.表和数据 开窗分析函数相 ...
- 【Python自动化Excel】pandas处理Excel数据的基本流程
这里所说的pandas并不是大熊猫,而是Python的第三方库.这个库能干嘛呢?它在Python数据分析领域可是无人不知.无人不晓的.可以说是Python世界中的Excel. pandas库处理数据相 ...
- SGU140. Integer Sequences
https://codeforces.com/problemsets/acmsguru/problem/99999/140 n元同余方程的求解 对于任意二元我们可以替换成kgcd(a,b),不断迭代下 ...
- HDU分拆素数和
https://acm.hdu.edu.cn/showproblem.php?pid=2098 时间复杂度 #include<bits/stdc++.h> using namespace ...
- SSRF漏洞用到的其他协议(dict协议,file协议)
0x00 引言 当SSRF打内网reids时,若gopher协议用不了,我们也可以用其他协议 0x01 dict协议一.定义:词典网络协议,在RFC 2009中进行描述.它的目标是超越Webster ...
- nodejs express异常捕获
参考链接: http://blog.coinidea.com/web开发/nodejs-1131.html 由于nodejs是非阻塞单进程单线程的,一旦nodejs抛出异常,整个服务就会停掉.服务将会 ...
- 676. Implement Magic Dictionary
Implement a magic directory with buildDict, and search methods. For the method buildDict, you'll be ...
- 安卓开发之intent
两个活动之间的跳转要通过intent来进行,intent跳转分为隐式的和显示的. 首先xml中定义Button,通过按下按钮实现回调,在回调函数中进行相应intent设置. <Button an ...