codeforces 220B . Little Elephant and Array 莫队+离散化
传送门:https://codeforces.com/problemset/problem/220/B
题意:
给你n个数,m次询问,每次询问问你在区间l,r内有多少个数满足其值为其出现的次数
题解:
莫队算法
思路:每次区间向外扩的更新的过程中,检查该位置的数ai的出现次数是否已经达到ai或ai+1,以判断是否要更新结果。同理,区间收缩的时候判断ai出现次数是否达到ai或ai-1。
因为数据范围为1e9,所以需要离散化,方便记录每种数出现的次数
代码:
/**
 *        ┏┓    ┏┓
 *        ┏┛┗━━━━━━━┛┗━━━┓
 *        ┃       ┃  
 *        ┃   ━    ┃
 *        ┃ >   < ┃
 *        ┃       ┃
 *        ┃... ⌒ ...  ┃
 *        ┃       ┃
 *        ┗━┓   ┏━┛
 *          ┃   ┃ Code is far away from bug with the animal protecting          
 *          ┃   ┃   神兽保佑,代码无bug
 *          ┃   ┃           
 *          ┃   ┃        
 *          ┃   ┃
 *          ┃   ┃           
 *          ┃   ┗━━━┓
 *          ┃       ┣┓
 *          ┃       ┏┛
 *          ┗┓┓┏━┳┓┏┛
 *           ┃┫┫ ┃┫┫
 *           ┗┻┛ ┗┻┛
 */
// warm heart, wagging tail,and a smile just for you!
//
//                            _ooOoo_
//                           o8888888o
//                           88" . "88
//                           (| -_- |)
//                           O\  =  /O
//                        ____/`---'\____
//                      .'  \|     |//  `.
//                     /  \|||  :  |||//  \
//                    /  _||||| -:- |||||-  \
//                    |   | \\  -  /// |   |
//                    | \_|  ''\---/''  |   |
//                    \  .-\__  `-`  ___/-. /
//                  ___`. .'  /--.--\  `. . __
//               ."" '<  `.___\_<|>_/___.'  >'"".
//              | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//              \  \ `-.   \_ __\ /__ _/   .-` /  /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                            `=---='
//        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//                     佛祖保佑      永无BUG
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define ls rt<<1
#define rs rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********\n")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n"
const double eps = 1e-8;
const int mod = 1e9 + 7;
const int maxn = 3e5 + 5;
const int INF = 0x3f3f3f3f;
const LL INFLL = 0x3f3f3f3f3f3f3f3f;
int a[maxn];
int pos[maxn];
struct node {
    int l, r, id;
} q[maxn];
bool cmp(node a, node b) {
    if(pos[a.l] == pos[b.l]) return a.r < b.r;
    return pos[a.l] < pos[b.l];
}
int b[maxn];
int cnt;
int Ans = 0;
int id[maxn];
int ans[maxn];
int vis[maxn];
int get_id(int x) {
    return lower_bound(b + 1, b + 1 + cnt, x) - b;
}
void add(int x) {
    if(vis[id[x]] == a[x]) {
        Ans--;
    }
    vis[id[x]]++;
    if(vis[id[x]] == a[x]) {
        Ans++;
    }
}
void del(int x) {
    if(vis[id[x]] == a[x]) {
        Ans--;
    }
    vis[id[x]]--;
    if(vis[id[x]] == a[x]) {
        Ans++;
    }
}
int main() {
#ifndef ONLINE_JUDGE
    FIN
#endif
    int n, m;
    scanf("%d%d", &n, &m);
    int sz = sqrt(n);
    for(int i = 1; i <= n; i++) {
        scanf("%d", &a[i]);
        b[i] = a[i];
        pos[i] = i / sz;
    }
    for(int i = 1; i <= m; i++) {
        scanf("%d%d", &q[i].l, &q[i].r);
        q[i].id = i;
    }
    sort(q + 1, q + 1 + m, cmp);
    sort(b + 1, b + n + 1);
    cnt = unique(b + 1, b + n + 1) - b - 1;
    for(int i = 1; i <= n; i++) {
        id[i] = get_id(a[i]);
    }
    int L = 1, R = 0;
    a[0] = -1;
    for(int i = 1; i <= m; i++) {
        while(R < q[i].r) {
            R++;
            add(R);
        }
        while(L < q[i].l) {
            del(L);
            L++;
        }
        while(R > q[i].r) {
            del(R);
            R--;
        }
        while(L > q[i].l) {
            L--;
            add(L);
        }
        ans[q[i].id] = Ans;
    }
    for(int i = 1; i <= m; i++) {
        printf("%d\n", ans[i]);
    }
    return 0;
}
/*
1
0
2
1
1
3*/
												
											codeforces 220B . Little Elephant and Array 莫队+离散化的更多相关文章
