#include <iostream>
#include <map>
#include <fstream>
#include <unordered_map>
#include <algorithm>
#include <cstdint>
#include <vector>
#define int long long
using namespace std;
const int NMAX=1e6;
int v[NMAX+5], sp[NMAX+5], n, p;
vector <int> vals;
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 cb (int val)
{
int st=0, dr=vals.size()-1, poz=0;
while (st<=dr)
{
int mij = (st+dr) >> 1;
if (vals[mij]<=val)
{
st=mij+1;
poz=mij;
}
else dr=mij-1;
}
return poz+1;
}
int32_t main ()
{
ios :: sync_with_stdio (0);
cin.tie (nullptr);
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++)
{
vals.push_back (-sp[i-1]+p*i-p);
vals.push_back (p*i-sp[i]);
}
sort (vals.begin(), vals.end());
long long ret=0;
for (int i=1;i<=n;i++)
{
aint.update (cb(-sp[i-1]+p*i-p), 1);
ret+=aint.query (cb (p*i-sp[i]), 2*n);
}
cout << ret;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |