比赛记录 - Codeforces Round #796 (Div. 2)

比赛链接

A - Cirno's Perfect Bitmasks Classroom

Even if it's a really easy question, she won't be able to answer it
Perfect Memento in Strict Sense

Cirno's perfect bitmasks classroom has just started!

Cirno gave her students a positive integer \(x\). As an assignment, her students need to find the minimum positive integer \(y\), which satisfies the following two conditions:

\[ x\ \texttt{and}\ y > 0 \]

\[ x\ \texttt{xor}\ y > 0 \]

Where \(\texttt{and}\) is the bitwise AND operation, and \(\texttt{xor}\) is the bitwise XOR operation

Among the students was Mystia, who was truly baffled by all these new operators. Please help her!

Input

The first line of input contains a single integer \(t\) (\(1 \leq t \leq 10^3\)) — the number of input test cases

For each test case, the only line of input contains one integer \(x\) (\(1 \leq x \leq 2^{30}\))

Output

For each test case, print a single integer — the minimum number of \(y\)

Example

input

1
2
3
4
5
6
7
8
7
1
2
5
9
16
114514
1000000

output

1
2
3
4
5
6
7
3
3
1
1
17
2
64

Note

Test case 1:

\(1\; \texttt{and}\; 3=1>0\), \(1\; \texttt{xor}\; 3=2>0\)

Test case 2:

\(2\; \texttt{and}\; 3=2>0\), \(2\; \texttt{xor}\; 3=1>0\)

代码参考

Show code

CodeForces_1688Aview raw
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
/*
* @Author: Tifa
* @Description: From <https://github.com/Tiphereth-A/CP-archives>
* !!! ATTENEION: All the context below is licensed under a
* GNU Affero General Public License, Version 3.
* See <https://www.gnu.org/licenses/agpl-3.0.txt>.
*/
#include <bits/stdc++.h>
using namespace std;
#define _for(i, l, r, vals...) \
for (decltype(l + r) i = (l), i##end = (r), ##vals; i <= i##end; ++i)
#define _run_break(expressions) \
{ \
expressions; \
break; \
}
template <class T>
bool chkmin(T &a, T b) {
return b < a ? a = b, true : false;
}
template <class T>
bool chkmax(T &a, T b) {
return a < b ? a = b, true : false;
}
auto solve() -> void {
int x;
cin >> x;
int _1 = 0, _2 = 0, _3 = 0;
_for(i, 0, 30)
if (!(x & (1 << i))) _run_break(_1 = i);
_for(i, 0, 30)
if ((x & (1 << i))) _run_break(_2 = i);
_for(i, 0, 30) _3 += !!(x & (1 << i));
cout << (_3 > 1 ? (1 << _2) : ((1 << _1) | (1 << _2))) << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int _t;
cin >> _t;
while (_t--) solve();
return 0;
}

B - Patchouli's Magical Talisman

She is skilled in all kinds of magics, and is keen on inventing new one
Perfect Memento in Strict Sense

Patchouli is making a magical talisman. She initially has \(n\) magical tokens. Their magical power can be represented with positive integers \(a_1, a_2, \ldots, a_n\)

Patchouli may perform the following two operations on the tokens

  • Fusion: Patchouli chooses two tokens, removes them, and creates a new token with magical power equal to the sum of the two chosen tokens
  • Reduction: Patchouli chooses a token with an even value of magical power \(x\), removes it and creates a new token with magical power equal to \(\frac{x}{2}\)

Tokens are more effective when their magical powers are odd values. Please help Patchouli to find the minimum number of operations she needs to make magical powers of all tokens odd values

Input

Each test contains multiple test cases

The first line contains a single integer \(t\) (\(1 \leq t \leq 10^3\)) — the number of test cases. The description of the test cases follows

For each test case, the first line contains one integer \(n\) (\(1 \leq n\leq 2\cdot 10^5\)) — the initial number of tokens

The second line contains \(n\) intergers \(a_1,a_2,\ldots,a_n\) (\(1 \leq a_i \leq 10^9\)) — the initial magical power of the \(n\) tokens

It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\)

Output

