Advertisement
hurmawe

5а задание. нахождение пустышек

Feb 12th, 2021
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. //общее количество пустышек
  2. vector<int> found_number_zero(const vector<bool>& array, const int& lenght)
  3. {
  4.     if (lenght % 2 == 0)
  5.     {
  6.         vector<int> output(lenght * 2 - 1);
  7.         for (int i = 0; i < lenght; i++)
  8.         {
  9.             output[lenght - 1 + i] = !(array[i]);
  10.         }
  11.         for (int i = lenght - 2; i >= 0; i--)
  12.         {
  13.             output[i] = output[i * 2 + 1] + output[i * 2 + 2];
  14.         }
  15.         return output;
  16.     }
  17.     else
  18.     {
  19.         vector<int> output((lenght + 1) * 2 - 1);
  20.         for (int i = 0; i < lenght; i++)
  21.         {
  22.             output[lenght + i] = !(array[i]);
  23.         }
  24.         for (int i = lenght - 1; i >= 0; i--)
  25.         {
  26.             output[i] = output[i * 2 + 1] + output[i * 2 + 2];
  27.         }
  28.         return output;
  29.     }
  30. }
  31. // количество пустышек на отрезке
  32. int found_number_zero_on_segment(const int l, const  int r, const  int x, const  int lx, const  int rx, const vector<int>& array)
  33. {
  34.     if (l >= rx or lx > r)
  35.         return 0;
  36.     if (lx >= l and rx <= r)
  37.         return array[x];
  38.     int m = (lx + rx) / 2;
  39.     int sl = sum(l, r, 2 * x + 1, lx, m, array);
  40.     int sr = sum(l, r, 2 * x + 2, m, rx, array);
  41.     return sl + sr;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement