【BestCoder】 HDOJ 5163 Taking Bus

分类讨论一下就行啦。。。。

#include <iostream>
#include <queue> 
#include <stack> 
#include <map> 
#include <set> 
#include <bitset> 
#include <cstdio> 
#include <algorithm> 
#include <cstring> 
#include <climits>
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 200005
#define maxm 400005
#define eps 1e-10
#define mod 1000000007
#define INF 0xi3f3f3f3f
#define PI (acos(-1.0))
#define lowbit(x) (x&(-x))
#define mp make_pair
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid 
#define rson o<<1 | 1, mid+1, R
#define pii pair<int, int>
//#pragma comment(linker, "/STACK:16777216")
typedef long long LL;
typedef unsigned long long ULL;
//typedef int LL;
using namespace std;
LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;}
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}
// head

int n, m;
LL sum[maxn];

void read()
{
    scanf("%d%d", &n, &m), n--;
    for(int i = 1; i <= n; i++) {
        scanf("%I64d", &sum[i]);
    }
    for(int i = n+1; i <= 2 * n; i++)
        sum[i] = sum[2 * n - i + 1];
    for(int i = 1; i <= 2 * n; i++) sum[i] += sum[i-1];
    //for(int i = 1; i <= 2 * n; i++) printf("AAA %d\n", sum[i]);
}

LL calc(int a, int b, int d)
{
    if(a == b) return 0;
    //printf("AAA %d %d %d\n", a, b, sum[6]);
    if(d == 0) {
        if(a < b) return sum[b-1] - sum[a-1];
        else {
            b = 2 * n - b + 1;
            return sum[b] - sum[a-1];
        }
    }
    else {
        if(a > b) return sum[a-1] - sum[b-1];
        else return sum[b-1] + sum[a-1];
    }
}

void work()
{
    int now = 1, a, b, t = n + 1;
    while(m--) {
        int d = 0;
        scanf("%d%d", &a, &b);
        LL ans;
        ans = calc(now, a, d);
        if(a < now) d = 1;
        ans += calc(a, b, d);
        now++;
        if(now > t) now -= t;
        if(a == b) printf("0\n");
        else printf("%I64d\n", ans);
    }
}

int main(void)
{
    int _;
    while(scanf("%d", &_)!=EOF) {
        while(_--) {
            read();
            work();
        }
    }


    return 0;
}


你可能感兴趣的:(BestCoder)