codeforces293E (树上点分治+树状数组)
和poj1747相比起来,只不过是限制条件多了一维。
而多了这一维,所以需要用树状数组来维护,从而快速得到答案。
因为没注意传进树状数组函数的参数可能是<=0的,导致超时了好久。
#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <bitset>
#include <algorithm>
#include <iostream>
#include <string>
#include <functional>
const int INF = << ;
typedef __int64 LL;
/**/
const int N = + ; int n, L, W;
struct Edge
{
int to, dis, next;
}g[N*];
struct Node
{
int l, w;
bool operator<(const Node&rhs)const
{
return w < rhs.w;
}
}a[N];
int head[N], e;
int tree[N];
int size[N], p, total, mins, root;
bool vis[N];
LL ans;
int maxL;
int lowbit(int x)
{
return x &(-x);
} //树状数组如果pos<=0,那么会死循环, 卡这里超时了好久。
void modify(int pos, int val)
{
pos += ;
while (pos <= maxL+)
{
tree[pos] += val;
pos += lowbit(pos);
}
}
int getSum(int pos)
{
pos += ;
if (pos <= ) return ;
int ret = ;
while (pos > )
{
ret += tree[pos];
pos -= lowbit(pos);
}
return ret;
} void addEdge(int u, int v, int dis)
{
g[e].to = v;
g[e].dis = dis;
g[e].next = head[u];
head[u] = e++;
} void getRoot(int u, int fa)
{
int maxs = ;
size[u] = ;
for (int i = head[u];i != -;i = g[i].next)
{
int v = g[i].to;
if (v == fa || vis[v]) continue;
getRoot(v, u);
size[u] += size[v];
maxs = std::max(maxs, size[v]);
}
maxs = std::max(maxs, total - size[u]);
if (mins > maxs)
{
mins = maxs;
root = u;
}
}
void getA(int u, int fa, int l, int w)
{
a[p].l = l;
a[p++].w = w;
maxL = std::max(maxL, l);
for (int i = head[u];i != -;i = g[i].next)
{
int v = g[i].to;
if (v == fa || vis[v]) continue;
getA(v, u, l + , w + g[i].dis);
}
} LL counts(int u, int ll, int ww)
{
p = ;
maxL = ;
getA(u, -, ll, ww);
std::sort(a, a + p);
int l = , r = p - ;
LL ret = ;
while (l < r &&a[l].w + a[r].w>W)
r--;
if (l < r)
{
for (int i = l+;i <= r;++i)
modify(a[i].l,);
while (l < r)
{
if (a[l].w + a[r].w <= W)
{
ret += getSum(std::min(L - a[l].l,maxL));
l++;
modify(a[l].l,-);
}
else
{
modify(a[r].l,-);
r--;
}
}
}
return ret;
} void go(int u)
{
vis[u] = true;
ans += counts(u, , );
for (int i = head[u];i != -;i = g[i].next)
{
int v = g[i].to;
if (vis[v]) continue;
ans -= counts(v, , g[i].dis);
mins = INF;
total = size[v];
getRoot(v, -);
go(root);
}
}
int main()
{
scanf("%d%d%d", &n, &L, &W); int u, v, dis;
memset(head, -, sizeof(head));
for (int i = ;i < n;++i)
{
u = i + ;
scanf("%d%d", &v, &dis);
addEdge(u, v, dis);
addEdge(v, u, dis);
}
mins = INF;
total = n;
getRoot(, -);
go(root);
printf("%I64d\n", ans); return ;
}
codeforces293E (树上点分治+树状数组)的更多相关文章
- BZOJ_3262_陌上花开_CDQ分治+树状数组
BZOJ_3262_陌上花开_CDQ分治+树状数组 Description 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),用三个整数表示. 现在要对每朵花评级,一朵花的级别是它拥有的 ...
- 【BZOJ4553】[Tjoi2016&Heoi2016]序列 cdq分治+树状数组
[BZOJ4553][Tjoi2016&Heoi2016]序列 Description 佳媛姐姐过生日的时候,她的小伙伴从某宝上买了一个有趣的玩具送给他.玩具上有一个数列,数列中某些项的值可能 ...
- BZOJ 1176 Mokia CDQ分治+树状数组
1176: [Balkan2007]Mokia Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 1854 Solved: 821[Submit][St ...
- 【bzoj3262】陌上花开 CDQ分治+树状数组
题目描述 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),又三个整数表示.现要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量.定义一朵花A比另一朵花B要美丽,当且仅当Sa&g ...
- 【BZOJ3460】Jc的宿舍(树上莫队+树状数组)
点此看题面 大致题意: 一棵树,每个节点有一个人,他打水需要\(T_i\)的时间,每次询问两点之间所有人去打水的最小等待时间. 伪·强制在线 这题看似强制在线,但实际上,\(pre\ mod\ 2\) ...
- 【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 ...
- BZOJ_2253_[2010 Beijing wc]纸箱堆叠 _CDQ分治+树状数组
BZOJ_2253_[2010 Beijing wc]纸箱堆叠 _CDQ分治+树状数组 Description P 工厂是一个生产纸箱的工厂.纸箱生产线在人工输入三个参数 n p a , , 之后, ...
- BZOJ_3295_[Cqoi2011]动态逆序对_CDQ分治+树状数组
BZOJ_3295_[Cqoi2011]动态逆序对_CDQ分治+树状数组 Description 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数.给1到n的一 ...
- BZOJ_2225_[Spoj 2371]Another Longest Increasing_CDQ 分治+树状数组
BZOJ_2225_[Spoj 2371]Another Longest Increasing_CDQ 分治+树状数组 Description 给定N个数对(xi, yi),求最长上升子 ...
随机推荐
- C++ Primer 学习笔记_60_重载操作符与转换 --赋值、下标、成员訪问操作符
重载操作符与转换 --赋值.下标.成员訪问操作符 一.赋值操作符 类赋值操作符接受类类型形參,通常该形參是对类类型的const引用,但也能够是类类型或对类类型的非const引用.假设未定义这个操作符, ...
- ASP.NET - 在线编辑器(KindEditor)
效果: 项目结构: 前端代码: <%@ Page Language="C#" AutoEventWireup="true" CodeFile=" ...
- Call Transaction 小节
采购订单: . CALL FUNCTION ‘ME_DISPLAY_PURCHASE_DOCUMENT’ EXPORTING i_ebeln = itab-ebeln EXCEPTIONS not_f ...
- ABAP函数:VIEW_MAINTENANCE_CALL(维护表视图等)
function:VIEW_MAINTENANCE_CALL 功能:维护表视图等 The function module calls the extended table maintenance (V ...
- Swift - 选择框(UIPickerView)的用法
1,选择框可以让用户以滑动的方式选择值.示例如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 ...
- apache的开源项目-模板引擎(Velocity)(转)
然后修改conf文件下的server.xml文件,在server.xml里的 <Connector port="8080" .... />字段后 ...
- Gap 锁
14.3.1 InnoDB Locking InnoDB 锁 本章节描述InnoDB 使用的锁类型: Shared and Exclusive Locks Intention Locks Record ...
- C++ 多源码文件简单组织
C++ 多源码文件简单组织 基本上和C的是一样的,只不过C++的方法要在类中声明.看一个简单实例.ainimal.h 类里面对外公开的信息. 点击(此处)折叠或打开 #ifndef _ANIMAL_ ...
- 由一道淘宝面试题到False sharing问题
今天在看淘宝之前的一道面试题目,内容是 在高性能服务器的代码中经常会看到类似这样的代码: typedef union { erts_smp_rwmtx_t rwmtx; byte cache_line ...
- poj 3450 Corporate Identity
题目链接:http://poj.org/problem?id=3450 题目分类:后缀数组 题意:求n个串的最长公共字串(输出字串) //#include<bits/stdc++.h> # ...