PGSStas

2019.03.19_D

Mar 19th, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.34 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define F first
  4. #define S second
  5. #define ll long long
  6. #define ull unsigned long long
  7. #define ld long double
  8. #define SELO ""
  9. #define openfiles ifstream cin("input"  SELO  ".txt"); ofstream cout("output"  SELO  ".txt");
  10. #define faster ios_base::sync_with_stdio(NULL); cin.tie(0); cout.tie(0);
  11. #define all(x) x.begin(), x.end()
  12.  
  13. using namespace std;
  14.  
  15. vector <pair <pair <ll, ll>, char > > v;
  16. vector <ll> symbols_Count;
  17.  
  18. int main()
  19. {
  20.     openfiles
  21.     string s;
  22.     cin >> s;
  23.     ll t = 0;
  24.     for(int i = 0; i < s.size(); i++)
  25.     {
  26.         int kol = 0;
  27.         int j = i;
  28.         while(s[j] >= '0' && s[j] <= '9')
  29.         {
  30.             kol = 10 * kol + (s[j] - '0');
  31.             j++;
  32.         }
  33.         char cur_Letter = s[j];
  34.         kol = max(kol, 1);
  35.         v.push_back({{0, kol - 1}, cur_Letter});
  36.         if(v.size() > 1)
  37.         {
  38.             v[v.size() - 1].F.F += v[v.size() - 2].F.S + 1;
  39.             v[v.size() - 1].F.S += v[v.size() - 2].F.S + 1;
  40.         }
  41.         t = j;
  42.         symbols_Count.push_back(t);
  43.         i = j;
  44.     }
  45.     int q;
  46.     cin >> q;
  47.     while(q--)
  48.     {
  49.         ll l, r;
  50.         cin >> l >> r;
  51.         l--;
  52.         r--;
  53.         pair <pair <ll, ll>, char> zzz = {{l, 1000000000000000ll}, 'z'};
  54.         int j1 = (int)(upper_bound(all(v), zzz) - v.begin()) - 1;
  55.         zzz.F.F = r;
  56.         int j2 = (int)(upper_bound(all(v), zzz) - v.begin()) - 1;
  57.         ll ans = 0;
  58.         if(j2 != j1)
  59.         {
  60.             ans = symbols_Count[j2 - 1] - symbols_Count[j1];
  61.             ll kol1 = v[j1].F.S - l + 1;
  62.             int jjj = 0;
  63.             if(kol1 != 1)
  64.                 while(kol1)
  65.                 {
  66.                     jjj++;
  67.                     kol1 /= 10;
  68.                 }
  69.             ans += 1 + jjj;
  70.             kol1 = r - v[j2].F.F + 1;
  71.             jjj = 0;
  72.             if(kol1 != 1)
  73.                 while(kol1)
  74.                 {
  75.                     jjj++;
  76.                     kol1 /= 10;
  77.                 }
  78.             ans += 1 + jjj;
  79.         }
  80.         else
  81.         {
  82.             ll kol1 = r - l + 1;
  83.             int jjj = 0;
  84.             if(kol1 != 1)
  85.                 while(kol1)
  86.                 {
  87.                     jjj++;
  88.                     kol1 /= 10;
  89.                 }
  90.             ans += 1 + jjj;
  91.         }
  92.         cout << ans << '\n';
  93.     }
  94. }
Add Comment
Please, Sign In to add comment