(规律)cf#524-B.Margarite and the best present

https://codeforces.com/contest/1080/problem/B

规律->奇数开始每两个数和为-1,偶数开始每两个数和为1,处理总个数的最后一个数即可

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
typedef long long ll;

int q, l, r;
int main()
{
    cin >> q;
    while(q--) {
        cin >> l >> r;
        int num = r - l + 1;
        if(num & 1) {
            ll ans = l & 1 ? num / 2 : -1 * num / 2;
            ans += r & 1 ? -r : r;
            cout << ans << endl;
        }
        else {
            ll ans = l & 1 ? num / 2 : -1 * num / 2;
            cout << ans << endl;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_40588429/article/details/84450869
今日推荐