Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int N = 1e5 + 13;
- int a[N], b[N], dist[N];
- int main()
- {
- int n, m;
- cin >> n >> m;
- for (int i = 0; i < n;++i)
- scanf("%d", &dist[i]);
- a[0] = b[0] = 0;
- b[n] = b[0];
- for (int i = 0; i <= n; ++i)
- {
- a[i] = a[i-1] + dist[i];
- b[n - i] = b[n - i + 1] + dist[n - i];
- }
- int save_a = a[n-1];
- for (int i = n; i > 0; --i)
- {
- a[i] = a[i-1];
- }
- a[0] = 0;
- swap(b[0], b[n]);
- for (int i = 0; i < m; ++i)
- {
- int x,y; scanf("%d%d", &x, &y);
- x--;y--;
- cout << min(abs(a[x]-a[y]), abs(b[x]-b[y])) << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement