#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cstring>
#include<cmath>
#define inf 0x3f3f3f3f
#define ll long long
#define N 1000009
using namespace std;
ll v[N],tag[N];
int L[N],R[N],blo[N];
int n,len;
ll read()
{
char ch=getchar();ll f=1,ret=0;
while(ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){ret=ret*10+ch-'0';ch=getchar();}
return ret;
}
void reset(int x)
{
if (tag[x]==-1) return;
for (int i=(x-1)*len+1;i<=min(n,x*len);i++) v[i]=tag[x];
tag[x]=-1;
}
ll solve(int l,int r,int d)
{
ll ret=0;int p=blo[l],q=blo[r];
reset(p);
for (int i=l;i<=min(r,R[l]);i++)
if (v[i]==d) ret++;else v[i]=d;
if (p!=q)
for (int i=L[r];i<=r;i++)
{
reset(q);
if (v[i]==d) ret++;else v[i]=d;
}
for (int i=p+1;i<=q-1;i++)
{
if (tag[i]!=-1)
if (tag[i]==d) ret+=len;
else tag[i]=d;
else
{
for (int j=(i-1)*len+1;j<=i*len;j++)
if (v[j]==d) ret++;
tag[i]=d;
}
}
return ret;
}
int main()
{
n=read();len=sqrt(n);
for (int i=1;i<=n;i++) v[i]=read();
for (int i=1;i<=len+1;i++) tag[i]=-1;
for (int i=1;i<=n;i++) {blo[i]=(i-1)/len+1;L[i]=(blo[i]-1)*len+1;R[i]=min(n,blo[i]*len);}
for (int i=1;i<=n;i++)
{
int l=read(),r=read();ll d=read();
printf("%lld\n",solve(l,r,d));
}
return 0;
}
【loj6284】数列分块练习 8
最新推荐文章于 2021-07-09 09:47:16 发布