题目传送门

题意:问区间内x的出现的次数
分析:莫队算法:用一个cnt记录x的次数就可以了。还有二分查找的方法

代码:

#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std; const int MAXN = 1e5 + 10;
const int INF = 0x3f3f3f3f;
struct Data
{
int b, l, r, x;
int id;
}data[MAXN];
int a[MAXN];
int cnt[MAXN];
int ans[MAXN];
int n, q; bool cmp(Data x, Data y)
{
if (x.b == y.b) return x.r < y.r;
return x.b < y.b;
} void Modui(void)
{
memset (cnt, 0, sizeof (cnt)); int l = 1, r = 0;
for (int i=1; i<=q; ++i)
{
while (data[i].l < l) cnt[a[--l]]++;
while (data[i].l > l) cnt[a[l]]--, l++;
while (data[i].r > r) cnt[a[++r]]++;
while (data[i].r < r) cnt[a[r]]--, r--; ans[data[i].id] = cnt[data[i].x];
} for (int i=1; i<=q; ++i)
{
printf ("%d\n", ans[i]);
}
} int main(void) //FZU 2072 Count
{
while (scanf ("%d%d", &n, &q) == 2)
{
int block = (int) sqrt (n * 1.0);
for (int i=1; i<=n; ++i) scanf ("%d", &a[i]);
for (int i=1; i<=q; ++i)
{
scanf ("%d%d%d", &data[i].l, &data[i].r, &data[i].x);
data[i].b = data[i].l / block; data[i].id = i;
} sort (data+1, data+1+q, cmp); Modui ();
} return 0;
}

代码(二分查找):

#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
using namespace std; const int MAXN = 1e5 + 10;
const int INF = 0x3f3f3f3f;
int a[MAXN];
int n, q;
vector<int> cnt[MAXN]; int cal(int x, int r)
{
if (!cnt[x].size ()) return 0;
int pos = upper_bound (cnt[x].begin (), cnt[x].end (), r) - cnt[x].begin () - 1;
return pos;
} int main(void) //FZU 2072 Count
{
// freopen ("A.in", "r", stdin); while (scanf ("%d%d", &n, &q) == 2)
{
for (int i=1; i<=n; ++i)
{
scanf ("%d", &a[i]);
}
for (int i=1; i<=n; ++i) cnt[a[i]].clear ();
for (int i=1; i<=n; ++i) cnt[a[i]].push_back (i); for (int i=1; i<=q; ++i)
{
int l, r, x; scanf ("%d%d%d", &l, &r, &x);
printf ("%d\n", cal (x, r) - cal (x, l - 1));
}
} return 0;
}

  

莫队算法/二分查找 FZU 2072 Count的更多相关文章

  1. HDU 6278 - Just h-index - [莫队算法+树状数组+二分][2018JSCPC江苏省赛C题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6278 Time Limit: 6000/3000 MS (Java/Others) Memory Li ...

  2. 【bzoj2506】calc 根号分治+STL-vector+二分+莫队算法

    题目描述 给一个长度为n的非负整数序列A1,A2,…,An.现有m个询问,每次询问给出l,r,p,k,问满足l<=i<=r且Ai mod p = k的值i的个数. 输入 第一行两个正整数n ...

  3. 「日常训练&知识学习」莫队算法(二):树上莫队(Count on a tree II,SPOJ COT2)

    题意与分析 题意是这样的,给定一颗节点有权值的树,然后给若干个询问,每次询问让你找出一条链上有多少个不同权值. 写这题之前要参看我的三个blog:Codeforces Round #326 Div. ...

  4. SPOJ COT2 Count on a tree II 树上莫队算法

    题意: 给出一棵\(n(n \leq 4 \times 10^4)\)个节点的树,每个节点上有个权值,和\(m(m \leq 10^5)\)个询问. 每次询问路径\(u \to v\)上有多少个权值不 ...

  5. NBUT 1457 莫队算法 离散化

    Sona Time Limit:5000MS     Memory Limit:65535KB     64bit IO Format: Submit Status Practice NBUT 145 ...

  6. Codeforces617 E . XOR and Favorite Number(莫队算法)

    XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: s ...

  7. 信心题--FUOJ2226(莫队算法)

    http://acm.fzu.edu.cn/problem.php?pid=2226 信心题,还说是信心题,题目给的真好.但是一点都不像信心题. 又是一个新的算法,莫队算法 莫队算法是一个用数组就可以 ...

  8. NBUT 1457 Sona(莫队算法+离散化)

    [1457] Sona 时间限制: 5000 ms 内存限制: 65535 K 问题描述 Sona, Maven of the Strings. Of cause, she can play the ...

  9. Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 莫队算法

    E. XOR and Favorite Number 题目连接: http://www.codeforces.com/contest/617/problem/E Descriptionww.co Bo ...

随机推荐

  1. nyoj_176_队花的烦恼二_201404262008

    队花的烦恼二 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 ACM队队花C小+最近在X大OJ上做题,竟发现了一道做不出来的…水题!她快郁闷死了……也许是最近状态不太 ...

  2. Web 后端编程的几个关键点(总结中...)

    基础 服务端结构 服务器如何部署,负载均衡,代理技术,如何向B端提供服务? 分布式架构 与前端界面的交互形式 数据 CURD 表之间的关联 较为棘手 如何将一对多 多对多的概念进行 面向对象 描述 前 ...

  3. HDU——2874 Connections between cities

    Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  4. Minimum Depth of Binary Tree(二叉树DFS)

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  5. 【SQL Server 学习系列】-- 收缩数据库文件大小

    USE WebExam; GO ALTER DATABASE WebExam SET RECOVERY SIMPLE; GO -- 收缩文件到 1 MB. ); GO ALTER DATABASE W ...

  6. Access restriction: The type 'JPEGImageWriter' is not API

    报错: Access restriction: The type 'JPEGImageWriter' is not API due to restriction on required library ...

  7. NTKO在线office控件使用实例

    目录 1. NTKO在线office控件使用实例 1.1. 基础介绍 1.2. 基本原理 1.3. 实例 1.3.1. 打开.保存部分代码 1.3.2. 动态设值 1. NTKO在线office控件使 ...

  8. POJ 1384 POJ 1384 Piggy-Bank(全然背包)

    链接:http://poj.org/problem?id=1384 Piggy-Bank Time Limit: 1000MS Memory Limit: 10000K Total Submissio ...

  9. 字节序:Big Endian 和 Little Endian

    一.字节序 字节序,也就是字节的顺序,指的是多字节的数据在内存中的存放顺序. 在几乎所有的机器上,多字节对象都被存储为连续的字节序列.例如:如果C/C++中的一个int型变量 a 的起始地址是& ...

  10. jquery選取所有checkbox和判斷是否全部checkbox已經被勾選

    前言 勾選/取消勾選 全部勾選checkbox的時候 勾選/取消勾選 所有對應的checkbox 當所有對應checkbox有別勾選的時候, 全部勾選checkbox 也要被勾選 完整程式碼範例 前言 ...