https://www.luogu.org/problemnew/show/P1600

nlogn竟然T了

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <map>
#include <vector> using namespace std;
const int N = ;
const int oo = ; #define lson jd << 1
#define rson jd << 1 | 1 #define yxy getchar()
#define one_ n <= 993
#define two_ n == 99994
#define three_ n == 99995 int head[N], pre[N], dis[N], tim[N], Answer[N], Askl[N], Askr[N];
bool vis[N];
int n, m, now = ;
struct Node {int u, v, w, nxt;}G[N];
queue <int> Q;
vector <int> Vt[N];
int bef[N], top[N], deep[N], size[N], fa[N], son[N], tree[N], spjs;
int L[N << ], R[N << ], W[N << ], F[N << ];
int ans; inline int read() {
int x = ; char c = yxy;
while(c < '' || c > '') c = yxy;
while(c >= '' && c <= '') x = x * + c - '', c = yxy;
return x;
} inline void add(int u, int v) {G[now].v = v;G[now].nxt = head[u]; head[u] = now ++;} inline void spfa(int start,int endd) {
for(int i = ; i <= n; i ++) dis[i] = oo, vis[i] = ;
dis[start] = ;
Q.push(start);
while(!Q.empty()) {
int topp = Q.front(); Q.pop();
vis[topp] = ;
for(int i = head[topp]; ~ i; i = G[i].nxt) {
if(dis[G[i].v] > dis[topp] + ) {
dis[G[i].v] = dis[topp] + ;
pre[G[i].v] = topp;
if(!vis[G[i].v]) {vis[G[i].v] = ; Q.push(G[i].v);}
}
}
}
} void calc(int start, int endd, int diss) {
int js = -; pre[start] = ;
while(endd) {
js ++;
if(tim[endd] == diss - js) Answer[endd] ++;
endd = pre[endd];
}
} void work_1() {
for(int i = ; i <= m; i ++) {
spfa(Askl[i], Askr[i]);
calc(Askl[i], Askr[i], dis[Askr[i]]);
}
} void work_2() {
for(int i = ; i <= m; i ++) Vt[Askl[i]].push_back(Askr[i]);
for(int i = ; i <= n; i ++) {
int L_ = i - tim[i], R_ = i + tim[i];
int siz_ = Vt[L_].size();
if(L_ > )
for(int j = ; j < siz_; j ++)
if(Vt[L_][j] >= i) Answer[i] ++;
siz_ = Vt[R_].size();
if(R_ <= n)
for(int j = ; j < siz_; j ++)
if(Vt[R_][j] <= i) Answer[i] ++;
}
} void Dfs_son(int u, int f_, int dep) {
printf("%d %d\n", u, f_);
fa[u] = f_;
deep[u] = dep;
size[u] = ;
for(int i = head[u]; ~ i; i = G[i].nxt) {
int v = G[i].v;
if(v != f_) {
Dfs_son(v, u, dep + );
if(size[v] > size[son[u]]) son[u] = v;
}
}
} void Dfs_un(int u, int tp){
top[u] = tp;
tree[u] = ++ spjs;
bef[spjs] = u;
if(!son[u]) return ;
Dfs_un(son[u], tp);
for(int i = head[u]; ~ i; i = G[i].nxt) {
int v = G[i].v;
if(v != fa[u] && v != son[u]) Dfs_un(v, v);
}
} void Sec_G(int l, int r, int jd, int x, int y) {
if(x <= l && r <= y) {
F[jd] ++; W[jd] += (r - l + ); return ;
}
int mid = (l + r) >> ;
if(x <= mid) Sec_G(l, mid, lson, x, y);
if(y > mid) Sec_G(mid + , r, rson, x, y);
W[jd] = W[lson] + W[rson];
} void down(int jd) {
F[lson] += F[jd]; F[rson] += F[jd];
W[lson] += (R[lson] - L[lson] + ) * F[jd];
W[rson] += (R[rson] - L[rson] + ) * F[jd];
F[jd] = ;
} inline void Sec_G_imp(int x, int y) {
int tp1 = deep[x], tp2 = deep[y];
while(tp1 != tp2) {
if(deep[tp1] < deep[tp2]) swap(x, y), swap(tp1, tp2);
Sec_G(, n, , tree[tp1], tree[x]);
x = fa[tp1];
tp1 = top[x];
}
if(deep[x] < deep[y]) swap(x, y);
Sec_G(, n, , tree[y], tree[x]);
return ;
} void Dfs_calc(int l, int r, int jd) {
if(l == r) {
if(deep[bef[l]] == tim[bef[l]]) Answer[bef[l]] = W[jd];
return ;
}
if(F[jd]) down(jd);
int mid = (l + r) >> ;
Dfs_calc(l, mid, lson);
Dfs_calc(mid + , r, rson);
} void work_3(){
Dfs_son(, , );
Dfs_un(, );
for(int i = ; i <= m; i ++) Sec_G_imp(Askl[i], Askr[i]);
Dfs_calc(, n, );
} int main()
{
n = read(); m = read();
for(int i = ; i <= n; i ++) head[i] = -;
for(int i = ; i <= n - ; i ++) {int u = read(); int v = read(); add(u, v); add(v, u);}
for(int i = ; i <= n; i ++) tim[i] = read();
for(int i = ; i <= m; i ++) Askl[i] = read(), Askr[i] = read();
if(one_) work_1();
else if(two_) work_2();
else if(three_) work_3();
for(int i = ; i <= n; i ++) printf("%d ", Answer[i]);
return ;
}

