CodeForces - 220B Little Elephant and Array (莫队+离散化 / 离线树状数组)
题意:N个数,M个查询,求[Li,Ri]区间内出现次数等于其数值大小的数的个数。
分析:用莫队处理离线问题是一种解决方案。但ai的范围可达到1e9,所以需要离散化预处理。每次区间向外扩的更新的过程中,检查该位置的数ai的出现次数是否已经达到ai或ai+1,以判断是否要更新结果。同理,区间收缩的时候判断ai出现次数是否达到ai或ai-1。
另一种更高效的方法是使用树状数组离线处理查询。用一个vector数组维护每个ai以此出现的位置。显然ai>N的数不会对结果做出贡献,所以数组开1e5就足够了。树状数组的维护操作:从1道N递推,当ai的出现次数sz>=ai后,对其从右往左数的第ai次出现的位置+1;当出现次数sz>ai次后,需要对从右往左数第ai+1的位置减2;但是出现次数sz>ai+1次后,上述操作会多减去一部分,那么相应的就应该在从右往左数第ai+2次出现的位置上+1。
莫队代码:
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
using namespace std;
const int maxn=1e5+;
typedef long long LL;
int N,M,res;
struct Node{
int val;
int id;
bool operator < (const Node &p) const {return val<p.val;}
}a[maxn];
bool cmpid(const Node &x,const Node &y) {return x.id<y.id;} int b[maxn];
int pos[maxn],cnt[maxn],block; //块数
int ans[maxn];
int v[maxn]; //离散化
struct Query{
int L,R,id;
}Q[maxn];
bool cmp1(const Query& x,const Query& y){ //根据所属块的大小排序
if(pos[x.L]==pos[y.L]) return x.R<y.R;
return pos[x.L]<pos[y.L];
} void add(int pos)
{
int id = v[a[pos].id];
if(cnt[id]==b[pos]-) res++;
else if(cnt[id]==b[pos]) res--;
cnt[id]++;
} void pop(int pos)
{
int id = v[a[pos].id];
if(cnt[id]==b[pos]) res--;
else if(cnt[id]==b[pos]+) res++;
cnt[id]--;
} //#define LOCAL
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int T;
int cas=;
while(scanf("%d%d",&N,&M)==){
block = ceil(sqrt(1.0*N));
memset(cnt,,sizeof(cnt));
for(int i=;i<=N;++i){
scanf("%d",&a[i].val);
a[i].id = i;
b[i] = a[i].val;
pos[i]=i/block;
}
//离散化
sort(a+,a+N+);
int tag = ;
v[a[].id] = tag;
for(int i=;i<=N;++i){
if(a[i].val == a[i-].val) v[a[i].id] = tag;
else v[a[i].id] = ++tag;
}
sort(a+,a+N+,cmpid); for(int i=;i<=M;++i){
scanf("%d%d",&Q[i].L,&Q[i].R);
Q[i].id = i;
}
sort(Q+,Q+M+,cmp1);
res=;
int curL=,curR=;
for(int i=;i<=M;++i){
while(curL>Q[i].L) add(--curL);
while(curR<Q[i].R) add(++curR);
while(curL<Q[i].L) pop(curL++);
while(curR>Q[i].R) pop(curR--);
ans[Q[i].id] = res;
}
for(int i=;i<=M;++i)
printf("%d\n",ans[i]);
}
return ;
}
离线树状数组代码:
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
using namespace std;
const int maxn=1e5+;
typedef long long LL;
int N,M;
int a[maxn];
int ans[maxn];
struct Query{
int L,R,id;
bool operator < (const Query &q) const {return R<q.R;}
}Q[maxn]; int bit[maxn];
inline int lowbit(int x) {return x&(-x);} void add(int i,int val){
for(;i<=N;i+=lowbit(i)) bit[i]+=val;
} int sum(int i){
int res=;
for(;i>;i-=lowbit(i)) res+=bit[i];
return res;
} #define LOCAL
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int T;
int cas=;
while(scanf("%d%d",&N,&M)==){
vector<int> pos[maxn];
memset(bit,,sizeof(bit));
for(int i=;i<=N;++i){
scanf("%d",&a[i]);
} for(int i=;i<=M;++i){
scanf("%d%d",&Q[i].L,&Q[i].R);
Q[i].id = i;
}
sort(Q+,Q+M+); int la=;
for(int i=;i<=N;++i){
if(a[i]<=N){ //如果a[i]>N 那么不可能对结果有贡献
pos[a[i]].push_back(i); //记录出现的位置
int sz = pos[a[i]].size();
if(sz>=a[i]){
add(pos[a[i]][sz-a[i]],); //对从右往左数的第a[i]次出现的位置,加1
//若a[i]出现的次数大于a[i],从右往左数出现的第a[i]+1次的位置已经被加1,不能作出贡献的前缀被多加了2,所以减去2
if(sz>a[i]) add(pos[a[i]][sz-a[i]-],-);
//但是若a[i]出现的次数大于a[i]+1,那么之前的-2操作就需要“补偿回来”
if(sz>a[i]+) add(pos[a[i]][sz-a[i]-],);
}
}
while(la<=M && Q[la].R==i){
ans[Q[la].id] = sum(Q[la].R) - sum(Q[la].L-);
la++;
}
if(la>M) break;
}
for(int i=;i<=M;++i)
printf("%d\n",ans[i]);
}
return ;
}
CodeForces - 220B Little Elephant and Array (莫队+离散化 / 离线树状数组)的更多相关文章
- codeforces 220B . Little Elephant and Array 莫队+离散化
传送门:https://codeforces.com/problemset/problem/220/B 题意: 给你n个数,m次询问,每次询问问你在区间l,r内有多少个数满足其值为其出现的次数 题解: ...
- CodeForces - 375D Tree and Queries (莫队+dfs序+树状数组)
You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. We will ass ...
- 【序列莫队+二分答案+树状数组】POJ2104-K-th Number
[题目大意] 给出一个长度为n的序列和m组查询(i,j,k),输出[i,j]中的第k大数. [思路] 先离散化然后莫队分块.用树状数组来维护当前每个值的个数,然后对于每次询问二分答案即可. 又一次实力 ...
- SPOJ DQUERY - D-query (莫队算法|主席树|离线树状数组)
DQUERY - D-query Given a sequence of n numbers a1, a2, ..., an and a number of d-queries. A d-query ...
- Educational Codeforces Round 10 D. Nested Segments 离线树状数组 离散化
D. Nested Segments 题目连接: http://www.codeforces.com/contest/652/problem/D Description You are given n ...
- Codeforces Round #365 (Div. 2) D - Mishka and Interesting sum(离线树状数组)
http://codeforces.com/contest/703/problem/D 题意: 给出一行数,有m次查询,每次查询输出区间内出现次数为偶数次的数字的异或和. 思路: 这儿利用一下异或和的 ...
- Codeforces Round #510 (Div. 2) D. Petya and Array(离散化+反向树状数组)
http://codeforces.com/contest/1042/problem/D 题意 给一个数组n个元素,求有多少个连续的子序列的和<t (1<=n<=200000,abs ...
- Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum (离线树状数组+前缀xor)
题目链接:http://codeforces.com/contest/703/problem/D 给你n个数,m次查询,每次查询问你l到r之间出现偶数次的数字xor和是多少. 我们可以先预处理前缀和X ...
- Codeforces Round #229 (Div. 2) C. Inna and Candy Boxes 树状数组s
C. Inna and Candy Boxes Inna loves sweets very much. She has n closed present boxes lines up in a ...
随机推荐
- datagrid后台分页js
$(function () { gridbind(); bindData(); }); //表格绑定function gridbind() { $('#dg').datagrid({ title: ' ...
- php yaf框架扩展实践五——数据层
从狭义角度上来理解数据层就是数据库,比较广义的理解来看数据库.远程数据.文件等都可以看做数据层.项目初期的时候一般单一的数据库就可以了,随着流量的增大就要对数据层做很多的改进,例如增加从库分散读压力, ...
- Apollo 刨析:Localization
九月 30 2014 11:27 上午 admin 0 Comments 今天我们来看一看Apollo中的Localization Component. 本地化在Apollo中的使用 像这样的 ...
- 图表 Chart
工作中,需要实现如下的图表,查阅了不少的资料,问了不少的人,下面对下图表的实现代码做下讲解. 实现代码: chart1.Series.Clear();//清空图表中的序列,图表中有默认的序列 //ch ...
- asp.net页面触发事件panel滚动条高度不变的实现方法
asp.net页面按钮点击触发事件后panel滚动条非自动回到顶端,每次都要往下拉一下,关于这个问题的解决方法如下 此文是为解决asp.net页面按钮点击触发事件后panel滚动条非自动回到顶端的解决 ...
- 鼠标滑入滑出,输入框获得失去焦点后触发事件的N种方法之一二
熟悉position的用法 <!doctype html><html lang="en"> <head> <meta charset=&q ...
- php 对字符串的分块处理
//如何把以下的字符串分成每一个小块, 如:2017.2.14\n我们都是中国人\n小日本 这样的为一个小块 $str = '2017.2.14 我们都是中国人 小日本 2017.2.15 订单取消 ...
- iOS-@2x,@3x是什么意思
当我们在公司使用UI给出的图片时候,xxx.png,xxx@2x.png,xxx@3x.png的时候,不知道分别代表着什么! 本人也是菜鸟一枚,全凭自己尝试理解而已,在尝试中得出下面的结论: xxx. ...
- DB2 SQL方式查看版本
SELECT service_level, fixpack_num FROM TABLE (sysproc.env_get_inst_info()) as INSTANCEINFO
- 【BZOJ1283/3550】序列/[ONTAK2010]Vacation 最大费用流
[BZOJ1283]序列 Description 给出一个长度为 的正整数序列Ci,求一个子序列,使得原序列中任意长度为 的子串中被选出的元素不超过K(K,M<=100) 个,并且选出的元素之和 ...