codeforces 293E Close Vertices
正解:点分治+树状数组。
点分治板子题,直接点分以后按照$w$排序,扫指针的时候把$w$合法的路径以$l$为下标加入树状数组统计就行了。
写这道题只是想看看我要写多久。。事实证明我确实是老年选手了,这种傻逼题写+调竟然用了$40min$。。
#include <bits/stdc++.h>
#define il inline
#define RG register
#define ll long long
#define lb(x) (x & -x)
#define N (300005) using namespace std; struct edge{ int nt,to,dis; }g[N];
struct data{ int l,w; }st[N]; int head[N],vis[N],dis[N],len[N],son[N],sz[N],c[N],l,w,n,num,top;
ll ans; il int gi(){
RG int x=,q=; RG char ch=getchar();
while ((ch<'' || ch>'') && ch!='-') ch=getchar();
if (ch=='-') q=-,ch=getchar();
while (ch>='' && ch<='') x=x*+ch-'',ch=getchar();
return q*x;
} il void insert(RG int from,RG int to,RG int dis){
g[++num]=(edge){head[from],to,dis},head[from]=num; return;
} il int cmp(const data &a,const data &b){ return a.w<b.w; } il void add(RG int x,RG int v){
if (!x) c[]+=v; else for (;x<=n;x+=lb(x)) c[x]+=v; return;
} il int query(RG int x){
RG int res=c[]; for (;x;x^=lb(x)) res+=c[x]; return res;
} il void getrt(RG int x,RG int p,RG int &rt){
son[x]=,sz[x]=;
for (RG int i=head[x],v;i;i=g[i].nt){
v=g[i].to; if (v==p || vis[v]) continue;
getrt(v,x,rt),sz[x]+=sz[v],son[x]=max(son[x],sz[v]);
}
son[x]=max(son[x],son[]-sz[x]);
if (son[rt]>=son[x]) rt=x; return;
} il void getdis(RG int x,RG int p){
st[++top]=(data){len[x],dis[x]},sz[x]=;
for (RG int i=head[x],v;i;i=g[i].nt){
v=g[i].to; if (v==p || vis[v]) continue;
len[v]=len[x]+,dis[v]=dis[x]+g[i].dis;
getdis(v,x),sz[x]+=sz[v];
}
return;
} il void calc(RG int rt,RG int llim,RG int wlim,RG int fg){
len[rt]=llim,dis[rt]=wlim,getdis(rt,top=);
sort(st+,st+top+,cmp); RG int p=;
for (RG int i=top;i;--i){
while (p<=top && st[p].w+st[i].w<=w) add(st[p++].l,);
if (l>=st[i].l) ans+=fg*query(l-st[i].l);
}
for (RG int i=;i<p;++i) add(st[i].l,-); return;
} il void solve(RG int x,RG int S){
RG int rt=; son[]=S,getrt(x,,rt),vis[rt]=,calc(rt,,,);
for (RG int i=head[rt];i;i=g[i].nt)
if (!vis[g[i].to]) calc(g[i].to,,g[i].dis,-);
for (RG int i=head[rt];i;i=g[i].nt)
if (!vis[g[i].to]) solve(g[i].to,sz[g[i].to]);
return;
} int main(){
#ifndef ONLINE_JUDGE
freopen("vertices.in","r",stdin);
freopen("vertices.out","w",stdout);
#endif
n=gi(),l=gi(),w=gi();
for (RG int i=,p,d;i<=n;++i)
p=gi(),d=gi(),insert(p,i,d),insert(i,p,d);
solve(,n),cout<<(ans-n)/; return ;
}
codeforces 293E Close Vertices的更多相关文章
- CodeForces 293E Close Vertices 点分治
题目传送门 题意:现在有一棵树,每条边的长度都为1,然后有一个权值,求存在多少个(u,v)点对,他们的路劲长度 <= l, 总权重 <= w. 题解: 1.找到树的重心. 2.求出每个点到 ...
- Codeforces 293E 点分治+cdq
Codeforces 293E 传送门:https://codeforces.com/contest/293/problem/E 题意: 给你一颗边权一开始为0的树,然后给你n-1次操作,每次给边加上 ...
- ●CodeForce 293E Close Vertices
题链: http://codeforces.com/contest/293/problem/E题解: 点分治,树状数组 大致思路和 POJ 1741 那道点分治入门题相同, 只是因为多了一个路径的边数 ...
- CF 293E Close Vertices——点分治
题目:http://codeforces.com/contest/293/problem/E 仍旧是点分治.用容斥,w的限制用排序+两个指针解决, l 的限制就用树状数组.有0的话就都+1,相对大小不 ...
- Connecting Vertices CodeForces - 888F (图论,计数)
链接 大意: 给定邻接表表示两点是否可以连接, 要求将图连成树, 且边不相交的方案数 n范围比较小, 可以直接区间dp $f[l][r]$表示答案, $g[l][r]$表示区间[l,r]全部连通且l, ...
- [ CodeForces 1065 B ] Vasya and Isolated Vertices
\(\\\) \(Description\) 求一个\(N\)个点\(M\)条边的无向图,点度为 \(0\) 的点最多和最少的数量. \(N\le 10^5,M\le \frac {N\times ( ...
- codeforces 459E
codeforces 459E E. Pashmak and Graph time limit per test 1 second memory limit per test 256 megabyte ...
- Codeforces 734E. Anton and Tree 搜索
E. Anton and Tree time limit per test: 3 seconds memory limit per test :256 megabytes input:standard ...
- Educational Codeforces Round 6 E. New Year Tree dfs+线段树
题目链接:http://codeforces.com/contest/620/problem/E E. New Year Tree time limit per test 3 seconds memo ...
随机推荐
- MySQL的异步复制、全同步复制与半同步复制
异步复制 异步复制,主库将事务 Binlog 事件写入到 Binlog 文件中,此时主库只会通知一下 Dump 线程发送这些新的 Binlog,然后主库就会继续处理提交操作,而此时不会保证这些 Bin ...
- css3中Animation
CSS3我在5年之前就有用了,包括公司项目都一直在很前沿的技术. 最近在写慕课网的七夕主题,用了大量的CSS3动画,但是真的沉淀下来仔细的去深入CSS3动画的各个属性发现还是很深的,这里就写下关于帧动 ...
- css-图文案例
效果如下: 附上代码: <html> <head> <title>World</title> <style type="text/css ...
- angularjs ui-view多视口多层嵌套路由配置
最近研究了一下ui-view多层嵌套,整理了一下 1.最简单的ui-view用法 html部分: <ul class="nav navbar-nav"> <li ...
- Jquery对于input事件的处理
Jquery对于input事件的处理.获取此表单元素,以及其内容 .text()不能使用在表单选项上 要使用.val() input元素使用类似onblur(this),事件在function(obj ...
- CRM Online Outlook Client Configuration Wizard
CRM Outlook客户端满足和便捷了用户对office outlook和CRM两个程序的使用需求.通过CRM outlook 客户端,用户可以像在浏览器中访问CRM一样,流畅的读写CRM数据.同时 ...
- (项目积累的)SQL数据库点滴
最近的的系统用的数据库是mssql,软件mssql 2008 r2 1.存储过程:后勤的综合管理系统(后端内网访问)三层架构配套用的是存储过程,里面列表展示的都是用存储过程,如下: 1)数据库脚本 U ...
- Intel超低功耗CPU的一些信息
2015年底: Intel Braswell是专门针对超低功耗移动和桌面平台的一个家族,现有赛扬N3000/N3050/N3150.奔腾N3700四款型号,其中N300的热设计功耗只有区区4W,其他三 ...
- map合并,相同键对应的值相加
最近在做统计钱的计算时遇到的一个需求,需要将一个大类别下的每一种钱进行特定的运算然后获得六年的钱,最后将这些钱按照年份进行汇总,获得总得大类型的六年的钱,在这个过程中采用了这种方法,每次算得钱放在ma ...
- The operation names in the portType match the method names in the SEI or Web service implementation class.
The Endpoint validation failed to validate due to the following errors: :: Invalid Endpoint Interfac ...