Different Integers
链接:https://www.nowcoder.com/acm/contest/139/J
来源:牛客网
输入描述:
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.
输入
复制
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相关推荐
- pandas将dataframe原有的数据列名称转化为整数数值列名称(convert dataframe column labelsl into integers)
pandas将dataframe原有的数据列名称转化为整数数值列名称(convert dataframe column labelsl into integers) 目录 pandas将datafra ...
- [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 ...
- “TypeError: list indices must be integers or slices, not str”有关报错解决方案
"TypeError: list indices must be integers or slices, not str"有关报错解决方案 参考文章: (1)"TypeE ...
- python3 错误string indices must be integers 的解决方法
这个错误意思是字符串的下标一定要是整数 出这种错误有多种可能,最形象直接的就是: a = '[abc]' print(a['0']) 有点pyhton基础的都知道下标怎么能是字符串'0',必须是整数0 ...
- list indices must be integers or slices, not tuple
11年it研发经验,从一个会计转行为算法工程师,学过C#,c++,java,android,php,go,js,python,CNN神经网络,四千多篇博文,三千多篇原创,只为与你分享,共同成长,一起进 ...
- 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 ...
- MATLAB报错“Integers can only be raised to positive integral powers.“
报错代码是下面这样的: I=imread('pout.jpg'); if size(I,3)>1 %判断如果是彩色图像,转换为灰度图 I=rgb2gray(I); end Gamma_ ...
- Divide Two Integers
Divide two integers without using multiplication, division and mod operator. 思路:不能使用乘法除法以及取模运算来计算两个数 ...
- leetcode之Divide Two Integers
题目:Divide Two Integers Divide two integers without using multiplication, division and mod operator. ...
- 成功解决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 ...
最新文章
- 【CV秋季划】深度学习换脸算法视频更新
- [vue] 如何实现一个虚拟DOM?说说你的思路
- C++学习——类的初始化
- 【计算机网络】周知端口号列表
- Linux驱动(1)--关于驱动的概述
- 公司顾问岗位职责_集团公司法律顾问岗位职责
- git 学习1--查看全局配置
- 谈谈遵守公司作战纪律
- QT 按钮(4种样式)
- 29. Element ownerDocument 属性
- 如何调试一个无法重现的错误?
- 【JSP售票系统】JSP+SSH+MSSQL火车票铁路售票系统源码
- 知行之桥EDI系统中那些你忽略的功能
- RDL和RDLC性能比较
- AWTK-MVVM 在 STM32H743 上的移植笔记
- 安卓手机续航测试软件,10款手机电池续航测试对决:都是骁龙865手机,续航差距有多大?...
- java的深浅拷贝_Java中深浅拷贝
- ALS算法(推荐系统)
- Java项目:SSM二手汽车交易商城网站管理系统
- mysql断网_MYSQL主从断网恢复复制的问题(1)