I solved a problem, URL: https://www.codechef.com/problems/IEMCO8E/
While running on Codechef IDE with custom inputs given this code runs fine with status ‘SUCCESSFULY EXECUTED’, but after submission this gives a ‘Runtime Error NZEC’!
Why so?
What modifications in the code are required?
Code given here:
/* package codechef; // don't place package name! */
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
{
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();
}
}
static class Pair<S extends Comparable<S>, T extends Comparable<T>> implements Comparable<Pair<S, T>> {
S first;
T second;
Pair(S f, T s) {
first = f;
second = s;
}
@Override
public int compareTo(Pair<S, T> o) {
int t = first.compareTo(o.first);
if (t == 0) return second.compareTo(o.second);
return t;
}
@Override
public int hashCode() {
return (31 + first.hashCode()) * 31 + second.hashCode();
}
@Override
public boolean equals(Object o) {
if (!(o instanceof Pair)) return false;
if (o == this) return true;
Pair p = (Pair) o;
return first.equals(p.first) && second.equals(p.second);
}
@Override
public String toString() {
return "Pair{" + first + ", " + second + "}";
}
}
public static void main (String[] args) throws java.lang.Exception
{
Reader rd = new Reader() ;
int n = rd.nextInt() ;
Pair<Long,Long> p[] = new Pair[2*n] ;
String[] lr, xy;
for(int i=0;i<n;++i){
lr = rd.readLine().split(" ") ;
p[2*i] = new Pair<>(Long.parseLong(lr[0]),(long)1) ;
p[2*i+1] = new Pair<>(Long.parseLong(lr[1])+1,(long)-1) ;
}
xy = rd.readLine().split(" ") ;
int x=Integer.parseInt(xy[0]),y=Integer.parseInt(xy[1]) ;
Arrays.sort(p);
long c=0,cnt=0,prev=-1, ans=0;
for(int i=0;i<2*n;++i){
c=p[i].first ;
if(cnt>=x && cnt<=y) ans+=(c-prev) ;
cnt += p[i].second ;
prev = c;
}
System.out.println(ans);
}//main end
}//class end
Kindly help me find out the reason for the same.
Thanks in Advance.