当前位置:   article > 正文

Codeforces Round 932 (Div. 2 ABCDE题) 视频讲解

Codeforces Round 932 (Div. 2 ABCDE题) 视频讲解

A. Entertainment in MAC

Problem Statement

Congratulations, you have been accepted to the Master’s Assistance Center! However, you were extremely bored in class and got tired of doing nothing, so you came up with a game for yourself.

You are given a string s s s and an even integer n n n. There are two types of operations that you can apply to it:

  1. Add the reversed string s s s to the end of the string s s s (for example, if $s = $ cpm, then after applying the operation $s = $ cpmmpc).
  2. Reverse the current string s s s (for example, if $s = $ cpm, then after applying the operation $s = $ mpc).

It is required to determine the lexicographically smallest † ^{\dagger} string that can be obtained after applying exactly n n n operations. Note that you can apply operations of different types in any order, but you must apply exactly n n n operations in total.

† ^{\dagger} A string a a a is lexicographically smaller than a string b b b if and only if one of the following holds:

  • a a a is a prefix of b b b, but a ≠ b a \ne b a=b;
  • in the first position where a a a and b b b differ, the string a a a has a letter that appears earlier in the alphabet than the corresponding letter in b b b.

Input

Each test consists of multiple test cases. The first line contains a single integer t t t ( 1 ≤ t ≤ 500 1 \leq t \leq 500 1t500) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single even integer n n n ( 2 ≤ n ≤ 1 0 9 2 \leq n \leq 10^9 2n109) — the number of operations applied to the string s s s.

The second line of each test case contains a single string s s s ( 1 ≤ ∣ s ∣ ≤ 100 1 \leq |s| \leq 100 1s100), consisting of lowercase English letters, — the string to which the operations are applied.

Output

For each test case, output a single line — the lexicographically smallest string that can be obtained after applying exactly n n n operations.

Example

Example

input
5
4
cpm
2
grib
10
kupitimilablodarbuz
1000000000
capybara
6
abacaba
output
cpm
birggrib
kupitimilablodarbuz
arabypaccapybara
abacaba

Note

In the first test case, you can apply the operation of the second type (i.e., reverse the string s s s) 4 4 4 times. Then the string s s s will remain equal to cpm.

In the second test case, you can do the following:

  • Apply the operation of the second type, after which s s s will become equal to birg.
  • Apply operation of the first type (i.e., add the reversed string s s s to the end of the string s s s), after which s s s will become equal to birggrib.

Solution

具体见文后视频。


Code

#include <bits/stdc++.h>
#define int long long

using namespace std;

typedef pair<int, int> PII;
typedef long long LL;

void solve()
{
	int n;
	string s;

	cin >> n >> s;

	int l = 0, r = s.size() - 1;
	while (l <= r && s[l] == s[r]) l ++, r --;
	if (l > r) cout << s << endl;
	else if (s[l] > s[r])
	{
		string t = s;
		reverse(t.begin(), t.end());
		t += s;
		cout << t << endl;
	}
	else
		cout << s << endl;
}

