码迷,mamicode.com
首页 > 其他好文 > 详细

2017-4-10:CQUPT训练题

时间:2017-04-12 19:57:50      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:efi   相同   family   --   getc   ini   nts   event   tree   

A - Binary Simulation(BIT || SegmentTree)

Given a binary number, we are about to do some operations on the number. Two types of operations can be here.

‘I i j‘    which means invert the bit from i to j (inclusive)

‘Q i‘    answer whether the ith bit is 0 or 1

The MSB (most significant bit) is the first bit (i.e. i=1). The binary number can contain leading zeroes.

Input

Input starts with an integer T (≤ 10), denoting the number of test cases.

Each case starts with a line containing a binary integer having length n (1 ≤ n ≤ 105). The next line will contain an integerq (1 ≤ q ≤ 50000) denoting the number of queries. Each query will be either in the form ‘I i j‘ where i, j are integers and1 ≤ i ≤ j ≤ n. Or the query will be in the form ‘Q i‘ where i is an integer and 1 ≤ i ≤ n.

Output

For each case, print the case number in a single line. Then for each query ‘Q i‘ you have to print 1 or 0 depending on theith bit.

Sample Input

2

0011001100

6

I 1 10

I 2 7

Q 2

Q 1

Q 7

Q 5

1011110111

6

I 1 10

I 2 7

Q 2

Q 1

Q 7

Q 5

Sample Output

Case 1:

0

1

1

0

Case 2:

0

0

0

1

Solve:

区间问题,有查询有修改,所以直接BIT水过,每次查询的时候只要判断这个其和是不是奇数,奇数就取相反,偶数就相同

Code:

技术分享
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 static const int MAXN = 1e5 + 10;
 4 #define CLR(x , v)            memset(x , v , sizeof(x));
 5 #define CASE(x)        printf("Case %d:\n", x);
 6 char in[MAXN] = {0};
 7 int data[MAXN] = {0};
 8 int tree[MAXN << 1] = {0};
 9 int si = 0;
10 inline int lowbit(int x)    {return x & (-x);}
11 void Update(int pos , int value)
12 {
13     for(int i = pos ; i <= si ; i += lowbit(i))
14     {
15         tree[i] += value;
16     }
17 }
18 int Sum(int x)
19 {
20     int ans = 0;
21     for(int i = x ; i > 0 ; i -= lowbit(i))
22         ans += tree[i];
23     return ans;
24 }
25 int main()
26 {
27     int t , q , x , y;
28     char cmd;
29     scanf("%d" , &t);
30     for(int c = 1 ; c <= t ; ++c)
31     {
32         CLR(in , \0);
33         CLR(tree , 0);
34         scanf(" %s" , in + 1);
35         CASE(c);
36         si = strlen(in + 1);
37         scanf("%d" , &q);
38         while(q--)
39         {
40             scanf(" %c%d" , &cmd , &x);
41             if(cmd == I)
42             {
43                 scanf("%d" , &y);
44                 Update(x , 1);
45                 Update(y + 1 , -1);
46             }
47             else
48             {
49                 if(Sum(x) & 1)
50                 {
51                     printf("%d\n" , in[x] == 0 ? 1 : 0);
52                 }
53                 else
54                     printf("%c\n" , in[x]);
55             }
56         }
57 
58     }
59     return 0;
60 }
View Code

 B - Points in Segments(二分查找)

Given n points (1 dimensional) and q segments, you have to find the number of points that lie in each of the segments. A point pi will lie in a segment A B if A ≤ pi ≤ B.

For example if the points are 1, 4, 6, 8, 10. And the segment is 0 to 5. Then there are 2 points that lie in the segment.

Input

Input starts with an integer T (≤ 5), denoting the number of test cases.

Each case starts with a line containing two integers n (1 ≤ n ≤ 105) and q (1 ≤ q ≤ 50000). The next line contains n space separated integers denoting the points in ascending order. All the integers are distinct and each of them range in [0, 108].

Each of the next q lines contains two integers Ak Bk (0 ≤ Ak ≤ Bk ≤ 108) denoting a segment.

Output

For each case, print the case number in a single line. Then for each segment, print the number of points that lie in that segment.

Sample Input

1

5 3

1 4 6 8 10

0 5

6 10

7 100000

Sample Output

Case 1:

2

3

2

Means:

给你一些点,然后给你一条线段,然后问你有多少点在线段上

Solve:

简单二分查找,因为给出的点都是有序的,所以直接存点就行了,二分找第一个大于等于线段左侧端点的点,找最后一个小于等于线段右侧端点的点,之间的数量就是了

Code:

技术分享
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define LSON            id << 1 , l , mid
 4 #define RSON            id << 1 | 1 , mid + 1 , r
 5 #define ROOT            1 , 0 , n
 6 #define CLR(x , y)      memset(x , y , sizeof(x))
 7 #define LOWBIT(x)       x & (-x)
 8 #define FORN(i , a , n)  for(int i = (a) ; i <= (n) ; ++i)
 9 #define FORP(i , n , a)  for(int i = (n) ; i >= (a) ; --i)
10 #define CASE(x)        printf("Case %d:\n", x);
11 static const double EPS = 1e-8;
12 static const int INF = 0X3fffffff;
13 typedef long long LL;
14 int n , q;
15 static const int MAXN = 1e5 + 10;
16 LL data[MAXN];
17 template<class T> inline
18 T read(T &x)
19 {
20     x = 0;
21     int f = 1 ; char ch = getchar();
22     while (ch < 0 || ch > 9) {if (ch == -) f = -1; ch = getchar();}
23     while (ch >= 0 && ch <= 9) {x = x * 10 + ch - 0; ch = getchar();}
24     x *= f;
25 }
26 int main()
27 {
28     int t;
29     read(t);
30     for(int c = 1 ; c <= t ; ++c)
31     {
32         int pos = 0;
33         CASE(c);
34         read(n) , read(q);
35         LL x;
36         for(int i = 0 ; i < n ; ++i)
37         {
38             read(x);
39             data[pos++] = x;
40         }
41         LL y;
42         while(q--)
43         {
44             read(x) , read(y);
45             LL temp1 = upper_bound(data , data + n , y) - data;
46             LL temp2 = lower_bound(data , data + n , x) - data;
47             printf("%lld\n" , temp1 - temp2);
48         }
49     }
50 }
View Code

 

2017-4-10:CQUPT训练题

标签:efi   相同   family   --   getc   ini   nts   event   tree   

原文地址:http://www.cnblogs.com/jianglingxin/p/6700714.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!