Pagini recente » Cod sursa (job #3157763) | Cod sursa (job #2790813) | Cod sursa (job #2204660) | Cod sursa (job #3247826) | Cod sursa (job #3319319)
//https://www.nerdarena.ro/problema/secv5
#include <bits/stdc++.h>
using namespace std;
ifstream fin("secv5.in");
ofstream fout("secv5.out");
template<typename T>
class FastIO
{
private:
ifstream& fin;
ofstream& fout;
static constexpr int IN_BUF_SIZE = 1 << 24;
static constexpr int OUT_BUF_SIZE = 1 << 24;
char inBuf[IN_BUF_SIZE];
int inPos = IN_BUF_SIZE, inBytes = IN_BUF_SIZE;
char outBuf[OUT_BUF_SIZE];
int outPos = 0;
inline char getChar()
{
if (inPos == inBytes)
{
fin.read(inBuf, IN_BUF_SIZE);
inBytes = fin.gcount();
inPos = 0;
if (inBytes == 0)
return -1;
}
return inBuf[inPos++];
}
inline void writeChar(char c)
{
if (outPos == OUT_BUF_SIZE)
{
fout.write(outBuf, outPos);
outPos = 0;
}
outBuf[outPos++] = c;
}
public:
FastIO(ifstream& finStream, ofstream& foutStream) : fin(finStream), fout(foutStream) {}
T read()
{
char c;
bool neg = false;
do
{
c = getChar();
if (c == -1)
return -1;
} while ((c < '0' || c > '9') && c != '-');
if (c == '-')
{
neg = true;
c = getChar();
}
T res = 0;
while (c >= '0' && c <= '9')
{
res = res * 10 + (c - '0');
c = getChar();
}
return neg ? -res : res;
}
void write(T x)
{
if (x < 0)
{
writeChar('-');
x = -x;
}
char temp[20];
int len = 0;
do
{
temp[len++] = '0' + x % 10;
x /= 10;
} while (x);
while (len--)
writeChar(temp[len]);
}
void flush()
{
if (outPos > 0)
{
fout.write(outBuf, outPos);
outPos = 0;
}
}
};
constexpr int NRMAX = 1 << 20;
constexpr int MOD = 666013;
int n;
int64_t v[NRMAX + 5];
vector<pair<int64_t, int>> h[MOD];
bool adg(int64_t x)
{
int nr = x % MOD;
for (auto& it : h[nr])
{
if (it.first == x)
{
++it.second;
return false;
}
}
h[nr].push_back(make_pair(x, 1));
return true;
}
bool scad(int64_t x)
{
int nr = x % MOD;
for (auto it = h[nr].begin(); it != h[nr].end(); ++it)
{
if (it->first == x)
{
--it->second;
if(it->second == 0)
{
int i = it - h[nr].begin();
swap(h[nr][i], h[nr].back());
h[nr].pop_back();
return true;
}
return false;
}
}
return false;
}
int64_t rezolvare(int lun)
{
for(int i = 0; i < MOD; ++i)
h[i].clear();
int i = 1, k = 0, j;
int64_t rez = 0;
for (j = 1; j <= n; j++)
{
if (adg(v[j]) == true)
{
//cout<<i<<" "<<j<<"\n";
++k;
while (k > lun)
{
if(scad(v[i]) == true)
--k;
++i;
}
}
rez += (j - i);
}
//cout<<rez<<" ";
return rez;
}
int main()
{
int l, u, i;
FastIO<int64_t> io(fin, fout);
n = io.read();
l = io.read();
u = io.read();
for (i = 1; i <= n; i++)
{
v[i] = io.read();
}
int64_t rez = rezolvare(u) - rezolvare(l - 1);
io.write(rez);
io.flush();
return 0;
}