3236: [Ahoi2013]作业
Time Limit: 100 Sec Memory Limit: 512 MBSubmit: 1376 Solved: 553
[ Submit][ Status][ Discuss]
Description
Input
Output
Sample Input
3 4
1 2 2
1 2 1 3
1 2 1 1
1 3 1 3
2 3 2 3
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
1 1
3 2
2 1
HINT
N=100000,M=1000000
Source
放了好久还是没有想到学长说的树套树做法,最后还是写的莫队。。。。。
莫队的话应该就没什么好说的了,最开始还加了离散化,后来学长说题上有说明所有数在[1,n]范围内才去掉(我真是已经水到读不懂题了cry)
附AC代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<climits>
#include<queue>
#include<string>
#include<stack>
#include<map>
#include<set>
#define N 2000020
#define M 5000020
using namespace std;
int read()
{
int x=0,f=1;char ch;
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int n,m,B;
int nu[N],hash[N],tot,cnt;
int bel[N],a[N];
struct node{int l,r,s,t,id;}p[M];
bool operator < (node a,node b)
{return bel[a.l]==bel[b.l]?a.r<b.r:a.l<b.l;}
int lowbit(int x){return x&(-x);}
int C[2][N],num[N],ans[N][2];
void add(int f,int x,int val)
{while(x<=n){C[f][x]+=val;x+=lowbit(x);}}
int ask(int f,int x)
{int ret=0;while(x){ret+=C[f][x];x-=lowbit(x);}return ret;}
void adj(int x)
{
add(0,x,1);
if(!num[x])add(1,x,1);
num[x]++;
}
void del(int x)
{
add(0,x,-1);num[x]--;
if(!num[x])add(1,x,-1);
}
void solve()
{
int x=1,y=1;adj(a[1]);
for(int i=1;i<=m;i++)
{
while(x<p[i].l)del(a[x++]);
while(x>p[i].l)adj(a[--x]);
while(y<p[i].r)adj(a[++y]);
while(y>p[i].r)del(a[y--]);
int ax=p[i].s,bx=p[i].t;
ans[p[i].id][0]=ask(0,bx)-ask(0,ax-1);
ans[p[i].id][1]=ask(1,bx)-ask(1,ax-1);
}
}
int main()
{
// freopen("in.txt","r",stdin);
// freopen("my.txt","w",stdout);
n=read(),m=read();B=sqrt(n);
for(int i=1;i<=n;i++)
{
bel[i]=(i-1)/B+1;
a[i]=read();
}
for(int i=1;i<=m;i++)
{
p[i].l=read();p[i].r=read();
p[i].s=read();p[i].t=read();
p[i].id=i;
}sort(p+1,p+1+m);solve();
for(int i=1;i<=m;i++)printf("%d %d\n",ans[i][0],ans[i][1]);
}
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<string>
#include<climits>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
int n,m;
int main()
{
freopen("in.txt","w",stdout);
srand(time(0));n=100000,m=100000;
printf("%d %d\n",n,m);
for(int i=1;i<=n;i++)printf("%d ",rand()%n+1);
puts("");
for(int i=1;i<=m;i++)
{
int x=(rand()*10%n+1),y=rand()*10%n+1;
if(x>y)swap(x,y);
int a=rand()*10%n+1,b=rand()*10%n+1;
if(a>b)swap(a,b);
printf("%d %d %d %d\n",x,y,a,b);
}
}