Getting Time limit exceeded for array updation problem which takes a lot of time to even just read input
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
0
down vote
favorite
For this problem - https://www.hackerearth.com/practice/data-structures/advanced-data-structures/segment-trees/practice-problems/algorithm/shivam-and-expensive-birthday-gift-da58b2f0/description/, I tried different ways to solve it, but just reading the input without even performing any opreations takes a lot of time. Even using my custom Reader class doesn't help much. Can someone please check if my logic is right?
public class Solutions
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;
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();
private static Reader scanner = new Reader();
static int set1s;
static void choice1 (int a, int x) 1;
set1s[x]++;
static void choice2 (int a, int x)
if ((a[x] & 1) == 1)
set1s[x]--;
a[x] >>= 1;
static int choice3 (int a, int x, int y)
int result = 0;
for (int i = x; i<=y; i++)
result += set1s[i];
return result;
public static void main(String args) throws Exception
int n = scanner.nextInt(), q = scanner.nextInt();
int a = new int[n+1];
set1s = new int[n+1];
while (q-- > 0)
int choice = scanner.nextInt(), x = scanner.nextInt();
switch (choice)
case 1:
choice1(a, x);
break;
case 2:
choice2 (a, x);
break;
case 3:
int y = scanner.nextInt();
System.out.println(choice3(a, x, y));
More than half of the test cases are failing due to TLE.
java array time-limit-exceeded
add a comment |Â
up vote
0
down vote
favorite
For this problem - https://www.hackerearth.com/practice/data-structures/advanced-data-structures/segment-trees/practice-problems/algorithm/shivam-and-expensive-birthday-gift-da58b2f0/description/, I tried different ways to solve it, but just reading the input without even performing any opreations takes a lot of time. Even using my custom Reader class doesn't help much. Can someone please check if my logic is right?
public class Solutions
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;
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();
private static Reader scanner = new Reader();
static int set1s;
static void choice1 (int a, int x) 1;
set1s[x]++;
static void choice2 (int a, int x)
if ((a[x] & 1) == 1)
set1s[x]--;
a[x] >>= 1;
static int choice3 (int a, int x, int y)
int result = 0;
for (int i = x; i<=y; i++)
result += set1s[i];
return result;
public static void main(String args) throws Exception
int n = scanner.nextInt(), q = scanner.nextInt();
int a = new int[n+1];
set1s = new int[n+1];
while (q-- > 0)
int choice = scanner.nextInt(), x = scanner.nextInt();
switch (choice)
case 1:
choice1(a, x);
break;
case 2:
choice2 (a, x);
break;
case 3:
int y = scanner.nextInt();
System.out.println(choice3(a, x, y));
More than half of the test cases are failing due to TLE.
java array time-limit-exceeded
"More than half of the test cases are failing due to TLE." â Are you sure that is because of the slow reading of the input data? Do you have some concrete numbers? How long does it take to just read 500,000 lines with your code?
â Martin R
May 9 at 12:14
Removing call to choice3() from your code gives 0 TLE from judge, only "Wrong Answer" so clearly reading the data is not the performance bottleneck.
â user158037
May 9 at 13:47
This doesn't appear to be your original code. Parts of it look very similar to the code in the best submission for this problem.
â tinstaafl
May 9 at 17:11
I was trying it myself, but the second case breaks for me (I don't even bother with the rest). If you look at the input, it contains ~90 successive1
operations on the same index. Because one such operation means2 * A[X] + 1
, I do not see how this could fit a 64-bit number. So to me, the test looks broken anyway. That, or I really have to go to sleep now.
â Koekje
May 9 at 21:08
@tinstaafl: I read somewhere about using custom class to scan input which is a lot faster that using Scanner, so got it from there. But the rest is what I tried out.
â user3248186
May 10 at 13:09
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
For this problem - https://www.hackerearth.com/practice/data-structures/advanced-data-structures/segment-trees/practice-problems/algorithm/shivam-and-expensive-birthday-gift-da58b2f0/description/, I tried different ways to solve it, but just reading the input without even performing any opreations takes a lot of time. Even using my custom Reader class doesn't help much. Can someone please check if my logic is right?
public class Solutions
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;
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();
private static Reader scanner = new Reader();
static int set1s;
static void choice1 (int a, int x) 1;
set1s[x]++;
static void choice2 (int a, int x)
if ((a[x] & 1) == 1)
set1s[x]--;
a[x] >>= 1;
static int choice3 (int a, int x, int y)
int result = 0;
for (int i = x; i<=y; i++)
result += set1s[i];
return result;
public static void main(String args) throws Exception
int n = scanner.nextInt(), q = scanner.nextInt();
int a = new int[n+1];
set1s = new int[n+1];
while (q-- > 0)
int choice = scanner.nextInt(), x = scanner.nextInt();
switch (choice)
case 1:
choice1(a, x);
break;
case 2:
choice2 (a, x);
break;
case 3:
int y = scanner.nextInt();
System.out.println(choice3(a, x, y));
More than half of the test cases are failing due to TLE.
java array time-limit-exceeded
For this problem - https://www.hackerearth.com/practice/data-structures/advanced-data-structures/segment-trees/practice-problems/algorithm/shivam-and-expensive-birthday-gift-da58b2f0/description/, I tried different ways to solve it, but just reading the input without even performing any opreations takes a lot of time. Even using my custom Reader class doesn't help much. Can someone please check if my logic is right?
public class Solutions
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;
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();
private static Reader scanner = new Reader();
static int set1s;
static void choice1 (int a, int x) 1;
set1s[x]++;
static void choice2 (int a, int x)
if ((a[x] & 1) == 1)
set1s[x]--;
a[x] >>= 1;
static int choice3 (int a, int x, int y)
int result = 0;
for (int i = x; i<=y; i++)
result += set1s[i];
return result;
public static void main(String args) throws Exception
int n = scanner.nextInt(), q = scanner.nextInt();
int a = new int[n+1];
set1s = new int[n+1];
while (q-- > 0)
int choice = scanner.nextInt(), x = scanner.nextInt();
switch (choice)
case 1:
choice1(a, x);
break;
case 2:
choice2 (a, x);
break;
case 3:
int y = scanner.nextInt();
System.out.println(choice3(a, x, y));
More than half of the test cases are failing due to TLE.
java array time-limit-exceeded
asked May 9 at 11:48
user3248186
1012
1012
"More than half of the test cases are failing due to TLE." â Are you sure that is because of the slow reading of the input data? Do you have some concrete numbers? How long does it take to just read 500,000 lines with your code?
â Martin R
May 9 at 12:14
Removing call to choice3() from your code gives 0 TLE from judge, only "Wrong Answer" so clearly reading the data is not the performance bottleneck.
â user158037
May 9 at 13:47
This doesn't appear to be your original code. Parts of it look very similar to the code in the best submission for this problem.
â tinstaafl
May 9 at 17:11
I was trying it myself, but the second case breaks for me (I don't even bother with the rest). If you look at the input, it contains ~90 successive1
operations on the same index. Because one such operation means2 * A[X] + 1
, I do not see how this could fit a 64-bit number. So to me, the test looks broken anyway. That, or I really have to go to sleep now.
â Koekje
May 9 at 21:08
@tinstaafl: I read somewhere about using custom class to scan input which is a lot faster that using Scanner, so got it from there. But the rest is what I tried out.
â user3248186
May 10 at 13:09
add a comment |Â
"More than half of the test cases are failing due to TLE." â Are you sure that is because of the slow reading of the input data? Do you have some concrete numbers? How long does it take to just read 500,000 lines with your code?
â Martin R
May 9 at 12:14
Removing call to choice3() from your code gives 0 TLE from judge, only "Wrong Answer" so clearly reading the data is not the performance bottleneck.
â user158037
May 9 at 13:47
This doesn't appear to be your original code. Parts of it look very similar to the code in the best submission for this problem.
â tinstaafl
May 9 at 17:11
I was trying it myself, but the second case breaks for me (I don't even bother with the rest). If you look at the input, it contains ~90 successive1
operations on the same index. Because one such operation means2 * A[X] + 1
, I do not see how this could fit a 64-bit number. So to me, the test looks broken anyway. That, or I really have to go to sleep now.
â Koekje
May 9 at 21:08
@tinstaafl: I read somewhere about using custom class to scan input which is a lot faster that using Scanner, so got it from there. But the rest is what I tried out.
â user3248186
May 10 at 13:09
"More than half of the test cases are failing due to TLE." â Are you sure that is because of the slow reading of the input data? Do you have some concrete numbers? How long does it take to just read 500,000 lines with your code?
â Martin R
May 9 at 12:14
"More than half of the test cases are failing due to TLE." â Are you sure that is because of the slow reading of the input data? Do you have some concrete numbers? How long does it take to just read 500,000 lines with your code?
â Martin R
May 9 at 12:14
Removing call to choice3() from your code gives 0 TLE from judge, only "Wrong Answer" so clearly reading the data is not the performance bottleneck.
â user158037
May 9 at 13:47
Removing call to choice3() from your code gives 0 TLE from judge, only "Wrong Answer" so clearly reading the data is not the performance bottleneck.
â user158037
May 9 at 13:47
This doesn't appear to be your original code. Parts of it look very similar to the code in the best submission for this problem.
â tinstaafl
May 9 at 17:11
This doesn't appear to be your original code. Parts of it look very similar to the code in the best submission for this problem.
â tinstaafl
May 9 at 17:11
I was trying it myself, but the second case breaks for me (I don't even bother with the rest). If you look at the input, it contains ~90 successive
1
operations on the same index. Because one such operation means 2 * A[X] + 1
, I do not see how this could fit a 64-bit number. So to me, the test looks broken anyway. That, or I really have to go to sleep now.â Koekje
May 9 at 21:08
I was trying it myself, but the second case breaks for me (I don't even bother with the rest). If you look at the input, it contains ~90 successive
1
operations on the same index. Because one such operation means 2 * A[X] + 1
, I do not see how this could fit a 64-bit number. So to me, the test looks broken anyway. That, or I really have to go to sleep now.â Koekje
May 9 at 21:08
@tinstaafl: I read somewhere about using custom class to scan input which is a lot faster that using Scanner, so got it from there. But the rest is what I tried out.
â user3248186
May 10 at 13:09
@tinstaafl: I read somewhere about using custom class to scan input which is a lot faster that using Scanner, so got it from there. But the rest is what I tried out.
â user3248186
May 10 at 13:09
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194006%2fgetting-time-limit-exceeded-for-array-updation-problem-which-takes-a-lot-of-time%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
"More than half of the test cases are failing due to TLE." â Are you sure that is because of the slow reading of the input data? Do you have some concrete numbers? How long does it take to just read 500,000 lines with your code?
â Martin R
May 9 at 12:14
Removing call to choice3() from your code gives 0 TLE from judge, only "Wrong Answer" so clearly reading the data is not the performance bottleneck.
â user158037
May 9 at 13:47
This doesn't appear to be your original code. Parts of it look very similar to the code in the best submission for this problem.
â tinstaafl
May 9 at 17:11
I was trying it myself, but the second case breaks for me (I don't even bother with the rest). If you look at the input, it contains ~90 successive
1
operations on the same index. Because one such operation means2 * A[X] + 1
, I do not see how this could fit a 64-bit number. So to me, the test looks broken anyway. That, or I really have to go to sleep now.â Koekje
May 9 at 21:08
@tinstaafl: I read somewhere about using custom class to scan input which is a lot faster that using Scanner, so got it from there. But the rest is what I tried out.
â user3248186
May 10 at 13:09