线段树合并都是蓝题了嘛 我可能和时代脱轨了emm...

直接离散化然后合并就好啦w

生病了真难受QAQ

//Love and Freedom.
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define ll long long
#define inf 20021225
#define mxn 100010
using namespace std; struct node
{
int ls,rs,sum;
}t[mxn*40];
int cnt,rt[mxn],ans[mxn];
struct edge
{
int to,lt;
}e[mxn];
int val[mxn],in[mxn],ecnt;
int p[mxn],dd[mxn],n; void add(int x,int y)
{
e[++ecnt].to=y;e[ecnt].lt=in[x];in[x]=ecnt;
} void pushup(int x)
{
t[x].sum = t[t[x].ls].sum+t[t[x].rs].sum;
} int merge(int x,int y,int l,int r)
{
if(!x||!y) return x+y;
if(l==r)
{
t[x].sum+=t[y].sum;
return x;
}
int mid = (l+r)>>1;
t[x].ls=merge(t[x].ls,t[y].ls,l,mid);
t[x].rs=merge(t[x].rs,t[y].rs,mid+1,r);
pushup(x); return x;
} void insert(int &x,int l,int r,int d)
{
if(!x) x=++cnt; t[x].sum++;
if(l==r) return;
int mid=(l+r)>>1;
if(d<=mid) insert(t[x].ls,l,mid,d);
else insert(t[x].rs,mid+1,r,d);
pushup(x);
} int query(int x,int l,int r,int d)
{
if(!x) return 0;
if(l==r) return t[x].sum;
int mid=(l+r)>>1;
if(d<=mid) return query(t[x].ls,l,mid,d)+t[t[x].rs].sum;
else return query(t[x].rs,mid+1,r,d);
}
bool cmp(int x,int y)
{
return val[x]<val[y];
}
void dfs(int x)
{
for(int i=in[x];i;i=e[i].lt)
dfs(e[i].to);
insert(rt[x],1,n+1,p[x]);// int tmp = ;
for(int i=in[x];i;i=e[i].lt)
rt[x]=merge(rt[x],rt[e[i].to],1,n+1);
ans[x] = query(rt[x],1,n+1,p[x]+1);
}
int main()
{
int f;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&val[i]),dd[i]=i;
sort(dd+1,dd+n+1,cmp);
for(int i=1;i<=n;i++) p[dd[i]]=i;
for(int i=2;i<=n;i++)
{
scanf("%d",&f);
add(f,i);
}
dfs(1);
for(int i=1;i<=n;i++) printf("%d\n",ans[i]);
return 0;
}

洛谷3605 Promotion Counting的更多相关文章

  1. 洛谷 P3184 [USACO16DEC]Counting Haybales数草垛

    P3184 [USACO16DEC]Counting Haybales数草垛 题目描述 Farmer John has just arranged his NN haybales (1 \leq N ...

  2. 洛谷P3605 [USACO17JAN] Promotion Counting 晋升者计数 [线段树合并]

    题目传送门 Promotion Counting 题目描述 The cows have once again tried to form a startup company, failing to r ...

  3. 洛谷 P1596 [USACO10OCT]湖计数Lake Counting

    题目链接 https://www.luogu.org/problemnew/show/P1596 题目描述 Due to recent rains, water has pooled in vario ...

  4. 洛谷1640 bzoj1854游戏 匈牙利就是又短又快

    bzoj炸了,靠离线版题目做了两道(过过样例什么的还是轻松的)但是交不了,正巧洛谷有个"大牛分站",就转回洛谷做题了 水题先行,一道傻逼匈牙利 其实本来的思路是搜索然后发现写出来类 ...

  5. 洛谷P1352 codevs1380 没有上司的舞会——S.B.S.

    没有上司的舞会  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond       题目描述 Description Ural大学有N个职员,编号为1~N.他们有 ...

  6. 洛谷P1108 低价购买[DP | LIS方案数]

    题目描述 “低价购买”这条建议是在奶牛股票市场取得成功的一半规则.要想被认为是伟大的投资者,你必须遵循以下的问题建议:“低价购买:再低价购买”.每次你购买一支股票,你必须用低于你上次购买它的价格购买它 ...

  7. 洛谷 P2701 [USACO5.3]巨大的牛棚Big Barn Label:二维数组前缀和 你够了 这次我用DP

    题目背景 (USACO 5.3.4) 题目描述 农夫约翰想要在他的正方形农场上建造一座正方形大牛棚.他讨厌在他的农场中砍树,想找一个能够让他在空旷无树的地方修建牛棚的地方.我们假定,他的农场划分成 N ...

  8. 洛谷P1710 地铁涨价

    P1710 地铁涨价 51通过 339提交 题目提供者洛谷OnlineJudge 标签O2优化云端评测2 难度提高+/省选- 提交  讨论  题解 最新讨论 求教:为什么只有40分 数组大小一定要开够 ...

  9. 洛谷P1371 NOI元丹

    P1371 NOI元丹 71通过 394提交 题目提供者洛谷OnlineJudge 标签云端评测 难度普及/提高- 提交  讨论  题解 最新讨论 我觉得不需要讨论O long long 不够 没有取 ...

随机推荐

  1. android7.0对于SharedPreferences设置模式的限制

    错误信息: 03-28 10:16:12.701   830   932 E AndroidRuntime: FATAL EXCEPTION: Thread-903-28 10:16:12.701   ...

  2. RESTful三理解

    目录 目录 前言 Web应用的会话状态 Cookie 资源的表现形式 HATEOAS RESTful 资源 URI 前言 最近看了一篇很赞的RESTful博客,传送门:http://www.cnblo ...

  3. win2019

    slmgr /upkslmgr /ipk N69G4-B89J2-4G8F4-WWYCC-J464Cslmgr /skms zh.us.toslmgr /ato

  4. Oracle11g安装步骤

    plsql安装等:https://blog.csdn.net/li66934791/article/details/83856225      https://www.cnblogs.com/gaoz ...

  5. MySQL replace into 与insert into

    https://blog.csdn.net/helloxiaozhe/article/details/77427266 使用replace带来的问题 1.Replace into 操作在唯一键重复情况 ...

  6. [LeetCode] 4. Median of Two Sorted Arrays(想法题/求第k小的数)

    传送门 Description There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the m ...

  7. lib.tcl

    #********************************************************************# 功能描述:定义公共的函数# 依赖关系:依赖于全局aitoo ...

  8. vsphere虚拟化之 NTP服务的创建(三)

    1.先修改windows 2012的注册表. HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\Config\ 设置 Annou ...

  9. Go错误处理机制及自定义错误

    错误处理机制: 先看一段代码:看看输出什么? package mainimport "fmt" func test() { num1 := 10 num2 := 0 res := ...

  10. python获取ip地址

    #!/usr/bin/env python # -*- coding: utf-8 -*- import os import socket,fcntl,struct #crontab下shell命令无 ...