Cod sursa(job #2524797)

Utilizator theo2003Theodor Negrescu theo2003 Data 16 ianuarie 2020 12:21:20
Problema Divk Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.59 kb
#include <unordered_map>
#include <fstream>
#include <algorithm>
#define LLEN val_list->nr
#define LIST val_list->arr
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;
int n, k, a, b, l[500001], ps[500002];
pair<int, int> ps_[500002];
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].first += ps_[x - 1].first + l[x];
        ps_[x].first %= k;
        ps_[x].second = x;
        ps[x] = ps_[x].first;
    }
    sort(ps_ + 1, ps_ + n + 1);
    ps_[n + 1] = {-1, -1};
    int current = ps_[1].first;
    int len = 1;
    int **arr = &vals[current].arr;
    for(int x = 2;x<=(n+1);x++){
        if(ps_[x].first == current)
            len++;
        else{
            *arr = new int[len];
            for(int y = x - len, z = 0;y!=x;y++){
                (*arr)[z++] = ps_[y].second;
            }
            vals[current].nr = len;
            current = ps_[x].first;
            len = 1;
            arr = &vals[current].arr;
        }
    }
    for(int x = 1; x<=n; x++) {
        if(vals.find(ps[x - 1]) == vals.end())
            continue;
        vallist * val_list = &vals[ps[x - 1]];
        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;
}