Cod sursa(job #1471579)

Utilizator atatomirTatomir Alex atatomir Data 14 august 2015 15:02:35
Problema P-sir Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.24 kb
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

#define fordef(it,a,b) for(it=a;it<=b;it++)
#define mp make_pair
#define pb push_back
#define ll long long
#define pa(a,b) pair< a,b >

#define maxN 2015

struct Elem{
    int value;
    long long sum;

    Elem(int _value,int _cnt){
        value = _value;
        sum = _cnt;
    }
    bool operator<(const Elem& who)const{
        return value < who.value;
    }
};

const long long mod = (1LL<<32);
int n,i,last;
int v[maxN];
vector< Elem > wh[maxN];
long long ans;

int diff(int a,int b){
    a -= b;
    if (a<0) return -a;
    return a;
}

void keep(long long &who){
    while (who<0) who += mod;
    while (who>=mod) who-=mod;
}

int main()
{
    freopen("psir.in","r",stdin);
    freopen("psir.out","w",stdout);

    scanf("%d",&n);
    for (i=1;i<=n;i++) scanf("%d",&v[i]);

    for (i=2;i<=n;i++) {
        wh[i].clear();

        for (last=i-1;last>0;last--) {
            long long act = 1;

            if ( diff(v[last],v[i]) == 0) {
                wh[i].pb( Elem(v[last],act) );
                continue;
            }

            vector< Elem >::iterator it;
            if(wh[last].size()!=0)
            if (v[last]<v[i]) {
                //! find value in interval ( v[i],-- )
                it = upper_bound(wh[last].begin(),wh[last].end(), Elem(v[i],0) );

                act += wh[last][ wh[last].size()-1 ].sum;
                if (it!=wh[last].begin()){
                    it--; act -= it->sum;
                }
            } else {
                //! find value in interval ( --,v[i] )
                it = lower_bound(wh[last].begin(),wh[last].end(), Elem(v[i],0) );

                if (it!=wh[last].begin()){
                    it--; act += it->sum;
                }
            }

            keep(act);
            wh[i].pb( Elem(v[last],act) );
        }

        if (wh[i].size()==0) continue;

        sort(wh[i].begin(),wh[i].end());
        for (int j=1;j<wh[i].size();j++) {
            wh[i][j].sum += wh[i][j-1].sum;
            keep( wh[i][j].sum );
        }

        ans += wh[ i ][ wh[i].size()-1 ].sum;
        keep(ans);
        //cerr << ans << '\n';
    }


    printf("%lld",ans);


    return 0;
}