1 #include <bits/stdc++.h>
2 using namespace std;
3 int sum[3][100050];
4 int main()
5 {
6 int n,q;
7 //freopen("de.txt","r",stdin);
8 while (~scanf("%d%d",&n,&q))
9 {
10 memset(sum,0,sizeof sum);
11 for (int i=1;i<=n;++i)
12 {
13 for (int j=0;j<3;++j)
14 sum[j][i]=sum[j][i-1];
15 int x;
16 scanf("%d",&x);
17 sum[x-1][i]++;
18 }
19 for (int i=0;i<q;++i)
20 {
21 int x,y;
22 scanf("%d%d",&x,&y);
23 printf("%d %d %d\n",sum[0][y]-sum[0][x-1],sum[1][y]-sum[1][x-1],sum[2][y]-sum[2][x-1]);
24 }
25 }
26
27 }
1 #include <iostream>
2 #include <cstring>
3 #include <cmath>
4 #include <cstdio>
5 #include <algorithm>
6 #include <string>
7 using namespace std;
8 #define maxn 111111
9 int sum[maxn<<2][3],sum2[maxn<<2],sum3[maxn<<2];
10 void pushup (int rt,int num)
11 {
12 sum[rt][num]=sum[rt<<1][num]+sum[rt<<1|1][num];
13 }
14
15 void update (int p,int l,int r,int rt,int num)
16 {
17 if (l==r)
18 {
19 sum[rt][num]++;
20 return;
21 }
22 int m=(l+r)>>1;
23 if (p<=m)
24 update(p,l,m,rt<<1,num);
25 else
26 update(p,m+1,r,rt<<1|1,num);
27 pushup(rt,num);
28 }
29 int query (int ll,int rr,int l,int r,int rt,int num)
30 {
31 if (ll<=l&&rr>=r)
32 return sum[rt][num];
33
34 int ret=0;
35 int m=(l+r)>>1;
36
37 if (ll<=m)
38 ret+=query(ll,rr,l,m,rt<<1,num);
39 if (rr>m)
40 ret+=query(ll,rr,m+1,r,rt<<1|1,num);
41 return ret;
42 }
43 int main()
44 {
45 int n,q;
46 //freopen("de.txt","r",stdin);
47 while (~scanf("%d%d",&n,&q))
48 {
49 memset(sum,0,sizeof sum);
50 for (int i=1;i<=n;++i)
51 {
52 int x;
53 scanf("%d",&x);
54 update(i,1,n,1,x-1);
55 }
56 for (int i=0;i<q;++i)
57 {
58 int x,y;
59 scanf("%d%d",&x,&y);
60 printf("%d ",query(x,y,1,n,1,0));
61 printf("%d ",query(x,y,1,n,1,1));
62 printf("%d\n",query(x,y,1,n,1,2));
63 }
64 }
65 return 0;
66 }
67 Time:360 ms
68 Memory:10376 kb