Hello, Codeforces.
Please, welcome c++20 support on Codeforces. Yes, it is 64-bit. Thanks to Brecht Sanders: I used his distribution GCC-11.2.0-64 from https://winlibs.com/.
If you have installed PBOX, you can add this compiler with the line pbox install gcc11-64-winlibs
. Probably, a good idea is to add C:\Programs\gcc11-64-winlibs\bin
into the PATH. More about PBOX you can read here.
I use the compilation command line similar to other GCC installations: g++ -Wall -Wextra -Wconversion -static -DONLINE_JUDGE -Wl,--stack=268435456 -O2 -std=c++20 <source>
. The only differences are -std=c++20
and -Wall -Wextra -Wconversion
(I plan to use somehow such warnings in Polygon to suggest fixes in uploaded files).
Now you can use c++20 in your solutions. I'm not sure there are many features useful in competitive programming. Probably, I'm wrong. For example, now you can write vector v{vector{1, 2}};
instead of vector<vector<int>> v{vector<int>{1, 2}};
. What else is useful? Please, if you are good with modern C++ then write.
You might be interested in looking at such a table. Before implementation, I always test every C++ distribution for the efficiency of reading and writing large amounts of data. For example, the latest GCC compiler from MSYS2 is terribly slow in some cases. I don't want to use it here. Also, it happens that some specifiers like lld
or Lf
work unexpectedly. In the table by reference, the second line is the added compiler. The columns correspond to different tests. The cell contains the time of the test execution. If I have time, I will someday publish scripts for testing c++ compiler installations.
Bye for now,
— Mike
Wow, yesterday I've just said this, and today I meet the release of GCC 11! That's great!!! Yay!!!
What is the advantages?
There are many new features in C++20, you can view them here
The main advantage is that now instead of being 3x faster than java, you can be 6x faster.
So you are telling me C++20 is 2x faster than C++14 ?
spëd
I have different results for a different problem. C++ 20 (64) submission: https://codeforces.me/contest/1328/submission/132103253 C++ 17 (64) submission: https://codeforces.me/contest/1328/submission/132103360 C++ 17 submission: https://codeforces.me/contest/1328/submission/132103444
Now that Mike is done with 64-bit PyPy and C++20, it's possible that he may look into providing updates for the other programming languages too. Do you have any constructive suggestions about improving Java performance on the Codeforces platform? For example, pili posted a request for GraalVM a few days ago. Did that request make any sense in your opinion?
I would probably say instead of going on to getting a new language, it might be better to work on server ig since I think site still performs a weird and hell slow sometimes.
Wow, latest gcc and c++20 support! Can't wait to try ;)
Seems like C++20 doesn’t introduce a lot for CP. Most useful probably are 3-way comparison shorthand and easier struct initialization.
Funnily enough, you can't actually write
vector v{vector{1, 2}};
instead of what you said, it's equivalent tovector<int> v{vector{1, 2}};
which is the same asvector{1, 2}
std::midpoint
for example. Now your binary search implementation will never overflowOh, also there are a lot of bit manipulation functions.
std::has_single_bit
— whether x is power of 2,std::popcount
— you know what, etc.Is binary search overflow actually an issue? The sum of lo+hi would have to be greater than int max which I'm not sure if occurs in CP problems. Also if you knew to use midpoint, then you could just rewrite the binary search to not overflow.
Last spring or summer, during some workshop, we had this exact bug in our code. On the other hand, it was the only time in my experience when this mattered
Or, you can also learn to use long long
Wow!!!That's great!!!
what is the function of operator <=> ? the operator can use for two values that can compare but < or == or > is return true. I can't think any uses of it.
It is more useful for implementing custom types. If you implement
<=>
, the compiler will generate<
,>
,<=
and>=
.i love codeforces be cause it is always up to date thanks mike for this update
1 min silence for those who still uses C++ 14
That's really good!
Does anyone know if ranges is good for competitive programming?
Thanks for the update to C++20!
Since it was asked in the post, here are some C++20 features I feel would be useful (and some making stuff merely easier to look at or type) in competitive programming in C++:
INT_MAX
), and one important issue is that it might affect some implementations of custom hashes. This is because C++20 finally enforces signed integers to be represented in twos-complement.printf
, butprintf
has severely limited functionality (more importantly, it doesn't deduce types and it doesn't work for user-defined types either). Usingstd::format
, you can get Python-like printing, saving the formatted string to a buffer and so on.<bit>
header for bit-related operations, which eliminates most of the need for using the__builtin_*
family of functions, and gives some more useful things likehas_single_bit
,bit_ceil
,bit_floor
.T::contains
forT
=std::map
,std::set
and family, which avoids pitfalls like usingT::count
forT
=multiset
.std::midpoint
that will finally prevent any overflow/underflow in binary search. A related function isstd::lerp
which does linear interpolation between two values while minimizing errors.std::ranges
library would help you do something like sort a vector like you would do in Python, and avoid using iterators (iterators are super-handy, but writing both iterators doesn't make sense if they're going to bebegin()
andend()
anyway). There are quite a few use-cases ofstd::ranges
that might help make code more concise and expressive.constexpr
: If you like metaprogramming as much as (or more than) I do, this is probably the most exciting feature. Now thatnew
is constexpr, it'll make life much easier to cheese problems by computing stuff at compile time, and you won't need to write your own constexpr allocators and stuff. This also meansstd::vector
andstd::string
are constexpr, which will ease metaprogramming quite a bit too (even though there are a few limitations while using them in a constexpr context).std::enable_if
s and use of SFINAE.operator==
: The first thing gets rid of having to declare multiple operators for the same order, and the second one eliminates the pesky compilation errors you get when you check for struct equality.auto
parameters of functions: This is officially called abbreviated function templates (because type deduction for auto is equivalent to that for templates). Rememberauto
return types in C++17? You can even do something likeauto add(auto x, auto y) { return x + y; }
or something like this now, rather than having to write out long templates.std::ssize
: Gives the signed size of a container. No more underflow errors while iterating withT::size
if you replace it withT::ssize
(and no more need to do typecasts either). This can also be used asssize(container)
.std::span
: Something analogous tostd::string_view
but for general types (and contiguously stored elements). Might not be useful that much in competitive programming, but this has a nice interface withstd::ranges
.<numbers>
: No more need to useacosl(-1.0)
for pi, orexp(1)
fore
. They're defined (with many more constants) in this header.starts_with
andends_with
: This makes prefix/suffix string matching simpler (better and faster than extracting a substring and checking for equality).coroutine
also looks interesting and seems to enable writing python-style generators with yield statements.init in foreach (like in c++17
if
)for(int i=0; int x : vec)
Something like
I believe it was implementation-defined, not undefined, wasn't it?
Yeah, I somehow swapped that out mentally while writing that out. I'll edit that in, thanks a ton for the correction!
It seems that designated initializers do not allow reordering (source) and work only for normal structs aka aggregates which std::pair is not.
Thanks for the correction! I had misremembered, and the correct thing that designated initializers allow that a normal aggregate initialization doesn't seem to allow is that you're allowed to skip out some variables in the initialization, and not reorder them. Fixed it now (hopefully).
The fact that
std::set
didn't come withcontains
until now and I had to usefind
orcount
instead is mind-boggling to me. You would think the one operation a container modelling a mathematical set would have is checking set containment!https://en.cppreference.com/w/cpp/container/set/contains
I have done submit 132081784 and it had RE on first test. But when I have submitted same code 132085792 C++17 it got AC. So, it was because I have used non-standart allocator, without it, my code still works. I think that isn't right and I don't know how to fix it.
It's looks like everything works if you remove "inline" from operators overload, but I'm not ready to guarantee it in general case :)
No, we have checked it.
I've checked too. submission
Did you add it specially before first Technocup qualification contest?)
When you have comparator function
auto cmp = [](const T&, const T&) -> bool { ... };
,you can now write
set<T, decltype(cmp)> s;
instead ofset<T, decltype(cmp)> s(cmp);
recently two of my solutions timed out/MLEd in C++ 17 64-bit but passed with a comfortable margin in C++17. Can anyone explain why is that the case? The difference was 100 MB for the MLE and ~0.2 sec for the TLE.
So why is 64 bit sometimes badder?
There might be many reasons for that: generally speaking, 64 bit arithmetic is slower for obvious reasons (and was much slower in the past because processors were designed to handle 32 bits efficiently but it's not as problematic now), the memory allocations are larger, cache will have higher miss rate (objects are simply bigger), and so on.
The performance depends on many factors and it might not be easy to predict: sometimes the 64 bit programs will realize variables only use 32 bits of space and downgrade some operations, sometimes 32 bit programs can pack two ops into 64 bits which is not possible when compiled with 64 bits and so on. The idea here is thar there are numerous compiler optimizations and it's virtually impossible to predict all the effects of these optimizations for a given program in advance even for very experienced compiler engineers. Finally, it depends on the processor a lot.
Okay, thanks a lot.
This looks like a great idea! I would suggest to also include
-Wshadow
(mentioned here). At least for me, this is one of the most useful warnings.I've seen some holy wars on
-Wshadow
because of constructors, e.g.:Nice I need to keep switching between 4 versions xD
Codechef also supports C++17
Btw, MikeMirzayanov, would you please consider adding Clang not just for diagnostics?
I'm working on a book about performance engineering with a lot of compiler-specific C/C++ examples, and the only reason I haven't ditched GCC for Clang/LLVM is that it isn't widely available in the competitive programming environment. Adding it to CodeForces will greatly catalyze the change.
One small downside to using clang is that you won't have
__gnu_pbds
for order statistics tree.Not really. GNU C++'s "Policy-Based Data Structures" is just a library, meaning there is a bunch of C++ headers in the system when GNU C++ toolchain is installed. They can be included with Clang just like any other header/library.
Moreover, using Clang does not imply or require using libc++. By default, Clang will use libstdc++ on Linux unless told otherwise.
You can now not implement a binary search for problems with binary search over a function
f
, if for whatever reason you don't want to do it, like so:edit: this can also be reduced to this:
You need partition_point, not lower_bound
You can almost make it look like python now:
Now I can write ungodly python-style C++ — The future is now!
Is it me or is this language insanely complicated? Wtf does this mean https://en.cppreference.com/w/cpp/ranges/iota_view
Thanks for this!
Please, add common lisp
It is very convenient to use projection functions when sorting structures. For example sorting edges by weight:
Very cool trick! Never thought you could pass pointer to member variable to projection.
Note overhead of member pointers though (https://quick-bench.com/q/WkWqebbpeT2KGuH80zTl9ULwp4E)
Or rather worse optimization I should say
How about
-Wshadow
? Is it included in those?When will release
C++20 32-bit
?Is there a specific reason behind asking for a 32-bit version? The 64-bit version is almost always faster than the 32-bit version, so I'm just curious about some use-case that's not so rare but makes 32-bit strictly necessary.
Pointers take two times less space. May result in better ML fit (especially for pointer-heavy data structures like self-balancing trees) and better cache fit, hence faster execution.
It seems that we can use chinese characters as variable names,is it?
my code in c++20 gives me TLE.
whereas the same code in c++17 gets accepted in 140ms.
can someone please tell the reason behind this.
Compiling your code with
-fsanitize=undefined -fsanitize=address
options and running it with the first testcase input data results in the following error:test.cpp:28:5: runtime error: execution reached the end of a value-returning function without returning a value
Your
pre()
function needs to returnvoid
instead ofint
. Fixing this source of undefined behaviour makes your solution run fine at least in https://codeforces.me/problemset/customtestI have different answers too
problem: https://codeforces.me/contest/459/submission/193205657
TLE 17 using GNU C++ 20 64: https://codeforces.me/contest/459/submission/193205461 ACC using GNU C++ 14 : https://codeforces.me/contest/459/submission/193205657
can anyone explain why?
ios optimizatons input