链接:https://www.nowcoder.com/acm/contest/139/J
来源:牛客网

Given a sequence of integers a1, a2, ..., an and q pairs of integers (l1, r1), (l2, r2), ..., (lq, rq), find count(l1, r1), count(l2, r2), ..., count(lq, rq) where count(i, j) is the number of different integers among a1, a2, ..., ai, aj, aj + 1, ..., an.

输入描述:

The input consists of several test cases and is terminated by end-of-file.The first line of each test cases contains two integers n and q.The second line contains n integers a

1

, a

2

, ..., a

n

.The i-th of the following q lines contains two integers l

i

 and r

i

.

输出描述:

For each test case, print q integers which denote the result.
示例1

输入

复制

3 2
1 2 1
1 2
1 3
4 1
1 2 3 4
1 3

输出

复制

2
1
3

备注:

* 1 ≤ n, q ≤ 10

5

* 1 ≤ a

i

 ≤ n* 1 ≤ l

i

, r

i

 ≤ n* The number of test cases does not exceed 10.

不行了。n忘乘了2,卡了一小时。就是延长数组一倍,树状数组维护。
#include <bits/stdc++.h>
#define maxn 100005
using namespace std;
int n,m;
int vis[maxn*2],a[maxn*2],tree[maxn*2];
struct node
{int l,r,id;bool operator <(const node &b){return r<b.r;}
}query[maxn];
int lowbit(int x)
{return x&(-x);
}int sum(int x)
//int query(int x)
{int res=0;while(x){res+=tree[x];x-=lowbit(x);}return res;
}
void add(int x,int val)
{while(x<=n){tree[x]+=val;x+=lowbit(x);}
}
int ans[maxn];
int main()
{while(~scanf("%d%d",&n,&m)){   int i;memset(tree,0,sizeof(tree));memset(vis,0,sizeof(vis));for(i=1;i<=n;i++){scanf("%d",&a[i]);a[i+n]=a[i];}for(i=1;i<=m;i++){   int u,v;scanf("%d%d",&u,&v);query[i].l=v;query[i].r=u+n;query[i].id=i;}sort(query+1,query+1+m);int cur=1;n*=2;for(i=1;i<=m;i++){for(int j=cur;j<=query[i].r;j++){if(vis[a[j]]!=0){add(vis[a[j]],-1);}add(j,1);vis[a[j]]=j;}cur=query[i].r+1;//cout<<query[i].r<<" "<<sum(query[i].r)<<endl;//cout<<query[i].l<<" "<<sum(query[i].l-1)<<endl;ans[query[i].id]=sum(query[i].r)-sum(query[i].l-1);}for(i=1;i<=m;i++){printf("%d\n",ans[i]);}
}
}

转载于:https://www.cnblogs.com/zyf3855923/p/9357571.html

Different Integers相关推荐

  1. pandas将dataframe原有的数据列名称转化为整数数值列名称(convert dataframe column labelsl into integers)

    pandas将dataframe原有的数据列名称转化为整数数值列名称(convert dataframe column labelsl into integers) 目录 pandas将datafra ...

  2. [LeetCode] Sum of Two Integers 两数之和

    Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...

  3. “TypeError: list indices must be integers or slices, not str”有关报错解决方案

    "TypeError: list indices must be integers or slices, not str"有关报错解决方案 参考文章: (1)"TypeE ...

  4. python3 错误string indices must be integers 的解决方法

    这个错误意思是字符串的下标一定要是整数 出这种错误有多种可能,最形象直接的就是: a = '[abc]' print(a['0']) 有点pyhton基础的都知道下标怎么能是字符串'0',必须是整数0 ...

  5. list indices must be integers or slices, not tuple

    11年it研发经验,从一个会计转行为算法工程师,学过C#,c++,java,android,php,go,js,python,CNN神经网络,四千多篇博文,三千多篇原创,只为与你分享,共同成长,一起进 ...

  6. leetcode 371. Sum of Two Integers

    Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...

  7. MATLAB报错“Integers can only be raised to positive integral powers.“

    报错代码是下面这样的: I=imread('pout.jpg'); if size(I,3)>1  %判断如果是彩色图像,转换为灰度图     I=rgb2gray(I); end Gamma_ ...

  8. Divide Two Integers

    Divide two integers without using multiplication, division and mod operator. 思路:不能使用乘法除法以及取模运算来计算两个数 ...

  9. leetcode之Divide Two Integers

    题目:Divide Two Integers Divide two integers without using multiplication, division and mod operator. ...

  10. 成功解决ValueError: ‘usecols‘ must either be list-like of all strings, all unicode, all integers or a ca

    成功解决ValueError: 'usecols' must either be list-like of all strings, all unicode, all integers or a ca ...

最新文章

  1. 【CV秋季划】深度学习换脸算法视频更新
  2. [vue] 如何实现一个虚拟DOM?说说你的思路
  3. C++学习——类的初始化
  4. 【计算机网络】周知端口号列表
  5. Linux驱动(1)--关于驱动的概述
  6. 公司顾问岗位职责_集团公司法律顾问岗位职责
  7. git 学习1--查看全局配置
  8. 谈谈遵守公司作战纪律
  9. QT 按钮(4种样式)
  10. 29. Element ownerDocument 属性
  11. 如何调试一个无法重现的错误?
  12. 【JSP售票系统】JSP+SSH+MSSQL火车票铁路售票系统源码
  13. 知行之桥EDI系统中那些你忽略的功能
  14. RDL和RDLC性能比较
  15. AWTK-MVVM 在 STM32H743 上的移植笔记
  16. 安卓手机续航测试软件,10款手机电池续航测试对决:都是骁龙865手机,续航差距有多大?...
  17. java的深浅拷贝_Java中深浅拷贝
  18. ALS算法(推荐系统)
  19. Java项目:SSM二手汽车交易商城网站管理系统
  20. mysql断网_MYSQL主从断网恢复复制的问题(1)

热门文章

  1. 对于整数数组类的算法的终极解决方案
  2. CodeIgniter URL添加后缀
  3. 2014年12月26日
  4. hdu 4856 Tunnels
  5. 用C#新建XML文件
  6. 关于从EXCEL中导入到SQL server中的问题
  7. 获取GridView中RowCommand的当前索引行
  8. [精华]如何编写高质量的VB代码
  9. Mr.J---重拾Ajax(四)-- 跨域
  10. xshess 要继续使用此程序,您必须应用最新的更新