http://codeforces.com/contest/293/problem/E

题意:求树上合法点对的个数。合法条件为:路径长度<=W,路径边数<=L。

显然是点分治。求解的时候第一维直接滑窗,第二维插入treap中统计,滑窗的时候删掉即可。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<set>
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define MS0(a) memset(a,0,sizeof(a))using namespace std;typedef long long ll;
const int maxn=1000100;
const int INF=1e9+10;int N;
ll L,W;
int p;ll w;
struct Edge
{int v;ll w;
};Edge e[maxn*2];int tot;
int first[maxn],Next[maxn*2];
bool vis[maxn];
struct PLL
{ll l,w;friend bool operator<(PLL A,PLL B){return A.l==B.l?A.w<B.w:A.l<B.l;}
};PLL d[maxn];int dn;
int rt,balance;void Init()
{tot=0;memset(first,-1,sizeof(first));
}void addedge(int u,int v,ll w)
{e[++tot]={v,w};Next[tot]=first[u];first[u]=tot;
}int get_rt(int u,int f,int sz)
{int cnt=1,balance1=0;for(int i=first[u];~i;i=Next[i]){int v=e[i].v;if(v==f||vis[v]) continue;int tmp=get_rt(v,u,sz);cnt+=tmp;balance1=max(balance1,tmp);}balance1=max(balance1,sz-cnt);if(balance1<balance){rt=u;balance=balance1;}return cnt;
}void dfs_d(int u,int f,ll len,ll sw)
{d[++dn]={len,sw};for(int i=first[u];~i;i=Next[i]){int v=e[i].v;ll w=e[i].w;if(v==f||vis[v]) continue;dfs_d(v,u,len+1,sw+w);}
}///---Treap
struct Node
{int ch[2];int v;int r;int sz;
};Node tr[4*maxn];int tot1;
int s[maxn],tot2;
int root;int newnode()
{int k;if(tot2) k=s[tot2--];else k=++tot1;tr[k].ch[0]=tr[k].ch[1]=-1;tr[k].v=0;tr[k].r=rand();tr[k].sz=1;return k;
}void up(int o)
{tr[o].sz=1;if(~tr[o].ch[0]) tr[o].sz+=tr[tr[o].ch[0]].sz;if(~tr[o].ch[1]) tr[o].sz+=tr[tr[o].ch[1]].sz;
}void rot(int &o,int d)
{int k=tr[o].ch[d^1];tr[o].ch[d^1]=tr[k].ch[d];tr[k].ch[d]=o;up(o);up(k);o=k;
}void Insert(int &o,ll x)
{if(o==-1){o=newnode();tr[o].v=x;return;}int d=x<tr[o].v?0:1;Insert(tr[o].ch[d],x);if(tr[tr[o].ch[d]].r>tr[o].r) rot(o,d^1);up(o);
}void Del(int &o,ll x)
{if(o==-1) return;if(tr[o].v==x){if(tr[o].ch[0]==-1) s[++tot2]=o,o=tr[o].ch[1];else if(tr[o].ch[1]==-1) s[++tot2]=o,o=tr[o].ch[0];else{int d=tr[tr[o].ch[0]].r>tr[tr[o].ch[1]].r?0:1;rot(o,d^1);Del(tr[o].ch[d^1],x);}if(~o) up(o);return;}if(x<tr[o].v) Del(tr[o].ch[0],x);else Del(tr[o].ch[1],x);up(o);
}ll query(int o,ll x)
{if(o==-1) return 0;if(x<tr[o].v) return query(tr[o].ch[0],x);ll ls=tr[o].ch[0]==-1?0:tr[tr[o].ch[0]].sz;return ls+1+query(tr[o].ch[1],x);
}ll calc(int u,int f,ll len,ll sw)
{dn=0;dfs_d(u,f,len,sw);sort(d+1,d+dn+1);ll res=0;root=-1;tot1=tot2=0;REP(i,1,dn) Insert(root,d[i].w);for(int l=1,r=dn;l<r;l++){while(r>l&&d[l].l+d[r].l>L) Del(root,d[r].w),r--;Del(root,d[l].w);res+=query(root,W-d[l].w);}return res;
}ll solve(int u)
{rt=u,balance=INF;int sz=get_rt(u,0,N);rt=u,balance=INF;get_rt(u,0,sz);u=rt;vis[u]=1;ll res=0;res+=calc(u,0,0,0);for(int i=first[u];~i;i=Next[i]){int v=e[i].v;ll w=e[i].w;if(vis[v]) continue;res-=calc(v,u,1,w);}for(int i=first[u];~i;i=Next[i]){int v=e[i].v;ll w=e[i].w;if(vis[v]) continue;res+=solve(v);}return res;
}int main()
{//test();return 0;freopen("in.txt","r",stdin);while(cin>>N>>L>>W){Init();REP(i,1,N-1){scanf("%d%I64d",&p,&w);addedge(p,i+1,w);addedge(i+1,p,w);}MS0(vis);printf("%I64d\n",solve(1));}return 0;
}

