AtCoder Beginner Contest 342 (ABCDEFG题)视频讲解

A - Yay!

Problem Statement

You are given a string S S S consisting of lowercase English letters. The length of S S S is between 3 3 3 and 100 100 100, inclusive.

All characters but one of S S S are the same.

Find x x x such that the x x x-th character of S S S differs from all other characters.

Constraints

S S S is a string of length between 3 3 3 and 100 100 100, inclusive, consisting of two different lowercase English letters.

All characters but one of S S S are the same.

Input

The input is given from Standard Input in the following format:

S S S

Output

Print the answer.

Sample Input 1
yay
Sample Output 1
2

The second character of yay differs from the first and third characters.

Sample Input 2
egg
Sample Output 2
1
Sample Input 3
zzzzzwz
Sample Output 3
6

Solution

具体见文末视频。


Code

cpp 复制代码
#include <bits/stdc++.h>
#define int long long

using namespace std;

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

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

	string S;

	cin >> S;

	S = ' ' + S;
	unordered_map<char, int> Cnt;
	for (int i = 1; i < S.size(); i ++)
		Cnt[S[i]] ++;

	for (auto v : Cnt)
		if (v.second == 1)
		{
			for (int i = 1; i < S.size(); i ++)
				if (S[i] == v.first)
				{
					cout << i << endl;
					return 0;
				}
		}

	return 0;
}

B - Which is ahead?

Problem Statement

There are N N N people standing in a line. The person standing at the i i i-th position from the front is person P i P_i Pi.

Process Q Q Q queries. The i i i-th query is as follows:

You are given integers A i A_i Ai and B i B_i Bi. Between person A i A_i Ai and person B i B_i Bi, print the person number of the person standing further to the front.

Constraints

All inputs are integers.
1 ≤ N ≤ 100 1 \leq N \leq 100 1≤N≤100
1 ≤ P i ≤ N 1 \leq P_i \leq N 1≤Pi≤N
P i ≠ P j ( i ≠ j ) P_i \neq P_j\ (i \neq j) Pi=Pj (i=j)
1 ≤ Q ≤ 100 1 \leq Q \leq 100 1≤Q≤100
KaTeX parse error: Expected 'EOF', got '&' at position 12: 1 \leq A_i &̲lt; B_i \leq N

Input

The input is given from Standard Input in the following format:

N N N
P 1 P_1 P1 ... \ldots ... P N P_N PN
Q Q Q
A 1 A_1 A1 B 1 B_1 B1
⋮ \vdots ⋮
A Q A_Q AQ B Q B_Q BQ

Output

Print Q Q Q lines. The i i i-th line should contain the response for the i i i-th query.

Sample Input 1
3
2 1 3
3
2 3
1 2
1 3
Sample Output 1
2
2
1

In the first query, person 2 2 2 is at the first position from the front, and person 3 3 3 is at the third position, so person 2 2 2 is further to the front.

In the second query, person 1 1 1 is at the second position from the front, and person 2 2 2 is at the first position, so person 2 2 2 is further to the front.

In the third query, person 1 1 1 is at the second position from the front, and person 3 3 3 is at the third position, so person 1 1 1 is further to the front.

Sample Input 2
7
3 7 2 1 6 5 4
13
2 3
1 2
1 3
3 6
3 7
2 4
3 7
1 3
4 7
1 6
2 4
1 3
1 3
Sample Output 2
3
2
3
3
3
2
3
3
7
1
2
3
3

Solution

具体见文末视频。

Code

cpp 复制代码
#include <bits/stdc++.h>
#define int long long

using namespace std;

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

const int SIZE = 1e2 + 10;

int N, Q;
int P[SIZE];

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

	cin >> N;

	int X;
	for (int i = 1; i <= N; i ++)
		cin >> X, P[X] = i;

	cin >> Q;

	while (Q --)
	{
		int l, r;

		cin >> l >> r;

		if (P[l] > P[r]) cout << r << endl;
		else cout << l << endl;
	}

	return 0;
}

C - Many Replacement

Problem Statement

You are given a string S S S of length N N N consisting of lowercase English letters.

You will perform an operation Q Q Q times on the string S S S.

The i i i-th operation ( 1 ≤ i ≤ Q ) (1\leq i\leq Q) (1≤i≤Q) is represented by a pair of characters ( c i , d i ) (c _ i,d _ i) (ci,di), which corresponds to the following operation:

Replace all occurrences of the character c i c _ i ci in S S S with the character d i d _ i di.

Print the string S S S after all operations are completed.

Constraints

