开始zz写了一个主席树,后来发现写个树状数组就行~

#include <cstdio>
#include <vector>
#include <algorithm>
#include <bits/stdc++.h>
#define N 100005
#define ll long long
#define setIO(s) freopen(s".in","r",stdin)
using namespace std;
int length,weight;
namespace BIT
{
int C[N];
int lowbit(int t)
{
return t&(-t);
}
void update(int x,int v)
{
for(;x<N;x+=lowbit(x)) C[x]+=v;
}
int query(int x)
{
int re=0;
for(;x>0;x-=lowbit(x)) re+=C[x];
return re;
}
};
ll answer;
struct Node
{
int u,d1,d2;
Node(int u=0,int d1=0,int d2=0):u(u),d1(d1),d2(d2){}
};
vector<Node>v;
bool cmp(Node a,Node b)
{
return a.d2<b.d2;
}
int n,edges,root,sn;
int hd[N],to[N<<1],nex[N<<1],val[N<<1];
int size[N],mx[N],vis[N],rt[N];
void add(int u,int v,int c)
{
nex[++edges]=hd[u],hd[u]=edges,to[edges]=v,val[edges]=c;
}
void getroot(int u,int ff)
{
size[u]=1,mx[u]=0;
for(int i=hd[u];i;i=nex[i])
if(to[i]!=ff&&!vis[to[i]])
getroot(to[i],u),size[u]+=size[to[i]],mx[u]=max(mx[u],size[to[i]]);
mx[u]=max(mx[u],sn-size[u]);
if(mx[u]<mx[root]) root=u;
}
// d1<=l d2<=w
void getdis(int u,int ff,int d1,int d2)
{
v.push_back(Node(u,d1,d2));
for(int i=hd[u];i;i=nex[i])
if(to[i]!=ff&&!vis[to[i]])
getdis(to[i],u,d1+1,d2+val[i]);
}
ll calc(int u,int D1,int D2)
{
getdis(u,0,D1,D2);
sort(v.begin(),v.end(),cmp);
ll re=0;
int i,j,l=0,r=(int)v.size()-1;
for(i=1;i<(int)v.size();++i) BIT::update(v[i].d1+1,1);
while(l<r)
{
if(v[l].d2+v[r].d2<=weight)
{
re+=(ll)BIT::query(length-v[l].d1+1);
++l,BIT::update(v[l].d1+1,-1);
}
else
{
BIT::update(v[r].d1+1,-1);
--r;
}
}
v.clear();
return re;
}
void solve(int u)
{
vis[u]=1;
answer+=calc(u,0,0);
for(int i=hd[u];i;i=nex[i]) if(!vis[to[i]]) answer-=calc(to[i],1,val[i]);
for(int i=hd[u];i;i=nex[i])
if(!vis[to[i]])
sn=size[to[i]],root=0,getroot(to[i],u),solve(root);
}
int main()
{
int i,j;
// setIO("input");
scanf("%d%d%d",&n,&length,&weight);
for(i=1;i<n;++i)
{
int a=i+1,b,c;
scanf("%d%d",&b,&c),add(a,b,c),add(b,a,c);
}
mx[root=0]=sn=n,getroot(1,0),solve(root);
printf("%lld\n",answer);
return 0;
}

  

