1. 程式人生 > >(規律)cf#524-B.Margarite and the best present

(規律)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;
        }
    }
}