Advertisement
Infiniti_Inter

E

Sep 8th, 2018
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. const int N = 1e5 + 13;
  5. int a[N], b[N], dist[N];
  6.  
  7. int main()
  8. {
  9.     int n, m;
  10.     cin >> n >> m;
  11.  
  12.     for (int i = 0; i < n;++i)
  13.         scanf("%d", &dist[i]);
  14.     a[0] = b[0] = 0;
  15.     b[n] = b[0];
  16.     for (int i = 0; i <= n; ++i)
  17.         {
  18.             a[i] = a[i-1] + dist[i];
  19.             b[n - i] = b[n - i + 1] + dist[n - i];
  20.         }
  21.     int save_a = a[n-1];
  22.     for (int i = n; i > 0; --i)
  23.     {
  24.         a[i] = a[i-1];
  25.  
  26.     }
  27.         a[0] = 0;
  28.         swap(b[0], b[n]);
  29.  
  30.  
  31.    for (int i = 0; i < m; ++i)
  32.    {
  33.        int x,y; scanf("%d%d", &x, &y);
  34.        x--;y--;
  35.             cout << min(abs(a[x]-a[y]), abs(b[x]-b[y])) << endl;
  36.    }
  37.  
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement