import java.io.DataInputStream;
import java.io.IOException;
import java.util.*;
import java.io.*;
class Fenwick{
private int data_[];
public int query(int index){
int ret=0;
for(;index>0;index-=(index&-index)){
ret+=data_[index];
}
return ret;
}
public void update(int index){
for(++index;index<data_.length;index+=(index&-index)){
++data_[index];
}
}
public Fenwick(int n){
data_=new int[n+1];
}
}
class Element{
long height;
int index;
}
public class Mountains {
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1) {
buffer[0] = -1;
}
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead) {
fillBuffer();
}
return buffer[bufferPointer++];
}
}
public static void main(String[] args) throws IOException {
Reader reader = new Reader();
int n = reader.nextInt();
Fenwick fw=new Fenwick(n);
Element[] arr = new Element[n];
for(int i=0;i<n;++i){
Element el = arr[i] = new Element();
el.height = reader.nextLong();
el.index = i;
}
Arrays.sort(arr, (x,y) -> Long.compare(x.height,y.height));
long ans=0;
for(int i=0;i<n;){
final long val=arr[i].height;
int j;
for(j=i;j<n&&arr[j].height==val;++j){
final int idx=arr[j].index;
final int tmp=fw.query(idx);
ans+=tmp*(long)(i-tmp);
}
for(;i<j;++i){
final int idx=arr[i].index;
fw.update(idx);
}
}
System.out.println(ans);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
203 ms |
16232 KB |
Output is correct |
2 |
Correct |
334 ms |
25956 KB |
Output is correct |
3 |
Correct |
320 ms |
25144 KB |
Output is correct |
4 |
Correct |
333 ms |
25736 KB |
Output is correct |
5 |
Correct |
328 ms |
25888 KB |
Output is correct |
6 |
Correct |
310 ms |
25040 KB |
Output is correct |
7 |
Correct |
316 ms |
25888 KB |
Output is correct |
8 |
Correct |
348 ms |
25412 KB |
Output is correct |
9 |
Correct |
328 ms |
25732 KB |
Output is correct |
10 |
Correct |
307 ms |
25336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
712 ms |
30156 KB |
Output is correct |
2 |
Correct |
474 ms |
29444 KB |
Output is correct |
3 |
Correct |
695 ms |
30432 KB |
Output is correct |
4 |
Correct |
693 ms |
30200 KB |
Output is correct |
5 |
Correct |
809 ms |
30168 KB |
Output is correct |
6 |
Correct |
753 ms |
30356 KB |
Output is correct |
7 |
Correct |
715 ms |
30100 KB |
Output is correct |
8 |
Correct |
304 ms |
25916 KB |
Output is correct |
9 |
Correct |
311 ms |
25572 KB |
Output is correct |
10 |
Correct |
197 ms |
15924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
712 ms |
30156 KB |
Output is correct |
2 |
Correct |
474 ms |
29444 KB |
Output is correct |
3 |
Correct |
695 ms |
30432 KB |
Output is correct |
4 |
Correct |
693 ms |
30200 KB |
Output is correct |
5 |
Correct |
809 ms |
30168 KB |
Output is correct |
6 |
Correct |
753 ms |
30356 KB |
Output is correct |
7 |
Correct |
715 ms |
30100 KB |
Output is correct |
8 |
Correct |
304 ms |
25916 KB |
Output is correct |
9 |
Correct |
311 ms |
25572 KB |
Output is correct |
10 |
Correct |
197 ms |
15924 KB |
Output is correct |
11 |
Correct |
868 ms |
31372 KB |
Output is correct |
12 |
Correct |
1100 ms |
31808 KB |
Output is correct |
13 |
Correct |
1257 ms |
33168 KB |
Output is correct |
14 |
Correct |
850 ms |
31560 KB |
Output is correct |
15 |
Correct |
959 ms |
32156 KB |
Output is correct |
16 |
Correct |
1070 ms |
32212 KB |
Output is correct |
17 |
Correct |
959 ms |
32392 KB |
Output is correct |
18 |
Correct |
293 ms |
25552 KB |
Output is correct |
19 |
Correct |
295 ms |
25636 KB |
Output is correct |
20 |
Correct |
216 ms |
15884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
223 ms |
15708 KB |
Output is correct |
2 |
Correct |
214 ms |
16028 KB |
Output is correct |
3 |
Correct |
207 ms |
16108 KB |
Output is correct |
4 |
Correct |
227 ms |
15848 KB |
Output is correct |
5 |
Correct |
220 ms |
16244 KB |
Output is correct |
6 |
Correct |
223 ms |
16028 KB |
Output is correct |
7 |
Correct |
204 ms |
15756 KB |
Output is correct |
8 |
Correct |
216 ms |
16200 KB |
Output is correct |
9 |
Correct |
200 ms |
16264 KB |
Output is correct |
10 |
Correct |
203 ms |
16104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
223 ms |
15708 KB |
Output is correct |
2 |
Correct |
214 ms |
16028 KB |
Output is correct |
3 |
Correct |
207 ms |
16108 KB |
Output is correct |
4 |
Correct |
227 ms |
15848 KB |
Output is correct |
5 |
Correct |
220 ms |
16244 KB |
Output is correct |
6 |
Correct |
223 ms |
16028 KB |
Output is correct |
7 |
Correct |
204 ms |
15756 KB |
Output is correct |
8 |
Correct |
216 ms |
16200 KB |
Output is correct |
9 |
Correct |
200 ms |
16264 KB |
Output is correct |
10 |
Correct |
203 ms |
16104 KB |
Output is correct |
11 |
Correct |
235 ms |
16484 KB |
Output is correct |
12 |
Correct |
260 ms |
16584 KB |
Output is correct |
13 |
Correct |
253 ms |
16536 KB |
Output is correct |
14 |
Correct |
240 ms |
16500 KB |
Output is correct |
15 |
Correct |
256 ms |
16528 KB |
Output is correct |
16 |
Correct |
244 ms |
16136 KB |
Output is correct |
17 |
Correct |
269 ms |
16656 KB |
Output is correct |
18 |
Correct |
234 ms |
16532 KB |
Output is correct |
19 |
Correct |
259 ms |
16584 KB |
Output is correct |
20 |
Correct |
210 ms |
16144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
712 ms |
30156 KB |
Output is correct |
2 |
Correct |
474 ms |
29444 KB |
Output is correct |
3 |
Correct |
695 ms |
30432 KB |
Output is correct |
4 |
Correct |
693 ms |
30200 KB |
Output is correct |
5 |
Correct |
809 ms |
30168 KB |
Output is correct |
6 |
Correct |
753 ms |
30356 KB |
Output is correct |
7 |
Correct |
715 ms |
30100 KB |
Output is correct |
8 |
Correct |
304 ms |
25916 KB |
Output is correct |
9 |
Correct |
311 ms |
25572 KB |
Output is correct |
10 |
Correct |
197 ms |
15924 KB |
Output is correct |
11 |
Correct |
868 ms |
31372 KB |
Output is correct |
12 |
Correct |
1100 ms |
31808 KB |
Output is correct |
13 |
Correct |
1257 ms |
33168 KB |
Output is correct |
14 |
Correct |
850 ms |
31560 KB |
Output is correct |
15 |
Correct |
959 ms |
32156 KB |
Output is correct |
16 |
Correct |
1070 ms |
32212 KB |
Output is correct |
17 |
Correct |
959 ms |
32392 KB |
Output is correct |
18 |
Correct |
293 ms |
25552 KB |
Output is correct |
19 |
Correct |
295 ms |
25636 KB |
Output is correct |
20 |
Correct |
216 ms |
15884 KB |
Output is correct |
21 |
Correct |
1514 ms |
31944 KB |
Output is correct |
22 |
Correct |
1433 ms |
32988 KB |
Output is correct |
23 |
Correct |
1598 ms |
33180 KB |
Output is correct |
24 |
Correct |
1344 ms |
31540 KB |
Output is correct |
25 |
Correct |
1441 ms |
32416 KB |
Output is correct |
26 |
Correct |
1487 ms |
32880 KB |
Output is correct |
27 |
Correct |
1282 ms |
32216 KB |
Output is correct |
28 |
Correct |
315 ms |
25708 KB |
Output is correct |
29 |
Correct |
305 ms |
25684 KB |
Output is correct |
30 |
Correct |
205 ms |
15888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
203 ms |
16232 KB |
Output is correct |
2 |
Correct |
334 ms |
25956 KB |
Output is correct |
3 |
Correct |
320 ms |
25144 KB |
Output is correct |
4 |
Correct |
333 ms |
25736 KB |
Output is correct |
5 |
Correct |
328 ms |
25888 KB |
Output is correct |
6 |
Correct |
310 ms |
25040 KB |
Output is correct |
7 |
Correct |
316 ms |
25888 KB |
Output is correct |
8 |
Correct |
348 ms |
25412 KB |
Output is correct |
9 |
Correct |
328 ms |
25732 KB |
Output is correct |
10 |
Correct |
307 ms |
25336 KB |
Output is correct |
11 |
Correct |
712 ms |
30156 KB |
Output is correct |
12 |
Correct |
474 ms |
29444 KB |
Output is correct |
13 |
Correct |
695 ms |
30432 KB |
Output is correct |
14 |
Correct |
693 ms |
30200 KB |
Output is correct |
15 |
Correct |
809 ms |
30168 KB |
Output is correct |
16 |
Correct |
753 ms |
30356 KB |
Output is correct |
17 |
Correct |
715 ms |
30100 KB |
Output is correct |
18 |
Correct |
304 ms |
25916 KB |
Output is correct |
19 |
Correct |
311 ms |
25572 KB |
Output is correct |
20 |
Correct |
197 ms |
15924 KB |
Output is correct |
21 |
Correct |
868 ms |
31372 KB |
Output is correct |
22 |
Correct |
1100 ms |
31808 KB |
Output is correct |
23 |
Correct |
1257 ms |
33168 KB |
Output is correct |
24 |
Correct |
850 ms |
31560 KB |
Output is correct |
25 |
Correct |
959 ms |
32156 KB |
Output is correct |
26 |
Correct |
1070 ms |
32212 KB |
Output is correct |
27 |
Correct |
959 ms |
32392 KB |
Output is correct |
28 |
Correct |
293 ms |
25552 KB |
Output is correct |
29 |
Correct |
295 ms |
25636 KB |
Output is correct |
30 |
Correct |
216 ms |
15884 KB |
Output is correct |
31 |
Correct |
223 ms |
15708 KB |
Output is correct |
32 |
Correct |
214 ms |
16028 KB |
Output is correct |
33 |
Correct |
207 ms |
16108 KB |
Output is correct |
34 |
Correct |
227 ms |
15848 KB |
Output is correct |
35 |
Correct |
220 ms |
16244 KB |
Output is correct |
36 |
Correct |
223 ms |
16028 KB |
Output is correct |
37 |
Correct |
204 ms |
15756 KB |
Output is correct |
38 |
Correct |
216 ms |
16200 KB |
Output is correct |
39 |
Correct |
200 ms |
16264 KB |
Output is correct |
40 |
Correct |
203 ms |
16104 KB |
Output is correct |
41 |
Correct |
235 ms |
16484 KB |
Output is correct |
42 |
Correct |
260 ms |
16584 KB |
Output is correct |
43 |
Correct |
253 ms |
16536 KB |
Output is correct |
44 |
Correct |
240 ms |
16500 KB |
Output is correct |
45 |
Correct |
256 ms |
16528 KB |
Output is correct |
46 |
Correct |
244 ms |
16136 KB |
Output is correct |
47 |
Correct |
269 ms |
16656 KB |
Output is correct |
48 |
Correct |
234 ms |
16532 KB |
Output is correct |
49 |
Correct |
259 ms |
16584 KB |
Output is correct |
50 |
Correct |
210 ms |
16144 KB |
Output is correct |
51 |
Correct |
1514 ms |
31944 KB |
Output is correct |
52 |
Correct |
1433 ms |
32988 KB |
Output is correct |
53 |
Correct |
1598 ms |
33180 KB |
Output is correct |
54 |
Correct |
1344 ms |
31540 KB |
Output is correct |
55 |
Correct |
1441 ms |
32416 KB |
Output is correct |
56 |
Correct |
1487 ms |
32880 KB |
Output is correct |
57 |
Correct |
1282 ms |
32216 KB |
Output is correct |
58 |
Correct |
315 ms |
25708 KB |
Output is correct |
59 |
Correct |
305 ms |
25684 KB |
Output is correct |
60 |
Correct |
205 ms |
15888 KB |
Output is correct |
61 |
Correct |
1289 ms |
30300 KB |
Output is correct |
62 |
Correct |
1344 ms |
31552 KB |
Output is correct |
63 |
Correct |
1595 ms |
33536 KB |
Output is correct |
64 |
Correct |
1569 ms |
33492 KB |
Output is correct |
65 |
Correct |
1432 ms |
33140 KB |
Output is correct |
66 |
Correct |
1394 ms |
32328 KB |
Output is correct |
67 |
Correct |
1523 ms |
33308 KB |
Output is correct |
68 |
Correct |
1461 ms |
32972 KB |
Output is correct |
69 |
Correct |
1539 ms |
32372 KB |
Output is correct |
70 |
Correct |
209 ms |
15980 KB |
Output is correct |