Cod sursa(job #2524781)

Utilizator theo2003Theodor Negrescu theo2003 Data 16 ianuarie 2020 11:59:59
Problema Divk Scor 70
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.16 kb
#include <unordered_map>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream cin("divk.in");
ofstream cout("divk.out");
struct vallist{
    int nr = 0, *arr = NULL, tmpiter = 0;
};
unordered_map<int, vallist> vals;
pair<int, int> tmp[500002];
int n, k, a, b, l[500001], ps[500001];
long long sequences = 0;
int main(){
    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;
        vals[ps[x]].nr++;
    }
    for(auto &i : vals)
        i.second.arr = new int[i.second.nr];
    for(int x = 1;x<=n;x++){
        vals[ps[x]].arr[vals[ps[x]].tmpiter++] = x;
    }
    for(int x = 1;x<=n;x++){
        if(vals.find(ps[x - 1]) == vals.end())
            continue;
        vallist * val_list = &vals[ps[x - 1]];
        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;
}