ARC112「A - B = C」

f:id:peroon:20210215202117p:plain

void solve(){
  ll l,r;cin>>l>>r;
  ll d = r-2*l+1;
  if(d<0){
    p(0);return;
  }
  ll ans = d * (d+1) / 2;
  p(ans);
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    // input
    ll N; 
    cin>>N;
    
    while(N--)solve();
    
    return 0;
}

別解:L,Rのまま図形的に考える

  • 最終的には上の解法と同じ式に帰着する
  • 条件を満たすのは下図のように三角領域になるので、辺の長さが分かれば求まる
  • 辺の長さは値域から求まる

f:id:peroon:20210215213030p:plain