CodeForces 221D Little Elephant and Array
Little Elephant and Array
This problem will be judged on CodeForces. Original ID: 221D
64-bit integer IO format: %I64d Java class name: (Any)
The Little Elephant loves playing with arrays. He has array a, consisting of n positive integers, indexed from 1 to n. Let's denote the number with index i as ai.
Additionally the Little Elephant has m queries to the array, each query is characterised by a pair of integers lj and rj (1 ≤ lj ≤ rj ≤ n). For each query lj, rj the Little Elephant has to count, how many numbers x exist, such that number x occurs exactly x times among numbers alj, alj + 1, ..., arj.
Help the Little Elephant to count the answers to all queries.
Input
The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 105) — the size of array a and the number of queries to it. The next line contains n space-separated positive integers a1, a2, ..., an (1 ≤ ai ≤ 109). Next m lines contain descriptions of queries, one per line. The j-th of these lines contains the description of the j-th query as two space-separated integers lj and rj (1 ≤ lj ≤ rj ≤ n).
Output
In m lines print m integers — the answers to the queries. The j-th line should contain the answer to the j-th query.
Sample Input
7 2
3 1 2 2 3 3 7
1 7
3 4
3
1
Source
#include <bits/stdc++.h>
#define A first
#define B second
using namespace std;
typedef pair<int,int> pii;
const int maxn = ;
int tree[maxn<<];
inline void pushdown(int v){
if(tree[v]){
tree[v<<] += tree[v];
tree[v<<|] += tree[v];
tree[v] = ;
}
}
void update(int L,int R,int lt,int rt,int val,int v){
if(lt <= L && rt >= R){
tree[v] += val;
return;
}
pushdown(v);
int mid = (L + R)>>;
if(lt <= mid) update(L,mid,lt,rt,val,v<<);
if(rt > mid) update(mid + ,R,lt,rt,val,v<<|);
}
int query(int L,int R,int pos,int v){
if(L == R) return tree[v];
pushdown(v);
int mid = (L + R)>>;
if(pos <= mid) return query(L,mid,pos,v<<);
if(pos > mid) return query(mid + ,R,pos,v<<|);
}
int ans[maxn],a[maxn],cnt[maxn];
vector<int>pos[maxn];
struct QU{
int x,y,id;
bool operator<(const QU &rhs)const{
return y < rhs.y;
}
}q[maxn];
pii pre[maxn];
int main(){
int n,m;
while(~scanf("%d%d",&n,&m)){
memset(tree,,sizeof tree);
memset(cnt,,sizeof cnt);
for(int i = ; i < n; ++i)
scanf("%d",a + i);
for(int i = ; i < m; ++i){
scanf("%d%d",&q[i].x,&q[i].y);
q[i].id = i;
}
for(int i = ; i < maxn; ++i) pos[i].clear();
sort(q,q + m);
for(int i = , j = ; i < n; ++i){
if(a[i] <= n){
pos[a[i]].push_back(i + );
cnt[a[i]]++;
if(cnt[a[i]] == a[i]){
pre[a[i]] = pii(,pos[a[i]][]);
update(,n,pre[a[i]].A,pre[a[i]].B,,);
}else if(cnt[a[i]] > a[i]){
update(,n,pre[a[i]].A,pre[a[i]].B,-,);
pre[a[i]] = pii(pre[a[i]].B + ,pos[a[i]][cnt[a[i]] - a[i]]);
update(,n,pre[a[i]].A,pre[a[i]].B,,);
}
}
while(j < m && q[j].y == i + ){
ans[q[j].id] = query(,n,q[j].x,);
++j;
}
}
for(int i = ; i < m; ++i)
printf("%d\n",max(,ans[i]));
}
return ;
}
改点求段
#include <bits/stdc++.h>
#define A first
#define B second
using namespace std;
typedef pair<int,int> pii;
const int maxn = ;
int tree[maxn<<];
void update(int L,int R,int pos,int val,int v){
if(L == R){
tree[v] += val;
return;
}
int mid = (L + R)>>;
if(pos <= mid) update(L,mid,pos,val,v<<);
if(pos > mid) update(mid + ,R,pos,val,v<<|);
tree[v] = tree[v<<] + tree[v<<|];
}
int query(int L,int R,int lt,int rt,int v){
if(lt <= L && rt >= R) return tree[v];
int mid = (L + R)>>,ret = ;
if(lt <= mid) ret = query(L,mid,lt,rt,v<<);
if(rt > mid) ret += query(mid + ,R,lt,rt,v<<|);
return ret;
}
int a[maxn],cnt[maxn],ans[maxn];
vector<int>pos[maxn];
struct QU{
int x,y,id;
bool operator<(const QU &rhs)const{
return y < rhs.y;
}
}q[maxn];
int main(){
int n,m;
while(~scanf("%d%d",&n,&m)){
memset(tree,,sizeof tree);
memset(cnt,,sizeof cnt);
for(int i = ; i < maxn; ++i) pos[i].clear();
for(int i = ; i < n; ++i)
scanf("%d",a + i);
for(int i = ; i < m; ++i){
scanf("%d%d",&q[i].x,&q[i].y);
q[i].id = i;
}
sort(q,q + m);
for(int i = ,j = ; i < n; ++i){
if(a[i] <= n){
pos[a[i]].push_back(i + );
++cnt[a[i]];
if(cnt[a[i]] >= a[i]) update(,n,pos[a[i]][cnt[a[i]] - a[i]],,);
if(cnt[a[i]] >= a[i] + ) update(,n,pos[a[i]][cnt[a[i]] - a[i] - ],-,);
if(cnt[a[i]] > a[i] + ) update(,n,pos[a[i]][cnt[a[i]] - a[i] - ],,);
}
while(j < m && q[j].y == i + ){
ans[q[j].id] = query(,n,q[j].x,q[j].y,);
++j;
}
}
for(int i = ; i < m; ++i)
printf("%d\n",ans[i]);
}
return ;
}
CodeForces 221D Little Elephant and Array的更多相关文章
- 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 ...
- codeforces 220B . Little Elephant and Array 莫队+离散化
传送门:https://codeforces.com/problemset/problem/220/B 题意: 给你n个数,m次询问,每次询问问你在区间l,r内有多少个数满足其值为其出现的次数 题解: ...
- CodeForces - 220B Little Elephant and Array (莫队+离散化 / 离线树状数组)
题意:N个数,M个查询,求[Li,Ri]区间内出现次数等于其数值大小的数的个数. 分析:用莫队处理离线问题是一种解决方案.但ai的范围可达到1e9,所以需要离散化预处理.每次区间向外扩的更新的过程中, ...
- Codeforces - 220B Little Elephant and Array(莫队模板题)
题意: m次查询.每次查询范围[L,R]中出现次数等于该数字的数字个数. 题解: 由于分块,在每次询问中,同一块时l至多移动根号n,从一块到另一块也是最多2倍根号n.对于r,每个块中因为同一块是按y排 ...
- AC日记——Little Elephant and Array codeforces 221d
221D - Little Elephant and Array 思路: 莫队: 代码: #include <cmath> #include <cstdio> #include ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- [POJ1741] Tree【树分治 点分治】
传送门:http://poj.org/problem?id=1741 写的第一道树分治题,撒花纪念~ 对于每一对点对(i, j),它有三种情况: ① 其中一个是根节点.这种情况比较简单,直接加上就好了 ...
- Android SQLite(2)如何判断表是否已经存在
在sql语句中用 if not exists void create_table(){ SQLiteDatabase dbWireter = dbhelper.getWritableDatabase( ...
- 219 Contains Duplicate II 存在重复 II
给定一个整数数组和一个整数 k,判断数组中是否存在两个不同的索引 i 和 j,使 nums [i] = nums [j],并且 i 和 j 的绝对差值最大为 k. 详见:https://leetcod ...
- APP统计
APP统计就是统计用户使用app的各项指标,比如说日活跃量,页面打开次数,新增用户数量,用户年龄分布,用户地区分布,用户性别分布以及用户使用时间段等等.将统计出来的用户信息进行比对分析,可以服务公司的 ...
- AJPFX关于代码块的总结
代码块: { 执行语句; }(1) 当出现在局部位置时, 为局部代码块. 局部位置: 如语句块中, 函数中, 构造代码块中, 静 ...
- HTML5——loading
https://www.cnblogs.com/wangmeijian/p/4449150.html https://www.cnblogs.com/yunser/p/canvas-baidu-loa ...
- Farseer.net轻量级ORM开源框架 V1.x 入门篇:视图实体类映射
导航 目 录:Farseer.net轻量级ORM开源框架 目录 上一篇:Farseer.net轻量级ORM开源框架 V1.x 入门篇:表的数据操作 下一篇:Farseer.net轻量级ORM开源框 ...
- android 开源
http://blog.csdn.net/xiaoxiao_job/article/details/45196119?ref=myread MPAndroidChart https://github. ...
- Java常用工具类---image图片处理工具类、Json工具类
package com.jarvis.base.util; import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStre ...
- 怎样让Oracle的存储过程返回结果集
Oracle存储过程: CREATE OR REPLACE PROCEDURE getcity ( citycode IN VARCHAR2, ref_cursor OUT sys_refcursor ...