Codeforces - 220B Little Elephant and Array(莫队模板题)
题意:
m次查询。每次查询范围[L,R]中出现次数等于该数字的数字个数。
题解:
由于分块,在每次询问中,同一块时l至多移动根号n,从一块到另一块也是最多2倍根号n。对于r,每个块中因为同一块是按y排序,所以最多移动根号n;一共根号n个块。注意l和r要分开考虑。
要注意的是这道题需要离散一下数据。
- #include <iostream>
- #include <cstdio>
- #include <cmath>
- #include <algorithm>
- #include <cstring>
- using namespace std;
- const int maxn = 1e5+;
- int n, m;
- int l, r;
- int a[maxn], b[maxn], d[maxn];
- int blk;
- int blg[maxn];
- int ans;
- int sum[maxn];
- int out[maxn];
- struct node {
- int x, y, id;
- }c[maxn];
- bool cmp(node u, node v) {
- return (blg[u.x]==blg[v.x])?u.y<v.y:u.x<v.x;
- }
- void update(int x, int t) {
- sum[d[x]] += t;
- if(sum[d[x]] == a[x])
- ans++;
- else if(sum[d[x]] == a[x]+t)
- ans--;
- }
- int main() {
- while(~scanf("%d%d", &n, &m)) {
- for(int i = ; i <= n; i++) {
- scanf("%d", &a[i]);
- b[i] = a[i];
- }
- sort(b+, b+n+);
- int num = unique(b+, b+n+)-b;
- for(int i = ; i <= n; i++) d[i] = lower_bound(b+, b+num+, a[i])-b;
- blk = sqrt(n);
- for(int i = ; i <= n; i++) blg[i] = (i-)/blk+;
- for(int i = ; i <= m; i++) {
- scanf("%d%d", &c[i].x, &c[i].y);
- c[i].id = i;
- }
- sort(c+, c+m+, cmp);
- l = ; r = ;
- ans = ;
- memset(sum, , sizeof(sum));
- for(int i = ; i <= m; i++) {
- while(l < c[i].x) update(l++, -);
- while(l > c[i].x) update(--l, );
- while(r < c[i].y) update(++r, );
- while(r > c[i].y) update(r--, -);
- out[c[i].id] = ans;
- }
- for(int i = ; i <= m; i++) printf("%d\n", out[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 - 220B Little Elephant and Array (莫队+离散化 / 离线树状数组)
题意:N个数,M个查询,求[Li,Ri]区间内出现次数等于其数值大小的数的个数. 分析:用莫队处理离线问题是一种解决方案.但ai的范围可达到1e9,所以需要离散化预处理.每次区间向外扩的更新的过程中, ...
- CODEFORCES 340 XOR and Favorite Number 莫队模板题
原来我直接学的是假的莫队 原题: Bob has a favorite number k and ai of length n. Now he asks you to answer m queries ...
- 【洛谷2709】小B的询问(莫队模板题)
点此看题面 大致题意: 有一个长度为\(N\)的序列,每个数字在\(1\sim K\)之间,有\(M\)个询问,每个询问给你一个区间,让你求出\(\sum_{i=1}^K c(i)^2\),其中\(c ...
- [SDOI2009][bzoj1878] HH的项链 [莫队模板题]
题面: 传送门 思路: 就是一道莫队的模板题目...... 开一个1000000的数组记录每个数出现的次数,然后每次从1到0或者从0到1更新答案 莫队讲解看这里:莫队 Code: #include&l ...
- 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 86D - Powerful array(莫队算法)
题目链接:http://codeforces.com/problemset/problem/86/D 题目大意:给定一个数组,每次询问一个区间[l,r],设cnt[i]为数字i在该区间内的出现次数,求 ...
- P2709 小B的询问——普通莫队&&模板
普通莫队概念 莫队:莫涛队长发明的算法,尊称莫队.其实就是优化的暴力. 普通莫队只兹磁询问不支持修改,是离线的. 莫队的基本思想:就是假定我得到了一个询问区间[l,r]的答案,那么我可以在极短(通常是 ...
- D. Powerful array 莫队算法或者说块状数组 其实都是有点优化的暴力
莫队算法就是优化的暴力算法.莫队算法是要把询问先按左端点属于的块排序,再按右端点排序.只是预先知道了所有的询问.可以合理的组织计算每个询问的顺序以此来降低复杂度. D. Powerful array ...
随机推荐
- ES6初识-Decorator
开始先按照个插件 npm install babel-plugin-transform-decorators-lagacy --save-dev 1.扩充和修改类的行为 2.修改的行为@readonl ...
- Java中使用webSocket
Java中使用webSocket package com.yaoqi.controller.message; import javax.websocket.*; import javax.websoc ...
- ethereum(以太坊)(十一)--字节数组(一)
pragma solidity ^0.4.0; contract byte1{ /* 固定大小字节数组(Fixed-size byte arrays) 固定大小字节数组可以通过bytes1,bytes ...
- c# .net 3.5 4.0 4.5 5.0 6.0各个版本新特性战略规划总结【转载】
引用:http://blog.csdn.net/attilax/article/details/42014327 c# .net 3.5 4.0 各个版本新特性战略规划总结 1. ---------- ...
- bash:/usr/bin/mogod/:connot execute binary:exec fotmat error
前两天博主在安装mogodb的时候出现以下错误,很是郁闷,明明按照教程里面做的,怎么到最后 执行命令的时候出错了呢,以下为错误execute binary:exec fotmat error" ...
- 硬盘安装Windows Server 2008(解决系统盘符变成D盘)
硬盘安装Windows 2008系统方法 操作系统最好用的无疑是server 2003,但是现在Server 2003支持的软件越来越少,很多是故意不支持Server 2003了, 像php5.5以上 ...
- cf978E Bus Video System
The busses in Berland are equipped with a video surveillance system. The system records information ...
- url_maneger.py
coding=UTF-8 # url管理器 class urlManeger: def __init__(self): self.new_urls = set() self.old_urls = se ...
- 使用Yeoman自动构建Ember项目
Yeoman是什么? Yeoman按照官方说法,它不只是一个工具,还是一个工作流.它其实包括了三个部分yo.grunt.bower,分别用于项目的启动.文件操作.包管理. Yo: Yo是一个项目初始化 ...
- Myeclipse代码格式化的样式和保存自动格式化
第一种方法:下载格式化代码样式文件,参考这位老兄的方法(包含了保存自动格式化):http://blog.csdn.net/u010028869/article/details/49780515 下 ...