Alyona and a tree CodeForces - 739B (线段树合并)
大意: 给定有根树, 每个点$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 (线段树合并)的更多相关文章
- 2016湖南省赛 I Tree Intersection(线段树合并,树链剖分)
2016湖南省赛 I Tree Intersection(线段树合并,树链剖分) 传送门:https://ac.nowcoder.com/acm/contest/1112/I 题意: 给你一个n个结点 ...
- [BZOJ 2212] [Poi2011] Tree Rotations 【线段树合并】
题目链接:BZOJ - 2212 题目分析 子树 x 内的逆序对个数为 :x 左子树内的逆序对个数 + x 右子树内的逆序对个数 + 跨越 x 左子树与右子树的逆序对. 左右子树内部的逆序对与是否交换 ...
- 【BZOJ2212】[POI2011]Tree Rotations (线段树合并)
题解: 傻逼题 启发式合并线段树里面查$nlog^2$ 线段树合并顺便维护一下$nlogn$ 注意是叶子为n 总结点2n 代码: #include <bits/stdc++.h> usin ...
- bzoj 2212 : [Poi2011]Tree Rotations (线段树合并)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2212 思路:用线段树合并求出交换左右儿子之前之后逆序对的数量,如果数量变小则交换. 实现 ...
- BZOJ2212 [Poi2011]Tree Rotations 【线段树合并】
题目链接 BZOJ2212 题解 一棵子树内的顺序不影响其与其它子树合并时的答案,这一点与归并排序的思想非常相似 所以我们只需单独处理每个节点的两棵子树所产生的最少逆序对即可 只有两种情况,要么正序要 ...
- P6847-[CEOI2019]Magic Tree【dp,线段树合并】
正题 题目链接:https://www.luogu.com.cn/problem/P6847 题目大意 \(n\)个点的一棵树上,每个时刻可以割掉一些边,一些节点上有果实表示如果在\(d_i\)时刻这 ...
- Z - New Year Tree CodeForces - 620E 线段树 区间种类 bitset
Z - New Year Tree CodeForces - 620E 这个题目还没有写,先想想思路,我觉得这个题目应该可以用bitset, 首先这个肯定是用dfs序把这个树转化成线段树,也就是二叉树 ...
- BZOJ 2212 [Poi2011]Tree Rotations(线段树合并)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2212 [题目大意] 给出一棵二叉树,每个叶节点上有一个权值,现在可以任意交换左右儿子, ...
- 「POI2011 R2 Day2」Tree Rotations【线段树合并】
题目链接 [BZOJ] [洛谷] [LOJ] 题解 由于是前序遍历,那么讨论一棵树上的逆序对的情况. 两个节点都在左子树上 两个节点都在右子树上 两个节点分别在不同的子树上. 前两种情况其实也可以归结 ...
随机推荐
- bsgs整理
目录 bsgs问题 或 poj2417: 概述 代码 exbsgs 鸣谢 \(gzy gzy gzy\) bsgs问题 或 poj2417: 给定质数\(p\),给定\(a\),\(b\),\((a, ...
- 大数乘法|2012年蓝桥杯B组题解析第六题-fishers
(9')大数乘法 对于32位字长的机器,大约超过20亿,用int类型就无法表示了,我们可以选择int64类型,但无论怎样扩展,固定的整数类型总是有表达的极限!如果对超级大整数进行精确运算呢?一个简单的 ...
- Lazarus分体式改成一体式窗口
安装包 anchordocking和Sparta_DockedFormEditor 然后点选保存并重新编译IDE即可
- 题解——Codeforces Round #508 (Div. 2) T1 (模拟)
依照题意暴力模拟即可A掉 #include <cstdio> #include <algorithm> #include <cstring> #include &l ...
- (转)Awesome GAN for Medical Imaging
Awesome GAN for Medical Imaging 2018-08-10 09:32:43 This blog is copied from: https://github.com/xin ...
- Visual Studio 安装easyX且导入graphics库后,outtextxy提示未定义标示符
1.点击 “项目” ,然后点击 “属性”. 2. 然后点击左侧 “配置与属性” 下的 “常规” ,在点击 “字符集” ,选择 “使用多字节字符集” 即可解决问题
- 并发学习一、MPI初步认识
学习参考地址:https://www.jianshu.com/p/2fd31665e816 编程使用的vs2015 社区版本(个人感觉比Vc6.0的丑界面看起来舒服多了) MPI基本函数 MPI调用借 ...
- Linux——vi的使用
记录一下vi的一些使用指令,蓝色部分是比较常用的,其中使用过的重新进行了描述,极少部分是未使用过的,还有一些未使用也未记录进来,后续再来补充修正: 参考资料:http://cn.linux.vbird ...
- uiautomatorviewer错误 unable toconnect to adb
报错信息: 如图为adb所在路径: 编辑uiautomatorviewer.bat文件,修改最后一行, 修改前: call "%java_exe%" "-Djava.ex ...
- js代码点击触发事件
js触发按钮点击事件 function load(){ //下面两种方法效果是一样的 document.getElementById("target").onclick(); do ...