Skip to content

Commit b516c3b

Browse files
authored
Merge pull request #515 from dwivediprashant/code/day-9-q-2
code(java) : solved que 2 of day 9 on codeforces
2 parents 6b6340c + 1e5f57c commit b516c3b

1 file changed

Lines changed: 138 additions & 0 deletions

File tree

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
// 1. Problem Statement -------------------------------------------------
2+
/*
3+
Given an array of n distinct integers.
4+
Define the weakness as the number of triplets (i, j, k) such that:
5+
i < j < k and a[i] > a[j] > a[k].
6+
7+
You need to count all such decreasing triplets.
8+
Constraints are large, so brute force is not allowed.
9+
*/
10+
11+
// 2. Approach -----------------------------------------------------------
12+
/*
13+
Fix j as the middle element.
14+
For each j:
15+
- Count how many elements greater than a[j] exist on its left
16+
- Count how many elements smaller than a[j] exist on its right
17+
18+
Each combination forms a valid triplet.
19+
Fenwick Tree (Binary Indexed Tree) is used to compute counts efficiently.
20+
Coordinate compression is applied because values are large.
21+
*/
22+
23+
// 3. Complexity ---------------------------------------------------------
24+
/*
25+
Time Complexity: O(n log n)
26+
Space Complexity: O(n)
27+
*/
28+
29+
// 4. Problem Submission Link --------------------------------------------
30+
/*
31+
https://codeforces.com/contest/61/submission/356384283
32+
*/
33+
34+
import java.io.*;
35+
import java.util.*;
36+
37+
public class Solution2 {
38+
39+
static class Fenwick {
40+
int n;
41+
long[] bit;
42+
43+
Fenwick(int n) {
44+
this.n = n;
45+
bit = new long[n + 1];
46+
}
47+
48+
void update(int i, long v) {
49+
while (i <= n) {
50+
bit[i] += v;
51+
i += i & -i;
52+
}
53+
}
54+
55+
long query(int i) {
56+
long s = 0;
57+
while (i > 0) {
58+
s += bit[i];
59+
i -= i & -i;
60+
}
61+
return s;
62+
}
63+
64+
long range(int l, int r) {
65+
return query(r) - query(l - 1);
66+
}
67+
}
68+
69+
public static void main(String[] args) throws Exception {
70+
FastScanner fs = new FastScanner(System.in);
71+
72+
int n = fs.nextInt();
73+
int[] a = new int[n];
74+
for (int i = 0; i < n; i++) a[i] = fs.nextInt();
75+
76+
int[] b = a.clone();
77+
Arrays.sort(b);
78+
Map<Integer, Integer> map = new HashMap<>(n);
79+
int id = 1;
80+
for (int x : b) if (!map.containsKey(x)) map.put(x, id++);
81+
82+
int[] c = new int[n];
83+
for (int i = 0; i < n; i++) c[i] = map.get(a[i]);
84+
85+
long[] L = new long[n];
86+
long[] R = new long[n];
87+
88+
Fenwick fw = new Fenwick(n);
89+
for (int i = 0; i < n; i++) {
90+
L[i] = fw.range(c[i] + 1, n);
91+
fw.update(c[i], 1);
92+
}
93+
94+
fw = new Fenwick(n);
95+
for (int i = n - 1; i >= 0; i--) {
96+
R[i] = fw.query(c[i] - 1);
97+
fw.update(c[i], 1);
98+
}
99+
100+
long ans = 0;
101+
for (int i = 0; i < n; i++) ans += L[i] * R[i];
102+
103+
System.out.println(ans);
104+
}
105+
106+
static class FastScanner {
107+
private final byte[] buf = new byte[1 << 16];
108+
private int idx = 0, size = 0;
109+
private final InputStream in;
110+
111+
FastScanner(InputStream in) {
112+
this.in = in;
113+
}
114+
115+
int read() throws IOException {
116+
if (idx >= size) {
117+
size = in.read(buf);
118+
idx = 0;
119+
if (size <= 0) return -1;
120+
}
121+
return buf[idx++];
122+
}
123+
124+
int nextInt() throws IOException {
125+
int c, s = 1, x = 0;
126+
do c = read(); while (c <= ' ');
127+
if (c == '-') {
128+
s = -1;
129+
c = read();
130+
}
131+
while (c > ' ') {
132+
x = x * 10 + (c - '0');
133+
c = read();
134+
}
135+
return x * s;
136+
}
137+
}
138+
}

0 commit comments

Comments
 (0)