SPOJ DQUERY 离线树状数组+离散化
题意:给出$(n <= 30000)$个数,$q <= 2e5$个查询,每个查询要求给出$[l,r]$内不同元素的个数
思路:这题可用主席树查询历史版本的方法做,感觉这个比较容易想到...但是主席树不太会用 其次可以用莫队分块的方法暴力过,再来就是使用树状数组维护不同数量的前缀和了,如果不使用离散化直接用map的话还会TLE... 通过维护当前位置上的数所记录的下标最靠右(即最近一次出现的位置),一边维护数量的前缀和,一边检查是否到达某个查询的右边界,再通过前缀性质减一下就得出了。
/** @Date : 2017-05-08 22:59:06
* @FileName: SPOJ DQUERY BIT or 主席树 or 莫队.cpp
* @Platform: Windows
* @Version : $Id$
*/
#include <bits/stdc++.h>
#define LL long long
#define PII pair
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8; vector v;
int a[30100];
int C[30100];
int pos[30100];
struct yuu
{
int l, r, m;
bool operator <(const yuu &a) const
{
return this->r < a.r;
}
}b[2*N];
int ans[2*N];
int n, q;
int cmp(yuu a, yuu b)
{
return a.r < b.r;
} void add(int x, int v)
{
while(x <= n)
{
C[x] += v;
x += (-x) & x;
}
return ;
} int getsum(int x)
{
int res = 0;
while(x)
{
res += C[x];
x -= (-x) & x;
}
return res;
} int main()
{
while(~scanf("%d", &n))
{
for(int i = 1; i <= n; i++)
{
scanf("%d", a + i);
v.PB(a[i]);
}
scanf("%d", &q);
for(int i = 0; i < q; i++)
{
scanf("%d%d", &b[i].l, &b[i].r);
b[i].m = i;
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(),v.end()), v.end());
for(int i = 1; i <= n; i++)
{
a[i] = lower_bound(v.begin(),v.end(), a[i]) - v.begin();
}
sort(b, b + q);
MMF(C);
MMF(pos);
int cnt = 0;
for(int i = 1; i <= n; i++)
{
if(!pos[a[i]])
{
add(i, 1);
pos[a[i]] = i;
}
else
{
add(i, 1);
add(pos[a[i]], -1);
pos[a[i]] = i;
}
while(cnt < q && i == b[cnt].r)
{
ans[b[cnt].m] = getsum(b[cnt].r) - getsum(b[cnt].l - 1);
cnt++;
}
}
for(int i = 0; i < q; i++)
printf("%d\n", ans[i]);
}
return 0;
}
SPOJ DQUERY 离线树状数组+离散化的更多相关文章
- Educational Codeforces Round 10 D. Nested Segments 离线树状数组 离散化
D. Nested Segments 题目连接: http://www.codeforces.com/contest/652/problem/D Description You are given n ...
- SPOJ DQUERY D-query 离线+树状数组
本来是想找个主席树的题目来练一下的,这个题目虽说可以用主席树做,但是用这个方法感觉更加叼炸天 第一次做这种离线方法,所谓离线,就在把所有询问先存贮起来,预处理之后再一个一个操作 像这个题目,每个操作要 ...
- SPOJ3267 D-query 离线+树状数组 在线主席树
分析:这个题,离线的话就是水题,如果强制在线,其实和离线一个思路,然后硬上主席树就行了 离线的代码 #include <iostream> #include <stdio.h> ...
- SPOJ 3267 D-query(离散化+在线主席树 | 离线树状数组)
DQUERY - D-query #sorting #tree English Vietnamese Given a sequence of n numbers a1, a2, ..., an and ...
- SPOJ DQUERY D-query (在线主席树/ 离线树状数组)
版权声明:本文为博主原创文章,未经博主允许不得转载. SPOJ DQUERY 题意: 给出一串数,询问[L,R]区间中有多少个不同的数 . 解法: 关键是查询到某个右端点时,使其左边出现过的数都记录在 ...
- SPOJ DQUERY - D-query (莫队算法|主席树|离线树状数组)
DQUERY - D-query Given a sequence of n numbers a1, a2, ..., an and a number of d-queries. A d-query ...
- CodeForces - 220B Little Elephant and Array (莫队+离散化 / 离线树状数组)
题意:N个数,M个查询,求[Li,Ri]区间内出现次数等于其数值大小的数的个数. 分析:用莫队处理离线问题是一种解决方案.但ai的范围可达到1e9,所以需要离散化预处理.每次区间向外扩的更新的过程中, ...
- POJ 3416 Crossing --离线+树状数组
题意: 给一些平面上的点,然后给一些查询(x,y),即以(x,y)为原点建立坐标系,一个人拿走第I,III象限的点,另一个人拿II,IV象限的,点不会在任何一个查询的坐标轴上,问每次两人的点数差为多少 ...
- HDU 2852 KiKi's K-Number(离线+树状数组)
题目链接 省赛训练赛上一题,貌似不难啊.当初,没做出.离线+树状数组+二分. #include <cstdio> #include <cstring> #include < ...
随机推荐
- vue视频插件VLC
VLC 仅支持windows下特定版本火狐浏览器--Firefox_ESR_55.3 <template> <object type='application/x-vlc-plugi ...
- HDU 5269 ZYB loves Xor I Trie树
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5269 bc:http://bestcoder.hdu.edu.cn/contests/con ...
- Alpha 冲刺8
队名:日不落战队 安琪(队长) 今天完成的任务 登录的数据post. okhttp学习第二弹. 明天的计划 okhttp学习第三弹. 个人信息界面数据get. 还剩下的任务 个人信息数据get. 遇到 ...
- Struts2(五)
以下内容是基于导入struts2-2.3.32.jar包来讲的 1.文件上传 A.单文件上传 <body> <form action="${pageContext.requ ...
- so加载报错:dlopen failed: couldn't map ... Permission denied
转自:https://blog.csdn.net/u013270444/article/details/60869376 问题描述: 我的应用当中集成了一个安全相关的sdk,而这个sdk中使用的so是 ...
- TP中循环遍历
循环遍历(重点) 在ThinkPHP中系统提供了2个标签来实现数组在模版中的遍历: volist标签.foreach标签. Volist语法格式: Foreach语法格式: 从上述的语法格式发现vol ...
- Java实现简单的RPC框架(美团面试)
一.RPC简介 RPC,全称为Remote Procedure Call,即远程过程调用,它是一个计算机通信协议.它允许像调用本地服务一样调用远程服务.它可以有不同的实现方式.如RMI(远程方法调用) ...
- 网页显示百度地图 Jquery
cshtml: <div class="modal" style="visibility: hidden"> <div id="ma ...
- 第94天:CSS3 盒模型详解
CSS3盒模型详解 盒模型设定为border-box时 width = border + padding + content 盒模型设定为content-box时 width = content所谓定 ...
- python的N个小功能(文件内容的匹配替换)
# -*- coding: utf-8 -*- """ Created on Fri Feb 17 20:25:05 2017 @author: who "&q ...