2386: Breed Counting
时间限制: 1 Sec
内存限制: 64 MB
提交: 81 解决: 31
[ 提交][ 状态][ 讨论版]
题目描述
输入
The next N lines contain an integer that is either 1, 2, or 3, giving the breed ID of a single cow in the ordering.
The next Q lines describe a query in the form of two integers a,b (a≤b).
输出
样例输入
6 3 2 1 1 3 2 1 1 6 3 3 2 4 样例输出
3 2 1 1 0 0 2 0 1比赛时直接用线段树开了3个数组做的。
其实还有更好的方法。
通过记录每个位置的前缀和,查询的时候直接输出 num[r] – num[l-1],就行了,就是这一段的和。 赞一个给力的孙学弟
源代码
#include
#include
#include
#include
#include
#include
using namespace std; #define maxn #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define mem(a,x) memset(a,x,sizeof(a)) int num1[maxn]; int num2[maxn]; int num3[maxn]; int main(){ int n,m; while(scanf("%d%d",&n,&m)!=EOF){ int tmp; mem(num1,0); mem(num2,0); mem(num3,0); for(int i=1;i<=n;i++){ num1[i] += num1[i-1]; num2[i] += num2[i-1]; num3[i] += num3[i-1]; scanf("%d",&tmp); if(tmp == 1) num1[i]++; if(tmp == 2) num2[i]++; if(tmp == 3) num3[i]++; } while(m--){ int a,b;scanf("%d%d",&a,&b); printf("%d ",num1[b] - num1[a-1]); printf("%d ",num2[b] - num2[a-1]); printf("%d\n",num3[b] - num3[a-1]); } } return 0; }
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/214695.html原文链接:https://javaforall.net
