题解 Defence
发现最少次数只和最左,最右及中间最长的全0段有关
本来想启发式合并,结果发现直接线段树合并搭配一个类似山海经的方法就可以过了
- yysy,线段树单次合并的具体复杂度并不是 \(O(logn)\) ,只在所有 \(n\) 棵线段树的总插入量为 \(n\) 次时全部合并才约为 \(O(nlogn)\)
Code:
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define N 100010
#define ll long long
#define pb push_back
//#define int long long
char buf[1<<21], *p1=buf, *p2=buf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf, 1, 1<<21, stdin)), p1==p2?EOF:*p1++)
inline int read() {
int ans=0, f=1; char c=getchar();
while (!isdigit(c)) {if (c=='-') f=-f; c=getchar();}
while (isdigit(c)) {ans=(ans<<3)+(ans<<1)+(c^48); c=getchar();}
return ans*f;
}
int n, m, q;
int head[N], size, ans[N];
vector<int> pos[N];
struct edge{int to, next;}e[N<<1];
inline void add(int s, int t) {edge* k=&e[++size]; k->to=t; k->next=head[s]; head[s]=size;}
namespace force{
vector<int> line[N];
void dfs(int u) {
for (int i=head[u],v; i; i=e[i].next) {
v = e[i].to;
dfs(v);
for (auto it:line[v]) line[u].pb(it);
}
for (auto it:pos[u]) line[u].pb(it);
sort(line[u].begin(), line[u].end());
int usize=unique(line[u].begin(), line[u].end())-line[u].begin();
line[u].resize(usize);
//cout<<"at "<<u<<": "; for (auto it:line[u]) cout<<it<<' '; cout<<endl;
int l, mid=0, r;
if (line[u].size()==0) {ans[u]=-1; return ;}
l=line[u][0]-1; r=m-line[u][line[u].size()-1];
for (vector<int>::iterator it=line[u].begin(); it+1!=line[u].end(); ++it) {
mid=max(mid, *(it+1)-*it-1);
}
//cout<<"line: "<<l<<' '<<mid<<' '<<r<<endl;
ans[u]=max(l+r, mid);
}
void solve() {
dfs(1);
for (int i=1; i<=n; ++i) printf("%d\n", ans[i]);
exit(0);
}
}
namespace task{
int siz[N], msiz[N], mson[N], l[N], mid[N], r[N];
const int SIZE=N*55;
int tl[SIZE], tr[SIZE], lst[SIZE], rst[SIZE], lmax[SIZE], rmax[SIZE], mmax[SIZE], lson[SIZE], rson[SIZE], rot[N], tot;
bool exi[SIZE];
#define tl(p) tl[p]
#define tr(p) tr[p]
#define lst(p) lst[p]
#define rst(p) rst[p]
#define lmax(p) lmax[p]
#define rmax(p) rmax[p]
#define mmax(p) mmax[p]
#define l(p) lson[p]
#define r(p) rson[p]
#define exi(p) exi[p]
void pushup(int p) {
exi(p)=exi(l(p))|exi(r(p));
if (!exi(p)) return ;
lst(p)=min(lst(l(p)), lst(r(p)));
rst(p)=max(rst(l(p)), rst(r(p)));
lmax(p)=lst(p)-tl(p); rmax(p)=tr(p)-rst(p);
mmax(p)=max(rmax(l(p))+lmax(r(p)), max(mmax(l(p)), mmax(r(p))));
//mmax(p)=max(lst(r(p))-rst(l(p))-1, max(mmax(l(p)), mmax(r(p))));
//cout<<"pushup "<<p<<' '<<tl(p)<<' '<<tr(p)<<' '<<lst(p)<<' '<<rst(p)<<' '<<lmax(p)<<' '<<rmax(p)<<' '<<mmax(p)<<endl;
}
void upd(int& p, int l, int r, int pos) {
if (!p) {p=++tot; tl(p)=l; tr(p)=r;}
if (l==r) {exi(p)=1; lst(p)=rst(p)=pos; return ;}
int mid=(l+r)>>1;
if (pos<=mid) upd(l(p), l, mid, pos);
else upd(r(p), mid+1, r, pos);
pushup(p);
}
int merge(int p1, int p2) {
if (!(p1&&p2)) return p1|p2;
if (tl(p1)==tr(p1)) {
exi(p1)|=exi(p2);
lst(p1)|=lst(p2);
rst(p1)|=rst(p2);
return p1;
}
l(p1)=merge(l(p1), l(p2));
r(p1)=merge(r(p1), r(p2));
pushup(p1);
return p1;
}
void dfs1(int u) {
siz[u]=pos[u].size();
for (int i=head[u],v; i; i=e[i].next) {
v = e[i].to;
dfs1(v);
siz[u]+=siz[v];
if (siz[v]>msiz[u]) msiz[u]=siz[v], mson[u]=v;
}
}
void dfs2(int u) {
//cout<<"dfs2 "<<u<<endl;
for (int i=head[u],v; i; i=e[i].next) {
v = e[i].to;
dfs2(v);
}
rot[u]=rot[mson[u]];
for (int i=head[u],v; i; i=e[i].next) {
v = e[i].to;
if (v==mson[u]) continue;
rot[u]=merge(rot[u], rot[v]);
}
for (auto it:pos[u]) upd(rot[u], 1, m, it);
//cout<<"line"<<u<<": "<<(lst(rot[u])-1)<<' '<<(m-rst(rot[u]))<<' '<<mmax(rot[u])<<endl;
ans[u] = max((lst(rot[u])-1)+(m-rst(rot[u])), mmax(rot[u]));
if (ans[u]>m) ans[u]=-1;
}
void solve() {
//memset(lst, 0x3f, sizeof(lst));
//cout<<double(sizeof(tl)*10+sizeof(siz)*5)/1024/1024<<endl;
lst(0)=INF;
dfs1(1); dfs2(1);
//cout<<"rst: "<<rst(rot[1])<<endl;
for (int i=1; i<=n; ++i) printf("%d\n", ans[i]);
exit(0);
}
}
signed main()
{
n=read(); m=read(); q=read();
for (int i=1,u,v; i<n; ++i) {
u=read(); v=read();
add(u, v);
}
for (int i=1,u; i<=q; ++i) {u=read(); pos[u].pb(read());}
//force::solve();
task::solve();
return 0;
}
题解 Defence的更多相关文章
- 2014-2015 ACM-ICPC, Asia Xian Regional Contest(部分题解)
摘要 本文主要给出了2014-2015 ACM-ICPC, Asia Xian Regional Contest的部分题解,说明了每题的题意.解题思路和代码实现,意即熟悉区域赛比赛题型. Built ...
- 【题解】Jury Compromise(链表+DP)
[题解]Jury Compromise(链表+DP) 传送门 题目大意 给你\(n\le 200\)个元素,一个元素有两个特征值,\(c_i\)和\(d_i\),\(c,d \in [0,20]\), ...
- $vjudge-dp$专题题解
因为感觉题解写不了多少,,,就懒得一道道题目慢慢写了,汇总了算了$QAQ$ 昂然后因为我估计以后还会有些什么$dp$专题啊$balabala$的,,,然后谢总肯定又会建一堆小组啥的,,,所以还是放个链 ...
- 8.8考试总结(NOIP模拟33)[Hunter·Defence·Connect]
无法逃避的是自我,而无法挽回的是过去. 前言 还算可以,不过 T1 少 \(\bmod\) 了一下挂了 25pts,T2 没看清题面挂了 27pts. 下回注意吧.. T1 Hunter 解题思路 感 ...
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...
- noip2016十连测题解
以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...
- BZOJ-2561-最小生成树 题解(最小割)
2561: 最小生成树(题解) Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1628 Solved: 786 传送门:http://www.lyd ...
- Codeforces Round #353 (Div. 2) ABCDE 题解 python
Problems # Name A Infinite Sequence standard input/output 1 s, 256 MB x3509 B Restoring P ...
- 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解
题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...
随机推荐
- Kotlin Coroutine(协程): 二、初识协程
@ 目录 前言 一.初识协程 1.runBlocking: 阻塞协程 2.launch: 创建协程 3.Job 4.coroutineScope 5.协程取消 6.协程超时 7.async 并行任务 ...
- netcore3.1 + vue (前后端分离) IIS 部署
1.安装 aspnetcore-runtime-3.1.1-win-x64.exe 2.安装dotnet-hosting-3.1.1-win.exe 3.安装urlrewrite和applicatio ...
- 交通规则:HOV车道
多乘员车道的限行时间一般为工作日上下班高峰,车上只有一个人时不能走该车道
- 如何快速更新长缓存的 HTTP 资源
前言 HTTP 缓存时间一直让开发者头疼.时间太短,性能不够好:时间太长,更新不及时.当遇到严重问题需紧急修复时,尽管后端文件可快速替换,但前端文件仍从本地缓存加载,导致更新长时间无法生效. 对于这个 ...
- ES6 数组Arrary 常用方法
ES6 数组Arrary 常用方法: <script type="text/javascript"> // 操作数据方法 // arr.push() 从后面添加元素,返 ...
- windows系统显示文字编码的CMD命令
chcp命令,显示.设置系统文字编码格式.
- Laravel使用artisan快速实现表单的登陆注册
1. 开发环境 macOS Mojave 10.14.6 XAMPP 5.6.38 Laravel 5.2 2. 在终端,先进入到项目根目录并执行执行命令 php artisan make:auth ...
- 【NOIP2007】Hanoi双塔问题
题目描述 给定A.B.C三根足够长的细柱,在A柱上放有2n个中间有孔的圆盘,共有n个不同的尺寸,每个尺寸都有两个相同的圆盘,注意这两个圆盘是不加区分的(下图为n=3的情形). 现要将这些圆盘移到C柱上 ...
- javascript学习(二)--函数
一.在JavaScript中,定义函数的方式如下: 1.第一种方式: function abs(x) { if (x >= 0) { return x; } else { return -x; ...
- [Vue warn]: Invalid prop: type check failed for prop "percentage". Expected Number, got Null
Vue组件报错 <ElProgress> at packages/progress/src/progress.vue 用了element组件 绑定数据时后端给我们传的参数为null,所以组 ...