- 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排 ...
 - 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在该区间内的出现次数,求 ...
 - CodeForces 86 D Powerful array 莫队
		
Powerful array 题意:求区间[l, r] 内的数的出现次数的平方 * 该数字. 题解:莫队离线操作, 然后加减位置的时候直接修改答案就好了. 这个题目中发现了一个很神奇的事情,本来数组开 ...
 - codeforces 86D,Powerful array 莫队
		
传送门:https://codeforces.com/contest/86/problem/D 题意: 给你n个数,m次询问,每次询问问你在区间l,r内每个数字出现的次数的平方于当前这个数的乘积的和 ...
 - CodeForces - 86D Powerful array (莫队)
		
题意:查询的是区间内每个数出现次数的平方×该数值的和. 分析:虽然是道莫队裸体,但是姿势不对就会超时.答案可能爆int,所以要开long long 存答案.一开始的维护操作,我先在res里减掉了a[p ...
 - D. Powerful array    莫队算法或者说块状数组  其实都是有点优化的暴力
		
莫队算法就是优化的暴力算法.莫队算法是要把询问先按左端点属于的块排序,再按右端点排序.只是预先知道了所有的询问.可以合理的组织计算每个询问的顺序以此来降低复杂度. D. Powerful array ...
 - CodeForces - 617E XOR and Favorite Number 莫队算法
		
https://vjudge.net/problem/CodeForces-617E 题意,给你n个数ax,m个询问Ly,Ry, 问LR内有几对i,j,使得ai^...^ aj =k. 题解:第一道 ...
 
随机推荐
- python之浮点型类型
			
浮点型:float 如3.14,2.88 class float(object): """ float(x) -> floating point number Co ...
 - app被Rejected 的各种原因翻译。这个绝对有用
			
1. Terms and conditions(法律与条款) 1.1 As a developer of applications for the App Store you are bound b ...
 - MacOS配置双网
			
目的 日常工作中,我们可能会同时需要用到公司的内网以及互联网,为了避免来回的切换,我们可以通过配置电脑的两个网卡来实现同时访问内网和互联网. 环境说明 互联网 无线网卡 网关 子网掩码 内网 有线网卡 ...
 - QT_OPENGL-------- 4.可编程管线绘制三角形
			
一.环境:qt下qmake编译首先在qt .pro文件中添加glew和glfw的链接 LIBS+= -L/usr/lib64 -lGLEW LIBS +=-L/usr/local/lib -lglfw ...
 - 随机数专题 Day08
			
package com.sxt.arraytest2; import java.util.Arrays; /* * 随机数专题 * Math类的random()方法 * m~n的随机数 * 公式:(i ...
 - 《C语言深度解剖》学习笔记之预处理
			
第3章 预处理 1.下面两行代码都是错的.因为注释先于预处理指令被处理,当这两行被展开成“//……”和“/*……*/”时,注释已处理完毕,所以出现错误 #define BSC // #define B ...
 - 9-1进程,进程池和socketserver
			
一 进程: # 什么是进程 : 运行中的程序,计算机中最小的资源分配单位# 程序开始执行就会产生一个主进程# python中主进程里面启动一个进程 —— 子进程# 同时主进程也被称为父进程# 父子进程 ...
 - Python语言的缺点
 - Streamy 使用RDBMS
 - oracle函数 LPAD(c1,n[,c2])
			
[功能]在字符串c1的左边用字符串c2填充,直到长度为n时为止 [参数]C1 字符串 n 追加后字符总长度 c2 追加字符串,默认为空格 [返回]字符型 [说明]如果c1长度大于n,则返回c1左边n个 ...