Bzoj 3236: [Ahoi2013]作业 莫队,分块

时间:2021-10-16 11:48:05

3236: [Ahoi2013]作业

Time Limit: 100 Sec  Memory Limit: 512 MB
Submit: 1113  Solved: 428
[Submit][Status][Discuss]

Description

Bzoj 3236: [Ahoi2013]作业  莫队,分块

Input

Bzoj 3236: [Ahoi2013]作业  莫队,分块

Output

Bzoj 3236: [Ahoi2013]作业  莫队,分块

Sample Input

3 4
1 2 2
1 2 1 3
1 2 1 1
1 3 1 3
2 3 2 3

Sample Output

2 2
1 1
3 2
2 1

HINT

N=100000,M=1000000

Source

By wangyisong1996加强数据

题解:

莫队+分块乱搞。

 #include<bits/stdc++.h>
using namespace std;
#define MAXN 100010
#define MAXM 1000010
int block,sum[MAXN],tot[MAXN],num[],color[MAXN],pos[MAXN],ans1[MAXM],ans2[MAXM];
struct node
{
int l,r,a,b,id;
}q[MAXM];
int read()
{
int s=,fh=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')fh=-;ch=getchar();}
while(ch>=''&&ch<=''){s=s*+(ch-'');ch=getchar();}
return s*fh;
}
bool cmp(node aa,node bb)
{
if(pos[aa.l]==pos[bb.l])return aa.r<bb.r;
return aa.l<bb.l;
}
void Del(int C)
{
int cc=pos[C];
sum[cc]--;tot[C]--;
if(tot[C]==)num[cc]--;
}
void Add(int C)
{
int cc=pos[C];
sum[cc]++;tot[C]++;
if(tot[C]==)num[cc]++;
}
int getans1(int aa,int bb)
{
int p1=pos[aa],p2=pos[bb],gs=,i;
if(p1==p2)
{
for(i=aa;i<=bb;i++)gs+=tot[i];
return gs;
}
for(i=aa;i<=p1*block;i++)gs+=tot[i];
for(i=p1+;i<=p2-;i++)gs+=sum[i];
for(i=(p2-)*block+;i<=bb;i++)gs+=tot[i];
return gs;
}
int getans2(int aa,int bb)
{
int p1=pos[aa],p2=pos[bb],gs=,i;
if(p1==p2)
{
for(i=aa;i<=bb;i++)if(tot[i]!=)gs++;
return gs;
}
for(i=aa;i<=p1*block;i++)if(tot[i]!=)gs++;
for(i=p1+;i<=p2-;i++)gs+=num[i];
for(i=(p2-)*block+;i<=bb;i++)if(tot[i]!=)gs++;
return gs;
}
int write(int k)
{
if(k<){putchar('-');k=-k;}
if(k>)write(k/);
putchar(k%+'');
}
int main()
{
int n,m,i,L,R;
n=read();m=read();
block=(int)sqrt(n);
for(i=;i<=n;i++)color[i]=read(),pos[i]=(i-)/block+;
//block=(int)sqrt(n);
for(i=;i<=m;i++)q[i].l=read(),q[i].r=read(),q[i].a=read(),q[i].b=read(),q[i].id=i;
//for(i=1;i<=n;i++)pos[i]=(i-1)/block+1;
sort(q+,q+m+,cmp);
L=;R=;
//memset(tot,0,sizeof(tot));//当前枚举的区间中每种颜色的个数.
//memset(sum,0,sizeof(sum));//每一块的总共的数量.
//memset(num,0,sizeof(num));//每一块的颜色的数量.
for(i=;i<=m;i++)
{
while(L<q[i].l)
{
Del(color[L]);
L++;
}
while(L>q[i].l)
{
L--;
Add(color[L]);
}
while(R<q[i].r)
{
R++;
Add(color[R]);
}
while(R>q[i].r)
{
Del(color[R]);
R--;
}
ans1[q[i].id]=getans1(q[i].a,q[i].b);
ans2[q[i].id]=getans2(q[i].a,q[i].b);
}
for(i=;i<=m;i++){write(ans1[i]);putchar(' ');write(ans2[i]);putchar('\n');}//printf("%d %d\n",ans1[i],ans2[i]);
return ;
}