题意:

给定n个数字, 然后给出m个区间, 求区间外其他数字的种类有多少。

分析:

将区间以r为基准升序排序, 每次处理pre~r的数字第一次出现的位置。

#include<bits/stdc++.h>
using namespace std;
const int maxN = 2e6 + ;
int a[maxN], bit[maxN], ans[maxN];
int n, q;
int lowbit(int x){
return x & -x;
}
int sum(int x){
int res = ;
while(x > ){
res += bit[x];
x -= lowbit(x);
}
return res;
}
void update(int x, int v){
while(x <= n){
bit[x] += v;
x += lowbit(x);
}
}
struct Interval{
int l , r, id;
bool operator <(const Interval& tmp)const {
return r < tmp.r;
}
}s[maxN];
int main(){
// freopen("1.txt","r", stdin);
while(~scanf("%d %d", &n, &q)){
for(int i = ; i <= n; i++){
scanf("%d", &a[i]);
a[n + i] = a[i];
bit[n + i] = bit[i] = ; //先初始化为0
}
for(int i = ; i < q; i++){
scanf("%d %d", &s[i].r, &s[i].l);
s[i].r += n;
s[i].id = i;
}
n *= ; sort(s, s + q);
map<int, int> mp;
int pre = ;
for(int i = ; i < q; i++){
int L = s[i].l , R = s[i].r, id = s[i].id; for(int j = pre; j <= R; j++){
int num = a[j];
if(mp.count(num)){
update(mp[num], -); //如果之前出现过, 那么消去之前的标记
}
update(j, ); //这个位置设为最后一次出现
mp[num] = j; //更新这个数最后一次出现的位置
// for(int i = 1; i <= R; i++) cout << bit[i] << " "; cout << "\n";
}
pre = R + ;
ans[id] = sum(R) - sum(L - );
}
for(int i = ; i < q; i++){
printf("%d\n", ans[i]);
}
}
}

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

  1. 牛客网暑期ACM多校训练营 第九场

    HPrefix Sum study from : https://blog.csdn.net/mitsuha_/article/details/81774727 k较小.分离x和k. 另外的可能:求a ...

  2. 牛客网暑期ACM多校训练营(第四场):A Ternary String(欧拉降幂)

    链接:牛客网暑期ACM多校训练营(第四场):A Ternary String 题意:给出一段数列 s,只包含 0.1.2 三种数.每秒在每个 2 后面会插入一个 1 ,每个 1 后面会插入一个 0,之 ...

  3. 牛客网暑期ACM多校训练营(第五场):F - take

    链接:牛客网暑期ACM多校训练营(第五场):F - take 题意: Kanade有n个盒子,第i个盒子有p [i]概率有一个d [i]大小的钻石. 起初,Kanade有一颗0号钻石.她将从第1到第n ...

  4. 牛客网 暑期ACM多校训练营(第二场)A.run-动态规划 or 递推?

    牛客网暑期ACM多校训练营(第二场) 水博客. A.run 题意就是一个人一秒可以走1步或者跑K步,不能连续跑2秒,他从0开始移动,移动到[L,R]的某一点就可以结束.问一共有多少种移动的方式. 个人 ...

  5. 牛客网 暑期ACM多校训练营(第一场)A.Monotonic Matrix-矩阵转化为格子路径的非降路径计数,Lindström-Gessel-Viennot引理-组合数学

    牛客网暑期ACM多校训练营(第一场) A.Monotonic Matrix 这个题就是给你一个n*m的矩阵,往里面填{0,1,2}这三种数,要求是Ai,j⩽Ai+1,j,Ai,j⩽Ai,j+1 ,问你 ...

  6. 牛客网暑期ACM多校训练营(第三场)H Diff-prime Pairs (贡献)

    牛客网暑期ACM多校训练营(第三场)H Diff-prime Pairs (贡献) 链接:https://ac.nowcoder.com/acm/contest/141/H来源:牛客网 Eddy ha ...

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

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

  8. Different Integers 牛客网暑期ACM多校训练营(第一场) J 离线+线状数组或者主席树

    Given a sequence of integers a1, a2, ..., an and q pairs of integers (l 1, r1), (l2, r2), ..., (lq, ...

  9. 牛客网暑期ACM多校训练营(第七场)Bit Compression

    链接:https://www.nowcoder.com/acm/contest/145/C 来源:牛客网 题目描述 A binary string s of length N = 2n is give ...

  10. 牛客网暑期ACM多校训练营(第一场) - J Different Integers(线段数组or莫队)

    链接:https://www.nowcoder.com/acm/contest/139/J来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 524288K,其他语言1048 ...

随机推荐

  1. eShopOnContainers 是一个基于微服务的.NET Core示例框架

    找到一个好的示例框架很难,但不是不可能.大多数是小型Todo风格的应用程序,通常基于SimpleCRUD.值得庆幸的是,Microsoft已经为eShopOnContainers创建了一个基于微服务的 ...

  2. 工作的时候 用到了 获取时间 DateTime 整理了一下

    获得当前系统时间: DateTime dt = DateTime.Now; Environment.TickCount可以得到“系统启动到现在”的毫秒值 DateTime now = DateTime ...

  3. Java之file操作

    File类既可以表示文件,也可以表示为文件夹 文件的创建.删除.重命名 1.文件的创建 File file=new File("new Hello.txt");//当前工程目录下 ...

  4. 错误之Illegal mix of collations for operation 'like'

    内容来自博客:https://www.cnblogs.com/install/p/4417527.html MySQL Illegal mix of collations for operation ...

  5. GDB 格式化结构体输出

    转载:http://blog.csdn.net/unix21/article/details/9991925 set print addressset print address on打开地址输出,当 ...

  6. EJB2.0版本的HelloWorld

    EJB2.0版本的HelloWorld   虽然EJB3.1已经出来了,可是EJB2.0的项目还需要维护啊.下面写个简单EJB2.0的HelloWorld程序,练练手.   环境: JBoss 4.0 ...

  7. 查询sqlserver数据库,表占用数据大小

     if exists(select 1 from tempdb..sysobjects where id=object_id('tempdb..#tabName') and xtype='u')dro ...

  8. Adding other views to UIButton

    Q: I want to add some views to UIButton, for example multiple UILabels, UIImages etc. One I add thos ...

  9. ubuntu16 安装elasticsearch6.3问题

    1.ubuntu16 安装elasticsearch6.3 elasticsearch 6.3需要的java的版本是1.8以上 安装java1.8 详见本博客的安装java https://www.c ...

  10. iOS打包上传app store各种问题解决总结

    问题1 this action could not be completed. try again 问题2 there was an error sending data to the iTunes ...