For each test case, print a single integer — the minimum number of operations Patchouli needs to make all tokens have an odd value of magical power

It can be shown that under such restrictions the required sequence of operations exists

Example

input

1
2
3
4
5
6
7
8
9
4
2
1 9
3
1 1 2
3
2 4 8
3
1049600 33792 1280

output

1
2
3
4
0
1
3
10

Note

Test case 1:

\(a\) consists solely of odd numbers initially

Test case 2:

Choose the tokens with magical power of \(1\) and \(2\) and perform Fusion. Now \(a=[1,3]\), both are odd numbers

Test case 3:

Choose the tokens with magical power of \(2\) and \(8\) and perform Fusion. Now \(a=[4,10]\)

Choose the token with magical power of \(10\) and perform Reduction. Now \(a=[4,5]\)

Choose the tokens with magical power of \(4\) and \(5\) and perform Fusion. Now \(a=[9]\), and \(9\) is an odd number

It can be shown that you can not make all the magical powers odd numbers in less than \(3\) moves, so the answer is \(3\)

代码参考

Show code

CodeForces_1688Bview raw
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
/*
* @Author: Tifa
* @Description: From <https://github.com/Tiphereth-A/CP-archives>
* !!! ATTENEION: All the context below is licensed under a
* GNU Affero General Public License, Version 3.
* See <https://www.gnu.org/licenses/agpl-3.0.txt>.
*/
#include <bits/stdc++.h>
using namespace std;
#define _for(i, l, r, vals...) \
for (decltype(l + r) i = (l), i##end = (r), ##vals; i <= i##end; ++i)
#define _foreach_cref(i, container) for (const auto &i : container)
#define _all(a) (a).begin(), (a).end()
#define _run_return_void(expressions) \
{ \
expressions; \
return; \
}
template <class T>
bool chkmin(T &a, T b) {
return b < a ? a = b, true : false;
}
template <class T>
bool chkmax(T &a, T b) {
return a < b ? a = b, true : false;
}
auto solve() -> void {
int n;
cin >> n;
vector<int> v;
v.reserve(n);
_for(i, 1, n, x) {
cin >> x;
v.push_back(x);
}
int odds = count_if(_all(v), [](const int x) { return x & 1; });
if (odds) _run_return_void(cout << n - odds << '\n');
int _0 = 64;
_foreach_cref(i, v) chkmin(_0, __builtin_ctz(i));
cout << _0 + n - 1 << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int _t;
cin >> _t;
while (_t--) solve();
return 0;
}

C - Manipulating History

As a human, she can erase history of its entirety. As a Bai Ze (Hakutaku), she can create history out of nothingness
Perfect Memento in Strict Sense

Keine has the ability to manipulate history

The history of Gensokyo is a string \(s\) of length \(1\) initially. To fix the chaos caused by Yukari, she needs to do the following operations \(n\) times, for the \(i\)-th time:

  • She chooses a non-empty substring \(t_{2i-1}\) of \(s\)
  • She replaces \(t_{2i-1}\) with a non-empty string, \(t_{2i}\). Note that the lengths of strings \(t_{2i-1}\) and \(t_{2i}\) can be different

Note that if \(t_{2i-1}\) occurs more than once in \(s\), exactly one of them will be replaced

For example, let \(s=\)"marisa", \(t_{2i-1}=\)"a", and \(t_{2i}=\)"z". After the operation, \(s\) becomes "mzrisa" or "marisz"

After \(n\) operations, Keine got the final string and an operation sequence \(t\) of length \(2n\). Just as Keine thinks she has finished, Yukari appears again and shuffles the order of \(t\). Worse still, Keine forgets the initial history

Help Keine find the initial history of Gensokyo!

Recall that a substring is a sequence of consecutive characters of the string. For example, for string "abc" its substrings are: "ab", "c", "bc" and some others. But the following strings are not its substring: "ac", "cba", "acb"

Hacks

You cannot make hacks in this problem

Input

Each test contains multiple test cases. The first line contains a single integer \(T\) (\(1 \leq T \leq 10^3\)) — the number of test cases. The description of the test cases follows

The first line of each test case contains a single integer \(n\) (\(1 \le n < 10 ^ 5\)) — the number of operations

The next \(2n\) lines contains one non-empty string \(t_{i}\) — the \(i\)-th string of the shuffled sequence \(t\)

The next line contains one non-empty string \(s\) — the final string

It is guaranteed that the total length of given strings (including \(t_i\) and \(s\)) over all test cases does not exceed \(2 \cdot 10 ^ 5\). All given strings consist of lowercase English letters only

It is guaranteed that the initial string exists. It can be shown that the initial string is unique

Output

For each test case, print the initial string in one line

Example

input

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
2
a
ab
b
cd
acd
3
z
a
a
aa
yakumo
ran
yakumoran

output

1
2
a
z

Note

Test case 1:

Initially \(s\) is "a"

  • In the first operation, Keine chooses "a", and replaces it with "ab". \(s\) becomes "ab"
  • In the second operation, Keine chooses "b", and replaces it with "cd". \(s\) becomes "acd"

So the final string is "acd", and \(t=[\)"a", "ab", "b", "cd"\(]\) before being shuffled

Test case 2:

Initially \(s\) is "z"

  • In the first operation, Keine chooses "z", and replaces it with "aa". \(s\) becomes "aa"
  • In the second operation, Keine chooses "a", and replaces it with "ran". \(s\) becomes "aran"
  • In the third operation, Keine chooses "a", and replaces it with "yakumo". \(s\) becomes "yakumoran"

So the final string is "yakumoran", and \(t=[\)"z", "aa", "a", "ran", "a", "yakumo"\(]\) before being shuffled

代码参考

Show code

CodeForces_1688Cview raw
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
/*
* @Author: Tifa
* @Description: From <https://github.com/Tiphereth-A/CP-archives>
* !!! ATTENEION: All the context below is licensed under a
* GNU Affero General Public License, Version 3.
* See <https://www.gnu.org/licenses/agpl-3.0.txt>.
*/
#include <bits/stdc++.h>
using namespace std;
#define _for(i, l, r, vals...) \
for (decltype(l + r) i = (l), i##end = (r), ##vals; i <= i##end; ++i)
#define _foreach_cref(i, container) for (const auto &i : container)
#define _run_return_void(expressions) \
{ \
expressions; \
return; \
}
template <class T>
bool chkmin(T &a, T b) {
return b < a ? a = b, true : false;
}
template <class T>
bool chkmax(T &a, T b) {
return a < b ? a = b, true : false;
}
auto solve() -> void {
int n;
cin >> n;
string s;
bitset<256> _;
_for(i, 1, n * 2 + 1) {
cin >> s;
_foreach_cref(ch, s) _[ch].flip();
}
_for(i, 'a', 'z')
if (_[i]) _run_return_void(cout << (char)i << '\n');
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int _t;
cin >> _t;
while (_t--) solve();
return 0;
}

D - The Enchanted Forest

The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached
Perfect Memento in Strict Sense

Marisa comes to pick mushrooms in the Enchanted Forest

The Enchanted forest can be represented by \(n\) points on the \(X\)-axis numbered \(1\) through \(n\). Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by \(a_1,a_2,\ldots,a_n\)

Marisa can start out at any point in the forest on minute \(0\). Each minute, the followings happen in order:

  • She moves from point \(x\) to \(y\) (\(|x-y|\le 1\), possibly \(y=x\))
  • She collects all mushrooms on point \(y\)
  • A new mushroom appears on each point in the forest

Note that she cannot collect mushrooms on minute \(0\)

Now, Marisa wants to know the maximum number of mushrooms she can pick after \(k\) minutes

Input

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

The first line of each test case contains two integers \(n\), \(k\) (\(1 \le n \le 2 \cdot 10 ^ 5\), \(1\le k \le 10^9\)) — the number of positions with mushrooms and the time Marisa has, respectively

The second line of each test case contains \(n\) integers \(a_1,a_2,\ldots,a_n\) (\(1 \le a_i \le 10^9\)) — the initial number of mushrooms on point \(1,2,\ldots,n\)

It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10 ^ 5\)

Output

For each test case, print the maximum number of mushrooms Marisa can pick after \(k\) minutes

Example

input

1
2
3
4
5
6
7
8
9
4
5 2
5 6 1 2 3
5 7
5 6 1 2 3
1 2
999999
5 70000
1000000000 1000000000 1000000000 1000000000 1000000000

output

1
2
3
4
12
37
1000000
5000349985

Note

Test case 1:

Marisa can start at \(x=2\). In the first minute, she moves to \(x=1\) and collect \(5\) mushrooms. The number of mushrooms will be \([1,7,2,3,4]\). In the second minute, she moves to \(x=2\) and collects \(7\) mushrooms. The numbers of mushrooms will be \([2,1,3,4,5]\). After \(2\) minutes, Marisa collects \(12\) mushrooms

It can be shown that it is impossible to collect more than \(12\) mushrooms

Test case 2:

This is one of her possible moving path:

\(2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5\)

It can be shown that it is impossible to collect more than \(37\) mushrooms

代码参考

Show code

CodeForces_1687Aview raw
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
/*
* @Author: Tifa
* @Description: From <https://github.com/Tiphereth-A/CP-archives>
* !!! ATTENEION: All the context below is licensed under a
* GNU Affero General Public License, Version 3.
* See <https://www.gnu.org/licenses/agpl-3.0.txt>.
*/
#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
#define _for(i, l, r, vals...) \
for (decltype(l + r) i = (l), i##end = (r), ##vals; i <= i##end; ++i)
#define _all(a) (a).begin(), (a).end()
#define _run_return_void(expressions) \
{ \
expressions; \
return; \
}
template <class T>
bool chkmin(T &a, T b) {
return b < a ? a = b, true : false;
}
template <class T>
bool chkmax(T &a, T b) {
return a < b ? a = b, true : false;
}
bool _[26];
auto solve() -> void {
i64 n, m;
cin >> n >> m;
vector<i64> v;
v.reserve(n);
_for(i, 1, n, x) {
cin >> x;
v.push_back(x);
}
if (m >= n)
_run_return_void(cout << accumulate(_all(v), (i64)(0)) +
(n) * (m * 2 - n - 1) / 2
<< '\n');
i64 sum = 0, maxs = 0;
_for(i, 0, n - 1) {
sum += v[i];
if (i >= m) sum -= v[i - m];
chkmax(maxs, sum);
}
cout << maxs + m * (m - 1) / 2 << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int _t;
cin >> _t;
while (_t--) solve();
return 0;
}

E - Railway System

As for the technology in the outside world, it is really too advanced for Gensokyo to even look up to
—Yasaka Kanako, Symposium of Post-mysticism

This is an interactive problem

Under the direct supervision of Kanako and the Moriya Shrine, the railway system of Gensokyo is finally finished. GSKR (Gensokyo Railways) consists of \(n\) stations with \(m\) bidirectional tracks connecting them. The \(i\)-th track has length \(l_i\) (\(1\le l_i\le 10^6\)). Due to budget limits, the railway system may not be connected, though there may be more than one track between two stations

The value of a railway system is defined as the total length of its all tracks. The maximum (or minimum) capacity of a railway system is defined as the maximum (or minimum) value among all of the currently functional system's full spanning forest

In brief, full spanning forest of a graph is a spanning forest with the same connectivity as the given graph

Kanako has a simulator only able to process no more than \(2m\) queries. The input of the simulator is a string \(s\) of length \(m\), consisting of characters 0 and/or 1. The simulator will assume the \(i\)-th track functional if \(s_i=\) 1. The device will then tell Kanako the maximum capacity of the system in the simulated state

Kanako wants to know the the minimum capacity of the system with all tracks functional with the help of the simulator

The structure of the railway system is fixed in advance. In other words, the interactor is not adaptive

Input

The first and only line of input contains two integers \(n,m\) (\(2 \leq n \leq 200\), \(1\le m \le 500\)) — the number of stations and tracks

Interaction

Begin the interaction by reading \(n,m\)

To make a query, print "? \(s\)" (without quotes, \(s\) is a string of length \(m\), consisting of characters 0 and/or 1). Then you should read our response from standard input — the maximum capacity of the system in the simulated state

If your program has made an invalid query or has run out of tries, the interactor will terminate immediately and your program will get a verdict Wrong answer

To give the final answer, print "! \(L\)" (without the quotes, \(L\) is the minimum capacity of the system with all tracks functional). Note that giving this answer is not counted towards the limit of \(2m\) queries

After printing a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use:

  • fflush(stdout) or cout.flush() in C++;
  • System.out.flush() in Java;
  • flush(output) in Pascal;
  • stdout.flush() in Python;
  • see documentation for other languages

Hacks

The first line of input must contain two integers \(n,m\) (\(2 \leq n \leq 200\), \(1\le m \le 500\)) — the number of stations and tracks

The next \(m\) lines of input must contain exactly \(3\) space-separated integers \(u_i\), \(v_i\), \(l_i\) (\(1\le u_i,v_i \le n\), \(u_i\ne v_i\), \(1 \leq l_i \leq 10^6\)) — the endpoints and the length of the \(i\)-th track

Example

input

1
2
3
4
5
6
7
8
9
5 4

0

5

9

7

output

1
2
3
4
5
6
7
8
9
? 0000

? 1110

? 1111

? 1101

! 7

Note

Here is the graph of the example, satisfying \(l_i=i\)

代码参考

Show code

CodeForces_1687Bview raw
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
/*
* @Author: Tifa
* @Description: From <https://github.com/Tiphereth-A/CP-archives>
* !!! ATTENEION: All the context below is licensed under a
* GNU Affero General Public License, Version 3.
* See <https://www.gnu.org/licenses/agpl-3.0.txt>.
*/
#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
using pii = pair<int, int>;
#define _for(i, l, r, vals...) \
for (decltype(l + r) i = (l), i##end = (r), ##vals; i <= i##end; ++i)
#define _all(a) (a).begin(), (a).end()
#define _run_continue(expressions) \
{ \
expressions; \
continue; \
}
template <class T>
bool chkmin(T &a, T b) {
return b < a ? a = b, true : false;
}
template <class T>
bool chkmax(T &a, T b) {
return a < b ? a = b, true : false;
}
auto solve() -> void {
int n, m;
cin >> n >> m;
vector<pii> l;
_for(i, 1, m, x) {
cout << "? ";
_for(j, 1, m) cout << "01"[j == i];
cout << endl;
cin >> x;
l.emplace_back(x, i);
}
sort(_all(l));
vector<bool> q(m + 1);
i64 ans = 0;
for (auto &&[v, k] : l) {
q[k] = 1;
cout << "? ";
_for(j, 1, m) cout << "01"[q[j]];
cout << endl;
int _;
cin >> _;
if (_ != ans + v) _run_continue(q[k] = 0);
ans += v;
}
cout << "! " << ans << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
solve();
return 0;
}

F - Sanae and Giant Robot

Is it really?! The robot only existing in my imagination?! The Colossal Walking Robot?!!
— Kochiya Sanae

Sanae made a giant robot — Hisoutensoku, but something is wrong with it. To make matters worse, Sanae can not figure out how to stop it, and she is forced to fix it on-the-fly The state of a robot can be represented by an array of integers of length \(n\). Initially, the robot is at state \(a\). She wishes to turn it into state \(b\)

As a great programmer, Sanae knows the art of copy-and-paste. In one operation, she can choose some segment from given segments, copy the segment from \(b\) and paste it into the same place of the robot, replacing the original state there. However, she has to ensure that the sum of \(a\) does not change after each copy operation in case the robot go haywire. Formally, Sanae can choose segment \([l,r]\) and assign \(a_i = b_i\) (\(l\le i\le r\)) if \(\sum\limits_{i=1}^n a_i\) does not change after the operation

Determine whether it is possible for Sanae to successfully turn the robot from the initial state \(a\) to the desired state \(b\) with any (possibly, zero) operations

Input

Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 2\cdot 10^4\)) — the number of test cases. The descriptions of the test cases follow

The first line of each test case contains two integers \(n\), \(m\) (\(2 \leq n\leq 2\cdot 10^5\), \(1 \leq m\leq 2\cdot 10^5\)) — the length of \(a\), \(b\) and the number of segments

The second line contains \(n\) intergers \(a_1,a_2,\ldots,a_n\) (\(1 \leq a_i \leq 10^9\)) — the initial state \(a\)

The third line contains \(n\) intergers \(b_1,b_2,\ldots,b_n\) (\(1 \leq b_i \leq 10^9\)) — the desired state \(b\)

Then \(m\) lines follow, the \(i\)-th line contains two intergers \(l_i,r_i\) (\(1 \leq l_i < r_i \leq n\)) — the segments that can be copy-pasted by Sanae

It is guaranteed that both the sum of \(n\) and the sum of \(m\) over all test cases does not exceed \(2 \cdot 10 ^ 5\)

Output

For each test case, print "YES" (without quotes) if \(a\) can be turned into \(b\), or "NO" (without quotes) otherwise

You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response)

