题意

给出一串数字以及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. Hdoj 1392.Surround the Trees 题解

    Problem Description There are a lot of trees in an area. A peasant wants to buy a rope to surround a ...

  2. Eclipse Memory Analyzer(MAT)使用

    https://user.qzone.qq.com/731573705/blog/1436389384 Eclipse Memory Analyzer(MAT)使用  一.OutOfMemoryErr ...

  3. centos7破解安装jira6.3.6(含Agile)

    应用场景:JIRA是Atlassian公司出品的项目与事务跟踪工具,被广泛应用于缺陷跟踪.客户服务.需求收集.流程审批.任务跟踪.项目跟踪 和敏捷管理等工作领域. 安装环境:centos7.3虚拟机 ...

  4. 第二届强网杯wp

    web web签到 利用了md5碰撞 payload为 param1 =%4d%c9%68%ff%0e%e3%5c%20%95%72%d4%77%7b%72%15%87%d3%6f%a7%b2%1b% ...

  5. SQL查询语句的进阶使用

    MySQL的进阶使用 sql语句一些功能的使用 导入现有大量数据文件步骤 1) 把*.sql文件拷贝到Linux某一位置(例如Desktop) 2) Linux命令行进入该位置 cd ~/Deskto ...

  6. java ee wildfly 批处理 job 工作

    配置批处理job,同时启动两个并行任务processData,syncTableTask,执行往后执行第三个任务job:playDurationTask. xml配置如下: <job id=&q ...

  7. 【CF242E】Xor Segment

    题目大意:给定一个长度为 N 的序列,支持两种询问,即:区间异或,区间求和. 题解:加深了对线段树的理解. 对于线段树维护的变量一定是易于 modify 的,对于查询的答案只需用维护的东西进行组合而成 ...

  8. 关于form与表单操作

    form表单自动提交规则 form表单中只有一个type=text的input,在input中按enter键,会自动提交: form表单中有多个type=text的input,且无type=submi ...

  9. fcntl F_GETFL

    F_GETFL 我的理解是file get flag #include <stdio.h>#include <fcntl.h>#include <unistd.h> ...

  10. Sublime text 3支持utf-8

    首先安装插件ConvertToUTF8和Codecs33 Sublime Text 3中文乱码问题解决(最新) 然后配置一些有用的用户设置 2. 使文档保存时自动存为 UTF-8 编码格式 默认情况下 ...