算是学到了树状数组一种新的打开方式
比赛的时候还在想怎么支持查找某个数,然后可以直接求幂和
对于某个算出他和前面所有小于他的数的距离为k,然后快速求出2^k之和;
显然是用树状数组:
每次插入的时候并不是在对应位置+1,而是加一个逆元的i次幂,然后每次查询都将求得的结果乘上当前位置的2的幂即可
∑
2
j
−
i
−
1
=
∑
2
j
−
1
/
2
i
\sum 2^{j-i-1}=\sum 2^{j-1}/2^{i}
∑2j−i−1=∑2j−1/2i
#include<unordered_set>
#include<unordered_map>
#include<functional>
#include<algorithm>
#include<string.h>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<set>
#include<map>
using namespace std;
//================================
#define debug(a) cout << #a": " << a << endl;
#define N 300010
const int mod = 998244353;
//================================
typedef pair<int,int> pii;
#define x first
#define y second
typedef long long LL; typedef unsigned long long ULL; typedef long double LD;
inline LL read() { LL s = 0, w = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') w = -1; for (; isdigit(ch); ch = getchar()) s = (s << 1) + (s << 3) + (ch ^ 48); return s * w; }
inline void print(LL x, int op = 10) { if (!x) { putchar('0'); if (op) putchar(op); return; } char F[40]; LL tmp = x > 0 ? x : -x; if (x < 0)putchar('-'); int cnt = 0; while (tmp > 0) { F[cnt++] = tmp % 10 + '0'; tmp /= 10; } while (cnt > 0)putchar(F[--cnt]); if (op) putchar(op); }
//=================================
LL n;
LL a[N],tr[N];
vector<LL> alls;
int lowbit(LL x){
return x&-x;
}
void add(LL x,LL c){
for(LL i=x;i<=n;i+=lowbit(i))
tr[i]+=c;
}
LL query(LL x){
LL ans=0;
for(LL i=x;i;i-=lowbit(i))
ans=(ans+tr[i])%mod;
return ans;
}
LL fpower(LL a,LL b){
LL res=1;
while(b){
if(b&1) res=(LL)res*a%mod;
a=(LL)a*a%mod;
b>>=1;
}
return res;
}
void init(){
scanf("%lld",&n);
for(int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
alls.push_back(a[i]);
}
sort(alls.begin(),alls.end());
alls.erase(unique(alls.begin(),alls.end()),alls.end());
for(int i=1;i<=n;i++)
a[i]=lower_bound(alls.begin(),alls.end(),a[i])-alls.begin()+1;
}
void solve(){
const LL divv = fpower(2,mod-2);
LL ans=0;
for(int i=1;i<=n;i++){
ans=((LL)ans+(LL)fpower(2,i-1)*query(a[i]))%mod;
add(a[i],fpower(divv,i));
}
cout<<ans<<endl;
}
//=================================
signed main(){
init();
solve();
return 0;
}