2018牛客网暑期ACM多校训练营(第一场)J Different Integers(树状数组)
题意
给出一串数字以及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(树状数组)的更多相关文章
- 2018牛客网暑期ACM多校训练营(第二场)I- car ( 思维)
2018牛客网暑期ACM多校训练营(第二场)I- car 链接:https://ac.nowcoder.com/acm/contest/140/I来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 ...
- 2018牛客网暑期ACM多校训练营(第一场)D图同构,J
链接:https://www.nowcoder.com/acm/contest/139/D来源:牛客网 同构图:假设G=(V,E)和G1=(V1,E1)是两个图,如果存在一个双射m:V→V1,使得对所 ...
- 2018 牛客网暑期ACM多校训练营(第一场) E Removal (DP)
Removal 链接:https://ac.nowcoder.com/acm/contest/139/E来源:牛客网 题目描述 Bobo has a sequence of integers s1, ...
- 2018牛客网暑期ACM多校训练营(第十场)A Rikka with Lowbit (树状数组)
链接:https://ac.nowcoder.com/acm/contest/148/A 来源:牛客网 Rikka with Lowbit 时间限制:C/C++ 5秒,其他语言10秒 空间限制:C/C ...
- 2018牛客网暑期ACM多校训练营(第十场)J Rikka with Nickname(二分,字符串)
链接:https://ac.nowcoder.com/acm/contest/148/J?&headNav=acm 来源:牛客网 Rikka with Nickname 时间限制:C/C++ ...
- 2018牛客网暑期ACM多校训练营(第二场)J Farm(树状数组)
题意 n*m的农场有若干种不同种类作物,如果作物接受了不同种类的肥料就会枯萎.现在进行t次施肥,每次对一个矩形区域施某种类的肥料.问最后枯萎的作物是多少. 分析 作者:xseventh链接:https ...
- 2018牛客网暑期ACM多校训练营(第一场)B Symmetric Matrix(思维+数列递推)
题意 给出一个矩阵,矩阵每行的和必须为2,且是一个主对称矩阵.问你大小为n的这样的合法矩阵有多少个. 分析 作者:美食不可负064链接:https://www.nowcoder.com/discuss ...
- 2018牛客网暑期ACM多校训练营(第五场) F - take - [数学期望][树状数组]
题目链接:https://www.nowcoder.com/acm/contest/143/F 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K ...
- 2018牛客网暑期ACM多校训练营(第二场) J - farm - [随机数哈希+二维树状数组]
题目链接:https://www.nowcoder.com/acm/contest/140/J 时间限制:C/C++ 4秒,其他语言8秒 空间限制:C/C++ 262144K,其他语言524288K ...
- 2018牛客网暑期ACM多校训练营(第三场) A - PACM Team - [四维01背包][四约束01背包]
题目链接:https://www.nowcoder.com/acm/contest/141/A 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K ...
随机推荐
- 自定义chromium浏览器
自定义chromium浏览器 来源 https://chaopeng.me/blog/2018/08/17/how-to-develop-full-homebrew-browser.html 最近有 ...
- HDOJ 5667 Sequence//费马小定理 矩阵快速幂
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5667 题意:如题给了一个函数式,给你a,b,c,n,p的值,叫你求f(n)%p的值 思路:先对函数取以a为 ...
- [UOJ317]【NOI2017】游戏 题解
题意 小 L 计划进行 \(n\) 场游戏,每场游戏使用一张地图,小 L 会选择一辆车在该地图上完成游戏. 小 L 的赛车有三辆,分别用大写字母 A.B.C 表示.地图一共有四种,分别用小写字 ...
- 【Luogu3731】[HAOI2017]新型城市化(网络流,Tarjan)
[Luogu3731][HAOI2017]新型城市化(网络流,Tarjan) 题面 洛谷 给定一张反图,保证原图能分成不超过两个团,问有多少种加上一条边的方法,使得最大团的个数至少加上\(1\). 题 ...
- Nginx-Cluster 构建
nx-Cluster and ReverseProxyServer-----------ReProxy-------------------------Client-----------192.168 ...
- CodeVS1288埃及分数(IDA*)
在古埃及,人们使用单位分数的和(形如1/a的, a是自然数)表示一切有理数. 如:2/3=1/2+1/6,但不允许2/3=1/3+1/3,因为加数中有相同的. 对于一个分数a/b,表示方法有很多种,但 ...
- LVS+Keepalived搭建高可用负载均衡
应用环境: LVS负责多台WEB端的负载均衡(LB):Keepalived负责LVS的高可用(HA),这里介绍主备模型. 测试环境: 配置步骤: 1. 安装软件 在LVS-1和LVS-2两台主机上安装 ...
- mysql慢查询,死锁解决方案
1. 先使用root用户登录到MySQL中 2. 使用show processlist查看其查询速率 +----+------+-----------------+------+---------+- ...
- LINQ的增删改查写法&&组合查询
.ToList();//返回一个集合,包含查到的所有值: .First();//返回查到的第一条数据,如果查不到会报错: .FirstOrDefault();返回查到的第一条数据,差不到返回一个nul ...
- 第二十五篇-Android 应用资源
这里介绍android的一些资源文件. 以一个登录界面为例. layout.xml <?xml version="1.0" encoding="utf-8" ...