题解真的是太神奇了2333

用离线和树状数组(为什么感觉和HH的项链是的,什么鬼),比较巧妙的是他把整个数列分成几段。用一个vector来记录每个数出现的位置。一共就是data[a[i]][sz]---data[a[i]][sz-a[i]],data[a[i]][sz-a[i]]----data[a[i]][sz-a[i]-1],0----data[a[i]][sz-a[i]-1],第一个区间的数是1,第二个区间是-1,第三个是0,这样区间加减的话就直接出来了是不是成立一共有a[i]个a[i]而且,还可以往后递推,太神奇了2333

  1. #include<bits/stdc++.h>
  2. #define lowbit(x) x&(-x)
  3. #define LL long long
  4. #define N 200005
  5. #define M 1000005
  6. #define mod 1000000007LL
  7. #define inf 0x7ffffffff
  8. using namespace std;
  9. inline int ra()
  10. {
  11. int x=,f=; char ch=getchar();
  12. while (ch<'' || ch>''){if (ch=='-') f=-; ch=getchar();}
  13. while (ch>='' && ch<=''){x=x*+ch-''; ch=getchar();}
  14. return x*f;
  15. }
  16. int c[N],ans[N],a[N],n,m;
  17. struct node{
  18. int l,r,id;
  19. bool operator < (const node t) {return r<t.r;}
  20. }q[N];
  21. void add(int i, int v)
  22. {
  23. while (i<=n)
  24. {
  25. c[i]+=v;
  26. i+=lowbit(i);
  27. }
  28. }
  29. int ask(int x)
  30. {
  31. int sum=;
  32. while (x>)
  33. {
  34. sum+=c[x];
  35. x-=lowbit(x);
  36. }
  37. return sum;
  38. }
  39. vector<int> data[N];
  40. int main()
  41. {
  42. int sz; n=ra();m=ra();
  43. for (int i=; i<=n; i++) a[i]=ra();
  44. for (int i=; i<=m; i++)
  45. {
  46. q[i].l=ra(),q[i].r=ra();
  47. q[i].id=i;
  48. }
  49. sort(q+,q+m+);
  50. for (int i=,k=; i<=n; i++)
  51. {
  52. if (a[i]<=n)
  53. {
  54. data[a[i]].push_back(i);
  55. sz=data[a[i]].size();
  56. if (sz>=a[i])
  57. {
  58. add(data[a[i]][sz-a[i]],);
  59. if (sz>a[i]) add(data[a[i]][sz-a[i]-],-);
  60. if (sz>a[i]+) add(data[a[i]][sz-a[i]-],);
  61. }
  62. }
  63. while (q[k].r==i && k<=m)
  64. {
  65. ans[q[k].id]=ask(q[k].r)-ask(q[k].l-);
  66. k++;
  67. }
  68. for (int j=; j<=n; j++)
  69. printf("%d ",ask(j));
  70. cout<<endl;
  71. }
  72. for (int i=; i<=m; i++)
  73. printf("%d\n",ans[i]);
  74. return ;
  75. }

cf221 D. Little Elephant and Array的更多相关文章

  1. Codeforces 221 D. Little Elephant and Array

    D. Little Elephant and Array time limit per test 4 seconds memory limit per test 256 megabytes input ...

  2. AC日记——Little Elephant and Array codeforces 221d

    221D - Little Elephant and Array 思路: 莫队: 代码: #include <cmath> #include <cstdio> #include ...

  3. CodeForces 221D Little Elephant and Array

    Little Elephant and Array Time Limit: 4000ms Memory Limit: 262144KB This problem will be judged on C ...

  4. Codeforces Round #136 (Div. 1) B. Little Elephant and Array

    B. Little Elephant and Array time limit per test 4 seconds memory limit per test 256 megabytes input ...

  5. Sona && Little Elephant and Array && Little Elephant and Array && D-query && Powerful array && Fast Queries (莫队)

    vjudge上莫队专题 真的是要吐槽自己(自己的莫队手残写了2个bug) s=sqrt(n) 是元素的个数而不是询问的个数(之所以是sqrt(n)使得左端点每个块左端点的范围嘴都是sqrt(n)) 在 ...

  6. Codeforces 221d D. Little Elephant and Array

    二次联通门 : Codeforces 221d D. Little Elephant and Array /* Codeforces 221d D. Little Elephant and Array ...

  7. 『题解』Codeforces220B Little Elephant and Array

    更好的阅读体验 Portal Portal1: Codeforces Portal2: Luogu Description The Little Elephant loves playing with ...

  8. Little Elephant and Array CodeForces - 220B (莫队)

    The Little Elephant loves playing with arrays. He has array a, consisting of npositive integers, ind ...

  9. Codeforces 220B - Little Elephant and Array 离线树状数组

    This problem can be solve in simpler O(NsqrtN) solution, but I will describe O(NlogN) one. We will s ...

随机推荐

  1. Scrapy 使用 Item 封装数据、使用 Item Pipline处理数据

    1.Item 和 Field Scrapy 提供一下两个类,用户可以使用它们自定义数据类,封装爬取到的数据: (1)Item类 自定义数据类(如 BookItem)的基类 (2)Field 用来描述自 ...

  2. request DELETE 请求

    Django对于PUT/DELETE请求并没有像POST/GET那样有一个字典结构.我们需要手动处理request.body获取参数: 第一种方式: ujson.loads(request.body. ...

  3. java并发:初探sleep方法

    sleep与wait sleep是Thread方法,使得当前线程从运行态变为阻塞态.但它不会释放对象的锁. wait方法是Object方法,它的作用是使得当前拥有对象锁的线程从运行态变为阻塞态, 它会 ...

  4. java课堂第一次随机测试和课件课后动手动脑问题解决(2019-9-16 )

    一.课堂测试 1.课堂测试:花二十分钟写一个能自动生成30道小学四则运算题目的 “软件” 要求 (1)减法结果不能为负数 (2)乘法结果不得超过一百,除法结果必须为整数 (3)题目避免重复: (4)可 ...

  5. Django(十七):静态文件、中间件

    一.静态文件 参考:https://www.cnblogs.com/jiajiaba/p/10584329.html 参考:https://docs.djangoproject.com/zh-hans ...

  6. NSQ学习记录

    一.简介 NSQ是一个基于Go语言的开源的分布式实时消息平台,他的代码托管在GitHub上. NSQ可用于大规模系统的实时消息服务,它的设计目标是为在分布式环境下提供一个强大的去除中心化的分布式服务架 ...

  7. 第1节 IMPALA:4、5、linux磁盘的挂载和上传压缩包并解压

    第二步:开机之后进行磁盘挂载 分区,格式化,挂载新磁盘 磁盘挂载 df -lh fdisk -l 开始分区 fdisk /dev/sdb   这个命令执行后依次输 n  p  1  回车  回车  w ...

  8. {$DEFINE WANYI}

    var Form5: TForm5; {$DEFINE WANYI}implementation{$R *.dfm}procedure TForm5.Button1Click(Sender: TObj ...

  9. 苹果vs中国竞争者:瘦死的骆驼比马大?

    前不久,苹果调整2019年第一财季的营收指引,预计第一季度毛利率为38%,相关收入大约为55亿美元,全年总体营收约为840亿美元,运营开支约为87亿美元.针对2019年的运营状况,库克亲自给投资者写了 ...

  10. vue 【 子子组件 => 子组件 => 父组件 】 的事件和参数的传递

    1,子子组件  TodoItem.vue     <template>   <div class="todo-item" :class="{'is-co ...