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 ...
随机推荐
- Fiddler中显示IP方法
如何在fiddler查看到请求的IP地址?就像下面这样 打开fiddler, 快捷键Ctrl+R 或者 菜单->Rules->Customize Rules…,然后在CustomRul ...
- Spring乱码问题解决方案
请求乱码 GET请求乱码: 原因:请求参数带在url地址上.url地址什么时候解析? tomcat收到请求对url进行编解码(ISO8859-1) 解决方案:在tomcat的8080端口配置出加上 U ...
- 时间复杂度和大O表示法
大O表示法:称一个函数g(n)是O(f(n)),当且仅当存在常数c>0和n0>=1,对一切n>n0均有|g(n)|<=c|f(n)|成立,也称函数g(n)以f(n)为界或者称g ...
- 【POJ2226】Muddy Fields
题目大意:给定一个 N*M 的图,图中有一些格子不能被任何东西覆盖,现有一些宽度为 1,长度任意的骨牌覆盖这些可以被覆盖的格子,骨牌之间可以重叠,求将所有可以被覆盖的格子覆盖所需的最小骨牌数是多少. ...
- [luogu3939][数颜色]
题目链接 思路 对于每一种颜色都建立一个动态开点线段树.然后每次查询的时候就去这个颜色的线段树上查询就行了.修改之后不要忘记交换颜色. 这个题目数据有点强.抄了个比较快的读入优化才卡过去. 代码 /* ...
- (转)ZooKeeper的Znode剖析
ZooKeeper的Znode剖析 https://blog.csdn.net/lihao21/article/details/51810395 根据节点的存活时间,可以对节点划分为持久节点和临时节点 ...
- python操作excel文件一(xlrd读取文件)
一般做接口测试,会把参数和一些数据放入excel表中,这样就不会重新编译代码,提高效率.一般如何操作呢?接下来跟着步骤一起学习吧 执行步骤: 1.首先要安装 xlrd这个模块,用 pip instal ...
- Good Bye 2018 B. New Year and the Treasure Geolocation
传送门 https://www.cnblogs.com/violet-acmer/p/10201535.html 题意: 在二维空间中有 n 个 obelisk 点,n 个 p 点: 存在坐标T(x, ...
- TODO java疑问
TODOjava 疑惑-关于方法调用的参数是基本类型和引用类型的差别 class DataWrap { int a; int b; } public class ReferenceTransferTe ...
- c#UDP协议
UDP协议是不可靠的协议,传输速率快 服务器端: using System; using System.Collections.Generic; using System.Linq; using Sy ...