题意

给出一串数字以及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. 【BZOJ3809】Gty的二逼妹子序列 莫队 分块

    题目描述 给你一个长度为\(n\)的数列,还有\(m\)个询问,对于每个询问\((l,r,a,b)\),输出区间\([l,r]\)有多少范围在\([a,b]\)的权值. \(n\leq 100000, ...

  2. MT【300】余弦的三倍角公式

    2017清华大学THUSSAT附加学科测试数学(二测)$\cos^5\dfrac{\pi}{9}+\cos^5\dfrac{5\pi}{9}+\cos^5\dfrac{7\pi}{9}$ 的值为___ ...

  3. 安装 linux-dash

    先看看软件的效果图,再介绍安装方法. 通过上图可以看到.软件可以实时监控CPU.内存.网络流量等相关信息,甚至可以监控到硬件信息安装方法:yum -y install httpd php zip un ...

  4. 自学Python4.7-生成器(方式一:生成器函数)

    自学Python之路-Python基础+模块+面向对象自学Python之路-Python网络编程自学Python之路-Python并发编程+数据库+前端自学Python之路-django 自学Pyth ...

  5. CodeForces - 589J(DFS)

    题目链接:http://codeforces.com/problemset/problem/589/J 题目大意:一个机器人打扫一个密闭的房间,房间由一个矩形构成,'*'表示家具,'.'表示该位置为空 ...

  6. Python3 与 C# 面向对象之~封装

      在线编程:https://mybinder.org/v2/gh/lotapp/BaseCode/master在线预览:http://github.lesschina.com/python/base ...

  7. limits.conf文件工作原理

    1. limits.conf 描述 limits.conf文件实际是Linux PAM(插入式认证模块,Pluggable Authentication Modules)中 pam_limits.so ...

  8. 如何在以太坊上搭建一个Dapp?

    原创: 前哨小兵甲 区块链前哨 昨天 策划|Tina作者|Mahesh Murthy俗话说,实践出真知!对于开发人员来说,最好的学习办法就是亲自动手做一个小项目.所以,接下来我们将会以一个投票程序为例 ...

  9. Arch Linux中安装Anaconda

    安装步骤 通过AUR安装yaourt -S anaconda 激活Anaconda环境source /opt/anaconda/bin/activate root 关闭Anaconda环境source ...

  10. 模拟赛 yjqb

    对于这种“不能交叉”的条件,不是很好处理.那么就考虑一下dp dp[i][j]表示,考虑A中用前i个,考虑连接B中用前j个,最大匹配.(类似LCS的DP) 转移:dp[i][j]=max(dp[i][ ...