NGiveUp's blog

By NGiveUp, history, 3 years ago, In English

Why i m getting TLE for this problem ??? and my solution is this. here preprocessing take O(n) and per query take constant time please help.

  • Vote: I like it
  • +1
  • Vote: I do not like it

| Write comment?
»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Use these two lines for fast IO at the start of the main function (can look at the below code).

ios_base::sync_with_stdio(false);
cin.tie(0);    cout.tie(0);  cerr.tie(0);

I submitted the same code as yours for finding the errors; your code was correct. Your code with fast IO You can read this for Theory for fast IO

I also faced some similar types of issues. I used endl instead of "\n" and got TLE on 9 TC too.

TLE with endl

AC with "\n"

Happy Coding :)

  • »
    »
    3 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Note that cout.tie(0) and cerr.tie(0) actually do nothing. What this does is it ties an input stream (e.g. cin) to an output stream (e.g. cout), so that if you cin a variable, it flushes the output beforehand. This should make sense, because otherwise the following code will be very confusing to beginners, because it won't prompt:

    #include <iostream>
    using namespace std;
    
    int main() {
        cin.tie(nullptr);
        cout << "Enter a number: ";
        int x; cin >> x;
        cout << "You entered " << x << '\n';
    }
    

    but tying cout/cerr (and clog as well) to nullptr or 0 do nothing, because they're not input streams