signed main()
{
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(0);

	int Data;

	cin >> Data;

	while (Data --)
		solve();

	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

B. Informatics in MAC

Problem Statement

In the Master’s Assistance Center, Nyam-Nyam was given a homework assignment in informatics.

There is an array a a a of length n n n, and you want to divide it into k > 1 k > 1 k>1 subsegments † ^{\dagger} in such a way that the MEX ⁡ ‡ \operatorname{MEX} ^{\ddagger} MEX on each subsegment is equal to the same integer.

Help Nyam-Nyam find any suitable division, or determine that it does not exist.

† ^{\dagger} A division of an array into k k k subsegments is defined as k k k pairs of integers ( l 1 , r 1 ) , ( l 2 , r 2 ) , … , ( l k , r k ) (l_1, r_1), (l_2, r_2), \ldots, (l_k, r_k) (l1,r1),(l2,r2),,(lk,rk) such that l i ≤ r i l_i \le r_i liri and for each 1 ≤ j ≤ k − 1 1 \le j \le k - 1 1jk1, l j + 1 = r j + 1 l_{j + 1} = r_j + 1 lj+1=rj+1, and also l 1 = 1 l_1 = 1 l1=1 and r k = n r_k = n rk=n. These pairs represent the subsegments themselves.

‡ MEX ⁡ ^{\ddagger}\operatorname{MEX} MEX of an array is the smallest non-negative integer that does not belong to the array.

For example:

  • MEX ⁡ \operatorname{MEX} MEX of the array [ 2 , 2 , 1 ] [2, 2, 1] [2,2,1] is 0 0 0, because 0 0 0 does not belong to the array.
  • MEX ⁡ \operatorname{MEX} MEX of the array [ 3 , 1 , 0 , 1 ] [3, 1, 0, 1] [3,1,0,1] is 2 2 2, because 0 0 0 and 1 1 1 belong to the array, but 2 2 2 does not.
  • MEX ⁡ \operatorname{MEX} MEX of the array [ 0 , 3 , 1 , 2 ] [0, 3, 1, 2] [0,3,1,2] is 4 4 4, because 0 0 0, 1 1 1, 2 2 2, and 3 3 3 belong to the array, but 4 4 4 does not.

Input

Each test consists of multiple test cases. The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \leq t \leq 10^4 1t104) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer n n n ( 2 ≤ n ≤ 1 0 5 2 \le n \le 10^5 2n105) — the length of the array a a a.

The second line of each test case contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an ( 0 ≤ a i < n 0 \le a_i < n 0ai<n) — the elements of the array a a a.

It is guaranteed that the sum of n n n over all test cases does not exceed 1 0 5 10^5 105.

Output

For each test case, output a single integer − 1 -1 1 if a suitable division does not exist.

Otherwise, on the first line, output an integer k k k ( 2 ≤ k ≤ n 2 \le k \le n 2kn) — the number of subsegments in the division.

Then output k k k lines — the division into subsegments. The i i i-th line should contain two integers l i l_i li and r i r_i ri ( 1 ≤ l i ≤ r i ≤ n 1 \le l_i \le r_i \le n 1lirin) — the boundaries of the i i i-th subsegment.

The following conditions must be satisfied:

  • For all 1 ≤ j ≤ k − 1 1 \le j \le k - 1 1jk1, l j + 1 = r j + 1 l_{j + 1} = r_j + 1 lj+1=rj+1;
  • l 1 = 1 l_1 = 1 l1=1, r k = n r_k = n rk=n.

If there are multiple possible solutions, output any of them.

Example

Example

input
5
2
0 0
5
0 1 2 3 4
8
0 1 7 1 0 1 0 3
3
2 2 2
4
0 1 2 0
output
2
1 1
2 2
-1
3
1 3
4 5
6 8
3
1 1
2 2
3 3
-1

Note

In the first test case, the array a a a can be divided into 2 2 2 subsegments with boundaries [ 1 , 1 ] [1, 1] [1,1] and [ 2 , 2 ] [2, 2] [2,2]:

  • MEX ⁡ \operatorname{MEX} MEX of the first subsegment [ 0 ] [0] [0] is 1 1 1, as 0 0 0 belongs to the subsegment, but 1 1 1 does not.
  • MEX ⁡ \operatorname{MEX} MEX of the second subsegment [ 0 ] [0] [0] is 1 1 1, as 0 0 0 belongs to the subsegment, but 1 1 1 does not.

In the second test case, it can be proven that the required division does not exist.

In the third test case, the array a a a can be divided into 3 3 3 subsegments with boundaries [ 1 , 3 ] [1, 3] [1,3], [ 4 , 5 ] [4, 5] [4,5], [ 6 , 8 ] [6, 8] [6,8]:

  • MEX ⁡ \operatorname{MEX} MEX of the first subsegment [ 0 , 1 , 7 ] [0, 1, 7] [0,1,7] is 2 2 2, as 0 0 0 and 1 1 1 belong to the subsegment, but 2 2 2 does not.
  • MEX ⁡ \operatorname{MEX} MEX of the second subsegment [ 1 , 0 ] [1, 0] [1,0] is 2 2 2, as 0 0 0 and 1 1 1 belong to the subsegment, but 2 2 2 does not.
  • MEX ⁡ \operatorname{MEX} MEX of the third subsegment [ 1 , 0 , 3 ] [1, 0, 3] [1,0,3] is 2 2 2, as 0 0 0 and 1 1 1 belong to the subsegment, but 2 2 2 does not.

