Solution -「LOCAL」模板
\(\mathcal{Description}\)
给定一棵 \(n\) 个结点树,\(1\) 为根,每个 \(u\) 结点有容量 \(k_u\)。\(m\) 次操作,每次操作 \((u,c)\),表示在 \(u\) 到根路径上的每个结点放一个颜色为 \(c\) 的小球,但若某一结点容量已满,则跳过该结点不放球。求所有操作完成后每个结点拥有小球的颜色种数。
\(n,m\le10^5\)。
\(\mathcal{Solution}\)
优雅的离线算法。
首先,若 \((\forall u)(k_u\ge m)\),有一个很显然的 DFN(虚树)+差分+BIT 的计算方法,且这种计算可以通过 std::set 做到在线。现在考虑 \(k_u\) 的限制,我们可以在每个结点的球数刚好达到限制时对它询问求到答案,即利用整体二分把每个点的询问挂到一个操作时刻之后,就做完啦。
复杂度 \(\mathcal O(m(\log m)(\log n))\)。
\(\mathcal{Code}\)
/* Clearink */
#include <set>
#include <cstdio>
#include <vector>
#include <algorithm>
inline int rint () {
int x = 0, f = 1; char s = getchar ();
for ( ; s < '0' || '9' < s; s = getchar () ) f = s == '-' ? -f : f;
for ( ; '0' <= s && s <= '9'; s = getchar () ) x = x * 10 + ( s ^ '0' );
return x * f;
}
template<typename Tp>
inline void wint ( Tp x ) {
if ( x < 0 ) putchar ( '-' ), x = -x;
if ( 9 < x ) wint ( x / 10 );
putchar ( x % 10 ^ '0' );
}
const int MAXN = 1e5, MAXLG = 17;
int n, m, ecnt, head[MAXN + 5], vol[MAXN + 5], tmpc[MAXN + 5], ans[MAXN + 5];
int dfc, dfn[MAXN + 5], ref[MAXN + 5], dep[MAXN + 5], siz[MAXN + 5];
int stc, stn[MAXN + 5], st[2 * MAXN + 5][MAXLG + 5], lg2[MAXN * 2 + 5];
std::vector<int> pts, qry[MAXN + 5];
std::set<int> vtr[MAXN + 5];
struct Edge { int to, nxt; } graph[MAXN * 2 + 5];
struct Event { int u, c; } evt[MAXN + 5];
struct BinaryIndexTree {
int val[MAXN + 5];
inline void update ( int x, const int k ) {
for ( ; x <= n; x += x & -x ) {
val[x] += k;
}
}
inline int sum ( int x ) {
int ret = 0;
for ( ; x; x -= x & -x ) ret += val[x];
return ret;
}
inline int sum ( const int l, const int r ) {
return l > r ? 0 : sum ( r ) - sum ( l - 1 );
}
} bit;
inline void link ( const int s, const int t ) {
graph[++ ecnt] = { t, head[s] };
head[s] = ecnt;
}
inline void init ( const int u, const int f ) {
ref[dfn[u] = ++ dfc] = u;
st[stn[u] = ++ stc][0] = u;
siz[u] = 1, dep[u] = dep[f] + 1;
for ( int i = head[u], v; i; i = graph[i].nxt ) {
if ( ( v = graph[i].to ) ^ f ) {
init ( v, u );
siz[u] += siz[v], st[++ stc][0] = u;
}
}
}
inline void initST () {
for ( int i = 2; i <= stc; ++ i ) lg2[i] = lg2[i >> 1] + 1;
for ( int j = 1; 1 << j <= stc; ++ j ) {
for ( int i = 1; i + ( 1 << j ) - 1 <= stc; ++ i ) {
if ( dep[st[i][j - 1]] < dep[st[i + ( 1 << j >> 1 )][j - 1]] ) {
st[i][j] = st[i][j - 1];
} else {
st[i][j] = st[i + ( 1 << j >> 1 )][j - 1];
}
}
}
}
inline int LCA ( int u, int v ) {
if ( ( u = stn[u] ) > ( v = stn[v] ) ) u ^= v ^= u ^= v;
int k = lg2[v - u + 1];
return dep[st[u][k]] < dep[st[v - ( 1 << k ) + 1][k]] ?
st[u][k] : st[v - ( 1 << k ) + 1][k];
}
inline void markQ ( std::vector<int>& pts, const int l, const int r ) {
if ( pts.empty () ) return ;
if ( l == r ) {
for ( int u: pts ) qry[l].push_back ( u );
return pts.clear ();
}
int mid = l + r >> 1;
for ( int i = l ? l : 1; i <= mid; ++ i ) bit.update ( dfn[evt[i].u], 1 );
std::vector<int> lc, rc;
for ( int u: pts ) {
if ( bit.sum ( dfn[u], dfn[u] + siz[u] - 1 ) < vol[u] ) rc.push_back ( u );
else lc.push_back ( u );
}
pts.clear ();
markQ ( rc, mid + 1, r );
for ( int i = l ? l : 1; i <= mid; ++ i ) bit.update ( dfn[evt[i].u], -1 );
markQ ( lc, l, mid );
}
inline void addNode ( std::set<int>& vtr, const int u ) {
auto ret ( vtr.insert ( dfn[u] ) );
if ( !ret.second ) return ;
bit.update ( dfn[u], 1 );
auto it ( ret.first ); int p = -1, q = -1;
if ( it != vtr.begin () ) {
bit.update ( dfn[LCA ( p = ref[*-- it], u )], -1 );
++ it;
}
if ( ++ it != vtr.end () ) {
bit.update ( dfn[LCA ( q = ref[*it], u )], -1 );
}
if ( ~p && ~q ) {
bit.update ( dfn[LCA ( p, q )], 1 );
}
}
int main () {
freopen ( "ac.in", "r", stdin );
freopen ( "ac.out", "w", stdout );
n = rint ();
for ( int i = 1, u, v; i < n; ++ i ) {
u = rint (), v = rint ();
link ( u, v ), link ( v, u );
}
for ( int i = 1; i <= n; ++ i ) {
vol[i] = rint ();
pts.push_back ( i );
}
m = rint ();
for ( int i = 1; i <= m; ++ i ) {
evt[i].u = rint (), tmpc[i] = evt[i].c = rint ();
}
std::sort ( tmpc + 1, tmpc + m + 1 );
int mxc = std::unique ( tmpc + 1, tmpc + m + 1 ) - tmpc - 1;
for ( int i = 1; i <= m; ++ i ) {
evt[i].c = std::lower_bound ( tmpc + 1, tmpc + mxc + 1, evt[i].c ) - tmpc;
}
init ( 1, 0 ), initST ();
markQ ( pts, 0, m );
for ( int i = 1; i <= m; ++ i ) {
addNode ( vtr[evt[i].c], evt[i].u );
for ( int u: qry[i] ) ans[u] = bit.sum ( dfn[u], dfn[u] + siz[u] - 1 );
}
for ( int q = rint (); q --; ) wint ( ans[rint ()] ), putchar ( '\n' );
return 0;
}
\(\mathcal{Details}\)
这题 Tricks 好多啊 owo。
Solution -「LOCAL」模板的更多相关文章
- Solution -「LOCAL」二进制的世界
\(\mathcal{Description}\) OurOJ. 给定序列 \(\{a_n\}\) 和一个二元运算 \(\operatorname{op}\in\{\operatorname{ ...
- Solution -「LOCAL」大括号树
\(\mathcal{Description}\) OurTeam & OurOJ. 给定一棵 \(n\) 个顶点的树,每个顶点标有字符 ( 或 ).将从 \(u\) 到 \(v\) ...
- Solution -「LOCAL」过河
\(\mathcal{Description}\) 一段坐标轴 \([0,L]\),从 \(0\) 出发,每次可以 \(+a\) 或 \(-b\),但不能越出 \([0,L]\).求可达的整点数. ...
- Solution -「LOCAL」Drainage System
\(\mathcal{Description}\) 合并果子,初始果子的权值在 \(1\sim n\) 之间,权值为 \(i\) 的有 \(a_i\) 个.每次可以挑 \(x\in[L,R]\) ...
- Solution -「LOCAL」Burning Flowers
灼之花好评,条条生日快乐(假装现在 8.15)! \(\mathcal{Description}\) 给定一棵以 \(1\) 为根的树,第 \(i\) 个结点有颜色 \(c_i\) 和光亮值 ...
- Solution -「LOCAL」画画图
\(\mathcal{Description}\) OurTeam. 给定一棵 \(n\) 个点的树形随机的带边权树,求所有含奇数条边的路径中位数之和.树形生成方式为随机取不连通两点连边直到全 ...
- Solution -「LOCAL」ZB 平衡树
\(\mathcal{Description}\) OurOJ. 维护一列二元组 \((a,b)\),给定初始 \(n\) 个元素,接下来 \(m\) 次操作: 在某个位置插入一个二元组: 翻 ...
- Solution -「LOCAL」舟游
\(\mathcal{Description}\) \(n\) 中卡牌,每种三张.对于一次 \(m\) 连抽,前 \(m-1\) 次抽到第 \(i\) 种的概率是 \(p_i\),第 \(m\) ...
- Solution -「LOCAL」充电
\(\mathcal{Description}\) 给定 \(n,m,p\),求序列 \(\{a_n\}\) 的数量,满足 \((\forall i\in[1,n])(a_i\in[1,m])\l ...
随机推荐
- jquery 的 ajax 传输 数组 ,但后台无法获取的 原因 与 解决 办法
1.前言 js传输数组到服务器 ,controller无法解析 ,打印结果是 null 2.原因 jQuery会调用jQuery.param序列化参数,源码是 jQuery.param( obj, t ...
- Nagios 请检查HTTP服务器关于该CGI的访问权限设置
无权查看任何主机的信息. 请检查HTTP服务器关于该CGI的访问权限设置. 搜索了一下方法 确保 htpasswd.user的所有组为nagios 解决办法: vi /usr/local/nagios ...
- 百度地图BMap实现在行政区域内做标注
使用环境 vue bmap.js element-ui 页面展示 前提步骤 在index中引入百度地图提供的js库 在使用的vue页面中实例化地图 <!-- 给id随便起给名字 --> & ...
- MongoDB-基础知识学习(一)
概述 最近mongodb在互联网的活跃度直线上升,并且我们公司也使用了mongoDB 3.6 作为生产重要的数据库,我们项目组要监控mongodb的op.log日志,在此整理以前学习的知识,为以后备份 ...
- 《剑指offer》面试题22. 链表中倒数第k个节点
问题描述 输入一个链表,输出该链表中倒数第k个节点.为了符合大多数人的习惯,本题从1开始计数,即链表的尾节点是倒数第1个节点.例如,一个链表有6个节点,从头节点开始,它们的值依次是1.2.3.4.5. ...
- Tomcat安装(启动)
1,安装 找到你需要用的Tomcat版本对应的zip压缩包,解压到需要安装的目录即可 2,目录介绍 bin 存放tomcat服务器的可执行程序 conf 存放tomcat服务器的配置文件 lib ...
- ansible command和shell的区别
1.command模块不支持管道符和变量等,如果要使用这些,需要shell模块. 2.在使用ansible中的时候,默认的模块是-m command,从而模块的参数不需要填写,直接使用即可
- 『无为则无心』Python函数 — 35、Python中的闭包
目录 1.闭包的概念 2.实现一个闭包 3.在闭包中外函数把临时变量绑定给内函数 4.闭包中内函数修改外函数局部变量 5.注意: 6.练习: 1.闭包的概念 请大家跟我理解一下,如果在一个函数的内部定 ...
- 介绍一个golang库:fastcache
学习VictoriaMetrics源码的时候发现,VictoriaMetrics的缓存部分,使用了同一产品下的fastcache.下面分享阅读fastcache源码的的结论: 1.官方介绍 fastc ...
- 布客·ApacheCN 编程/后端/大数据/人工智能学习资源 2020.9
公告 ApacheCN 项目的最终目标:五年内备份并翻译 Github 上的所有教程(其实快被我们啃完了,剩下的不多了). 警告各位培训班:对 ApacheCN 宣传文章的举报,也将视为对 Apach ...