-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLG6834.cpp
93 lines (83 loc) · 2 KB
/
LG6834.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include<bits/stdc++.h>
using namespace std;
#define int long long
template < typename Tp >
void read(Tp &x) {
x = 0; int fh = 1; char ch = 1;
while(ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if(ch == '-') fh = -1, ch = getchar();
while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
x *= fh;
}
const int mod = 998244353;
const int maxn = 1000000 + 7;
int n, k;
int a[maxn], b[maxn];
int s[maxn];
int lowbit(int x) {
return x & (-x);
}
struct BIT {
int c[maxn], N;
void build(int x) {N = x;}
void add(int pos, int k) {
for(int i = pos; i <= N; i += lowbit(i)) c[i] = ((c[i] + k) % mod + mod) % mod;
}
int query(int x) {
int res = 0;
while(x) {
res = (res + c[x]) % mod;
x -= lowbit(x);
}
return res;
}
}T, T1;
void Init(void) {
read(n); read(k);
for(int i = 1; i <= n; i++) {
read(a[i]);
s[i] = s[i - 1] + a[i];
}
}
int fpow(int x, int p){
int res = 1;
while(p) {
if(p & 1) res = res * x % mod;
x = x * x % mod; p >>= 1;
}
return res;
}
void Work(void) {
int ans = 0;
for(int i = 1; i <= n; i++) b[i] = a[i];
sort(b + 1, b + n + 1);
int M; M = unique(b + 1, b + n + 1) - b - 1;
T.build(M), T1.build(M);
for(int i = 1; i <= n; i++) a[i] = lower_bound(b + 1, b + M + 1, a[i]) - b;
for(int i = 1; i <= n; i++) {
T.add(a[i], b[a[i]]); T1.add(a[i], 1);
// printf("%lld : IN\n", i);
int st;
if(i - k - 1 >= 1) {
T.add(a[i - k - 1], -b[a[i - k - 1]]);
T1.add(a[i - k - 1], -1);
// printf("%lld : OUT\n", i-k-1);
}
st=max(1ll, i - k);
int p = i - st;
int sum = T.query(a[i]), sour = T1.query(a[i]);
sum = ((-sum + sour * b[a[i]] % mod) % mod + mod) % mod;
sum = sum * fpow(p, mod - 2) % mod;
ans = (ans + sum) % mod;
// for(int j = st; j < i; j++) {
// ans = (ans + (max(0ll, a[i] - a[j]) * fpow(p, mod - 2) % mod) % mod) % mod;
// }
}
ans = (ans + b[a[1]]) % mod;
printf("%lld\n", ans);
}
signed main(void) {
Init();
Work();
return 0;
}