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

Автор Asterisk007, история, 22 месяца назад, По-английски

Since I cant find the editorial i am asking my doubt here. my code to problem F

https://mirror.codeforces.com/contest/1729/problem/F

is repeatedly failing test case 7.

Here is my code for your reference:

https://mirror.codeforces.com/contest/1729/submission/172019259

thanks

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

»
22 месяца назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

you are trying to cast string to int with stoi() but the result is too large to represent it as integer. My local G++ 12.2.0 fails with std::out_of_range if i want to cast string '111...1' (length = 1000) to int with stoi() for example

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

    im new to cpp. which is a more efficient method to do this cast?

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

      stoi() is the best. You just can't store 10^100 as a number using fundamental types (like int or long long) in cpp

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

      i forgot about stoll(). This function works like stoi() but it casts to long long (stoi() casts to int)

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

string qq=s.substr(l-1,r-l+1); int j=stoi(qq);//here it is exceeding integer limit...

notice that you don't need to store the whole number in 'j' variable ...just store gg%9 in 'j'

but still it will give TLE you need to process all 'w' substrings and store the information in an double dimentional vector and later in queries you can use them in O(1) time :-)