Skip to content

Commit 1b6332b

Browse files
Create bit.java
1 parent e2612b2 commit 1b6332b

File tree

1 file changed

+249
-0
lines changed

1 file changed

+249
-0
lines changed

Data Structures/Fenwick_tree/bit.java

Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
import java.util.*;
2+
import java.awt.List;
3+
import java.io.*;
4+
import java.lang.*;
5+
import java.lang.reflect.Array;
6+
7+
public class code8 {
8+
9+
10+
11+
public static void main(String[] args) {
12+
InputReader in = new InputReader(System.in);
13+
PrintWriter pw = new PrintWriter(System.out);
14+
15+
// Code starts..
16+
17+
int n = in.nextInt();
18+
int[] a = in.nextIntArray(n);
19+
query[] qr1 = new query[n];
20+
pair[] p = new pair[n];
21+
q = n;
22+
HashMap<Integer, Integer> map = new HashMap<>();
23+
for(int i=0; i<n; i++)
24+
map.put(a[i],i);
25+
Arrays.sort(a);
26+
for(int i=n-1; i>=0; i--)
27+
{
28+
p[n-i-1] = new pair(-a[i], map.get(a[i]));
29+
int l = 0;
30+
int r = map.get(a[i])-1;
31+
int x = a[i];
32+
qr1[n-i-1] = new query(l, r, -a[i], map.get(a[i]));
33+
// pw.println(qr1[n-i-1].l+" "+qr1[n-i-1].x+" "+p[n-i-1]);
34+
35+
}
36+
37+
int[] ans = answerQueries(n, qr1, q, p);
38+
39+
for(int i=0; i<n; i++)
40+
{
41+
int l = map.get(a[i])+1;
42+
int r = n-1;
43+
int x = a[i];
44+
qr1[i].l = l;
45+
qr1[i].r = r;
46+
qr1[i].x = x;
47+
qr1[i].idx = map.get(a[i]);
48+
49+
p[i].x = x;
50+
p[i].y = map.get(a[i]);
51+
// pw.println(qr1[i].l+" "+qr1[i].x+" "+p[i]+" ");
52+
53+
}
54+
55+
56+
57+
int[] ans2 = answerQueries(n, qr1, q, p);
58+
long sum = 0;
59+
for(int i=0; i<q; i++)
60+
{
61+
sum += (long)ans[i]*ans2[i];
62+
//pw.println(ans[i]+" "+ans2[i]);
63+
}
64+
65+
66+
pw.print(sum);
67+
// code ends...
68+
69+
pw.flush();
70+
pw.close();
71+
}
72+
73+
74+
75+
public static void update(int bit[], int idx, int val, int n) {
76+
77+
78+
for(; idx<=n; idx+= (idx&-idx))
79+
bit[idx] += val;
80+
}
81+
82+
83+
84+
public static int query(int bit[], int idx, int n) {
85+
int sum = 0;
86+
for (; idx > 0; idx -= idx&-idx)
87+
sum += bit[idx];
88+
return sum;
89+
}
90+
91+
92+
93+
public static int[] answerQueries(int n, query[] qr, int q,
94+
pair[] p)
95+
96+
{
97+
98+
int[] bit = new int[n+1];
99+
/*p = suffle(p, new Random());
100+
qr = suffle(qr, new Random());
101+
102+
//Arrays.sort(p);
103+
Arrays.sort(qr);
104+
*/
105+
int curr = 0;
106+
int[] ans = new int[q];
107+
for (int i=0; i<q; i++)
108+
{
109+
//System.out.println(p[curr]+" "+i+" "+curr);
110+
if(curr<n)
111+
while (p[curr].x <= qr[i].x && curr<n)
112+
{
113+
update(bit, p[curr].y+1, 1, n);
114+
curr++;
115+
if(curr==n)
116+
break;
117+
}
118+
119+
ans[qr[i].idx] = query(bit, qr[i].r+1, n) -
120+
query(bit, qr[i].l, n);
121+
}
122+
123+
return ans;
124+
125+
126+
}
127+
128+
129+
130+
131+
132+
static class InputReader
133+
{
134+
135+
private final InputStream stream;
136+
private final byte[] buf = new byte[8192];
137+
private int curChar, snumChars;
138+
private SpaceCharFilter filter;
139+
140+
public InputReader(InputStream stream) {
141+
this.stream = stream;
142+
}
143+
144+
public int snext() {
145+
if (snumChars == -1)
146+
throw new InputMismatchException();
147+
if (curChar >= snumChars) {
148+
curChar = 0;
149+
try {
150+
snumChars = stream.read(buf);
151+
} catch (IOException e) {
152+
throw new InputMismatchException();
153+
}
154+
if (snumChars <= 0)
155+
return -1;
156+
}
157+
return buf[curChar++];
158+
}
159+
160+
public int nextInt() {
161+
int c = snext();
162+
while (isSpaceChar(c)) {
163+
c = snext();
164+
}
165+
int sgn = 1;
166+
if (c == '-') {
167+
sgn = -1;
168+
c = snext();
169+
}
170+
int res = 0;
171+
do {
172+
if (c < '0' || c > '9')
173+
throw new InputMismatchException();
174+
res *= 10;
175+
res += c - '0';
176+
c = snext();
177+
} while (!isSpaceChar(c));
178+
return res * sgn;
179+
}
180+
181+
public long nextLong() {
182+
int c = snext();
183+
while (isSpaceChar(c)) {
184+
c = snext();
185+
}
186+
int sgn = 1;
187+
if (c == '-') {
188+
sgn = -1;
189+
c = snext();
190+
}
191+
long res = 0;
192+
do {
193+
if (c < '0' || c > '9')
194+
throw new InputMismatchException();
195+
res *= 10;
196+
res += c - '0';
197+
c = snext();
198+
} while (!isSpaceChar(c));
199+
return res * sgn;
200+
}
201+
202+
public int[] nextIntArray(int n) {
203+
int a[] = new int[n];
204+
for (int i = 0; i < n; i++) {
205+
a[i] = nextInt();
206+
}
207+
return a;
208+
}
209+
210+
public String readString() {
211+
int c = snext();
212+
while (isSpaceChar(c)) {
213+
c = snext();
214+
}
215+
StringBuilder res = new StringBuilder();
216+
do {
217+
res.appendCodePoint(c);
218+
c = snext();
219+
} while (!isSpaceChar(c));
220+
return res.toString();
221+
}
222+
223+
public String nextLine() {
224+
int c = snext();
225+
while (isSpaceChar(c))
226+
c = snext();
227+
StringBuilder res = new StringBuilder();
228+
do {
229+
res.appendCodePoint(c);
230+
c = snext();
231+
} while (!isEndOfLine(c));
232+
return res.toString();
233+
}
234+
235+
public boolean isSpaceChar(int c) {
236+
if (filter != null)
237+
return filter.isSpaceChar(c);
238+
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
239+
}
240+
241+
private boolean isEndOfLine(int c) {
242+
return c == '\n' || c == '\r' || c == -1;
243+
}
244+
245+
public interface SpaceCharFilter {
246+
public boolean isSpaceChar(int ch);
247+
}
248+
}
249+
}

0 commit comments

Comments
 (0)