码迷,mamicode.com
首页 > 其他好文 > 详细

[hdu5400 Arithmetic Sequence]预处理,容斥

时间:2015-08-19 00:18:57      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:

题意:http://acm.hdu.edu.cn/showproblem.php?pid=5400

思路:预处理出每个点向左和向右的最远边界,从左向右枚举中间点,把区间答案加到总答案里面。由与可能与前面的区间重叠,需要减去重复的答案,由于左边界非降,所以重叠的区间长度很容易得到。

 

#pragma comment(linker, "/STACK:10240000")
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

#define X                   first
#define Y                   second
#define pb                  push_back
#define mp                  make_pair
#define all(a)              (a).begin(), (a).end()
#define fillchar(a, x)      memset(a, x, sizeof(a))
#define copy(a, b)          memcpy(a, b, sizeof(a))

typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull;

//#ifndef ONLINE_JUDGE
void RI(vector<int>&a,int n){a.resize(n);for(int i=0;i<n;i++)scanf("%d",&a[i]);}
void RI(){}void RI(int&X){scanf("%d",&X);}template<typename...R>
void RI(int&f,R&...r){RI(f);RI(r...);}void RI(int*p,int*q){int d=p<q?1:-1;
while(p!=q){scanf("%d",p);p+=d;}}void print(){cout<<endl;}template<typename T>
void print(const T t){cout<<t<<endl;}template<typename F,typename...R>
void print(const F f,const R...r){cout<<f<<", ";print(r...);}template<typename T>
void print(T*p, T*q){int d=p<q?1:-1;while(p!=q){cout<<*p<<", ";p+=d;}cout<<endl;}
//#endif
template<typename T>bool umax(T&a, const T&b){return b<=a?false:(a=b,true);}
template<typename T>bool umin(T&a, const T&b){return b>=a?false:(a=b,true);}

const double PI = acos(-1.0);
const int INF = 1e9 + 7;
const double EPS = 1e-12;

/* -------------------------------------------------------------------------------- */

const int maxn = 1e5 + 7;

int a[maxn], R[maxn], L[maxn];

int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
    int n, d1, d2;
    while (cin >> n >> d1 >> d2) {
        for (int i = 1; i <= n; i ++) {
            scanf("%d", a + i);
        }
        for (int i = n; i >= 2; i --) {
            a[i] = a[i] - a[i - 1];
        }
        a[n + 1] = INF * 2;
        R[n + 1] = INF;
        for (int i = 1; i <= n; i ++) {
            L[i] = max(1, a[i] == d1? L[i - 1] : i);
        }
        for (int i = n; i; i --) {
            R[i] = min(n, a[i + 1] == d2? R[i + 1] : i);
        }
        int lastl = 0, lastr =0;
        ll ans = 0;
        for (int i = 1; i <= n; i ++) {
            ll c = R[i] - L[i] + 1;
            ans += c * (c + 1) / 2;
            ll cc = min(lastr, R[i]) - L[i] + 1;
            if (cc > 0) ans -= cc * (cc + 1) / 2;
            lastl = L[i];
            umax(lastr, R[i]);
        }
        cout << ans << endl;
    }
    return 0;
}

[hdu5400 Arithmetic Sequence]预处理,容斥

标签:

原文地址:http://www.cnblogs.com/jklongint/p/4740926.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!