嘟嘟嘟

题面很迷,看这个吧。

首先暴力很简单,从每一个叶子节点开始爬,直到那条特殊的边。

正解稍微想想就能搞出来:(x, y)这条特殊的边把整棵树分成了两部分,然后我们分别从x, y开始在他的那部分子树dfs,求出到达节点v时满足条件的一个区间。因为从v到u是向下取整,那么反过来合法的区间就是[k * (du[u] - 1), k * (du[u] - 1) + (du[u] - 1) - 1]。

递归到叶子节点后在原序列中二分找在[Min, Max]中的数的个数,乘上k,累加到答案中。

时间复杂度O(nlogn)。

 #include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-;
const int maxn = 1e6 + ;
const int MAX_NUM = 2e9;
inline ll read()
{
ll ans = ;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) {last = ch; ch = getchar();}
while(isdigit(ch)) {ans = ans * + ch - ''; ch = getchar();}
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < ) x = -x, putchar('-');
if(x >= ) write(x / );
putchar(x % + '');
} int n, m, k;
ll a[maxn];
int s1, s2, du[maxn];
ll ans = ; struct Edge
{
int nxt, to;
}e[maxn << ];
int head[maxn], ecnt = -;
void addEdge(int x, int y)
{
e[++ecnt] = (Edge){head[x], y};
head[x] = ecnt;
} void dfs(int now, int f, ll Min, ll Max)
{
if(du[now] == )
{
int l = lower_bound(a + , a + m + , Min) - a;
int r = upper_bound(a + , a + m + , Max) - a - ;
ans += (r - l + ) * k;
return;
}
int x = du[now] - ;
for(int i = head[now]; i != -; i = e[i].nxt)
{
if(e[i].to == f) continue;
dfs(e[i].to, now, Min * x, Max * x + x - );
}
} int main()
{
Mem(head, -);
n = read(); m = read(); k = read();
for(int i = ; i <= m; ++i) a[i] = read();
sort(a + , a + m + );
for(int i = ; i < n; ++i)
{
int x = read(), y = read();
du[x]++; du[y]++;
if(i == ) s1 = x, s2 = y;
addEdge(x ,y); addEdge(y, x);
}
dfs(s1, s2, k, k); dfs(s2, s1, k, k);
write(ans), enter;
return ;
}

[POI2014]MRO-Ant colony的更多相关文章

  1. [BZOJ3872][Poi2014]Ant colony

    [BZOJ3872][Poi2014]Ant colony 试题描述 There is an entrance to the ant hill in every chamber with only o ...

  2. bzoj 3872: [Poi2014]Ant colony -- 树形dp+二分

    3872: [Poi2014]Ant colony Time Limit: 30 Sec  Memory Limit: 128 MB Description   There is an entranc ...

  3. 【BZOJ3872】[Poi2014]Ant colony 树形DP+二分

    [BZOJ3872][Poi2014]Ant colony Description 给定一棵有n个节点的树.在每个叶子节点,有g群蚂蚁要从外面进来,其中第i群有m[i]只蚂蚁.这些蚂蚁会相继进入树中, ...

  4. Codeforces 474 F. Ant colony

    线段树求某一段的GCD..... F. Ant colony time limit per test 1 second memory limit per test 256 megabytes inpu ...

  5. Codeforces Round #271 (Div. 2) F. Ant colony 线段树

    F. Ant colony time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  6. 【BZOJ3872】Ant colony(二分,动态规划)

    [BZOJ3872]Ant colony(二分,动态规划) 题面 又是权限题... Description There is an entrance to the ant hill in every ...

  7. CodeForces 474F Ant colony ST+二分

    Ant colony 题解: 因为一个数是合法数,那么询问区间内的其他数都要是这个数的倍数,也就是这个区间内的gcd刚好是这个数. 对于这个区间的gcd来说,不能通过前后缀来算. 所以通过ST表来询问 ...

  8. Codeforces G. Ant colony

    题目描述: F. Ant colonytime limit per test1 secondmemory limit per test256 megabytesinputstandard inputo ...

  9. [POI2014]Ant colony

    题目大意: 给定一棵$n(n\le10^6)$个结点的树.在每个叶子结点,有$g$群蚂蚁要从外面进来,其中第$i$群有$m_i$只蚂蚁.这些蚂蚁依次爬树(一群蚂蚁爬完后才会爬另一群),若当前经过结点度 ...

  10. bzoj 3872 [Poi2014]Ant colony——二分答案

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3872 可以倒推出每个叶子节点可以接受的值域.然后每个叶子二分有多少个区间符合即可. 注意一开 ...

随机推荐

  1. CSAPP阅读笔记-32位64位的区别--来自第三章引言的笔记--P110

    仅从寻址上看,32位和64位机器能寻址的内存空间大小不同. 需要知道的是,计算机系统对存储器作了抽象,程序“认为”内存是一个很大的字节数组,然而实际上它是由多个硬件存储器和操作系统组合起来实现的. 程 ...

  2. URL篇之相对URL

    URL有两种方式:绝对的和相对的. 绝对URL中包含有访问资源所需的全部信息,是访问网络资源必须的. 相对URL是不完整的,要从相对URL中获取访问资源所需的全部信息,就必须相对于另一个被称为其基础( ...

  3. 踩坑--springboot打war包

    springboot需要jdk1.8+tomcat8.0 第一步:从IDEA中通过maven install将项目打成war包 第二步:将war包放在tomcat的webApp目录下,启动tomcat ...

  4. DNS服务器设置

    1,域名解析:ip能够访问,但是域名不能访问. 2,配置好网络之后,切换到命令行模式,配置好的网络便不能用了. 具体方法: 打开vim /etc/sysconfig/network-scripts/i ...

  5. bootstrap日历插件地址

    http://www.bootcss.com/p/bootstrap-datetimepicker/

  6. 九度oj题目1385:重建二叉树

    题目1385:重建二叉树 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4419 解决:1311 题目描述: 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和 ...

  7. mongo 多条件or

    or语句 or b=2 > db.XXX.find({"$or":[{"a":1}, {"b":2}]});  等于java mong ...

  8. LintCode刷题小记491

    题目: 判断一个正整数是不是回文数. 回文数的定义是,将这个数反转之后,得到的数仍然是同一个数. 样例: 11, 121, 1, 12321 这些是回文数. 23, 32, 1232 这些不是回文数. ...

  9. window 常用MySQL数据库命令总结

    登录:cmd - mysql -uroot -p 创建数据库:CREATE DATABASE `tpcms` DEFAULT CHARACTER SET utf8 COLLATE utf8_gener ...

  10. NIOSocket Server Client

    最近在看Netty框架,顺便写了一下NIO SocketChannel服务端和客户端 Server.java import java.io.IOException; import java.net.I ...