UESTC--1256
2016-01-18 15:30
363 查看
昊昊爱运动
Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others)
Submit
Status
昊昊喜欢运动
他N天内会参加M种运动(每种运动用一个[1,m]的整数表示)
舍友有Q个问题
问昊昊第l天到第r天参加了多少种不同的运动
Input
输入两个数N, M (1≤N≤2000, 1≤M≤100);输入N个数ai表示在第i天昊昊做了第ai类型的运动;
输入一个数Q(1≤Q≤106);
输入Q行
每行两个数 l, r(1≤l≤r≤n);
Output
一共Q行每一行输出一个数 表示昊昊在第l天到第r天一共做了多少种活动
Sample input and output
Sample Input | Sample Output |
---|---|
5 3 1 2 3 2 2 3 1 4 2 4 1 5 | 3 2 3 |
Source
第七届ACM趣味程序设计竞赛第二场(正式赛)解体思路:前缀和思想,但还是659s过,不知道人家0s是怎么写出来的,希望大神赐教.
<pre name="code" class="cpp">#include<stdio.h> #include<string.h> int pre[2005][105];//表示[1,i]区间里,j出现的次数 int main(){ int n,m,x,t,l,r,ans; while(scanf("%d%d",&n,&m)!=EOF){ memset(pre,0,sizeof(pre)); for(int i=1;i<=n;i++){ scanf("%d",&x); pre[i][x]=1; for(int j=1;j<=m;j++)pre[i][j]+=pre[i-1][j]; } scanf("%d",&t); for(int i=1;i<=t;i++){ ans=0; scanf("%d%d",&l,&r); for(int j=1;j<=m;j++)ans+=(pre[r][j]>pre[l-1][j]); printf("%d\n",ans); } } return 0; }
相关文章推荐
- Divide and conquer:Dropping tests(POJ 2976)
- 果冻回弹效果 拉刷新效果 UIBezierPath
- UItableView搜索和全部删除
- UICollectionView的基本使用
- UITableView 分割线贴近左端屏幕开始
- 学习笔记:关于segue
- NGUI 相片循环滚动效果
- UI控件RecyclerView浅析
- 【django】request
- LeetCode Range Sum Query - Mutable
- UILabel根据文字计算高度在UItableview中的应用
- 《iOS Human Interface Guidelines》——HealthKit
- UIpickerView获得选中值
- XML hexadecimal value 0x__, is an invalid character
- 1367:[Baltic2004]sequence 左偏树
- UIALERT使用指南
- 将UIColor转换为RGB值
- 1月18号 UILabel 加上导入.tff格式的字体
- UITableView 状态栏返回顶部 和自定义按钮返回顶部
- 使用CForm类(Form builder)上传文件