#include <iostream>
#include <map>
#include <fstream>
#include <unordered_map>
using namespace std;
const int NMAX=1e6;
int v[NMAX+5], sp[NMAX+5], n, p;
map <int, int> mp;
unordered_map <int, int> norm;
class SegTree
{
public:
int t[8*NMAX+5];
void actupdate (int nod, int st, int dr, int poz, int val)
{
if (st==dr)
{
t[nod]++;
}
else
{
int mij = (st+dr) >> 1;
if (poz<=mij)
actupdate (nod*2, st, mij, poz, val);
else actupdate (nod*2+1, mij+1, dr, poz, val);
t[nod]=t[nod*2]+t[nod*2+1];
}
}
void update (int poz, int val)
{
actupdate (1, 1, NMAX*2, poz, val);
}
int actquery (int nod, int st, int dr, int stq, int drq)
{
if (stq<=st && dr<=drq)
{
return t[nod];
}
else
{
int mij = (st+dr) >> 1;
if (drq<=mij)
return actquery (nod*2, st, mij, stq, drq);
if (stq>mij)
return actquery (nod*2+1, mij+1, dr, stq, drq);
return actquery (nod*2, st, mij, stq, drq)+actquery (nod*2+1, mij+1, dr, stq, drq);
}
}
int query (int st, int dr)
{
return actquery (1, 1, 2*NMAX, st, dr);
}
};
SegTree aint;
int main ()
{
cin >> n;
for (int i=1;i<=n;i++)
{
cin >> v[i];
sp[i]=sp[i-1]+v[i];
}
cin >> p;
for (int i=1;i<=n;i++)
{
mp[-sp[i-1]+p*i-p]++;
mp[p*i-sp[i]]++;
}
int cnt=0;
for (auto chestie : mp)
{
norm[chestie.first]=++cnt;
}
long long int ret=0;
for (int i=1;i<=n;i++)
{
aint.update (norm[-sp[i-1]+p*i-p], 1);
ret+=aint.query (norm[p*i-sp[i]], cnt);
}
cout << ret;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |