莫队算法/二分查找 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 ...
随机推荐
- [Nescafé 20] 玉蟾宫
★ 输入文件:jademoon.in 输出文件:jademoon.out 简单对比 时间限制:1 s 内存限制:128 MB [背景] 有一天,小猫rainbow和freda来到了湘西 ...
- jQuery插件之ajaxFileUpload(ajax文件上传)
一.ajaxFileUpload是一个异步上传文件的jQuery插件. 传一个不知道什么版本的上来,以后不用到处找了. 语法:$.ajaxFileUpload([options]) options参数 ...
- codeforces 691D(数据结构)
D. Swaps in Permutation time limit per test 5 seconds memory limit per test 256 megabytes input stan ...
- 洛谷——P1007 独木桥
P1007 独木桥 题目背景 战争已经进入到紧要时间.你是运输小队长,正在率领运输部队向前线运送物资.运输任务像做题一样的无聊.你希望找些刺激,于是命令你的士兵们到前方的一座独木桥上欣赏风景,而你留在 ...
- Servlet CDI 例子分析
@WebServlet("/cdiservlet") //以@WebServlet注释开头,注释指定相对于上下文根的URL模式,即在根目录下使用/cdiservlet来访问 pub ...
- Memcached与Spring集成的方式(待实践)
主要是基于这几种方式http://www.cnblogs.com/EasonJim/p/7624822.html去实现与Spring集成,而个人建议使用Xmemcached去集成好一些,因为现在官方还 ...
- CSS3的animation功能
旋转动画 <img src="https://facebook.github.io/react/img/logo.svg" class="App-logo" ...
- 【python】一些好的学习网址
http://www.cnblogs.com/BeginMan/p/3179302.html http://www.cnblogs.com/huxi/category/251137.html http ...
- Bundle格式文件的安装
安装VMware Workstation for Linux,文件是Bundle格式,安裝如下: 1 su要先取得root權限2hmod +x VMware-Workstation-Full-7.1. ...
- Scala-LIST/Tuple/Map
环境: CentOS 6.3 LIST(列表) 代码: $ cat list.scala var mylist = List(1,2,3) println(mylist) var mylist1 = ...