fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main(){
  5. ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  6. int t;cin>>t;
  7. while(t--){
  8. int n;cin>>n;
  9. vector<int> a(n+2);
  10. for(int i=1;i<=n;i++)cin>>a[i];
  11.  
  12. vector<int> pfxL(n+2),pfxR(n+2);
  13. for(int i=2;i<=n;i++)
  14. pfxL[i]=pfxL[i-1]+(a[i]-a[i-1]>a[i+1]-a[i]?1:a[i+1]-a[i]);
  15. for(int i=n-1;i>=1;i--)
  16. pfxR[i]=pfxR[i+1]+(a[i]-a[i+1]<a[i+1]-a[i]?1:a[i]-a[i-1]);
  17.  
  18. int q;cin>>q;
  19. while(q--){
  20. int a,b;cin>>a>>b;
  21. if(a<b)cout<<pfxL[b]-pfxL[a-1];
  22. else cout<<pfxR[a]-pfxR[b+1];
  23. cout<<'\n';
  24. }
  25.  
  26. }
  27. return 0;
  28. }
Success #stdin #stdout 0s 5312KB
stdin
1
5
0 8 12 15 20
5
1 4
1 5
3 4
3 2
5 1
stdout
7
8
6
0
-3