1 ≤ N ≤ 2 × 1 0 5 1\leq N\leq2\times10^5 1≤N≤2×105
S S S is a string of length N N N consisting of lowercase English letters.
1 ≤ Q ≤ 2 × 1 0 5 1\leq Q\leq2\times10^5 1≤Q≤2×105
c i c _ i ci and d i d _ i di are lowercase English letters ( 1 ≤ i ≤ Q ) (1\leq i\leq Q) (1≤i≤Q).
N N N and Q Q Q are integers.

Input

The input is given from Standard Input in the following format:

N N N
S S S
Q Q Q
c 1 c _ 1 c1 d 1 d _ 1 d1
c 2 c _ 2 c2 d 2 d _ 2 d2
⋮ \vdots ⋮
c Q c _ Q cQ d Q d _ Q dQ

Output

Print the string S S S after all operations are completed.

Sample Input 1
7
atcoder
4
r a
t e
d v
a r
Sample Output 1
recover

S S S changes as follows: atcoderatcodeaaecodeaaecovearecover.

For example, in the fourth operation, all occurrences of a in S = S={} S=aecovea (the first and seventh characters) are replaced with r, resulting in S = S={} S=recover.

After all operations are completed, S = S={} S=recover, so print recover.

Sample Input 2
3
abc
4
a a
s k
n n
z b
Sample Output 2
abc

There may be operations where c i = d i c _ i=d _ i ci=di or S S S does not contain c i c _ i ci.

Sample Input 3
34
supercalifragilisticexpialidocious
20
g c
l g
g m
c m
r o
s e
a a
o f
f s
e t
t l
d v
p k
v h
x i
h n
n j
i r
s i
u a
Sample Output 3
laklimamriiamrmrllrmlrkramrjimrial

Solution

具体见文末视频。


Code

cpp 复制代码
#include <bits/stdc++.h>
#define int long long

using namespace std;

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

const int SIZE = 2e5 + 10;

int N, Q;
string S;
int P[27], Vis[27];

int Find(int x)
{
	if (P[x] != x) P[x] = Find(P[x]);
	return P[x];
}

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

	cin >> N >> S;

	S = ' ' + S;
	for (int i = 0; i < 26; i ++)
		P[i] = i;

	cin >> Q;

	while (Q --)
	{
		char a, b;

		cin >> a >> b;

		for (int i = 0; i < 26; i ++)
			if (P[i] == a - 'a')
				P[i] = b - 'a';
	}

	for (int i = 1; i <= N; i ++)
		cout << char(P[S[i] - 'a'] + 'a');

	return 0;
}

D - Square Pair

Problem Statement

You are given a sequence of non-negative integers A = ( A 1 , ... , A N ) A=(A_1,\ldots,A_N) A=(A1,...,AN) of length N N N. Find the number of pairs of integers ( i , j ) (i,j) (i,j) that satisfy both of the following conditions:
KaTeX parse error: Expected 'EOF', got '&' at position 9: 1\leq i &̲lt; j\leq N
A i A j A_i A_j AiAj is a square number.

Here, a non-negative integer a a a is called a square number when it can be expressed as a = d 2 a=d^2 a=d2 using some non-negative integer d d d.

Constraints

All inputs are integers.
2 ≤ N ≤ 2 × 1 0 5 2\leq N\leq 2\times 10^5 2≤N≤2×105
0 ≤ A i ≤ 2 × 1 0 5 0\leq A_i\leq 2\times 10^5 0≤Ai≤2×105

Input

The input is given from Standard Input in the following format:

N N N
A 1 A_1 A1 ... \ldots ... A N A_N AN

Output

Print the answer.

Sample Input 1
5
0 3 2 8 12
Sample Output 1
6

Six pairs of integers, ( i , j ) = ( 1 , 2 ) , ( 1 , 3 ) , ( 1 , 4 ) , ( 1 , 5 ) , ( 2 , 5 ) , ( 3 , 4 ) (i,j)=(1,2),(1,3),(1,4),(1,5),(2,5),(3,4) (i,j)=(1,2),(1,3),(1,4),(1,5),(2,5),(3,4), satisfy the conditions.

For example, A 2 A 5 = 36 A_2A_5=36 A2A5=36, and 36 36 36 is a square number, so the pair ( i , j ) = ( 2 , 5 ) (i,j)=(2,5) (i,j)=(2,5) satisfies the conditions.

Sample Input 2
8
2 2 4 6 3 100 100 25
Sample Output 2
7

Solution

两种方法可以求解,具体见文末视频。


Code

方法1:

cpp 复制代码
#include <bits/stdc++.h>
#define int long long

using namespace std;

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

const int SIZE = 2e5 + 10;

