传送门

此题很有意思,有多种解法

1.用天天爱跑步的方法,进入子树的时候ans-query,出去子树的时候ans+query,query可以用树状数组或线段树来搞

2.按dfs序建立主席树

3.线段树的合并

前两个都会,于是学习一下线段树的合并。。

道理用文字解释不清。。。直接看代码就能看懂。。

可以脑补出,合并的操作复杂度是logn的,总时间复杂度nlogn

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 100001 int n, m, cnt, size;
int head[N], to[N << 1], next[N << 1], root[N], sum[N * 20], ls[N * 20], rs[N * 20], ans[N], a[N], b[N]; inline int read()
{
int x = 0, f = 1;
char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
return x * f;
} inline void add(int x, int y)
{
to[cnt] = y;
next[cnt] = head[x];
head[x] = cnt++;
} inline void insert(int &now, int l, int r, int x)
{
now = ++size;
if(l == r)
{
sum[now] = 1;
return;
}
int mid = (l + r) >> 1;
if(x <= mid) insert(ls[now], l, mid, x);
else insert(rs[now], mid + 1, r, x);
sum[now] = sum[ls[now]] + sum[rs[now]];
} inline void merge(int &x, int y)
{
if(!x || !y)
{
x += y;
return;
}
sum[x] += sum[y];
merge(ls[x], ls[y]);
merge(rs[x], rs[y]);
} inline int query(int now, int l, int r, int x, int y)
{
if(x <= l && r <= y) return sum[now];
int mid = (l + r) >> 1, ret = 0;
if(x <= mid) ret += query(ls[now], l, mid, x, y);
if(mid < y) ret += query(rs[now], mid + 1, r, x, y);
return ret;
} inline void dfs(int u)
{
int i, v;
for(i = head[u]; i ^ -1; i = next[i])
{
v = to[i];
dfs(v);
merge(root[u], root[v]);
}
ans[u] = query(root[u], 1, m, a[u] + 1, m);
} int main()
{
int i, x;
n = read();
memset(head, -1, sizeof(head));
for(i = 1; i <= n; i++) a[i] = b[i] = read();
for(i = 2; i <= n; i++)
{
x = read();
add(x, i);
}
std::sort(b + 1, b + n + 1);
m = std::unique(b + 1, b + n + 1) - b - 1;
for(i = 1; i <= n; i++)
a[i] = std::lower_bound(b + 1, b + m + 1, a[i]) - b;
for(i = 1; i <= n; i++)
insert(root[i], 1, m, a[i]);
dfs(1);
for(i = 1; i <= n; i++)
printf("%d\n", ans[i]);
return 0;
}

  

最新文章

  1. redis-内存异常 Redis is configured to save RDB snapshots解决
  2. window.onload和$(document).ready(function(){})的区别
  3. GDUFE-OJ 1361校庆抽奖 翻转
  4. java基础-servlet-2:生命周期
  5. AndRoid studio创建APP图标
  6. Expression Blend4经验分享:文字公告无缝循环滚动效果
  7. 获取Python安装目录
  8. 【OpenGL】VS2010环境配置 [转]
  9. angular 自定义指令 link
  10. DevExpress中使用DocumentManager,并确保不重复
  11. BP神经网络求解异或问题(Python实现)
  12. 利用Jenkins自动部署工具间接构建kettle的调度平台
  13. 对libssh的golang 封装 ssh2go
  14. bzoj1619[Usaco2008 Nov]Guarding the Farm 保卫牧场
  15. 认识IL代码---从开始到现在 &lt;第二篇&gt;
  16. Leetcode算法刷题:第100题 Same Tree
  17. hive字段名、注释中文显示问号
  18. css a的伪类顺序
  19. ThreadPoolExecutor使用
  20. 福州大学软件工程1816 | W班 第1次作业成绩排名

热门文章

  1. uvm_transaction——事物
  2. POJ 2955 Brackets (区间DP,常规)
  3. 洛谷 P1311 选择客栈
  4. ubuntu 16.04 国内源安装docker
  5. Android(java)学习笔记145:Handler消息机制的原理和实现
  6. osx launchpad图标的删除
  7. 广播接收者 BroadcastReceiver
  8. servlet多文件上传(带进度条)
  9. Bootstrap CSS概览
  10. ios之UIScrollView