Getting Time limit exceeded for array updation problem which takes a lot of time to even just read input

The name of the pictureThe name of the pictureThe name of the pictureClash 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.







share|improve this question



















  • "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 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
















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.







share|improve this question



















  • "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 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












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.







share|improve this question











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.









share|improve this question










share|improve this question




share|improve this question









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 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
















  • "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 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















"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















active

oldest

votes











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















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



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














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













































































Popular posts from this blog

Chat program with C++ and SFML

Function to Return a JSON Like Objects Using VBA Collections and Arrays

Will my employers contract hold up in court?