题意

给出一串数字以及q次查询,每次查询l,r],要求求出[1,l]和[r,n]的所有不相同的数字个数。

分析

先对数组进行倍增,变为两倍长,然后查询就变成一个完整的区间。离线处理,按r从小到大排序,数组从1到2n扫一遍,每次更新每种数最后出现的位置,用树状数组处理。把前一次出现位置在树状数组里面更新-1(由于r从小到大查询,为了正确求出[l,r]中的不同数字数,必须更新最新的同时把旧的删去,目的在于以最新的位置判断,避免重复),这次的位置更新+1,然后如果扫描到的i>=r则对查询区间进行求和查询。

#include<iostream>
#include<cmath>
#include<cstring>
#include<queue>
#include<vector>
#include<cstdio>
#include<algorithm>
#include<map>
#include<set>
#define rep(i,e) for(int i=0;i<(e);i++)
#define rep1(i,e) for(int i=1;i<=(e);i++)
#define repx(i,x,e) for(int i=(x);i<=(e);i++)
#define X first
#define Y second
#define PB push_back
#define MP make_pair
#define mset(var,val) memset(var,val,sizeof(var))
#define scd(a) scanf("%d",&a)
#define scdd(a,b) scanf("%d%d",&a,&b)
#define scddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define pd(a) printf("%d\n",a)
#define scl(a) scanf("%lld",&a)
#define scll(a,b) scanf("%lld%lld",&a,&b)
#define sclll(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define IOS ios::sync_with_stdio(false);cin.tie(0)
#define lc idx<<1
#define rc idx<<1|1
#define rson mid+1,r,rc
#define lson l,mid,lc
using namespace std;
typedef long long ll;
template <class T>
void test(T a) {
cout<<a<<endl;
}
template <class T,class T2>
void test(T a,T2 b) {
cout<<a<<" "<<b<<endl;
}
template <class T,class T2,class T3>
void test(T a,T2 b,T3 c) {
cout<<a<<" "<<b<<" "<<c<<endl;
}
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const ll mod = 1e9+;
int T;
void testcase() {
printf("Case %d: ",++T);
}
const int MAXN = 2e5+;
const int MAXM = ;
const double PI = acos(-1.0);
const double eps = 1e-; struct node{
int l,r,id;
bool operator <(const node &b)const{
return r<b.r;
}
}Q[MAXN]; int n,q;
int bit[MAXN],a[MAXN],ans[MAXN];
int last[MAXN]; int lowbit(int x){ return x&-x; }
void update(int x,int d){
while(x<=n){
bit[x]+=d;
x+=lowbit(x);
}
}
int query(int x){
int res=;
while(x>){
res+=bit[x];
x-=lowbit(x);
}
return res;
}
int query(int l,int r){
return query(r)-query(l-);
} int main() {
#ifdef LOCAL
freopen("data.in","r",stdin);
#endif // LOCAL
while(~scdd(n,q)){
for(int i=;i<=n;i++) scd(a[i]),a[i+n]=a[i]; for(int i=;i<q;i++){
scdd(Q[i].l,Q[i].r);
Q[i].id=i;
Q[i].l+=n;
swap(Q[i].l,Q[i].r);
}
n<<=;
sort(Q,Q+q);
mset(last,);
mset(bit,);
int k=;
for(int i=;i<=n&&k<q;i++){
if(last[a[i]]) update(last[a[i]],-);
last[a[i]]=i;
update(last[a[i]],);
while(k<q&&Q[k].r<=i){
ans[Q[k].id]=query(Q[k].l,Q[k].r);
k++;
}
}
for(int i=;i<q;i++){
printf("%d\n",ans[i]);
}
}
return ;
}