int N;
int A[SIZE], Vis[SIZE], Cnt[SIZE];
int fact[SIZE], id, id2;
PII f2[SIZE];

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

	cin >> N;

	int mx = 0;
	for (int i = 1; i <= N; i ++)
		cin >> A[i], Cnt[A[i]] ++, mx = max(mx, A[i]);

	int Result = 0;
	for (int i = 1; i <= mx; i ++)
	{
		id = 0, id2 = 0;
		for (int j = 1; j <= i / j; j ++)
			if (i % j == 0)
			{
				fact[ ++ id] = j;
				if (i / j != j) fact[ ++ id] = i / j;
			}
		
		for (int j = 1; j <= id; j ++)
			for (int k = 1; k <= id; k ++)
				f2[ ++ id2] = {fact[j] * fact[k], i * i / fact[j] / fact[k]};

		int T1 = 0, T2 = 0;
		for (int j = 1; j <= id2; j ++)
		{
			auto v = f2[j];
			if (v.first > mx || v.second > mx) continue;
			if (v.first == v.second && !Vis[v.first])
				T1 += Cnt[v.first] * max(0ll, Cnt[v.first] - 1) / 2, Vis[v.first] = Vis[v.second] = 1;
			else if (!Vis[v.first])
				T2 += Cnt[v.first] * Cnt[v.second], Vis[v.first] = Vis[v.second] = 1;
		}
		for (int j = 1; j <= id2; j ++)
		{
			auto v = f2[j];
			if (v.first > mx || v.second > mx) continue;
			Vis[v.first] = Vis[v.second] = 0;
		}
		Result += T1 + T2;
	}

	int Zero = 0;
	for (int i = 1; i <= N; i ++)
	{
		if (A[i]) continue;
		Result += N - Zero - 1;
		Zero ++;
	}
	cout << Result << endl;

	return 0;
}

方法2:

cpp 复制代码
#include <bits/stdc++.h>
#define int long long

using namespace std;

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

const int SIZE = 2e5 + 10;

int N;
int A[SIZE], Cnt[SIZE];
int primes[SIZE], cnt;
bool st[SIZE];

void get_primes(int n)
{
    for (int i = 2; i <= n; i ++ )
    {
        if (!st[i]) primes[cnt ++ ] = i;
        for (int j = 0; primes[j] <= n / i; j ++ )
        {
            st[primes[j] * i] = true;
            if (i % primes[j] == 0) break;
        }
    }
}

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

	cin >> N;

	get_primes(SIZE - 10);

	int Result = 0;
	for (int i = 1; i <= N; i ++)
	{
		cin >> A[i];
		if (!A[i]) continue;
		for (int j = 0; j < cnt; j ++)
		{
			if (primes[j] * primes[j] > A[i]) break;
			while (A[i] && A[i] % (primes[j] * primes[j]) == 0)
				A[i] /= (primes[j] * primes[j]);
		}
		Result += Cnt[A[i]];
		Cnt[A[i]] ++;
	}
	
	int Zero = 0;
	for (int i = 1; i <= N; i ++)
	{
		if (A[i]) continue;
		Result += N - Zero - 1;
		Zero ++;
	}

	cout << Result << endl;

	return 0;
}

E - Last Train

Problem Statement

In the country of AtCoder, there are N N N stations: station 1 1 1, station 2 2 2, ... \ldots ..., station N N N.

You are given M M M pieces of information about trains in the country. The i i i-th piece of information ( 1 ≤ i ≤ M ) (1\leq i\leq M) (1≤i≤M) is represented by a tuple of six positive integers ( l i , d i , k i , c i , A i , B i ) (l _ i,d _ i,k _ i,c _ i,A _ i,B _ i) (li,di,ki,ci,Ai,Bi), which corresponds to the following information:

For each t = l i , l i + d i , l i + 2 d i , ... , l i + ( k i − 1 ) d i t=l _ i,l _ i+d _ i,l _ i+2d _ i,\ldots,l _ i+(k _ i-1)d _ i t=li,li+di,li+2di,...,li+(ki−1)di, there is a train as follows:

The train departs from station A i A _ i Ai at time t t t and arrives at station B i B _ i Bi at time t + c i t+c _ i t+ci.

No trains exist other than those described by this information, and it is impossible to move from one station to another by any means other than by train.

Also, assume that the time required for transfers is negligible.

Let f ( S ) f(S) f(S) be the latest time at which one can arrive at station N N N from station S S S.

More precisely, f ( S ) f(S) f(S) is defined as the maximum value of t t t for which there is a sequence of tuples of four integers ( ( t i , c i , A i , B i ) ) i = 1 , 2 , ... , k \big((t _ i,c _ i,A _ i,B _ i)\big) _ {i=1,2,\ldots,k} ((ti,ci,Ai,Bi))i=1,2,...,k that satisfies all of the following conditions:
t ≤ t 1 t\leq t _ 1 t≤t1
A 1 = S , B k = N A _ 1=S,B _ k=N A1=S,Bk=N
B i = A i + 1 B _ i=A _ {i+1} Bi=Ai+1 for all 1 ≤ i < k 1\leq i\lt k 1≤i<k,

