B Tokitsukaze and a+b=n (medium)

这题我是找数学规律然后马上做出来了

思路

这题一个循环都会超时,所以不能用循环

观察两行可以发现,两行中所要相加的数是一一对应的,各行的总数相等且正好是答案。还正好是连续的。

所以只要找到头和尾的数所在位置运算就是答案(头-尾+1)

#include <iostream>
using namespace std;

int main() {
    long long int t, n, l1, l2, r1, r2, res;
    cin>>t;
    while (t--) { 
        cin>>n;
        cin>>l1>>r1;
        cin>>l2>>r2;
        if(l1+l2>n||r1+r2<n){
            cout<<"0"<<endl;
        }else{
            cout<<abs(max(l1,n-r2)-min(n-l2,r1))+1<<endl;
        } 
    }
}