Hello! I had run time error on this problem : https://codeforces.net/contest/377/problem/A
Here is my code : https://codeforces.net/contest/377/submission/74861444
It works normally on my laptop.
Can someone help me please?
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 164 |
1 | maomao90 | 164 |
3 | Um_nik | 163 |
4 | atcoder_official | 160 |
4 | adamant | 160 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
8 | Dominater069 | 154 |
8 | nor | 154 |
Hello! I had run time error on this problem : https://codeforces.net/contest/377/problem/A
Here is my code : https://codeforces.net/contest/377/submission/74861444
It works normally on my laptop.
Can someone help me please?
Название |
---|
No runtime error is weird. Here's what your runtime error has to say:
NOTE: You can learn more about your RTE by scrolling to the bottom of your submission and selecting the more details option.
Diagnostics detected issues [cpp.g++17-drmemory]: Dr,2020-03-31.M Dr. Memory version 1.11.0 Dr,2020-03-31.M Running "program.exe"
This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information. C:/Programs/mingw-w64-7/lib/gcc/i686-w64-mingw32/7.3.0/include/c++/debug/vector:417: Error: attempt to subscript container with out-of-bounds index 1, but container only holds 1 elements.
Objects involved in the operation: sequence "this" @ 0x00586BC4 { type = std::__debug::vector<std::pair<int, int>, std::allocator<std::pair<int, int> > >; }
Clearly, you're trying to access an element that is not yours, i.e., element at index 1, whilst the size of your vector is only 1.
It works normally on my laptop.
No, it doesn't. That's the thing about C++. It lets you think you're right all the time. You may get the correct result but that may just be a fluke. I'll explain to you a little about std::vector or in general about containers in C++.
When you create a vector, even when you haven't added any elements, it has already allocated some amount of memory for you beforehand but as you haven't added anything yet, the size() method returns 0. It works this way in order to minimise the number of reallocations and copying, which also has the added benefit of making push_back() time complexity O(1) on average. Now, let's look at another case. Let's say you have a million elements in your vector and you call vector::clear(). Now, the size() method returns to you 0, but guess what? You can still access all the million elements as they are not freed from memory. Instead, when you do push_back() or emplace_back(), you re-write over the already allocated memory. The reason for this is that if you have a million elements and decide to clear all of them, there's still a chance you might wanna add a million more elements again. This method is extremely optimal as there are many benefits as compared to: allocate when necessary. One more scenario. Let's consider you have some elements in your vector and you choose to add a few more. Note that I mentioned above that the vector pre-allocates some extra memory at each re-allocation. So, you think you only have space for those "some" elements, but in reality, you have more space secretly allocated (so that inserting can be O(1) average). There is a certain load factor that determines when it is a good time for the container to reallocate with more memory. It is usually around 0.7 or 0.8 in most container implementations. The load factor is calculated as:
. When this factor is hit, you have re-allocation with copying, but there are other factors too that determine/predict how much extra memory should be allocated beforehand.
The reason why I explained all of the above is because sometimes you have $$$X$$$ elements in your vector, and you may, by mistake, access the $$$(X + 1)th$$$ element or the $$$(X + 10)th$$$ element which may or may not work correctly, depending on the reallocation factor. Try accessing the $$$(X + 1000)th$$$ element. An exception will be raised in the latter scenario. However, in the former ones, no exception will be raised because your vector will always have allocated some extra memory.
Moral of the story: C++ may make you think you're right but :((
Here, on CF, you have various software integrated together to catch every small mistake you make. Dr. Memory happens to be the one that checks for out-of-bounds.
The various kinds of RTEs you could possibly get: out-of-bounds-access/write, signed-integer-overflow, memory-leak, divide-by-zero, etc.
Hey, if anyone knows how I can write text separated by space in TeX fractions, it'd be really helpful. All the text I write in formulas has its spacing removed. Thanks!
Hey zeus, thanks for the explanation it's interesting to know more about how it works.
But in my case I do not see where I am accessing an element that is not mine.
What should I do then to fix this runtime error?
Hey Zeus, the problem is fixed.
It was caused by some undifined behaviors of c++ by using ios::sync and tie functions with scanf and printf.
https://codeforces.net/contest/377/submission/74876271