ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

P3605 [USACO17JAN]Promotion Counting P 题解

2022-08-21 23:00:08  阅读:136  来源: 互联网

标签:rt int 题解 mid tot USACO17JAN Promotion now define


solution

考虑权值线段树合并:首先离散化,然后对于一个节点,我们将它的所有子树合并上来,并统计所有能力指数的个数(权值线段树基本操作),查询时只需查询 \(p_i+1\sim n\) 的和即可

code

#include <bits/stdc++.h>
#define Tp template<typename Ty>
#define Ts template<typename Ty,typename... Ar>
#define I inline
#define ll long long
#define CI const int
#define RI register int
#define W while
#define gc getchar
#define D isdigit(c=gc())
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))
#define ms(a,x) memset((a),(x),sizeof(a))
using namespace std;
namespace SlowIO {
	I void readc (char& c) {W (isspace (c = gc ()));}
	Tp I void read (Ty& x) {char c; int f = 1; x = 0; W (! D) f = c ^ '-' ? 1 : -1; W (x = (x * 10) + (c ^ 48), D); x *= f;}
	Ts I void read (Ty& x, Ar&... y) {read (x); read (y...);}
} using namespace SlowIO;
CI N = 1e5, N2 = 2e5, N4 = 5e6; int sum[N4 + 5], tg[N4 + 5], sl[N4 + 5], sr[N4 + 5], a[N + 5], lsh[N + 5], anss[N + 5], rt[N + 5], ntot = 0, tot, n;
namespace graph {
	int nxt[N2 + 5], to[N2 + 5], hd[N + 5], cnt = 0;
	void add (int u, int v) {++ cnt; to[cnt] = v; nxt[cnt] = hd[u]; hd[u] = cnt;}
} using namespace graph;
void pushup (int root) {sum[root] = sum[sl[root]] + sum[sr[root]];}
void update (int &root, int L, int R, int pos) {
	if (! root) root = ++ ntot; if (L == R) {++ sum[root]; tg[root] = L; return ;} int mid = (L + R) >> 1;
	if (pos <= mid) update (sl[root], L, mid, pos); else update (sr[root], mid + 1, R, pos); pushup (root);
}
int merge (int a, int b, int L, int R) {
	if (! a || ! b) return a + b; if (L == R) {sum[a] += sum[b]; tg[a] = tg[b]; return a;} int mid = (L + R) >> 1;
	sl[a] = merge (sl[a], sl[b], L, mid); sr[a] = merge (sr[a], sr[b], mid + 1, R); pushup (a); return a;
}
int query (int root, int L, int R, int x, int y) {
	if (L >= x && R <= y) return sum[root]; if (R < x || L > y) return 0; int mid = (L + R) >> 1, s = 0;
	if (x <= mid) s += query (sl[root], L, mid, x, y); if (y > mid) s += query (sr[root], mid + 1, R, x, y); return s;
}
void dfs (int now, int fa) {
	RI i, j; for (i = hd[now]; i; i = nxt[i]) {if (to[i] == fa) continue; dfs (to[i], now); rt[now] = merge (rt[now], rt[to[i]], 1, tot); } update (rt[now], 1, tot, a[now]);
	anss[now] = query (rt[now], 1, tot, a[now] + 1, tot);
}
int main () {
	RI i, j; for (read (n), i = 1; i <= n; ++ i) read (a[i]), lsh[i] = a[i]; sort (lsh + 1, lsh + n + 1); tot = unique (lsh + 1, lsh + n + 1) - lsh - 1;
	for (i = 1; i <= n; ++ i) a[i] = lower_bound (lsh + 1, lsh + tot + 1, a[i]) - lsh; for (i = 2; i <= n; ++ i) {int x; read (x); add (i, x); add (x, i);} dfs (1, 0);
	for (i = 1; i <= n; ++ i) printf ("%d\n", anss[i]);
	return 0;
}

标签:rt,int,题解,mid,tot,USACO17JAN,Promotion,now,define
来源: https://www.cnblogs.com/ClapEcho233/p/16611319.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有