Codeforces 894.D Ralph And His Tour in Binary Country
2.5 seconds
512 megabytes
standard input
standard output
Ralph is in the Binary Country. The Binary Country consists of n cities and (n - 1) bidirectional roads connecting the cities. The roads are numbered from 1 to (n - 1), the i-th road connects the city labeled (here ⌊ x⌋ denotes the x rounded down to the nearest integer) and the city labeled (i + 1), and the length of the i-th road is Li.
Now Ralph gives you m queries. In each query he tells you some city Ai and an integer Hi. He wants to make some tours starting from this city. He can choose any city in the Binary Country (including Ai) as the terminal city for a tour. He gains happiness (Hi - L) during a tour, where L is the distance between the city Ai and the terminal city.
Ralph is interested in tours from Ai in which he can gain positive happiness. For each query, compute the sum of happiness gains for all such tours.
Ralph will never take the same tour twice or more (in one query), he will never pass the same city twice or more in one tour.
The first line contains two integers n and m (1 ≤ n ≤ 106, 1 ≤ m ≤ 105).
(n - 1) lines follow, each line contains one integer Li (1 ≤ Li ≤ 105), which denotes the length of the i-th road.
m lines follow, each line contains two integers Ai and Hi (1 ≤ Ai ≤ n, 0 ≤ Hi ≤ 107).
Print m lines, on the i-th line print one integer — the answer for the i-th query.
2 2
5
1 8
2 4
11
4
6 4
2
1
1
3
2
2 4
1 3
3 2
1 7
11
6
3
28
Here is the explanation for the second sample.
Ralph's first query is to start tours from city 2 and Hi equals to 4. Here are the options:
- He can choose city 5 as his terminal city. Since the distance between city 5 and city 2 is 3, he can gain happiness 4 - 3 = 1.
- He can choose city 4 as his terminal city and gain happiness 3.
- He can choose city 1 as his terminal city and gain happiness 2.
- He can choose city 3 as his terminal city and gain happiness 1.
- Note that Ralph can choose city 2 as his terminal city and gain happiness 4.
- Ralph won't choose city 6 as his terminal city because the distance between city 6 and city 2 is 5, which leads to negative happiness for Ralph.
So the answer for the first query is 1 + 3 + 2 + 1 + 4 = 11.
大致题意:m次询问,每次给定一个a和h,第i个点的贡献是h减i到a的距离,求大于0的贡献.
分析:非常棒的一道题!改变了我对cf测评机的认识.我一开始错误地认为只需要求出一个点祖先的贡献和它的子树的节点的贡献就好了,没有考虑到还有一种情况:它的父亲的其它儿子的子树可能还有贡献.这样的话就必须求出每个点为根的子树中每个点到根节点的距离.事实上只需要存下距离就够了,因为我们不关心到底是哪一个点.我原本以为内存会爆掉,因为n有10^6,要把每个点都给保存下来,还有保存子树的所有点的距离,数组绝对是开不下的,但是如果不求出这个是无法继续做下去的,于是用vector,竟然没有爆内存,神奇.
因为题目给定的是一个二叉树,每个节点的编号都是有规律的,所以可以从第n号点从下往上更新,利用左右儿子的信息去更新根节点的信息.为了在查询的时候方便一些,可以维护一下前缀和,即第i个点的子树中前j个距离的和,这样可以利用vector的upper_bound来快速查找关键点p,利用公式进行O(1)计算.
接下来的事情就很好办了,每次从查询的点开始,先看看左右子树之前有没有被查询,如果没有就查询一下,完了之后就跳到祖先,并记录上一次查询的是哪一个点,下次就不查询它了.
参考了网上神犇的vector的写法,用得很6,个人认为它最大的用处是关于内存方面的.这道题也纠正了我的一个常识性错误:if (k) ......我一直以为如果k > 0这个判断语句才会成立,万万没想到是k != 0这个语句就成立了,看来还是基本功不扎实,要多多练习.
#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; typedef long long ll; const int maxn = ;
vector <ll>e[maxn], sum[maxn];
ll n, m, len[maxn], a, h;
ll ans; ll query(ll x, ll h)
{
if (h <= )
return ;
ll p = upper_bound(e[x].begin(), e[x].end(), h) - e[x].begin();
return p * h - sum[x][p - ];
} void init()
{
for (int i = n; i >= ; i--)
{
e[i].push_back();
int lc = i * , rc = i * + ;
if (lc <= n)
{
for (int j = ; j < e[lc].size(); j++)
e[i].push_back(e[lc][j] + len[lc]);
}
if (rc <= n)
{
for (int j = ; j < e[rc].size(); j++)
e[i].push_back(e[rc][j] + len[rc]);
}
sort(e[i].begin(), e[i].end());
sum[i].resize(e[i].size());
for (int j = ; j < sum[i].size(); j++)
sum[i][j] = sum[i][j - ] + e[i][j];
}
} int main()
{
cin >> n >> m;
for (int i = ; i <= n; i++)
scanf("%I64d", &len[i]);
init();
while (m--)
{
ans = ;
scanf("%I64d%I64d", &a, &h);
ll last = ;
while (a && h >= )
{
ans += h;
ll lc = a * , rc = a * + ;
if (last != lc && lc <= n)
ans += query(lc, h - len[lc]);
if (last != rc && rc <= n)
ans += query(rc, h - len[rc]);
last = a;
h -= len[a];
a /= ;
}
printf("%I64d\n", ans);
} return ;
}
Codeforces 894.D Ralph And His Tour in Binary Country的更多相关文章
- 【Codeforces】894D. Ralph And His Tour in Binary Country 思维+二分
题意 给定一棵$n$个节点完全二叉树,$m$次询问,每次询问从$a$节点到其它所有节点(包括自身)的距离$L$与给定$H_a$之差$H_a-L$大于$0$的值之和 对整棵树从叶子节点到父节点从上往下预 ...
- [codeforces 894 E] Ralph and Mushrooms 解题报告 (SCC+拓扑排序+DP)
题目链接:http://codeforces.com/problemset/problem/894/E 题目大意: $n$个点$m$条边的有向图,每条边有一个权值,可以重复走. 第$i$次走过某条边权 ...
- Codeforces 894.E Ralph and Mushrooms
E. Ralph and Mushrooms time limit per test 2.5 seconds memory limit per test 512 megabytes input sta ...
- Codeforces 894.B Ralph And His Magic Field
B. Ralph And His Magic Field time limit per test 1 second memory limit per test 256 megabytes input ...
- Codeforces 894.C Marco and GCD Sequence
C. Marco and GCD Sequence time limit per test 1 second memory limit per test 256 megabytes input sta ...
- 【Codeforces】894E.Ralph and Mushrooms Tarjan缩点+DP
题意 给定$n$个点$m$条边有向图及边权$w$,第$i$次经过一条边边权为$w-1-2.-..-i$,$w\ge 0$给定起点$s$问从起点出发最多能够得到权和,某条边可重复经过 有向图能够重复经过 ...
- Codeforces 894.A QAQ
A. QAQ time limit per test 1 second memory limit per test 256 megabytes input standard input output ...
- Codeforces Round #307 (Div. 2) D. GukiZ and Binary Operations (矩阵高速幂)
题目地址:http://codeforces.com/contest/551/problem/D 分析下公式能够知道,相当于每一位上放0或者1使得最后成为0或者1.假设最后是0的话,那么全部相邻位一定 ...
- Educational Codeforces Round 75 (Rated for Div. 2) B. Binary Palindromes
链接: https://codeforces.com/contest/1251/problem/B 题意: A palindrome is a string t which reads the sam ...
随机推荐
- Struts2(一.基本介绍,环境搭建及需求分析)
Struts2框架开发 前言 开发工具:eclipse struts1:老项目使用较多,维护时需要用到 struts2:新项目使用较多 一.特点 1. 无侵入式设计 struts2 与 struts ...
- HIVE中的数据怎么导出到hdfs或本地呢
思路一:重定向 在我不知道工具 ,也不熟悉HIQL语法的情况下,没办法了,只有选择一个最简单粗暴的方法,重定向. 在shell中使用 hive -e 可以在shell中执行hive命令,hive -f ...
- docker应用容器化准则—12 factor
在云的时代,越来越多的传统应用需要迁移到云环境下,新应用也要求能适应云的架构设计和开发模式.而12-factor提供了一套标准的云原生应用开发的最佳原则. 在容器云项目中应用容器化主要参考12-Fac ...
- Pyhone学习之环境搭建
一.python 环境搭建 本章节我们将向大家介绍如何在本地搭建Python开发环境.Python可应用于多平台包括 Linux 和 Mac OS X.你可以通过终端窗口输入 "python ...
- 【win10系统问题】远程桌面登录一次后,第二次登录看不到用户名和密码输入框
[win10系统远程桌面登录问题] 远程桌面登录某服务器一次后,第二次登录看不到用户名和密码输入框 [解决方法] 在注册表里找到该路径下的远程服务器ip,删除即可: HKEY_CURRENT_USER ...
- Appstate的几种状态及在android 和ios触发
AppState能告诉你当前应用是在前台还是在后台,或者处于切换应用的状态,并且能在状态变化的时候通知你. AppState 通常在处理推送通知的时候用来决定内容和对应的行为 一: App State ...
- 使用Scrapy构建一个网络爬虫
记得n年前项目需要一个灵活的爬虫工具,就组织了一个小团队用Java实现了一个爬虫框架,可以根据目标网站的结构.地址和需要的内容,做简单的配置开发,即可实现特定网站的爬虫功能.因为要考虑到各种特殊情形, ...
- [笔记] mysql5.6一些编译参数
cmake \ -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ -DSYSCONFDIR=/etc \ -DWITH_INNOBASE_STORAGE_ENGINE ...
- Codeforces Round #765 Div.1 F. Souvenirs 线段树
题目链接:http://codeforces.com/contest/765/problem/F 题意概述: 给出一个序列,若干组询问,问给出下标区间中两数作差的最小绝对值. 分析: 这个题揭示着数据 ...
- 移动平台的meta标签
这个meta在移动平台上有非常神奇的地方. 1. <meta name="viewport" content="width=device-width; initia ...