莫队算法/二分查找 FZU 2072 Count
题意:问区间内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的更多相关文章
- 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 ...
- 【bzoj2506】calc 根号分治+STL-vector+二分+莫队算法
题目描述 给一个长度为n的非负整数序列A1,A2,…,An.现有m个询问,每次询问给出l,r,p,k,问满足l<=i<=r且Ai mod p = k的值i的个数. 输入 第一行两个正整数n ...
- 「日常训练&知识学习」莫队算法(二):树上莫队(Count on a tree II,SPOJ COT2)
题意与分析 题意是这样的,给定一颗节点有权值的树,然后给若干个询问,每次询问让你找出一条链上有多少个不同权值. 写这题之前要参看我的三个blog:Codeforces Round #326 Div. ...
- SPOJ COT2 Count on a tree II 树上莫队算法
题意: 给出一棵\(n(n \leq 4 \times 10^4)\)个节点的树,每个节点上有个权值,和\(m(m \leq 10^5)\)个询问. 每次询问路径\(u \to v\)上有多少个权值不 ...
- NBUT 1457 莫队算法 离散化
Sona Time Limit:5000MS Memory Limit:65535KB 64bit IO Format: Submit Status Practice NBUT 145 ...
- Codeforces617 E . XOR and Favorite Number(莫队算法)
XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: s ...
- 信心题--FUOJ2226(莫队算法)
http://acm.fzu.edu.cn/problem.php?pid=2226 信心题,还说是信心题,题目给的真好.但是一点都不像信心题. 又是一个新的算法,莫队算法 莫队算法是一个用数组就可以 ...
- NBUT 1457 Sona(莫队算法+离散化)
[1457] Sona 时间限制: 5000 ms 内存限制: 65535 K 问题描述 Sona, Maven of the Strings. Of cause, she can play the ...
- 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 ...
随机推荐
- [ C++ 快速高精度模板 ] [ BigN类 ] 大整数类 高精度 模板 BigInt FFT 快速傅里叶变换
[原创 转载请注明]瞎写的,如果代码有错,或者各位大佬有什么意见建议,望不吝赐教 更新日志: 对于规模较小的整数乘法使用$$O(n^2)$$方法,提高速度 modify()和operator[]的bu ...
- centos7 host修改
首先要说明,hostname和hosts文件没有必然联系,有不明白的同学可以先自行查阅资料了解hostname和hosts文件的关系.这里简要说明一下. hosts文件是dns服务的前身,网络刚开始出 ...
- 关于FFM (Field-aware Factorization Machines) 的几篇博客
深入FFM原理与实践 http://tech.meituan.com/deep-understanding-of-ffm-principles-and-practices.html#mjx-eqn-e ...
- POJ3728 The merchant解题报告
Description There are N cities in a country, and there is one and only one simple path between each ...
- cogs——1215. [Tyvj Aug11] 冗余电网
1215. [Tyvj Aug11] 冗余电网 ★ 输入文件:ugrid.in 输出文件:ugrid.out 简单对比 时间限制:1 s 内存限制:128 MB TYVJ八月月赛提高组 ...
- [Jexus系列] 一、安装并运行 Jexus
注意,本教程使用的jexus版本为5.8.3专业版,操作系统为 Ubunutu 16.04 64位 一.创建默认站点 不熟悉vim的可以看这个: vim超简单入门教程 sudo mkdir -p /v ...
- 码农小汪-spring框架学习之2-spring IoC and Beans 控制反转 依赖注入 ApplicationContext BeanFactory
spring Ioc依赖注入控制反转 事实上这个东西很好理解的,并非那么的复杂. 当某个Java对象,须要调用还有一个Java对象的时候(被依赖的对象)的方法时.曾经我们的做法是怎么做呢?主动的去创建 ...
- 关于jQuery写插件及其演示
关于写jQuery插件是非常有必要的.这是前端学习其中必须经过的一个过程 对于初次写插件先想清楚原理 (function($){ $.fn.yourName = function(opt ...
- jqury-validate表单验证
首先需要引入插件:jquery.validate.js这个插件. 然后对需要验证的表单实现js: $("#add-firewalls-form").validate({ submi ...
- iOS UI03_LTView
// // LTView.h // OC03_LTView // // Created by dllo on 15/7/31. // Copyright (c) 2015年 dllo. All ...