For all 1 ≤ i ≤ k 1\leq i\leq k 1≤i≤k, there is a train that departs from station A i A _ i Ai at time t i t _ i ti and arrives at station B i B _ i Bi at time t i + c i t _ i+c _ i ti+ci.
t i + c i ≤ t i + 1 t _ i+c _ i\leq t _ {i+1} ti+ci≤ti+1 for all 1 ≤ i < k 1\leq i\lt k 1≤i<k.

If no such t t t exists, set f ( S ) = − ∞ f(S)=-\infty f(S)=−∞.

Find f ( 1 ) , f ( 2 ) , ... , f ( N − 1 ) f(1),f(2),\ldots,f(N-1) f(1),f(2),...,f(N−1).

Constraints

2 ≤ N ≤ 2 × 1 0 5 2\leq N\leq2\times10 ^ 5 2≤N≤2×105
1 ≤ M ≤ 2 × 1 0 5 1\leq M\leq2\times10 ^ 5 1≤M≤2×105
1 ≤ l i , d i , k i , c i ≤ 1 0 9 ( 1 ≤ i ≤ M ) 1\leq l _ i,d _ i,k _ i,c _ i\leq10 ^ 9\ (1\leq i\leq M) 1≤li,di,ki,ci≤109 (1≤i≤M)
1 ≤ A i , B i ≤ N ( 1 ≤ i ≤ M ) 1\leq A _ i,B _ i\leq N\ (1\leq i\leq M) 1≤Ai,Bi≤N (1≤i≤M)
A i ≠ B i ( 1 ≤ i ≤ M ) A _ i\neq B _ i\ (1\leq i\leq M) Ai=Bi (1≤i≤M)

All input values are integers.

Input

The input is given from Standard Input in the following format:

N N N M M M
l 1 l _ 1 l1 d 1 d _ 1 d1 k 1 k _ 1 k1 c 1 c _ 1 c1 A 1 A _ 1 A1 B 1 B _ 1 B1
l 2 l _ 2 l2 d 2 d _ 2 d2 k 2 k _ 2 k2 c 2 c _ 2 c2 A 2 A _ 2 A2 B 2 B _ 2 B2
⋮ \vdots ⋮
l M l _ M lM d M d _ M dM k M k _ M kM c M c _ M cM A M A _ M AM B M B _ M BM

Output

Print N − 1 N-1 N−1 lines.

The k k k-th line should contain f ( k ) f(k) f(k) if f ( k ) ≠ − ∞ f(k)\neq-\infty f(k)=−∞, and Unreachable if f ( k ) = − ∞ f(k)=-\infty f(k)=−∞.

Sample Input 1
6 7
10 5 10 3 1 3
13 5 10 2 3 4
15 5 10 7 4 6
3 10 2 4 2 5
7 10 2 3 5 6
5 3 18 2 2 3
6 3 20 4 2 1
Sample Output 1
55
56
58
60
17

The following diagram shows the trains running in the country (information about arrival and departure times is omitted).

Consider the latest time at which one can arrive at station 6 6 6 from station 2 2 2.

As shown in the following diagram, one can arrive at station 6 6 6 by departing from station 2 2 2 at time 56 56 56 and moving as station 2 → 2\rightarrow 2→ station 3 → 3\rightarrow 3→ station 4 → 4\rightarrow 4→ station 6 6 6.

It is impossible to depart from station 2 2 2 after time 56 56 56 and arrive at station 6 6 6, so f ( 2 ) = 56 f(2)=56 f(2)=56.

Sample Input 2
5 5
1000000000 1000000000 1000000000 1000000000 1 5
5 9 2 6 2 3
10 4 1 6 2 3
1 1 1 1 3 5
3 1 4 1 5 1
Sample Output 2
1000000000000000000
Unreachable
1
Unreachable

There is a train that departs from station 1 1 1 at time 1 0 18 10 ^ {18} 1018 and arrives at station 5 5 5 at time 1 0 18 + 1 0 9 10 ^ {18}+10 ^ 9 1018+109. There are no trains departing from station 1 1 1 after that time, so f ( 1 ) = 1 0 18 f(1)=10 ^ {18} f(1)=1018.

As seen here, the answer may not fit within a 32 bit ⁡ 32\operatorname{bit} 32bit integer.

Also, both the second and third pieces of information guarantee that there is a train that departs from station 2 2 2 at time 14 14 14 and arrives at station 3 3 3 at time 20 20 20.

As seen here, some trains may appear in multiple pieces of information.

