bzoj4397【Usaco2015 Dec】Breed Counting

4397: [Usaco2015 dec]Breed Counting

Time Limit: 10 Sec  Memory Limit: 128 MB

Submit: 29  Solved: 25

[Submit][Status][Discuss]

Description

Farmer John‘s N cows, conveniently numbered 1…N, are all standing in a row (they seem to do so often that it now takes very little prompting from Farmer John to line them up). Each cow has a breed ID:
1 for Holsteins, 2 for Guernseys, and 3 for Jerseys. Farmer John would like your help counting the number of cows of each breed that lie within certain intervals of the ordering.

给定一个长度为N的序列,每一个位置上的数仅仅可能是1,2,3中的一种。

有Q次询问,每次给定两个数a,b。请分别输出区间[a,b]里数字1,2。3的个数。

Input

The first line of input contains NN and QQ (1≤N≤100,000 1≤Q≤100,000).

The next NN lines contain an integer that is either 1, 2, or 3, giving the breed ID of a single cow in the ordering.

The next QQ lines describe a query in the form of two integers a,b (a≤b).

Output

For each of the QQ queries (a,b), print a line containing three numbers: the number of cows numbered a…b that are Holsteins (breed 1), Guernseys (breed 2), and Jerseys (breed 3).

Sample Input

6 3

2

1

1

3

2

1

1 6

3 3

2 4

Sample Output

3 2 1

1 0 0

2 0 1

HINT

Source

Silver鸣谢Claris提供译文

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<queue>
#define F(i,j,n) for(int i=j;i<=n;i++)
#define D(i,j,n) for(int i=j;i>=n;i--)
#define ll long long
#define pa pair<int,int>
#define maxn 100005
#define inf 1000000000
using namespace std;
int n,m,x,y,sum[4][maxn];
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (ch<‘0‘||ch>‘9‘){if (ch==‘-‘) f=-1;ch=getchar();}
	while (ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
	return x*f;
}
int main()
{
	n=read();m=read();
	F(i,1,n)
	{
		F(j,1,3) sum[j][i]=sum[j][i-1];
		x=read();
		sum[x][i]++;
	}
	F(i,1,m)
	{
		x=read();y=read();
		printf("%d %d %d\n",sum[1][y]-sum[1][x-1],sum[2][y]-sum[2][x-1],sum[3][y]-sum[3][x-1]);
	}
}
时间: 2024-10-08 09:45:19

bzoj4397【Usaco2015 Dec】Breed Counting的相关文章

bzoj4397【Usaco2015 Dec】Breed Counting(前缀和、树状数组)

题目描述 Farmer John's N cows, conveniently numbered 1…N, are all standing in a row (they seem to do so often that it now takes very little prompting from Farmer John to line them up). Each cow has a breed ID: 1 for Holsteins, 2 for Guernseys, and 3 for

【BZOJ】【1640】【USACO2007 Nov】/【1692】【USACO2007 Dec】队列变换

后缀数组/贪心 每次从等待序列的头或尾拿出一个放到答案序列的末尾,那么每次贪心比较头和尾的字典序大小即可…… TAT贪心很好想,但是我一开始没想到是可以直接比较字符串大小……而是一位一位判的,WA了…… 膜拜了zyf的做法TAT 1 /************************************************************** 2 Problem: 1692 3 User: Tunix 4 Language: C++ 5 Result: Accepted 6 Ti

【POJ - 2386】Lake Counting (dfs+染色)

-->Lake Counting 直接上中文了 Descriptions: 由于近日阴雨连天,约翰的农场中中积水汇聚成一个个不同的池塘,农场可以用 N x M (1 <= N <= 100; 1 <= M <= 100) 的正方形来表示.农场中的每个格子可以用'W'或者是'.'来分别代表积水或者土地,约翰想知道他的农场中有多少池塘.池塘的定义:一片相互连通的积水.任何一个正方形格子被认为和与它相邻的8个格子相连. 给你约翰农场的航拍图,确定有多少池塘 Input Line 1

【BZOJ3888】【Usaco2015 Jan】Stampede 线段树判区间覆盖

广告: #include <stdio.h> int main() { puts("转载请注明出处[vmurder]谢谢"); puts("网址:blog.csdn.net/vmurder/article/details/44066313"); } 题意: PoPoQQQ站在原点上向y轴正半轴看,然后有一群羊驼从他眼前飞过.这些羊驼初始都在第二象限,尾巴在(Xi,Yi),头在(Xi+1,Yi),每Ci秒向右走一个单位. PoPoQQQ能看见一匹羊驼当且仅

【UVA 11401】Triangle Counting

题 题意 求1到n长度的n根棍子(3≤n≤1000000)能组成多少不同三角形. 分析 我看大家的递推公式都是 a[i]=a[i-1]+ ((i-1)*(i-2)/2-(i-1)/2)/2; 因为以最大长度i 为最大边的三角形有 第二边为i-1.i-2....2 分别有 i-2个.i-3.... .1个,总共就有(i-1)*(i-2)/2个. 2 到 i-1 做第二边时,有(i-1)/2条边算到了两边相等,也就是要减去 (i-1)/2,因为第二边的在第三边出现了,所以算了两次,再除以2. 我的递

【BZOJ3889】【Usaco2015 Jan】Cow Routing 双键值最短路

广告: #include <stdio.h> int main() { puts("转载请注明出处[vmurder]谢谢"); puts("网址:blog.csdn.net/vmurder/article/details/44064091"); } 题意: 从样例讲起. 第一行 s,t,m表示:起点,终点,m条航线. 然后m组,每组第一行len,n表示这条航线的代价, 这类似于公交车,只要用了就花这些钱,但是用多少都这些钱. 注意是单向边. 举例: 23

【BZOJ3891】【Usaco2014 Dec】Piggy Back bfs+动规?

广告: #include <stdio.h> int main() { puts("转载请注明出处[vmurder]谢谢"); puts("网址:blog.csdn.net/vmurder/article/details/43970835"); } 题解: bfs出1.2.n到每个点距离 然后枚举求min{B*f[1]+E*f[2]+P*f[n]}; 代码: #include <queue> #include <cstdio> #

【BZOJ1690】【Usaco2007 Dec】奶牛的旅行 分数规划 判断负环

题解: 分数规划+判断负环. 代码: #include <queue> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define N 1010 #define M 5050 #define eps 1e-8 using namespace std; double mid,fun[N]; struct Eli { int v,n; do

【Usaco2015 FEB】Cow Hopscotch (Gold)

Just like humans enjoy playing the game of Hopscotch, Farmer John's cows have invented a variant of the game for themselves to play. Being played by clumsy animals weighing nearly a ton, Cow Hopscotch almost always ends in disaster, but this has surp