Cod sursa(job #2524777)

Utilizator theo2003Theodor Negrescu theo2003 Data 16 ianuarie 2020 11:51:14
Problema Divk Scor 70
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.79 kb
#include <vector>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream cin("divk.in");
ofstream cout("divk.out");
struct vallist{
    int nr = -1, *arr = NULL, val = -1;
    vallist(int len, int v){
        nr = len;
        val = v;
        arr = new int[len];
    }
    vallist(int v){
        val = v;
    }
};
vector<vallist> vals;
pair<int, int> tmp[500002];
int n, k, a, b, l[500001], ps[500001];
long long sequences = 0;
int main(){
    vals.reserve(510009);
    cin>>n>>k>>a>>b;
    for(int x = 1;x<=n;x++){
        cin>>l[x];
        l[x] %= k;
    }
    for(int x = 1;x<=n;x++){
        ps[x] += ps[x - 1] + l[x];
        ps[x] %= k;
    }
    {
        for(int x = 1;x<=n;x++){
            tmp[x] = pair<int, int>(ps[x], x);
        }
        sort(tmp + 1, tmp + n + 1);
        tmp[n + 1] = {-1, -1};
        int current = tmp[1].first, len = 1;
        for(int x = 2;x<=(n+1);x++){
            if(current != tmp[x].first){
                vals.push_back(vallist(len, current));
                for(int y = x - len, tmpi = 0;y != x;y++){
                    vals.back().arr[tmpi++] = tmp[y].second;
                }
                current = tmp[x].first;
                len = 1;
            }else len++;
        }
    }
    for(int x = 1;x<=n;x++){
        auto val_list = lower_bound(vals.begin(), vals.end(), vallist(ps[x - 1]), [](vallist a, vallist b){return a.val < b.val;});
        if(val_list->val != ps[x - 1])
            continue;
        int LLEN = val_list->nr;
        auto LIST = val_list->arr;
        auto f = lower_bound(LIST, LIST + LLEN, x + a - 1), l = upper_bound(LIST, LIST + LLEN, x + b - 1);
        if(f == (LIST + LLEN))
            continue;
        sequences += distance(f, l);
    }
    cout<<sequences<<endl;
}