首页 > 其他分享 >Codeforces 894D Ralph And His Tour in Binary Country

Codeforces 894D Ralph And His Tour in Binary Country

时间:2023-05-01 20:00:52浏览次数:42  
标签:Binary His dh int Country tot fa 子树 ds

预处理出对于 \(u\) 节点其子树内节点(包括 \(u\))与 \(u\) 的距离,从小到大排序得到 \(ds_u\)
同时对 \(ds_u\) 进行前缀和处理 \(dh_{u, i} = \sum\limits_{j = 1}^{i} ds_{u, j}\)
这样设 \(tot\) 为 \(ds_u\) 二分得到的 \(ds_{u, i}\le h\) 的右端点,也即为 \(u\) 子树内对答案有贡献的点的数量。
拆一下贡献的式子:\(\sum\limits_{i = 1}^{tot} (h - ds_{u, i}) = tot\times h - \sum\limits_{i = 1}^{tot} (h - ds_{u, i}) = tot\times h - dh_{u, tot}\)
然后就可以 \(O(1)\) 求子树贡献了

对于询问:
首先可以得到 \(u\) 子树内对答案的贡献
发现还有子树外的贡献,便考虑类似容斥的思想:
\(u\) 一直往上跳(即 \(u\leftarrow fa_u\))的同时计算 \(fa_u\) 这个子树的贡献,发现包含了 \(u\),则减去 \(u\) 子树的贡献

注意因为每次往上跳都会跳 \(l_u\) 的长度,所以 \(fa_u\) 子树对应的 \(h_{fa}\) 要在往上跳时更新:\(h_{fa}\leftarrow h_{fa} - l_u\)
同时考虑 \(u\) 子树被 \(fa_u\) 子树包含,所以其对于 \(fa_u\) 子树还有贡献应该再多走 \(l_u\),所以 \(u\) 子树对应 \(h_u\) 应为 \(h_{fa} - l_u\)

感觉写的好抽象(,看代码应该好些

#include<bits/stdc++.h>
using namespace std;
#define int64 long long
const int N = 1e6 + 10;
int64 l[N];
vector<int64> ds[N * 2], dh[N];
int lbcnt(int id, int64 sum) {
	return lower_bound(ds[id].begin() + 1, ds[id].end(), sum) - ds[id].begin() - 1;
}
int main() {
	int n, m;
	scanf("%d%d", &n, &m);
	for (int i = 2; i <= n; i++) {
		scanf("%lld", &l[i]);
	}
	for (int i = n; i; i--) {
		for (int64 x : ds[i << 1]) {
			ds[i].push_back(x + l[i << 1]);
		}
		for (int64 x : ds[i << 1 | 1]) {
			ds[i].push_back(x + l[i << 1 | 1]);
		}
		inplace_merge(ds[i].begin(), ds[i].begin() + ds[i << 1].size(), ds[i].end());
		ds[i].insert(ds[i].begin(), 0);
	}
	for (int i = 1; i <= n; i++) {
		ds[i].insert(ds[i].begin(), 0);
		dh[i].push_back(0);
		for (int j = 1; j < (int)(ds[i].size()); j++) {
			dh[i].push_back(ds[i][j] + dh[i].back());
		}
	}
	for (int i = 1; i <= m; i++) {
		int u;
		int64 h;
		scanf("%d%lld", &u, &h);
		int tot = lbcnt(u, h);
		int64 ans = tot * h - dh[u][tot];
		int64 w = l[u];
		for (int v = u >> 1; v; u >>= 1, v >>= 1, w += l[u]) {
			int totu = lbcnt(u, h - w - l[u]), totv = lbcnt(v, h - w);
			ans += totv * (h - w) - dh[v][totv]; ans -= totu * (h - w - l[u]) - dh[u][totu];
		}
		printf("%lld\n", ans);
	}
	return 0;
}

标签:Binary,His,dh,int,Country,tot,fa,子树,ds
From: https://www.cnblogs.com/lhzawa/p/17366906.html

相关文章

  • 06 - react的类组件中的状态state render函数 this指向问题 事件绑定
    //注册事件importReactDomfrom"react-dom"import{Component}from"react"//类组件中的状态通过this.state.xxx来获取状态classHelloextendsComponent{//事件对象eventhandleClick(e){console.log(this)//udnefiend使用箭头函数解决this......
  • C++-std::this_thread::get_id()-获取线程id
    C++-std::this_thread::get_id()-获取线程idstd::this_thread::get_id()头文件:<thread>函数:std::this_thread::get_id()用例:std::thread::idthread_id=std::this_thread::get_id();std::thread对象的成员函数get_id()头文件:<thread>函数:std::thread::idget_id()用例:......
  • refusing to merge unrelated histories问题的解决
    问题描述将代码从云端拉取到本地,出现了这个错误,说我这里无法合并并不相关的历史,然后就屁颠屁颠地去找报错原因去了问题解决输入gitpulloriginmain--allow-unrelated-histories即,合并并不相关的分支,即便并不相关!然后,错误成功解决啦!......
  • To build this project, the following workloads must be installed: macos问题的处
    如遇跨平台的NETSDK1147错误的处理方法:【报错提示】NETSDK1147Tobuildthisproject,thefollowingworkloadsmustbeinstalled:macosToinstalltheseworkloads,runthefollowingcommand:dotnetworkloadrestore   KristofferStrube.Blazor.SVGEditor   You......
  • 二叉树Binary Tree
    二叉树BinaryTree1.树的一些常用术语2.二叉树的概念树有很多种,每个节点最多只能有两个子节点的一种形式称为二叉树;二叉树的子节点分为左子节点和右子节点;以下三种均为二叉树:若该二叉树的所有叶子节点都在最后一层,且节点总数n==\(2^k\)-1,k为层数,则称为满二叉树......
  • TypeError: this.libOptions.parse is not a function
    安装完node.js运行项目后,报错:TypeError:this.libOptions.parseisnotafunctionatESLint8Plugin.<anonymous>(C:\ProgramFiles\JetBrains\GoLand2022.1.4\plugins\JavaScriptLanguage\languageService\eslint\bin\eslint8-plugin.js:139:64)atstep......
  • whistle使用
    whistle抓包笔记安装启动Mac或Windows系统可以采用一键安装:https://juejin.cn/post/7096345607740063775whistle安装过程需要以下步骤(缺一不可):安装Node安装whistle启动whistle配置代理安装根证书1.安装Nodewhistle支持v0.10.0以上版本的Node,为获取更好的性能,推......
  • this指针
    1.this指针的概念与特性this指针概念首先来看一个例子#include<iostream>usingnamespacestd;classDate{public: voidInit(intyear,intmonth,intday) { _year=year; _month=month; _day=day; } voidPrint() { cout<<_year<<"-&q......
  • syspolicy_purge_history sql job failed
    错误信息如下:'FileC:\ProgramFiles(x86)\MicrosoftSQLServer\130\Tools\PowerShell\Modules\SQLPS\Sqlps.ps1cannotbeloadedbecauserunningscriptsisdisabledonthissystem根据错误信息提示检查发现服务器注册表里缺少内容-Microsoft.SqlServer.Management.Power......
  • SQL Injector - GET Manual Setup Binary Payload Attack
    bt5上操作:***********************************************************************Fast-Track-Anewbeginning...****Version:4.0.2......