ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#197231 | #2385. 胜天半子 | snow_trace | 100 | 2487ms | 138356kb | C++11 | 3.2kb | 2023-11-09 09:44:04 | 2023-11-09 12:03:01 |
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 998244353;
const int inv = 998244353+1>>1;
int qp(int p,int q){
int ans = 1,pro = p;
while(q){
if(q&1)ans = ans*pro%mod;
q>>=1;pro = pro*pro%mod;
}return ans;
}int niyuan(int x){
return qp(x,mod-2);
}struct node{
int l,r;int lazy = 1,sum = 0;
void add(int x){
lazy = lazy*x%mod;sum = sum*x%mod;return;
}
}tree1[2000005],tree2[2000005];
inline void push_down1(int k){
if(tree1[k].lazy!=1)tree1[k<<1].add(tree1[k].lazy),tree1[k<<1|1].add(tree1[k].lazy),tree1[k].lazy = 1;return;
}inline void push_down2(int k){
if(tree2[k].lazy!=1)tree2[k<<1].add(tree2[k].lazy),tree2[k<<1|1].add(tree2[k].lazy),tree2[k].lazy = 1;return;
}inline void push_up1(int k){
tree1[k].sum = (tree1[k<<1].sum+tree1[k<<1|1].sum)%mod;
}inline void push_up2(int k){
tree2[k].sum = (tree2[k<<1].sum+tree2[k<<1|1].sum)%mod;
}inline void build1(int l,int r,int k){
tree1[k].l = l,tree1[k].r = r;
if(l+1 == r){
tree1[k].sum = 1;return;
}build1(l,l+r>>1,k<<1),build1(l+r>>1,r,k<<1|1);push_up1(k);
}inline void build2(int l,int r,int k){
tree2[k].l = l,tree2[k].r = r;
if(l+1 == r){
tree2[k].sum = 1;return;
}build2(l,l+r>>1,k<<1),build2(l+r>>1,r,k<<1|1);push_up2(k);
}inline void upd1(int k,int pos){
int l = tree1[k].l,r = tree1[k].r;
if(pos>=r)return;
if(l>=pos){
tree1[k].add(2);return;
}push_down1(k);upd1(k<<1,pos);upd1(k<<1|1,pos);push_up1(k);
}inline void upd2(int k,int pos){
int l = tree2[k].l,r = tree2[k].r;
if(pos>=r)return;
if(l>=pos){
tree2[k].add(inv);return;
}push_down2(k);upd2(k<<1,pos);upd2(k<<1|1,pos);push_up2(k);
}inline int query1(int l,int r,int k){
int ll = tree1[k].l,rr = tree1[k].r;
if(l>=rr or ll>=r)return 0;
if(l<=ll and rr<=r)return tree1[k].sum;push_down1(k);
return (query1(l,r,k<<1)+query1(l,r,k<<1|1))%mod;
}inline int query2(int l,int r,int k){
int ll = tree2[k].l,rr = tree2[k].r;
if(l>=rr or ll>=r)return 0;
if(l<=ll and rr<=r)return tree2[k].sum;push_down2(k);
return (query2(l,r,k<<1)+query2(l,r,k<<1|1))%mod;
}
int a[500005];
vector<pair<int,int> >p;
int n;
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 1;i<=n;i++)cin >> a[i];
for(int i = 1;i<=n;i++)p.push_back({a[i],i});
build1(1,n+1,1),build2(1,n+1,1);
//cout << 111 << endl;
int ans = 0;
sort(p.begin(),p.end());reverse(p.begin(),p.end());
for(int i =0;i<p.size();i++){
int id = p[i].second;
// cout << " " << id << " " << p[i].first << endl;
if(id!=n)upd1(1,id+1);upd2(1,id);
int q1 = query2(id,n+1,1),q2 = query1(1,id+1,1);
// cout << q1 << " " << niyuan(q2) << endl;
// cout << niyuan(q1) << " " << q2 << endl;cout << endl;
ans = (ans+p[i].first*q1%mod*q2%mod)%mod;
}cout << ans << endl;
return 0;
}/*
****** ******
********** **********
************* *************
*****************************
*****************************
*****************************
***************************
***********************
*******************
***************
***********
*******
***
*
*/
Details
小提示:点击横条可展开更详细的信息
Test #1:
score: 10
Accepted
time: 11ms
memory: 126280kb
input:
10 391025536 534111809 87391850 717213748 549806037 146679483 332329431 354719733 915413648 936262795
output:
313455611
result:
ok 1 number(s): "313455611"
Test #2:
score: 10
Accepted
time: 7ms
memory: 126284kb
input:
100 954759219 905502779 807519582 781552230 758309007 723757145 711489415 654050518 652846076 637356...
output:
769415708
result:
ok 1 number(s): "769415708"
Test #3:
score: 10
Accepted
time: 7ms
memory: 126288kb
input:
100 988968776 972536122 956739770 952203362 941180364 928023078 862509294 840037070 811395553 739326...
output:
24673783
result:
ok 1 number(s): "24673783"
Test #4:
score: 10
Accepted
time: 12ms
memory: 126460kb
input:
5000 999055700 997981714 997127741 996687146 996645302 994183022 993839721 993577550 993132750 99218...
output:
240553746
result:
ok 1 number(s): "240553746"
Test #5:
score: 10
Accepted
time: 18ms
memory: 126456kb
input:
5000 999402420 999383899 998043142 998036168 996714542 995854467 994013899 993583353 993133300 99280...
output:
725794147
result:
ok 1 number(s): "725794147"
Test #6:
score: 10
Accepted
time: 146ms
memory: 128948kb
input:
100000 999983033 999969215 999939917 999913875 999878133 999851751 999832454 999754907 999722070 999...
output:
211792036
result:
ok 1 number(s): "211792036"
Test #7:
score: 10
Accepted
time: 146ms
memory: 128948kb
input:
100000 999931068 999770813 999721486 999719350 999707892 999696269 999667058 999642633 999639736 999...
output:
563213385
result:
ok 1 number(s): "563213385"
Test #8:
score: 10
Accepted
time: 645ms
memory: 138352kb
input:
500000 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 1...
output:
602574492
result:
ok 1 number(s): "602574492"
Test #9:
score: 10
Accepted
time: 657ms
memory: 138352kb
input:
500000 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 1...
output:
979709968
result:
ok 1 number(s): "979709968"
Test #10:
score: 10
Accepted
time: 838ms
memory: 138356kb
input:
500000 999993657 999990841 999983142 999963508 999960723 999954634 999948225 999943432 999930307 999...
output:
499135918
result:
ok 1 number(s): "499135918"
Extra Test:
score: 0
Extra Test Passed