Sample Input 3
16 20
4018 9698 2850 3026 8 11
2310 7571 7732 1862 13 14
2440 2121 20 1849 11 16
2560 5115 190 3655 5 16
1936 6664 39 8822 4 16
7597 8325 20 7576 12 5
5396 1088 540 7765 15 1
3226 88 6988 2504 13 5
1838 7490 63 4098 8 3
1456 5042 4 2815 14 7
3762 6803 5054 6994 10 9
9526 6001 61 8025 7 8
5176 6747 107 3403 1 5
2014 5533 2031 8127 8 11
8102 5878 58 9548 9 10
3788 174 3088 5950 3 13
7778 5389 100 9003 10 15
556 9425 9458 109 3 11
5725 7937 10 3282 2 9
6951 7211 8590 1994 15 12
Sample Output 3
720358
77158
540926
255168
969295
Unreachable
369586
466218
343148
541289
42739
165772
618082
16582
591828

Solution

具体见文末视频。


Code

cpp 复制代码
#include <bits/stdc++.h>
#define int long long

using namespace std;

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

const int SIZE = 2e5 + 10, INF = 3e18 + 1;

int N, M;
struct Information
{
	int l, d, r, c, u;
	bool operator< (const Information &T)const { return r < T.r; }
}w[SIZE];
int h[SIZE], e[SIZE], ne[SIZE], idx;
int Dist[SIZE], Vis[SIZE];

void add(int a, int b, Information c)
{
	e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx ++;
}

void Dijkstra(int S)
{
	memset(Dist, -0x3f, sizeof Dist);
	priority_queue<Information, vector<Information>> Heap;
	Heap.push({INF, 0, INF, 0, S}), Dist[S] = INF;

	while (Heap.size())
	{
		auto T = Heap.top();
		Heap.pop();

		int u = T.u;
		if (Vis[u]) continue;
		Vis[u] = 1;

		for (int i = h[u]; ~i; i = ne[i])
		{
			int j = e[i], Time;
			auto v = w[i];
			v.l += v.c, v.r += v.c;
			if (T.r >= v.r) Time = v.r;
			else Time = v.l + (T.r - v.l) / v.d * v.d;
			Dist[j] = max(Time - v.c, Dist[j]), v.r = Time - v.c, v.u = j;
			Heap.push(v);
		}
	}
}

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

	memset(h, -1, sizeof h);

	cin >> N >> M;

	int l, d, k, c, a, b;
	for (int i = 1; i <= M; i ++)
		cin >> l >> d >> k >> c >> a >> b, add(b, a, {l, d, l + (k - 1) * d, c});

	Dijkstra(N);

	for (int i = 1; i < N; i ++)
		if (Dist[i] < 0) cout << "Unreachable" << endl;
		else cout << Dist[i] << endl;

	return 0;
}

F - Black Jack

Problem Statement

You will play a game against a dealer.

The game uses a D D D-sided die (dice) that shows an integer from 1 1 1 to D D D with equal probability, and two variables x x x and y y y initialized to 0 0 0. The game proceeds as follows:

You may perform the following operation any number of times: roll the die and add the result to x x x. You can choose whether to continue rolling or not after each roll.

Then, the dealer will repeat the following operation as long as KaTeX parse error: Expected 'EOF', got '&' at position 3: y &̲lt; L: roll the die and add the result to y y y.

If KaTeX parse error: Expected 'EOF', got '&' at position 3: x &̲gt; N, you lose. Otherwise, you win if KaTeX parse error: Expected 'EOF', got '&' at position 3: y &̲gt; N or KaTeX parse error: Expected 'EOF', got '&' at position 3: x &̲gt; y and lose if neither is satisfied.

Determine the probability of your winning when you act in a way that maximizes the probability of winning.

Constraints

All inputs are integers.
1 ≤ L ≤ N ≤ 2 × 1 0 5 1 \leq L \leq N \leq 2 \times 10^5 1≤L≤N≤2×105
1 ≤ D ≤ N 1 \leq D \leq N 1≤D≤N

Input

The input is given from Standard Input in the following format:

N N N L L L D D D

Output

Print the answer. Your output will be considered correct if its absolute or relative error from the true value is at most 1 0 − 6 10^{-6} 10−6.

Sample Input 1
3 2 2
Sample Output 1
0.468750000000000

It can be proved that the optimal strategy is to continue rolling as long as x x x is not greater than 2 2 2.

Sample Input 2
200000 200000 200000
Sample Output 2
0.999986408692793

Solution

方法1:利用线段树

方法2:前缀和方法

具体见文末视频。


Code

方法1:

cpp 复制代码
#include <bits/stdc++.h>
#define int long long

using namespace std;

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

const int SIZE = 1e6 + 10;

int N, L, D;
double f[SIZE], g[SIZE], dp[SIZE];
struct Segment
{
	int l, r;
	double Sum;
}Tree[SIZE << 2];

