Cod sursa(job #2524814)

Utilizator theo2003Theodor Negrescu theo2003 Data 16 ianuarie 2020 13:01:16
Problema Divk Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.36 kb
#include <fstream>
#include <algorithm>
using namespace std;
ifstream cin("divk.in");
ofstream cout("divk.out");
int n, k, a, b, l[500001];
pair<int, int> ps_[500002];
int _ps[500002], tmp = 0;
pair<int, pair<int, int> > starts[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].first += ps_[x - 1].first + l[x];
        ps_[x].first %= k;
        ps_[x].second = x;
    }
    for(int x = 0;x<=n;x++)
        _ps[x] = ps_[x].first;
    sort(ps_ + 1, ps_ + n + 1);
    for(int x = 1, current = -1;x<=n;x++){
        if(ps_[x].first != current){
            starts[tmp++] = {ps_[x].first, {x, x}};
            current = ps_[x].first;
        }else starts[tmp - 1].second.second = x;
    }
    for(int x = 1;x<=n;x++){
        auto vallist = *lower_bound(starts, starts + tmp, pair<int, pair<int, int> >(_ps[x - 1], pair<int, int>(0,0)));
        if(vallist.first != _ps[x - 1])
            continue;
        auto f = lower_bound(ps_ + vallist.second.first, ps_ + vallist.second.second + 1, pair<int, int>(_ps[x - 1], x + a - 1)), l = upper_bound(ps_ + vallist.second.first, ps_ + vallist.second.second + 1, pair<int, int>(_ps[x - 1], x + b - 1));
        sequences += distance(f, l);
    }
    cout<<sequences<<endl;
}