Solution

具体见文后视频。


Code

#include <bits/stdc++.h>
#define int long long

using namespace std;

typedef pair<int, int> PII;
typedef long long LL;

void solve()
{
	int n;
	cin >> n;

	std::vector<int> a(n), vis(n, 0);
	for (int i = 0; i < n; i ++)
		cin >> a[i], vis[a[i]] = 1;

	int mex = 0;
	for (int i = 0; i < n; i ++)
		if (!vis[i]) break;
		else mex ++;

	int tmp = 0, cnt = 0;
	unordered_map<int, int> has;
	std::vector<PII> way;
	for (int r = 0, l = 0; r < n; r ++)
	{
		has[a[r]] = 1;
		while (has.count(tmp)) tmp ++;
		if (tmp == mex) way.emplace_back(l + 1, r + 1), l = r + 1, cnt ++, tmp = 0, has.clear();
	}
	way.back().second = n;

	if (cnt > 1)
	{
		cout << way.size() << endl;
		for (auto v : way)
			cout << v.first << " " << v.second << endl;
	}
	else
		cout << -1 << endl;
}

signed main()
{
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(0);

	int Data;

	cin >> Data;

	while (Data --)
		solve();

	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58

C. Messenger in MAC

Problem Statement

In the new messenger for the students of the Master’s Assistance Center, Keftemerum, an update is planned, in which developers want to optimize the set of messages shown to the user. There are a total of n n n messages. Each message is characterized by two integers a i a_i ai and b i b_i bi. The time spent reading the set of messages with numbers p 1 , p 2 , … , p k p_1, p_2, \ldots, p_k p1,p2,,pk ( 1 ≤ p i ≤ n 1 \le p_i \le n 1pin, all p i p_i pi are distinct) is calculated by the formula:

∑ i = 1 k a p i + ∑ i = 1 k − 1 ∣ b p i − b p i + 1 ∣ \Large \sum_{i=1}^{k} a_{p_i} + \sum_{i=1}^{k - 1} |b_{p_i} - b_{p_{i+1}}| i=1kapi+i=1k1bpibpi+1

Note that the time to read a set of messages consisting of one message with number p 1 p_1 p1 is equal to a p 1 a_{p_1} ap1. Also, the time to read an empty set of messages is considered to be 0 0 0.

The user can determine the time l l l that he is willing to spend in the messenger. The messenger must inform the user of the maximum possible size of the set of messages, the reading time of which does not exceed l l l. Note that the maximum size of the set of messages can be equal to 0 0 0.

The developers of the popular messenger failed to implement this function, so they asked you to solve this problem.

Input

Each test consists of multiple test cases. The first line contains a single integer t t t ( 1 ≤ t ≤ 5 ⋅ 1 0 4 1 \leq t \leq 5 \cdot 10^4 1t5104) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers n n n and l l l ( 1 ≤ n ≤ 2000 1 \leq n \leq 2000 1n2000, 1 ≤ l ≤ 1 0 9 1 \leq l \leq 10^9 1l109) — the number of messages and the time the user is willing to spend in the messenger.

The i i i-th of the next n n n lines contains two integers a i a_i ai and b i b_i bi ( 1 ≤ a i , b i ≤ 1 0 9 1 \le a_i, b_i \le 10^9 1ai,bi109) — characteristics of the i i i-th message.

It is guaranteed that the sum of n 2 n^2 n2 over all test cases does not exceed 4 ⋅ 1 0 6 4 \cdot 10^6 4106.

Output

For each test case, output a single integer — the maximum possible size of a set of messages, the reading time of which does not exceed l l l.

Example

input
5
5 8
4 3
1 5
2 4
4 3
2 3
1 6
4 10
3 12
4 8
2 1
2 12
5 26
24 7
8 28
30 22
3 8
17 17
5 14
15 3
1000000000 998244353
179 239
228 1337
993 1007
output
3
1
2
1
0

Note

In the first test case, you can take a set of three messages with numbers p 1 = 3 p_1 = 3 p1=3, p 2 = 2 p_2 = 2 p2=2, and p 3 = 5 p_3 = 5 p3=5. The time spent reading this set is equal to a 3 + a 2 + a 5 + ∣ b 3 − b 2 ∣ + ∣ b 2 − b 5 ∣ = 2 + 1 + 2 + ∣ 4 − 5 ∣ + ∣ 5 − 3 ∣ = 8 a_3 + a_2 + a_5 + |b_3 - b_2| + |b_2 - b_5| = 2 + 1 + 2 + |4 - 5| + |5 - 3| = 8 a3+a2+a5+b3b2+b2b5=2+1+2+∣45∣+∣53∣=8.

In the second test case, you can take a set of one message with number p 1 = 1 p_1 = 1 p1=1. The time spent reading this set is equal to a 1 = 4 a_1 = 4 a1=4.

In the fifth test case, it can be shown that there is no such non-empty set of messages, the reading time of which does not exceed l l l.

Solution

具体见文后视频。


Code

#include <bits/stdc++.h>
#define int long long

using namespace std;

typedef pair<int, int> PII;
typedef long long LL;

const int N = 2e3 + 10, INF = 4e18;

int n, l;
PII mes[N];
int f[N][N], g[N], p[N];

bool cmp(PII a, PII b) { return a.second < b.second; }

void solve()
{
	cin >> n >> l;

	for (int i = 1; i <= n; i ++)
		cin >> mes[i].first >> mes[i].second;
	sort(mes + 1, mes + 1 + n, cmp);

	for (int i = 0; i <= n; i ++) for (int j = 0; j <= n; j ++) f[i][j] = INF, g[j] = INF;
	for (int i = 1; i <= n; i ++)
	{
		for (int j = i; j >= 2; j --)
		{
			f[i][j] = g[j - 1] + mes[i].first + mes[i].second;
			if (g[j] > f[i][j] - mes[i].second) g[j] = f[i][j] - mes[i].second, p[j] = i;
		}
		f[i][1] = mes[i].first;
		if (f[i][1] - mes[i].second < g[1]) g[1] = f[i][1] - mes[i].second, p[1] = i;
	}

	for (int j = 1; j <= n; j ++) g[j] = INF;
	for (int i = 1; i <= n; i ++)
		for (int j = 1; j <= i; j ++)
			g[j] = min(f[i][j], g[j]);

	for (int i = 1; i <= n; i ++)
		if (g[i] > l)
		{
			cout << i - 1 << endl;
			return;
		}
	cout << n << endl;
}

signed main()
{
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(0);

	int Data;

	cin >> Data;

	while (Data --)
		solve();

	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65

D. Exam in MAC

Problem Statement

The Master’s Assistance Center has announced an entrance exam, which consists of the following.

The candidate is given a set s s s of size n n n and some strange integer c c c. For this set, it is needed to calculate the number of pairs of integers ( x , y ) (x, y) (x,y) such that 0 ≤ x ≤ y ≤ c 0 \leq x \leq y \leq c 0xyc, x + y x + y x+y is not contained in the set s s s, and also y − x y - x yx is not contained in the set s s s.

Your friend wants to enter the Center. Help him pass the exam!

Input

Each test consists of multiple test cases. The first line contains a single integer t t t ( 1 ≤ t ≤ 2 ⋅ 1 0 4 1 \leq t \leq 2 \cdot 10^4 1t2104) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers n n n and c c c ( 1 ≤ n ≤ 3 ⋅ 1 0 5 1 \leq n \leq 3 \cdot 10^5 1n3105, 1 ≤ c ≤ 1 0 9 1 \leq c \leq 10^9 1c109) — the size of the set and the strange integer.

The second line of each test case contains n n n integers s 1 , s 2 , … , s n s_1, s_2, \ldots, s_{n} s1,s2,,sn ( 0 ≤ s 1 < s 2 < … < s n ≤ c 0 \leq s_1 < s_2 < \ldots < s_{n} \leq c 0s1<s2<<snc) — the elements of the set s s s.

It is guaranteed that the sum of n n n over all test cases does not exceed 3 ⋅ 1 0 5 3 \cdot 10^5 3105.

Output

For each test case, output a single integer — the number of suitable pairs of integers.

Example

Example

input
8
3 3
1 2 3
1 179
57
4 6
0 3 5 6
1 1
1
5 10
0 2 4 8 10
5 10
1 3 5 7 9
4 10
2 4 6 7
3 1000000000
228 1337 998244353
output
3
16139
10
2
33
36
35
499999998999122959

Note

In the first test case, the following pairs are suitable: ( 0 , 0 ) (0, 0) (0,0), ( 2 , 2 ) (2, 2) (2,2), ( 3 , 3 ) (3, 3) (3,3).

In the third test case, the following pairs are suitable: ( 0 , 1 ) (0, 1) (0,1), ( 0 , 2 ) (0, 2) (0,2), ( 0 , 4 ) (0, 4) (0,4), ( 1 , 3 ) (1, 3) (1,3), ( 2 , 6 ) (2, 6) (2,6), ( 3 , 4 ) (3, 4) (3,4), ( 3 , 5 ) (3, 5) (3,5), ( 4 , 5 ) (4, 5) (4,5), ( 4 , 6 ) (4, 6) (4,6), ( 5 , 6 ) (5, 6) (5,6).

Solution

具体见文后视频。

Code

#include <bits/stdc++.h>
#define int long long

using namespace std;

typedef pair<int, int> PII;
typedef long long LL;

void solve()
{
	int n, c;

	cin >> n >> c;

	int tot = (c + 2) * (c + 1) / 2, illigal = 0, a;
	int cnt[2] = {0};
	for (int i = 0; i < n; i ++)
	{
		cin >> a;
		cnt[a & 1] ++, illigal += (a / 2 + 1), illigal += (c - a + 1), illigal -= cnt[a & 1];
	}

	cout << tot - illigal << endl;
}

signed main()
{
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(0);

	int Data;

	cin >> Data;

	while (Data --)
		solve();

	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

E. Distance Learning Courses in MAC

Problem Statement

The New Year has arrived in the Master’s Assistance Center, which means it’s time to introduce a new feature!

Now students are given distance learning courses, with a total of n n n courses available. For the i i i-th distance learning course, a student can receive a grade ranging from x i x_i xi to y i y_i yi.

However, not all courses may be available to each student. Specifically, the j j j-th student is only given courses with numbers from l j l_j lj to r j r_j rj, meaning the distance learning courses with numbers l j , l j + 1 , … , r j l_j, l_j + 1, \ldots, r_j lj,lj+1,,rj.

The creators of the distance learning courses have decided to determine the final grade in a special way. Let the j j j-th student receive grades c l j , c l j + 1 , … , c r j c_{l_j}, c_{l_j + 1}, \ldots, c_{r_j} clj,clj+1,,crj for their distance learning courses. Then their final grade will be equal to c l j c_{l_j} clj ∣ | c l j + 1 c_{l_j + 1} clj+1 ∣ | … \ldots ∣ | c r j c_{r_j} crj, where ∣ | denotes the bitwise OR operation.

Since the chatbot for solving distance learning courses is broken, the students have asked for your help. For each of the q q q students, tell them the maximum final grade they can achieve.

Input

Each test consists of multiple test cases. The first line contains a single integer t t t ( 1 ≤ t ≤ 2 ⋅ 1 0 4 1 \leq t \leq 2 \cdot 10^4 1t2104) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer n n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \le n \le 2 \cdot 10^5 1n2105) — the number of distance learning courses.

Each of the following n n n lines contains two integers x i x_i xi and y i y_i yi ( 0 ≤ x i ≤ y i < 2 30 0 \le x_i \le y_i < 2^{30} 0xiyi<230) — the minimum and maximum grade that can be received for the i i i-th course.

The next line contains a single integer q q q ( 1 ≤ q ≤ 2 ⋅ 1 0 5 1 \le q \le 2\cdot10^5 1q2105) — the number of students.

Each of the following q q q lines contains two integers l j l_j lj and r j r_j rj ( 1 ≤ l j ≤ r j ≤ n 1 \le l_j \le r_j \le n 1ljrjn) — the minimum and maximum course numbers accessible to the j j j-th student.

It is guaranteed that the sum of n n n over all test cases and the sum of q q q over all test cases do not exceed 2 ⋅ 1 0 5 2\cdot10^5 2105.

Output

For each test case, output q q q integers, where the j j j-th integer is the maximum final grade that the j j j-th student can achieve.

Example

input
3
2
0 1
3 4
3
1 1
1 2
2 2
4
1 7
1 7
3 10
2 2
5
1 3
3 4
2 3
1 4
1 2
6
1 2
2 2
0 1
1 1
3 3
0 0
4
3 4
5 5
2 5
1 2
output
1 5 4
15 11 15 15 7
1 3 3 3

Note

In the first test case:

  1. The maximum grade for the first student is 1 1 1:

    • On the first distance learning course, he will receive a grade of 1 1 1.

    Therefore, the final grade is 1 1 1.

  2. The maximum grade for the second student is 5 5 5:

    • On the first distance learning course, he will receive a grade of 1 1 1.
    • On the second distance learning course, he will receive a grade of 4 4 4.

    Therefore, the final grade is 1 1 1 ∣ | 4 4 4 = = = 5 5 5.

  3. The maximum grade for the third student is 4 4 4:

    • On the second distance learning course, he will receive a grade of 4 4 4.

    Therefore, the final grade is 4 4 4.

In the second test case:

  1. The maximum grade for the first student is 15 15 15:

    • On the first distance learning course, he will receive a grade of 7 7 7.
    • On the second distance learning course, he will receive a grade of 4 4 4.
    • On the third distance learning course, he will receive a grade of 8 8 8.

    Therefore, the final grade is 7 7 7 ∣ | 4 4 4 ∣ | 8 8 8 = = = 15 15 15.

  2. The maximum grade for the second student is 11 11 11:

    • On the third distance learning course, he will receive a grade of 9 9 9.
    • On the fourth distance learning course, he will receive a grade of 2 2 2.

    Therefore, the final grade is 9 9 9 ∣ | 2 2 2 = = = 11 11 11.

Solution

具体见文后视频。


Code

#include <bits/stdc++.h>
#define int long long

using namespace std;

typedef pair<int, int> PII;
typedef long long LL;

void solve()
{
	int n, q;
	cin >> n;
	std::vector<int> x(n), y(n), bit(n, 30);
	std::vector<array<int, 31>> cnt(n + 1), vis(n + 1);
	for (int i = 0; i < n; i ++) {
		vis[i + 1].fill(0);
		cin >> x[i] >> y[i];
		while (bit[i] >= 0 && (x[i] >> bit[i] & 1) == (y[i] >> bit[i] & 1)) bit[i] --;
		bit[i] ++;
		for (int j = bit[i]; j <= 30; j ++) vis[i + 1][j] += (x[i] >> j & 1);
		x[i] &= ((1 << bit[i]) - 1), y[i] &= ((1 << bit[i]) - 1);
	}
	cnt[0].fill(0);
	for (int i = 1; i <= n; i ++) {
		cnt[i] = cnt[i - 1];
		for (int j = 30; j >= 0; j --)
			cnt[i][j] += (y[i - 1] >> j & 1), vis[i][j] += vis[i - 1][j];
	}

	cin >> q;
	while (q --) {
		int l, r;
		cin >> l >> r;

		int res = 0, flg = 1;
		for (int i = 30; i >= 0; i --) if (vis[r][i] - vis[l - 1][i] > 0) res += (1 << i);
		for (int i = 30; i >= 0; i --) {
			// if (l == 3 && r == 4) cout << i << ":" << cnt[r][i] - cnt[l - 1][i] << endl;
			if (vis[r][i] - vis[l - 1][i] > 0) {
				if (cnt[r][i] - cnt[l - 1][i] > 0)
				{
					res |= ((1 << i + 1) - 1), flg = 0;
					cout << res << " \n"[q == 0];
					break;
				}
			} else {
				if (cnt[r][i] - cnt[l - 1][i] > 1) {
					res |= ((1 << i + 1) - 1), flg = 0;
					cout << res << " \n"[q == 0];
					break;
				} else if (cnt[r][i] - cnt[l - 1][i] == 1)
					res |= (1 << i);
			}
		}
		if (!flg) continue;
		cout << res << " \n"[q == 0];
	}
}

signed main()
{
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(0);

	int Data;

	cin >> Data;

	while (Data --)
		solve();

	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74

视频讲解

Codeforces Round 932 (Div. 2)(A ~ E 题讲解)


最后祝大家早日在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/217969
推荐阅读
相关标签
  

闽ICP备14008679号