[Luogu] 1600的更多相关文章

  1. NOIP 2016 天天爱跑步 (luogu 1600 & uoj 261) - 线段树

    题目传送门 传送点I 传送点II 题目大意 (此题目不需要大意,我认为它已经很简洁了) 显然线段树合并(我也不知道哪来这么多显然) 考虑将每条路径拆成两条路径 s -> lca 和 t -> ...

  2. SQL 2008升级SQL 2008 R2完全教程或者10.00.1600升级10.50.1600

    http://blog.csdn.net/feng19821209/article/details/8571571 SQL 2008升级SQL 2008 R2完全教程或者10.00.1600升级10. ...

  3. SQL 2008 R2下载 升级R2 SP1或者SQL 2008从10.50.1600升级10.5.2500

    SQL Server 2008 R2 中英文 开发版/企业版/标准版 链接地址 一. 简体中文 1. SQL Server 2008 R2 Developer (x86, x64, ia64) - D ...

  4. Luogu 魔法学院杯-第二弹(萌新的第一法blog)

    虽然有点久远  还是放一下吧. 传送门:https://www.luogu.org/contest/show?tid=754 第一题  沉迷游戏,伤感情 #include <queue> ...

  5. luogu p1268 树的重量——构造,真正考验编程能力

    题目链接:http://www.luogu.org/problem/show?pid=1268#sub -------- 这道题费了我不少心思= =其实思路和标称毫无差别,但是由于不习惯ACM风格的题 ...

  6. Droidicon – 1600+ 漂亮的 Android 图标

    Droidicon 提供超过1600款定制图标,让你可以超级容易的把图标和徽章添加到您的应用程序中.你可以自定义你想要的各种形式的图标,给图标添加描边,透明度和颜色过滤器.包括这些 Google Ma ...

  7. 【BZOJ】1600: [Usaco2008 Oct]建造栅栏(dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1600 说好的今天开始刷水.. 本题一开始我以为是排列组合,但是自己弱想不出来,只想到了如果四边有一条 ...

  8. [luogu P2170] 选学霸(并查集+dp)

    题目传送门:https://www.luogu.org/problem/show?pid=2170 题目描述 老师想从N名学生中选M人当学霸,但有K对人实力相当,如果实力相当的人中,一部分被选上,另一 ...

  9. [luogu P2647] 最大收益(贪心+dp)

    题目传送门:https://www.luogu.org/problem/show?pid=2647 题目描述 现在你面前有n个物品,编号分别为1,2,3,--,n.你可以在这当中任意选择任意多个物品. ...

随机推荐

  1. Redis主从及Cluster区别及注意事项

    https://yq.aliyun.com/articles/647342 https://blog.csdn.net/biren_wang/article/details/78117392 http ...

  2. Git 常用命令 MD

    | Markdown版本笔记 | 我的GitHub首页 | 我的博客 | 我的微信 | 我的邮箱 || :------------: | :------------: | :------------: ...

  3. 创建多线程时,为何创建后需要Sleep?

    后面的线程用到了前面线程初始的结构,sleep一下只是做到了使前一个线程初始化完成后后一个线程才开始运行.但这样不严格,需要同步机制.在么多个线程都要用的数据结构不要放到子线程内部去初始化,程序启动时 ...

  4. Hackthebox--------irked

    菜鸟一枚,大佬轻喷!! Web页面就一张表情图和一句IRC is almost working!(是irc服务么??!!) 查看图片信息,貌似图片没这么简单.... 果然没这么简单,不行,得想办法得到 ...

  5. 解决input获取焦点时底部菜单被顶上来问题

    <div class="search-box"> <input class="search-input" type="text&qu ...

  6. 如何在Web应用里消费SAP Leonardo的机器学习API

    去年5月的时候,Jerry曾经写了一篇文章:使用Java程序消费SAP Leonardo的机器学习API,而最近另外做的一个项目,需要在Web应用里做同样的事情. 因为有了前一篇文章的铺垫,避免了很多 ...

  7. Linux命令——cat、more、less、head、tail

    cat 一次显示整个文件 -n:显示行号 -b :和 -n 相似,只不过对于空白行不编号 -s:当遇到有连续两行以上的空白行,就代换为一行的空白行 -E显示换行符 [root@localhost ~] ...

  8. Why do we name variables in Tensorflow?

    Reference:Stack Overflow. The name parameter is optional (you can create variables and constants wit ...

  9. 【Jenkins】忘记admin密码,可以用此方法帮你解决

    突然有一日发现自己忘记了jenkins的管理员密码,因为我一直登录的是另外一个非管理员账户.如果出现必须要使用管理员账户操作的,比如用户管理那里的,必须要管理员账号吧,这就尴尬了. 很方的我打开安装j ...

  10. #Python绘制 文本进度条,带刷新、时间暂缓的

    #Python绘制 文本进度条,带刷新.时间暂缓的 #文本进度条 import time as T st=T.perf_counter() print('-'*6,'执行开始','-'*6) maxx ...