Skip to content

Commit 5e5d749

Browse files
authored
Merge pull request #397 from AlgorithmWithGod/khj20006
[20250629] BOJ / P3 / 시그마 시그마 시그마 시그마 / 권혁준
2 parents 15697be + d9c90ce commit 5e5d749

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
class IOController {
6+
BufferedReader br;
7+
BufferedWriter bw;
8+
StringTokenizer st;
9+
10+
public IOController() {
11+
br = new BufferedReader(new InputStreamReader(System.in));
12+
bw = new BufferedWriter(new OutputStreamWriter(System.out));
13+
st = new StringTokenizer("");
14+
}
15+
16+
String nextLine() throws Exception {
17+
String line = br.readLine();
18+
st = new StringTokenizer(line);
19+
return line;
20+
}
21+
22+
String nextToken() throws Exception {
23+
while (!st.hasMoreTokens()) nextLine();
24+
return st.nextToken();
25+
}
26+
27+
int nextInt() throws Exception {
28+
return Integer.parseInt(nextToken());
29+
}
30+
31+
long nextLong() throws Exception {
32+
return Long.parseLong(nextToken());
33+
}
34+
35+
double nextDouble() throws Exception {
36+
return Double.parseDouble(nextToken());
37+
}
38+
39+
void close() throws Exception {
40+
bw.flush();
41+
bw.close();
42+
}
43+
44+
void write(String content) throws Exception {
45+
bw.write(content);
46+
}
47+
48+
}
49+
50+
class Segtree {
51+
int size;
52+
long[] seg;
53+
Segtree(int size) {
54+
this.size = size;
55+
seg = new long[this.size*4];
56+
}
57+
58+
void update(int s, int e, int i, int v, int n) {
59+
if(s == e) {
60+
seg[n] = v;
61+
return;
62+
}
63+
int m = (s+e)>>1;
64+
if(i <= m) update(s,m,i,v,n*2);
65+
else update(m+1,e,i,v,n*2+1);
66+
seg[n] = seg[n*2] + seg[n*2+1];
67+
}
68+
69+
long find(int s, int e, int l, int r, int n) {
70+
if(l>r || l>e || r<s) return 0;
71+
if(l<=s && e<=r) return seg[n];
72+
int m = (s+e)>>1;
73+
return find(s,m,l,r,n*2) + find(m+1,e,l,r,n*2+1);
74+
}
75+
76+
}
77+
78+
public class Main {
79+
80+
static IOController io;
81+
82+
//
83+
84+
static final long MOD = 998244353;
85+
86+
static int N;
87+
static int[][] infos;
88+
static Segtree left, right;
89+
90+
public static void main(String[] args) throws Exception {
91+
92+
io = new IOController();
93+
94+
init();
95+
solve();
96+
97+
io.close();
98+
99+
}
100+
101+
public static void init() throws Exception {
102+
103+
N = io.nextInt();
104+
infos = new int[N][];
105+
for(int i=1;i<=N;i++){
106+
int a = io.nextInt();
107+
infos[i-1] = new int[]{a,i};
108+
}
109+
left = new Segtree(N);
110+
right = new Segtree(N);
111+
112+
}
113+
114+
static void solve() throws Exception {
115+
116+
Arrays.sort(infos, (a,b) -> a[0]==b[0] ? b[1]-a[1] : a[0]-b[0]);
117+
118+
long ans = 0;
119+
for(int[] info : infos) {
120+
int val = info[0], idx = info[1];
121+
long res1 = left.find(1,N,1,idx-1,1) % MOD * (N-idx+1) % MOD;
122+
long res2 = right.find(1,N,idx+1,N,1) % MOD * idx % MOD;
123+
ans = (ans + (res1 + res2) * val) % MOD;
124+
left.update(1,N,idx,idx,1);
125+
right.update(1,N,idx,N-idx+1,1);
126+
}
127+
io.write(ans + "\n");
128+
129+
}
130+
131+
}
132+
```

0 commit comments

Comments
 (0)