View Code

转载于:https://www.cnblogs.com/--560/p/5284716.html

codeforces 293E Close Vertices 点分治+滑窗+treap相关推荐

  1. 【CodeForces - 144C】Anagram Search(尺取,滑窗问题,处理字符串计数)

    题干: A string t is called an anagram of the string s, if it is possible to rearrange letters in t so ...

  2. 用Java实现Stream流处理中的滑窗

    2019独角兽企业重金招聘Python工程师标准>>> 简单地说,滑窗算法是一种移动固定大小的窗口(子列表)来遍历数据结构的方法,主要是基于固定步骤的序列流数据. 如果我们想通过使用 ...

  3. 风控特征—时间滑窗统计特征体系

    " 本文介绍了风控业务中构建时间滑窗特征的一些实践经验,是一篇既能让读者快速上手特征工程又能加深其业务理解的深度好文." 作者:求是汪在路上 来源:知乎专栏 风控模型算法. 编辑: ...

  4. leetcode 3. Longest Substring Without Repeating Characters | 3. 无重复字符的最长子串(双指针+滑窗)

    题目 https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题解 双指针+滑窗,维护一个 set, ...

  5. 风控特征:时间滑窗统计特征体系

    风控业务背景 俗话说, 路遥知马力,日久见人心.在风控中也是如此,我们常从时间维度提取借款人在不同时间点的特征,以此来判断借款人的风险.在实践中,这类特征通常会占到80%以上.由于是通过时间切片和聚合 ...

  6. 计蒜客 A2232.程序设计:蒜厂年会-单调队列(双端队列(STL deque)实现)滑窗维护最小前缀和...

    程序设计:蒜厂年会 问答问题反馈 只看题面 16.79% 1000ms 262144K 在蒜厂年会上有一个抽奖,在一个环形的桌子上,有 nn 个纸团,每个纸团上写一个数字,表示你可以获得多少蒜币.但是 ...

  7. TCP滑窗与拥塞控制

    TCP滑窗与拥塞控制 滑窗 TCP的拥塞控制由"慢启动(Slow start)"和"拥塞避免(Congestion avoidance)"组成 慢启动:从1个M ...

  8. 基于滑窗捕获的伪卫星系统抗远近效应方法研究

    [摘  要]通过滑窗捕获方法来对抗伪卫星系统中的远近效应,在伪卫星基站发射端进行分时隙发送信号,同时在接收端通过滑窗的方式增加一维时域的捕获.实验结果表明,在远近效应较强的情况下,滑窗捕获相关峰均比值 ...

  9. Leetcode滑窗系列(java):643. 子数组最大平均数 I

    Leetcode滑窗系列(java):643. 子数组最大平均数 I(新手小白仅供参考) 题目来源 leetcode 题目描述 个人思路 创建一个滑窗,将其值的和作为作为判断基准 然后滑窗的左右边界各 ...

最新文章

  1. moravec 角点检测
  2. 浅谈L0,L1,L2范数及其应用
  3. python3多进程 进程池 协程并发
  4. docker安装前提条件
  5. 创纪录!浪潮云海完成OpenStack Rocky版本全球最大规模单一集群测试!
  6. java环境变量设置详解_JAVA环境变量配置详解(Windows)
  7. 【java笔记】方法引用介绍和使用
  8. el-table实现动态表格插入行(开发小记)
  9. 班级网站的设计与实现
  10. 关于 Alpine Docker 镜像漏洞 CVE-2019-5021
  11. python转json的函数_python将字符串转换成json的方法小结
  12. foxmail客户单发送html活动页面(含链接跳转)邮件(发送 转发)
  13. 删除Windows启动管理器下的加载项
  14. mac brew安装php7.4
  15. CentOS7 查询版本/CPU/内存/硬盘容量等命令
  16. chown -R 用户名:组名 ./ 及 chown用法介绍
  17. matlab 2022更新
  18. [数据结构][Python]鸡尾酒排序、桶排序
  19. 【淘宝SEO技巧】深度揭秘淘宝搜索排名算法模型
  20. 软件项目管理-项目人力资源管理

热门文章

  1. 架构——大师言语汇总
  2. KlayGE SVN原生支持立体显示
  3. 第三章 “我要点爆”微信小程序云开发之点爆方式页面和爆炸之音页面制作
  4. 内嵌iframe撑高父容器,底部有4px留白问题解决办法
  5. javascript 点点滴滴 jquery
  6. 比较分析 Spring AOP 和 AspectJ 之间的差别
  7. UI Bootstrap
  8. JSON 基础解释.
  9. php alert 乱码
  10. 修正STM32F103ZE的散列文件