void Pushup(int u)
{
	Tree[u].Sum = Tree[u << 1].Sum + Tree[u << 1 | 1].Sum;
}

void Build(int u, int l, int r)
{
	Tree[u] = {l, r};
	if (l == r) return;
	int mid = l + r >> 1;
	Build(u << 1, l, mid), Build(u << 1 | 1, mid + 1, r);
}

void Modify(int u, int x, double d)
{
	if (Tree[u].l == Tree[u].r)
	{
		Tree[u].Sum = d;
		return;
	}

	int mid = Tree[u].l + Tree[u].r >> 1;
	if (mid >= x) Modify(u << 1, x, d);
	else Modify(u << 1 | 1, x, d);
	Pushup(u);
}

double Query(int u, int l, int r)
{
	if (Tree[u].l >= l && Tree[u].r <= r)
		return Tree[u].Sum;

	int mid = Tree[u].l + Tree[u].r >> 1;
	double Result = 0;
	if (mid >= l) Result += Query(u << 1, l, r);
	if (mid < r) Result += Query(u << 1 | 1, l, r);

	return Result;
}

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

	cin >> N >> L >> D;

	Build(1, 0, max(L + D - 1, N));
	f[0] = 1.0;
	Modify(1, 0, 1.0);
	for (int i = 1; i <= L + D - 1; i ++)
	{
		if (i >= L) f[i] = Query(1, i - min(D, i), L - 1) / D;
		else f[i] = Query(1, i - min(i, D), i - 1) / D;
		Modify(1, i, f[i]);
	}

	for (int i = 0; i < L; i ++)
		f[i] = 0;
	for (int i = N + 1; i <= L + D - 1; i ++)
		f[0] += f[i];
	for (int i = 1; i <= N; i ++)
		f[i] += f[i - 1];

	for (int i = N; i >= 0; i --)
	{
		int l = i + 1, r = min(i + D, N);
		if (l <= r) dp[i] = Query(1, l, r) / D;
		dp[i] = max(dp[i], f[i - 1]);
		Modify(1, i, dp[i]);
	}

	printf("%.8lf\n", dp[0]);

	return 0;
}

方法2:

cpp 复制代码
#include <bits/stdc++.h>
#define int long long

using namespace std;

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

const int SIZE = 4e5 + 10;

int N, L, D;
double f[SIZE], g[SIZE], dp[SIZE];
double S[SIZE];

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

	cin >> N >> L >> D;

	f[0] = 1.0, S[0] = 1.0;
	for (int i = 1; i <= L + D - 1; i ++)
	{
		if (i >= L) f[i] = (S[L - 1] - (min(i, D) == i ? 0 : S[i - min(i, D) - 1])) / D;
		else f[i] = (S[i - 1] - (min(i, D) == i ? 0 : S[i - min(i, D) - 1])) / D;
		S[i] = S[i - 1] + f[i];
	}

	for (int i = 0; i < L; i ++)
		f[i] = 0;
	for (int i = N + 1; i <= L + D - 1; i ++)
		f[0] += f[i];
	for (int i = 1; i <= N; i ++)
		f[i] += f[i - 1];

	for (int i = N; i >= 0; i --)
	{
		int l = i + 1, r = min(i + D, N);
		if (l <= r) dp[i] = (S[l] - S[r + 1]) / D;
		dp[i] = max(dp[i], f[i - 1]);
		S[i] = S[i + 1] + dp[i];
	}

	printf("%.8lf\n", dp[0]);

	return 0;
}

G - Retroactive Range Chmax

Problem Statement

You are given an integer sequence A = ( A 1 , A 2 , ... , A N ) A=(A_1,A_2,\ldots,A_N) A=(A1,A2,...,AN) of length N N N.

Process Q Q Q operations in order. There are three types of operations:

A type-1 operation is represented by a triple of integers ( l , r , x ) (l,r,x) (l,r,x), which corresponds to replacing A i A_i Ai with max ⁡ { A i , x } \max\lbrace A_i,x\rbrace max{Ai,x} for each i = l , l + 1 , ... , r i=l,l+1,\ldots,r i=l,l+1,...,r.

A type-2 operation is represented by an integer i i i, which corresponds to canceling the i i i-th operation (it is guaranteed that the i i i-th operation is of type 1 and has not already been canceled). The new sequence A A A can be obtained by performing all type-1 operations that have not been canceled , starting from the initial state.

A type-3 operation is represented by an integer i i i, which corresponds to printing the current value of A i A_i Ai.

Constraints

1 ≤ N ≤ 2 × 1 0 5 1\leq N\leq2\times10^5 1≤N≤2×105
1 ≤ A i ≤ 1 0 9 ( 1 ≤ i ≤ N ) 1\leq A_i\leq10^9\ (1\leq i\leq N) 1≤Ai≤109 (1≤i≤N)
1 ≤ Q ≤ 2 × 1 0 5 1\leq Q\leq2\times10^5 1≤Q≤2×105

