题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5400

题意:定义(d1,d2)算术序列为:对于给定的序列b1,b2,...,bn,存在i使得bj+1=bj+d1( j∈[1,i) );bj+1=bj+d2(j∈[i,n)).现在给出d1,d2和一个序列a1,a2,...,an,找出有多少个区间[l,r]满足(d1,d2)算术序列。

解法:我们可以找出对于给定的序列,满足算术序列的所有最大子串的长度,然后对于每一个子串,找出他的所有子串即可。对于一个长度为n的序列,其所有非空子串的个数为n(n+1)/2(可以由挡板原理算出)。加了读入挂竟然跑到了HDU上面的第一名。


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 100010;
int n, x, y, d1, d2, sub[maxn];
struct FastIO
{
    static const int S = 1310720;
    int wpos;
    char wbuf[S];
    FastIO() : wpos(0) {}
    inline int xchar()
    {
        static char buf[S];
        static int len = 0, pos = 0;
        if (pos == len)
            pos = 0, len = fread(buf, 1, S, stdin);
        if (pos == len) exit(0);
        return buf[pos ++];
    }
    inline int xuint()
    {
        int c = xchar(), x = 0;
        while (c <= 32) c = xchar();
        for (; '0' <= c && c <= '9'; c = xchar()) x = x * 10 + c - '0';
        return x;
    }
    inline int xint()
    {
        int s = 1, c = xchar(), x = 0;
        while (c <= 32) c = xchar();
        if (c == '-') s = -1, c = xchar();
        for (; '0' <= c && c <= '9'; c = xchar()) x = x * 10 + c - '0';
        return x * s;
    }
    inline void xstring(char *s)
    {
        int c = xchar();
        while (c <= 32) c = xchar();
        for (; c > 32; c = xchar()) * s++ = c;
        *s = 0;
    }
    inline void wchar(int x)
    {
        if (wpos == S) fwrite(wbuf, 1, S, stdout), wpos = 0;
        wbuf[wpos ++] = x;
    }
    inline void wint(LL x)
    {
        if (x < 0) wchar('-'), x = -x;
        char s[24];
        int n = 0;
        while (x || !n) s[n ++] = '0' + x % 10, x /= 10;
        while (n--) wchar(s[n]);
        wchar('\n');
    }
    inline void wstring(const char *s)
    {
        while (*s) wchar(*s++);
    }
    ~FastIO()
    {
        if (wpos) fwrite(wbuf, 1, wpos, stdout), wpos = 0;
    }
} io;
int main()
{
    while(1)
    {
        n = io.xint();
        d1 = io.xint();
        d2 = io.xint();
        y = io.xint();
        for(int i=1; i<n; i++){
            x = y;
            y=io.xint();
            sub[i] = y-x;
        }
        sub[n] = 0x3f3f3f3f;
        bool flag = 1;
        LL ans = n;
        int l = 0, r = 0;
        for(int i=1; i<=n; i++)
        {
            if(sub[i] == d1 && flag){
                l++;
            }
            else if(sub[i] == d2){
                r++;
                flag = false;
            }
            else{
                ans += 1LL*(l+r)*(l+r+1)/2;
                l = r = 0;
                flag = true;
                if(sub[i] == d1){
                    l++;
                }
            }
        }
        io.wint(ans);
    }
    return 0;
}