牛客网2018多校第一场J题
链接: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.参考博客https://www.nowcoder.com/discuss/87249 作者:scaufat链接:https://www.nowcoder.com/discuss/87249来源:牛客网
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 const int N = 2e5+10; 7 int n,m; 8 int a[N]; 9 int c[N]; 10 int vis[N]; 11 int nex[N]; //记录每个a[i]上一次出现的位置 12 int last[N];//为了记录nex数组所添加的辅助数组用来记录a[i]上一次出现的位置 13 int pre[N]; 14 int answer[N]; 15 struct node 16 { 17 int l,r,id; 18 }querys[N]; 19 int cmp(node x,node y) 20 { 21 return x.l<y.l; 22 } 23 int lowbit(int x) 24 { 25 return x&(-x); 26 } 27 void update(int id,int val) 28 { 29 while(id<=n) 30 { 31 c[id]+=val; 32 id+=lowbit(id); 33 } 34 } 35 int query(int id) 36 { 37 int sum = 0; 38 while(id>0) 39 { 40 sum+=c[id]; 41 id-=lowbit(id); 42 } 43 return sum; 44 } 45 int query(int l,int r) 46 { 47 return query(r) - query(l-1); 48 } 49 int main() 50 { 51 while(scanf("%d%d",&n,&m)!=EOF) 52 { 53 memset(last,-1,sizeof(last)); 54 memset(nex,-1,sizeof(nex)); 55 memset(pre,0,sizeof(pre)); 56 memset(vis,0,sizeof(vis)); 57 memset(a,0,sizeof(a)); 58 memset(c,0,sizeof(c)); 59 memset(answer,0,sizeof(answer)); 60 for(int i = 1; i <= n; i++) 61 { 62 scanf("%d",&a[i]); 63 a[i+n] = a[i]; 64 } 65 n*=2; 66 pre[0] = 0; 67 for(int i = 1; i <= n; i++) 68 { 69 if(!vis[a[i]]) 70 { 71 vis[a[i]] = 1; 72 pre[i] = pre[i-1]+1; 73 } 74 else{ 75 pre[i] = pre[i-1]; 76 } 77 if(~last[a[i]]) 78 { 79 nex[last[a[i]]] = i; 80 } 81 last[a[i]] = i; 82 } 83 for(int i = 0; i < m; i++) 84 { 85 scanf("%d%d",&querys[i].l,&querys[i].r); 86 querys[i].l+=(n/2); 87 swap(querys[i].l,querys[i].r); 88 querys[i].id = i; 89 } 90 sort(querys,querys+m,cmp); 91 int now = 1; 92 for(int i = 0; i < m; i++) 93 { 94 while(now < querys[i].l) 95 { 96 if(~nex[now])//如果当前位置的下一个位置存在,就更新树状数组 97 update(nex[now],1);//更新当前i位置的数a[i]的下一个下一个位置 98 now++; 99 } 100 answer[querys[i].id] = pre[querys[i].r]-pre[querys[i].l-1]+query(querys[i].l,querys[i].r); 101 } 102 for(int i = 0; i < m; i++) 103 printf("%d\n",answer[i]); 104 } 105 return 0; 106 }
转载于:https://www.cnblogs.com/--lr/p/9364917.html
牛客网2018多校第一场J题相关推荐
- 牛客网 2018校招真题 网易 骰子游戏
Description 牛客网 2018校招真题 骰子游戏 Solving Ideas 参考<剑指offer>n个骰子的点数 考虑用两个数组来存储骰子点数的每一个总数出现的次数. 在一次循 ...
- 牛客网 2018校招真题 美团点评 K的倍数
Description 牛客网 2018校招真题 K的倍数 Solving Ideas sum[i + 1]: 表示序列p[0]...p[i]的和 从长度最大的子串开始判断,当剩余需要判断子串长度不可 ...
- 牛客网 2018校招真题 摩拜 排序次数
Description 牛客网 2018校招真题 排序次数 Solving Ideas 将数组a的元素拷贝到数组b 对数组b进行排序 对比数组b,统计数组a中已排序的元素个数 如:a = [2, 11 ...
- 牛客网 2018校招真题 滴滴出行 寻找丑数
Description 牛客网 2018校招真题 寻找丑数 Solving Ideas 参考<剑指offer>丑数 Time complexity : O(n)O(n)O(n) Space ...
- 牛客网 2018校招真题 爱奇艺 最后一位
Description 牛客网 2018校招真题 最后一位 Solving Ideas 二分查找 Solution import java.io.BufferedReader; import java ...
- 牛客网 2018校招真题 京东 回文
Description 牛客网 2018校招真题 回文 Solving Ideas 计算以str[str.length() - 1]为结尾的最大的回文长度,从而判断最少需要追加多少个字母才能使整个串成 ...
- 牛客网 2018校招真题 美团点评 关灯游戏
Description 牛客网 2018校招真题 关灯游戏 Solving Ideas 只与最右边的数字有关,如果最右边的数字为1,则Alice赢,否则Bob赢 假设最右边的数字为1 因为Alice是 ...
- 牛客网 2018校招真题 美团点评 重要节点
Description 牛客网 2018校招真题 重要节点 Solving Ideas BFS 创建一个标记数组arrive,arrive[i][j]为true表示从i可以到达j, false则不能 ...
- 牛客网 2018校招真题 招商银行信用卡 整数成绩最大化
Description 牛客网 2018校招真题 整数成绩最大化 Solving Ideas 当n>4时,最大受益为尽可能拆分出更多的数字3,最后一个不足3的,若其为1则分给其中一个3,得到4, ...
最新文章
- oracle创建directirtory,oracle11g使用Direct NFS创建表空间
- 一个空值_MySQL数据库表中 NULL 和空值到底有什么区别呢?
- bcftools合并vcf文件
- cocos2d-x 关于tilemap滚动时黑线闪动的问题
- Windows MSVC 符号表(.lib文件)(C++符号表解析)(符号表是如何产生的)(第四步:链接)
- webapi部署到IIS 404错误
- Oracle加快终止对以往Java版本的免费支持期
- JavaScript的new关键字执行过程(1)
- 必杀技———SQL基础整理系列(一)
- 2、使用Keras构建回归模型
- vue 引入json地图_vue中echarts引入中国地图
- SuperMap GIS 10i软件概览
- 读取QQ ClientKey C++版本
- IIS发布网站,如此简单
- vue怎么制作甘特图——dhtmlx-gantt
- 【C语言】动态内存开辟的使用『malloc』
- 使用HybridSN进行高光谱图像分类
- form表单字段默认值
- idea中增大jdk内存
- 关于将AAB转化为APK
热门文章
- java 全选 反选取值_全选反选以及获取选中的数据
- qt 对话框位置如何确定_便利店如何确定收银台位置?
- 360好还是电脑管家好_安装了电脑管家,为什么电脑还是卡?
- 2021年南宁二中高考成绩查询,2021年广西南宁二中高考物理冲刺试卷(一).docx...
- spring jdbc_Spring JDBC示例
- java访问修饰符_Java访问修饰符
- scala案例_Scala案例类和案例对象深入(第1部分)
- 使用Kotlin的Android TextView –全面教程
- Elasticsearch的javaAPI之get,delete,bulk
- OpenSSH升级-无需替换旧版本文件