In a type-1 operation, 1 ≤ l ≤ r ≤ N 1\leq l\leq r\leq N 1≤l≤r≤N and 1 ≤ x ≤ 1 0 9 1\leq x\leq10^9 1≤x≤109.

In a type-2 operation, i i i is not greater than the number of operations given before, and 1 ≤ i 1\leq i 1≤i.

In a type-2 operation, the i i i-th operation is of type 1.

In type-2 operations, the same i i i does not appear multiple times.

In a type-3 operation, 1 ≤ i ≤ N 1\leq i\leq N 1≤i≤N.

All input values are integers.

Input

The input is given from Standard Input in the following format:

N N N
A 1 A_1 A1 A 2 A_2 A2 ... \ldots ... A N A_N AN
Q Q Q
query ⁡ 1 \operatorname{query}_1 query1
query ⁡ 2 \operatorname{query}_2 query2
⋮ \vdots ⋮
query ⁡ Q \operatorname{query}_Q queryQ

Here, query ⁡ k ( 1 ≤ k ≤ Q ) \operatorname{query}_k\ (1\leq k\leq Q) queryk (1≤k≤Q) represents the k k k-th operation, and depending on the type of the k k k-th operation, one of the following is given.

For a type-1 operation, the following is given, meaning that the k k k-th operation is a type-1 operation represented by ( l , r , x ) (l,r,x) (l,r,x):

1 1 1 l l l r r r x x x

For a type-2 operation, the following is given, meaning that the k k k-th operation is a type-2 operation represented by i i i:

2 2 2 i i i

For a type-3 operation, the following is given, meaning that the k k k-th operation is a type-3 operation represented by i i i:

3 3 3 i i i

Output

Let q q q be the number of type-3 operations. Print q q q lines.

The i i i-th line ( 1 ≤ i ≤ q ) (1\leq i\leq q) (1≤i≤q) should contain the value that should be printed for the i i i-th given type-3 operation.

Sample Input 1
6
2 7 1 8 2 8
15
3 1
3 3
3 4
1 1 5 4
3 1
3 3
3 4
1 3 6 9
3 1
3 3
3 4
2 4
3 1
3 3
3 4
Sample Output 1
2
1
8
4
4
8
4
9
9
2
9
9

Initially, the sequence A A A is ( 2 , 7 , 1 , 8 , 2 , 8 ) (2,7,1,8,2,8) (2,7,1,8,2,8).

For the 1 1 1-st, 2 2 2-nd, 3 3 3-rd operations, print the values of A 1 , A 3 , A 4 A_1, A_3, A_4 A1,A3,A4, which are 2 , 1 , 8 2,1,8 2,1,8, respectively.

The 4 4 4-th operation replaces the values of A 1 , A 2 , A 3 , A 4 , A 5 A_1, A_2, A_3, A_4, A_5 A1,A2,A3,A4,A5 with max ⁡ { A i , 4 } \max\lbrace A_i,4\rbrace max{Ai,4}.

Just after this operation, A A A is ( 4 , 7 , 4 , 8 , 4 , 8 ) (4,7,4,8,4,8) (4,7,4,8,4,8).

For the 5 5 5-th, 6 6 6-th, 7 7 7-th operations, print the values of A 1 , A 3 , A 4 A_1, A_3, A_4 A1,A3,A4 at this point, which are 4 , 4 , 8 4,4,8 4,4,8, respectively.

The 8 8 8-th operation replaces the values of A 3 , A 4 , A 5 , A 6 A_3, A_4, A_5, A_6 A3,A4,A5,A6 with max ⁡ { A i , 9 } \max\lbrace A_i,9\rbrace max{Ai,9}.

Just after this operation, A A A is ( 4 , 7 , 9 , 9 , 9 , 9 ) (4,7,9,9,9,9) (4,7,9,9,9,9).

For the 9 9 9-th, 10 10 10-th, 11 11 11-th operations, print the values of A 1 , A 3 , A 4 A_1, A_3, A_4 A1,A3,A4 at this point, which are 4 , 9 , 9 4,9,9 4,9,9, respectively.

The 12 12 12-th operation cancels the 4 4 4-th operation.

Just after this operation, A A A is ( 2 , 7 , 9 , 9 , 9 , 9 ) (2,7,9,9,9,9) (2,7,9,9,9,9).

For the 13 13 13-th, 14 14 14-th, 15 15 15-th operations, print the values of A 1 , A 3 , A 4 A_1, A_3, A_4 A1,A3,A4 at this point, which are 2 , 9 , 9 2,9,9 2,9,9, respectively.

