bzoj4756
http://www.lydsy.com/JudgeOnline/problem.php?id=4756
水题一枚。。。但是我写了一个小时。。。手贱打反查不出来。。。
就是每次线段树合并,先把自己的儿子都合并了, 最后和自己合并。。。
#include<bits/stdc++.h>
using namespace std;
const int N = ;
vector<int> G[N];
int n, sum, cnt;
int t[N], id[N], size[N * ], lc[N * ], rc[N * ], ans[N], root[N * ];
bool cp(int i, int j) { return t[i] < t[j]; }
void update(int l, int r, int &x, int pos)
{
x = ++cnt; int mid = (l + r) >> ;
++size[x]; if(l == r) return;
if(pos <= mid) update(l, mid, lc[x], pos);
else update(mid + , r, rc[x], pos);
}
int merge(int x, int y)
{
if(!x || !y) return x + y;
sum += size[lc[x]] * size[rc[y]];
lc[x] = merge(lc[x], lc[y]);
rc[x] = merge(rc[x], rc[y]);
size[x] += size[y];
return x;
}
void dfs(int u)
{
int last = ;
for(int i = ; i < G[u].size(); ++i)
{
int v = G[u][i];
dfs(v); root[v] = merge(root[v], root[last]);
last = v;
}
sum = ; root[u] = merge(root[u], root[last]);
ans[u] = sum;
}
int main()
{
scanf("%d", &n);
for(int i = ; i <= n; ++i)
{
scanf("%d", &t[i]); id[i] = i;
}
sort(id + , id + n + , cp);
for(int i = ; i <= n; ++i) t[id[i]] = t[id[i - ]] + (t[id[i]] != t[id[i - ]]);
for(int i = ; i <= n; ++i) update(, n, root[i], t[i]);
for(int i = ; i <= n; ++i)
{
int x; scanf("%d", &x);
G[x].push_back(i);
}
dfs();
for(int i = ; i <= n; ++i) printf("%d\n", ans[i]);
return ;
}
bzoj4756的更多相关文章
- [模板]BZOJ4756线段树合并
题面 Solution: 板子不解释 #include <iostream> #include <algorithm> #include <cstdio> #inc ...
- BZOJ4756: [Usaco2017 Jan]Promotion Counting(线段树合并)
题意 题目链接 Sol 线段树合并板子题 #include<bits/stdc++.h> using namespace std; const int MAXN = 400000, SS ...
- BZOJ4756:[USACO]Promotion Counting(线段树合并)
Description n只奶牛构成了一个树形的公司,每个奶牛有一个能力值pi,1号奶牛为树根. 问对于每个奶牛来说,它的子树中有几个能力值比它大的. Input n,表示有几只奶牛 n<=10 ...
- [BZOJ4756]Promotion Counting
Description The cows have once again tried to form a startup company, failing to remember from past ...
- bzoj4756 [Usaco2017 Jan]Promotion Counting
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4756 [题解] dsu on tree,树状数组直接上 O(nlog^2n) # inclu ...
- 【bzoj4756】[Usaco2017 Jan]Promotion Counting 离散化+树状数组
原文地址:http://www.cnblogs.com/GXZlegend/p/6832263.html 题目描述 The cows have once again tried to form a s ...
- [BZOJ4756][Usaco2017 Jan]Promotion Counting 树状数组
4756: [Usaco2017 Jan]Promotion Counting Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 305 Solved: ...
- BZOJ4756:[USACO2017JAN]Promotion Counting
浅谈线段树合并:https://www.cnblogs.com/AKMer/p/10251001.html 题目传送门:https://lydsy.com/JudgeOnline/problem.ph ...
- 【dsu || 线段树合并】bzoj4756: [Usaco2017 Jan]Promotion Counting
调半天原来是dsu写不熟 Description The cows have once again tried to form a startup company, failing to rememb ...
- [BZOJ4756] [Usaco2017 Jan]Promotion Counting(线段树合并)
传送门 此题很有意思,有多种解法 1.用天天爱跑步的方法,进入子树的时候ans-query,出去子树的时候ans+query,query可以用树状数组或线段树来搞 2.按dfs序建立主席树 3.线段树 ...
随机推荐
- 05JavaScript中的事件处理
JavaScript中的事件处理 在JavaScript中,事件的发生主要是由窗口中内容变化.键盘和鼠标引起的.JavaScript在某些事件发生的时候,可以通过一些相应的事件处理器来捕获这些事件,并 ...
- 06Microsoft SQL Server 完整性约束
Microsoft SQL Server 完整性约束 标识 IDENTITY自动编号 CREATE TABLE table_name( id ,), NAME ) not null, sex ) de ...
- libevent reference Mannual IV --Helper functions and types
FYI: http://www.wangafu.net/~nickm/libevent-book/Ref5_evutil.html Helper functions and types for Lib ...
- MyBatis 的基本要素—核心对象
MyBatis 三个基本要素 ➢ 核心接口和类 ➢ MyBatis 核心配置文件(mybatis-config.xml) ➢ SQL 映射文件(mapper.xml) MyBatis 核心接口和类 ...
- Python 开发面试题
Python部分 将一个字符串逆序,不能使用反转函数 求从10到100中能被3或5整除的数的和 What is Python? What are the benefits of using Pytho ...
- 后台工具screen
之前在putty之类的远程命令行操作服务器的时候,遇到关闭软件,对应的操作就会关闭.很多时候,就是开着电脑,然后挂在那里,虽然不用电脑跑,但是也耗电...主要是putty这些软件有时候会伴随黑屏崩掉. ...
- 51nod 1096 距离之和最小 1108 距离之和最小 V2
[题解] 很显然在一条坐标轴上到各个点距离之和最小的点就是它们的中位数.怎么证明呢?我们假设现在找的某个点x左边有a个点,右边有b个点(a>b).我们把x向左移动d个单位,并保证x左边依然有a个 ...
- 洛谷 2633 BZOJ 2588 Spoj 10628. Count on a tree
[题解] 蜜汁强制在线... 每个点开一个从它到根的可持久化权值线段树.查询的时候利用差分的思想在树上左右横跳就好了. #include<cstdio> #include<algor ...
- codeforces 372 Complete the Word(双指针)
codeforces 372 Complete the Word(双指针) 题链 题意:给出一个字符串,其中'?'代表这个字符是可变的,要求一个连续的26位长的串,其中每个字母都只出现一次 #incl ...
- JUnit基本用法
JUnit的一些注意事项: 测试方法必须使用@Test修饰 测试方法必须使用public void进行修饰,不能带参数 一般使用单元测试会新建一个test目录存放测试代码,在生产部署的时候只需要将te ...