前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >2019-计蒜之道-复赛A--外教-Michale-变身大熊猫(最长上升子序列+树状数组)

2019-计蒜之道-复赛A--外教-Michale-变身大熊猫(最长上升子序列+树状数组)

作者头像
xiaohejun
发布2020-02-18 09:33:23
2170
发布2020-02-18 09:33:23
举报

本题要求

代码语言:javascript
复制
#pragma GCC optimize("O2") 
#include <bits/stdc++.h>
using namespace std;

#define dbg(x) cerr << #x"=" << x << endl;
typedef long long LL;
int n;
const LL MOD = 998244353;
typedef pair<LL, LL> P;
#define len first
#define cnt second
#define val first
#define id second
const int MAX_N = 5*1e5+100;
const LL INF = 1e9;
P a[MAX_N];
P bit[MAX_N];
int dp[MAX_N];
P L[MAX_N], R[MAX_N];

void upd(P &a, P b){
	if(b.len > a.len){
		a = b;
	} else if(b.len == a.len){
		a.cnt = (a.cnt + b.cnt) % MOD;
	}
}

void add(int i, P p){
	while(i <= n){
		upd(bit[i], p);
		i += i & -i;	
	}
}

P query(int i){
	P res;
	while(i){
		upd(res, bit[i]);
		i -= i & -i;
	}
	return res;
}

bool cmp(P a, P b){
	return a.val < b.val || (a.val == b.val && a.id > b.id);
}

LL powN(LL base, int n){
	LL res = 1LL;
	while(n){
		if(n&1) res = res * base % MOD;
		base = base * base % MOD;
		n >>= 1;
	}
	return res;
} 

int main(){
    //freopen("in.txt","r",stdin);    
	ios::sync_with_stdio(0); cin.tie(0);
	scanf("%d", &n);
	for(int i = 0; i < n; ++i) dp[i] = INF;
	for(int i = 0; i < n; ++i){
		scanf("%lld", &a[i].val);
		*lower_bound(dp, dp+n, a[i].val) = a[i].val;
		a[i].id = i+1;
	}
	int mx = lower_bound(dp, dp+n, INF) - dp;
	sort(a, a+n, cmp);
	LL ans = 0;
	for(int i = 0; i < n; ++i){
		P p = query(a[i].id-1);
		if(++p.len == 1) p.cnt = 1;
		// p.len表示以当前作为结尾的上升序列的最大长度.p.cnt表示有多少个这样的序列 
		if(p.len == mx){
			ans = (ans + p.cnt) % MOD;
		}
		add(a[i].id, p);
		L[a[i].id] = p;
	}
	for(int i = 1; i <= n; ++i) {
		bit[i].len = 0;
		bit[i].cnt = 0;
	}
	for(int i = n-1; i >= 0; --i){
		P p = query(n-a[i].id);
		if(++p.len == 1) p.cnt = 1;
		add(n+1-a[i].id, p);
		R[a[i].id] = p;
	}
	LL q = powN(ans, MOD-2);
	for(int i = 1; i <= n; ++i){
		if(L[i].len + R[i].len == mx+1){
			printf("%lld ", L[i].cnt * R[i].cnt % MOD * q % MOD);
		} else printf("0 ");
	}
	putchar('\n');
	return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-08-102,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档