#include<bits/stdc++.h>
using namespace std;
//#define DEBUG
#define int long long
pair<int,int> f(int x){
int l=1,r=200000;
while(l<r){
int m=(l+r+1)>>1;
if(m*(m-1)/2<x) l=m;
else r=m-1;
}
return {l,x-l*(l-1)/2};
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n,q,t; cin>>n>>q>>t;
int a[n+1];
for(int i=1;i<=n;++i){
cin>>a[i];
a[i]-=i*(i-1)/2;
}
while(q--){
int _x,_y; cin>>_x>>_y;
pair<int,int> x=f(_x),y=f(_y);
#ifdef DEBUG
cout<<"x: "<<x.first<<" "<<x.second<<'\n';
cout<<"y: "<<y.first<<" "<<y.second<<'\n';
#endif
if(x.first<y.first) swap(x,y);
while(x.first>y.first) if(x.second>=a[--x.first])
--x.second;
while(x.second!=y.second){
#ifdef DEBUG
cout<<"x: "<<x.first<<" "<<x.second<<'\n';
cout<<"y: "<<y.first<<" "<<y.second<<'\n';
#endif
if(x.second>a[--x.first])
--x.second;
if(y.second>a[--y.first])
--y.second;
}
#ifdef DEBUG
cout<<x.first<<" "<<x.second<<'\n';
#endif
cout<<(x.first)*(x.first-1)/2+x.second<<'\n';
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |