大意: 给定有根树, 每个点$x$有权值$a_x$, 对于每个点$x$, 求出$x$子树内所有点$y$, 需要满足$dist(x,y)<=a_y$.

刚开始想错了, 直接打线段树合并了.....因为范围是$long \space long$常数极大, 空间很可能会被卡, 不过竟然过了. 实际上本题每个点对树链上的贡献是单调的, 直接二分就行了

放一下线段树合并代码

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc tr[o].l
#define rc tr[o].r
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head const int N = 2e5+10;
int n, tot, a[N], rt[N], ans[N];
struct _ {int to,w;};
vector<_> g[N];
ll d[N], L, R;
struct {int l,r,sum;} tr[N<<6];
typedef int&& dd; int merge(int x, int y) {
if (!x||!y) return x+y;
tr[x].l=merge(tr[x].l,tr[y].l);
tr[x].r=merge(tr[x].r,tr[y].r);
tr[x].sum+=tr[y].sum;
return x;
}
int query(int o, ll l, ll r, ll ql, ll qr) {
if (!o||ql<=l&&r<=qr) return tr[o].sum;
int ans = 0;
if (mid>=ql) ans+=query(ls,ql,qr);
if (mid<qr) ans+=query(rs,ql,qr);
return ans;
}
void update(int &o, ll l, ll r, ll x) {
if (!o) o=++tot;
++tr[o].sum;
if (l==r) return;
if (mid>=x) update(ls,x);
else update(rs,x);
}
void dfs(int x) {
for (auto &&e:g[x]) d[e.to]=d[x]+e.w,dfs(e.to);
L = min(L, d[x]), R = max(R, d[x]);
L = min(L, d[x]-a[x]), R = max(R, d[x]-a[x]);
}
void solve(int x) {
for (auto &&e:g[x]) {
solve(e.to); rt[x]=merge(rt[x],rt[e.to]);
}
ans[x] = query(rt[x],L,R,L,d[x]);
update(rt[x],L,R,d[x]-a[x]);
} int main() {
int &&t = n;
scanf("%d", &n);
REP(i,1,n) scanf("%d", a+i);
REP(i,2,n) {
int f, w;
scanf("%d%d", &f, &w);
g[f].pb({i,w});
}
dfs(1),solve(1);
REP(i,1,n) printf("%d ", ans[i]);hr;
}

Alyona and a tree CodeForces - 739B (线段树合并)的更多相关文章

  1. 2016湖南省赛 I Tree Intersection(线段树合并,树链剖分)

    2016湖南省赛 I Tree Intersection(线段树合并,树链剖分) 传送门:https://ac.nowcoder.com/acm/contest/1112/I 题意: 给你一个n个结点 ...

  2. [BZOJ 2212] [Poi2011] Tree Rotations 【线段树合并】

    题目链接:BZOJ - 2212 题目分析 子树 x 内的逆序对个数为 :x 左子树内的逆序对个数 + x 右子树内的逆序对个数 + 跨越 x 左子树与右子树的逆序对. 左右子树内部的逆序对与是否交换 ...

  3. 【BZOJ2212】[POI2011]Tree Rotations (线段树合并)

    题解: 傻逼题 启发式合并线段树里面查$nlog^2$ 线段树合并顺便维护一下$nlogn$ 注意是叶子为n 总结点2n 代码: #include <bits/stdc++.h> usin ...

  4. bzoj 2212 : [Poi2011]Tree Rotations (线段树合并)

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2212 思路:用线段树合并求出交换左右儿子之前之后逆序对的数量,如果数量变小则交换. 实现 ...

  5. BZOJ2212 [Poi2011]Tree Rotations 【线段树合并】

    题目链接 BZOJ2212 题解 一棵子树内的顺序不影响其与其它子树合并时的答案,这一点与归并排序的思想非常相似 所以我们只需单独处理每个节点的两棵子树所产生的最少逆序对即可 只有两种情况,要么正序要 ...

  6. P6847-[CEOI2019]Magic Tree【dp,线段树合并】

    正题 题目链接:https://www.luogu.com.cn/problem/P6847 题目大意 \(n\)个点的一棵树上,每个时刻可以割掉一些边,一些节点上有果实表示如果在\(d_i\)时刻这 ...

  7. Z - New Year Tree CodeForces - 620E 线段树 区间种类 bitset

    Z - New Year Tree CodeForces - 620E 这个题目还没有写,先想想思路,我觉得这个题目应该可以用bitset, 首先这个肯定是用dfs序把这个树转化成线段树,也就是二叉树 ...

  8. BZOJ 2212 [Poi2011]Tree Rotations(线段树合并)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2212 [题目大意] 给出一棵二叉树,每个叶节点上有一个权值,现在可以任意交换左右儿子, ...

  9. 「POI2011 R2 Day2」Tree Rotations【线段树合并】

    题目链接 [BZOJ] [洛谷] [LOJ] 题解 由于是前序遍历,那么讨论一棵树上的逆序对的情况. 两个节点都在左子树上 两个节点都在右子树上 两个节点分别在不同的子树上. 前两种情况其实也可以归结 ...

随机推荐

  1. 再谈fedora下的音乐和视频播放器的安装

    rpm包就相当于windows下的exe,已经是编译后的二进制代码,可以使用rpm命令或dnf install ???.rpm来安装 lnux下软件的安装跟windows不同, 后者要到处去找, 要做 ...

  2. Tutorial on word2vector using GloVe and Word2Vec

    Tutorial on word2vector using GloVe and Word2Vec 2018-05-04 10:02:53 Some Important Reference Pages ...

  3. 论文笔记之:Deep Attributes Driven Multi-Camera Person Re-identification

    Deep Attributes Driven Multi-Camera Person Re-identification 2017-06-28  21:38:55    [Motivation] 本文 ...

  4. Optical Flow related Tutorials

    Optical Flow related Tutorials 2017-04-01 10:50:55 Reference: 1. http://blog.csdn.net/carson2005/art ...

  5. React native 之设置IOS的图标,名称和启动图(下篇文章会讲到RN的android的相关设置)

    1.首先,app的名称: 如图所示:我的工程名叫BOOk 在BOOk下面的info.plist的文件里设置app的相关信息:比如Bundle name就是设置APP的名称 2.App的图标:(这里注意 ...

  6. 获取IP及判断IP是否在区间

    /// <summary> /// 获取客户端IP /// </summary> /// <returns></returns> public stat ...

  7. 彻底弄懂JS事件委托的概念和作用

    一.写在前头    接到某厂电话问什么是事件代理的时候,一开始说addEventListener,然后他说直接绑定新的元素不会报dom不存在的错误吗?然后我就混乱了,我印象中这个方法是可以绑定新节点的 ...

  8. 常见的Java面试题及答案整理

    1. 基础篇 1. 面向对象特征:封装,继承,多态和抽象 封装封装给对象提供了隐藏内部特性和行为的能力.对象提供一些能被其他对象访问的方法来改变它内部的数据.在 Java 当中,有 3 种修饰符: p ...

  9. eclipse中svn项目重定向地址

    有这种需求,一般是把项目提交到IP地址已经跟换的服务器,无法连接服务器再提交项目,比较着急. 我们看看怎么办: ② ③

  10. vue总是报缩进、空格的错

    在bulid/webpack.base.conf.js里 createLintingRule的内容注释掉