[BZOJ4756] [Usaco2017 Jan]Promotion Counting(线段树合并)
此题很有意思,有多种解法
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;
}
[BZOJ4756] [Usaco2017 Jan]Promotion Counting(线段树合并)的更多相关文章
- BZOJ4756: [Usaco2017 Jan]Promotion Counting(线段树合并)
题意 题目链接 Sol 线段树合并板子题 #include<bits/stdc++.h> using namespace std; const int MAXN = 400000, SS ...
- BZOJ[Usaco2017 Jan]Promotion Counting——线段树合并
题目描述 The cows have once again tried to form a startup company, failing to remember from past experie ...
- bzoj 4756 [Usaco2017 Jan]Promotion Counting——线段树合并
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4756 线段树合并裸题.那种返回 int 的与传引用的 merge 都能过.不知别的题是不是这 ...
- [BZOJ4756][Usaco2017 Jan]Promotion Counting 树状数组
4756: [Usaco2017 Jan]Promotion Counting Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 305 Solved: ...
- 【dsu || 线段树合并】bzoj4756: [Usaco2017 Jan]Promotion Counting
调半天原来是dsu写不熟 Description The cows have once again tried to form a startup company, failing to rememb ...
- BZOJ4756:[USACO]Promotion Counting(线段树合并)
Description n只奶牛构成了一个树形的公司,每个奶牛有一个能力值pi,1号奶牛为树根. 问对于每个奶牛来说,它的子树中有几个能力值比它大的. Input n,表示有几只奶牛 n<=10 ...
- bzoj 4756 Promotion Counting —— 线段树合并
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4756 合并子树的权值线段树: merge 返回 int 或者是 void 都可以. 代码如下 ...
- 【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
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4756 [题解] dsu on tree,树状数组直接上 O(nlog^2n) # inclu ...
随机推荐
- spark中产生shuffle的算子
Spark中产生shuffle的算子 作用 算子名 能否替换,由谁替换 去重 distinct() 不能 聚合 reduceByKey() groupByKey groupBy() groupByKe ...
- 使用POI创建word表格合并单元格兼容wps
poi创建word表格合并单元格代码如下: /** * @Description: 跨列合并 */ public void mergeCellsHorizontal(XWPFTable table, ...
- java代码(处理json串)
package test; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; public class ...
- (十二)mybatis之动态代理
mybatis之动态代理的应用 在前文(https://www.cnblogs.com/NYfor2018/p/9093472.html)我们知道了,Mybatis的使用需要用到Mapper映射文件, ...
- 项目中多条数据保存的json实例
//js代码function checkCode(num){ var typeid = $("#typeid").val(); if(typeid == "") ...
- javaEE(5)_Cookie和Session
一.会话 1.什么是会话?会话可简单理解为:用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话.类似打电话一样.2.会话过程中要解决的一些问题?每个用户 ...
- vsftp配置日志及其启用本地时间
vsftp配置日志及其启用本地时间 1. 启用vsftp日志 xferlog_enable=YES xferlog_std_format=YES xferlog_file=/var/log/xferl ...
- 使用一位数组解决 1 1 2 3 5 8 13 数列问题 斐波纳契数列 Fibonacci
斐波纳契数列 Fibonacci 输出这个数列的前20个数是什么? 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 使用数组实现输出数列的前30 ...
- 51nod 1264 线段相交——计算几何
题目链接:http://www.51nod.com/Challenge/Problem.html#!#problemId=1264 检查点的位置就行了,具体见注释. /* (a-c)×(d-c)*(d ...
- noip_最后一遍_2-图论部分
大体按照 数学 图论 dp 数据结构 这样的顺序 模板集 这个真的只有模板了……………… ·spfa #include<bits/stdc++.h> using namespace std ...