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最高),如果两个不同的节点有相同等级的父节点 ...
随机推荐
- Latent Activity Trajectory (LAT)
https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/funcZone_TKDE_Zheng.pdf Specific ...
- 我的Android进阶之旅------>Android中StateListDrawable支持的状态
Android中StateListDrawable支持的状态 android:state_active 代表是否处于激活状态 android:state_checked 代表是否处于已勾选状态 an ...
- 安装了包,pycharm却提示找不到包
这段时间,我爬虫爬到了一个论坛的数据,有个分析需要知道他的字符编码,因此使用到了 chardet,我在终端很顺利的安装了这个,但是在pycharm里使用的时候老是提示有错误,向下面这样: 其实这个是因 ...
- linux 基础-变量,shell基本语法
变量 定义变量 your_name="runoob.com" #变量名和等号之间不能有空格 使用变量 your_name="qinjx" echo $your_ ...
- js阻止a链接
<!DOCTYPE HTML> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 事件监听机制——列出指定目录内容、添加Dialog对话框
事件监听机制理解与Dialog练习 利用Java语言,仿照我的电脑目录进行打开目录,输入文件路径,查看该路径下所有的文件,设置两个文本框,一个转到按钮,当点击转到按钮时,查看路径是否正确,若正确在第二 ...
- web前端框架之自定义form表单验证
自定义form验证初试 .在后端创建一个类MainForm,并且在类中自定义host ip port phone等,然后写入方法,在post方法中创建MainForm对象,并且把post方法中的sel ...
- LINQ 学习路程 -- 查询操作 Distinct Except Intersect Union
Set Operators Usage Distinct 去掉集合的重复项 Except 返回两个集合的不同,第一个集合的元素不能出现在第二个集合中 Intersect 返回两个集合的交集,即元素同时 ...
- 算法(Algorithms)第4版 练习 1.3.1
package com.qiusongde; import java.util.Iterator; import java.util.NoSuchElementException; import ed ...
- Codeforces Round #335 (Div. 2) C. Sorting Railway Cars
C. Sorting Railway Cars time limit per test 2 seconds memory limit per test 256 megabytes input stan ...