当前位置: 首页 > 编程日记 > 正文

Codeforces 894.D Ralph And His Tour in Binary Country

D. Ralph And His Tour in Binary Country
time limit per test
2.5 seconds
memory limit per test
512 megabytes
input
standard input
output
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.

Input

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 ≤ n0 ≤ Hi ≤ 107).

Output

Print m lines, on the i-th line print one integer — the answer for the i-th query.

Examples
input
2 2
5
1 8
2 4
output
11
4
input
6 4
2
1
1
3
2
2 4
1 3
3 2
1 7
output
11
6
3
28
Note

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 = 1000010;
vector <ll>e[maxn], sum[maxn];
ll n, m, len[maxn], a, h;
ll ans;ll query(ll x, ll h)
{if (h <= 0)return 0;ll p = upper_bound(e[x].begin(), e[x].end(), h) - e[x].begin();return p * h - sum[x][p - 1];
}void init()
{for (int i = n; i >= 1; i--){e[i].push_back(0);int lc = i * 2, rc = i * 2 + 1;if (lc <= n){for (int j = 0; j < e[lc].size(); j++)e[i].push_back(e[lc][j] + len[lc]);}if (rc <= n){for (int j = 0; 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 = 1; j < sum[i].size(); j++)sum[i][j] = sum[i][j - 1] + e[i][j];}
}int main()
{cin >> n >> m;for (int i = 2; i <= n; i++)scanf("%I64d", &len[i]);init();while (m--){ans = 0;scanf("%I64d%I64d", &a, &h);ll last = 0;while (a && h >= 1){ans += h;ll lc = a * 2, rc = a * 2 + 1;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 /= 2;}printf("%I64d\n", ans);}return 0;
}

          

转载于:https://www.cnblogs.com/zbtrs/p/8035485.html

相关文章:

linux下获取系统时间 和 时间偏移

获取linux时间 并计算时间偏移 void getSystemTimer(void){#if 0 char *wdate[]{"Sun","Mon","Tue","Wed","Thu","Fri","Sat"} ; time_t timep; struct tm *p; time(&timep); pgmtime(&timep)…

使用git命令上传本地文件到GitHub上

1、官网下载git并且anz安装 2、在Github上申请账号 3、在本地使用git命令生成私钥和公钥 连续按三次 回车键 $ ssh-keygen -t rsa -C "账号"生成如图 将生成的公钥设置到github上 &#xff0c; 生成公钥的地址是 ./ssh &#xff08;C盘的用户目录&#xff09; 4…

POI解析Excel文件工具类

/*** 读取excel数据*/public static List<Map<String, Object>> exportListFromExcel(File file, int sheetNum) throws IOException { return exportListFromExcel(new FileInputStream(file), FilenameUtils.getExtension(file.getName()), sheetNum); } publ…

关于Bulk加载模式

Bulk加载模式是Informatica提供的一种高性能数据加载模式&#xff0c;它利用数据库底层机制&#xff0c;依靠调用数据库本身提供的Utility来进行数据的加载  该方式将绕过数据库的log记录&#xff0c;以此提高数据库加载性能&#xff0c;因此Bulk模式不能进行数据的Rollback操…

C#:CsvReader读取.CSV文件(转换成DataTable)

原文引用&#xff1a;https://www.codeproject.com/Articles/9258/A-Fast-CSV-Reader using LumenWorks.Framework.IO.Csv;using System;using System.Collections.Generic;using System.Data;using System.IO;using System.Linq;using System.Text;using System.Threading.Tas…

Standup Timer的MVC模式及项目结构分析

前言 学习android一段时间了&#xff0c;为了进一步了解android的应用是如何设计开发的&#xff0c;决定详细研究几个开源的android应用。从一些开源应用中吸收点东西&#xff0c;一边进行量的积累&#xff0c;一边探索android的学习研究方向。这里我首先选择了jwood的Standup …

ISDN的配置

ISDN的配置 1、 实验目的&#xff1a; 通过配置ISDN使两个路由器之间能够通信 2、 实验拓扑图&#xff1a; 3、 实验步骤&#xff1a; &#xff08;1&#xff09; R1的配置 Router> R1的ISDN配置 Router>en R…

5,ORM组件XCode(动手)

本篇才真正是XCode教程第一篇。《速览》是为了以最简洁的语言最短小的篇幅去吸引开发者&#xff1b;《简介》则是对XCode组件和XCode开发模式的一个整体介绍&#xff0c;让开发者从宏观的角度去理解XCode&#xff1b;《共舞》把XCode提到了一个新的高度&#xff0c;让开发者感受…

前端知识分享.md

目录 一些支持固定列和表头的组件库element UIiViewAmaze UIlayui一些比较好的IDEMVVM 框架VueNode生态其它SassTypeScript一些支持固定列和表头的组件库 element UI https://element.eleme.cn/#/zh-CN/component/table iView https://www.iviewui.com/components/table Amaze …

C 输入 输出

C 输入 & 输出 当我们提到输入时&#xff0c;这意味着要向程序填充一些数据。输入可以是以文件的形式或从命令行中进行。C 语言提供了一系列内置的函数来读取给定的输入&#xff0c;并根据需要填充到程序中。 当我们提到输出时&#xff0c;这意味着要在屏幕上、打印机上或任…

搭建服务器环境 安装jdk、mysql、Tomcat 以及配置https 记录

1.在cenos上安装 jdk #在usr/local下创建 java 文件夹 mkdir java#将jdk拷贝到该文件夹中 [rootVM_0_15_centos jdk1.8.0_181]# cp jdk-8u181-linux-x64.tar.gz /usr/local/java/#解压该文件 [rootVM_0_15_centos jdk1.8.0_181]# tar -zxvf jdk-8u181-linux-x64.tar.gz #配置环…

一个Java程序员应该掌握的10项技能

1、语法&#xff1a;必须比较熟悉&#xff0c;在写代码的时候IDE的编辑器对某一行报错应该能够根据报错信息知道是什么样的语法错误并且知道任何修正。 2、命令&#xff1a;必须熟悉JDK带的一些常用命令及其常用选项&#xff0c;命令至少需要熟悉&#xff1a;appletviewer、 Ht…

逆向-攻防世界-maze

题目提示是走迷宫。 IDA载入程序分析。 输入字符长度必须是24&#xff0c;开头必须是nctf{&#xff0c;结尾必须是}。在125处按R就可以变成字符。 sub_400650和sub_400660是关键函数&#xff0c;分析sub_400650。 v10的下一字节减1. sub_400660v10的下一字节加1. 分析这两个函数…

linxu passwd 给linux用户设置密码 命令

[rootlocalhost ~]# passwd # 修改 root 用户的密码passwd 给linux用户设置密码 命令 passwd www 直接passwd是当前用户设置密码 非交互式修改密码&#xff1a; echo "123456" |passwd --stdin www [rootMongoDB ~]# echo "123456" | passwd…

MultipartFile 使用 记录

MultipartFile file 获取文件流 file.getInputStream()&#xff1b; 获取文件名称 String fileName file.getOriginalFilename(); 待补充

多语言加载图片问题

系统要求多语言&#xff1a;中文和英文&#xff0c;分别在两个xml文件中书写相关内容&#xff0c;类似&#xff1a; <root><resource name"VersionName">Version</resource><resource name"Logout">Logout</resource></r…

5.html基础标签:块级+行级元素+特殊字符+嵌套规则

<!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>Document</title> </head> <body><!-- 块元素&#xff1a;独立成一行(相当于标签前后都设置了br),可以设置宽高,默认宽高100%文字…

[摘]终于找到一个有助理解left/right/full outer join的例子

近日在学习《Understading DB2》的时候找到了一个例子&#xff0c;对于理解 left/right/full 三种 outer join 的大有裨益。 先看样本数据&#xff0c;来自DB2的示例数据库 sample&#xff1a; db2 > insert into employee values(99999,killkill,N,Huang,null,null,null,no…

vue——props的两种常用方法

vue——props的两种常用方法 1、实现父——>子的通信 举例如下&#xff1a; 父组件 parent.vue<children :channel"object1"></children> 子组件 children.vue export default{name:"children",props:["channel"],data(){return{…

idea导入模板

1.我的配置文件下载 请点下面的连接 https://download.csdn.net/download/huyande123/10728802 先导入第二个文件&#xff0c;在导入第一个文件 在此基础上添加了类注释和方法注释&#xff08;方法注释快捷键 qtab&#xff09; 参考链接 https://blog.csdn.net/qq_34581118…

为office添加繁简体转换

为office添加繁简体转换本人所在的公司是一间港资公司&#xff0c;很多香港同事习惯繁体&#xff0c;而我们内地的同事则习惯简体&#xff0c;于是免不了要进行繁简体转换&#xff0c;这时候就要装一个office的插件来达到这样的功能&#xff0c;如下&#xff1a;1、没装插件的时…

关闭Windows 7中的 Program Compatibility Assistant

感觉微软总喜欢把简单问题复杂化。安装几个小软件也老是弹出这样的对话框&#xff1a; 然后点击“What settings are applied?”&#xff0c;看到帮助中一段&#xff1a; 提示我在组策略里能够关闭这个烦人的程序兼容性助手&#xff0c;却没有明说&#xff0c;故意卖关子呢。那…

Swift学习:自动引用计数

swift 使用自动引用计数&#xff08;ARC&#xff09;机制来跟踪和管理你的应用程序的内存。通常情况下&#xff0c;swift 内存管理机制会一直起作用&#xff0c;你无须自己来考虑内存的管理。ARC 会在类的实例不再被使用时&#xff0c;自动释放其占用的内存。 然而在少数情况下…

简单又实用的分享!SharePoint母版页引用(实战)

分享人&#xff1a;广州华软 极简 一. 前言 此SharePoint 版本为2013&#xff0c;请注意版本号。此文以图文形式&#xff0c;描述了根网站及子网站引用母版页&#xff0c;需要注意的点已用图文形式以标明。 本文适用于初学者。 二. 目录 1. 前言 2. 目录 3. 如何引用母版页 4. …

mysql 修改某字段的格式为 utf8mb4

ALTER TABLE confession MODIFY content TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;#查看表中字段字符集 SHOW FULL COLUMNS FROM confession;

TileList自动滚动指定单元格,可视部分

TileList自动滚动指定单元格&#xff0c;可视部分 TileList1.scrollToIndex(50)

深入.NET DataTable

1、ADO.NET相关对象一句话介绍1)DataAdapter&#xff1a;DataAdapter实际是一个SQL语句集合&#xff0c;因为对Database的操作最终需要归结到SQL语句。2)Dataset&#xff1a;DataSet可以理解成若干DataTable的集合&#xff0c;DataSet在内存里面维护一个表集合包括表间关系。对…

天平称球问题-转

称球问题一般会有以下3种变形&#xff1a; 1、N个球&#xff0c;其中有一个坏的&#xff0c;知道是轻还是重&#xff0c;用天平称出坏球来。 2、N个球&#xff0c;其中有一个坏的&#xff0c;不知是轻还是重&#xff0c;用天平称出坏球来。 3、N个球&#xff0c;其中有一个坏…

Java虚拟机垃圾收集算法

1、标记-清除算法 标记-清除算法分为 “标记” 和 “清除” 两个步骤&#xff1a;首先标记出所有需要回收的对象&#xff0c;然后在标记完成后统一回收所有被标记的对象&#xff0c;是垃圾收集算法中的最基础的收集算法。 缺点&#xff1a;一、标记和清除两个步骤效率都不高&am…

WMIC的用法

获得系统版本信息wmic datafile where Namec:\\windows\\explorer.exe get Manufacturer,Version,Filename获得信筒进程 wmic process list full 注意&#xff1a;这里的full也可以换成brief&#xff08;简洁&#xff09;获得硬件信息&#xff08;这里以cpu为例&#xf…