QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#487074 | #5346. Binary Tree | PetroTarnavskyi# | AC ✓ | 93ms | 5328kb | C++20 | 2.0kb | 2024-07-22 15:49:16 | 2024-07-22 15:49:16 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second
typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;
const int mod = 21092013;
int add(int a, int b)
{
return a + b < mod ? a + b : a + b - mod;
}
int sub(int a, int b)
{
return a - b >= 0 ? a - b : a - b + mod;
}
int mult(int a, int b)
{
return (LL)a * b % mod;
}
int binpow(int a, int p)
{
int ans = 1;
while (p)
{
if (p & 1) ans = mult(ans, a);
a = mult(a, a);
p /= 2;
}
return ans;
}
void solve()
{
string S, t;
cin >> S >> t;
string s = "";
FOR (i, 0, SZ(S))
{
if (S[i] == 'U')
{
if (!s.empty())
s.pop_back();
}
else
s.PB(S[i]);
}
int n = SZ(t);
VI l(n), r(n), u(n);
VI dp(n + 2, 0);
FOR (i, 0, n)
{
if (t[i] == 'L')
l[i] = 1;
if (t[i] == 'R')
r[i] = 1;
if (t[i] == 'U')
u[i] = 1;
if (i)
{
u[i] += u[i - 1];
l[i] += l[i - 1];
r[i] += r[i - 1];
}
}
dp[n] = 1;
RFOR (i, n, 0)
{
int j = i;
if (t[i] == 'L')
j = lower_bound(ALL(l), l[i] + 1) - l.begin();
if (t[i] == 'R')
j = lower_bound(ALL(r), r[i] + 1) - r.begin();
dp[i] = sub(dp[i + 1], dp[j + 1]);
dp[i] = add(dp[i], dp[i + 1]);
}
int ans = 0;
FOR (cnt, 1, SZ(s) + 1)
{
int j = lower_bound(ALL(u), cnt) - u.begin();
if (j == n)
break;
ans = add(ans, 1);
if (s[SZ(s) - cnt] == 'R')
j = lower_bound(ALL(l), l[j] + 1) - l.begin();
else
j = lower_bound(ALL(r), r[j] + 1) - r.begin();
if (j == n)
continue;
ans = add(ans, dp[j + 1]);
}
ans = add(ans, dp[0]);
cout << ans << '\n';
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
FOR (tc, 0, t)
{
cout << "Case " << tc + 1 << ": ";
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3556kb
input:
2 L LU L L
output:
Case 1: 3 Case 2: 2
result:
ok 2 lines
Test #2:
score: 0
Accepted
time: 93ms
memory: 5328kb
input:
15 LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL...
output:
Case 1: 5382631 Case 2: 2888332 Case 3: 21 Case 4: 18976447 Case 5: 1732056 Case 6: 13231118 Case 7: 15776153 Case 8: 16265552 Case 9: 2420344 Case 10: 3222712 Case 11: 10627679 Case 12: 5598969 Case 13: 7517700 Case 14: 20658228 Case 15: 9220181
result:
ok 15 lines