BZOJ 3653权限题。

这题方法很多,但我会的不多……

给定了$a$,我们考虑讨论$b$的位置:

1、$b$在$a$到根的链上,那么这样子$a$的子树中的每一个结点(除了$a$之外)都是可以成为$c$的,答案就是$min(dep_a - 1, k) * (siz_a - 1)$。

2、$b$在$a$的子树中,这样子$c$的位置受$b$限制,这样子的答案就是$\sum_{x}(siz_x - 1) \ (x \in subtree(a), dep_x - dep_a \leq k)$。

第一个直接算就好,第二个可以用主席树维护出来,具体方法就和CF893F Subtree Minimum Query一样,按照深度建一棵线段树,然后查询的时候只要看一下对应深度的子树中和是多少就可以了。

所以在线的时候就可以回答了。

时间复杂度$O((n + q) logn)$。

Code:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll; const int N = 3e5 + ; int n, qn, tot = , head[N], bel[N];
int dfsc = , dep[N], siz[N], id[N]; struct Edge {
int to, nxt;
} e[N << ]; inline void add(int from, int to) {
e[++tot].to = to;
e[tot].nxt = head[from];
head[from] = tot;
} struct SortNode {
int val, pos; friend bool operator < (const SortNode &x, const SortNode &y) {
return x.val < y.val;
} } so[N]; inline void read(int &X) {
X = ; char ch = ; int op = ;
for(; ch > '' || ch < ''; ch = getchar())
if(ch == '-') op = -;
for(; ch >= '' && ch <= ''; ch = getchar())
X = (X << ) + (X << ) + ch - ;
X *= op;
} inline int min(int x, int y) {
return x > y ? y : x;
} inline int max(int x, int y) {
return x > y ? x : y;
} inline void chkMax(int &x, int y) {
if(y > x) x = y;
} void dfs(int x, int fat, int depth) {
dep[x] = depth, siz[x] = , id[x] = ++dfsc;
for(int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
if(y == fat) continue;
dfs(y, x, depth + );
siz[x] += siz[y];
}
} namespace SegT {
struct Node {
int lc, rc;
ll sum;
} s[N * ]; int root[N], nodeCnt = ; #define lc(p) s[p].lc
#define rc(p) s[p].rc
#define sum(p) s[p].sum
#define mid ((l + r) >> 1) void ins(int &p, int l, int r, int x, ll v, int pre) {
s[p = ++nodeCnt] = s[pre];
sum(p) += v;
if(l == r) return; if(x <= mid) ins(lc(p), l, mid, x, v, lc(pre));
else ins(rc(p), mid + , r, x, v, rc(pre));
} ll query(int p, int l, int r, int x, int y) {
if(x > y) return 0LL;
if(x <= l && y >= r) return sum(p); ll res = 0LL;
if(x <= mid) res += query(lc(p), l, mid, x, y);
if(y > mid) res += query(rc(p), mid + , r, x, y); return res;
} } using namespace SegT; int main() {
// freopen("2.in", "r", stdin);
// freopen("my.out", "w", stdout); read(n), read(qn);
for(int x, y, i = ; i < n; i++) {
read(x), read(y);
add(x, y), add(y, x);
}
dfs(, , ); for(int i = ; i <= n; i++)
so[i].val = dep[i], so[i].pos = i;
sort(so + , so + + n); int maxd = ;
for(int i = ; i <= n; i++) {
ins(root[i], , n, id[so[i].pos], 1LL * (siz[so[i].pos] - ), root[i - ]);
chkMax(maxd, so[i].val);
bel[so[i].val] = i;
} for(int x, k; qn--; ) {
read(x), read(k);
ll res = 1LL * min(dep[x] - , k) * (siz[x] - );
res += query(root[bel[min(maxd, dep[x] + k)]], , n, id[x] + , id[x] + siz[x] - );
printf("%lld\n", res);
} return ;
}