Example

input

1
2
3
4
5
6
7
8
9
10
11
2
5 2
1 5 4 2 3
3 2 5 4 1
1 3
2 5
5 2
1 5 4 2 3
3 2 4 5 1
1 2
2 4

output

1
2
YES
NO

Note

Test case 1:

One possible way of turning \(a\) to \(b\):

First, select \([1,3]\). After the operation, \(a=[3,2,5,2,3]\)

Then, select \([2,5]\). After the operation, \(a=[3,2,5,4,1]=b\)

Test case 2:

It can be shown that it is impossible to turn \(a\) into \(b\)

代码参考

Show code

CodeForces_1687Cview raw
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
75
76
77
78
79
80
81
82
83
84
/*
* @Author: Tifa
* @Description: From <https://github.com/Tiphereth-A/CP-archives>
* !!! ATTENEION: All the context below is licensed under a
* GNU Affero General Public License, Version 3.
* See <https://www.gnu.org/licenses/agpl-3.0.txt>.
*/
#include <bits/extc++.h>
using namespace std;
using i64 = std::int64_t;
using vi = std::vector<int>;
using vi64 = std::vector<i64>;
#define for_(i, l, r, vars...) \
for (decltype(l + r) i = (l), i##end = (r), ##vars; i <= i##end; ++i)
#define foreach_cref_(i, container) for (const auto &i : container)
#define all_(a) (a).begin(), (a).end()
#define run_exec_(expressions, after_process) \
{ \
expressions; \
after_process; \
}
#define run_continue_(expressions) run_exec_(expressions, continue)
template <class T>
bool chkmin(T &a, T b) {
return b < a ? a = b, true : false;
}
template <class T>
bool chkmax(T &a, T b) {
return a < b ? a = b, true : false;
}
auto solve([[maybe_unused]] int t_) -> void {
int n, m;
cin >> n >> m;
vi64 d;
d.reserve(n + 1);
d.push_back(0);
for_(i, 1, n, x) {
cin >> x;
d.push_back(x);
}
for_(i, 1, n, x) {
cin >> x;
d[i] -= x;
}
partial_sum(all_(d), d.begin());
set<int> s;
queue<int> q;
for_(i, 1, n) {
if (!d[i]) run_continue_(q.push(i));
s.insert(i);
}
vector<vi> g(n + 1);
for_(i, 1, m, l, r) {
cin >> l >> r;
g[l - 1].push_back(r);
g[r].push_back(l - 1);
}
int cnt = 0;
while (!q.empty()) {
++cnt;
auto &&now = q.front();
foreach_cref_(i, g[now]) {
if (d[i]) continue;
auto &&[l, r] = minmax(now, i);
for (auto it = s.lower_bound(l); it != s.end() && *it <= r;
it = s.erase(it)) {
q.push(*it);
d[*it] = 0;
}
}
q.pop();
}
cout << (cnt == n ? "YES\n" : "NO\n");
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int i_ = 0;
int t_;
std::cin >> t_;
for (i_ = 1; i_ <= t_; ++i_) solve(i_);
return 0;
}