D - What
Meaning of the questions: to a rooted tree, the root node 1, there is $ Q $ operations, each of the weights of all the nodes of a node and its subtree value plus a value of the operation, asked the final weight of each node value.
Ideas: dfs re-order differential on the line.
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 7; vector<int> G[N]; int dfn[N], id, n, q, last[N], pos[N]; long long val[N], ans[N]; void dfs(int u, int fa) { dfn[u] = ++id; pos[id] = u; for (auto v: G[u]) { if (v == fa) continue; dfs(v, u); } last[u] = id; } int main() { scanf("%d%d", &n, &q); for (int i = 1, u, v; i < n; i++) { scanf("%d%d", &u, &v); G[u].push_back(v); G[v].push_back(u); } dfs(1, 0); while (q--) { long long x; int u; scanf("%d%lld", &u, &x); val[dfn[u]] += x; val[last[u] + 1] -= x; } for (int i = 1; i <= n; i++) { val[i] += val[i - 1]; ans[pos[i]] = val[i]; } for (int i = 1; i <= n; i++) printf("%lld%c", ans[i], " \n"[i == n]); return 0; }
E - Strings of Impurity
The meaning of problems: two strings to $ s $, $ t $, $ s $ copy to $ 10 $ ^ {100} views into $ s' $, i.e., 'abc' becomes' abcabc ... abcabc ', $ Q t $ a $ s' $ sequence occurs, the first end position of the index, there is no output is -1.
As a sample
$ S '$: conteStcONtest
$ T $: Son
capitalizing on the matching position, the answer is 10.
Thinking: subsequence problem can use a $ ne [i] [j] $ string represents $ s $ $ I $ to position, location of the next $ J $ character, may be pretreated from the back sweep out, and then statistics answer with $ t $ to greedy match $ s $, can match multiple ago ago, currently no $ t_ {j} $ this character after location, it is necessary to go to the next $ s $ string, then the answer just look at how many jumped $ s $ strings and the final position is.
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 7; char s[N], t[N]; int ne[N][26]; int fir[26]; int main() { scanf("%s%s", s + 1, t + 1); int len1 = strlen(s + 1), len2 = strlen(t + 1); for (int i = len1; i >= 0; i--) { for (int j = 0; j < 26; j++) ne[i][j] = fir[j]; if (i) fir[s[i] - 'a'] = i; } int id = 0, ans = 1; for (int i = 1; i <= len2; i++) { if (ne[id][t[i] - 'a']) { id = ne[id][t[i] - 'a']; } else { if (!fir[t[i] - 'a']) { puts("-1"); return 0; } id = fir[t[i] - 'a']; ans++; } } printf("%lld\n", 1LL * (ans - 1) * len1 + id); return 0; }
F - Coincidence
Meaning of the questions: Given $ L $ and $ R $, asked how many of the $ x, y \ left (L \ leq x \ leq y \ leq R \ right) $ satisfy $ y $ Mod $ x = y $ XOR $ x $
Thinking:
$ \ Because Y $ Mod $ X <X $
$ \ THEREFORE Y $ the XOR $ X <X $
description will $ Y $ and $ X $ highest bit 1 must be in the same position
$ \ therefore \ dfrac {y} X} {<$ 2
$ \ THEREFORE Y - X = Y $ $ X $ the XOR
few examples will discover that this condition is satisfied, and $ X $ $ $ Y in binary can be achieved without direct subtraction borrow , such as
$ Y $: 10110 (22 is)
$ X $: 10010 (18 is)
this time $ y $ XOR $ x = y - x = 4 $
can get $ y $ and $ x = x $ and $ X $ and the same position as the most significant bit $ Y $ 1
can then do DP digits.
DP digital enumerator case simultaneously two binary numbers, which does not exceed $ Y $ $ R $, $ x $ L is not less than $ $, there is a case where the current leading zeros, that a $ Y $ and X $ $ must be the same, then what assurance of every $ x $ can not be greater than $ y $ (it appears the current position is $ y $ 0, $ x $ current bit is 1) on the line.
#include <bits/stdc++.h> #define ll long long using namespace std; const int MOD = 1e9 + 7; ll dp[70][2][2][2]; ll solve(ll l, ll r, int pos, bool limit1, bool limit2, bool lead) { if (pos < 0) return 1; ll &ans = dp[pos][limit1][limit2][lead]; if (ans != -1) return ans; ans = 0; int up2 = limit2 ? (r >> pos & 1) : 1; int up1 = limit1 ? (l >> pos & 1) : 0; for (int i = up1; i <= 1; i++) for (int j = 0; j <= up2; j++) { if (i > j) continue; if (lead && i != j) continue; (ans += solve(l, r, pos - 1, limit1 && i == up1, limit2 && j == up2, lead && j == 0)) %= MOD; } return ans; } int main() { memset(dp, -1, sizeof(dp)); ll l, r; scanf("%lld%lld", &l, &r); printf("%lld", solve(l, r, 60, 1, 1, 1)); return 0; }