HDOJ 2433 Counter Strike 逆序对 线段树

Problem Description

Anti-terrorism is becoming more and more serious nowadays. The country now has n soldiers,and every solider has a score.

We want to choose some soldiers to fulfill an urgent task. The soldiers chosen must be adjacent to each other in order to make sure that they can cooperate well. And all the soldiers chosen must have an average score greater than a.

Now, please calculate how many ways can the chief of staff choose the soldiers.
 
Input
The first line consists of a single integer t, indicating number of test cases.

For each test case, the first line gives n, the number of soldiers, and a, the minimum possible average score(n<=100000,a<=10000). The second line gives n integers, corresponding to the soldiers' scores in order. All the scores are no greater than 10000.
 
Output
An integer n, number of ways to choose the soldiers.
 
Sample Input
2
5 3
1 3 7 2 4
1 1000
9999
 
Sample Output
10 1
 
 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 
 7 struct date
 8 {
 9       int lt,rt,Max,ans;
10 }node[300005];
11 int arr[100005],cnt[100005],val,N,A;
12 
13 int build_tree( int lt,int rt,int t )
14 {               
15        node[t].lt  = lt;
16        node[t].rt  = rt;
17        node[t].ans = 0;
18        if( lt == rt )
19             return node[t].Max = cnt[lt];
20        int mid = ( lt + rt )>>1;
21             return node[t].Max = max( build_tree( lt,mid,t<<1 ),build_tree( mid+1,rt,t<<1|1 ) );
22 }
23        
24 int inset_tree( int t,int wi )
25 {
26     val += node[t].ans;
27     if( node[t].lt == node[t].rt )
28     {
29        if( node[t].Max > 0 )val++; 
30        return 0;
31     }
32     if( node[t<<1].Max >= wi )
33     {
34         inset_tree( t<<1,wi );  
35         node[t<<1|1].ans++;
36     }
37     else inset_tree( t<<1|1,wi );
38                 return 0;
39 }
40 
41 int main( )
42 {
43     int i,T;
44     long long res;
45     scanf("%d",&T); 
46     while( T-- )
47     {
48         res = 0;
49         scanf("%d%d",&N,&A);
50         for( i = 1; i <= N; i++ )
51         {
52             scanf("%d",&arr[i]);
53             arr[i] = arr[i-1] + arr[i] - A;
54             cnt[i] = arr[i];
55         }    
56         sort( &cnt[1], &cnt[1] + N );
57         int t = unique( &cnt[1],&cnt[1] + N ) - &cnt[1];
58         build_tree( 1,t,1 );
59         for( i = 1; i <= N; i++ )
60         {
61             val = 0;
62             inset_tree( 1,arr[i] );
63             res += val;
64         }
65         printf("%I64d\n",res);
66     }
67     //system("pause");
68     return 0;
69 }

 

posted on 2013-03-12 16:01  浪舟  阅读(186)  评论(0编辑  收藏  举报

导航