結果

問題 No.1733 Sum of Sorted Subarrays
ユーザー chocoruskchocorusk
提出日時 2021-11-05 22:34:31
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,292 ms / 3,000 ms
コード長 4,021 bytes
コンパイル時間 3,773 ms
コンパイル使用メモリ 207,860 KB
実行使用メモリ 20,480 KB
最終ジャッジ日時 2024-04-24 06:26:42
合計ジャッジ時間 22,937 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,248 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 659 ms
12,672 KB
testcase_09 AC 1,068 ms
19,840 KB
testcase_10 AC 568 ms
12,672 KB
testcase_11 AC 740 ms
13,184 KB
testcase_12 AC 781 ms
19,200 KB
testcase_13 AC 808 ms
19,328 KB
testcase_14 AC 1,244 ms
20,224 KB
testcase_15 AC 1,177 ms
20,096 KB
testcase_16 AC 587 ms
12,672 KB
testcase_17 AC 1,292 ms
20,096 KB
testcase_18 AC 968 ms
19,584 KB
testcase_19 AC 1,068 ms
19,584 KB
testcase_20 AC 941 ms
19,456 KB
testcase_21 AC 1,136 ms
19,968 KB
testcase_22 AC 690 ms
13,056 KB
testcase_23 AC 1,200 ms
20,352 KB
testcase_24 AC 1,191 ms
20,352 KB
testcase_25 AC 1,204 ms
20,480 KB
testcase_26 AC 754 ms
20,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#include <list>
#include <atcoder/all>
#define popcount __builtin_popcount
using namespace std;
using namespace atcoder;
typedef long long ll;
typedef pair<int, int> P;
using mint=modint998244353;
template<typename T>
struct BIT{
    vector<T> bit;
    int size;
    BIT(int n):size(n), bit(n+1, 0){}
    T sum(int i){ //[0, i)
        T s=0;
        while(i>0){
            s+=bit[i];
            i-=(i&(-i));
        }
        return s;
    }
    T sum(int l, int r){ //[l, r)
        return sum(r)-sum(l);
    }
    void add(int i, T x){
        i++;
        while(i<=size){
            bit[i]+=x;
            i+=(i&(-i));
        }
    }
};
template<typename Monoid, typename OperatorMonoid=Monoid>
struct LazySegmentTree{
	using F=function<Monoid(Monoid, Monoid)>;
	using G=function<Monoid(Monoid, OperatorMonoid, int)>;
	using H=function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>;
	int sz;
	vector<Monoid> data;
	vector<OperatorMonoid> lazy;
	const F f;
	const G g;
	const H h;
	const Monoid e1;
	const OperatorMonoid e0;

	LazySegmentTree(int n, const F f, const G g, const H h, const Monoid &e1, const OperatorMonoid &e0): f(f), g(g), h(h), e1(e1), e0(e0){
		sz=1;
		while(sz<n) sz<<=1;
		data.resize(2*sz-1, e1);
		lazy.resize(2*sz-1, e0);
	}

	void build(vector<Monoid> v){
		for(int i=0; i<v.size(); i++) data[i+sz-1]=v[i];
		for(int i=sz-2; i>=0; i--) data[i]=f(data[2*i+1], data[2*i+2]);
	}

	void eval(int k, int l, int r){
		if(lazy[k]!=e0){
			data[k]=g(data[k], lazy[k], r-l);
			if(k<sz-1){
				lazy[2*k+1]=h(lazy[2*k+1], lazy[k]);
				lazy[2*k+2]=h(lazy[2*k+2], lazy[k]);
			}
		}
		lazy[k]=e0;
	}

	void update(int a, int b, const OperatorMonoid &x, int k, int l, int r){
		eval(k, l, r);
		if(r<=a || b<=l) return;
		if(a<=l && r<=b){
			lazy[k]=h(lazy[k], x);
			eval(k, l, r);
		}else{
			update(a, b, x, 2*k+1, l, (l+r)/2);
			update(a, b, x, 2*k+2, (l+r)/2, r);
			data[k]=f(data[2*k+1], data[2*k+2]);
		}
	}
	void update(int a, int b, const OperatorMonoid &x){
		return update(a, b, x, 0, 0, sz);
	}

	Monoid find(int a, int b, int k, int l, int r){
		eval(k, l, r);
		if(b<=l || r<=a) return e1;
		if(a<=l && r<=b) return data[k];
		else return f(find(a, b, 2*k+1, l, (l+r)/2), find(a, b, 2*k+2, (l+r)/2, r));
	}
	Monoid find(int a, int b){
		return find(a, b, 0, 0, sz);
	}
	Monoid operator[](const int &k){
		return find(k, k+1);
	}
};
int n;
int a[200020];
int main()
{
    cin>>n;
    vector<P> v(n);
    for(int i=0; i<n; i++){
        cin>>a[i];
        v[i]=P(a[i], i);
    }
    sort(v.begin(), v.end());
    BIT<int> bit(n);
    using Pm=pair<mint, mint>;
    auto f=[&](mint a, mint b){
        return a+b;
    };
    auto g=[&](mint a, Pm x, int len){
        return a*x.first+x.second*len;
    };
    auto h=[&](Pm x, Pm y){
        return Pm(x.first*y.first, y.first*x.second+y.second);
    };
    LazySegmentTree<mint, Pm> seg1(n, f, g, h, mint(0), Pm(1, 0));
    LazySegmentTree<mint, Pm> seg2(n, f, g, h, mint(0), Pm(1, 0));
    mint ans=0;
    mint inv2=mint(2).inv();
    mint p2[200020], p2i[200020];
    p2[0]=p2i[0]=1;
    for(int i=1; i<=n; i++){
        p2[i]=p2[i-1]*2;
        p2i[i]=p2i[i-1]*inv2;
    }
    for(int z=0; z<n; z++){
        int m=v[z].second;
        bit.add(m, 1);
        int c=bit.sum(m);
        seg2.update(m+1, n, Pm(2, 0));
        seg2.update(m, m+1, Pm(0, (n-m)*p2[c]));
        seg1.update(m+1, n, Pm(inv2, 0));
        seg1.update(m, m+1, Pm(0, (m+1)*p2i[c]));
        ans+=(n-m+seg2.find(m+1, n)*p2i[c]*inv2)*(m+1+seg1.find(0, m)*p2[c]*inv2)*mint(a[m]);
    }
    cout<<ans.val()<<endl;
    return 0;
}
0