# BZOJ3236 [Ahoi2013]作业

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int N=2100000;
const int M=2001000;
int pos[N],c1[N],c2[N],c,num[N],ans1[N],ans2[N],n,m;
typedef struct seg{int lx,rx,pos,a,b;}seg; seg qu[M];
int lowbit(int x){return (x&(-x));}
int add(int x,int c){for (;x<=n;x+=lowbit(x)) c1[x]+=c;}
int addx(int x,int c){for (;x<=n;x+=lowbit(x)) c2[x]+=c;}
int sum(int x){c=0;for(;x;x-=lowbit(x))c+=c1[x];return c;}
int sumx(int x){c=0;for(;x;x-=lowbit(x))c+=c2[x];return c;}
int change(int x,int c)
}
int modify(seg x)
{ int l=x.a-1,r=x.b;
ans1[x.pos]=sum(r)-sum(l);
ans2[x.pos]=sumx(r)-sumx(l);
}
bool cmp(seg x,seg y)
{ if (pos[x.lx]!=pos[y.lx]) return pos[x.lx]<pos[y.lx];
return x.rx<y.rx;}

int main()
{ //freopen("1.in","r",stdin);
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++) scanf("%d",&num[i]);
for (int i=1;i<=m;i++)
{scanf("%d%d%d%d",&qu[i].lx,&qu[i].rx,&qu[i].a,&qu[i].b);
qu[i].pos=i;}
int k=int(sqrt(n));
for (int i=1;i<=n;i++) pos[i]=i/k+1;
sort(qu+1,qu+m+1,cmp);
int l=1,r=0;
for (int i=1;i<=m;i++)
{ seg x=qu[i];
while (r<x.rx) r++,change(r,1);
while (r>x.rx) change(r,-1),r--;
while (l>x.lx) l--,change(l,1);
while (l<x.lx) change(l,-1),l++;
modify(x);}
for (int i=1;i<=m;i++)
printf("%d %d\n",ans1[i],ans2[i]);
}
View Code

posted @ 2014-05-01 23:20  william's blog  阅读(358)  评论(0编辑  收藏  举报