Блог пользователя naivedyam

Автор naivedyam, 6 недель назад, По-английски

THIS PROBLEM from Codeforces Edu section seems to be an exact replica of CSES problem DYNNAMIC RANGE QUERIES. I solved the CSES one and it passed but solving the Codeforces one was giving runtime error. Tried fixing it but didn't help much. Even the one that passed on CSES dosen't pass on CPH and gives Runtime error on my VS Code too. So I decided to copy the same code that passed on CSES and try it here on codeforces but for some reason, even the code that passed on CSES is giving me runtime error on Codeforces too! It is not even a difference in the version of C++ since on both the sites I used C++20.

THIS IS MY CSES CODE

While [submission:286148512] is my Codeforces submission (both are same since I tried pasting the same code after 2 runtime errors). Could someone explain why this weird behavior even while using the same C++ versions? And why is my CSES submission getting AC on CSES but giving run time error on both codeforces and vs code?

  • Проголосовать: нравится
  • +3
  • Проголосовать: не нравится

»
6 недель назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

CSES problem queries use 1 indexing. Codeforces problem queries use 0 indexing. You'll have to fix your calls to update and query.

  • »
    »
    6 недель назад, # ^ |
    Rev. 5   Проголосовать: нравится 0 Проголосовать: не нравится

    Also, many things wrong with this code. Take this as constructive criticism please, it'll help your code style and performance.

    • Why does update return something? And what's the point of setting return value of recursive call to tree[index]?

    • Do not use #define int long long, it will make the compiler not be able to vectorize as well (read: SIMD) for a lot of your use cases.

    • Do not blindly do arr.push_back without first reserving memory for it using arr.reserve(n). You are wasting allocations, which are expensive.

    • It is weird that you have an initializer for Node but still use a variable called def to represent that initial state.

    • Try avoiding global state as much as possible -- I can understand tree and arr but n has no reason to be up there (when you accidentally define int n in main() it will be hard to debug!).

    • »
      »
      »
      6 недель назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      Thanks for pointing out the problems in the code. I am just learning segment trees so I decided to follow 146704540 as a template and solving the problems via doing manipulations in it. Could you suggest a better one for the same? (PS — I am not experienced with graphs/trees).

      Also that #define long long thing should be avoided just for this problem or any question in general? I have been using it in my template for all the problems I have done till now. Didn't know that's a wrong practice (was taught to use that as a template when I started cp because they said this helps avoid accidently submitting a problem with constrains beyond int into int datatype)

»
6 недель назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

one is 0-indexed and the other one isnt.