记录编号 191976 评测结果 AAAAAAAAAA
题目名称 [AHOI 2013] 作业 最终得分 100
用户昵称 Gravatarforever 是否通过 通过
代码语言 C++ 运行时间 15.347 s
提交时间 2015-10-09 14:13:22 内存使用 99.47 MiB
显示代码纯文本
  1. #include<cstdio>
  2. #include<cmath>
  3. #include<algorithm>
  4. using namespace std;
  5. struct dd{
  6. int l,r,id,A,B,belong;
  7. }jie[2000010];
  8. int belong[2000010],a[2000010],n,m,c[2000010],d[2000010],as[2000010],bs[2000010];
  9. int s[2000010];
  10. int lowbit(int x){
  11. return x&(-x);
  12. }
  13. bool comp(const dd a,const dd b){
  14. if(a.belong==b.belong) return a.r<b.r;
  15. return a.l<b.l;
  16. }
  17. int Que(int x,int y){
  18. int sum=0; x--;
  19. for(;y;y-=lowbit(y)) sum+=c[y];
  20. for(;x;x-=lowbit(x)) sum-=c[x];
  21. return sum;
  22. }
  23. int Quue(int x,int y){
  24. int sum=0;x--;
  25. for(;y;y-=lowbit(y)) sum+=d[y];
  26. for(;x;x-=lowbit(x)) sum-=d[x];
  27. return sum;
  28. }
  29. void update(int x,int po){
  30. for(int i=x;i<=n;i+=lowbit(i)) c[i]+=po;
  31. s[x]+=po;
  32. if(s[x]==0||(s[x]==1&&po>0)){
  33. for(int i=x;i<=n;i+=lowbit(i)) d[i]+=po;
  34. }
  35. }
  36. int main(){
  37. freopen("ahoi2013_homework.in","r",stdin);
  38. freopen("ahoi2013_homework.out","w",stdout);
  39. scanf("%d%d",&n,&m);
  40. a[0]=0;int bol=(int)sqrt(n+0.5);
  41. for(int i=1;i<=n;++i) {
  42. scanf("%d",&a[i]);
  43. belong[i]=(i+1)/bol+1;
  44. }
  45. for(int i=1;i<=m;++i){
  46. jie[i].id=i; scanf("%d%d%d%d",&jie[i].l,&jie[i].r,&jie[i].A,&jie[i].B);
  47. jie[i].belong=belong[jie[i].l];
  48. }
  49. sort(jie+1,jie+m+1,comp);
  50. int le=jie[1].l,re=jie[1].r;
  51. for(int i=le;i<=re;++i) update(a[i],1);
  52. as[jie[1].id]=Que(jie[1].A,jie[1].B);
  53. bs[jie[1].id]=Quue(jie[1].A,jie[1].B);
  54. for(int i=2;i<=m;++i){
  55. while(le>jie[i].l) update(a[--le],1);
  56. while(re<jie[i].r) update(a[++re],1);
  57. while(re>jie[i].r) update(a[re--],-1);
  58. while(le<jie[i].l) update(a[le++],-1);
  59. as[jie[i].id]=Que(jie[i].A,jie[i].B);
  60. bs[jie[i].id]=Quue(jie[i].A,jie[i].B);
  61. }
  62. for(int i=1;i<=m;++i) printf("%d %d\n",as[i],bs[i]);
  63. return 0;
  64. }