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.线段树 ...
随机推荐
- Java线程处理
Java线程处理 创建线程 继承Thread类 public class TestThread extends Thread{ public void run(){ System.out.printl ...
- 10Java Server Pages 隐式对象
Java Server Pages 隐式对象 JSP隐式对象是Web容器加载的一组类的实例,它不像一般的Java对象那样用“new”去获取实例,而是可以直接在JSP页面使用的对象.JSP提供的隐式对象 ...
- jquery的delegate()方法
delegate() 方法为指定的元素(属于被选元素的子元素)添加一个或多个事件处理程序,并规定当这些事件发生时运行的函数. 使用 delegate() 方法的事件处理程序适用于当前或未来的元素(比如 ...
- 2019西安多校联训 Day4
T1 大水题!!难度简单,显然的贪心策略即可,but... 思路:首先我们按与i点作战后活下来的士兵排序,然后 若当前剩余兵力足够直接减掉战斗死亡人数,如果不够就加 够再打它,但是!我们在考完试观察测 ...
- Luogu P4549 裴蜀定理 / Min
思路 题目已经给出了正解.我们只需要将裴蜀定理推广到若干数的线性组合就可以做这道题了 要注意的是需要在输入的时候取一个绝对值.因为可能会有负数存在.我之前也写过裴蜀定理的证明,要看的话点这里 吐槽 第 ...
- geth搭建以太坊私链及常用操作
一.下载安装geth客户端 https://www.ethereum.org/ 二.搭建私有链 1.准备创世区块配置文件 要运行私有链,我们就需要定义自己的创世区块,创世区块信息写在一个json格式的 ...
- 充当别的mcu的外部存储器(51类)
// 锁存地址 - STC12C5A60S2 reg [15:0]rAddr_51; //存放51单片机传过来的地址 读51地址寄存器 always @ (posedge MCLKout or neg ...
- Nginx学习总结(3)——Nginx配置及应用场景之高级配置
一.Nginx反向代理 反向代理(Reverse Proxy)方式是指以代理服务器来接受Internet上的连接请求,然后将请求转发给内部网络上的服务器:并将从服务器上得到的结果返回给Internet ...
- Introduction to TensorFlow
Lecture note 1: Introduction to TensorFlow Why TensorFlow TensorFlow was originally created by resea ...
- SGU 485 Arrays
485. Arrays Time limit per test: 1.75 second(s)Memory limit: 262144 kilobytes input: standardoutput: ...