hdu 4031 Attack(树状数组)

刚开始看错题意,以为防御全部都要缓冲,wa了几次,重新看题才知道是部分缓冲。

ans = 总的攻击次数 - 防御次数

View Code
 1 /*

 2 Author:Zhaofa Fang

 3 Lang:C++

 4 */

 5 #include <cstdio>

 6 #include <cstdlib>

 7 #include <sstream>

 8 #include <iostream>

 9 #include <cmath>

10 #include <cstring>

11 #include <algorithm>

12 #include <string>

13 #include <utility>

14 #include <vector>

15 #include <queue>

16 #include <stack>

17 #include <map>

18 #include <set>

19 

20 using namespace std;

21 

22 typedef long long ll;

23 #define DEBUG(x) cout<< #x << ':' << x << endl

24 #define PII pair<int,int>

25 #define PB push_back

26 #define MP make_pair

27 #define FI first

28 #define SE second

29 #define lowbit(x) (x & (x ^ (x-1)))

30 #define INF (1<<30)

31 

32 struct attack

33 {

34     int si,ti;

35 }a[20005];

36 int C[20005];

37 void add(int x,int pos,int n)

38  {

39      while(pos<=n)

40      {

41          C[pos] += x;

42          pos += lowbit(pos);

43      }

44  }

45  int SUM(int pos)

46  {

47      int res = 0;

48      while(pos > 0)

49      {

50          res += C[pos];

51          pos -= lowbit(pos);

52      }

53      return res;

54  }

55 int main()

56 {

57     #ifndef ONLINE_JUDGE

58     freopen("out","r",stdin);

59     #endif

60     int T;

61     cin>>T;

62     int cas = 0;

63     while(T--)

64     {

65         int n,q,t;

66         scanf("%d%d%d",&n,&q,&t);

67         printf("Case %d:\n",++cas);

68         int cnt = 0;

69         char op[10];

70         int p;

71         memset(C,0,sizeof(C));

72         for(int i=1;i<=q;i++)

73         {

74             scanf("%s",op);

75             if(op[0] == 'A')

76             {

77                 scanf("%d%d",&a[cnt].si,&a[cnt].ti);

78                 add(1,a[cnt].si,n);

79                 add(-1,a[cnt].ti+1,n);

80                 cnt++;

81             }

82             else

83             {

84                 scanf("%d",&p);

85                 int tmp = 0;

86                 for(int j=0;j<cnt;j++)

87                 {

88                     if(a[j].si<=p && p<=a[j].ti)

89                     {

90                         tmp++;

91                         j += t-1;

92                     }

93                 }

94                 printf("%d\n",SUM(p)-tmp);

95             }

96         }

97     }

98     return 0;

99 }

你可能感兴趣的:(树状数组)