Codeforces Round #136 (Div. 1) B. Little Elephant and Array(简单莫队)
来源:互联网 发布:网络管理软件哪个好 编辑:程序博客网 时间:2024/05/29 02:46
题意:给你n个数,m次询问,每次询问一个区间[l, r],问这个区间内元素出现个数等于元素本身的有几个。(n, m <= 1e5)
思路:裸的莫队,因为每个数比较大,离散化下就好了。
代码:
#include<bits/stdc++.h>using namespace std;typedef long long ll;const int maxn = 1e5+5;int n, m, unit, tmp;int a[maxn], aa[maxn], Hash[maxn];int num[maxn], ans[maxn];struct node{ int id, l, r, blk; bool operator <(const node &a) const { if(blk == a.blk) return r < a.r; else return blk < a.blk; }}op[maxn];void add(int id){ num[aa[id]]++; if(num[aa[id]] == a[id]) tmp++; if(num[aa[id]] == a[id]+1) tmp--;}void del(int id){ num[aa[id]]--; if(num[aa[id]] == a[id]) tmp++; if(num[aa[id]] == a[id]-1) tmp--;}void solve(){ tmp = 0; memset(num, 0, sizeof(num)); int l = 1, r = 0; for(int i = 1; i <= m; i++) { while(r < op[i].r) add(++r); while(r > op[i].r) del(r--); while(l < op[i].l) del(l++); while(l > op[i].l) add(--l); ans[op[i].id] = tmp; } for(int i = 1; i <= m; i++) printf("%d\n", ans[i]);}int main(void){ while(cin >> n >> m) { unit = (int)sqrt(n); for(int i = 1; i <= n; i++) scanf("%d", &a[i]), Hash[i] = a[i]; sort(Hash+1, Hash+1+n); int d = unique(Hash+1, Hash+1+n)-Hash-1; for(int i = 1; i <= n; i++) aa[i] = lower_bound(Hash+1, Hash+1+d, a[i])-Hash; for(int i = 1; i <= m; i++) scanf("%d%d", &op[i].l, &op[i].r), op[i].id = i, op[i].blk = op[i].l/unit; sort(op+1, op+1+m); solve(); } return 0;}
阅读全文
1 0
- Codeforces Round #136 (Div. 1) B. Little Elephant and Array(简单莫队)
- Codeforces Round #136 (Div. 1) B. Little Elephant and Array
- Codeforces Round #136 (Div. 1) B. Little Elephant and Array
- Codeforces Round #136 (Div. 2) D. Little Elephant and Array
- Codeforces Round #136 (Div. 2) D. Little Elephant and Array
- Codeforces Round #136 (Div. 2) Little Elephant and Array
- CodeForces Round #136(220B) - Little Elephant and Array
- Codeforces Round #136 (Div. 2) B. Little Elephant and Numbers
- Codeforces Round #129 (Div. 1) B. Little Elephant and Cards
- Codeforces Round #157 (Div. 1) B. Little Elephant and Elections
- Codeforces Round #129 (Div. 1) B. Little Elephant and Cards
- CodeForces 220B Little Elephant and Array 莫队算法
- Codeforces Round #129 (Div. 2) B. Little Elephant and Sorting
- (Codeforces Round #136 (Div. 2))A. Little Elephant and Function(递归分析,简单)
- Codeforces Round #136 (Div. 2) D. Little Elephant and Array 线段树 离线处理
- Codeforces 220B Little Elephant and Array
- CodeForces 220B Little Elephant and Array
- Codeforces Round #136 (Div. 2) / 221B Little Elephant and Numbers (数论)
- ROS下视频消息发布与订阅
- HDOJ 2077 汉诺塔IV
- 正则表达式
- 推荐研究互联网必读的10本书
- SDUT_3362_数据结构实验之图论六:村村通公路
- Codeforces Round #136 (Div. 1) B. Little Elephant and Array(简单莫队)
- 笔记5
- 国庆技术博客之MVP架构模式
- Android 文件模式
- 笔记6-数组
- HDOJ 2078 复习时间
- 全栈者角度--常用HTML标签整理
- FlowNet 论文笔记
- C++ MFC / VS2013 之九 (对话框:模态对话框及其弹出过程)