Luogu 3899 [湖南集训]谈笑风生的更多相关文章

  1. 主席树 || 可持久化线段树 || BZOJ 3653: 谈笑风生 || Luogu P3899 [湖南集训]谈笑风生

    题面:P3899 [湖南集训]谈笑风生 题解: 我很喜欢这道题. 因为A是给定的,所以实质是求二元组的个数.我们以A(即给定的P)作为基点寻找答案,那么情况分两类.一种是B为A的父亲,另一种是A为B的 ...

  2. luogu P3899 [湖南集训]谈笑风生

    传送门 nmyzd,mgdhls,bnmbzdgdnlql,a,wgttxfs 对于一个点\(a\),点\(b\)只有可能是他的祖先或者在\(a\)子树里 如果点\(b\)是\(a\)祖先,那么答案为 ...

  3. luogu P3899 [湖南集训]谈笑风生 线段树合并

    Code: #include<bits/stdc++.h> #define maxn 300002 #define ll long long using namespace std; vo ...

  4. [Luogu P3899] [湖南集训]谈笑风生 (主席树)

    题面 传送门:https://www.luogu.org/problemnew/show/P3899 Solution 你们搞的这道题啊,excited! 这题真的很有意思. 首先,我们可以先理解一下 ...

  5. bzoj 3653 [湖南集训]谈笑风生

    题目描述 设 T 为一棵有根树,我们做如下的定义: • 设 a 和 b 为 T 中的两个不同节点.如果 a 是 b 的祖先,那么称"a 比 b 不知道高明到哪里去了". • 设 a ...

  6. P3899 [湖南集训]谈笑风生

    题目链接 https://www.lydsy.com/JudgeOnline/problem.php?id=3653 https://www.luogu.org/problemnew/show/P38 ...

  7. 洛谷P3899 [湖南集训]谈笑风生(线段树合并)

    题意 题目链接 Sol 线段树合并板子题,目前我看到两种写法,分别是这样的. 前一种每次需要新建一个节点,空间是\(O(4nlogn)\) 后者不需要新建,空间是\(O(nlogn)\)(面向数据算空 ...

  8. 【洛谷 P3899】 [湖南集训]谈笑风生 (主席树)

    题目链接 容易发现\(a,b,c\)肯定是在一条直链上的. 定义\(size(u)\)表示以\(u\)为根的子树大小(不包括\(u\)) 分两种情况, 1.\(b\)是\(a\)的祖先,对答案的贡献是 ...

  9. P3899 [湖南集训]谈笑风生 主席树

    #include<iostream> #include<string.h> #include<algorithm> #include<stdio.h> ...

随机推荐

  1. vi下的查找替换命令

    1.查找 查找命令 /pattern:向下查找匹配的字符 ?pattern:向上查找匹配的字符 其中pattern是需要匹配的字符串,例如: /cd #全文查找cd / cd <Enter> ...

  2. 【LeetCode】汇总

    此贴为汇总贴 673. Number of Longest Increasing Subsequence 075. Sort Colors 009. Palindrome Number 008. St ...

  3. Google搜索被屏蔽,如何使用Google搜索

    我们在国内使用搜索引擎最多的是Google和Baidu啦,在引擎上找一些我们需要的知识,最近好像www.google.cn已经无法访问了,并且香港的链接www.google.com.hk也无法访问了, ...

  4. 学习动态性能表(4)--v$sqltext&v$sqlarea

    学习动态性能表 第四篇-(1)-V$SQLTEXT  2007.5.29 本视图包括Shared pool中SQL语句的完整文本,一条SQL语句可能分成多个块被保存于多个记录内. 注:V$SQLARE ...

  5. poj 3046 Ant Counting——多重集合的背包

    题目:http://poj.org/problem?id=3046 多重集合的背包问题. 1.式子:考虑dp[ i ][ j ]能从dp[ i-1 ][ k ](max(0 , j - c[ i ] ...

  6. About CoffeeScript

    本篇文章是对CoffeeScript做一个初步的了解.入门的学习笔记. 什么是CoffeeScript 一种新编程语言,是一套JavaScript的转译语言,可编译成高效的JavaScript.还可以 ...

  7. MongoDB 4.X搭建

    一.MongoDB4.X搭建 1.下载mongdb安装包,在官网上找到对应的版本,我的是centos7 找到上面的连接,通过命令行: 2.将下载的mongodb-linux-x86_64-4.0.0. ...

  8. 事务之四:Spring事务--原理

    一.Spring事务的基本原理 Spring事务的本质其实就是数据库对事务的支持,没有数据库的事务支持,spring是无法提供事务功能的.对于纯JDBC操作数据库,想要用到事务,可以按照以下步骤进行: ...

  9. org.apache.hadoop.security.AccessControlException: Permissiondenied: user=liuyingping, access=WRITE,inode="/user/root/output":root:supergroup:drwxr-xr-x

    原因: 权限问题.用户liuyingping没有访问hdfs下文件的权限. 参考:HDFS客户端的权限错误:Permission denied 解决方案(推荐): 在系统的环境变量添加HADOOP_U ...

  10. Linux学习笔记 -- 目录与文件的管理

    目录结构 Linux的目录结构为树状结构,最顶级的目录为根目录 “/”. 其他目录通过挂载可以将它们添加到树中,通过解除挂载可以移除它们. 在开始本教程前我们需要先知道什么是. 绝对路径与相对路径 绝 ...