2018牛客网暑期ACM多校训练营(第一场)J Different Integers(树状数组)的更多相关文章

  1. 2018牛客网暑期ACM多校训练营(第二场)I- car ( 思维)

    2018牛客网暑期ACM多校训练营(第二场)I- car 链接:https://ac.nowcoder.com/acm/contest/140/I来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 ...

  2. 2018牛客网暑期ACM多校训练营(第一场)D图同构,J

    链接:https://www.nowcoder.com/acm/contest/139/D来源:牛客网 同构图:假设G=(V,E)和G1=(V1,E1)是两个图,如果存在一个双射m:V→V1,使得对所 ...

  3. 2018 牛客网暑期ACM多校训练营(第一场) E Removal (DP)

    Removal 链接:https://ac.nowcoder.com/acm/contest/139/E来源:牛客网 题目描述 Bobo has a sequence of integers s1, ...

  4. 2018牛客网暑期ACM多校训练营(第十场)A Rikka with Lowbit (树状数组)

    链接:https://ac.nowcoder.com/acm/contest/148/A 来源:牛客网 Rikka with Lowbit 时间限制:C/C++ 5秒,其他语言10秒 空间限制:C/C ...

  5. 2018牛客网暑期ACM多校训练营(第十场)J Rikka with Nickname(二分,字符串)

    链接:https://ac.nowcoder.com/acm/contest/148/J?&headNav=acm 来源:牛客网 Rikka with Nickname 时间限制:C/C++ ...

  6. 2018牛客网暑期ACM多校训练营(第二场)J Farm(树状数组)

    题意 n*m的农场有若干种不同种类作物,如果作物接受了不同种类的肥料就会枯萎.现在进行t次施肥,每次对一个矩形区域施某种类的肥料.问最后枯萎的作物是多少. 分析 作者:xseventh链接:https ...

  7. 2018牛客网暑期ACM多校训练营(第一场)B Symmetric Matrix(思维+数列递推)

    题意 给出一个矩阵,矩阵每行的和必须为2,且是一个主对称矩阵.问你大小为n的这样的合法矩阵有多少个. 分析 作者:美食不可负064链接:https://www.nowcoder.com/discuss ...

  8. 2018牛客网暑期ACM多校训练营(第五场) F - take - [数学期望][树状数组]

    题目链接:https://www.nowcoder.com/acm/contest/143/F 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K ...

  9. 2018牛客网暑期ACM多校训练营(第二场) J - farm - [随机数哈希+二维树状数组]

    题目链接:https://www.nowcoder.com/acm/contest/140/J 时间限制:C/C++ 4秒,其他语言8秒 空间限制:C/C++ 262144K,其他语言524288K ...

  10. 2018牛客网暑期ACM多校训练营(第三场) A - PACM Team - [四维01背包][四约束01背包]

    题目链接:https://www.nowcoder.com/acm/contest/141/A 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K ...

随机推荐

  1. codeforces553C Love Triangles

    题目链接:codeforces553C Love Triangles 我们来看一下对于一个合法三角形可能出现的边 我们发现,在确定了两边之后,第三条边是什么也就随之确定了 我们用\(1\)表示\(lo ...

  2. Ionic3报错Error: Uncaught (in promise): Error: StaticInjectorError

    ERROR Error: Uncaught (in promise): Error: StaticInjectorError[Geolocation]: StaticInjectorError[Geo ...

  3. IT人员必须掌握的10项软技能

    现代企业的IT部门员工在具备技术能力的同时,还需要掌握一些软技能.现在来看这些软技能其实并不是什么新鲜事物,只是IT人员所需的这些软技能的范围与数量正在不断增加着. IT人员需要具备哪些技术能力,要取 ...

  4. 【BZOJ5197】Gambling Guide (最短路,期望)

    [BZOJ5197]Gambling Guide (最短路,期望) 题面 BZOJ权限题 洛谷 题解 假设我们求出了每个点的期望,那么对于一个点,只有向期望更小的点移动的时候才会更新答案. 即转移是: ...

  5. 使用 sizeof 获取字符串数组的大小

    @2018-11-1 字符串组成的数组存放于指针数组中,使用 sizeof 获取数组大小 [验证] #include <stdio.h> #define BootScreen " ...

  6. day15-集合

    快捷键: 先定义name,age再利用快捷键生成,Alt+shift+s +c:空参 Alt+shift+s +o:有参 Alt+shift+s +r:set&get方法Ctrl+shift+ ...

  7. Java中数组判断元素存在几种方式比较详解

    1. 通过将数组转换成List,然后使用List中的contains进行判断其是否存在 public static boolean useList(String[] arr,String contai ...

  8. python 验证码识别

    一.python识别简单验证码: 代码: ''' func:实现简单验证码获取 ''' import pytesseract from PIL import Image #首先通过Image打开一个图 ...

  9. 洛谷P4316 绿豆蛙的归宿

    一眼看去,这不是高斯消元吗? 然后发现数据范围是100000... 然后发现是DAG...直接拓扑序递推即可. 边(x, y,z)的贡献是P(x) * z / out[x] #include < ...

  10. Win32 SDK:ListBox 为什么不整个 LB_SETTEXT

    Win32 & SDK: ListBox 有个 LB_GETTEXT,为什么没有 LB_SETTEXT,想修改指定Item的Text,还真不容易. 自已写了一个,支持单选和多选模式,部分代码: ...