标签:
HJS大牛想去街上吃饭,街道旁边拴着很多狗,他想我堂堂......(省略n个字)岂会被狗咬,所以他很牛的从狗的面前经过,不管是否被上一条狗咬过,下次还会从狗的面前过(J I A N)
现在问题来了,我们给狗编号从1...n,有多次询问,每次询问M,N这一段有多少狗咬过他有多少狗没有咬过他
5 6
-1 -2 3 4 5
1 2
1 3
1 4
2 4
3 5
1 5
0 2
1 2
2 2
2 1
3 0
3 2
//主要是要剔除0,即不确定的数量 #include <stdio.h> int ydog[1000001],dog[1000001],flag[1000001],flag1[1000001];//定义全局变量区而不可定义为栈区(栈溢出),s[0] = 0 int main() { int N,M,i,a,b,s; while(scanf("%d%d",&N,&M) != EOF) { for(i=1;i<=N;i++) { scanf("%d",&ydog[i]); if(ydog[i] > 0) { dog[i] = dog[i-1] + 1;//dog[0] = 0 flag[i] = 1;//咬人 flag1[i] = flag1[i-1];//要剔除的0的个数和前一个相同 } else{ dog[i] = dog[i-1];//dog[0] = 0 flag[i] = 0;//不确定和确定不咬人的 if(ydog[i] == 0) flag1[i] = flag1[i-1] + 1;//要剔除掉0(不确定) else{ flag1[i] = flag1[i-1]; } } } for(i=1;i<=M;i++) { scanf("%d%d",&a,&b);//编号 printf("%d %d\n",dog[b]-dog[a]+flag[a],(b-a+1-(flag1[b]-flag1[a]+(!ydog[a])))-(dog[b]-dog[a]+flag[a])); } } return 0; }
//----JZT_wuyang #include <stdio.h> int main() { int n, m, x, y, z, i, a[100005] = {0}, b[100005] = {0}; while (~scanf("%d%d", &n, &m)) { for (i = 1, y = z = 0; i <= n; i++) { scanf("%d", &x); if (x > 0) y ++; else if (x < 0) z ++; a[i] = y; b[i] = z; } while (m--) { scanf("%d%d", &x, &y); printf("%d %d\n", a[y]-a[x-1], b[y]-b[x-1]); } } return 0; }
标签:
原文地址:http://www.cnblogs.com/520xiuge/p/5347085.html