转载请注明出处,谢谢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 (树的分治+树状数组)的更多相关文章

  1. 【bzoj3648】环套树+点分治+树状数组

    tree 1s 128M  by hzw czy神犇种了一棵树,他想知道地球的质量 给定一棵n个点的树,求树上经过点的个数≥K的路径数量ans 对于部分数据,树上某两点间会多出最多一条无向边 输入数据 ...

  2. hdu_4918_Query on the subtree(树的分治+树状数组)

    题目链接:hdu_4918_Query on the subtree 题意: 给出一颗n个点的树,每个点有一个权值,有两种操作,一种是将某个点的权值修改为v,另一种是查询距离点u不超过d的点的权值和. ...

  3. [bzoj3155]Preprefix sum(树状数组)

    3155: Preprefix sum Time Limit: 1 Sec  Memory Limit: 512 MBSubmit: 1183  Solved: 546[Submit][Status] ...

  4. Codeforces 980E The Number Games - 贪心 - 树状数组

    题目传送门 传送点I 传送点II 传送点III 题目大意 给定一颗有$n$个点的树,$i$号点的权值是$2^{i}$要求删去$k$个点,使得剩下的点仍然连通,并且总权值和最大,问删去的所有点的编号. ...

  5. CF293E Close Vertices 点分治+树状数组

    开始zz写了一个主席树,后来发现写个树状数组就行~ #include <cstdio> #include <vector> #include <algorithm> ...

  6. 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 ...

  7. bzoj3648: 寝室管理(环套树+点分治)

    好题..写了两个半小时hh,省选的时候要一个半小时内调出这种题目还真是难= = 题目大意是给一棵树或环套树,求点距大于等于K的点对数 这里的树状数组做了一点变换.不是向上更新和向下求和,而是反过来,所 ...

  8. 【BZOJ-3648】寝室管理 环套树 + 树状数组 + 点分治

    3648: 寝室管理 Time Limit: 40 Sec  Memory Limit: 512 MBSubmit: 239  Solved: 106[Submit][Status][Discuss] ...

  9. 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) ,问在每个区间里所有 ...

随机推荐

  1. PHP中将对数据库的操作,封装成一个工具类以及学会使用面向对象的方式进行编程

    <?php class SqlTool { //属性 private $conn; private $host="localhost"; private $user=&quo ...

  2. jQuery代码不能执行,必须在代码之前就要包含jQuery包

    <script>    $(function () {        $("#btnRegister").click(function () {            ...

  3. delphi 打开文件夹并定位到一个文件(使用ShellExecute时加一个select参数,原来这么简单!)

    strFileName := FcxLV[nIndex].Items.Item[FcxLV[nIndex].ItemIndex].SubItems.Strings[0]; //路径  ShellExe ...

  4. ORACLE RAC中的oc4j和gsd资源以及RAC相关的进程

    1.RAC相比单实例数据库多出的进程: LMS - Gobal Cache Service Process 全局缓存服务进程 LMD - Global Enqueue Service Daemon 全 ...

  5. 【转】浅析terminal创建时ptmx和pts关系

      我们打开一个terminal,那么将会在devpts文件系统/dev/pts下创建一个对应的pts字符文件,该pts字符文件节点直接由/dev/ptmx节点的驱动函数ptmx_open()调用de ...

  6. 判断一个字符串中是否包含另一个字符串(KMP、BF)

    判断一个字符串是否是另一个字符串的子串,也就是strstr()函数的实现,简单的实现方法是BF算法. 1.BF算法 int BF(char *s, char *p){ ; ; int j; while ...

  7. 将Controller中的数据传递到View中显示

    如何将Controller 中的数据传送到View 步骤: (1)要有数据,如果要用到对象可以在Model 中定义对应的类 (2)要有装数据的容器: System.Text.StringBuilder ...

  8. 从头开始-01.C语言环境测试

    在Mac下编写C程序需要以下几步: 编写代码 a>编译:把C语言编译成0和1 b>工具:clang编译器 c>指令:cc -c 文件名.c      编译成功会生成一个. o目标文件 ...

  9. mysql distinct&group by 应用

    在使用mysql时,有时需要查询出某个字段不重复的记录,虽然mysql提供 有distinct这个关键字来过滤掉多余的重复记录只保留一条,但往往只用它来返回不重复记录的条数,而不是用它来返回不重记录的 ...

  10. js清空页面控件值

    function funClear() {var txts = document.getElementsByTagName("input");for (var i = 0; i & ...