Sample Input 2
24
721 78 541 256 970 478 370 467 344 542 43 166 619 17 592 222 983 729 338 747 62 452 815 838
35
3 10
3 8
3 8
3 13
3 9
1 1 17 251
3 3
3 19
3 13
3 22
3 1
3 15
3 18
3 10
3 15
1 16 19 883
1 8 23 212
3 5
3 13
2 6
3 15
1 5 18 914
2 17
3 20
1 23 23 56
3 13
2 25
3 13
3 13
3 10
2 16
1 17 22 308
3 19
3 17
3 7
Sample Output 2
542
467
467
619
344
541
338
619
452
721
592
729
542
592
970
619
592
747
914
914
914
914
338
983
914

Solution

具体见文末视频。


Code

cpp 复制代码
#include <bits/stdc++.h>
#define int long long

using namespace std;

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

const int SIZE = 2e5 + 10;

int N, Q;
int A[SIZE];
struct Segment
{
	int l, r;
	map<int, int> Cnt;
}Tree[SIZE << 2];

void Build(int u, int l, int r)
{
	Tree[u] = {l, r};
	if (l == r)
	{
		Tree[u].Cnt[A[l]] ++;
		return;
	}
	int mid = l + r >> 1;
	Build(u << 1, l, mid), Build(u << 1 | 1, mid + 1, r);
}

void Modify(int u, int l, int r, int d, int k)
{
	if (Tree[u].l >= l && Tree[u].r <= r)
	{
		if (k == 1) Tree[u].Cnt[d] ++;
		else
		{
			Tree[u].Cnt[d] --;
			if (!Tree[u].Cnt[d]) Tree[u].Cnt.erase(d);
		}
		return ;
	}

	int mid = Tree[u].l + Tree[u].r >> 1;
	if (mid >= l) Modify(u << 1, l, r, d, k);
	if (mid < r) Modify(u << 1 | 1, l, r, d, k);
}

int Query(int u, int x)
{
	if (Tree[u].l == Tree[u].r && Tree[u].l == x)
		return Tree[u].Cnt.rbegin() -> first;

	int mid = Tree[u].l + Tree[u].r >> 1, v = -1e18;
	if (Tree[u].Cnt.size()) v = Tree[u].Cnt.rbegin() -> first;
	if (mid >= x) return max(v, Query(u << 1, x));
	else return max(v, Query(u << 1 | 1, x));
}

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

	cin >> N;

	for (int i = 1; i <= N; i ++)
		cin >> A[i];

	Build(1, 1, N);

	cin >> Q;

	std::vector<array<int, 3>> qry;
	qry.resize(Q + 1);
	int idx = 0;
	while (Q --)
	{
		int op, l, r, x;

		cin >> op >> l;

		idx ++;
		if (op == 1)
			cin >> r >> x, Modify(1, l, r, x, 1), qry[idx] = {l, r, x};
		else if (op == 2)
			Modify(1, qry[l][0], qry[l][1], qry[l][2], 2);
		else
			cout << Query(1, l) << endl;
	}

	return 0;
}

视频题解

AtCoder Beginner Contest 342(ABCDEFG)

欢迎大家关注我的B站空间:https://space.bilibili.com/630340560


最后祝大家早日

相关推荐
神仙别闹4 分钟前
基于MFC实现的赛车游戏
c++·游戏·mfc
小c君tt11 分钟前
MFC中 error C2440错误分析及解决方法
c++·mfc
小沈熬夜秃头中୧⍤⃝13 分钟前
【贪心算法】No.1---贪心算法(1)
算法·贪心算法
木向1 小时前
leetcode92:反转链表||
数据结构·c++·算法·leetcode·链表
阿阿越1 小时前
算法每日练 -- 双指针篇(持续更新中)
数据结构·c++·算法
hunandede1 小时前
FFmpeg存放压缩后的音视频数据的结构体:AVPacket简介,结构体,函数
c++
skaiuijing1 小时前
Sparrow系列拓展篇:对调度层进行抽象并引入IPC机制信号量
c语言·算法·操作系统·调度算法·操作系统内核
Star Patrick1 小时前
算法训练(leetcode)二刷第十九天 | *39. 组合总和、*40. 组合总和 II、*131. 分割回文串
python·算法·leetcode
hunandede1 小时前
FFmpeg 4.3 音视频-多路H265监控录放C++开发十三:将AVFrame转换成AVPacket。视频编码,AVPacket 重要函数,结构体成员学习
c++·ffmpeg·音视频
武子康2 小时前
大数据-214 数据挖掘 机器学习理论 - KMeans Python 实现 算法验证 sklearn n_clusters labels
大数据·人工智能·python·深度学习·算法·机器学习·数据挖掘