cf221 D. Little Elephant and Array
题解真的是太神奇了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
- #include<bits/stdc++.h>
- #define lowbit(x) x&(-x)
- #define LL long long
- #define N 200005
- #define M 1000005
- #define mod 1000000007LL
- #define inf 0x7ffffffff
- using namespace std;
- inline int ra()
- {
- int x=,f=; char ch=getchar();
- while (ch<'' || ch>''){if (ch=='-') f=-; ch=getchar();}
- while (ch>='' && ch<=''){x=x*+ch-''; ch=getchar();}
- return x*f;
- }
- int c[N],ans[N],a[N],n,m;
- struct node{
- int l,r,id;
- bool operator < (const node t) {return r<t.r;}
- }q[N];
- void add(int i, int v)
- {
- while (i<=n)
- {
- c[i]+=v;
- i+=lowbit(i);
- }
- }
- int ask(int x)
- {
- int sum=;
- while (x>)
- {
- sum+=c[x];
- x-=lowbit(x);
- }
- return sum;
- }
- vector<int> data[N];
- int main()
- {
- int sz; n=ra();m=ra();
- for (int i=; i<=n; i++) a[i]=ra();
- for (int i=; i<=m; i++)
- {
- q[i].l=ra(),q[i].r=ra();
- q[i].id=i;
- }
- sort(q+,q+m+);
- for (int i=,k=; i<=n; i++)
- {
- if (a[i]<=n)
- {
- data[a[i]].push_back(i);
- sz=data[a[i]].size();
- if (sz>=a[i])
- {
- add(data[a[i]][sz-a[i]],);
- if (sz>a[i]) add(data[a[i]][sz-a[i]-],-);
- if (sz>a[i]+) add(data[a[i]][sz-a[i]-],);
- }
- }
- while (q[k].r==i && k<=m)
- {
- ans[q[k].id]=ask(q[k].r)-ask(q[k].l-);
- k++;
- }
- for (int j=; j<=n; j++)
- printf("%d ",ask(j));
- cout<<endl;
- }
- for (int i=; i<=m; i++)
- printf("%d\n",ans[i]);
- return ;
- }
cf221 D. Little Elephant and Array的更多相关文章
- 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 ...
- AC日记——Little Elephant and Array codeforces 221d
221D - Little Elephant and Array 思路: 莫队: 代码: #include <cmath> #include <cstdio> #include ...
- CodeForces 221D Little Elephant and Array
Little Elephant and Array Time Limit: 4000ms Memory Limit: 262144KB This problem will be judged on C ...
- 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 ...
- 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)) 在 ...
- Codeforces 221d D. Little Elephant and Array
二次联通门 : Codeforces 221d D. Little Elephant and Array /* Codeforces 221d D. Little Elephant and Array ...
- 『题解』Codeforces220B Little Elephant and Array
更好的阅读体验 Portal Portal1: Codeforces Portal2: Luogu Description The Little Elephant loves playing with ...
- Little Elephant and Array CodeForces - 220B (莫队)
The Little Elephant loves playing with arrays. He has array a, consisting of npositive integers, ind ...
- 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 ...
随机推荐
- Scrapy 使用 Item 封装数据、使用 Item Pipline处理数据
1.Item 和 Field Scrapy 提供一下两个类,用户可以使用它们自定义数据类,封装爬取到的数据: (1)Item类 自定义数据类(如 BookItem)的基类 (2)Field 用来描述自 ...
- request DELETE 请求
Django对于PUT/DELETE请求并没有像POST/GET那样有一个字典结构.我们需要手动处理request.body获取参数: 第一种方式: ujson.loads(request.body. ...
- java并发:初探sleep方法
sleep与wait sleep是Thread方法,使得当前线程从运行态变为阻塞态.但它不会释放对象的锁. wait方法是Object方法,它的作用是使得当前拥有对象锁的线程从运行态变为阻塞态, 它会 ...
- java课堂第一次随机测试和课件课后动手动脑问题解决(2019-9-16 )
一.课堂测试 1.课堂测试:花二十分钟写一个能自动生成30道小学四则运算题目的 “软件” 要求 (1)减法结果不能为负数 (2)乘法结果不得超过一百,除法结果必须为整数 (3)题目避免重复: (4)可 ...
- Django(十七):静态文件、中间件
一.静态文件 参考:https://www.cnblogs.com/jiajiaba/p/10584329.html 参考:https://docs.djangoproject.com/zh-hans ...
- NSQ学习记录
一.简介 NSQ是一个基于Go语言的开源的分布式实时消息平台,他的代码托管在GitHub上. NSQ可用于大规模系统的实时消息服务,它的设计目标是为在分布式环境下提供一个强大的去除中心化的分布式服务架 ...
- 第1节 IMPALA:4、5、linux磁盘的挂载和上传压缩包并解压
第二步:开机之后进行磁盘挂载 分区,格式化,挂载新磁盘 磁盘挂载 df -lh fdisk -l 开始分区 fdisk /dev/sdb 这个命令执行后依次输 n p 1 回车 回车 w ...
- {$DEFINE WANYI}
var Form5: TForm5; {$DEFINE WANYI}implementation{$R *.dfm}procedure TForm5.Button1Click(Sender: TObj ...
- 苹果vs中国竞争者:瘦死的骆驼比马大?
前不久,苹果调整2019年第一财季的营收指引,预计第一季度毛利率为38%,相关收入大约为55亿美元,全年总体营收约为840亿美元,运营开支约为87亿美元.针对2019年的运营状况,库克亲自给投资者写了 ...
- vue 【 子子组件 => 子组件 => 父组件 】 的事件和参数的传递
1,子子组件 TodoItem.vue <template> <div class="todo-item" :class="{'is-co ...