1. 程式人生 > 其它 >「ABC221E」LEQ 題解

「ABC221E」LEQ 題解

E - LEQ

Time Limit: \(2\; sec\) / Memory Limit: \(1024\; MB\)

Score : \(500\; points\)

Problem Statement|題目描述

  • Given is a sequence of \(N\) integers: \(A=(A_1 ,A_2 ,…,A_N )\).

  • 給定一個包含 \(N\) 個整數的序列: \(A=(A_1 ,A_2 ,…,A_N )\)

  • Find the number of (not necessarily contiguous) subsequences$ A′ =(A_1′ ,A_2′ ,…,A_k′ )$ of length at least \(2\)

    that satisfy the following condition:

\(A_1′\leq A_k′ .\)

  • 求子序列的個數(不一定是連續的)\(A′ =(A_1′ ,A_2′ ,…,A_k′ )\),要求長度至少為 \(2\),且滿足以下條件:

\(A_1′\leq A_k′ .\)

  • Since the count can be enormous, print it modulo \(998244353\).

  • 由於結果可能很大,所以將其對 \(998244353\) 取模輸出。

  • Here, two subsequences are distinguished when they originate from different sets of indices, even if they are the same as sequences.

  • 在這裡,當兩個子序列來自不同的集合位置時,即使它們與序列相同,也可以區分它們。

Constraints|資料範圍

  • \(2\leq N\leq 3\times 10^5\)

  • \(1\leq A_i\leq 10^9\)

  • All values in input are integers.

  • \(2\leq N\leq 3\times 10^5\)

  • \(1\leq A_i\leq 10^9\)

  • 輸入中的所有值都是整數。

Input|輸入

Input is given from Standard Input in the following format:

\(N\)
\(A_1\ A_2\ …\ A_N\)

  • 輸入為以下格式的標準輸入(中間有空格):

\(N\)
\(A_1\ A_2\ …\ A_N\)

Output|輸出

  • Print the number of (not necessarily contiguous) subsequences$ A′ =(A_1′ ,A_2′ ,…,A_k′ )$ of length at least \(2\) that satisfy the condition in Problem Statement.

  • 輸出子序列的個數(不一定是連續的)\(A′ =(A_1′ ,A_2′ ,…,A_k′ )\),要求長度至少為\(2\),且滿足題目描述中的條件。


分析

AC程式碼

#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
const int mod=998244353;
const int maxn=300010;
const int inf=0x3f3f3f3f;
int n;
inline ll power(ll a,ll b){
	ll ans=1;
	while(b){
		if(b&1)ans=(ans*a)%mod;
		b>>=1;
		a=(a*a)%mod;
	}
	return ans%mod;
}
inline ll inv(ll x){return power(x,mod-2);}//逆元
ll a[maxn],b[maxn];
int sum[maxn];
inline int lowbit(int x){return x&(-x);}
inline void modify(int x,int k){
	while(x<=n){
		sum[x]=(sum[x]+k)%mod;
		x+=lowbit(x);
	}
}
inline ll ask(int x){
	ll res=0;
	while(x){
		res=(res+sum[x])%mod;
		x-=lowbit(x);
	}
	return res;
}
inline ll query(int l,int r){
	ll res=(ask(r)-ask(l-1)+mod)%mod;
//	printf("%d\n",res);
	return res;
}
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%lld",&a[i]);
		b[i]=a[i];
	}
	sort(b+1,b+1+n);
	cc_hash_table<int,int>rank;//雜湊表
	for(int i=1;i<=n;i++)rank[b[i]]=i;//離散化
	for(int i=1;i<=n;i++)//樹狀陣列
		modify(rank[a[i]],power(2,i));
	ll ans=0;
	for(int i=1;i<=n;i++){
		modify(rank[a[i]],mod-power(2,i));
		ans+=inv(power(2,i+1))*query(rank[a[i]],n);
		ans%=mod;
	}
	printf("%lld",ans);
	return 0;
}

$$-----CONTINUE------$$

< last 「ABC221D」Online games 題解

> catalogue 「ABC221」題解