大意: 给定有根树, 每个点$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. Mybatis自增长id处理

    目录 1.使用useGenerateKey 2.使用select LAST_INSERT_ID() 3.使用select @@IDENTITY 4.在MySql中模拟Sequence 参考: 1.使用 ...

  2. sql server查看用户权限

    System.ServiceModel.FaultException: Server error. Detail: The EXECUTE permission was denied on the o ...

  3. 4-Four-Seeing hands

      ①Several cases have been reported in Russia recently of people who can read and detect colours wit ...

  4. BZOJ 3673: 可持久化并查集(可持久化并查集+启发式合并)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3673 题意: 思路: 可持久化数组可以用可持久化线段树来实现,并查集的查询操作和原来的一般并查集操作 ...

  5. 正则解析json数据

    http://tool.chinaz.com/regex http://tool.oschina.net/regex/

  6. java——File

    注意事项: 1:创建File对象需要导包, import java.io.File 2:File对象没有无参数构造.创建对象需要传参. 3:File类的对象,既可以代表文件也可以代表文件夹.   构造 ...

  7. flutter安装与配置 v1.2.1版本

    1---- 上面是下载地址https://flutter.dev/docs/development/tools/sdk/archive#windows 2---- 下载后,解压安装到C盘 3--- 测 ...

  8. 关于JavaScript和Java的区别和联系

    转载自: Javascript和Java除了名字和语法有点像,其他没有任何的关系. 做个比较是为了让大家更好的理解Javascript,事实上,两种语言根本没有可比性,是完全不同的.   Javasc ...

  9. 在不进入Guest OS的情况下,取得Guest OS的IP地址

    因为是个Headless 服务器,总是需要GUI VNC 到 Host OS, 然后进入里面的虚拟机,打 ipconfig / ifconfig  ,非常的不方便. 查了网上,找到上面的方法 1)确保 ...

  10. Ubuntu 16 , 从时间服务器更新时间

    因为在公司的内网,所以不能用Ubuntu默认的服务器去更新时间. 只能改成从网关 10.182.202.2 上取时间 1) 如果没有安装ntp 的话,先安装 apt-get install ntp 2 ...