CF 293 E Close Vertices (树的分治+树状数组)
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove
题目:给出一棵树,问有多少条路径权值和不大于w,长度不大于l。
http://codeforces.com/contest/293/problem/E
有男人八题很相似,但是多了一个限制。
同样 还是点分治,考虑二元组(到根的路径权值和,到根的路径长度)。
按第一维度排序之后,可以用two points查询权值小不大于w的,然后 用树状数组维护路径长度。
也就是第一个条件用two points,第二个条件用树状数组维护。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define lson step << 1
#define rson step << 1 | 1
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a , b)
#define lowbit(x) (x & (-x))
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
typedef long long LL;
const int N = 100005;
struct Edge {
int v , w , next;
}e[N << 1];
int n , l , w , tot , start[N];
int del[N] = {0} , size[N];
LL ans = 0LL;
void _add (int u , int v , int w) {
e[tot].v = v ; e[tot].next = start[u];
e[tot].w = w;
start[u] = tot ++;
}
void add (int u , int v , int w) {
_add (u , v , w);
_add (v , u , w);
}
void calsize (int u , int pre) {
size[u] = 1;
for (int i = start[u] ; i != -1 ; i = e[i].next) {
int v = e[i].v;
if (v == pre || del[v]) continue;
calsize (v , u);
size[u] += size[v];
}
}
int totalsize , maxsize , rootidx;
void dfs (int u , int pre) {
int mx = totalsize - size[u];
for (int i = start[u] ; i != -1 ; i = e[i].next) {
int v = e[i].v;
if (v == pre || del[v]) continue;
mx = max (mx , size[v]);
dfs (v , u);
}
if (mx < maxsize) maxsize = mx , rootidx = u;
}
int search (int r) {
calsize (r , -1);
totalsize = size[r];
maxsize = 1 << 30;
dfs (r , -1);
return rootidx;
}
vector<pair<int,int> > sub[N] , all;
int idx , dist[N] , cnt[N];
void gao (int u , int pre) {
all.pb(mp(dist[u] , cnt[u]));
sub[idx].pb(mp(dist[u] , cnt[u]));
for (int i = start[u] ; i != -1 ; i = e[i].next) {
int v = e[i].v , w = e[i].w;
if (v == pre || del[v]) continue;
dist[v] = dist[u] + w;
cnt[v] = cnt[u] + 1;
gao (v , u);
}
}
int s[N] , up;
void add (int x , int val) {
for (int i = x ; i <= up ; i += lowbit (i)) {
s[i] += val;
}
}
int ask (int x) {
int ret = 0;
for (int i = x ; i > 0 ; i -= lowbit (i)) {
ret += s[i];
}
return ret;
}
LL fuck (vector<pair<int , int> > &v) {
LL ret = 0;
up = 0;
for (int i = 0 ; i < v.size() ; i ++)
up = max (up , v[i].second);
for (int i = 1 ; i <= up ; i ++)
s[i] = 0;
for (int i = 0 ; i < v.size() ; i ++)
add (v[i].second , 1);
for (int i = 0 , j = v.size() - 1 ; i < v.size() ; i ++) {
while (j >= i && v[i].first + v[j].first > w) {
add (v[j].second , -1);
j --;
}
if (j < i) break;
ret += ask (min(up , (l - v[i].second)));
add (v[i].second , -1);
}
return ret;
}
void solve (int root) {
root = search (root);
del[root] = 1;
if (totalsize == 1) return ;
idx = 0 ;all.clear();
for (int i = start[root] ; i != -1 ; i = e[i].next) {
int v = e[i].v , w = e[i].w;
if (del[v]) continue;
sub[idx].clear();
dist[v] = w ; cnt[v] = 1;
gao (v , -1);
sort (sub[idx].begin() , sub[idx].end());
idx ++;
}
sort (all.begin() , all.end());
ans += fuck (all);
for (int i = 0 ; i < idx ; i ++) {
for (int j = 0 ; j < sub[i].size() ; j ++) {
if (sub[i][j].first <= w && sub[i][j].second <= l) {
ans ++;
}
}
ans -= fuck (sub[i]);
}
for (int i = start[root] ; i != -1 ; i = e[i].next) {
int v = e[i].v;
if (del[v]) continue;
solve (v);
}
}
int main () {
// freopen ("input.txt" , "r" , stdin);
// freopen ("output.txt" , "w" , stdout);
tot = 0;memset (start , -1 , sizeof(start));
scanf ("%d %d %d" , &n , &l , &w);
for (int i = 1 ; i < n ; i ++) {
int p , d;
scanf ("%d %d" , &p , &d);
add (i + 1 , p , d);
}
solve (1);
printf ("%I64d\n" , ans);
return 0;
}
CF 293 E Close Vertices (树的分治+树状数组)的更多相关文章
- 【bzoj3648】环套树+点分治+树状数组
tree 1s 128M by hzw czy神犇种了一棵树,他想知道地球的质量 给定一棵n个点的树,求树上经过点的个数≥K的路径数量ans 对于部分数据,树上某两点间会多出最多一条无向边 输入数据 ...
- hdu_4918_Query on the subtree(树的分治+树状数组)
题目链接:hdu_4918_Query on the subtree 题意: 给出一颗n个点的树,每个点有一个权值,有两种操作,一种是将某个点的权值修改为v,另一种是查询距离点u不超过d的点的权值和. ...
- [bzoj3155]Preprefix sum(树状数组)
3155: Preprefix sum Time Limit: 1 Sec Memory Limit: 512 MBSubmit: 1183 Solved: 546[Submit][Status] ...
- Codeforces 980E The Number Games - 贪心 - 树状数组
题目传送门 传送点I 传送点II 传送点III 题目大意 给定一颗有$n$个点的树,$i$号点的权值是$2^{i}$要求删去$k$个点,使得剩下的点仍然连通,并且总权值和最大,问删去的所有点的编号. ...
- CF293E Close Vertices 点分治+树状数组
开始zz写了一个主席树,后来发现写个树状数组就行~ #include <cstdio> #include <vector> #include <algorithm> ...
- HDU4918 Query on the subtree 点分治+树状数组
bobo has a tree, whose vertices are conveniently labeled by 1,2,…,n. At the very begining, the i-th ...
- bzoj3648: 寝室管理(环套树+点分治)
好题..写了两个半小时hh,省选的时候要一个半小时内调出这种题目还真是难= = 题目大意是给一棵树或环套树,求点距大于等于K的点对数 这里的树状数组做了一点变换.不是向上更新和向下求和,而是反过来,所 ...
- 【BZOJ-3648】寝室管理 环套树 + 树状数组 + 点分治
3648: 寝室管理 Time Limit: 40 Sec Memory Limit: 512 MBSubmit: 239 Solved: 106[Submit][Status][Discuss] ...
- CF #365 (Div. 2) D - Mishka and Interesting sum 离线树状数组
题目链接:CF #365 (Div. 2) D - Mishka and Interesting sum 题意:给出n个数和m个询问,(1 ≤ n, m ≤ 1 000 000) ,问在每个区间里所有 ...
随机推荐
- 关于PHPExcel
在学PHPExcel的时候,在网上查了很多资料,花了很多时间,下面是我想要分享给大家的,我找到的并进行了一定修改的亲身实践成功的资料,希望大家对大家有所帮助. 首先,需要下载PhpExcel资料,下载 ...
- 第二章——Serializable的使用(跨进程使用和Intent的传递对象)
一.Serializable类(JAVA本身具有的) 简介:Serializable是一个接口. 作用:是JAVA提供的序列化接口,实现序列化和反序列化的操作. 二.跨进程使用 1.事前准备 publ ...
- 在PHP代码中处理JSON 格式的字符串的两种方法:
总结: 在PHP代码中处理JSON 格式的字符串的两种方法: 方法一: $json= '[{"id":"1","name":"\u ...
- VB生成xml
Dim text As XmlText Dim doc As New XmlDocument '加入XML的声明段落 Dim node As XmlNode = doc.CreateXmlDeclar ...
- Datagridview控件实现分页功能
可以进行sql语句进行设置: 1.先新建一个窗体,一个DataGridView控件.两个label控件.两个Button控件 2.代码如下: using System; using Sy ...
- 客户端持久化解决方案: Web SQL
客户端持久化解决方案: Web SQL Web SQL 提供了一组使用 SQL 操作客户端数据库的 APIs, 不是 HTML5 规范的一部分,是一个独立的规范. 核心方法 openDatabase: ...
- Fsu0413's Qt builds
http://fsu0413.github.io/QtCompile/#!index.md
- mysql入库中文乱码问题
mysql> show variables like '%char%';+--------------------------+----------------------------+| Va ...
- UI线程与worker线程
也谈谈我对UI线程和worker线程的理解 UI线程又叫界面线程,能够响应操作系统的特定消息,包括界面消息.鼠标键盘消息.自定义消息等,是在普通的worker线程基础上加上消息循环来实现的,在这个消息 ...
- Android 弹出通知Toast的使用
//官方帮助文档:http://wear.techbrood.com/guide/topics/ui/notifiers/toasts.html <LinearLayout xmlns:andr ...