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最高),如果两个不同的节点有相同等级的父节点 ...
随机推荐
- GridView 显示行号 设置行号列的宽度
/// <summary> /// GridView 显示行号 设置行号列的宽度 /// </summary> /// <param name="gv" ...
- [Errno 14] PYCURL ERROR 7 - "couldn't connect to host"
该问题就是防火墙的问题.关闭即可. /etc/init.d/iptables stop
- 洛谷 P2051 [AHOI2009]中国象棋
题目描述 这次小可可想解决的难题和中国象棋有关,在一个N行M列的棋盘上,让你放若干个炮(可以是0个),使得没有一个炮可以攻击到另一个炮,请问有多少种放置方法.大家肯定很清楚,在中国象棋中炮的行走方式是 ...
- struts2 获取表单数据封装到list和map集合
一.获取封装表单数据到list集合 示例 获取用户输入的用户名和密码并输出用户名. jsp页面 list[0]表示list中的第一个user对象 Java代码 二.封装表单数据到map集合 示例 获取 ...
- Linux踢出已登录用户
1.使用w命令可以查看当前登录系统的所有用户 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root tty1 - 08:05 4:29 0.09s 0.09s - ...
- maven编译问题-maven项目运行时找不到文件,解决方案之一
问题描述:以上信息是tomcat在启动项目的时候报的错误信息,发现没有找到配置文件,实际上配置文件在项目中是存在的,但是,在编译过程中,配置文件没有能加载到编译后的项目中.就造成了,找不到这些怕配置文 ...
- SpringBoot_集成Shiro后获取当前用户
//SecurityUtils.getSubject().getPrincipal(); 就可以获取了 protected User getCurrentUser(){ return (User) ...
- Spring MVC 通过反射将数据导出到excel
直接上代码 // 导出excel方法 @RequestMapping("exportExcel") public void exportExcel(HttpServletReque ...
- 【leetcode刷题笔记】Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- 跨平台(I版到K版)迁移实践总结
所谓跨平台迁移,对于了解openstack冷迁移过程的同学来说,其实就是手动去执行冷迁移的代码行为,当然像我这种抵制体力劳动的人,肯定会想写脚本去跑,即使不会也要边学边用. 迁移并非想象 ...