CF293E Close Vertices 点分治+树状数组的更多相关文章

  1. BZOJ_3262_陌上花开_CDQ分治+树状数组

    BZOJ_3262_陌上花开_CDQ分治+树状数组 Description 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),用三个整数表示. 现在要对每朵花评级,一朵花的级别是它拥有的 ...

  2. 【BZOJ4553】[Tjoi2016&Heoi2016]序列 cdq分治+树状数组

    [BZOJ4553][Tjoi2016&Heoi2016]序列 Description 佳媛姐姐过生日的时候,她的小伙伴从某宝上买了一个有趣的玩具送给他.玩具上有一个数列,数列中某些项的值可能 ...

  3. BZOJ 1176 Mokia CDQ分治+树状数组

    1176: [Balkan2007]Mokia Time Limit: 30 Sec  Memory Limit: 162 MBSubmit: 1854  Solved: 821[Submit][St ...

  4. 【bzoj3262】陌上花开 CDQ分治+树状数组

    题目描述 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),又三个整数表示.现要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量.定义一朵花A比另一朵花B要美丽,当且仅当Sa&g ...

  5. 【bzoj2225】[Spoj 2371]Another Longest Increasing CDQ分治+树状数组

    题目描述 给定N个数对(xi, yi),求最长上升子序列的长度.上升序列定义为{(xi, yi)}满足对i<j有xi<xj且yi<yj. 样例输入 8 1 3 3 2 1 1 4 5 ...

  6. BZOJ_2253_[2010 Beijing wc]纸箱堆叠 _CDQ分治+树状数组

    BZOJ_2253_[2010 Beijing wc]纸箱堆叠 _CDQ分治+树状数组 Description P 工厂是一个生产纸箱的工厂.纸箱生产线在人工输入三个参数 n p a , , 之后, ...

  7. BZOJ_3295_[Cqoi2011]动态逆序对_CDQ分治+树状数组

    BZOJ_3295_[Cqoi2011]动态逆序对_CDQ分治+树状数组 Description 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数.给1到n的一 ...

  8. BZOJ_2225_[Spoj 2371]Another Longest Increasing_CDQ 分治+树状数组

    BZOJ_2225_[Spoj 2371]Another Longest Increasing_CDQ 分治+树状数组 Description        给定N个数对(xi, yi),求最长上升子 ...

  9. BZOJ_2683_简单题&&BZOJ_1176_[Balkan2007]Mokia_CDQ分治+树状数组

    BZOJ_2683_简单题&&BZOJ_1176_[Balkan2007]Mokia_CDQ分治+树状数组 Description 维护一个W*W的矩阵,初始值均为S.每次操作可以增加 ...

随机推荐

  1. The Maze

    There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...

  2. GS7 安装使用Oracle19c 客户端的说明

    1. 最近Oracle放出了 windows版本的oracle19c的安装文件(具体时间不详, 自己知道的时候比较晚了) 2. 发现文件其实比较多如图示: 3. 经过自己测试实现发现 不能使用  如下 ...

  3. [转帖]流言终结者 —— “SQL Server 是Sybase的产品而不是微软的”

    流言终结者 —— “SQL Server 是Sybase的产品而不是微软的” https://www.cnblogs.com/xxxtech/archive/2011/12/30/2307859.ht ...

  4. Linux学习笔记(16)Linux前后台进程切换(fg/bg/jobs/ctrl+z)

    关键词:Linux前后台进程切换,linux进程切换 fg.bg.jobs.&.ctrl + z都是跟系统任务有关的,虽然现在基本上不怎么需要用到这些命令,但学会了也是很实用的一.& ...

  5. MySQL的库、表的详细操作

    目录 MySQL的库.表的详细操作 一 库操作 二 表操作 MySQL的库.表的详细操作 本节目录 一 库操作 1.创建数据库 1.1 语法 CREATE DATABASE 数据库名 charset ...

  6. python打印带颜色的字体

    在python开发的过程中,经常会遇到需要打印各种信息.海量的信息堆砌在控制台中,就会导致信息都混在一起,降低了重要信息的可读性.这时候,如果能给重要的信息加上字体颜色,那么就会更加方便用户阅读了. ...

  7. python-day9(正式学习)

    目录 深浅拷贝 拷贝 浅拷贝 深拷贝 异常处理 什么是异常 语法错误 逻辑错误 异常的种类 常用的异常 其他异常 异常处理 提前预防 事后预防 抛出异常(基本没用) 断言(调试用,现在基本上没用) 文 ...

  8. Hive 教程(三)-DDL基础

    DDL,Hive Data Definition Language,数据定义语言: 通俗理解就是数据库与库表相关的操作,本文总结一下基本方法 hive 数据仓库配置 hive 数据仓库默认位置在 hd ...

  9. 解决 mysql (10038)

    1.授权 mysql>grant all privileges on *.*  to  'root'@'%'  identified by 'youpassword'  with grant o ...

  10. Python 并发网络库

    Python 并发网络库 Tornado VS Gevent VS Asyncio Tornado:并发网络库,同时也是一个 web 微框架 Gevent:绿色线程(greenlet)实现并发,猴子补 ...