CF 293E Close Vertices——点分治
题目:http://codeforces.com/contest/293/problem/E
仍旧是点分治。用容斥,w的限制用排序+两个指针解决, l 的限制就用树状数组。有0的话就都+1,相对大小不变。
切勿每次memset!!!会T得不行。add(sta[ l ].len)即可,但要判一下(l==r)以防不测。(真的有那种数据!)
最后注意树状数组的范围是L(即L+1),不是n。不然可以尝试:
2 10 12
1 5
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
const int N=1e5+;
int n,L,W,hd[N],xnt,to[N<<],nxt[N<<],w[N<<];
int mn,rt,f[N],l,r,siz[N],lm;
ll ans;
bool vis[N];
struct Sta{
int w,len;Sta(int w=,int l=):w(w),len(l) {}
bool operator< (const Sta &b)const
{return w==b.w?len<b.len:w<b.w;}
}sta[N];
void add(int x,int y,int z)
{
to[++xnt]=y;nxt[xnt]=hd[x];w[xnt]=z;hd[x]=xnt;
to[++xnt]=x;nxt[xnt]=hd[y];w[xnt]=z;hd[y]=xnt;
}
void getrt(int cr,int fa,int s)
{
siz[cr]=;int mx=;
for(int i=hd[cr],v;i;i=nxt[i]) if(!vis[v=to[i]]&&v!=fa)
{
getrt(v,cr,s);siz[cr]+=siz[v];mx=max(mx,siz[v]);
}
mx=max(mx,s-siz[cr]);
if(mx<mn)mn=mx,rt=cr;
}
void add(int x,int k){x++;for(;x<=lm;x+=(x&-x))f[x]+=k;}//x<=L+1!!!
int query(int x){x++;int ret=;for(;x;x-=(x&-x))ret+=f[x];return ret;}
void dfs(int cr,int fa,int pw,int pl)
{
if(pw>W||pl>L)return;
sta[++r]=Sta(pw,pl);add(sta[r].len,);
for(int i=hd[cr],v;i;i=nxt[i]) if(!vis[v=to[i]]&&v!=fa)
dfs(v,cr,pw+w[i],pl+);
}
ll calc(int cr,int pw,int pl)
{
l=;r=;dfs(cr,,pw,pl);
sort(sta+l,sta+r+);///////
// printf("l=%d r=%d\n",l,r);
// for(int i=l;i<=r;i++)printf("sta[%d].w=%d .len=%d\n",i,sta[i].w,sta[i].len);
ll ret=;
while(l<r)
if(sta[l].w+sta[r].w>W)add(sta[r--].len,-);
else ret+=query(L-sta[l].len)-(sta[l].len<=L-sta[l].len),
// printf("l=%d r=%d query(%d)=%d\n"
// ,l,r,L-sta[l].len,query(L-sta[l].len)),
add(sta[l++].len,-);
if(l==r)add(sta[l].len,-);
// memset(f,0,sizeof f);///TLE!!!!!
return ret;
}
void solve(int cr,int s)
{
// printf("cr=%d\n",cr);
vis[cr]=;ans+=calc(cr,,);
// printf("cr=%d ans=%lld\n",cr,ans);
for(int i=hd[cr],v;i;i=nxt[i]) if(!vis[v=to[i]])
{
ans-=calc(v,w[i],);
// printf("(cr=%d ans=%lld)(v=%d)\n",cr,ans,v);
int ts=(siz[cr]>siz[v]?siz[v]:s-siz[cr]);
mn=N;getrt(v,,ts);solve(rt,ts);
}
}
int main()
{
scanf("%d%d%d",&n,&L,&W);lm=L+;
for(int i=,y,z;i<=n;i++)
{
scanf("%d%d",&y,&z);add(i,y,z);
}
mn=N;getrt(,,n);solve(rt,n);
printf("%I64d\n",ans);
return ;
}
CF 293E Close Vertices——点分治的更多相关文章
- CodeForces 293E Close Vertices 点分治
题目传送门 题意:现在有一棵树,每条边的长度都为1,然后有一个权值,求存在多少个(u,v)点对,他们的路劲长度 <= l, 总权重 <= w. 题解: 1.找到树的重心. 2.求出每个点到 ...
- codeforces 293E Close Vertices
题目链接 正解:点分治+树状数组. 点分治板子题,直接点分以后按照$w$排序,扫指针的时候把$w$合法的路径以$l$为下标加入树状数组统计就行了. 写这道题只是想看看我要写多久..事实证明我确实是老年 ...
- CF 716E. Digit Tree [点分治]
题意:一棵树,边上有一个个位数字,走一条路径会得到一个数字,求有多少路径得到的数字可以整除\(P\) 路径统计一般就是点分治了 \[ a*10^{deep} + b \ \equiv \pmod P\ ...
- ●CodeForce 293E Close Vertices
题链: http://codeforces.com/contest/293/problem/E题解: 点分治,树状数组 大致思路和 POJ 1741 那道点分治入门题相同, 只是因为多了一个路径的边数 ...
- CF293E Close Vertices 点分治+树状数组
开始zz写了一个主席树,后来发现写个树状数组就行~ #include <cstdio> #include <vector> #include <algorithm> ...
- CF 293 E Close Vertices (树的分治+树状数组)
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 题目:给出一棵树,问有多少条路径权值和不大于w,长 ...
- cf293E Close Vertices(树分治+BIT)
E. Close Vertices You've got a weighted tree, consisting of n vertices. Each edge has a non-negative ...
- Codeforces 293E 点分治+cdq
Codeforces 293E 传送门:https://codeforces.com/contest/293/problem/E 题意: 给你一颗边权一开始为0的树,然后给你n-1次操作,每次给边加上 ...
- CF 322E - Ciel the Commander 树的点分治
树链剖分可以看成是树的边分治,什么是点分治呢? CF322E - Ciel the Commander 题目:给出一棵树,对于每个节点有一个等级(A-Z,A最高),如果两个不同的节点有相同等级的父节点 ...
随机推荐
- 九度OJ 1182:统计单词 (计数)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3920 解决:1443 题目描述: 编一个程序,读入用户输入的,以"."结尾的一行文字,统计一共有多少个单词,并分别输出 ...
- How to avoid Over-fitting using Regularization?
http://www.mit.edu/~9.520/scribe-notes/cl7.pdf https://en.wikipedia.org/wiki/Bayesian_interpretation ...
- PAT 1064. 朋友数(20)
如果两个整数各位数字的和是一样的,则被称为是“朋友数”,而那个公共的和就是它们的“朋友证号”.例如123和51就是朋友数,因为1+2+3 = 5+1 = 6,而6就是它们的朋友证号.给定一些整数,要求 ...
- Android Screen Orientation
Ref:Android横竖屏切换小结 Ref:Android游戏开发之横竖屏的切换(二十七)
- python webserver客户端
1.库 suds库,只能做webserver客户端,轻量化,使用方便.安装使用pip. 2.使用 如有webserver情况如下: url:http://10.110.35.41:8980/wsser ...
- hadoop1.2.1 datanode 由于权限无法启动 expected: rwxr-xr-x
/************************************************************ STARTUP_MSG: Starting DataNode STARTUP ...
- ubuntu14.04 pygame安装 python2.7
系统:ubuntu14.04 LTS amd64python版本:2.7.6pygame版本:1.9.1release别这种方法了,这么安装不知道什么原因就出现了问题,在使用pygame.image. ...
- 《程序员代码面试指南》第三章 二叉树问题 判断t1 树中是否有与t2 树拓扑结构完全相同的子树
题目 判断t1 树中是否有与t2 树拓扑结构完全相同的子树 java代码 package com.lizhouwei.chapter3; /** * @Description: 判断t1 树中是否有与 ...
- 第四篇、javascript
一.正则表达式 提示:此专题需要多轮复习反复的加深和理解 正则表达式的两种用法: 1)regexp.xxx(string); 2)string.yyy(regexp); 验证用户输入的手机号格式是否合 ...
- javascript数字时钟
<html> <head> <script type="text/javascript"> function startTime() { var ...