I optimized as much as I could, yet failed to get AC. I am getting TLE each time for test-case(4).
Just wondering if anyone got AC for D-Query using java ?
Please help if there is something wrong with my code..
Below is my code:
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Codechef obj = new Codechef();
obj.solve();
}
public void solve() throws java.lang.Exception
{
Reader sc = new Reader( );
int t = 1; //sc.nextInt();
int q,n;
List<Data> myData = new ArrayList<>();
Data obj;
while(t-->0)
{
myData.clear();
n = sc.nextInt();
for(int i = 0; i<n;i++)
{
obj = new Data();
obj.key = i+1;
obj.val = sc.nextInt();
obj.ansKey = -1;
myData.add(obj);
}
q = sc.nextInt();
for(int i = 0; i<q; i++)
{
obj = new Data();
obj.r1 = sc.nextInt();
obj.r2 = sc.nextInt();
obj.key = obj.r2;
obj.ansKey = i+1;
myData.add(obj);
}
Collections.sort(myData,new sortByEndRangeValue());
int bitTree[] = new int[n+1];
int history[] = new int[1000001];
List<Data> ansList = new ArrayList<>();
for(Data objData : myData)
{
if(objData.val>0)
{
if(history[objData.val]>0)
updateIndex(bitTree,history[objData.val],-1);
history[objData.val] = objData.key;
updateIndex(bitTree,history[objData.val],1);
// System.out.println("Key=" + objData.key + getSum(bitTree,objData.key));
}
else
{
objData.ans = getSum(bitTree,objData.r2) - getSum(bitTree,objData.r1-1);
ansList.add(objData);
}
}
Collections.sort(ansList, new sortByQueryIndex());
OutputStream out = new BufferedOutputStream( System.out );
for(Data objData : ansList)
{
if(objData.val==0)
{
out.write((objData.ans + "\n").getBytes());
// System.out.println();
}
}
out.flush();
}
}
public static int getSum(int BITree[], int index)
{
int sum = 0;
while(index>0)
{
sum += BITree[index];
index -= index & -index;
}
return sum;
}
public static void updateIndex(int BITree[], int index, int val)
{
int len = BITree.length;
while(index<len)
{
BITree[index] += val;
index += index & (-index);
}
}
class sortByEndRangeValue implements Comparator<Data>
{
@Override
public int compare(Data o1, Data o2) {
if(o1.key == o2.key)
{
if(o1.val>0)
return -1;
if(o2.val>0)
return 1;
return -1;
}
if(o1.key<o2.key)
return -1;
return 1;
}
}
class sortByQueryIndex implements Comparator<Data>
{
@Override
public int compare(Data o1, Data o2) {
if(o1.ansKey<o2.ansKey)
return -1;
return 1;
}
}
class Data
{
int key;
int ansKey;
int r1, r2;
int val;
int ans;
}
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 Reader(String file_name) throws IOException
{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
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 void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
}