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.线段树 ...
随机推荐
- Calendar的用法
DAY_OF_MONTH的主要作用是cal.get(DAY_OF_MONTH),用来获得这一天在是这个月的第多少天 Calendar.DAY_OF_YEAR的主要作用是cal.get(DAY_OF_Y ...
- idea_复制包名类名
- C/C++学习)22.QTcpServer、QTcpSocket、QUdpSocket使用
一.TCP/UDP通信在Qt中的实现过程: 废话不说,首先下面是Qt中TCP/UDP的实现图解: 1.Qt下TCP通信详解: 针对上图进行简单的说明: QTcpServer用来创建服务 ...
- docke容器使用
Docker 容器使用 Docker 客户端 docker 客户端非常简单 ,我们可以直接输入 docker 命令来查看到 Docker 客户端的所有命令选项. runoob@runoob:~# do ...
- LES on Wind turbine
- 《编译原理》构造 LL(1) 分析表的步骤 - 例题解析
<编译原理>构造 LL(1) 分析表的步骤 - 例题解析 易错点及扩展: 1.求每个产生式的 SELECT 集 2.注意区分是对谁 FIRST 集 FOLLOW 集 3.开始符号的 FOL ...
- Sigmoid Function
本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/51734189 Sigmodi 函数是一 ...
- noip模拟赛 立方数2
题目描述LYK定义了一个数叫“立方数”,若一个数可以被写作是一个正整数的3次方,则这个数就是立方数,例如1,8,27就是最小的3个立方数.LYK还定义了一个数叫“立方差数”,若一个数可以被写作是两个立 ...
- Neo4j ETL工具快速上手:简化从关系数据库到图数据库的数据迁移
注:本文系从https://medium.com/neo4j/tap-into-hidden-connections-translating-your-relational-data-to-graph ...
- 【GC概述以及查看堆内存使用】Java内存管理和GC学习
内存划分 1.JAVA内存主要划分为方法栈.方法区.堆. 2.方法栈上内存会自动释放: 3.方法区上主要加载了类的元信息.静态变量.常量.改区域又称为持久代(Perm Gen),默认是最小16M,最大 ...