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的更多相关文章

  1. [模板]BZOJ4756线段树合并

    题面 Solution: 板子不解释 #include <iostream> #include <algorithm> #include <cstdio> #inc ...

  2. BZOJ4756: [Usaco2017 Jan]Promotion Counting(线段树合并)

    题意 题目链接 Sol 线段树合并板子题 #include<bits/stdc++.h> using namespace std; const int MAXN = 400000, SS ...

  3. BZOJ4756:[USACO]Promotion Counting(线段树合并)

    Description n只奶牛构成了一个树形的公司,每个奶牛有一个能力值pi,1号奶牛为树根. 问对于每个奶牛来说,它的子树中有几个能力值比它大的. Input n,表示有几只奶牛 n<=10 ...

  4. [BZOJ4756]Promotion Counting

    Description The cows have once again tried to form a startup company, failing to remember from past ...

  5. bzoj4756 [Usaco2017 Jan]Promotion Counting

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4756 [题解] dsu on tree,树状数组直接上 O(nlog^2n) # inclu ...

  6. 【bzoj4756】[Usaco2017 Jan]Promotion Counting 离散化+树状数组

    原文地址:http://www.cnblogs.com/GXZlegend/p/6832263.html 题目描述 The cows have once again tried to form a s ...

  7. [BZOJ4756][Usaco2017 Jan]Promotion Counting 树状数组

    4756: [Usaco2017 Jan]Promotion Counting Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 305  Solved: ...

  8. BZOJ4756:[USACO2017JAN]Promotion Counting

    浅谈线段树合并:https://www.cnblogs.com/AKMer/p/10251001.html 题目传送门:https://lydsy.com/JudgeOnline/problem.ph ...

  9. 【dsu || 线段树合并】bzoj4756: [Usaco2017 Jan]Promotion Counting

    调半天原来是dsu写不熟 Description The cows have once again tried to form a startup company, failing to rememb ...

  10. [BZOJ4756] [Usaco2017 Jan]Promotion Counting(线段树合并)

    传送门 此题很有意思,有多种解法 1.用天天爱跑步的方法,进入子树的时候ans-query,出去子树的时候ans+query,query可以用树状数组或线段树来搞 2.按dfs序建立主席树 3.线段树 ...

随机推荐

  1. BigDecimal舍入规则

    1.ROUND_UP 舍入远离零的舍入模式. 在丢弃非零部分之前始终增加数字(始终对非零舍弃部分前面的数字加1). 注意,此舍入模式始终不会减少计算值的大小. 2.ROUND_DOWN 接近零的舍入模 ...

  2. Go:函数、defer

    一.函数可赋值给一个变量 示例1: package main import "fmt" func add(a, b int) int { return a + b } func m ...

  3. https报错注销源文件内容

    open -a pycharm /Users/vivi/Library/Python/3.6/lib/python/site-packages/requests/packages/urllib3/co ...

  4. [bzoj4300][绝世好题] (动规)

    Description 给定一个长度为n的数列ai,求ai的子序列bi的最长长度,满足bi&bi-1!=0(2<=i<=len). Input 输入文件共2行. 第一行包括一个整数 ...

  5. Java基础学习总结(87)——坚持写Java等技术类博客的好处

    1.加深对技术点的理解 每天写博客,可以加深对技术点的理解,假如工作中,对某个技术点运用的不熟,当你通过博客的形式写出来,这个过程中,遇到不懂的知识点,你就会查阅相关的资料,弄明白他. 2.自己日后用 ...

  6. opengl 对投影变化函数的理解

    投影变化分两种: 1 . 平行投影       2 . 透视投影 投影变化的设置一般放在reshape函数当中调用 每次要对投影变化进行操作的时候我们需要修改矩阵的变化模式,指定它为投影变化 glMa ...

  7. codevs1197 Vigenère密码

    题目描述 Description 16 世纪法国外交家Blaise de Vigenère设计了一种多表密码加密算法——Vigenère密码.Vigenère 密码的加密解密算法简单易用,且破译难度比 ...

  8. java虚拟机(三)-- 虚拟机类加载机制

    1.类加载的时机:类从被加载到虚拟机内存中开始,到卸载出内存为止.包含以下几个阶段: 1.加载 2.验证 3.准备 4.解析 5.初始化 6.使用 7.卸载 2.类加载器的种类 1.启动类加载器:这个 ...

  9. sharepoint 2013安装--没安装成功--机器配置太低了

    油管上的sharepoint2013安装教程 https://www.youtube.com/watch?v=3lQVMGWJQho 下载脚本的网址 http://gallery.technet.mi ...

  10. Ubuntu 16.04安装PDF阅读器FoxitReader

    下载: https://www.foxitsoftware.cn/downloads/ 安装: tar zxvf FoxitReader2.4.1.0609_Server_x64_enu_Setup. ...