problem_id
stringlengths
6
6
language
stringclasses
2 values
original_status
stringclasses
3 values
original_src
stringlengths
19
243k
changed_src
stringlengths
19
243k
change
stringclasses
3 values
i1
int64
0
8.44k
i2
int64
0
8.44k
j1
int64
0
8.44k
j2
int64
0
8.44k
error
stringclasses
270 values
stderr
stringlengths
0
226k
p02936
C++
Runtime Error
/* * atcoder/abc138_2nd/d.cpp */ // C++ 14 #include <algorithm> #include <cassert> #include <cmath> #include <cstring> // memset #include <iostream> #include <vector> using namespace std; #define ll long long #define loop(__x, __start, __end) for (int __x = __start; __x < __end; __x++) template <class T> ostream &operator<<(ostream &out, vector<T> const &v) { for (auto &&a : v) out << a << " "; out << endl; return out; } template <class T> void dump(T &a) { cout << a << endl; } template <class T> bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } const int MAX = 100001; int N, Q; vector<int> G[MAX]; vector<int> A(MAX, 0); vector<int> B(MAX, 0); void dfs(int n = 0, int prev = -1, ll acc = 0) { acc += A[n]; B[n] = acc; for (auto &&u : G[n]) { if (u == prev) continue; dfs(u, n, acc); } } void solve() { cin >> N >> Q; int a, b, p, x; loop(n, 0, N - 1) { cin >> a >> b; a--; b--; G[a].push_back(b); G[b].push_back(a); } loop(n, 0, Q) { cin >> p >> x; p--; A[p] += x; } dfs(); loop(n, 0, N) cout << B[n] << " "; cout << endl; } int main() { // cout.precision(15); cout << fixed; solve(); return 0; }
/* * atcoder/abc138_2nd/d.cpp */ // C++ 14 #include <algorithm> #include <cassert> #include <cmath> #include <cstring> // memset #include <iostream> #include <vector> using namespace std; #define ll long long #define loop(__x, __start, __end) for (int __x = __start; __x < __end; __x++) template <class T> ostream &operator<<(ostream &out, vector<T> const &v) { for (auto &&a : v) out << a << " "; out << endl; return out; } template <class T> void dump(T &a) { cout << a << endl; } template <class T> bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } const int MAX = 200001; int N, Q; vector<int> G[MAX]; vector<int> A(MAX, 0); vector<int> B(MAX, 0); void dfs(int n = 0, int prev = -1, ll acc = 0) { acc += A[n]; B[n] = acc; for (auto &&u : G[n]) { if (u == prev) continue; dfs(u, n, acc); } } void solve() { cin >> N >> Q; int a, b, p, x; loop(n, 0, N - 1) { cin >> a >> b; a--; b--; G[a].push_back(b); G[b].push_back(a); } loop(n, 0, Q) { cin >> p >> x; p--; A[p] += x; } dfs(); loop(n, 0, N) cout << B[n] << " "; cout << endl; } int main() { // cout.precision(15); cout << fixed; solve(); return 0; }
replace
36
37
36
37
0
p02936
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; typedef long long ll; typedef pair<int, int> P; typedef tuple<ll, ll, ll> T; const long long INF = 1LL << 60; const int MOD = 1000000000 + 7; #define rev(s) (string((s).rbegin(), (s).rend())) template <typename T> inline string toString(const T &a) { ostringstream oss; oss << a; return oss.str(); }; // cout << fixed << setprecision(10) << ans << endl; 有効桁数指定 // *min_element(c + l, c + r) *max_element(c + l, c + r) 配列の中のmin-max // int dx[8]={1,1,0,-1,-1,-1,0,1}; // int dy[8]={0,1,1,1,0,-1,-1,-1}; // int dx[4]={1,0,-1,0}; // int dy[4]={0,1,0,-1}; // ~ は、-1の時だけfalse int n, q; vector<int> tree[20005]; int cnt[20005]; ll sum[20005]; int seen[20005]; void dfs(int n) { for (int i = 0; i < int(tree[n].size()); i++) { if (!seen[tree[n][i]]) { seen[tree[n][i]] = 1; sum[tree[n][i]] = cnt[tree[n][i]] + sum[n]; dfs(tree[n][i]); } } } int main() { cin >> n >> q; rep(i, n - 1) { int a, b; cin >> a >> b; tree[a - 1].push_back(b - 1); tree[b - 1].push_back(a - 1); } rep(i, q) { int p, x; cin >> p >> x; cnt[p - 1] += x; } seen[0] = 1; sum[0] = cnt[0]; dfs(0); rep(i, n) { cout << sum[i] << " "; if (i == n - 1) cout << endl; } }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; typedef long long ll; typedef pair<int, int> P; typedef tuple<ll, ll, ll> T; const long long INF = 1LL << 60; const int MOD = 1000000000 + 7; #define rev(s) (string((s).rbegin(), (s).rend())) template <typename T> inline string toString(const T &a) { ostringstream oss; oss << a; return oss.str(); }; // cout << fixed << setprecision(10) << ans << endl; 有効桁数指定 // *min_element(c + l, c + r) *max_element(c + l, c + r) 配列の中のmin-max // int dx[8]={1,1,0,-1,-1,-1,0,1}; // int dy[8]={0,1,1,1,0,-1,-1,-1}; // int dx[4]={1,0,-1,0}; // int dy[4]={0,1,0,-1}; // ~ は、-1の時だけfalse int n, q; vector<int> tree[200005]; int cnt[200005]; ll sum[200005]; int seen[200005]; void dfs(int n) { for (int i = 0; i < int(tree[n].size()); i++) { if (!seen[tree[n][i]]) { seen[tree[n][i]] = 1; sum[tree[n][i]] = cnt[tree[n][i]] + sum[n]; dfs(tree[n][i]); } } } int main() { cin >> n >> q; rep(i, n - 1) { int a, b; cin >> a >> b; tree[a - 1].push_back(b - 1); tree[b - 1].push_back(a - 1); } rep(i, q) { int p, x; cin >> p >> x; cnt[p - 1] += x; } seen[0] = 1; sum[0] = cnt[0]; dfs(0); rep(i, n) { cout << sum[i] << " "; if (i == n - 1) cout << endl; } }
replace
23
27
23
27
0
p02936
C++
Runtime Error
/* بِسْمِ اللَّهِ الرَّحْمَٰنِ الرَّحِيمِ */ // codeforces1038D #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; #define FASTIO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); #define mp make_pair #define pb push_back void parseArray(ll *A, ll n) { for (ll K = 0; K < n; K++) { cin >> A[K]; } } ll modInverse(ll a, ll b) { return 1 < a ? b - modInverse(b % a, a) * b / a : 1; } ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); } ld dist(ll x, ll y, ll a, ll b) { return sqrt((x - a) * (x - a) + (y - b) * (y - b)); } void debug(ll *a, ll n) { for (int k = 0; k < n; k++) { cout << a[k] << " "; } cerr << "\n"; } ll n, q; vector<ll> g[111111]; ll c[111111]; bool vis[111111]; void dfs(ll v, ll x) { if (vis[v]) return; vis[v] = true; c[v] += x; for (ll e : g[v]) { dfs(e, c[v]); } } int main() { FASTIO; cin >> n >> q; ll x, y; for (int k = 0; k < n - 1; k++) { cin >> x >> y; g[x].pb(y); g[y].pb(x); } for (int k = 0; k < q; k++) { cin >> x >> y; c[x] += y; } dfs(1, 0); for (int k = 1; k <= n; k++) cout << c[k] << " "; cout << endl; return 0; }
/* بِسْمِ اللَّهِ الرَّحْمَٰنِ الرَّحِيمِ */ // codeforces1038D #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; #define FASTIO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); #define mp make_pair #define pb push_back void parseArray(ll *A, ll n) { for (ll K = 0; K < n; K++) { cin >> A[K]; } } ll modInverse(ll a, ll b) { return 1 < a ? b - modInverse(b % a, a) * b / a : 1; } ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); } ld dist(ll x, ll y, ll a, ll b) { return sqrt((x - a) * (x - a) + (y - b) * (y - b)); } void debug(ll *a, ll n) { for (int k = 0; k < n; k++) { cout << a[k] << " "; } cerr << "\n"; } ll n, q; vector<ll> g[222222]; ll c[222222]; bool vis[222222]; void dfs(int v, ll x) { if (vis[v]) return; vis[v] = true; c[v] += x; for (ll e : g[v]) { dfs(e, c[v]); } } int main() { FASTIO; cin >> n >> q; ll x, y; for (int k = 0; k < n - 1; k++) { cin >> x >> y; g[x].pb(y); g[y].pb(x); } for (int k = 0; k < q; k++) { cin >> x >> y; c[x] += y; } dfs(1, 0); for (int k = 1; k <= n; k++) cout << c[k] << " "; cout << endl; return 0; }
replace
30
34
30
34
0
p02936
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 1e5 + 5; ll a[maxn], b[maxn]; vector<int> v[maxn]; void f(int n) { for (int i = 0; i < v[n].size(); i++) { int k = v[n][i]; a[k] += (b[n] + b[k]); b[k] += b[n]; for (int i = 0; i < v[k].size(); i++) { int l = v[k][i]; a[l] += (b[l] + b[k]); b[l] += b[k]; f(l); } } } int main() { int n, q; memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b)); scanf("%d %d", &n, &q); for (int i = 0; i < n - 1; i++) { int from, to; scanf("%d %d", &from, &to); v[from].push_back(to); } for (int i = 0; i < q; i++) { int w, l; scanf("%d %d", &w, &l); b[w] += l; } a[1] = b[1]; f(1); printf("%lld", a[1]); for (int i = 2; i <= n; i++) printf(" %lld", a[i]); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 2e5 + 5; ll a[maxn], b[maxn]; vector<int> v[maxn]; void f(int n) { for (int i = 0; i < v[n].size(); i++) { int k = v[n][i]; a[k] += (b[n] + b[k]); b[k] += b[n]; for (int i = 0; i < v[k].size(); i++) { int l = v[k][i]; a[l] += (b[l] + b[k]); b[l] += b[k]; f(l); } } } int main() { int n, q; memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b)); scanf("%d %d", &n, &q); for (int i = 0; i < n - 1; i++) { int from, to; scanf("%d %d", &from, &to); v[from].push_back(to); } for (int i = 0; i < q; i++) { int w, l; scanf("%d %d", &w, &l); b[w] += l; } a[1] = b[1]; f(1); printf("%lld", a[1]); for (int i = 2; i <= n; i++) printf(" %lld", a[i]); return 0; }
replace
3
4
3
4
0
p02936
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 2e5 + 10; ll a[maxn]; ll ans[maxn]; int ru[maxn]; int tot = 0, head[maxn], ne[maxn], to[maxn]; vector<int> s[maxn]; void add(int x, int y) { tot++; to[tot] = y; ne[tot] = head[x]; head[x] = tot; } void dfs(int x, int pre) { a[x] += a[pre]; for (int i = head[x]; i; i = ne[i]) { if (to[i] != pre) { dfs(to[i], x); } } } int main() { int n, q; scanf("%d%d", &n, &q); for (int i = 0; i < n - 1; i++) { int x, y; scanf("%d%d", &x, &y); add(x, y); add(y, x); } for (int i = 0; i < q; i++) { int x, y; scanf("%d%d", &x, &y); a[x] += y; } int pos = 1; dfs(pos, 0); for (int i = 1; i <= n; i++) printf("%lld%c", a[i], i == n ? '\n' : ' '); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 4e5 + 10; ll a[maxn]; ll ans[maxn]; int ru[maxn]; int tot = 0, head[maxn], ne[maxn], to[maxn]; vector<int> s[maxn]; void add(int x, int y) { tot++; to[tot] = y; ne[tot] = head[x]; head[x] = tot; } void dfs(int x, int pre) { a[x] += a[pre]; for (int i = head[x]; i; i = ne[i]) { if (to[i] != pre) { dfs(to[i], x); } } } int main() { int n, q; scanf("%d%d", &n, &q); for (int i = 0; i < n - 1; i++) { int x, y; scanf("%d%d", &x, &y); add(x, y); add(y, x); } for (int i = 0; i < q; i++) { int x, y; scanf("%d%d", &x, &y); a[x] += y; } int pos = 1; dfs(pos, 0); for (int i = 1; i <= n; i++) printf("%lld%c", a[i], i == n ? '\n' : ' '); return 0; }
replace
3
4
3
4
0
p02936
Python
Runtime Error
def c(i, p, f, g, k, vis): if vis[i]: return vis[i] = True w = f[i] + p k[i] = w ch = g[i] for x in ch: c(x, w, f, g, k, vis) def main(): import sys sys.setrecursionlimit(200000) n, q, *abpx = map(int, sys.stdin.read().split()) g = [[] for _ in range(n + 1)] for u, v in zip(*[iter(abpx[: 2 * (n - 1)])] * 2): g[u].append(v) g[v].append(u) f = [0 for _ in range(n + 1)] for p, x in zip(*[iter(abpx[2 * (n - 1) :])] * 2): f[p] += x k = [0 for _ in range(n + 1)] vis = [False for _ in range(n + 1)] c(1, 0, f, g, k, vis) ans = " ".join(map(str, k[1:])) print(ans) if __name__ == "__main__": main()
def c(i, p, f, g, k, vis): if vis[i]: return vis[i] = True w = f[i] + p k[i] = w ch = g[i] for x in ch: c(x, w, f, g, k, vis) def main(): import sys sys.setrecursionlimit(300000) n, q, *abpx = map(int, sys.stdin.read().split()) g = [[] for _ in range(n + 1)] for u, v in zip(*[iter(abpx[: 2 * (n - 1)])] * 2): g[u].append(v) g[v].append(u) f = [0 for _ in range(n + 1)] for p, x in zip(*[iter(abpx[2 * (n - 1) :])] * 2): f[p] += x k = [0 for _ in range(n + 1)] vis = [False for _ in range(n + 1)] c(1, 0, f, g, k, vis) ans = " ".join(map(str, k[1:])) print(ans) if __name__ == "__main__": main()
replace
14
15
14
15
0
p02936
Python
Runtime Error
from collections import deque def solve(string): n, q, *abpx = map(int, string.split()) ab, px = abpx[: 2 * n - 2], abpx[2 * n - 2 :] check = [True] * (n + 1) t = {i: [] for i in range(n + 1)} c = [0] * (n + 1) d = deque([1]) for a, b in zip(*[iter(ab)] * 2): t[a].append(b) t[b].append(a) for p, x in zip(*[iter(px)] * 2): c[p] += x while len(d) > 0: curr = d.pop() check[curr] = False next_ = [i for i in t[curr] if check[i]] for n_ in next_: c[n_] += c[curr] d.extend(next_) return " ".join([str(_c) for _c in c[1:]]) if __name__ == "__main__": n, m = map(int, input().split()) print(solve("{} {}\n".format(n, m) + "\n".join([input() for _ in range(n + m)])))
from collections import deque def solve(string): n, q, *abpx = map(int, string.split()) ab, px = abpx[: 2 * n - 2], abpx[2 * n - 2 :] check = [True] * (n + 1) t = {i: [] for i in range(n + 1)} c = [0] * (n + 1) d = deque([1]) for a, b in zip(*[iter(ab)] * 2): t[a].append(b) t[b].append(a) for p, x in zip(*[iter(px)] * 2): c[p] += x while len(d) > 0: curr = d.pop() check[curr] = False next_ = [i for i in t[curr] if check[i]] for n_ in next_: c[n_] += c[curr] d.extend(next_) return " ".join([str(_c) for _c in c[1:]]) if __name__ == "__main__": n, m = map(int, input().split()) print( solve("{} {}\n".format(n, m) + "\n".join([input() for _ in range(n + m - 1)])) )
replace
27
28
27
30
EOFError: EOF when reading a line
Traceback (most recent call last): File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02936/Python/s487174146.py", line 28, in <module> print(solve('{} {}\n'.format(n, m) + '\n'.join([input() for _ in range(n + m)]))) File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02936/Python/s487174146.py", line 28, in <listcomp> print(solve('{} {}\n'.format(n, m) + '\n'.join([input() for _ in range(n + m)]))) EOFError: EOF when reading a line
p02936
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); ++i) typedef long long ll; typedef pair<ll, ll> P; const int MAX = 2 * 1e5 + 10; const int INF = 1e9; const int MOD = 1e9 + 7; int N, Q; vector<int> G[MAX]; vector<int> ans(MAX); void dfs(int n, int x, map<int, int> m) { ans[n] = x + m[n]; for (auto i : G[n]) { dfs(i, ans[n], m); } } int main() { cin >> N >> Q; rep(i, N - 1) { int a, b; cin >> a >> b; a--, b--; G[a].push_back(b); } map<int, int> m; rep(i, Q) { int p, x; cin >> p >> x; p--; m[p] += x; } dfs(0, 0, m); rep(i, N) cout << ans[i] << " "; cout << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); ++i) typedef long long ll; typedef pair<ll, ll> P; const int MAX = 2 * 1e5 + 10; const int INF = 1e9; const int MOD = 1e9 + 7; int N, Q; vector<int> G[MAX]; vector<int> ans(MAX); void dfs(int n, int x, map<int, int> &m) { ans[n] = x + m[n]; for (auto i : G[n]) { dfs(i, ans[n], m); } } int main() { cin >> N >> Q; rep(i, N - 1) { int a, b; cin >> a >> b; a--, b--; G[a].push_back(b); } map<int, int> m; rep(i, Q) { int p, x; cin >> p >> x; p--; m[p] += x; } dfs(0, 0, m); rep(i, N) cout << ans[i] << " "; cout << endl; }
replace
15
16
15
16
TLE
p02936
C++
Runtime Error
/*input 6 2 1 2 1 3 2 4 3 6 2 5 1 10 1 10 */ // assic value of ('0'-'9') is(48 - 57) and (a-z) is (97-122) and (A-Z) // is(65-90) and 32 for space #include <bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define endl '\n' #define pii pair<ll int, ll int> #define vi vector<ll int> #define is_empty(v) v.empty() #define vs vector<string> #define vvi vector<vector<ll, ll>> #define all(it, a) for (auto it = (a).begin(); it != (a).end(); it++) #define F first #define S second #define sz(x) (ll int)x.size() #define inf 1000000007 #define rep(i, a, b) for (ll int i = a; i < b; i++) #define repr(i, a, b) for (ll int i = a; i > b; i--) #define reprr(i, a, b) for (ll int i = a; i >= b; i--) #define lbnd lower_bound #define ubnd upper_bound #define bs binary_search #define mp make_pair #define sum(v) accumulate(v.begin(), v.end(), (ll)0) // map <long long int,long long int> ma; // set <long long int, greater <long long int> > s; const ll N = 1e5 + 5; vector<vi> adj(N, vi()); ll a[N]; ll ans[N]; void dfs(ll u, ll p) { ans[u] += a[u]; rep(i, 0, sz(adj[u])) { if (adj[u][i] == p) continue; a[adj[u][i]] += a[u]; dfs(adj[u][i], u); } return; } int solve() { ll n, q; cin >> n >> q; rep(i, 0, n - 1) { ll x, y; cin >> x >> y; x--; y--; adj[x].pb(y); adj[y].pb(x); } while (q--) { ll v, x; cin >> v >> x; v--; a[v] += x; } dfs(0, -1); rep(i, 0, n) cout << ans[i] << " "; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll t = 1; // cin>>t; while (t--) solve(); }
/*input 6 2 1 2 1 3 2 4 3 6 2 5 1 10 1 10 */ // assic value of ('0'-'9') is(48 - 57) and (a-z) is (97-122) and (A-Z) // is(65-90) and 32 for space #include <bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define endl '\n' #define pii pair<ll int, ll int> #define vi vector<ll int> #define is_empty(v) v.empty() #define vs vector<string> #define vvi vector<vector<ll, ll>> #define all(it, a) for (auto it = (a).begin(); it != (a).end(); it++) #define F first #define S second #define sz(x) (ll int)x.size() #define inf 1000000007 #define rep(i, a, b) for (ll int i = a; i < b; i++) #define repr(i, a, b) for (ll int i = a; i > b; i--) #define reprr(i, a, b) for (ll int i = a; i >= b; i--) #define lbnd lower_bound #define ubnd upper_bound #define bs binary_search #define mp make_pair #define sum(v) accumulate(v.begin(), v.end(), (ll)0) // map <long long int,long long int> ma; // set <long long int, greater <long long int> > s; const ll N = 2e5 + 5; vector<vi> adj(N, vi()); ll a[N]; ll ans[N]; void dfs(ll u, ll p) { ans[u] += a[u]; rep(i, 0, sz(adj[u])) { if (adj[u][i] == p) continue; a[adj[u][i]] += a[u]; dfs(adj[u][i], u); } return; } int solve() { ll n, q; cin >> n >> q; rep(i, 0, n - 1) { ll x, y; cin >> x >> y; x--; y--; adj[x].pb(y); adj[y].pb(x); } while (q--) { ll v, x; cin >> v >> x; v--; a[v] += x; } dfs(0, -1); rep(i, 0, n) cout << ans[i] << " "; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll t = 1; // cin>>t; while (t--) solve(); }
replace
38
39
38
39
0
p02936
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long typedef pair<ll, ll> P; #define M 1000000007 int n, q, a, b, x, y; vector<vector<int>> d(100010); vector<int> t(100010, 0), v(100010); int c = 0; void dfs(int p) { c += t[p]; v[p] = c; for (auto au : d[p]) { dfs(au); } c -= t[p]; } int main() { cin >> n >> q; for (int i = 0; i < n - 1; i++) { cin >> a >> b; d[a].push_back(b); } for (int i = 0; i < q; i++) { cin >> x >> y; t[x] += y; } dfs(1); for (int i = 1; i <= n; i++) { cout << v[i] << " "; } }
#include <bits/stdc++.h> using namespace std; #define ll long long typedef pair<ll, ll> P; #define M 1000000007 int n, q, a, b, x, y; vector<vector<int>> d(200010); vector<int> t(200010, 0), v(200010); int c = 0; void dfs(int p) { c += t[p]; v[p] = c; for (auto au : d[p]) { dfs(au); } c -= t[p]; } int main() { cin >> n >> q; for (int i = 0; i < n - 1; i++) { cin >> a >> b; d[a].push_back(b); } for (int i = 0; i < q; i++) { cin >> x >> y; t[x] += y; } dfs(1); for (int i = 1; i <= n; i++) { cout << v[i] << " "; } }
replace
6
8
6
8
0
p02936
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { int N, Q; cin >> N >> Q; vector<vector<int>> t(N); for (int i = 0; i < N - 1; i++) { int a, b; cin >> a >> b; a--; b--; t[a].push_back(b); } vector<int> point(N); for (int i = 0; i < Q; i++) { int p, x; cin >> p >> x; p--; queue<int> que; que.push(p); while (!que.empty()) { int u = que.front(); que.pop(); point[u] += x; for (auto v : t[u]) { que.push(v); } } } for (int i = 0; i < N; i++) { if (i) cout << " "; cout << point[i]; } cout << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int N, Q; cin >> N >> Q; vector<vector<int>> t(N); for (int i = 0; i < N - 1; i++) { int a, b; cin >> a >> b; a--; b--; t[a].push_back(b); } vector<int> point(N); for (int i = 0; i < Q; i++) { int p, x; cin >> p >> x; p--; point[p] += x; } queue<int> que; que.push(0); while (!que.empty()) { int u = que.front(); que.pop(); int last = point[u]; for (auto v : t[u]) { point[v] += last; que.push(v); } } for (int i = 0; i < N; i++) { if (i) cout << " "; cout << point[i]; } cout << endl; }
replace
20
29
20
31
TLE
p02936
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) #define chmax(x, y) x = max(x, y) #define chmin(x, y) x = min(x, y) using namespace std; typedef long long ll; const int MOD = 1000000007; vector<int> to[20005]; vector<int> ans; void dfs(int v, int p = -1) { for (int u : to[v]) { if (u == p) continue; ans[u] += ans[v]; dfs(u, v); } } int main() { int N, Q; cin >> N >> Q; rep(i, N - 1) { int a, b; cin >> a >> b; a--; b--; to[a].push_back(b); to[b].push_back(a); } ans.resize(N); rep(i, Q) { int p, x; cin >> p >> x; p--; ans[p] += x; } dfs(0); rep(i, N) { cout << ans[i] << endl; } }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) #define chmax(x, y) x = max(x, y) #define chmin(x, y) x = min(x, y) using namespace std; typedef long long ll; const int MOD = 1000000007; vector<int> to[200005]; vector<int> ans; void dfs(int v, int p = -1) { for (int u : to[v]) { if (u == p) continue; ans[u] += ans[v]; dfs(u, v); } } int main() { int N, Q; cin >> N >> Q; rep(i, N - 1) { int a, b; cin >> a >> b; a--; b--; to[a].push_back(b); to[b].push_back(a); } ans.resize(N); rep(i, Q) { int p, x; cin >> p >> x; p--; ans[p] += x; } dfs(0); rep(i, N) { cout << ans[i] << endl; } }
replace
10
11
10
11
0
p02936
C++
Runtime Error
#include <bits/stdc++.h> #define hell 1000000007 #define lcm(a, b) (a * b) / __gcd(a, b) #define ll long long #define vll vector<ll> #define vi vector<int> #define pll vector<pair<ll, ll>> #define pb push_back #define mp make_pair #define all(v) v.begin(), v.end() #define lbnd lower_bound #define ubnd upper_bound #define bs binary_search #define F first #define S second #define rep(i, a, b) for (i = a; i < b; i++) #define parr(a, n) \ for (ll it = 0; it < n; it++) \ cout << a[it] << " "; \ cout << endl; #define pcont(a) \ for (auto it : a) \ cout << it << " "; \ cout << endl; #define ret(x) return cout << x, 0; #define endl '\n' /*constants*/ #define MAXN 100005 #define PI 3.14159265358979323846 /*debug*/ #define TRACE #ifdef TRACE #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { std::cout << name << " : " << arg1 << '\n'; } template <typename Arg1, typename... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *comma = strchr(names + 1, ','); std::cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } #else #define trace(...) #endif using namespace std; ll s[MAXN], vis[MAXN], out[MAXN]; vll adj[MAXN]; void dfs(ll u, ll k) { vis[u] = 1; out[u] = s[u] + k; for (auto it : adj[u]) { if (!vis[it]) { dfs(it, k + s[u]); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll i, j, n, m, k, cnt = 0, ans = 0, t = 1; cin >> n >> t; rep(i, 0, n - 1) { cin >> j >> k; adj[j].pb(k); adj[k].pb(j); } rep(i, 0, t) { cin >> j >> k; s[j] += k; } dfs(1, 0); rep(i, 1, n + 1) cout << out[i] << " "; return 0; }
#include <bits/stdc++.h> #define hell 1000000007 #define lcm(a, b) (a * b) / __gcd(a, b) #define ll long long #define vll vector<ll> #define vi vector<int> #define pll vector<pair<ll, ll>> #define pb push_back #define mp make_pair #define all(v) v.begin(), v.end() #define lbnd lower_bound #define ubnd upper_bound #define bs binary_search #define F first #define S second #define rep(i, a, b) for (i = a; i < b; i++) #define parr(a, n) \ for (ll it = 0; it < n; it++) \ cout << a[it] << " "; \ cout << endl; #define pcont(a) \ for (auto it : a) \ cout << it << " "; \ cout << endl; #define ret(x) return cout << x, 0; #define endl '\n' /*constants*/ #define MAXN 200005 #define PI 3.14159265358979323846 /*debug*/ #define TRACE #ifdef TRACE #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { std::cout << name << " : " << arg1 << '\n'; } template <typename Arg1, typename... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *comma = strchr(names + 1, ','); std::cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } #else #define trace(...) #endif using namespace std; ll s[MAXN], vis[MAXN], out[MAXN]; vll adj[MAXN]; void dfs(ll u, ll k) { vis[u] = 1; out[u] = s[u] + k; for (auto it : adj[u]) { if (!vis[it]) { dfs(it, k + s[u]); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll i, j, n, m, k, cnt = 0, ans = 0, t = 1; cin >> n >> t; rep(i, 0, n - 1) { cin >> j >> k; adj[j].pb(k); adj[k].pb(j); } rep(i, 0, t) { cin >> j >> k; s[j] += k; } dfs(1, 0); rep(i, 1, n + 1) cout << out[i] << " "; return 0; }
replace
29
30
29
30
0
p02936
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; const ll MOD = pow(10, 9) + 7; // const ll MOD = 998244353; // const ll MOD = ; ll mod(ll A, ll M) { return (A % M + M) % M; } const ll INF = 1LL << 60; template <class T> bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } ll divCeil(ll A, ll B) { return (A + (B - 1)) / B; } ll myctoi(char C) { return C - '0'; } char myitoc(ll N) { return '0' + N; } #define FINALANS(A) \ { \ cout << (A) << endl; \ exit(0); \ } vector<vector<ll>> G(111111); vector<ll> cnt(111111, 0); vector<bool> visited(111111, false); void dfs(ll V) { if (visited.at(V)) return; visited.at(V) = true; // cerr << V << endl; for (ll i = 0; i < G.at(V).size(); i++) { if (!visited.at(G.at(V).at(i))) cnt.at(G.at(V).at(i)) += cnt.at(V); dfs(G.at(V).at(i)); } } int main() { ll N, Q; cin >> N >> Q; for (ll i = 0; i < N - 1; i++) { ll a, b; cin >> a >> b; a--, b--; G.at(a).push_back(b); G.at(b).push_back(a); } for (ll i = 0; i < Q; i++) { ll p, x; cin >> p >> x; p--; cnt.at(p) += x; } dfs(0); for (ll i = 0; i < N; i++) { cout << cnt.at(i); if (i == N - 1) cout << endl; else cout << " "; } }
#include <bits/stdc++.h> using namespace std; using ll = long long; const ll MOD = pow(10, 9) + 7; // const ll MOD = 998244353; // const ll MOD = ; ll mod(ll A, ll M) { return (A % M + M) % M; } const ll INF = 1LL << 60; template <class T> bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } ll divCeil(ll A, ll B) { return (A + (B - 1)) / B; } ll myctoi(char C) { return C - '0'; } char myitoc(ll N) { return '0' + N; } #define FINALANS(A) \ { \ cout << (A) << endl; \ exit(0); \ } vector<vector<ll>> G(222222); vector<ll> cnt(222222, 0); vector<bool> visited(222222, false); void dfs(ll V) { if (visited.at(V)) return; visited.at(V) = true; // cerr << V << endl; for (ll i = 0; i < G.at(V).size(); i++) { if (!visited.at(G.at(V).at(i))) cnt.at(G.at(V).at(i)) += cnt.at(V); dfs(G.at(V).at(i)); } } int main() { ll N, Q; cin >> N >> Q; for (ll i = 0; i < N - 1; i++) { ll a, b; cin >> a >> b; a--, b--; G.at(a).push_back(b); G.at(b).push_back(a); } for (ll i = 0; i < Q; i++) { ll p, x; cin >> p >> x; p--; cnt.at(p) += x; } dfs(0); for (ll i = 0; i < N; i++) { cout << cnt.at(i); if (i == N - 1) cout << endl; else cout << " "; } }
replace
31
34
31
34
0
p02936
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; int increment(vector<vector<int>> &c, int parent, int start, int x, vector<ll> steps, vector<ll> &scores) { x += steps.at(start); scores.at(start) = x; for (int node : c.at(start)) { if (node == parent) { continue; } increment(c, start, node, x, steps, scores); } return 0; } int main() { int N, Q; cin >> N >> Q; vector<vector<int>> c(N); vector<ll> scores(N); int a, b; for (int i = 0; i < N - 1; i++) { cin >> a >> b; a--; b--; c.at(a).push_back(b); c.at(b).push_back(a); } int p, x; vector<ll> steps(N); for (int i = 0; i < Q; i++) { cin >> p >> x; p--; steps.at(p) += x; } increment(c, -1, 0, 0, steps, scores); for (int i = 0; i < N; i++) { if (i != 0) { cout << " "; } cout << scores.at(i); } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; int increment(vector<vector<int>> &c, int parent, int start, int x, vector<ll> &steps, vector<ll> &scores) { x += steps.at(start); scores.at(start) = x; for (int node : c.at(start)) { if (node == parent) { continue; } increment(c, start, node, x, steps, scores); } return 0; } int main() { int N, Q; cin >> N >> Q; vector<vector<int>> c(N); vector<ll> scores(N); int a, b; for (int i = 0; i < N - 1; i++) { cin >> a >> b; a--; b--; c.at(a).push_back(b); c.at(b).push_back(a); } int p, x; vector<ll> steps(N); for (int i = 0; i < Q; i++) { cin >> p >> x; p--; steps.at(p) += x; } increment(c, -1, 0, 0, steps, scores); for (int i = 0; i < N; i++) { if (i != 0) { cout << " "; } cout << scores.at(i); } cout << endl; return 0; }
replace
5
6
5
6
0
p02936
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (ll i = 0; i < n; ++i) #define ALL(a) (a).begin(), (a).end() using namespace std; using ll = long long; using pll = pair<ll, ll>; const ll MOD = 1e9 + 7; const ll LINF = 1LL << 60; const int INF = 1e9 + 7; struct node { vector<ll> child; ll counter = 0; }; vector<node> tree(100010); void dfs(ll crnt_node, ll parent) { tree[crnt_node].counter += tree[parent].counter; rep(i, tree[crnt_node].child.size()) { ll next_node = tree[crnt_node].child[i]; if (next_node == parent) continue; dfs(next_node, crnt_node); } } int main() { ll n, q; cin >> n >> q; rep(i, n - 1) { ll a, b; cin >> a >> b; a--, b--; tree[a].child.push_back(b); tree[b].child.push_back(a); } rep(i, q) { ll p, x; cin >> p >> x; p--; tree[p].counter += x; } rep(i, tree[0].child.size()) { ll next_node = tree[0].child[i]; dfs(next_node, 0); } rep(i, n) cout << tree[i].counter << " "; }
#include <bits/stdc++.h> #define rep(i, n) for (ll i = 0; i < n; ++i) #define ALL(a) (a).begin(), (a).end() using namespace std; using ll = long long; using pll = pair<ll, ll>; const ll MOD = 1e9 + 7; const ll LINF = 1LL << 60; const int INF = 1e9 + 7; struct node { vector<ll> child; ll counter = 0; }; vector<node> tree(200020); void dfs(ll crnt_node, ll parent) { tree[crnt_node].counter += tree[parent].counter; rep(i, tree[crnt_node].child.size()) { ll next_node = tree[crnt_node].child[i]; if (next_node == parent) continue; dfs(next_node, crnt_node); } } int main() { ll n, q; cin >> n >> q; rep(i, n - 1) { ll a, b; cin >> a >> b; a--, b--; tree[a].child.push_back(b); tree[b].child.push_back(a); } rep(i, q) { ll p, x; cin >> p >> x; p--; tree[p].counter += x; } rep(i, tree[0].child.size()) { ll next_node = tree[0].child[i]; dfs(next_node, 0); } rep(i, n) cout << tree[i].counter << " "; }
replace
18
19
18
19
0
p02936
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define FOR(i, a, b) for (ll i = (ll)a; i <= (ll)b; i++) #define DEC(i, a, b) for (ll i = (ll)a; i >= (ll)b; i--) typedef pair<ll, ll> pi; typedef pair<pi, ll> pii; typedef pair<pi, pi> pipi; #define f first #define s second typedef vector<ll> vi; typedef vector<pi> vpi; typedef vector<pii> vpii; #define pb push_back #define pf push_front #define all(v) v.begin(), v.end() #define disc(v) \ sort(all(v)); \ v.resize(unique(all(v)) - v.begin()); #define INF (ll)1e9 + 100 #define LLINF (ll)1e18 #define fastio \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define sandybridge \ __attribute__((optimize("Ofast"), target("arch=sandybridge"))) mt19937 rng(chrono::steady_clock::now() .time_since_epoch() .count()); // can be used by calling rng() or shuffle(A, A+n, rng) inline ll rand(ll x, ll y) { ++y; return (rng() % (y - x)) + x; } // inclusivesss ll n, q, a, b, pre[100005], pos[100005], coun = 1; vi adj[100005]; ll ft[100005]; // 1-index ll ls(ll x) { return (x & (-x)); } // N means the largest possible index you need for the fenwick!! Refer to // flowering for more. void update(ll l, ll r, ll v) { // Updates from l to r inclusive r++; // Update the difference at r+1 not r for (; l <= n; l += ls(l)) ft[l] += v; // Add v to the diff at l for (; r <= n; r += ls(r)) ft[r] -= v; // Minus v from diff at r+1 } ll query(ll p) { // Returns the element at p ll sum = 0; for (; p; p -= ls(p)) sum += ft[p]; return sum; } void dfs(ll x, ll p) { pre[x] = coun++; for (auto it : adj[x]) if (it != p) dfs(it, x); pos[x] = coun - 1; } int main() { fastio; cin >> n >> q; FOR(i, 1, n - 1) { cin >> a >> b; adj[a].pb(b); adj[b].pb(a); } dfs(1, -1); while (q--) { cin >> a >> b; update(pre[a], pos[a], b); } FOR(i, 1, n) cout << query(pre[i]) << " "; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define FOR(i, a, b) for (ll i = (ll)a; i <= (ll)b; i++) #define DEC(i, a, b) for (ll i = (ll)a; i >= (ll)b; i--) typedef pair<ll, ll> pi; typedef pair<pi, ll> pii; typedef pair<pi, pi> pipi; #define f first #define s second typedef vector<ll> vi; typedef vector<pi> vpi; typedef vector<pii> vpii; #define pb push_back #define pf push_front #define all(v) v.begin(), v.end() #define disc(v) \ sort(all(v)); \ v.resize(unique(all(v)) - v.begin()); #define INF (ll)1e9 + 100 #define LLINF (ll)1e18 #define fastio \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define sandybridge \ __attribute__((optimize("Ofast"), target("arch=sandybridge"))) mt19937 rng(chrono::steady_clock::now() .time_since_epoch() .count()); // can be used by calling rng() or shuffle(A, A+n, rng) inline ll rand(ll x, ll y) { ++y; return (rng() % (y - x)) + x; } // inclusivesss ll n, q, a, b, pre[200005], pos[200005], coun = 1; vi adj[200005]; ll ft[200005]; // 1-index ll ls(ll x) { return (x & (-x)); } // N means the largest possible index you need for the fenwick!! Refer to // flowering for more. void update(ll l, ll r, ll v) { // Updates from l to r inclusive r++; // Update the difference at r+1 not r for (; l <= n; l += ls(l)) ft[l] += v; // Add v to the diff at l for (; r <= n; r += ls(r)) ft[r] -= v; // Minus v from diff at r+1 } ll query(ll p) { // Returns the element at p ll sum = 0; for (; p; p -= ls(p)) sum += ft[p]; return sum; } void dfs(ll x, ll p) { pre[x] = coun++; for (auto it : adj[x]) if (it != p) dfs(it, x); pos[x] = coun - 1; } int main() { fastio; cin >> n >> q; FOR(i, 1, n - 1) { cin >> a >> b; adj[a].pb(b); adj[b].pb(a); } dfs(1, -1); while (q--) { cin >> a >> b; update(pre[a], pos[a], b); } FOR(i, 1, n) cout << query(pre[i]) << " "; }
replace
37
40
37
40
0
p02936
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cmath> #include <cstdlib> #include <fstream> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <sstream> #include <string.h> #include <string> #include <vector> using namespace std; long long dp[3][100100] = {}; const long long INF = 1LL << 60; #define MOD 1000000007 #define Int int64_t #define PI 3.14159265358979 #define dump(a) cout << a << endl; template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } /** * @fn * 最大値を取ります.Vector<int>を渡すこと. */ long long int imax(vector<int> a) { int MAX = -10000000; for (int i = 0; i < a.size(); i++) { if (MAX < a.at(i)) MAX = a.at(i); } return MAX; } /** * @fn * 最小値を取ります.Vector<int>を渡すこと. */ long long int imin(vector<int> a) { int MIN = 1000000000; for (int i = 0; i < a.size(); i++) { if (MIN > a.at(i)) MIN = a.at(i); } return MIN; } /** * @fn * 自然数について、桁数を求める */ Int getDigit(Int num) { return (Int)(log10(num)) + 1; } int get2Digit(Int num) { int cnt = 0; while (num > 0) { if (num % 2 != 0) { return cnt; } cnt++; num /= 2; } return cnt; } long long int absmin(vector<int> a, long long int b) { int mini = 100000000; int d = 0; for (int i = 0; i < a.size(); i++) { if (mini > abs(a.at(i) - b)) { mini = abs(a.at(i) - b); d = i; } } return d; } long long int absmax(vector<int> a, long long int b) { int max = -100000000; int d = 0; for (int i = 0; i < a.size(); i++) { if (max < abs(a.at(i) - b)) { max = abs(a.at(i) - b); d = i; } } return d; } int gcd(int a, int b) { if (a < b) swap(a, b); if (b < 1) return -1; if (a % b == 0) return b; return gcd(b, a % b); } void haerobatya(int start, int end, char level) { if (start > end) swap(start, end); for (int i = start; i <= end; i++) { if (i < 100) { cout << "https://atcoder.jp/contests/abc0" << i << "/tasks/abc0" << i << "_" << level << endl; } else { cout << "https://atcoder.jp/contests/abc" << i << "/tasks/abc" << i << "_" << level << endl; } } } long long int cntmod(long long int warareru, long long int mod) { long long int i = 0; for (i = 0; true; i++) { if (warareru % mod == 0 && warareru >= mod) { warareru /= mod; } else { return i; } } } void ansYes(bool a) { if (a) cout << "Yes" << endl; else cout << "No" << endl; } void ansYES(bool a) { if (a) cout << "YES" << endl; else cout << "NO" << endl; } Int euclidean_gcd(Int a, Int b) { while (true) { if (a < b) swap(a, b); if (!b) break; } return a; } // UnionFind群 class UnionFind { protected: vector<int> par; vector<int> data; int cnt; public: UnionFind() : par(1), data(1), cnt(1){}; UnionFind(int N); UnionFind(const vector<int> &par, const vector<int> &data, int cnt); int root(int x); int marge(int x, int y); bool same(int x, int y); int add(); int add(int x); inline int getSize(int x) { return data.at(root(x)); }; int allroot(); inline bool isroot(int x) { return (par.at(x) == -1); }; }; UnionFind::UnionFind(int N) : par(N), data(N), cnt(N) { for (int i = 0; i < N; i++) { par.at(i) = -1; data.at(i) = 1; } } int UnionFind::root(int x) { if (par.at(x) == -1) return x; return par.at(x) = root(par.at(x)); // 経路圧縮 } int UnionFind::marge(int x, int y) { int rx = root(x); int ry = root(y); if (rx == ry) return rx; else { // ノード数の少ない方に接続 if (data.at(rx) < data.at(ry)) swap(rx, ry); par.at(ry) = rx; data.at(rx) += data.at(ry); return rx; } } bool UnionFind::same(int x, int y) { return root(x) == root(y); } int UnionFind::add() { par.push_back(cnt); cnt++; return cnt - 1; } int UnionFind::add(int x) { par.push_back(cnt); cnt++; return marge(x, cnt); } int UnionFind::allroot() { int cnt = 0; for (const auto it : par) { if (it == -1) cnt++; } return cnt; } // 約数の数を数える int yakusu(int n) { int cnt = 0; for (int i = 1; i * i <= n; i++) { if (n % i == 0) { cnt++; if (i * i != n) cnt++; } } return cnt; } int vecsum(vector<int> a) { int res = 0; for (auto z : a) { res += z; } return res; } vector<int> yakusurekkyo(int n) { vector<int> a; for (int i = 1; i * i <= n; i++) { if (n % i == 0) { a.push_back(i); if (i * i != n) a.push_back(n / i); } } return a; } vector<Int> tree(100011); vector<queue<Int>> to(100011, queue<Int>()); void bfs(Int x, Int b) { tree.at(x) += b; Int tmp = tree.at(x); if (to.at(x).empty()) return; while (!to.at(x).empty()) { Int z = to.at(x).front(); to.at(x).pop(); bfs(z, tmp); } return; } int main() { int n, q; cin >> n >> q; for (int i = 0; i < n - 1; i++) { Int a, b; cin >> a >> b; to.at(a - 1).push(b - 1); } for (int i = 0; i < q; i++) { Int basyo, kaisu; cin >> basyo >> kaisu; tree.at(basyo - 1) += kaisu; } bfs(0, 0); for (int i = 0; i < n; i++) { cout << tree.at(i) << endl; } }
#include <algorithm> #include <bitset> #include <cmath> #include <cstdlib> #include <fstream> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <sstream> #include <string.h> #include <string> #include <vector> using namespace std; long long dp[3][100100] = {}; const long long INF = 1LL << 60; #define MOD 1000000007 #define Int int64_t #define PI 3.14159265358979 #define dump(a) cout << a << endl; template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } /** * @fn * 最大値を取ります.Vector<int>を渡すこと. */ long long int imax(vector<int> a) { int MAX = -10000000; for (int i = 0; i < a.size(); i++) { if (MAX < a.at(i)) MAX = a.at(i); } return MAX; } /** * @fn * 最小値を取ります.Vector<int>を渡すこと. */ long long int imin(vector<int> a) { int MIN = 1000000000; for (int i = 0; i < a.size(); i++) { if (MIN > a.at(i)) MIN = a.at(i); } return MIN; } /** * @fn * 自然数について、桁数を求める */ Int getDigit(Int num) { return (Int)(log10(num)) + 1; } int get2Digit(Int num) { int cnt = 0; while (num > 0) { if (num % 2 != 0) { return cnt; } cnt++; num /= 2; } return cnt; } long long int absmin(vector<int> a, long long int b) { int mini = 100000000; int d = 0; for (int i = 0; i < a.size(); i++) { if (mini > abs(a.at(i) - b)) { mini = abs(a.at(i) - b); d = i; } } return d; } long long int absmax(vector<int> a, long long int b) { int max = -100000000; int d = 0; for (int i = 0; i < a.size(); i++) { if (max < abs(a.at(i) - b)) { max = abs(a.at(i) - b); d = i; } } return d; } int gcd(int a, int b) { if (a < b) swap(a, b); if (b < 1) return -1; if (a % b == 0) return b; return gcd(b, a % b); } void haerobatya(int start, int end, char level) { if (start > end) swap(start, end); for (int i = start; i <= end; i++) { if (i < 100) { cout << "https://atcoder.jp/contests/abc0" << i << "/tasks/abc0" << i << "_" << level << endl; } else { cout << "https://atcoder.jp/contests/abc" << i << "/tasks/abc" << i << "_" << level << endl; } } } long long int cntmod(long long int warareru, long long int mod) { long long int i = 0; for (i = 0; true; i++) { if (warareru % mod == 0 && warareru >= mod) { warareru /= mod; } else { return i; } } } void ansYes(bool a) { if (a) cout << "Yes" << endl; else cout << "No" << endl; } void ansYES(bool a) { if (a) cout << "YES" << endl; else cout << "NO" << endl; } Int euclidean_gcd(Int a, Int b) { while (true) { if (a < b) swap(a, b); if (!b) break; } return a; } // UnionFind群 class UnionFind { protected: vector<int> par; vector<int> data; int cnt; public: UnionFind() : par(1), data(1), cnt(1){}; UnionFind(int N); UnionFind(const vector<int> &par, const vector<int> &data, int cnt); int root(int x); int marge(int x, int y); bool same(int x, int y); int add(); int add(int x); inline int getSize(int x) { return data.at(root(x)); }; int allroot(); inline bool isroot(int x) { return (par.at(x) == -1); }; }; UnionFind::UnionFind(int N) : par(N), data(N), cnt(N) { for (int i = 0; i < N; i++) { par.at(i) = -1; data.at(i) = 1; } } int UnionFind::root(int x) { if (par.at(x) == -1) return x; return par.at(x) = root(par.at(x)); // 経路圧縮 } int UnionFind::marge(int x, int y) { int rx = root(x); int ry = root(y); if (rx == ry) return rx; else { // ノード数の少ない方に接続 if (data.at(rx) < data.at(ry)) swap(rx, ry); par.at(ry) = rx; data.at(rx) += data.at(ry); return rx; } } bool UnionFind::same(int x, int y) { return root(x) == root(y); } int UnionFind::add() { par.push_back(cnt); cnt++; return cnt - 1; } int UnionFind::add(int x) { par.push_back(cnt); cnt++; return marge(x, cnt); } int UnionFind::allroot() { int cnt = 0; for (const auto it : par) { if (it == -1) cnt++; } return cnt; } // 約数の数を数える int yakusu(int n) { int cnt = 0; for (int i = 1; i * i <= n; i++) { if (n % i == 0) { cnt++; if (i * i != n) cnt++; } } return cnt; } int vecsum(vector<int> a) { int res = 0; for (auto z : a) { res += z; } return res; } vector<int> yakusurekkyo(int n) { vector<int> a; for (int i = 1; i * i <= n; i++) { if (n % i == 0) { a.push_back(i); if (i * i != n) a.push_back(n / i); } } return a; } vector<Int> tree(200011, 0); vector<queue<Int>> to(200011, queue<Int>()); void bfs(Int x, Int b) { tree.at(x) += b; Int tmp = tree.at(x); if (to.at(x).empty()) return; while (!to.at(x).empty()) { Int z = to.at(x).front(); to.at(x).pop(); bfs(z, tmp); } return; } int main() { int n, q; cin >> n >> q; for (int i = 0; i < n - 1; i++) { Int a, b; cin >> a >> b; to.at(a - 1).push(b - 1); } for (int i = 0; i < q; i++) { Int basyo, kaisu; cin >> basyo >> kaisu; tree.at(basyo - 1) += kaisu; } bfs(0, 0); for (int i = 0; i < n; i++) { cout << tree.at(i) << endl; } }
replace
282
284
282
284
0
p02936
C++
Runtime Error
#include <cctype> #include <cstdio> #define getc \ (l == r && (r = (l = c) + fread(c, 1, 1 << 21, stdin), l == r) ? EOF : *l++) char c[1 << 21], *l = c, *r = c; inline int read() { int x = 0; char ch = getc; while (!isdigit(ch)) ch = getc; while (isdigit(ch)) { x = x * 10 + (ch ^ 48); ch = getc; } return x; } const int N = 100010; int n, q, cnt, last[N], s[N]; struct Edge { int y, pre; } E[N << 1]; void dfs(int x, int fa, int sum) { s[x] += sum; for (int j = last[x]; j; j = E[j].pre) if (E[j].y != fa) dfs(E[j].y, x, s[x]); } int main() { n = read(); q = read(); for (int i = 1; i < n; ++i) { int x = read(), y = read(); E[++cnt] = (Edge){y, last[x]}; last[x] = cnt; E[++cnt] = (Edge){x, last[y]}; last[y] = cnt; } while (q--) { int p = read(), x = read(); s[p] += x; } dfs(1, -1, 0); for (int i = 1; i <= n; ++i) printf("%d ", s[i]); return 0; }
#include <cctype> #include <cstdio> #define getc \ (l == r && (r = (l = c) + fread(c, 1, 1 << 21, stdin), l == r) ? EOF : *l++) char c[1 << 21], *l = c, *r = c; inline int read() { int x = 0; char ch = getc; while (!isdigit(ch)) ch = getc; while (isdigit(ch)) { x = x * 10 + (ch ^ 48); ch = getc; } return x; } const int N = 200010; int n, q, cnt, last[N], s[N]; struct Edge { int y, pre; } E[N << 1]; void dfs(int x, int fa, int sum) { s[x] += sum; for (int j = last[x]; j; j = E[j].pre) if (E[j].y != fa) dfs(E[j].y, x, s[x]); } int main() { n = read(); q = read(); for (int i = 1; i < n; ++i) { int x = read(), y = read(); E[++cnt] = (Edge){y, last[x]}; last[x] = cnt; E[++cnt] = (Edge){x, last[y]}; last[y] = cnt; } while (q--) { int p = read(), x = read(); s[p] += x; } dfs(1, -1, 0); for (int i = 1; i <= n; ++i) printf("%d ", s[i]); return 0; }
replace
18
19
18
19
0
p02936
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 5; vector<int> edges[MAXN]; long long a[MAXN], add[MAXN]; bool vis[MAXN]; void dfs(int u, long long qtt) { vis[u] = true; a[u] = add[u] + qtt; for (int v : edges[u]) { if (!vis[v]) dfs(v, qtt + add[u]); } } int main() { ios::sync_with_stdio(false); cin.tie(NULL); int n, q; cin >> n >> q; for (int i = 1; i <= n - 1; i++) { int u, v; cin >> u >> v; edges[u].push_back(v); edges[v].push_back(u); } while (q--) { int u, x; cin >> u >> x; add[u] += x; } dfs(1, 0); for (int i = 1; i <= n; i++) cout << a[i] << ' '; cout << endl; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 5; vector<int> edges[MAXN]; long long a[MAXN], add[MAXN]; bool vis[MAXN]; void dfs(int u, long long qtt) { vis[u] = true; a[u] = add[u] + qtt; for (int v : edges[u]) { if (!vis[v]) dfs(v, qtt + add[u]); } } int main() { ios::sync_with_stdio(false); cin.tie(NULL); int n, q; cin >> n >> q; for (int i = 1; i <= n - 1; i++) { int u, v; cin >> u >> v; edges[u].push_back(v); edges[v].push_back(u); } while (q--) { int u, x; cin >> u >> x; add[u] += x; } dfs(1, 0); for (int i = 1; i <= n; i++) cout << a[i] << ' '; cout << endl; }
replace
3
4
3
4
0
p02936
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; void solve(int v, const vector<vector<int>> G, vector<bool> &seen, vector<long long> &cost) { seen[v] = 1; for (auto nv : G[v]) { if (seen[nv]) continue; cost[nv] += cost[v]; solve(nv, G, seen, cost); } } int main() { int N, Q; scanf("%d%d", &N, &Q); vector<vector<int>> G(N, vector<int>()); for (int i = 0; i < N - 1; i++) { int a, b; scanf("%d%d", &a, &b); a--, b--; G[a].push_back(b); G[b].push_back(a); } vector<long long> cost(N); for (int i = 0; i < Q; i++) { int a; long long b; scanf("%d%lld", &a, &b); a--; cost[a] += b; } vector<bool> seen(N); solve(0, G, seen, cost); for (int i = 0; i < N; i++) { if (i) printf(" "); printf("%lld", cost[i]); } printf("\n"); return 0; }
#include <bits/stdc++.h> using namespace std; void solve(int v, const vector<vector<int>> &G, vector<bool> &seen, vector<long long> &cost) { seen[v] = 1; for (auto nv : G[v]) { if (seen[nv]) continue; cost[nv] += cost[v]; solve(nv, G, seen, cost); } } int main() { int N, Q; scanf("%d%d", &N, &Q); vector<vector<int>> G(N, vector<int>()); for (int i = 0; i < N - 1; i++) { int a, b; scanf("%d%d", &a, &b); a--, b--; G[a].push_back(b); G[b].push_back(a); } vector<long long> cost(N); for (int i = 0; i < Q; i++) { int a; long long b; scanf("%d%lld", &a, &b); a--; cost[a] += b; } vector<bool> seen(N); solve(0, G, seen, cost); for (int i = 0; i < N; i++) { if (i) printf(" "); printf("%lld", cost[i]); } printf("\n"); return 0; }
replace
2
3
2
3
TLE
p02936
C++
Runtime Error
#include <algorithm> #include <bits/stdc++.h> #include <cctype> #include <cmath> #include <cstdio> #include <cstring> #include <functional> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define DEBUG(x) cout << '>' << #x << ':' << x << endl; #define REP(i, n) for (int i = 0; i < (n); i++) #define FOR(i, a, b) for (int i = (a); i <= (b); i++) #define FORD(i, a, b) for (int i = (a); i >= (b); i--) inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } const int INF = 1 << 29; typedef long long int ll; inline int two(int n) { return 1 << n; } inline int test(int n, int b) { return (n >> b) & 1; } inline void set_bit(int &n, int b) { n |= two(b); } inline void unset_bit(int &n, int b) { n &= ~two(b); } inline int last_bit(int n) { return n & (-n); } inline int ones(int n) { int res = 0; while (n && ++res) n -= n & (-n); return res; } template <class T> void chmax(T &a, const T &b) { a = max(a, b); } template <class T> void chmin(T &a, const T &b) { a = min(a, b); } const int MOD = 1000000007; #define pp pair<ll, ll> bool isPowerOfTwo(ll x) { return x && (!(x & (x - 1))); } bool isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } int highestPowerof2(unsigned int n) { // Invalid input if (n < 1) return 0; int res = 1; for (int i = 0; i < 8 * sizeof(unsigned int); i++) { int curr = 1 << i; if (curr > n) break; res = curr; } return res; } long long binpow(long long a, long long b) { long long res = 1; while (b > 0) { if (b & 1) res = res * a; a = a * a; b >>= 1; } return res; } const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1}; const int dy[] = {0, -1, 0, 1, 1, -1, -1, 1}; /*lcm(gcd(N1, M), gcd(N2, M), ..., gcd(Nk, M)) = gcd(lcm(N1, ..., Nk), M) gcd(lcm(N1, M), lcm(N2, M), ..., lcm(Nk, M)) = lcm(gcd(N1, ..., Nk), M). If gcd(N1, N2) = 1, then gcd(N1·N2, M) = gcd(N1, M)·gcd(N2, M) lcm(N1·N2, M) = lcm(N1, M)·lcm(N2, M)/M. lcm(M, N, P) · gcd(M, N) · gcd(N, P) · gcd(P, M) = NMP · gcd(N, M, P). */ //////////////////////////////////////////////////////////////////// const int maxn = 1e5 + 10; ll ans[maxn]; vector<ll> adj[maxn]; bool vis[maxn]; void dfs(ll x, ll p) { vis[x] = true; ans[x] += ans[p]; for (auto i : adj[x]) { if (!vis[i]) { dfs(i, x); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; while (t--) { ll n, q; cin >> n >> q; ll a, b; REP(i, n - 1) { cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } REP(i, q) { cin >> a >> b; ans[a] += b; } dfs(1, 0); for (int i = 1; i <= n; i++) cout << ans[i] << " "; } return 0; } /* Look for: * the exact constraints (multiple sets are too slow for n=10^6 :( ) * special cases (n=1?) * 1LL<<i and not 1<<i * overflow (int vs ll?) * array bounds * if you have no idea just guess the appropriate well-known algo instead of * doing nothing :/ */
#include <algorithm> #include <bits/stdc++.h> #include <cctype> #include <cmath> #include <cstdio> #include <cstring> #include <functional> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define DEBUG(x) cout << '>' << #x << ':' << x << endl; #define REP(i, n) for (int i = 0; i < (n); i++) #define FOR(i, a, b) for (int i = (a); i <= (b); i++) #define FORD(i, a, b) for (int i = (a); i >= (b); i--) inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } const int INF = 1 << 29; typedef long long int ll; inline int two(int n) { return 1 << n; } inline int test(int n, int b) { return (n >> b) & 1; } inline void set_bit(int &n, int b) { n |= two(b); } inline void unset_bit(int &n, int b) { n &= ~two(b); } inline int last_bit(int n) { return n & (-n); } inline int ones(int n) { int res = 0; while (n && ++res) n -= n & (-n); return res; } template <class T> void chmax(T &a, const T &b) { a = max(a, b); } template <class T> void chmin(T &a, const T &b) { a = min(a, b); } const int MOD = 1000000007; #define pp pair<ll, ll> bool isPowerOfTwo(ll x) { return x && (!(x & (x - 1))); } bool isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } int highestPowerof2(unsigned int n) { // Invalid input if (n < 1) return 0; int res = 1; for (int i = 0; i < 8 * sizeof(unsigned int); i++) { int curr = 1 << i; if (curr > n) break; res = curr; } return res; } long long binpow(long long a, long long b) { long long res = 1; while (b > 0) { if (b & 1) res = res * a; a = a * a; b >>= 1; } return res; } const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1}; const int dy[] = {0, -1, 0, 1, 1, -1, -1, 1}; /*lcm(gcd(N1, M), gcd(N2, M), ..., gcd(Nk, M)) = gcd(lcm(N1, ..., Nk), M) gcd(lcm(N1, M), lcm(N2, M), ..., lcm(Nk, M)) = lcm(gcd(N1, ..., Nk), M). If gcd(N1, N2) = 1, then gcd(N1·N2, M) = gcd(N1, M)·gcd(N2, M) lcm(N1·N2, M) = lcm(N1, M)·lcm(N2, M)/M. lcm(M, N, P) · gcd(M, N) · gcd(N, P) · gcd(P, M) = NMP · gcd(N, M, P). */ //////////////////////////////////////////////////////////////////// const int maxn = 2 * (1e5 + 10); ll ans[maxn]; vector<ll> adj[maxn]; bool vis[maxn]; void dfs(ll x, ll p) { vis[x] = true; ans[x] += ans[p]; for (auto i : adj[x]) { if (!vis[i]) { dfs(i, x); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; while (t--) { ll n, q; cin >> n >> q; ll a, b; REP(i, n - 1) { cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } REP(i, q) { cin >> a >> b; ans[a] += b; } dfs(1, 0); for (int i = 1; i <= n; i++) cout << ans[i] << " "; } return 0; } /* Look for: * the exact constraints (multiple sets are too slow for n=10^6 :( ) * special cases (n=1?) * 1LL<<i and not 1<<i * overflow (int vs ll?) * array bounds * if you have no idea just guess the appropriate well-known algo instead of * doing nothing :/ */
replace
92
93
92
93
0
p02936
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define MOD 1000000007; #define INF 1000000000000000001; #define ll long long vector<int> g[200005]; vector<pair<int, int>> wg[123456]; int level[123456]; void dfs(int s, int pre) { for (auto i : g[s]) { if (i != pre) { level[i] += level[s]; dfs(i, s); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; for (size_t i = 1; i < n; i++) { int x, y; cin >> x >> y; g[x].push_back(y); g[y].push_back(x); } for (int i = 0; i < m; ++i) { int p, q; cin >> p >> q; level[p] += q; } dfs(1, 0); for (int i = 1; i <= n; i++) cout << level[i] << " "; cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define MOD 1000000007; #define INF 1000000000000000001; #define ll long long vector<int> g[200005]; vector<pair<int, int>> wg[123456]; int level[200005]; void dfs(int s, int pre) { for (auto i : g[s]) { if (i != pre) { level[i] += level[s]; dfs(i, s); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; for (size_t i = 1; i < n; i++) { int x, y; cin >> x >> y; g[x].push_back(y); g[y].push_back(x); } for (int i = 0; i < m; ++i) { int p, q; cin >> p >> q; level[p] += q; } dfs(1, 0); for (int i = 1; i <= n; i++) cout << level[i] << " "; cout << endl; return 0; }
replace
7
8
7
8
0
p02936
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (ll i = 0; i < n; ++i) #define all_map(itr, mp) for (auto itr = mp.begin(); itr != mp.end(); itr++) #define ALL(a) (a).begin(), (a).end() using namespace std; using ll = long long; using pll = pair<ll, ll>; const ll MOD = 1e9 + 7; const ll LINF = 1LL << 62; const int INF = 1e9 + 7; vector<vector<ll>> tree(100010); vector<ll> point(100010, 0); void dfs(ll crr, ll parent) { rep(i, tree[crr].size()) { if (tree[crr][i] == parent) continue; point[tree[crr][i]] += point[crr]; dfs(tree[crr][i], crr); } } int main() { ll n, q; cin >> n >> q; rep(i, n - 1) { ll a, b; cin >> a >> b; a--, b--; tree[a].push_back(b); tree[b].push_back(a); } rep(i, q) { ll p, x; cin >> p >> x; p--; point[p] += x; } dfs(0, 0); rep(i, n) cout << point[i] << " "; }
#include <bits/stdc++.h> #define rep(i, n) for (ll i = 0; i < n; ++i) #define all_map(itr, mp) for (auto itr = mp.begin(); itr != mp.end(); itr++) #define ALL(a) (a).begin(), (a).end() using namespace std; using ll = long long; using pll = pair<ll, ll>; const ll MOD = 1e9 + 7; const ll LINF = 1LL << 62; const int INF = 1e9 + 7; vector<vector<ll>> tree(200010); vector<ll> point(200010, 0); void dfs(ll crr, ll parent) { rep(i, tree[crr].size()) { if (tree[crr][i] == parent) continue; point[tree[crr][i]] += point[crr]; dfs(tree[crr][i], crr); } } int main() { ll n, q; cin >> n >> q; rep(i, n - 1) { ll a, b; cin >> a >> b; a--, b--; tree[a].push_back(b); tree[b].push_back(a); } rep(i, q) { ll p, x; cin >> p >> x; p--; point[p] += x; } dfs(0, 0); rep(i, n) cout << point[i] << " "; }
replace
14
16
14
16
0
p02936
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define REP(i, n) for (ll i = 0; i < (ll)(n); i++) #define REPD(i, n) for (ll i = n - 1; i >= 0; i--) #define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++) #define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--) #define ALL(x) (x).begin(), (x).end() #define SIZE(x) ((ll)(x).size()) #define MAX(x) *max_element(ALL(x)) #define MIN(x) *min_element(ALL(x)) #define INF 1000000000000 #define MOD 10000007 #define MAXR 100000 ll N, Q; vector<vector<ll>> G(200010); vector<ll> cost(200010, 0); vector<bool> visited(200010, false); void dfs(ll i, ll prev_i) { if (!visited[i]) { visited[i] = true; cost[i] += cost[prev_i]; } for (auto next_i : G[i]) dfs(next_i, i); } signed main() { cin >> N >> Q; FOR(i, 1, N - 1) { ll a, b; cin >> a >> b; G[a].push_back(b); G[b].push_back(a); } REP(j, Q) { ll p, x; cin >> p >> x; cost[p] += x; } dfs(1, 0); FOR(i, 1, N) cout << cost[i] << " "; cout << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define REP(i, n) for (ll i = 0; i < (ll)(n); i++) #define REPD(i, n) for (ll i = n - 1; i >= 0; i--) #define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++) #define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--) #define ALL(x) (x).begin(), (x).end() #define SIZE(x) ((ll)(x).size()) #define MAX(x) *max_element(ALL(x)) #define MIN(x) *min_element(ALL(x)) #define INF 1000000000000 #define MOD 10000007 #define MAXR 100000 ll N, Q; vector<vector<ll>> G(200010); vector<ll> cost(200010, 0); vector<bool> visited(200010, false); void dfs(ll i, ll prev_i) { if (visited[i]) return; visited[i] = true; cost[i] += cost[prev_i]; for (auto next_i : G[i]) dfs(next_i, i); } signed main() { cin >> N >> Q; FOR(i, 1, N - 1) { ll a, b; cin >> a >> b; G[a].push_back(b); G[b].push_back(a); } REP(j, Q) { ll p, x; cin >> p >> x; cost[p] += x; } dfs(1, 0); FOR(i, 1, N) cout << cost[i] << " "; cout << endl; }
replace
24
28
24
28
-11
p02936
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll MOD = 1000000007; vector<int> to[20005]; vector<int> ans; void dfs(int v, int p = -1) { for (int u = 0; u < int(to[v].size()); u++) { int x = to[v][u]; if (x == p) continue; ans[x] += ans[v]; dfs(x, v); } } int main() { int n, q; cin >> n >> q; for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; --a; --b; to[a].push_back(b); to[b].push_back(a); } ans.resize(n); for (int i = 0; i < q; i++) { int p, x; cin >> p >> x; --p; ans[p] += x; } dfs(0); for (int i = 0; i < n; i++) cout << ans[i] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll MOD = 1000000007; vector<int> to[200005]; vector<int> ans; void dfs(int v, int p = -1) { for (int u = 0; u < int(to[v].size()); u++) { int x = to[v][u]; if (x == p) continue; ans[x] += ans[v]; dfs(x, v); } } int main() { int n, q; cin >> n >> q; for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; --a; --b; to[a].push_back(b); to[b].push_back(a); } ans.resize(n); for (int i = 0; i < q; i++) { int p, x; cin >> p >> x; --p; ans[p] += x; } dfs(0); for (int i = 0; i < n; i++) cout << ans[i] << endl; return 0; }
replace
6
7
6
7
0
p02936
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> pi; typedef vector<ll> vi; typedef vector<pi> vpi; #define f first #define s second #define FOR(i, s, e) for (ll i = s; i <= ll(e); ++i) #define DEC(i, s, e) for (ll i = s; i >= ll(e); --i) #define pb push_back #define all(x) (x).begin(), (x).end() #define lbd(x, y) lower_bound(all(x), y) #define ubd(x, y) upper_bound(all(x), y) #define aFOR(i, x) for (auto i : x) #define mem(x, i) memset(x, i, sizeof x) #define fast ios_base::sync_with_stdio(false), cin.tie(0) typedef long double ld; #define maxn 100001 int N, Q; vi adj[maxn]; int st[maxn], en[maxn]; int fw[maxn]; int cur = 1; void upd(int p, int v) { // update position p by +=v for (int i = p; i <= N; i += (i & (-i))) { fw[i] += v; } } void update_range(int a, int b, int c) { // update position a to b by +=c upd(a, c); // upd function from the PURQ fenwick upd(b + 1, -c); } int qry(int p) { // query p int ans = 0; for (int i = p; i > 0; i -= (i & (-i))) { ans += fw[i]; } return ans; } void dfs(int x, int p) { st[x] = cur++; aFOR(i, adj[x]) if (i != p) dfs(i, x); en[x] = cur - 1; } int32_t main() { cin >> N >> Q; FOR(i, 1, N - 1) { int a, b; cin >> a >> b; adj[a].pb(b); adj[b].pb(a); } dfs(1, -1); FOR(i, 1, Q) { int p, x; cin >> p >> x; update_range(st[p], en[p], x); } FOR(i, 1, N) cout << qry(st[i]) << ' '; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> pi; typedef vector<ll> vi; typedef vector<pi> vpi; #define f first #define s second #define FOR(i, s, e) for (ll i = s; i <= ll(e); ++i) #define DEC(i, s, e) for (ll i = s; i >= ll(e); --i) #define pb push_back #define all(x) (x).begin(), (x).end() #define lbd(x, y) lower_bound(all(x), y) #define ubd(x, y) upper_bound(all(x), y) #define aFOR(i, x) for (auto i : x) #define mem(x, i) memset(x, i, sizeof x) #define fast ios_base::sync_with_stdio(false), cin.tie(0) typedef long double ld; #define maxn 200001 int N, Q; vi adj[maxn]; int st[maxn], en[maxn]; int fw[maxn]; int cur = 1; void upd(int p, int v) { // update position p by +=v for (int i = p; i <= N; i += (i & (-i))) { fw[i] += v; } } void update_range(int a, int b, int c) { // update position a to b by +=c upd(a, c); // upd function from the PURQ fenwick upd(b + 1, -c); } int qry(int p) { // query p int ans = 0; for (int i = p; i > 0; i -= (i & (-i))) { ans += fw[i]; } return ans; } void dfs(int x, int p) { st[x] = cur++; aFOR(i, adj[x]) if (i != p) dfs(i, x); en[x] = cur - 1; } int32_t main() { cin >> N >> Q; FOR(i, 1, N - 1) { int a, b; cin >> a >> b; adj[a].pb(b); adj[b].pb(a); } dfs(1, -1); FOR(i, 1, Q) { int p, x; cin >> p >> x; update_range(st[p], en[p], x); } FOR(i, 1, N) cout << qry(st[i]) << ' '; }
replace
19
20
19
20
0
p02936
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <chrono> #include <cmath> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <stack> #include <unordered_map> #include <vector> using namespace std; using ll = long long; const long long INF = (long long)1e18 + 1; #define DIV 1000000007 vector<ll> counter(2000001); void countup(unordered_map<ll, vector<ll>> tree, ll prev, ll next) { counter[next] += counter[prev]; for (auto elem : tree[next]) { if (elem == prev) continue; countup(tree, next, elem); } } int main() { cin.tie(0); ios::sync_with_stdio(false); #ifdef TEST chrono::system_clock::time_point start, end; start = chrono::system_clock::now(); #endif long idx = 0; long N, Q; cin >> N >> Q; unordered_map<ll, vector<ll>> tree; for (size_t i = 1; i <= N - 1; i++) { ll a, b; cin >> a >> b; tree[a].push_back(b); tree[b].push_back(a); } for (size_t i = 0; i < Q; i++) { long p, x; cin >> p >> x; counter[p] += x; } countup(tree, 0L, 1L); for (size_t i = 1; i <= N; i++) { cout << counter[i] << " "; } cout << endl; #ifdef TEST end = chrono::system_clock::now(); cerr << static_cast<double>( chrono::duration_cast<chrono::microseconds>(end - start).count() / 1000.0) << "[ms]" << endl; #endif return 0; }
#include <algorithm> #include <bitset> #include <chrono> #include <cmath> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <stack> #include <unordered_map> #include <vector> using namespace std; using ll = long long; const long long INF = (long long)1e18 + 1; #define DIV 1000000007 vector<ll> counter(2000001); void countup(unordered_map<ll, vector<ll>> &tree, ll prev, ll next) { counter[next] += counter[prev]; for (auto elem : tree[next]) { if (elem == prev) continue; countup(tree, next, elem); } } int main() { cin.tie(0); ios::sync_with_stdio(false); #ifdef TEST chrono::system_clock::time_point start, end; start = chrono::system_clock::now(); #endif long idx = 0; long N, Q; cin >> N >> Q; unordered_map<ll, vector<ll>> tree; for (size_t i = 1; i <= N - 1; i++) { ll a, b; cin >> a >> b; tree[a].push_back(b); tree[b].push_back(a); } for (size_t i = 0; i < Q; i++) { long p, x; cin >> p >> x; counter[p] += x; } countup(tree, 0L, 1L); for (size_t i = 1; i <= N; i++) { cout << counter[i] << " "; } cout << endl; #ifdef TEST end = chrono::system_clock::now(); cerr << static_cast<double>( chrono::duration_cast<chrono::microseconds>(end - start).count() / 1000.0) << "[ms]" << endl; #endif return 0; }
replace
19
20
19
20
TLE
p02936
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #ifdef ENABLE_DEBUG_OUTPUT #define DEBUG_LOG(s) cout << s << endl; #else #define DEBUG_LOG(s) void(); #endif vector<vector<int>> g; vector<bool> v; vector<int64_t> c; vector<int64_t> p; void dfs(const int s, int64_t x) { if (v[s]) return; v[s] = true; x += p[s]; c[s] += x; for (int elm : g[s]) { if (not v[elm]) dfs(elm, x); } } int main() { int n, q; cin >> n >> q; g.assign(n, vector<int>(0)); for (auto i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; g[a - 1].push_back(b - 1); g[b - 1].push_back(a - 1); } for (auto i = 0; i < q; i++) { int a, b; cin >> a >> b; p[a - 1] += b; } v.assign(n, false); c.assign(n, 0); dfs(0, 0); for (auto i = 0; i < n; i++) { cout << c[i] << " "; } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #ifdef ENABLE_DEBUG_OUTPUT #define DEBUG_LOG(s) cout << s << endl; #else #define DEBUG_LOG(s) void(); #endif vector<vector<int>> g; vector<bool> v; vector<int64_t> c; vector<int64_t> p; void dfs(const int s, int64_t x) { if (v[s]) return; v[s] = true; x += p[s]; c[s] += x; for (int elm : g[s]) { if (not v[elm]) dfs(elm, x); } } int main() { int n, q; cin >> n >> q; g.assign(n, vector<int>(0)); for (auto i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; g[a - 1].push_back(b - 1); g[b - 1].push_back(a - 1); } p.assign(n, 0); for (auto i = 0; i < q; i++) { int a, b; cin >> a >> b; p[a - 1] += b; } v.assign(n, false); c.assign(n, 0); dfs(0, 0); for (auto i = 0; i < n; i++) { cout << c[i] << " "; } cout << endl; return 0; }
insert
38
38
38
39
-11
p02936
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define LLINF 9223372036854775807 #define MOD ll(1e9 + 7) #define all(x) (x).begin(), (x).end() #define dbg(x) cerr << #x << ": " << x << endl vector<bool> visited(100005, false); int main() { ll n, q2; cin >> n >> q2; vector<vector<ll>> tree(n); for (int i = 0; i < n - 1; i++) { ll a, b; cin >> a >> b; a--; b--; tree[a].push_back(b); tree[b].push_back(a); } vector<ll> ans(n, 0); for (int i = 0; i < q2; i++) { ll p, x; cin >> p >> x; p--; ans[p] += x; } queue<ll> q; q.push(0); visited[0] = true; while (!q.empty()) { ll now = q.front(); q.pop(); for (int i = 0; i < tree[now].size(); i++) { if (visited[tree[now][i]] == false) { q.push(tree[now][i]); ans[tree[now][i]] += ans[now]; visited[tree[now][i]] = true; } } } for (int i = 0; i < n; i++) { cout << ans[i] << " "; } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define LLINF 9223372036854775807 #define MOD ll(1e9 + 7) #define all(x) (x).begin(), (x).end() #define dbg(x) cerr << #x << ": " << x << endl vector<bool> visited(200005, false); int main() { ll n, q2; cin >> n >> q2; vector<vector<ll>> tree(n); for (int i = 0; i < n - 1; i++) { ll a, b; cin >> a >> b; a--; b--; tree[a].push_back(b); tree[b].push_back(a); } vector<ll> ans(n, 0); for (int i = 0; i < q2; i++) { ll p, x; cin >> p >> x; p--; ans[p] += x; } queue<ll> q; q.push(0); visited[0] = true; while (!q.empty()) { ll now = q.front(); q.pop(); for (int i = 0; i < tree[now].size(); i++) { if (visited[tree[now][i]] == false) { q.push(tree[now][i]); ans[tree[now][i]] += ans[now]; visited[tree[now][i]] = true; } } } for (int i = 0; i < n; i++) { cout << ans[i] << " "; } cout << endl; return 0; }
replace
10
11
10
11
0
p02936
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define _GLIBCXX_DEBUG #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define REP(i, n) for (int i = 1; i < (int)(n); i++) #define Sort(a) sort(a.begin(), a.end()) typedef long long int ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vll; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } vi edges[20020]; ll sum[20020]; void dfs(int i, int p) { for (int j : edges[i]) { if (j == p) continue; sum[j] += sum[i]; dfs(j, i); } } int main() { int n, q; cin >> n >> q; int a, b; rep(i, n - 1) { cin >> a >> b; edges[a - 1].push_back(b - 1); edges[b - 1].push_back(a - 1); } int p, x; rep(i, q) { cin >> p >> x; sum[p - 1] += x; } dfs(0, -1); rep(i, n) { if (i != n - 1) cout << sum[i] << " "; else cout << sum[i] << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define _GLIBCXX_DEBUG #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define REP(i, n) for (int i = 1; i < (int)(n); i++) #define Sort(a) sort(a.begin(), a.end()) typedef long long int ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vll; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } vi edges[200200]; ll sum[200200]; void dfs(int i, int p) { for (int j : edges[i]) { if (j == p) continue; sum[j] += sum[i]; dfs(j, i); } } int main() { int n, q; cin >> n >> q; int a, b; rep(i, n - 1) { cin >> a >> b; edges[a - 1].push_back(b - 1); edges[b - 1].push_back(a - 1); } int p, x; rep(i, q) { cin >> p >> x; sum[p - 1] += x; } dfs(0, -1); rep(i, n) { if (i != n - 1) cout << sum[i] << " "; else cout << sum[i] << endl; } return 0; }
replace
25
27
25
27
0
p02936
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (ll i = 0; i < (n); i++) #define rep2(i, a, b) for (ll i = (a); i < (b); i++) typedef uint64_t ull; typedef int64_t ll; typedef std::pair<ll, ll> PLL; using namespace std; ll N, Q; vector<ll> edges[100100]; ll c[100100]; void dfs(ll ix, ll carry, ll par) { c[ix] += carry; for (auto nx : edges[ix]) { if (nx == par) continue; dfs(nx, c[ix], ix); } } signed main() { cin >> N >> Q; rep(i, N - 1) { ll a, b; cin >> a >> b; a--; b--; edges[a].push_back(b); edges[b].push_back(a); } rep(i, Q) { ll p, x; cin >> p >> x; p--; c[p] += x; } dfs(0, 0, -1); rep(i, N) { cout << c[i]; if (i == N - 1) cout << endl; else cout << " "; } return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (ll i = 0; i < (n); i++) #define rep2(i, a, b) for (ll i = (a); i < (b); i++) typedef uint64_t ull; typedef int64_t ll; typedef std::pair<ll, ll> PLL; using namespace std; ll N, Q; vector<ll> edges[300100]; ll c[300100]; void dfs(ll ix, ll carry, ll par) { c[ix] += carry; for (auto nx : edges[ix]) { if (nx == par) continue; dfs(nx, c[ix], ix); } } signed main() { cin >> N >> Q; rep(i, N - 1) { ll a, b; cin >> a >> b; a--; b--; edges[a].push_back(b); edges[b].push_back(a); } rep(i, Q) { ll p, x; cin >> p >> x; p--; c[p] += x; } dfs(0, 0, -1); rep(i, N) { cout << c[i]; if (i == N - 1) cout << endl; else cout << " "; } return 0; }
replace
11
13
11
13
0
p02936
C++
Runtime Error
// https://youtu.be/mRHBP3xwgn4 #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef long long unsigned llu; typedef pair<long long, long long> pll; const long long inf = 2000000000000000000LL; // 2e18 #define pi acos(-1.0) #define fast \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define shesh "\n" #define ff first #define ss second #define pb push_back #define bp pop_back #define pf push_front #define fp pop_front #define ub upper_bound #define lb lower_bound #define all(x) x.begin(), x.end() #define debug(x) cout << "debug " << x << "\n" #define mest(a, b) memset(a, b, sizeof(a)) ll ans[100010], a[100010]; vector<ll> v[100010]; bool vis[100010]; void dfs(ll node, ll carry) { carry += a[node]; ans[node] += carry; for (ll i = 0; i < v[node].size(); i++) { if (!vis[v[node][i]]) { vis[v[node][i]] = true; dfs(v[node][i], carry); } } } int main() { fast; ll t, n, m, i, j, k, temp, flag, q, A, b, p, x; cin >> n >> q; for (i = 0; i < n - 1; i++) { cin >> A >> b; v[A].pb(b); v[b].pb(A); } while (q--) { cin >> p >> x; a[p] += x; } vis[1] = true; dfs(1, 0); for (i = 1; i <= n; i++) { cout << ans[i] << ' '; } return 0; }
// https://youtu.be/mRHBP3xwgn4 #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef long long unsigned llu; typedef pair<long long, long long> pll; const long long inf = 2000000000000000000LL; // 2e18 #define pi acos(-1.0) #define fast \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define shesh "\n" #define ff first #define ss second #define pb push_back #define bp pop_back #define pf push_front #define fp pop_front #define ub upper_bound #define lb lower_bound #define all(x) x.begin(), x.end() #define debug(x) cout << "debug " << x << "\n" #define mest(a, b) memset(a, b, sizeof(a)) ll ans[200010], a[200010]; vector<ll> v[200010]; bool vis[200010]; void dfs(ll node, ll carry) { carry += a[node]; ans[node] += carry; for (ll i = 0; i < v[node].size(); i++) { if (!vis[v[node][i]]) { vis[v[node][i]] = true; dfs(v[node][i], carry); } } } int main() { fast; ll t, n, m, i, j, k, temp, flag, q, A, b, p, x; cin >> n >> q; for (i = 0; i < n - 1; i++) { cin >> A >> b; v[A].pb(b); v[b].pb(A); } while (q--) { cin >> p >> x; a[p] += x; } vis[1] = true; dfs(1, 0); for (i = 1; i <= n; i++) { cout << ans[i] << ' '; } return 0; }
replace
27
30
27
30
0
p02936
C++
Runtime Error
#include <algorithm> #include <cmath> #include <functional> #include <iostream> #include <map> #include <vector> #define reps(i, s, n) for (int(i) = (s); (i) < (n); (i)++) #define rep(i, n) reps(i, 0, n) using namespace std; using ll = long long; vector<int> Graph[20010]; int Score[20010]; bool flag[20010]; void dfs(int s) { if (flag[s]) return; flag[s] = true; for (int v : Graph[s]) { if (flag[v]) continue; Score[v] += Score[s]; dfs(v); } } int main() { int n, q; cin >> n >> q; int a, b; rep(i, n - 1) { cin >> a >> b; a--; b--; Graph[a].push_back(b); Graph[b].push_back(a); Score[i] = 0; } Score[n - 1] = 0; int p, x; rep(i, q) { cin >> p >> x; p--; Score[p] += x; } dfs(0); rep(i, n) { cout << Score[i] << endl; } return 0; }
#include <algorithm> #include <cmath> #include <functional> #include <iostream> #include <map> #include <vector> #define reps(i, s, n) for (int(i) = (s); (i) < (n); (i)++) #define rep(i, n) reps(i, 0, n) using namespace std; using ll = long long; vector<int> Graph[200010]; int Score[200010]; bool flag[200010]; void dfs(int s) { if (flag[s]) return; flag[s] = true; for (int v : Graph[s]) { if (flag[v]) continue; Score[v] += Score[s]; dfs(v); } } int main() { int n, q; cin >> n >> q; int a, b; rep(i, n - 1) { cin >> a >> b; a--; b--; Graph[a].push_back(b); Graph[b].push_back(a); Score[i] = 0; } Score[n - 1] = 0; int p, x; rep(i, q) { cin >> p >> x; p--; Score[p] += x; } dfs(0); rep(i, n) { cout << Score[i] << endl; } return 0; }
replace
12
15
12
15
0
p02936
C++
Runtime Error
#include <bits/stdc++.h> #define __sel(_1, _2, x, ...) x #define repn(i, n) for (ll i = 0; i < (n); i++) #define repr(i, a, b) for (ll i = (a); i < (b); i++) #define rep(i, ...) __sel(__VA_ARGS__, repr, repn)(i, __VA_ARGS__) #define rrepn(i, n) for (ll i = (n - 1); i >= 0; i--) #define rrepr(i, a, b) for (ll i = (b - 1); i >= (a); i--) #define rrep(i, ...) __sel(__VA_ARGS__, rrepr, rrepn)(i, __VA_ARGS__) #define umax(m, x) (m = max(m, x)) #define umin(m, x) (m = min(m, x)) #define rng(v) v.begin(), v.end() #define rrng(v) v.rbegin(), v.rend() #define bit(x, n) ((x) >> (n)&1) #define pb push_back #define mp make_pair #define mt make_tuple #define fi first #define se second #define shv(v) \ for (auto __t1 : v) \ cerr << __t1 << " "; \ cerr << endl; #define db(x) cerr << #x << ": " << (x) << endl; #define dbv(v) \ cerr << #v << ": "; \ shv(v); #define dbm(m) \ cerr << #m << ":" << endl; \ for (auto __t2 : m) { \ shv(__t2); \ } using namespace std; using ll = long long int; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; using vi = vector<int>; using vl = vector<ll>; using vd = vector<double>; using vld = vector<ld>; using vb = vector<bool>; using vc = vector<char>; using vs = vector<string>; using vpii = vector<pii>; using vpll = vector<pll>; using vvi = vector<vector<int>>; using vvl = vector<vector<ll>>; using vvd = vector<vector<double>>; using vvld = vector<vector<ld>>; using vvb = vector<vector<bool>>; using vvc = vector<vector<char>>; using vvs = vector<vector<string>>; const int INF = 1e9; const ll LINF = 1e18; const ll MOD = 1e9 + 7; inline int gi() { int x; cin >> x; return x; } inline ll gl() { int x; cin >> x; return x; } inline char gc() { char x; cin >> x; return x; } inline string gs() { string x; cin >> x; return x; } inline vi gvi(int n) { vi v(n); rep(i, n) cin >> v[i]; return v; } inline vl gvl(int n) { vl v(n); rep(i, n) cin >> v[i]; return v; } inline vc gvc(int n) { vc v(n); rep(i, n) cin >> v[i]; return v; } inline vs gvs(int n) { vs v(n); rep(i, n) cin >> v[i]; return v; } inline vvi gvvi(int h, int w) { vvi m(h, vi(w)); rep(i, h) rep(j, w) cin >> m[i][j]; return m; } inline vvl gvvl(int h, int w) { vvl m(h, vl(w)); rep(i, h) rep(j, w) cin >> m[i][j]; return m; } inline vvc gvvc(int h, int w) { vvc m(h, vc(w)); rep(i, h) rep(j, w) cin >> m[i][j]; return m; } inline vvs gvvs(int h, int w) { vvs m(h, vs(w)); rep(i, h) rep(j, w) cin >> m[i][j]; return m; } inline void yn(bool f) { cout << (f ? "Yes" : "No") << endl; } void dfs(vvi &g, vi &cnt, int p, vi &ans) { for (auto t : g[p]) { if (ans[t] != -1) continue; ans[t] = ans[p] + cnt[t]; dfs(g, cnt, t, ans); } } int main() { int n = gi(), q = gi(); vi cnt(n, 0); vvi g(n); rep(i, 1, n) { int a = gi(), b = gi(); g[a - 1].push_back(b - 1); g[b - 1].push_back(a - 1); } rep(i, q) { int p = gi(), x = gi(); cnt[p - 1] += x; } vi ans(n, -1); dfs(g, cnt, 0, ans); for (auto t : ans) cout << t << " "; cout << endl; return 0; }
#include <bits/stdc++.h> #define __sel(_1, _2, x, ...) x #define repn(i, n) for (ll i = 0; i < (n); i++) #define repr(i, a, b) for (ll i = (a); i < (b); i++) #define rep(i, ...) __sel(__VA_ARGS__, repr, repn)(i, __VA_ARGS__) #define rrepn(i, n) for (ll i = (n - 1); i >= 0; i--) #define rrepr(i, a, b) for (ll i = (b - 1); i >= (a); i--) #define rrep(i, ...) __sel(__VA_ARGS__, rrepr, rrepn)(i, __VA_ARGS__) #define umax(m, x) (m = max(m, x)) #define umin(m, x) (m = min(m, x)) #define rng(v) v.begin(), v.end() #define rrng(v) v.rbegin(), v.rend() #define bit(x, n) ((x) >> (n)&1) #define pb push_back #define mp make_pair #define mt make_tuple #define fi first #define se second #define shv(v) \ for (auto __t1 : v) \ cerr << __t1 << " "; \ cerr << endl; #define db(x) cerr << #x << ": " << (x) << endl; #define dbv(v) \ cerr << #v << ": "; \ shv(v); #define dbm(m) \ cerr << #m << ":" << endl; \ for (auto __t2 : m) { \ shv(__t2); \ } using namespace std; using ll = long long int; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; using vi = vector<int>; using vl = vector<ll>; using vd = vector<double>; using vld = vector<ld>; using vb = vector<bool>; using vc = vector<char>; using vs = vector<string>; using vpii = vector<pii>; using vpll = vector<pll>; using vvi = vector<vector<int>>; using vvl = vector<vector<ll>>; using vvd = vector<vector<double>>; using vvld = vector<vector<ld>>; using vvb = vector<vector<bool>>; using vvc = vector<vector<char>>; using vvs = vector<vector<string>>; const int INF = 1e9; const ll LINF = 1e18; const ll MOD = 1e9 + 7; inline int gi() { int x; cin >> x; return x; } inline ll gl() { int x; cin >> x; return x; } inline char gc() { char x; cin >> x; return x; } inline string gs() { string x; cin >> x; return x; } inline vi gvi(int n) { vi v(n); rep(i, n) cin >> v[i]; return v; } inline vl gvl(int n) { vl v(n); rep(i, n) cin >> v[i]; return v; } inline vc gvc(int n) { vc v(n); rep(i, n) cin >> v[i]; return v; } inline vs gvs(int n) { vs v(n); rep(i, n) cin >> v[i]; return v; } inline vvi gvvi(int h, int w) { vvi m(h, vi(w)); rep(i, h) rep(j, w) cin >> m[i][j]; return m; } inline vvl gvvl(int h, int w) { vvl m(h, vl(w)); rep(i, h) rep(j, w) cin >> m[i][j]; return m; } inline vvc gvvc(int h, int w) { vvc m(h, vc(w)); rep(i, h) rep(j, w) cin >> m[i][j]; return m; } inline vvs gvvs(int h, int w) { vvs m(h, vs(w)); rep(i, h) rep(j, w) cin >> m[i][j]; return m; } inline void yn(bool f) { cout << (f ? "Yes" : "No") << endl; } void dfs(vvi &g, vi &cnt, int p, vi &ans) { for (auto t : g[p]) { if (ans[t] != -1) continue; ans[t] = ans[p] + cnt[t]; dfs(g, cnt, t, ans); } } int main() { int n = gi(), q = gi(); vi cnt(n, 0); vvi g(n); rep(i, 1, n) { int a = gi(), b = gi(); g[a - 1].push_back(b - 1); g[b - 1].push_back(a - 1); } rep(i, q) { int p = gi(), x = gi(); cnt[p - 1] += x; } vi ans(n, -1); ans[0] = cnt[0]; dfs(g, cnt, 0, ans); for (auto t : ans) cout << t << " "; cout << endl; return 0; }
insert
155
155
155
156
0
p02936
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define MOD 1000000007 #define INF 1LL << 30 #define rep(i, n) for (int i = 0; i < (n); ++i) #define all(x) (x).begin(), (x).end() int s[100010]; vector<vector<int>> G; void dfs(int v, int p, int x) { s[v] += x; for (auto u : G[v]) { if (u == p) continue; dfs(u, v, s[v]); } } int main() { int n, q; cin >> n >> q; G.resize(n); rep(i, n - 1) { int a, b; cin >> a >> b; a--, b--; G[a].push_back(b); G[b].push_back(a); } rep(i, q) { int p, x; cin >> p >> x; p--; s[p] += x; } dfs(0, -1, 0); rep(i, n) cout << s[i] << " "; cout << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define MOD 1000000007 #define INF 1LL << 30 #define rep(i, n) for (int i = 0; i < (n); ++i) #define all(x) (x).begin(), (x).end() int s[200010]; vector<vector<int>> G; void dfs(int v, int p, int x) { s[v] += x; for (auto u : G[v]) { if (u == p) continue; dfs(u, v, s[v]); } } int main() { int n, q; cin >> n >> q; G.resize(n); rep(i, n - 1) { int a, b; cin >> a >> b; a--, b--; G[a].push_back(b); G[b].push_back(a); } rep(i, q) { int p, x; cin >> p >> x; p--; s[p] += x; } dfs(0, -1, 0); rep(i, n) cout << s[i] << " "; cout << endl; }
replace
8
9
8
9
0
p02936
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define REPR(i, n) for (int i = (int)(n); i > 0; i--) #define FOR(i, m, n) for (int i = (int)(m); i < (int)(n); i++) #define INF 2e9 #define ALL(v) (v).begin(), (v).end() template <typename Iter> std::string join(Iter begin, Iter end, std::string const &separator) { std::ostringstream result; if (begin != end) { result << *begin++; }; while (begin != end) { result << separator << *begin++; }; return result.str(); } using namespace std; typedef long long ll; void dfs(vector<vector<ll>> edges, vector<ll> actions, vector<ll> &values, ll current_node, ll parent_value, ll parent_node) { values[current_node] = parent_value + actions[current_node]; for (auto e : edges[current_node]) { if (e == parent_node) { continue; } dfs(edges, actions, values, e, values[current_node], current_node); } } void solve() { ll N, Q; cin >> N >> Q; vector<vector<ll>> edges(N, vector<ll>()); REP(i, N - 1) { ll a, b; cin >> a >> b; edges[a - 1].push_back(b - 1); edges[b - 1].push_back(a - 1); } vector<ll> actions(N); REP(i, Q) { ll p, x; cin >> p >> x; actions[p - 1] += x; } vector<ll> values(N); dfs(edges, actions, values, 0, 0, -1); cout << join(ALL(values), " ") << endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); solve(); return 0; }
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define REPR(i, n) for (int i = (int)(n); i > 0; i--) #define FOR(i, m, n) for (int i = (int)(m); i < (int)(n); i++) #define INF 2e9 #define ALL(v) (v).begin(), (v).end() template <typename Iter> std::string join(Iter begin, Iter end, std::string const &separator) { std::ostringstream result; if (begin != end) { result << *begin++; }; while (begin != end) { result << separator << *begin++; }; return result.str(); } using namespace std; typedef long long ll; void dfs(vector<vector<ll>> &edges, vector<ll> &actions, vector<ll> &values, ll current_node, ll parent_value, ll parent_node) { values[current_node] = parent_value + actions[current_node]; for (auto e : edges[current_node]) { if (e == parent_node) { continue; } dfs(edges, actions, values, e, values[current_node], current_node); } } void solve() { ll N, Q; cin >> N >> Q; vector<vector<ll>> edges(N, vector<ll>()); REP(i, N - 1) { ll a, b; cin >> a >> b; edges[a - 1].push_back(b - 1); edges[b - 1].push_back(a - 1); } vector<ll> actions(N); REP(i, Q) { ll p, x; cin >> p >> x; actions[p - 1] += x; } vector<ll> values(N); dfs(edges, actions, values, 0, 0, -1); cout << join(ALL(values), " ") << endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); solve(); return 0; }
replace
19
20
19
20
TLE
p02936
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using pii = pair<int, int>; using ll = long long; #define fi first #define se second #define pb push_back #define rep(i, n) for (int i = 0; i < (int)(n); i++) template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } // int vvi[200010][200010]; // int n; bool visited[200010]; void dfs(vector<vector<int>> &vvi, vector<ll> &vi, ll p, int i) { // cout << "p: " << p << " i: " << i << endl; vi[i] += p; visited[i] = true; /*for(auto k : vvi[i]) { cout << k << " "; } cout << endl;*/ for (auto j : vvi[i]) { if (!visited[j]) dfs(vvi, vi, vi[i], j); } } int main() { int n, q; cin >> n >> q; vector<vector<int>> vvi(n, vector<int>(n)); rep(i, n) rep(j, n) vvi[i][j] = 0; int a, b; rep(i, n - 1) { cin >> a >> b; a--; b--; vvi[a].pb(b); vvi[b].pb(a); } rep(i, n) visited[i] = false; vector<ll> vi(n, 0); int p, x; rep(i, q) { cin >> p >> x; vi[p - 1] += x; } // cout << "debug" << endl; rep(i, n) { // cout << vi[i] << endl; } // cout << "debug" << endl; dfs(vvi, vi, 0, 0); rep(i, n) { cout << vi[i]; if (i == n - 1) cout << endl; else cout << " "; } return 0; }
#include <bits/stdc++.h> using namespace std; using pii = pair<int, int>; using ll = long long; #define fi first #define se second #define pb push_back #define rep(i, n) for (int i = 0; i < (int)(n); i++) template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } // int vvi[200010][200010]; // int n; bool visited[200010]; void dfs(vector<vector<int>> &vvi, vector<ll> &vi, ll p, int i) { // cout << "p: " << p << " i: " << i << endl; vi[i] += p; visited[i] = true; /*for(auto k : vvi[i]) { cout << k << " "; } cout << endl;*/ for (auto j : vvi[i]) { if (!visited[j]) dfs(vvi, vi, vi[i], j); } } int main() { int n, q; cin >> n >> q; vector<vector<int>> vvi(n, vector<int>()); int a, b; rep(i, n - 1) { cin >> a >> b; a--; b--; vvi[a].pb(b); vvi[b].pb(a); } rep(i, n) visited[i] = false; vector<ll> vi(n, 0); int p, x; rep(i, q) { cin >> p >> x; vi[p - 1] += x; } // cout << "debug" << endl; rep(i, n) { // cout << vi[i] << endl; } // cout << "debug" << endl; dfs(vvi, vi, 0, 0); rep(i, n) { cout << vi[i]; if (i == n - 1) cout << endl; else cout << " "; } return 0; }
replace
46
48
46
47
0
p02936
C++
Runtime Error
/*input */ #include <bits/stdc++.h> using namespace std; #define all(a) a.begin(), a.end() #define ff first #define ss second #define pb push_back using ll = long long; using pii = pair<int, int>; template <typename T> using V = vector<T>; const int N = 1e5 + 10; ll add[N], ans[N]; vector<int> adj[N]; void dfs(int cur, int par) { ans[cur] += add[cur]; for (auto nxt : adj[cur]) { if (nxt == par) continue; add[nxt] += add[cur]; dfs(nxt, cur); } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, q; cin >> n >> q; for (int i = 1; i < n; ++i) { int a, b; cin >> a >> b; adj[a].pb(b); adj[b].pb(a); } while (q--) { int x, p; cin >> p >> x; add[p] += (ll)x; } dfs(1, 0); for (int i = 1; i <= n; ++i) { cout << ans[i] << " \n"[i == n]; } }
/*input */ #include <bits/stdc++.h> using namespace std; #define all(a) a.begin(), a.end() #define ff first #define ss second #define pb push_back using ll = long long; using pii = pair<int, int>; template <typename T> using V = vector<T>; const int N = 2e5 + 10; ll add[N], ans[N]; vector<int> adj[N]; void dfs(int cur, int par) { ans[cur] += add[cur]; for (auto nxt : adj[cur]) { if (nxt == par) continue; add[nxt] += add[cur]; dfs(nxt, cur); } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, q; cin >> n >> q; for (int i = 1; i < n; ++i) { int a, b; cin >> a >> b; adj[a].pb(b); adj[b].pb(a); } while (q--) { int x, p; cin >> p >> x; add[p] += (ll)x; } dfs(1, 0); for (int i = 1; i <= n; ++i) { cout << ans[i] << " \n"[i == n]; } }
replace
15
16
15
16
0
p02936
C++
Runtime Error
#include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <utility> #include <vector> using namespace std; using ll = long long; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(x) (x).begin(), (x).end() const int INF = (1 << 30); const ll INFL = (1LL << 62); const ll MOD = 1000000007; template <class T> bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } template <class T> ll mod_pow(ll a, ll n, T mod) { mod = (ll)mod; ll res = 1, p = a % mod; while (n) { if (n & 1) res = res * p % mod; p = p * p % mod; n >>= 1; } return res; } void print() { std::cout << std::endl; } template <typename T, typename... A> void print(const T &first, const A &...rest) { cout << sizeof...(rest) << endl; std::cout << first; if (sizeof...(rest)) std::cout << " "; print(rest...); } template <typename... A> void print(const A &...rest) { print(rest...); } template <typename A> void print(const std::vector<A> &v) { std::for_each(v.begin(), v.end(), [](A x) { std::cout << x << " "; }); std::cout << std::endl; } vector<vector<int>> to(200005); vector<int> ans; void dfs(int v, int p = -1) { for (int u : to[v]) { if (u == p) continue; ans[u] += ans[v]; dfs(u, v); } } int main() { cin.tie(0); ios::sync_with_stdio(false); int n, q; cin >> n >> q; rep(i, n - 1) { int a, b; scanf("%d %d", &a, &b); --a; --b; to[a].push_back(b); to[b].push_back(a); } ans.resize(n); rep(i, q) { int p, x; scanf("%d %d", &p, &x); --p; ans[p] += x; } dfs(0); rep(i, n) { printf("%d\n", ans[i]); } return 0; }
#include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <utility> #include <vector> using namespace std; using ll = long long; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(x) (x).begin(), (x).end() const int INF = (1 << 30); const ll INFL = (1LL << 62); const ll MOD = 1000000007; template <class T> bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } template <class T> ll mod_pow(ll a, ll n, T mod) { mod = (ll)mod; ll res = 1, p = a % mod; while (n) { if (n & 1) res = res * p % mod; p = p * p % mod; n >>= 1; } return res; } void print() { std::cout << std::endl; } template <typename T, typename... A> void print(const T &first, const A &...rest) { cout << sizeof...(rest) << endl; std::cout << first; if (sizeof...(rest)) std::cout << " "; print(rest...); } template <typename... A> void print(const A &...rest) { print(rest...); } template <typename A> void print(const std::vector<A> &v) { std::for_each(v.begin(), v.end(), [](A x) { std::cout << x << " "; }); std::cout << std::endl; } vector<vector<int>> to(200005); vector<int> ans; void dfs(int v, int p = -1) { for (int u : to[v]) { if (u == p) continue; ans[u] += ans[v]; dfs(u, v); } } int main() { cin.tie(0); ios::sync_with_stdio(false); int n, q; scanf("%d %d", &n, &q); rep(i, n - 1) { int a, b; scanf("%d %d", &a, &b); --a; --b; to[a].push_back(b); to[b].push_back(a); } ans.resize(n); rep(i, q) { int p, x; scanf("%d %d", &p, &x); --p; ans[p] += x; } dfs(0); rep(i, n) { printf("%d\n", ans[i]); } return 0; }
replace
86
87
86
87
-11
p02936
C++
Runtime Error
#include <iostream> #include <vector> using namespace std; typedef pair<int, int> ii; struct node { int counter = 0; vector<int> neighbors; bool visited = false; }; void dfs(vector<node> &nodes, int subroot) { auto parent = nodes[subroot]; for (int neighbor : nodes[subroot].neighbors) { auto n = nodes[neighbor]; if (!n.visited) { n.visited = true; n.counter += parent.counter; dfs(nodes, neighbor); } } } int main() { // input int n, q; cin >> n >> q; vector<node> nodes(n + 1); // n-1 edges >> adj_list for (int i = 0; i < n - 1; ++i) { int node1, node2; cin >> node1 >> node2; nodes[node1].neighbors.push_back(node2); nodes[node2].neighbors.push_back(node1); } // q operations for (int i = 0; i < q; ++i) { int node, counter; cin >> node >> counter; nodes[node].counter += counter; } // dfs and add every subroot's counter to its children nodes[1].visited = true; dfs(nodes, 1); // output for (int i = 1; i < n + 1; ++i) { cout << nodes[i].counter << " "; } }
#include <iostream> #include <vector> using namespace std; typedef pair<int, int> ii; struct node { int counter = 0; vector<int> neighbors; bool visited = false; }; void dfs(vector<node> &nodes, int subroot) { auto parent = nodes[subroot]; for (int neighbor : parent.neighbors) { auto &n = nodes[neighbor]; if (!n.visited) { n.visited = true; n.counter += parent.counter; dfs(nodes, neighbor); } } } int main() { // input int n, q; cin >> n >> q; vector<node> nodes(n + 1); // n-1 edges >> adj_list for (int i = 0; i < n - 1; ++i) { int node1, node2; cin >> node1 >> node2; nodes[node1].neighbors.push_back(node2); nodes[node2].neighbors.push_back(node1); } // q operations for (int i = 0; i < q; ++i) { int node, counter; cin >> node >> counter; nodes[node].counter += counter; } // dfs and add every subroot's counter to its children nodes[1].visited = true; dfs(nodes, 1); // output for (int i = 1; i < n + 1; ++i) { cout << nodes[i].counter << " "; } }
replace
14
16
14
16
-11
p02936
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int n, q, cnt, h[200005], nex[200005], to[200005 << 1], ans[200005]; void addedge(int x, int y) { nex[++cnt] = h[x]; to[cnt] = y; h[x] = cnt; } void dfs(int x, int f) { ans[x] += ans[f]; for (int i = h[x]; i; i = nex[i]) if (to[i] != f) dfs(to[i], x); } int main() { cin >> n >> q; for (int i = 1, x, y; i < n && cin >> x >> y; i++) { addedge(x, y); addedge(y, x); } while (q--) { int x, v; cin >> x >> v; ans[x] += v; } dfs(1, 0); for (int i = 1; i <= n; i++) cout << ans[i] << ' '; cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int n, q, cnt, h[200005], nex[200005 << 1], to[200005 << 1], ans[200005]; void addedge(int x, int y) { nex[++cnt] = h[x]; to[cnt] = y; h[x] = cnt; } void dfs(int x, int f) { ans[x] += ans[f]; for (int i = h[x]; i; i = nex[i]) if (to[i] != f) dfs(to[i], x); } int main() { cin >> n >> q; for (int i = 1, x, y; i < n && cin >> x >> y; i++) { addedge(x, y); addedge(y, x); } while (q--) { int x, v; cin >> x >> v; ans[x] += v; } dfs(1, 0); for (int i = 1; i <= n; i++) cout << ans[i] << ' '; cout << endl; return 0; }
replace
3
4
3
4
0
p02936
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long int mod = 1e9 + 7; int INF = 2e18; struct leaves { int up; vector<int> down; int point = 0; }; int N, Q; int ans[100100]; leaves tree[100100]; void dfs(int direct, int point) { tree[direct].point += point; for (int i = 0; i < tree[direct].down.size(); i++) { dfs(tree[direct].down[i], tree[direct].point); } } signed main() { cin >> N >> Q; for (int i = 0; i < N - 1; i++) { int a, b; cin >> a >> b; tree[a].down.push_back(b); tree[b].up = a; } for (int i = 0; i < Q; i++) { int a, b; cin >> a >> b; tree[a].point += b; } dfs(1, 0); for (int i = 1; i <= N; i++) { if (i != N) cout << tree[i].point << " "; else cout << tree[i].point << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long int mod = 1e9 + 7; int INF = 2e18; struct leaves { int up; vector<int> down; int point = 0; }; int N, Q; int ans[250000]; leaves tree[250000]; void dfs(int direct, int point) { tree[direct].point += point; for (int i = 0; i < tree[direct].down.size(); i++) { dfs(tree[direct].down[i], tree[direct].point); } } signed main() { cin >> N >> Q; for (int i = 0; i < N - 1; i++) { int a, b; cin >> a >> b; tree[a].down.push_back(b); tree[b].up = a; } for (int i = 0; i < Q; i++) { int a, b; cin >> a >> b; tree[a].point += b; } dfs(1, 0); for (int i = 1; i <= N; i++) { if (i != N) cout << tree[i].point << " "; else cout << tree[i].point << endl; } return 0; }
replace
13
15
13
15
0
p02936
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<ll, ll> P; #define fi first #define se second #define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++) #define rep(i, n) repl(i, 0, n) #define all(x) (x).begin(), (x).end() #define dbg(x) cout << #x "=" << x << endl #define mmax(x, y) (x > y ? x : y) #define mmin(x, y) (x < y ? x : y) #define maxch(x, y) x = mmax(x, y) #define minch(x, y) x = mmin(x, y) #define uni(x) x.erase(unique(all(x)), x.end()) #define exist(x, y) (find(all(x), y) != x.end()) #define bcnt __builtin_popcountll #define INF 1e16 #define mod 1000000007 ll n, q; ll a[100010], b[100010]; vector<ll> g[100010]; void dfs(ll v, ll pre, ll sum) { sum += a[v]; b[v] += sum; for (ll nv : g[v]) { if (nv == pre) continue; dfs(nv, v, sum); } } int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> n >> q; rep(i, n - 1) { ll u, v; cin >> u >> v; u--; v--; g[u].push_back(v); g[v].push_back(u); } while (q--) { ll v, x; cin >> v >> x; v--; a[v] += x; } dfs(0, -1, 0); rep(i, n) cout << b[i] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<ll, ll> P; #define fi first #define se second #define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++) #define rep(i, n) repl(i, 0, n) #define all(x) (x).begin(), (x).end() #define dbg(x) cout << #x "=" << x << endl #define mmax(x, y) (x > y ? x : y) #define mmin(x, y) (x < y ? x : y) #define maxch(x, y) x = mmax(x, y) #define minch(x, y) x = mmin(x, y) #define uni(x) x.erase(unique(all(x)), x.end()) #define exist(x, y) (find(all(x), y) != x.end()) #define bcnt __builtin_popcountll #define INF 1e16 #define mod 1000000007 ll n, q; ll a[200010], b[200010]; vector<ll> g[200010]; void dfs(ll v, ll pre, ll sum) { sum += a[v]; b[v] += sum; for (ll nv : g[v]) { if (nv == pre) continue; dfs(nv, v, sum); } } int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> n >> q; rep(i, n - 1) { ll u, v; cin >> u >> v; u--; v--; g[u].push_back(v); g[v].push_back(u); } while (q--) { ll v, x; cin >> v >> x; v--; a[v] += x; } dfs(0, -1, 0); rep(i, n) cout << b[i] << endl; return 0; }
replace
25
27
25
27
0
p02936
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int N = 1e5; vector<int> arr[N]; vector<int> val(N, 0); vector<int> ans(N); bool seen[N]; void sol(int v, int value) { ans[v] = val[v] + value; seen[v] = true; for (int i = 0; i < arr[v].size(); i++) { if (!seen[arr[v][i]]) sol(arr[v][i], value + val[v]); } } int main() { // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); int n, q; cin >> n >> q; memset(seen, false, sizeof(seen)); for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; arr[u - 1].push_back(v - 1); arr[v - 1].push_back(u - 1); } while (q--) { int p, x; cin >> p >> x; val[p - 1] += x; } sol(0, 0); for (int i = 0; i < n; i++) cout << ans[i] << " "; }
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 7; vector<int> arr[N]; vector<int> val(N, 0); vector<int> ans(N); bool seen[N]; void sol(int v, int value) { ans[v] = val[v] + value; seen[v] = true; for (int i = 0; i < arr[v].size(); i++) { if (!seen[arr[v][i]]) sol(arr[v][i], value + val[v]); } } int main() { // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); int n, q; cin >> n >> q; memset(seen, false, sizeof(seen)); for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; arr[u - 1].push_back(v - 1); arr[v - 1].push_back(u - 1); } while (q--) { int p, x; cin >> p >> x; val[p - 1] += x; } sol(0, 0); for (int i = 0; i < n; i++) cout << ans[i] << " "; }
replace
3
4
3
4
0
p02936
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define vi vector<int> #define vll vector<ll> #define pll pair<ll, ll> #define pii pair<int, int> #define pb push_back #define mp make_pair #define SIZE 100005 vll tree[SIZE]; ll arr[SIZE]; int main() { // #ifndef ONLINE_JUDGE // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // #endif ll N, Q; cin >> N >> Q; for (ll i = 1; i < N; ++i) { ll u, v; cin >> u >> v; tree[u].pb(v); } while (Q--) { ll node, value; cin >> node >> value; arr[node] += value; } for (ll i = 1; i <= N; ++i) { vll ::iterator itr; for (itr = tree[i].begin(); itr != tree[i].end(); ++itr) { arr[*itr] += arr[i]; } } for (int i = 1; i <= N; ++i) { cout << arr[i] << endl; } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define vi vector<int> #define vll vector<ll> #define pll pair<ll, ll> #define pii pair<int, int> #define pb push_back #define mp make_pair #define SIZE 200005 vll tree[SIZE]; ll arr[SIZE]; int main() { // #ifndef ONLINE_JUDGE // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // #endif ll N, Q; cin >> N >> Q; for (ll i = 1; i < N; ++i) { ll u, v; cin >> u >> v; tree[u].pb(v); } while (Q--) { ll node, value; cin >> node >> value; arr[node] += value; } for (ll i = 1; i <= N; ++i) { vll ::iterator itr; for (itr = tree[i].begin(); itr != tree[i].end(); ++itr) { arr[*itr] += arr[i]; } } for (int i = 1; i <= N; ++i) { cout << arr[i] << endl; } }
replace
9
10
9
10
0
p02936
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; vector<long long> adj[20005]; vector<long long> res; void dfs(long long v, long long p = -1) { for (auto itr : adj[v]) { if (itr == p) continue; res[itr] += res[v]; // dfs(itr, v); } } int main() { long long n, q; cin >> n >> q; long long a, b; for (int i = 0; i < n - 1; i++) { cin >> a >> b; a--; b--; adj[a].push_back(b); adj[b].push_back(a); } long long p, x; res.resize(n); for (int i = 0; i < q; i++) { cin >> p >> x; p--; res[p] += x; } dfs(0); // Yo ! for (int i = 0; i < n; i++) { cout << res[i] << " "; } }
#include <bits/stdc++.h> using namespace std; vector<long long> adj[200005]; vector<long long> res; void dfs(long long v, long long p = -1) { for (auto itr : adj[v]) { if (itr == p) continue; res[itr] += res[v]; // dfs(itr, v); } } int main() { long long n, q; cin >> n >> q; long long a, b; for (int i = 0; i < n - 1; i++) { cin >> a >> b; a--; b--; adj[a].push_back(b); adj[b].push_back(a); } long long p, x; res.resize(n); for (int i = 0; i < q; i++) { cin >> p >> x; p--; res[p] += x; } dfs(0); // Yo ! for (int i = 0; i < n; i++) { cout << res[i] << " "; } }
replace
3
4
3
4
0
p02936
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define P pair<ll, ll> #define FOR(I, A, B) for (ll I = ll(A); I < ll(B); ++I) #define FORR(I, A, B) for (ll I = ll((B)-1); I >= ll(A); --I) #define TO(x, t, f) ((x) ? (t) : (f)) #define SORT(x) (sort(x.begin(), x.end())) // 0 2 2 3 4 5 8 9 #define POSL(x, v) \ (lower_bound(x.begin(), x.end(), v) - x.begin()) // xi>=v x is sorted #define POSU(x, v) \ (upper_bound(x.begin(), x.end(), v) - x.begin()) // xi>v x is sorted #define NUM(x, v) (POSU(x, v) - POSL(x, v)) // x is sorted #define REV(x) (reverse(x.begin(), x.end())) // reverse ll gcd(ll a, ll b) { if (a % b == 0) return b; return gcd(b, a % b); } ll lcm(ll a, ll b) { ll c = gcd(a, b); return ((a / c) * (b / c) * c); } #define NEXTP(x) next_permutation(x.begin(), x.end()) const ll INF = ll(1e18) + ll(7); const ll MOD = 1000000007LL; #define out(a) cout << fixed << setprecision((a)) vector<ll> tree[100001]; ll ans[100001]; bool did[100001]; int main() { ll N, Q; cin >> N >> Q; FOR(i, 0, N - 1) { ll a, b; cin >> a >> b; a--; b--; tree[a].push_back(b); tree[b].push_back(a); } FOR(i, 0, Q) { ll p, x; cin >> p >> x; ans[p - 1] += x; } did[0] = true; queue<ll> q; q.push(0); while (q.size()) { ll pa = q.front(); q.pop(); for (auto to : tree[pa]) { if (not did[to]) { did[to] = true; ans[to] += ans[pa]; q.push(to); } } } cout << ans[0]; FOR(i, 1, N) cout << " " << ans[i]; cout << "\n"; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define P pair<ll, ll> #define FOR(I, A, B) for (ll I = ll(A); I < ll(B); ++I) #define FORR(I, A, B) for (ll I = ll((B)-1); I >= ll(A); --I) #define TO(x, t, f) ((x) ? (t) : (f)) #define SORT(x) (sort(x.begin(), x.end())) // 0 2 2 3 4 5 8 9 #define POSL(x, v) \ (lower_bound(x.begin(), x.end(), v) - x.begin()) // xi>=v x is sorted #define POSU(x, v) \ (upper_bound(x.begin(), x.end(), v) - x.begin()) // xi>v x is sorted #define NUM(x, v) (POSU(x, v) - POSL(x, v)) // x is sorted #define REV(x) (reverse(x.begin(), x.end())) // reverse ll gcd(ll a, ll b) { if (a % b == 0) return b; return gcd(b, a % b); } ll lcm(ll a, ll b) { ll c = gcd(a, b); return ((a / c) * (b / c) * c); } #define NEXTP(x) next_permutation(x.begin(), x.end()) const ll INF = ll(1e18) + ll(7); const ll MOD = 1000000007LL; #define out(a) cout << fixed << setprecision((a)) vector<ll> tree[200001]; ll ans[200001]; bool did[200001]; int main() { ll N, Q; cin >> N >> Q; FOR(i, 0, N - 1) { ll a, b; cin >> a >> b; a--; b--; tree[a].push_back(b); tree[b].push_back(a); } FOR(i, 0, Q) { ll p, x; cin >> p >> x; ans[p - 1] += x; } did[0] = true; queue<ll> q; q.push(0); while (q.size()) { ll pa = q.front(); q.pop(); for (auto to : tree[pa]) { if (not did[to]) { did[to] = true; ans[to] += ans[pa]; q.push(to); } } } cout << ans[0]; FOR(i, 1, N) cout << " " << ans[i]; cout << "\n"; }
replace
28
31
28
31
0
p02936
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define llong long long #define INF (__INT32_MAX__ / 2) #define EPS 10e-8 #define MAX_N 100000 using namespace std; typedef pair<int, int> ipair; typedef pair<llong, llong> llpair; llong modulo(llong left, llong right) { return (left % right + right) % right; } int N, Q; llpair nodes[MAX_N]; //(diff, cnter) vector<int> adj_list[MAX_N]; bool isvisited[MAX_N]; void dfs(int node) { isvisited[node] = true; int cnter = nodes[node].second; for (int i = 0; i < adj_list[node].size(); ++i) { int next = adj_list[node][i]; if (isvisited[next]) continue; nodes[next].second = cnter + nodes[next].first; dfs(next); } } int main(int argc, char **argv) { cin >> N >> Q; for (int i = 0; i < N - 1; ++i) { int a, b; cin >> a >> b; adj_list[a - 1].push_back(b - 1); adj_list[b - 1].push_back(a - 1); } // init for (int i = 0; i < N; ++i) nodes[i].first = nodes[i].second = 0; for (int i = 0; i < Q; ++i) { int p, x; cin >> p >> x; nodes[p - 1].first += x; } // DFS nodes[0].second = nodes[0].first; dfs(0); for (int i = 0; i < N; ++i) { if (i != 0) cout << " "; cout << nodes[i].second; } cout << endl; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define llong long long #define INF (__INT32_MAX__ / 2) #define EPS 10e-8 #define MAX_N 200000 using namespace std; typedef pair<int, int> ipair; typedef pair<llong, llong> llpair; llong modulo(llong left, llong right) { return (left % right + right) % right; } int N, Q; llpair nodes[MAX_N]; //(diff, cnter) vector<int> adj_list[MAX_N]; bool isvisited[MAX_N]; void dfs(int node) { isvisited[node] = true; int cnter = nodes[node].second; for (int i = 0; i < adj_list[node].size(); ++i) { int next = adj_list[node][i]; if (isvisited[next]) continue; nodes[next].second = cnter + nodes[next].first; dfs(next); } } int main(int argc, char **argv) { cin >> N >> Q; for (int i = 0; i < N - 1; ++i) { int a, b; cin >> a >> b; adj_list[a - 1].push_back(b - 1); adj_list[b - 1].push_back(a - 1); } // init for (int i = 0; i < N; ++i) nodes[i].first = nodes[i].second = 0; for (int i = 0; i < Q; ++i) { int p, x; cin >> p >> x; nodes[p - 1].first += x; } // DFS nodes[0].second = nodes[0].first; dfs(0); for (int i = 0; i < N; ++i) { if (i != 0) cout << " "; cout << nodes[i].second; } cout << endl; }
replace
15
16
15
16
0
p02936
C++
Time Limit Exceeded
// https://atcoder.jp/contests/abc138/tasks/abc138 #include <algorithm> #include <bitset> #include <cstdio> #include <cstdlib> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; using ll = long long; using P = pair<ll, ll>; int main() { int N, Q; cin >> N >> Q; vector<int> a(N), b(N); for (int i = 0; i < N - 1; ++i) cin >> a[i] >> b[i], --a[i], --b[i]; vector<int> p(Q), x(Q); for (int i = 0; i < Q; ++i) cin >> p[i] >> x[i], --p[i]; vector<vector<int>> G(N); for (int i = 0; i < N - 1; ++i) { G[a[i]].push_back(b[i]); G[b[i]].push_back(a[i]); } vector<int> parent(N, -1); // 頂点 i の親 queue<int> que; que.push(0); while (!que.empty()) { int v = que.front(); que.pop(); for (const auto &nv : G[v]) { if (parent[v] == nv) continue; parent[nv] = v; que.push(nv); } } vector<int> res(N, 0); for (int j = 0; j < Q; ++j) { queue<int> vque; // cout << "p[j] = " << p[j] << ", " << "x[j] = " << x[j] << endl; vque.push(p[j]); while (!vque.empty()) { int v = vque.front(); vque.pop(); res[v] += x[j]; for (const auto &nv : G[v]) { // cout << "nv = " << nv << endl; if (parent[v] == nv) continue; vque.push(nv); } } } for (int i = 0; i < N; ++i) { cout << res[i]; if (i != N - 1) cout << " "; else cout << endl; } return 0; }
// https://atcoder.jp/contests/abc138/tasks/abc138 #include <algorithm> #include <bitset> #include <cstdio> #include <cstdlib> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; using ll = long long; using P = pair<ll, ll>; int main() { int N, Q; cin >> N >> Q; vector<int> a(N), b(N); for (int i = 0; i < N - 1; ++i) cin >> a[i] >> b[i], --a[i], --b[i]; vector<int> p(Q), x(Q); for (int i = 0; i < Q; ++i) cin >> p[i] >> x[i], --p[i]; vector<vector<int>> G(N); for (int i = 0; i < N - 1; ++i) { G[a[i]].push_back(b[i]); G[b[i]].push_back(a[i]); } vector<int> parent(N, -1); // 頂点 i の親 queue<int> que; que.push(0); while (!que.empty()) { int v = que.front(); que.pop(); for (const auto &nv : G[v]) { if (parent[v] == nv) continue; parent[nv] = v; que.push(nv); } } vector<int> res(N, 0); for (int j = 0; j < Q; ++j) { res[p[j]] += x[j]; } que.push(0); while (!que.empty()) { int v = que.front(); que.pop(); for (const auto &nv : G[v]) { if (parent[v] == nv) continue; res[nv] += res[v]; que.push(nv); } } for (int i = 0; i < N; ++i) { cout << res[i]; if (i != N - 1) cout << " "; else cout << endl; } return 0; }
replace
48
61
48
60
TLE
p02936
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) using namespace std; using Graph = vector<vector<int>>; typedef long long ll; typedef pair<int, int> P; const int inf = 1 << 21; const ll INF = 1LL << 60; const ll mod = 1e9 + 7; const int dx[4] = {-1, 0, 1, 0}; const int dy[4] = {0, -1, 0, 1}; // abc138d int n, q; vector<int> adj[200002]; ll cnt[200002]; void dfs(int s, int p) { for (int i = 0; i < adj[s].size(); i++) { if (adj[s][i] == p) continue; cnt[adj[s][i]] += cnt[s]; dfs(adj[s][i], cnt[adj[s][i]]); } } int main() { cin >> n >> q; rep(i, n - 1) { int a, b; cin >> a >> b; a--; b--; adj[a].push_back(b); adj[b].push_back(a); } rep(i, n) cnt[i] = 0; rep(i, q) { int p, x; cin >> p >> x; p--; cnt[p] += x; } dfs(0, -1); rep(i, n) cout << cnt[i] << ' '; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) using namespace std; using Graph = vector<vector<int>>; typedef long long ll; typedef pair<int, int> P; const int inf = 1 << 21; const ll INF = 1LL << 60; const ll mod = 1e9 + 7; const int dx[4] = {-1, 0, 1, 0}; const int dy[4] = {0, -1, 0, 1}; // abc138d int n, q; vector<int> adj[200002]; ll cnt[200002]; void dfs(int s, int p) { for (int i = 0; i < adj[s].size(); i++) { if (adj[s][i] == p) continue; cnt[adj[s][i]] += cnt[s]; dfs(adj[s][i], s); } } int main() { cin >> n >> q; rep(i, n - 1) { int a, b; cin >> a >> b; a--; b--; adj[a].push_back(b); adj[b].push_back(a); } rep(i, n) cnt[i] = 0; rep(i, q) { int p, x; cin >> p >> x; p--; cnt[p] += x; } dfs(0, -1); rep(i, n) cout << cnt[i] << ' '; return 0; }
replace
23
24
23
24
-11
p02936
C++
Runtime Error
#include <bits/stdc++.h> #define fst(t) std::get<0>(t) #define snd(t) std::get<1>(t) #define thd(t) std::get<2>(t) #define unless(p) if (!(p)) #define until(p) while (!(p)) using ll = std::int64_t; using P = std::tuple<int, int>; int N, Q; std::vector<int> G[200100]; ll cnt[200100]; int l[200100], r[200100]; int p, x; void dfs(int v, int p, int &c) { l[v] = c; c += 1; for (int w : G[v]) { if (w == p) { continue; } dfs(w, v, c); } r[v] = c; c += 1; } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); std::cin >> N >> Q; for (int i = 0; i < N - 1; ++i) { int A, B; std::cin >> A >> B; G[A].emplace_back(B); G[B].emplace_back(A); } int c = 0; dfs(1, -1, c); for (int i = 0; i < Q; ++i) { std::cin >> p >> x; cnt[l[p]] += x; cnt[r[p]] -= x; } for (int i = 1; i < c; ++i) { cnt[i] += cnt[i - 1]; } for (int i = 1; i <= N; ++i) { std::cout << cnt[l[i]] << " \n"[i == N]; } }
#include <bits/stdc++.h> #define fst(t) std::get<0>(t) #define snd(t) std::get<1>(t) #define thd(t) std::get<2>(t) #define unless(p) if (!(p)) #define until(p) while (!(p)) using ll = std::int64_t; using P = std::tuple<int, int>; int N, Q; std::vector<int> G[200100]; ll cnt[400100]; int l[200100], r[200100]; int p, x; void dfs(int v, int p, int &c) { l[v] = c; c += 1; for (int w : G[v]) { if (w == p) { continue; } dfs(w, v, c); } r[v] = c; c += 1; } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); std::cin >> N >> Q; for (int i = 0; i < N - 1; ++i) { int A, B; std::cin >> A >> B; G[A].emplace_back(B); G[B].emplace_back(A); } int c = 0; dfs(1, -1, c); for (int i = 0; i < Q; ++i) { std::cin >> p >> x; cnt[l[p]] += x; cnt[r[p]] -= x; } for (int i = 1; i < c; ++i) { cnt[i] += cnt[i - 1]; } for (int i = 1; i <= N; ++i) { std::cout << cnt[l[i]] << " \n"[i == N]; } }
replace
13
14
13
14
0
p02936
C++
Runtime Error
#include <bits/stdc++.h> #define REP(i, x) for (int i = 0; i < (int)(x); i++) #define REPS(i, x) for (int i = 1; i <= (int)(x); i++) #define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--) #define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--) #define FOR(i, m, n) for (int i = m; i < n; i++) #define ALL(v) v.begin(), v.end() #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()); #define INF 1e9 using namespace std; typedef long long ll; vector<ll> ans(100000); vector<queue<int>> ds(100000); vector<ll> vs(100000, 0); void dfs(int idx, int prv_val) { int cur_val; cur_val = prv_val + vs.at(idx); ans.at(idx) = cur_val; while (!ds.at(idx).empty()) { int nxt = ds.at(idx).front(); dfs(nxt, cur_val); ds.at(idx).pop(); } } signed main() { // std::ifstream in ("input.txt"); std::cin.rdbuf(in.rdbuf()); int n, q; cin >> n >> q; int frm, to; REP(i, n - 1) { cin >> frm >> to; ds.at(frm - 1).push(to - 1); } int p, x; REP(i, q) { cin >> p >> x; vs.at(p - 1) += x; } dfs(0, 0); cout << ans.at(0); REP(i, n - 1) { cout << " " << ans.at(i + 1); } cout << endl; }
#include <bits/stdc++.h> #define REP(i, x) for (int i = 0; i < (int)(x); i++) #define REPS(i, x) for (int i = 1; i <= (int)(x); i++) #define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--) #define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--) #define FOR(i, m, n) for (int i = m; i < n; i++) #define ALL(v) v.begin(), v.end() #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()); #define INF 1e9 using namespace std; typedef long long ll; vector<ll> ans(1000000); vector<queue<int>> ds(1000000); vector<ll> vs(1000000, 0); void dfs(int idx, int prv_val) { int cur_val; cur_val = prv_val + vs.at(idx); ans.at(idx) = cur_val; while (!ds.at(idx).empty()) { int nxt = ds.at(idx).front(); dfs(nxt, cur_val); ds.at(idx).pop(); } } signed main() { // std::ifstream in ("input.txt"); std::cin.rdbuf(in.rdbuf()); int n, q; cin >> n >> q; int frm, to; REP(i, n - 1) { cin >> frm >> to; ds.at(frm - 1).push(to - 1); } int p, x; REP(i, q) { cin >> p >> x; vs.at(p - 1) += x; } dfs(0, 0); cout << ans.at(0); REP(i, n - 1) { cout << " " << ans.at(i + 1); } cout << endl; }
replace
12
15
12
15
0
p02936
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define pb push_back #define N 200000 vector<int> v[N]; int n, q, tot, d[N], l[N], r[N], s[N]; void dfs(int x, int fa) { d[x] = ++tot, l[x] = r[x] = tot; for (int i = 0; i < v[x].size(); i++) { if (v[x][i] == fa) continue; dfs(v[x][i], x); l[x] = min(l[x], l[v[x][i]]); r[x] = max(r[x], r[v[x][i]]); } } int main() { // freopen("sample.in", "r", stdin); cin >> n >> q; for (int i = 1; i < n; i++) { int x, y; scanf("%d%d", &x, &y); v[x].pb(y), v[y].pb(x); } dfs(1, 0); for (int i = 1; i <= q; i++) { int p, x; scanf("%d%d", &p, &x); s[l[p]] += x, s[r[p] + 1] -= x; } for (int i = 1; i <= n; i++) s[i] = s[i] + s[i - 1]; for (int i = 1; i <= n; i++) printf("%d ", s[d[i]]); }
#include <bits/stdc++.h> using namespace std; #define pb push_back #define N 300000 vector<int> v[N]; int n, q, tot, d[N], l[N], r[N], s[N]; void dfs(int x, int fa) { d[x] = ++tot, l[x] = r[x] = tot; for (int i = 0; i < v[x].size(); i++) { if (v[x][i] == fa) continue; dfs(v[x][i], x); l[x] = min(l[x], l[v[x][i]]); r[x] = max(r[x], r[v[x][i]]); } } int main() { // freopen("sample.in", "r", stdin); cin >> n >> q; for (int i = 1; i < n; i++) { int x, y; scanf("%d%d", &x, &y); v[x].pb(y), v[y].pb(x); } dfs(1, 0); for (int i = 1; i <= q; i++) { int p, x; scanf("%d%d", &p, &x); s[l[p]] += x, s[r[p] + 1] -= x; } for (int i = 1; i <= n; i++) s[i] = s[i] + s[i - 1]; for (int i = 1; i <= n; i++) printf("%d ", s[d[i]]); }
replace
3
4
3
4
0
p02936
C++
Runtime Error
#include <algorithm> #include <functional> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define N (1000000000 + 7) #define M (998244353) #define INF 1e16 typedef long long ll; typedef pair<ll, ll> P; ll sum[200010] = {0}; bool visited[200010]; vector<ll> g[100010]; int main(void) { ll n, q; cin >> n >> q; for (ll i = 0; i < n - 1; i++) { ll a, b; cin >> a >> b; g[a].push_back(b); g[b].push_back(a); } for (ll i = 0; i < q; i++) { ll p, x; cin >> p >> x; sum[p] += x; } queue<ll> que; que.push(1); visited[1] = true; while (!que.empty()) { ll p = que.front(); que.pop(); for (ll i = 0; i < g[p].size(); i++) { ll next = g[p][i]; if (visited[next]) continue; sum[next] += sum[p]; que.push(next); visited[next] = true; } } for (ll i = 1; i <= n; i++) { if (i == n) cout << sum[i] << endl; else cout << sum[i] << " "; } return 0; }
#include <algorithm> #include <functional> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define N (1000000000 + 7) #define M (998244353) #define INF 1e16 typedef long long ll; typedef pair<ll, ll> P; ll sum[200010] = {0}; bool visited[200010]; vector<ll> g[200010]; int main(void) { ll n, q; cin >> n >> q; for (ll i = 0; i < n - 1; i++) { ll a, b; cin >> a >> b; g[a].push_back(b); g[b].push_back(a); } for (ll i = 0; i < q; i++) { ll p, x; cin >> p >> x; sum[p] += x; } queue<ll> que; que.push(1); visited[1] = true; while (!que.empty()) { ll p = que.front(); que.pop(); for (ll i = 0; i < g[p].size(); i++) { ll next = g[p][i]; if (visited[next]) continue; sum[next] += sum[p]; que.push(next); visited[next] = true; } } for (ll i = 1; i <= n; i++) { if (i == n) cout << sum[i] << endl; else cout << sum[i] << " "; } return 0; }
replace
20
21
20
21
0
p02936
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <vector> using namespace std; typedef long long ll; int main() { int n, q; cin >> n >> q; vector<int> parent(n); // [0]->error for (int i = 1; i < n; i++) { int a, b; cin >> a >> b; parent[b - 1] = a - 1; } vector<ll> cost(n, 0); for (int i = 0; i < q; i++) { int p, x; cin >> p >> x; cost[p - 1] += x; } for (int i = 0; i < n; i++) { ll sum = cost[0]; for (int node = i; node != 0; node = parent[node]) { sum += cost[node]; } cout << sum << ' '; } cout << endl; return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; typedef long long ll; int main() { int n, q; cin >> n >> q; vector<int> parent(n); // [0]->error for (int i = 1; i < n; i++) { int a, b; cin >> a >> b; parent[b - 1] = a - 1; } vector<ll> cost(n, 0); for (int i = 0; i < q; i++) { int p, x; cin >> p >> x; cost[p - 1] += x; } cout << cost[0] << ' '; for (int i = 1; i < n; i++) { cost[i] += cost[parent[i]]; cout << cost[i] << ' '; } cout << endl; return 0; }
replace
23
29
23
27
TLE
p02936
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using Point = pair<int, int>; // int point[5001][5001] = {0}; vector<bool> used(100, false); void bfs(vector<vector<int>> &data, vector<long long> &ans, int t) { used[t] = true; // ans[t].first = time; for (int i = 0; i < data[t].size(); i++) { if (!used[data[t][i]]) { ans[data[t][i]] += ans[t]; bfs(data, ans, data[t][i]); } } // ans[t].second = time; } int main() { int N, Q; cin >> N >> Q; string t; getline(cin, t); vector<vector<int>> data(N); vector<long long> ans(N, 0); for (int i = 0; i < N - 1; i++) { int a, b; cin >> a >> b; a--; b--; data[a].push_back(b); data[b].push_back(a); } for (int i = 0; i < Q; i++) { int a; long long b; cin >> a >> b; a--; ans[a] += b; } // used[0] = true; bfs(data, ans, 0); for (int i = 0; i < N; i++) { cout << ans[i] << endl; } // cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using Point = pair<int, int>; // int point[5001][5001] = {0}; vector<bool> used(200000, false); void bfs(vector<vector<int>> &data, vector<long long> &ans, int t) { used[t] = true; // ans[t].first = time; for (int i = 0; i < data[t].size(); i++) { if (!used[data[t][i]]) { ans[data[t][i]] += ans[t]; bfs(data, ans, data[t][i]); } } // ans[t].second = time; } int main() { int N, Q; cin >> N >> Q; string t; getline(cin, t); vector<vector<int>> data(N); vector<long long> ans(N, 0); for (int i = 0; i < N - 1; i++) { int a, b; cin >> a >> b; a--; b--; data[a].push_back(b); data[b].push_back(a); } for (int i = 0; i < Q; i++) { int a; long long b; cin >> a >> b; a--; ans[a] += b; } // used[0] = true; bfs(data, ans, 0); for (int i = 0; i < N; i++) { cout << ans[i] << endl; } // cout << ans << endl; return 0; }
replace
6
7
6
7
0
p02936
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int w, h; void dfs(vector<vector<int>> g, int v, vector<bool> &seen, vector<long long> &add_x, vector<long long> &count, long long add) { seen[v] = true; add += add_x[v]; count[v] += add; for (auto i : g[v]) { if (seen[i]) { continue; } dfs(g, i, seen, add_x, count, add); } add -= add_x[v]; } int main() { int N, Q; cin >> N >> Q; vector<vector<int>> g(N + 1); vector<bool> seen(N + 1, false); for (int i = 0; i < N - 1; i++) { int a, b; cin >> a >> b; g[a].push_back(b); g[b].push_back(a); } vector<long long> add_x(N + 1, 0); for (int i = 0; i < Q; i++) { int p, x; cin >> p >> x; add_x[p] += x; } vector<long long> count(N + 1, 0); long long add = 0; dfs(g, 1, seen, add_x, count, add); for (int i = 1; i <= N; i++) { cout << count[i] << " "; } }
#include <bits/stdc++.h> using namespace std; int w, h; void dfs(vector<vector<int>> &g, int v, vector<bool> &seen, vector<long long> &add_x, vector<long long> &count, long long add) { seen[v] = true; add += add_x[v]; count[v] += add; for (auto i : g[v]) { if (seen[i]) { continue; } dfs(g, i, seen, add_x, count, add); } add -= add_x[v]; } int main() { int N, Q; cin >> N >> Q; vector<vector<int>> g(N + 1); vector<bool> seen(N + 1, false); for (int i = 0; i < N - 1; i++) { int a, b; cin >> a >> b; g[a].push_back(b); g[b].push_back(a); } vector<long long> add_x(N + 1, 0); for (int i = 0; i < Q; i++) { int p, x; cin >> p >> x; add_x[p] += x; } vector<long long> count(N + 1, 0); long long add = 0; dfs(g, 1, seen, add_x, count, add); for (int i = 1; i <= N; i++) { cout << count[i] << " "; } }
replace
5
6
5
6
TLE
p02937
C++
Runtime Error
#include <algorithm> #include <deque> #include <iomanip> #include <iostream> #include <locale> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <string.h> #include <vector> using namespace std; #define FORN(i, m, n) for (int i = m; i < (n); i++) #define PRINTVEC(v) \ FORN(i, 0, v.size()) cout << v[i] << " "; \ cout << endl #define PRINTMAT(m) \ FORN(j, 0, m.size()) { PRINTVEC(m[j]); } #define p_b(x) push_back(x) #define m_p(a, b) make_pair(a, b) typedef long long ll; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string s, t; cin >> s >> t; vector<vector<int>> v(27); FORN(i, 0, s.size()) v[s[i] - 'a'].p_b(i); ll ans = 0, cur = -1; FORN(i, 0, t.size()) { int x = t[i] - 'a', pos; if (v[x].empty()) { cout << -1 << endl; return 0; } pos = upper_bound(v[x].begin(), v[x].end(), cur) - v[x].begin(); if (pos == v[x].size()) { pos = v[x][0]; ans++; } cur = v[x][pos]; } ans = ans * s.size() + cur + 1; cout << ans << endl; }
#include <algorithm> #include <deque> #include <iomanip> #include <iostream> #include <locale> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <string.h> #include <vector> using namespace std; #define FORN(i, m, n) for (int i = m; i < (n); i++) #define PRINTVEC(v) \ FORN(i, 0, v.size()) cout << v[i] << " "; \ cout << endl #define PRINTMAT(m) \ FORN(j, 0, m.size()) { PRINTVEC(m[j]); } #define p_b(x) push_back(x) #define m_p(a, b) make_pair(a, b) typedef long long ll; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string s, t; cin >> s >> t; vector<vector<int>> v(27); FORN(i, 0, s.size()) v[s[i] - 'a'].p_b(i); ll ans = 0, cur = -1; FORN(i, 0, t.size()) { int x = t[i] - 'a', pos; if (v[x].empty()) { cout << -1 << endl; return 0; } pos = upper_bound(v[x].begin(), v[x].end(), cur) - v[x].begin(); if (pos == v[x].size()) { pos = 0; ans++; } cur = v[x][pos]; } ans = ans * s.size() + cur + 1; cout << ans << endl; }
replace
40
41
40
41
0
p02937
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define INF 1001000100010001000 #define MOD 1000000007 #define EPS 1e-10 #define int long long #define rep(i, N) for (int i = 0; i < N; i++) #define Rep(i, N) for (int i = 1; i < N; i++) #define For(i, a, b) for (int i = (a); i < (b); i++) #define pb push_back #define eb emplace_back #define mp make_pair #define pii pair<int, int> #define vi vector<int> #define vvi vector<vi> #define vb vector<bool> #define vvb vector<vb> #define vp vector<pii> #define all(a) (a).begin(), (a).end() #define Int(x) \ int x; \ cin >> x; #define int2(x, y) \ Int(x); \ Int(y); #define int3(x, y, z) \ Int(x); \ int2(y, z); #define in(x, a, b) ((a) <= (x) && (x) < (b)) #define fir first #define sec second #define ffir first.first #define fsec first.second #define sfir second.first #define ssec second.second #define Decimal fixed << setprecision(10) // int dxy[5] = {0, 1, 0, -1, 0}; // cmd signed main() { std::ios::sync_with_stdio(false); std::cin.tie(0); string input; cin >> input; string target; cin >> target; vvi table(input.size(), vi(30, -1)); { vi tmp(30, -1); for (int i = input.size() - 1; i >= 0; i--) { tmp[input[i] - 'a'] = i; rep(j, table[i].size()) { if (table[i][j] == -1) table[i][j] = tmp[j]; } } for (int i = input.size() - 1; i >= 0; i--) { tmp[input[i] - 'a'] = i; rep(j, table[i].size()) { if (table[i][j] == -1) table[i][j] = tmp[j]; } } } int ans = 0, cnt = 0; rep(i, target.size()) { int tmp = table[ans][target[i] - 'a']; if (tmp == -1) { std::cout << -1 << std::endl; return 0; } else { if (ans > tmp) { cnt++; } ans = tmp + 1; } } cout << cnt * input.size() + ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define INF 1001000100010001000 #define MOD 1000000007 #define EPS 1e-10 #define int long long #define rep(i, N) for (int i = 0; i < N; i++) #define Rep(i, N) for (int i = 1; i < N; i++) #define For(i, a, b) for (int i = (a); i < (b); i++) #define pb push_back #define eb emplace_back #define mp make_pair #define pii pair<int, int> #define vi vector<int> #define vvi vector<vi> #define vb vector<bool> #define vvb vector<vb> #define vp vector<pii> #define all(a) (a).begin(), (a).end() #define Int(x) \ int x; \ cin >> x; #define int2(x, y) \ Int(x); \ Int(y); #define int3(x, y, z) \ Int(x); \ int2(y, z); #define in(x, a, b) ((a) <= (x) && (x) < (b)) #define fir first #define sec second #define ffir first.first #define fsec first.second #define sfir second.first #define ssec second.second #define Decimal fixed << setprecision(10) // int dxy[5] = {0, 1, 0, -1, 0}; // cmd signed main() { std::ios::sync_with_stdio(false); std::cin.tie(0); string input; cin >> input; string target; cin >> target; vvi table(input.size(), vi(30, -1)); { vi tmp(30, -1); for (int i = input.size() - 1; i >= 0; i--) { tmp[input[i] - 'a'] = i; rep(j, table[i].size()) { if (table[i][j] == -1) table[i][j] = tmp[j]; } } for (int i = input.size() - 1; i >= 0; i--) { tmp[input[i] - 'a'] = i; rep(j, table[i].size()) { if (table[i][j] == -1) table[i][j] = tmp[j]; } } } int ans = 0, cnt = 0; rep(i, target.size()) { int tmp = table[ans][target[i] - 'a']; if (tmp == -1) { std::cout << -1 << std::endl; return 0; } else { if (ans > tmp) { cnt++; } ans = tmp + 1; if (ans == input.size()) { ans = 0; cnt++; } } } cout << cnt * input.size() + ans << endl; return 0; }
insert
82
82
82
86
0
p02937
C++
Runtime Error
/* ID:dyx_6661 TASK: LANG: */ #include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <deque> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <vector> using namespace std; typedef long long int ll; typedef double db; typedef string str; typedef pair<int, int> pa; typedef vector<int> vec; const int n_size = 1e5 + 5; ll f[n_size][30]; char a[2 * n_size], b[n_size]; ll ans = 0, cnt = 1; int main() { // freopen(".in","r",stdin); // freopen(".out","w",stdout); scanf("%s", a + 1); scanf("%s", b + 1); ll n = strlen(a + 1), m = strlen(b + 1); for (ll i = 1; i <= n; i++) a[i + n] = a[i]; for (ll i = 2 * n; i >= 1; i--) { for (int j = 1; j <= 26; j++) { if (a[i] == j + 'a' - 1) { f[i][j] = i; } else { f[i][j] = f[i + 1][j]; } } } for (ll i = 1; i <= m; i++) { if (!f[1][b[i] - 'a' + 1]) { printf("-1"); return 0; } ans += f[cnt][b[i] - 'a' + 1] - cnt + 1; if (f[cnt][b[i] - 'a' + 1] > n) cnt = f[cnt][b[i] - 'a' + 1] - n; cnt = f[cnt][b[i] - 'a' + 1] + 1; } printf("%lld", ans); return 0; }
/* ID:dyx_6661 TASK: LANG: */ #include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <deque> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <vector> using namespace std; typedef long long int ll; typedef double db; typedef string str; typedef pair<int, int> pa; typedef vector<int> vec; const int n_size = 1e5 + 5; ll f[2 * n_size][30]; char a[2 * n_size], b[n_size]; ll ans = 0, cnt = 1; int main() { // freopen(".in","r",stdin); // freopen(".out","w",stdout); scanf("%s", a + 1); scanf("%s", b + 1); ll n = strlen(a + 1), m = strlen(b + 1); for (ll i = 1; i <= n; i++) a[i + n] = a[i]; for (ll i = 2 * n; i >= 1; i--) { for (int j = 1; j <= 26; j++) { if (a[i] == j + 'a' - 1) { f[i][j] = i; } else { f[i][j] = f[i + 1][j]; } } } for (ll i = 1; i <= m; i++) { if (!f[1][b[i] - 'a' + 1]) { printf("-1"); return 0; } ans += f[cnt][b[i] - 'a' + 1] - cnt + 1; if (f[cnt][b[i] - 'a' + 1] > n) cnt = f[cnt][b[i] - 'a' + 1] - n; cnt = f[cnt][b[i] - 'a' + 1] + 1; } printf("%lld", ans); return 0; }
replace
25
26
25
26
0
p02937
C++
Runtime Error
#define _CRT_SECURE_NO_WARNINGS #include <algorithm> #include <array> #include <bitset> #include <cassert> #include <chrono> #include <ciso646> #include <climits> #include <cmath> #include <cstdio> #include <cstring> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <type_traits> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--) #define REP(i, n) for (int i = 0; i < (n); i++) #define RREP(i, n) for (int i = (n)-1; i >= 0; i--) #define inf 0x3f3f3f3f3f3f3f3f #define ALL(a) (a).begin(), (a).end() #define DEBUG(x) // cerr<<#x<<": "<<x<<endl #define ll long long #define ull unsigned long long using pii = pair<ll, ll>; #define eps 1e-14 #define SETUP \ cin.tie(0), ios::sync_with_stdio(false), \ cout << setprecision(15) << std::fixed; #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) template <class T> using vec2 = std::vector<vector<T>>; //// bit //// #ifdef _MSC_VER #ifdef _WIN32 inline unsigned int __builtin_ctz(unsigned int x) { unsigned long r; _BitScanForward(&r, x); return r; } inline unsigned int __builtin_clz(unsigned int x) { unsigned long r; _BitScanReverse(&r, x); return 31 - r; } inline unsigned int __builtin_ffs(unsigned int x) { unsigned long r; return _BitScanForward(&r, x) ? r + 1 : 0; } // inline unsigned int __builtin_popcount(unsigned int x){ return __popcnt(x); } #ifdef _WIN64 inline unsigned long long __builtin_ctzll(unsigned long long x) { unsigned long r; _BitScanForward64(&r, x); return r; } inline unsigned long long __builtin_clzll(unsigned long long x) { unsigned long r; _BitScanReverse64(&r, x); return 63 - r; } inline unsigned long long __builtin_ffsll(unsigned long long x) { unsigned long r; return _BitScanForward64(&r, x) ? r + 1 : 0; } inline unsigned long long __builtin_popcountll(unsigned long long x) { return __popcnt64(x); } #else inline unsigned int hidword(unsigned long long x) { return static_cast<unsigned int>(x >> 32); } inline unsigned int lodword(unsigned long long x) { return static_cast<unsigned int>(x & 0xFFFFFFFF); } inline unsigned long long __builtin_ctzll(unsigned long long x) { return lodword(x) ? __builtin_ctz(lodword(x)) : __builtin_ctz(hidword(x)) + 32; } inline unsigned long long __builtin_clzll(unsigned long long x) { return hidword(x) ? __builtin_clz(hidword(x)) : __builtin_clz(lodword(x)) + 32; } inline unsigned long long __builtin_ffsll(unsigned long long x) { return lodword(x) ? __builtin_ffs(lodword(x)) : hidword(x) ? __builtin_ffs(hidword(x)) + 32 : 0; } // inline unsigned long long __builtin_popcountll(unsigned long long x) { return // __builtin_popcount(lodword(x)) + __builtin_popcount(hidword(x)); } #endif // _WIN64 #endif // _WIN32 #endif // _MSC_VER namespace { struct input_returnner { ll N; input_returnner(ll N_ = 0) : N(N_) {} template <typename T> operator vector<T>() const { vector<T> res(N); for (auto &a : res) cin >> a; return std::move(res); } template <typename T> operator T() const { T res; cin >> res; return res; } template <typename T> T operator-(T right) { return T(input_returnner()) - right; } template <typename T> T operator+(T right) { return T(input_returnner()) + right; } template <typename T> T operator*(T right) { return T(input_returnner()) * right; } template <typename T> T operator/(T right) { return T(input_returnner()) / right; } template <typename T> T operator<<(T right) { return T(input_returnner()) << right; } template <typename T> T operator>>(T right) { return T(input_returnner()) >> right; } }; template <typename T> input_returnner in() { return in<T>(); } input_returnner in() { return input_returnner(); } input_returnner in(ll N) { return std::move(input_returnner(N)); } } // namespace template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (T &x : vec) is >> x; return is; } template <typename T> struct is_vector : std::false_type {}; template <typename T> struct is_vector<std::vector<T>> : std::true_type {}; template <typename T> constexpr bool is_vector_v = is_vector<T>::value; template <typename T> std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) { if (!v.empty()) { for (int i = 0; i < v.size(); ++i) { out << v[i] << (i == v.size() - 1 ? "\n" : (is_vector_v<T> ? "" : ", ")); } } return out; } namespace std { // ref: // https://stackoverflow.com/questions/7110301/generic-hash-for-tuples-in-unordered-map-unordered-set template <class T> inline void hash_combine(std::size_t &seed, T const &v) { seed ^= std::hash<T>()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2); } // Recursive template code derived from Matthieu M. template <class Tuple, size_t Index = std::tuple_size<Tuple>::value - 1> struct HashValueImpl { static void apply(size_t &seed, Tuple const &tuple) { HashValueImpl<Tuple, Index - 1>::apply(seed, tuple); hash_combine(seed, std::get<Index>(tuple)); } }; template <class Tuple> struct HashValueImpl<Tuple, 0> { static void apply(size_t &seed, Tuple const &tuple) { hash_combine(seed, std::get<0>(tuple)); } }; template <typename... TT> struct hash<std::tuple<TT...>> { size_t operator()(std::tuple<TT...> const &tt) const { size_t seed = 0; HashValueImpl<std::tuple<TT...>>::apply(seed, tt); return seed; } }; template <class T, class U> class hash<std::pair<T, U>> { public: size_t operator()(const std::pair<T, U> &x) const { return hash<std::tuple<T, U>>()(std::tie(x.first, x.second)); } }; } // namespace std // ref: https://stackoverflow.com/questions/6245735/pretty-print-stdtuple namespace aux { template <std::size_t...> struct seq {}; template <std::size_t N, std::size_t... Is> struct gen_seq : gen_seq<N - 1, N - 1, Is...> {}; template <std::size_t... Is> struct gen_seq<0, Is...> : seq<Is...> {}; template <class Ch, class Tr, class Tuple, std::size_t... Is> void print_tuple(std::basic_ostream<Ch, Tr> &os, Tuple const &t, seq<Is...>) { using swallow = int[]; (void)swallow{0, (void(os << (Is == 0 ? "" : ", ") << std::get<Is>(t)), 0)...}; } } // namespace aux template <class Ch, class Tr, class... Args> auto operator<<(std::basic_ostream<Ch, Tr> &os, std::tuple<Args...> const &t) -> std::basic_ostream<Ch, Tr> & { os << "("; aux::print_tuple(os, t, aux::gen_seq<sizeof...(Args)>()); return os << ")"; } template <class S, class T> std::ostream &operator<<(std::ostream &os, const std::pair<S, T> &p) { return os << "(" << p.first << ", " << p.second << ")"; } // ref: // https://stackoverflow.com/questions/8542591/c11-reverse-range-based-for-loo�Fp template <typename T> struct reversion_wrapper { T &iterable; }; template <typename T> auto begin(reversion_wrapper<T> w) { return std::rbegin(w.iterable); } template <typename T> auto end(reversion_wrapper<T> w) { return std::rend(w.iterable); } template <typename T> reversion_wrapper<T> REV(T &&iterable) { return {iterable}; } template <class T> bool inside(T left, T val, T right) { return left <= val and val < right; } template <class T> T bitCount(T num) { T res = 0; while (num > 0) { if (num & 1) ++res; num >>= 1; } return res; } ll MOD = 1e9 + 7; // ll MOD = 998244353; void solve(); signed main() { SETUP; #ifdef _DEBUG while (true) { #endif solve(); #ifdef _DEBUG cout << "-------" << endl; } #endif #ifdef _DEBUG system("pause"); #endif return 0; } #define int ll void solve() { string S; cin >> S; string T; cin >> T; vector<vector<int>> pos('z'); REP(i, S.size()) { int c = S[i]; pos[c].push_back(i); } for (auto &t : T) { if (pos[t].empty()) { cout << -1 << endl; return; } } int index = 0; int res = 0; for (auto &t : T) { auto it = lower_bound(ALL(pos[t]), index); if (it == pos[t].end()) { index = pos[t][0] + 1; ++res; } else { index = *it + 1; } } cout << res * S.size() + index << endl; }
#define _CRT_SECURE_NO_WARNINGS #include <algorithm> #include <array> #include <bitset> #include <cassert> #include <chrono> #include <ciso646> #include <climits> #include <cmath> #include <cstdio> #include <cstring> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <type_traits> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--) #define REP(i, n) for (int i = 0; i < (n); i++) #define RREP(i, n) for (int i = (n)-1; i >= 0; i--) #define inf 0x3f3f3f3f3f3f3f3f #define ALL(a) (a).begin(), (a).end() #define DEBUG(x) // cerr<<#x<<": "<<x<<endl #define ll long long #define ull unsigned long long using pii = pair<ll, ll>; #define eps 1e-14 #define SETUP \ cin.tie(0), ios::sync_with_stdio(false), \ cout << setprecision(15) << std::fixed; #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) template <class T> using vec2 = std::vector<vector<T>>; //// bit //// #ifdef _MSC_VER #ifdef _WIN32 inline unsigned int __builtin_ctz(unsigned int x) { unsigned long r; _BitScanForward(&r, x); return r; } inline unsigned int __builtin_clz(unsigned int x) { unsigned long r; _BitScanReverse(&r, x); return 31 - r; } inline unsigned int __builtin_ffs(unsigned int x) { unsigned long r; return _BitScanForward(&r, x) ? r + 1 : 0; } // inline unsigned int __builtin_popcount(unsigned int x){ return __popcnt(x); } #ifdef _WIN64 inline unsigned long long __builtin_ctzll(unsigned long long x) { unsigned long r; _BitScanForward64(&r, x); return r; } inline unsigned long long __builtin_clzll(unsigned long long x) { unsigned long r; _BitScanReverse64(&r, x); return 63 - r; } inline unsigned long long __builtin_ffsll(unsigned long long x) { unsigned long r; return _BitScanForward64(&r, x) ? r + 1 : 0; } inline unsigned long long __builtin_popcountll(unsigned long long x) { return __popcnt64(x); } #else inline unsigned int hidword(unsigned long long x) { return static_cast<unsigned int>(x >> 32); } inline unsigned int lodword(unsigned long long x) { return static_cast<unsigned int>(x & 0xFFFFFFFF); } inline unsigned long long __builtin_ctzll(unsigned long long x) { return lodword(x) ? __builtin_ctz(lodword(x)) : __builtin_ctz(hidword(x)) + 32; } inline unsigned long long __builtin_clzll(unsigned long long x) { return hidword(x) ? __builtin_clz(hidword(x)) : __builtin_clz(lodword(x)) + 32; } inline unsigned long long __builtin_ffsll(unsigned long long x) { return lodword(x) ? __builtin_ffs(lodword(x)) : hidword(x) ? __builtin_ffs(hidword(x)) + 32 : 0; } // inline unsigned long long __builtin_popcountll(unsigned long long x) { return // __builtin_popcount(lodword(x)) + __builtin_popcount(hidword(x)); } #endif // _WIN64 #endif // _WIN32 #endif // _MSC_VER namespace { struct input_returnner { ll N; input_returnner(ll N_ = 0) : N(N_) {} template <typename T> operator vector<T>() const { vector<T> res(N); for (auto &a : res) cin >> a; return std::move(res); } template <typename T> operator T() const { T res; cin >> res; return res; } template <typename T> T operator-(T right) { return T(input_returnner()) - right; } template <typename T> T operator+(T right) { return T(input_returnner()) + right; } template <typename T> T operator*(T right) { return T(input_returnner()) * right; } template <typename T> T operator/(T right) { return T(input_returnner()) / right; } template <typename T> T operator<<(T right) { return T(input_returnner()) << right; } template <typename T> T operator>>(T right) { return T(input_returnner()) >> right; } }; template <typename T> input_returnner in() { return in<T>(); } input_returnner in() { return input_returnner(); } input_returnner in(ll N) { return std::move(input_returnner(N)); } } // namespace template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (T &x : vec) is >> x; return is; } template <typename T> struct is_vector : std::false_type {}; template <typename T> struct is_vector<std::vector<T>> : std::true_type {}; template <typename T> constexpr bool is_vector_v = is_vector<T>::value; template <typename T> std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) { if (!v.empty()) { for (int i = 0; i < v.size(); ++i) { out << v[i] << (i == v.size() - 1 ? "\n" : (is_vector_v<T> ? "" : ", ")); } } return out; } namespace std { // ref: // https://stackoverflow.com/questions/7110301/generic-hash-for-tuples-in-unordered-map-unordered-set template <class T> inline void hash_combine(std::size_t &seed, T const &v) { seed ^= std::hash<T>()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2); } // Recursive template code derived from Matthieu M. template <class Tuple, size_t Index = std::tuple_size<Tuple>::value - 1> struct HashValueImpl { static void apply(size_t &seed, Tuple const &tuple) { HashValueImpl<Tuple, Index - 1>::apply(seed, tuple); hash_combine(seed, std::get<Index>(tuple)); } }; template <class Tuple> struct HashValueImpl<Tuple, 0> { static void apply(size_t &seed, Tuple const &tuple) { hash_combine(seed, std::get<0>(tuple)); } }; template <typename... TT> struct hash<std::tuple<TT...>> { size_t operator()(std::tuple<TT...> const &tt) const { size_t seed = 0; HashValueImpl<std::tuple<TT...>>::apply(seed, tt); return seed; } }; template <class T, class U> class hash<std::pair<T, U>> { public: size_t operator()(const std::pair<T, U> &x) const { return hash<std::tuple<T, U>>()(std::tie(x.first, x.second)); } }; } // namespace std // ref: https://stackoverflow.com/questions/6245735/pretty-print-stdtuple namespace aux { template <std::size_t...> struct seq {}; template <std::size_t N, std::size_t... Is> struct gen_seq : gen_seq<N - 1, N - 1, Is...> {}; template <std::size_t... Is> struct gen_seq<0, Is...> : seq<Is...> {}; template <class Ch, class Tr, class Tuple, std::size_t... Is> void print_tuple(std::basic_ostream<Ch, Tr> &os, Tuple const &t, seq<Is...>) { using swallow = int[]; (void)swallow{0, (void(os << (Is == 0 ? "" : ", ") << std::get<Is>(t)), 0)...}; } } // namespace aux template <class Ch, class Tr, class... Args> auto operator<<(std::basic_ostream<Ch, Tr> &os, std::tuple<Args...> const &t) -> std::basic_ostream<Ch, Tr> & { os << "("; aux::print_tuple(os, t, aux::gen_seq<sizeof...(Args)>()); return os << ")"; } template <class S, class T> std::ostream &operator<<(std::ostream &os, const std::pair<S, T> &p) { return os << "(" << p.first << ", " << p.second << ")"; } // ref: // https://stackoverflow.com/questions/8542591/c11-reverse-range-based-for-loo�Fp template <typename T> struct reversion_wrapper { T &iterable; }; template <typename T> auto begin(reversion_wrapper<T> w) { return std::rbegin(w.iterable); } template <typename T> auto end(reversion_wrapper<T> w) { return std::rend(w.iterable); } template <typename T> reversion_wrapper<T> REV(T &&iterable) { return {iterable}; } template <class T> bool inside(T left, T val, T right) { return left <= val and val < right; } template <class T> T bitCount(T num) { T res = 0; while (num > 0) { if (num & 1) ++res; num >>= 1; } return res; } ll MOD = 1e9 + 7; // ll MOD = 998244353; void solve(); signed main() { SETUP; #ifdef _DEBUG while (true) { #endif solve(); #ifdef _DEBUG cout << "-------" << endl; } #endif #ifdef _DEBUG system("pause"); #endif return 0; } #define int ll void solve() { string S; cin >> S; string T; cin >> T; vector<vector<int>> pos('z' + 1); REP(i, S.size()) { int c = S[i]; pos[c].push_back(i); } for (auto &t : T) { if (pos[t].empty()) { cout << -1 << endl; return; } } int index = 0; int res = 0; for (auto &t : T) { auto it = lower_bound(ALL(pos[t]), index); if (it == pos[t].end()) { index = pos[t][0] + 1; ++res; } else { index = *it + 1; } } cout << res * S.size() + index << endl; }
replace
301
302
301
302
0
p02937
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; ++i) #define ll long long #define P pair<int, int> #define fast_io \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); const int MOD = 1000000007; const int INF = 2002002002; const ll LLINF = 9009009009009009009; using namespace std; int main() { fast_io const int N_ALPHA = 26; vector<vector<int>> alp(N_ALPHA); string s, t; cin >> s >> t; rep(i, s.size()) alp[s[i] - 'a'].push_back(i + 1); ll n = s.size(), ans = 0; ; int f_ind, c_ind = 0; for (char c : t) { f_ind = c_ind; if (alp[c - 'a'].empty()) { cout << -1 << endl; return 0; } c_ind = alp[c - 'a'][0]; for (int a : alp[c - 'a']) if (a > f_ind) { c_ind = a; break; } if (f_ind < c_ind) ans += (c_ind - f_ind); else ans += (n - f_ind) + c_ind; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; ++i) #define ll long long #define P pair<int, int> #define fast_io \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); const int MOD = 1000000007; const int INF = 2002002002; const ll LLINF = 9009009009009009009; using namespace std; int main() { fast_io const int N_ALPHA = 26; vector<vector<int>> alp(N_ALPHA); string s, t; cin >> s >> t; rep(i, s.size()) alp[s[i] - 'a'].push_back(i + 1); ll n = s.size(), ans = 0; ; int f_ind, c_ind = 0; for (char c : t) { f_ind = c_ind; if (alp[c - 'a'].empty()) { cout << -1 << endl; return 0; } if (alp[c - 'a'].end() - upper_bound(alp[c - 'a'].begin(), alp[c - 'a'].end(), f_ind) == 0) c_ind = alp[c - 'a'][0]; else c_ind = *upper_bound(alp[c - 'a'].begin(), alp[c - 'a'].end(), f_ind); if (f_ind < c_ind) ans += (c_ind - f_ind); else ans += (n - f_ind) + c_ind; } cout << ans << endl; return 0; }
replace
30
36
30
36
TLE
p02937
C++
Runtime Error
#include <bits/stdc++.h> const int N = 1e5 + 7; char s[N], t[N]; int lens, lent; using ll = long long; ll ans = 0, last = 0; std::vector<int> ref[30]; int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); std::cin >> (s + 1) >> (t + 1); lens = strlen(s + 1), lent = strlen(t + 1); for (int i = 1; i <= lens; i++) ref[s[i]].push_back(i); ans = lens; for (int i = 1; i <= lent; i++) { if (ref[t[i]].empty()) { std::cout << -1; exit(0); } auto pos = std::upper_bound(ref[t[i]].begin(), ref[t[i]].end(), last); if (pos == ref[t[i]].end()) { ans += lens; i--; last = 0; } else last = *pos; } std::cout << ans - (lens - last); exit(0); }
#include <bits/stdc++.h> const int N = 1e5 + 7; char s[N], t[N]; int lens, lent; using ll = long long; ll ans = 0, last = 0; std::vector<int> ref[200]; int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); std::cin >> (s + 1) >> (t + 1); lens = strlen(s + 1), lent = strlen(t + 1); for (int i = 1; i <= lens; i++) ref[s[i]].push_back(i); ans = lens; for (int i = 1; i <= lent; i++) { if (ref[t[i]].empty()) { std::cout << -1; exit(0); } auto pos = std::upper_bound(ref[t[i]].begin(), ref[t[i]].end(), last); if (pos == ref[t[i]].end()) { ans += lens; i--; last = 0; } else last = *pos; } std::cout << ans - (lens - last); exit(0); }
replace
7
8
7
8
0
p02937
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 2e5 + 6; vector<int> e[30]; int main() { string s, t; cin >> s >> t; int len = s.length(); int len2 = t.length(); for (int i = 0; i < len; i++) { e[s[i] - 'a'].push_back(i); } ll now = 0; ll cnt = 0; int flag = 1; for (int i = 0; i < len2; i++) { int u = t[i] - 'a'; if (e[u].size() == 0) { flag = 0; break; } if (flag == 0) break; int pos = lower_bound(e[u].begin(), e[u].end(), now) - e[u].begin(); if (pos >= e[u].size()) { now = 0; cnt++; pos = lower_bound(e[u].begin(), e[u].end(), now) - e[u].begin(); } now = e[u][pos] + 1; } ll ans = 1ll * cnt * len + now; if (ans <= 0) while (1) ; if (flag) cout << ans << endl; else cout << -1 << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 2e5 + 6; vector<int> e[30]; int main() { string s, t; cin >> s >> t; int len = s.length(); int len2 = t.length(); for (int i = 0; i < len; i++) { e[s[i] - 'a'].push_back(i); } ll now = 0; ll cnt = 0; int flag = 1; for (int i = 0; i < len2; i++) { int u = t[i] - 'a'; if (e[u].size() == 0) { flag = 0; break; } if (flag == 0) break; int pos = lower_bound(e[u].begin(), e[u].end(), now) - e[u].begin(); if (pos >= e[u].size()) { now = 0; cnt++; pos = lower_bound(e[u].begin(), e[u].end(), now) - e[u].begin(); } now = e[u][pos] + 1; } ll ans = 1ll * cnt * len + now; if (ans < 0) while (1) ; if (flag) cout << ans << endl; else cout << -1 << endl; }
replace
34
35
34
35
TLE
p02937
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cmath> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> #define INF 100000000000 #define MOD 1000000007 #define ll long long int // #define int long long int using namespace std; int main() { string s, t; cin >> s >> t; // able or unable vector<int> freq1(40, 0); // s material vector<int> freq2(40, 0); // t object // cout<<99<<endl; for (int i = 0; i < s.size(); i++) { freq1[s[i] - 'a']++; } for (int i = 0; i < t.size(); i++) { freq2[t[i] - 'a']++; } // cout<<888<<endl; for (int i = 0; i < 40; i++) { if (freq1[i] == 0 and freq2[i] > 0) { cout << -1 << endl; return 0; } if (freq1[i] == 0 and freq2[i] == 0) continue; } ll ans = 0; vector<vector<int>> moji(40); for (int i = 0; i < s.size(); i++) { moji[s[i] - 'a'].push_back(i); } for (int i = 0; i < s.size(); i++) { moji[s[i] - 'a'].push_back(i + s.size()); } int now = moji[t[0] - 'a'].at(0); int terget = 1; ans += now; while (true) { auto next = upper_bound(moji[t[terget] - 'a'].begin(), moji[t[terget] - 'a'].end(), now); int dif = *next - now; ans += dif; now = *next; now = now % s.size(); terget++; if (terget == t.size()) break; } cout << ans + 1 << endl; return 0; }
#include <algorithm> #include <bitset> #include <cmath> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> #define INF 100000000000 #define MOD 1000000007 #define ll long long int // #define int long long int using namespace std; int main() { string s, t; cin >> s >> t; // able or unable vector<int> freq1(40, 0); // s material vector<int> freq2(40, 0); // t object // cout<<99<<endl; for (int i = 0; i < s.size(); i++) { freq1[s[i] - 'a']++; } for (int i = 0; i < t.size(); i++) { freq2[t[i] - 'a']++; } // cout<<888<<endl; for (int i = 0; i < 40; i++) { if (freq1[i] == 0 and freq2[i] > 0) { cout << -1 << endl; return 0; } if (freq1[i] == 0 and freq2[i] == 0) continue; } ll ans = 0; vector<vector<int>> moji(40); for (int i = 0; i < s.size(); i++) { moji[s[i] - 'a'].push_back(i); } for (int i = 0; i < s.size(); i++) { moji[s[i] - 'a'].push_back(i + s.size()); } int now = moji[t[0] - 'a'].at(0); int terget = 1; ans += now; while (true) { if (terget == t.size()) break; auto next = upper_bound(moji[t[terget] - 'a'].begin(), moji[t[terget] - 'a'].end(), now); int dif = *next - now; ans += dif; now = *next; now = now % s.size(); terget++; if (terget == t.size()) break; } cout << ans + 1 << endl; return 0; }
insert
63
63
63
66
0
p02937
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) int main() { string s, t; cin >> s >> t; string ss = s; int tlong = t.size(); int64_t ans = 0; int64_t x = 0; for (int i = 0; i < tlong; i++) { x = ss.find_first_of(t.at(i)); ss.erase(ss.begin(), ss.begin() + x + 1); ss += s; // cout <<x+1<<endl; if (x >= 0) ans += x + 1; else { ans = -1; break; } } cout << ans; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) int main() { string s, t; cin >> s >> t; string ss = s; int tlong = t.size(); int64_t ans = 0; int64_t x = 0; for (int i = 0; i < tlong; i++) { x = ss.find_first_of(t.at(i)); ss.erase(ss.begin(), ss.begin() + x + 1); if (ss.size() < s.size()) ss += s; // cout <<x+1<<endl; if (x >= 0) ans += x + 1; else { ans = -1; break; } } cout << ans; }
replace
14
15
14
16
TLE
p02937
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { string s, t; cin >> s >> t; int lenS = (int)s.size(), lenT = (int)t.size(); vector<vector<int>> s_index(26); for (int i = 0; i < lenS; i++) { int c = s.at(i) - 'a'; s_index.at(c).push_back(i); } for (int i = 0; i < lenS; i++) { int c = s.at(i) - 'a'; s_index.at(c).push_back(i + lenS); } long long ans = 0; int p = 0; for (int i = 0; i < lenT; i++) { int c = t.at(i) - 'a'; if (s_index.at(c).size() == 0) { cout << -1 << endl; } p = *lower_bound(s_index.at(c).begin(), s_index.at(c).end(), p) + 1; if (p >= lenS) { p -= lenS; ans += lenS; } } ans += p; cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int main() { string s, t; cin >> s >> t; int lenS = (int)s.size(), lenT = (int)t.size(); vector<vector<int>> s_index(26); for (int i = 0; i < lenS; i++) { int c = s.at(i) - 'a'; s_index.at(c).push_back(i); } for (int i = 0; i < lenS; i++) { int c = s.at(i) - 'a'; s_index.at(c).push_back(i + lenS); } long long ans = 0; int p = 0; for (int i = 0; i < lenT; i++) { int c = t.at(i) - 'a'; if (s_index.at(c).size() == 0) { cout << -1 << endl; return 0; } p = *lower_bound(s_index.at(c).begin(), s_index.at(c).end(), p) + 1; if (p >= lenS) { p -= lenS; ans += lenS; } } ans += p; cout << ans << endl; }
insert
24
24
24
25
0
p02937
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef pair<int, int> P; #define INF 10000000000000 #define rep(i, n) for (ll i = 0; i < (int)(n); i++) template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } int main() { string s, t; ll pos = 0; ll pre = -1; ll ite = 0; bool flag = true; vector<vector<int>> alpha(26); cin >> s >> t; rep(i, s.size()) alpha[s[i] - 'a'].push_back(i + 1); rep(i, t.size()) { if (alpha[t[i] - 'a'].empty()) { flag = false; break; } rep(j, alpha[t[i] - 'a'].size()) { if (pre < alpha[t[i] - 'a'][j]) { pos = ite * s.size() + alpha[t[i] - 'a'][j]; pre = alpha[t[i] - 'a'][j]; break; } if (j == alpha[t[i] - 'a'].size() - 1) { ite++; pos = ite * s.size() + alpha[t[i] - 'a'][0]; pre = alpha[t[i] - 'a'][0]; break; } } } if (!flag) cout << "-1" << endl; else if (pos > s.size() * pow(10, 100)) cout << "-1" << endl; else cout << pos << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef pair<int, int> P; #define INF 10000000000000 #define rep(i, n) for (ll i = 0; i < (int)(n); i++) template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } int main() { string s, t; ll pos = 0; ll pre = -1; ll ite = 0; bool flag = true; vector<vector<int>> alpha(26); cin >> s >> t; rep(i, s.size()) alpha[s[i] - 'a'].push_back(i + 1); rep(i, t.size()) { if (alpha[t[i] - 'a'].empty()) { flag = false; break; } auto it = upper_bound(alpha[t[i] - 'a'].begin(), alpha[t[i] - 'a'].end(), pre); if (it != alpha[t[i] - 'a'].end()) { pos = ite * s.size() + *it; pre = *it; } else { ite++; pos = ite * s.size() + alpha[t[i] - 'a'][0]; pre = alpha[t[i] - 'a'][0]; } } if (!flag) cout << "-1" << endl; else if (pos > s.size() * pow(10, 100)) cout << "-1" << endl; else cout << pos << endl; return 0; }
replace
40
53
40
49
TLE
p02937
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; typedef long long ll; int main() { string s, t; cin >> s >> t; map<char, set<int>> mp; vector<vector<int>> v; rep(i, s.size()) { v[s[i] - 'a'].push_back(i); } int id = -1; ll ans = 0; rep(i, t.size()) { int x = t[i] - 'a'; if (v[x].size() == 0) { cout << -1 << endl; return 0; } auto it = upper_bound(v[x].begin(), v[x].end(), id); if (it == v[x].end()) { ans += s.size(); id = *(v[x].begin()); } else { id = *it; } } ans += id + 1; cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; typedef long long ll; int main() { string s, t; cin >> s >> t; map<char, set<int>> mp; vector<vector<int>> v(26); rep(i, s.size()) { v[s[i] - 'a'].push_back(i); } int id = -1; ll ans = 0; rep(i, t.size()) { int x = t[i] - 'a'; if (v[x].size() == 0) { cout << -1 << endl; return 0; } auto it = upper_bound(v[x].begin(), v[x].end(), id); if (it == v[x].end()) { ans += s.size(); id = *(v[x].begin()); } else { id = *it; } } ans += id + 1; cout << ans << endl; return 0; }
replace
9
10
9
10
-11
p02937
C++
Time Limit Exceeded
/** * code generated by JHelper * More info: https://github.com/AlexeyDmitriev/JHelper * @author tatsumack */ #include <fstream> #include <iostream> #include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <limits.h> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> #define int long long #define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i) #define REPS(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i) #define FOR(i, a, b) for (int i = (a), i##_len = (b); i <= i##_len; ++i) #define REV(i, a, b) for (int i = (a); i >= (b); --i) #define CLR(a, b) memset((a), (b), sizeof(a)) #define DUMP(x) cout << #x << " = " << (x) << endl; #define INF 1001001001001001001ll #define fcout cout << fixed << setprecision(12) using namespace std; typedef pair<int, int> P; class EStringsOfImpurity { public: static constexpr int kStressIterations = 0; static void generateTest(std::ostream &test) {} void solve(std::istream &cin, std::ostream &cout) { string s, t; cin >> s >> t; vector<vector<int>> pos(50); REP(i, s.size()) { char c = s[i]; pos[c - 'a'].push_back(i + 1); } int cur = 0; int res = 0; REP(i, t.size()) { char c = t[i]; auto v = pos[c - 'a']; if (v.empty()) { cout << -1 << endl; return; } auto itr = upper_bound(v.begin(), v.end(), cur); if (itr != v.end()) { int next = *itr; res += next - cur; cur = next; } else { int next = v[0]; res += s.size() - cur; res += next; cur = next; } } cout << res << endl; } }; signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); std::istream &in(std::cin); std::ostream &out(std::cout); EStringsOfImpurity solver; solver.solve(in, out); return 0; }
/** * code generated by JHelper * More info: https://github.com/AlexeyDmitriev/JHelper * @author tatsumack */ #include <fstream> #include <iostream> #include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <limits.h> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> #define int long long #define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i) #define REPS(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i) #define FOR(i, a, b) for (int i = (a), i##_len = (b); i <= i##_len; ++i) #define REV(i, a, b) for (int i = (a); i >= (b); --i) #define CLR(a, b) memset((a), (b), sizeof(a)) #define DUMP(x) cout << #x << " = " << (x) << endl; #define INF 1001001001001001001ll #define fcout cout << fixed << setprecision(12) using namespace std; typedef pair<int, int> P; class EStringsOfImpurity { public: static constexpr int kStressIterations = 0; static void generateTest(std::ostream &test) {} void solve(std::istream &cin, std::ostream &cout) { string s, t; cin >> s >> t; vector<vector<int>> pos(50); REP(i, s.size()) { char c = s[i]; pos[c - 'a'].push_back(i + 1); } int cur = 0; int res = 0; REP(i, t.size()) { char c = t[i]; const auto &v = pos[c - 'a']; if (v.empty()) { cout << -1 << endl; return; } auto itr = upper_bound(v.begin(), v.end(), cur); if (itr != v.end()) { int next = *itr; res += next - cur; cur = next; } else { int next = v[0]; res += s.size() - cur; res += next; cur = next; } } cout << res << endl; } }; signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); std::istream &in(std::cin); std::ostream &out(std::cout); EStringsOfImpurity solver; solver.solve(in, out); return 0; }
replace
70
71
70
71
TLE
p02937
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { string s, t; cin >> s >> t; string ss = s + s; vector<int> v[26]; for (int i = 0; i < ss.size(); i++) { v[ss[i] - 'a'].emplace_back(i); } long long curr = v[t[0] - 'a'][0]; if (t.size() == 1) { cout << curr + 1 << endl; return 0; } for (int i = 1; i < t.size(); i++) { int c = t[i] - 'a'; int x = curr % s.size(); if (v[c].empty()) { cout << -1 << endl; return 0; } int next = *upper_bound(v[c].begin(), v[c].end(), x); curr += next - x; } cout << curr + 1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string s, t; cin >> s >> t; string ss = s + s; vector<int> v[26]; for (int i = 0; i < ss.size(); i++) { v[ss[i] - 'a'].emplace_back(i); } if (v[t[0] - 'a'].empty()) { cout << -1 << endl; return 0; } long long curr = v[t[0] - 'a'][0]; if (t.size() == 1) { cout << curr + 1 << endl; return 0; } for (int i = 1; i < t.size(); i++) { int c = t[i] - 'a'; int x = curr % s.size(); if (v[c].empty()) { cout << -1 << endl; return 0; } int next = *upper_bound(v[c].begin(), v[c].end(), x); curr += next - x; } cout << curr + 1 << endl; return 0; }
insert
9
9
9
13
0
p02937
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long #define ALL(v) (v).begin(), (v).end() #define REP(i, p, n) for (int i = p; i < (int)(n); ++i) #define rep(i, n) REP(i, 0, n) #define dump(a) (cerr << #a << "=" << (a) << endl) #define DUMP(list) \ cout << "{ "; \ for (auto nth : list) { \ cout << nth << " "; \ } \ cout << "}" << endl; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } using namespace std; int main() { string s, t; cin >> s; cin >> t; int n = s.size(), m = t.size(); vector<vector<int>> is(26); rep(i, n) is[s[i] - 'a'].push_back(i); rep(i, n) is[s[i] - 'a'].push_back(i + n); ll ans = 0; int p = 0; rep(i, m) { int c = t[i] - 'a'; if (is[c].size() == 0) { puts("-1"); } p = *lower_bound(is[c].begin(), is[c].end(), p) + 1; if (p >= n) { p -= n; ans += n; } } ans += p; cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define ll long long #define ALL(v) (v).begin(), (v).end() #define REP(i, p, n) for (int i = p; i < (int)(n); ++i) #define rep(i, n) REP(i, 0, n) #define dump(a) (cerr << #a << "=" << (a) << endl) #define DUMP(list) \ cout << "{ "; \ for (auto nth : list) { \ cout << nth << " "; \ } \ cout << "}" << endl; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } using namespace std; int main() { string s, t; cin >> s; cin >> t; int n = s.size(), m = t.size(); vector<vector<int>> is(26); rep(i, n) is[s[i] - 'a'].push_back(i); rep(i, n) is[s[i] - 'a'].push_back(i + n); ll ans = 0; int p = 0; rep(i, m) { int c = t[i] - 'a'; if (is[c].size() == 0) { puts("-1"); return 0; } p = *lower_bound(is[c].begin(), is[c].end(), p) + 1; if (p >= n) { p -= n; ans += n; } } ans += p; cout << ans << endl; return 0; }
insert
43
43
43
44
0
p02937
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef unsigned int uint; typedef unsigned char uchar; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; #define REP(i, x) for (int i = 0; i < (int)(x); i++) #define REPS(i, x) for (int i = 1; i <= (int)(x); i++) #define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--) #define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--) #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++) #define RFOR(i, c) \ for (__typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); i++) #define ALL(container) (container).begin(), (container).end() #define RALL(container) (container).rbegin(), (container).rend() #define SZ(container) ((int)container.size()) #define mp(a, b) make_pair(a, b) #define pb push_back #define eb emplace_back #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()); #define __builtin_popcount __builtin_popcountll template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } template <class T> ostream &operator<<(ostream &os, const vector<T> &t) { os << "["; FOR(it, t) { if (it != t.begin()) os << ","; os << *it; } os << "]"; return os; } template <class T> ostream &operator<<(ostream &os, const set<T> &t) { os << "{"; FOR(it, t) { if (it != t.begin()) os << ","; os << *it; } os << "}"; return os; } template <class S, class T> ostream &operator<<(ostream &os, const map<S, T> &t) { os << "{"; FOR(it, t) { if (it != t.begin()) os << ","; os << *it; } os << "}"; return os; } template <class S, class T> ostream &operator<<(ostream &os, const pair<S, T> &t) { return os << "(" << t.first << "," << t.second << ")"; } template <class S, class T> pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first + t.first, s.second + t.second); } template <class S, class T> pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first - t.first, s.second - t.second); } const int INF = 1 << 28; const double EPS = 1e-8; const int MOD = 1000000007; int T, n, m; int main(int argc, char *argv[]) { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); string s, t; while (cin >> s >> t) { n = s.size(); vector<vector<int>> tbl(26, vi(n, -1)); REP(i, n) { tbl[s[i] - 'a'][i] = i; } REP(_, 2) { RREP(i, n) { REP(j, 26) { if (tbl[j][i] == -1) { tbl[j][i] = tbl[j][(i + 1) % n]; } } } } ll ans = 0, p = 0; REP(i, t.size()) { ll q = tbl[t[i] - 'a'][p]; if (q == -1) { ans = -1; break; } if (q < p) ans += n; ans += q - p + 1; p = q + 1; } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef unsigned int uint; typedef unsigned char uchar; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; #define REP(i, x) for (int i = 0; i < (int)(x); i++) #define REPS(i, x) for (int i = 1; i <= (int)(x); i++) #define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--) #define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--) #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++) #define RFOR(i, c) \ for (__typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); i++) #define ALL(container) (container).begin(), (container).end() #define RALL(container) (container).rbegin(), (container).rend() #define SZ(container) ((int)container.size()) #define mp(a, b) make_pair(a, b) #define pb push_back #define eb emplace_back #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()); #define __builtin_popcount __builtin_popcountll template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } template <class T> ostream &operator<<(ostream &os, const vector<T> &t) { os << "["; FOR(it, t) { if (it != t.begin()) os << ","; os << *it; } os << "]"; return os; } template <class T> ostream &operator<<(ostream &os, const set<T> &t) { os << "{"; FOR(it, t) { if (it != t.begin()) os << ","; os << *it; } os << "}"; return os; } template <class S, class T> ostream &operator<<(ostream &os, const map<S, T> &t) { os << "{"; FOR(it, t) { if (it != t.begin()) os << ","; os << *it; } os << "}"; return os; } template <class S, class T> ostream &operator<<(ostream &os, const pair<S, T> &t) { return os << "(" << t.first << "," << t.second << ")"; } template <class S, class T> pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first + t.first, s.second + t.second); } template <class S, class T> pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first - t.first, s.second - t.second); } const int INF = 1 << 28; const double EPS = 1e-8; const int MOD = 1000000007; int T, n, m; int main(int argc, char *argv[]) { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); string s, t; while (cin >> s >> t) { n = s.size(); vector<vector<int>> tbl(26, vi(n, -1)); REP(i, n) { tbl[s[i] - 'a'][i] = i; } REP(_, 2) { RREP(i, n) { REP(j, 26) { if (tbl[j][i] == -1) { tbl[j][i] = tbl[j][(i + 1) % n]; } } } } ll ans = 0, p = 0; REP(i, t.size()) { ll q = tbl[t[i] - 'a'][p]; if (q == -1) { ans = -1; break; } if (q < p) ans += n; ans += q - p + 1; p = (q + 1) % n; } cout << ans << endl; } return 0; }
replace
119
120
119
120
0
p02937
C++
Runtime Error
#include <algorithm> #include <climits> #include <cstdlib> #include <functional> #include <iostream> #include <math.h> #include <numeric> #include <string.h> #include <string> #include <vector> #define LOOP(N) for (int i = 0; i < (N); ++i) #define REP(i, N) for (int i = 0; i < (N); ++i) #define FOR(i, start, end) for (int i = (start); i < (end); ++i) #define ALL(a) (a).begin(), (a).end() using namespace std; using uint = unsigned int; using ll = long long; using ull = unsigned long long; using Graph = vector<vector<int>>; const uint ALPHABET_NUM = 'z' - 'a' + 1; int main() { string s, t; cin >> s >> t; vector<vector<int>> alphabet_pos(ALPHABET_NUM); REP(i, (int)s.length()) { alphabet_pos[s[i] - 'a'].push_back(i); } // impossible REP(i, (int)t.length()) { if (alphabet_pos[t[i] - 'a'].empty()) { cout << -1 << endl; return 0; } } ull result = 0; ull now = 0, next; auto next_pos = alphabet_pos[0].begin(); REP(i, (int)t.length()) { auto index_p = &alphabet_pos[t[i] - 'a']; if (i > 0 && t[i - 1] == t[i]) { next_pos = lower_bound(next_pos + 1, index_p->end(), now); } else { next_pos = lower_bound(ALL(*index_p), now); } if (next_pos == index_p->end()) { next = (*index_p)[0]; result += (ull)s.length() + next - now; // cout << "non " << now << " " << next << endl; } else { next = *next_pos; result += next - now; // cout << "find " << now << " " << next << endl; } now = next; // cout << result << endl; } cout << result + 1; }
#include <algorithm> #include <climits> #include <cstdlib> #include <functional> #include <iostream> #include <math.h> #include <numeric> #include <string.h> #include <string> #include <vector> #define LOOP(N) for (int i = 0; i < (N); ++i) #define REP(i, N) for (int i = 0; i < (N); ++i) #define FOR(i, start, end) for (int i = (start); i < (end); ++i) #define ALL(a) (a).begin(), (a).end() using namespace std; using uint = unsigned int; using ll = long long; using ull = unsigned long long; using Graph = vector<vector<int>>; const uint ALPHABET_NUM = 'z' - 'a' + 1; int main() { string s, t; cin >> s >> t; vector<vector<int>> alphabet_pos(ALPHABET_NUM); REP(i, (int)s.length()) { alphabet_pos[s[i] - 'a'].push_back(i); } // impossible REP(i, (int)t.length()) { if (alphabet_pos[t[i] - 'a'].empty()) { cout << -1 << endl; return 0; } } ull result = 0; ull now = 0, next; auto next_pos = alphabet_pos[0].begin(); REP(i, (int)t.length()) { auto index_p = &alphabet_pos[t[i] - 'a']; if (i > 0 && t[i - 1] == t[i]) { next_pos = lower_bound(next_pos + 1, index_p->end(), now); } else { next_pos = lower_bound(ALL(*index_p), now); } if (next_pos == index_p->end()) { next = (*index_p)[0]; next_pos = index_p->begin(); result += (ull)s.length() + next - now; // cout << "non " << now << " " << next << endl; } else { next = *next_pos; result += next - now; // cout << "find " << now << " " << next << endl; } now = next; // cout << result << endl; } cout << result + 1; }
insert
51
51
51
52
0
p02937
C++
Time Limit Exceeded
// vim: shiftwidth=2 expandtab foldmethod=marker // include {{{ #include <cmath> #include <cstdlib> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> // }}} // misc {{{ using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep2(i, s, n) for (int i = (s); i < (int)(n); i++) // }}} // debug(...) {{{ static bool debug_enabled = false; static int debug_max_elms = 10; static int debug_max_count = 50; static int debug_current_count = 0; void debug1(char const *x) { cout << x; } template <class T> void debug1(const T &x) { cout << x; } template <class T1, class T2> void debug1(const pair<T1, T2> &x) { cout << "(" << x.first << ", " << x.second << ")"; } template <class T> void debug1(const vector<T> &x) { cout << "["; auto i = x.begin(); while (i != x.end()) { debug1(*i); ++i; if (i != x.end()) cout << ", "; if (std::distance(x.begin(), i) > debug_max_elms) { cout << "..."; break; } } cout << "]"; } template <class K, class V> void debug1(const map<K, V> &x) { cout << "{"; auto i = x.begin(); while (i != x.end()) { debug1(get<0>(*i)); cout << ": "; debug1(get<1>(*i)); ++i; if (i != x.end()) cout << ", "; } cout << "}"; } void debug2() { cout << endl; } template <class T, class... Ts> void debug2(const T &x, const Ts &...xs) { cout << ' '; debug1(x); debug2(xs...); } template <class T, class... Ts> void debug(const T &x, const Ts &...xs) { if (debug_enabled) { ++debug_current_count; if (debug_current_count > debug_max_count) { cout << "... (debug log disabled)" << endl; debug_enabled = false; } cout << "[D] "; debug1(x); debug2(xs...); } } // }}} // main {{{ void solve(); int main() { auto v = getenv("DEBUG"); if (v && string(v) == "1") debug_enabled = true; if (false) debug("dummy for suppress warnings", vector<int>(0)); solve(); } // }}} int dp[30][100001]; int next(unsigned char c, const string &s, int i) { if (i == s.size()) return -1; int idx = c - 'a'; if (dp[idx][i] > -2) return dp[idx][i]; if (s[i] == c) { return dp[idx][i] = i; } return dp[idx][i] = next(c, s, i + 1); } ll calc(const string &s, const string &t, int i, int j) { debug("calc:", i, s[i], j, t[j]); if (j == t.size()) return 0; debug(s.substr(i, s.size() - i), t.substr(j, t.size() - j)); int nx = next(t[j], s, i); debug("nx", nx); if (nx == -1) { if (i == 0) return -1; ll rest = calc(s, t, 0, j); if (rest == -1) return -1; else return s.length() - i + rest; } ll rest = calc(s, t, nx + 1, j + 1); if (rest == -1) return -1; return nx - i + 1 + rest; } void solve() { string s, t; getline(cin, s); getline(cin, t); rep(i, 30) rep(j, 100001) dp[i][j] = -2; cout << calc(s, t, 0, 0) << endl; }
// vim: shiftwidth=2 expandtab foldmethod=marker // include {{{ #include <cmath> #include <cstdlib> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> // }}} // misc {{{ using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep2(i, s, n) for (int i = (s); i < (int)(n); i++) // }}} // debug(...) {{{ static bool debug_enabled = false; static int debug_max_elms = 10; static int debug_max_count = 50; static int debug_current_count = 0; void debug1(char const *x) { cout << x; } template <class T> void debug1(const T &x) { cout << x; } template <class T1, class T2> void debug1(const pair<T1, T2> &x) { cout << "(" << x.first << ", " << x.second << ")"; } template <class T> void debug1(const vector<T> &x) { cout << "["; auto i = x.begin(); while (i != x.end()) { debug1(*i); ++i; if (i != x.end()) cout << ", "; if (std::distance(x.begin(), i) > debug_max_elms) { cout << "..."; break; } } cout << "]"; } template <class K, class V> void debug1(const map<K, V> &x) { cout << "{"; auto i = x.begin(); while (i != x.end()) { debug1(get<0>(*i)); cout << ": "; debug1(get<1>(*i)); ++i; if (i != x.end()) cout << ", "; } cout << "}"; } void debug2() { cout << endl; } template <class T, class... Ts> void debug2(const T &x, const Ts &...xs) { cout << ' '; debug1(x); debug2(xs...); } template <class T, class... Ts> void debug(const T &x, const Ts &...xs) { if (debug_enabled) { ++debug_current_count; if (debug_current_count > debug_max_count) { cout << "... (debug log disabled)" << endl; debug_enabled = false; } cout << "[D] "; debug1(x); debug2(xs...); } } // }}} // main {{{ void solve(); int main() { auto v = getenv("DEBUG"); if (v && string(v) == "1") debug_enabled = true; if (false) debug("dummy for suppress warnings", vector<int>(0)); solve(); } // }}} int dp[30][100001]; int next(unsigned char c, const string &s, int i) { if (i == s.size()) return -1; int idx = c - 'a'; if (dp[idx][i] > -2) return dp[idx][i]; if (s[i] == c) { return dp[idx][i] = i; } return dp[idx][i] = next(c, s, i + 1); } ll calc(const string &s, const string &t, int i, int j) { debug("calc:", i, s[i], j, t[j]); if (j == t.size()) return 0; // debug(s.substr(i, s.size() - i), t.substr(j, t.size() - j)); int nx = next(t[j], s, i); debug("nx", nx); if (nx == -1) { if (i == 0) return -1; ll rest = calc(s, t, 0, j); if (rest == -1) return -1; else return s.length() - i + rest; } ll rest = calc(s, t, nx + 1, j + 1); if (rest == -1) return -1; return nx - i + 1 + rest; } void solve() { string s, t; getline(cin, s); getline(cin, t); rep(i, 30) rep(j, 100001) dp[i][j] = -2; cout << calc(s, t, 0, 0) << endl; }
replace
118
119
118
119
TLE
p02937
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; using ll = long long; using P = pair<int, int>; int main() { string s, t; cin >> s >> t; int n = s.size(), m = t.size(); map<char, vector<int>> mp; rep(i, n) mp[s[i]].push_back(i); rep(i, m) if (mp[t[i]].empty()) { cout << -1 << endl; return 0; } ll ans = 0; int now = -1; rep(i, m) { char c = t[i]; for (int j = 0; j < mp[t[i]].size(); j++) { if (now < mp[t[i]][j]) { ans += mp[t[i]][j] - now; now = mp[t[i]][j]; break; } if (j == mp[t[i]].size() - 1) { ans += n - now + mp[t[i]][0]; now = mp[t[i]][0]; } } } cout << ans << endl; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; using ll = long long; using P = pair<int, int>; int main() { string s, t; cin >> s >> t; int n = s.size(), m = t.size(); map<char, vector<int>> mp; rep(i, n) mp[s[i]].push_back(i); rep(i, m) if (mp[t[i]].empty()) { cout << -1 << endl; return 0; } ll ans = 0; int now = -1; rep(i, m) { char c = t[i]; int idx = upper_bound(mp[c].begin(), mp[c].end(), now) - mp[c].begin(); if (idx == mp[c].size()) { ans += n - now + mp[c][0]; now = mp[c][0]; } else { ans += mp[c][idx] - now; now = mp[c][idx]; } } cout << ans << endl; }
replace
20
30
20
27
TLE
p02937
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define int long long #define endl "\n" #define REP(i, a, n) for (int i = a; i < n; ++i) #define REPR(i, a, n) for (int i = a; i > n; --i) #define RUP(a, b) ((a + b - 1) / (b)) #define ALL(v) (v).begin(), (v).end() #define pb push_back #define mp make_pair #define mt make_tuple typedef long long ll; typedef pair<int, int> Pii; typedef tuple<int, int, int> Tiii; typedef vector<int> Vi; typedef vector<Vi> VVi; typedef vector<Pii> VPii; typedef vector<string> Vs; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } template <class T> void YesNo(T a) { cout << (a ? "Yes" : "No") << endl; } template <class T> void YESNO(T a) { cout << (a ? "YES" : "NO") << endl; } int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int lcm(int a, int b) { return a / gcd(a, b) * b; } void uniq(Vi &v) { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); } signed main() { cin.tie(0); cout.tie(0); ios::sync_with_stdio(false); string s, t; int ans = 0; cin >> s >> t; map<char, Vi> ma; map<char, int> mab; REP(i, 0, s.size()) { ma[s[i]].pb(i + 1); mab[s[i]]++; } int now = 0; REP(i, 0, t.size()) { if (mab[t[i]] == 0) { cout << -1 << endl; return 0; } REP(j, 0, ma[t[i]].size() + 1) { if (j == ma[t[i]].size()) { ans += s.size(); now = ma[t[i]][0]; break; } if (now < ma[t[i]][j]) { now = ma[t[i]][j]; break; } } } cout << ans + now << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long #define endl "\n" #define REP(i, a, n) for (int i = a; i < n; ++i) #define REPR(i, a, n) for (int i = a; i > n; --i) #define RUP(a, b) ((a + b - 1) / (b)) #define ALL(v) (v).begin(), (v).end() #define pb push_back #define mp make_pair #define mt make_tuple typedef long long ll; typedef pair<int, int> Pii; typedef tuple<int, int, int> Tiii; typedef vector<int> Vi; typedef vector<Vi> VVi; typedef vector<Pii> VPii; typedef vector<string> Vs; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } template <class T> void YesNo(T a) { cout << (a ? "Yes" : "No") << endl; } template <class T> void YESNO(T a) { cout << (a ? "YES" : "NO") << endl; } int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int lcm(int a, int b) { return a / gcd(a, b) * b; } void uniq(Vi &v) { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); } signed main() { cin.tie(0); cout.tie(0); ios::sync_with_stdio(false); string s, t; int ans = 0; cin >> s >> t; map<char, Vi> ma; map<char, int> mab; REP(i, 0, s.size()) { ma[s[i]].pb(i + 1); mab[s[i]]++; } int now = 0; REP(i, 0, t.size()) { if (mab[t[i]] == 0) { cout << -1 << endl; return 0; } auto iter = upper_bound(ALL(ma[t[i]]), now); if (iter == ma[t[i]].end()) { ans += s.size(); now = ma[t[i]][0]; } else { now = *iter; } } cout << ans + now << endl; return 0; }
replace
64
74
64
70
TLE
p02937
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define inf 10e17 #define rep(i, n) for (long long i = 0; i < n; i++) #define repr(i, n, m) for (long long i = m; i < n; i++) #define mod 1000000007 #define sorti(x) sort(x.begin(), x.end()) #define sortd(x) sort(x.begin(), x.end(), std::greater<long long>()) #define debug(x) std::cerr << (x) << std::endl; #define roll(x) \ for (auto &&itr : x) { \ cerr << (itr) << " "; \ } template <class T> inline void chmax(T &ans, T t) { if (t > ans) ans = t; } template <class T> inline void chmin(T &ans, T t) { if (t < ans) ans = t; } int main() { string s, t; cin >> s >> t; map<char, vector<ll>> mp; rep(i, s.size()) { mp[s[i]].push_back(i); } for (auto i : t) { if (mp[i].size() == 0) { cout << "-1" << endl; return 0; } } ll sum = 0, pre = -1; for (auto t_ch : t) { auto ch_vec = mp[t_ch]; auto it = upper_bound(ch_vec.begin(), ch_vec.end(), pre); // cout << *it << endl; if (it == ch_vec.end() || *it <= pre) { sum += 1; pre = ch_vec.front(); } else { pre = *it; } } cout << sum * s.size() + pre + 1 << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define inf 10e17 #define rep(i, n) for (long long i = 0; i < n; i++) #define repr(i, n, m) for (long long i = m; i < n; i++) #define mod 1000000007 #define sorti(x) sort(x.begin(), x.end()) #define sortd(x) sort(x.begin(), x.end(), std::greater<long long>()) #define debug(x) std::cerr << (x) << std::endl; #define roll(x) \ for (auto &&itr : x) { \ cerr << (itr) << " "; \ } template <class T> inline void chmax(T &ans, T t) { if (t > ans) ans = t; } template <class T> inline void chmin(T &ans, T t) { if (t < ans) ans = t; } int main() { string s, t; cin >> s >> t; map<char, vector<ll>> mp; rep(i, s.size()) { mp[s[i]].push_back(i); } for (auto i : t) { if (mp[i].size() == 0) { cout << "-1" << endl; return 0; } } ll sum = 0, pre = -1; for (auto t_ch : t) { auto &&ch_vec = mp[t_ch]; auto &&it = upper_bound(ch_vec.begin(), ch_vec.end(), pre); // cout << *it << endl; if (it == ch_vec.end() || *it <= pre) { sum += 1; pre = ch_vec.front(); } else { pre = *it; } } cout << sum * s.size() + pre + 1 << endl; }
replace
40
42
40
42
TLE
p02937
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int INF = 1e9; const int MOD = 1e9 + 7; const ll LINF = 1e18; int main() { string s, t; cin >> s >> t; vector<vector<ll>> ma(26); for (ll i = 0; i < (ll)s.length(); ++i) { ll u = s[i] - 'a'; ma[u].push_back(i + 1); } ll ans = 0; ll cnt = 0; for (ll i = 0; i < (ll)t.length(); ++i) { ll u = t[i] - 'a'; if (ma[u].empty()) { cout << -1 << endl; return 0; } bool match = false; for (auto v : ma[u]) { if (cnt < v) { ans += v - cnt; match = true; cnt = v; break; } } if (!match) { ans += s.length() - cnt; ans += ma[u][0]; cnt = ma[u][0]; } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int INF = 1e9; const int MOD = 1e9 + 7; const ll LINF = 1e18; int main() { string s, t; cin >> s >> t; vector<vector<ll>> ma(26); for (ll i = 0; i < (ll)s.length(); ++i) { ll u = s[i] - 'a'; ma[u].push_back(i + 1); } ll ans = 0; ll cnt = 0; for (ll i = 0; i < (ll)t.length(); ++i) { ll u = t[i] - 'a'; if (ma[u].empty()) { cout << -1 << endl; return 0; } bool match = false; auto it = upper_bound(ma[u].begin(), ma[u].end(), cnt); if (it != ma[u].end()) { ll id = distance(ma[u].begin(), it); ans += ma[u][id] - cnt; match = true; cnt = ma[u][id]; } if (!match) { ans += s.length() - cnt; ans += ma[u][0]; cnt = ma[u][0]; } } cout << ans << endl; }
replace
30
37
30
36
TLE
p02937
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <iostream> #include <vector> using namespace std; int main() { string s, t; cin >> s; cin >> t; long long n = s.size(); long long m = t.size(); vector<vector<long long>> index(30); for (long long i = 0; i < 30; i++) index[s[i] - 'a'].push_back(i + 1); long long ans = 0; long long last = 0; for (long long i = 0; i < m; i++) { long long nch = t[i] - 'a'; if (index[nch].size() <= 0) { printf("-1\n"); return 0; } auto itr = upper_bound(index[nch].begin(), index[nch].end(), last); if (itr == index[nch].end()) { ans += n; itr = index[nch].begin(); } last = *itr; } ans += last; cout << ans << endl; return 0; }
#include <algorithm> #include <cstdio> #include <iostream> #include <vector> using namespace std; int main() { string s, t; cin >> s; cin >> t; long long n = s.size(); long long m = t.size(); vector<vector<long long>> index(30); for (long long i = 0; i < n; i++) index[s[i] - 'a'].push_back(i + 1); long long ans = 0; long long last = 0; for (long long i = 0; i < m; i++) { long long nch = t[i] - 'a'; if (index[nch].size() <= 0) { printf("-1\n"); return 0; } auto itr = upper_bound(index[nch].begin(), index[nch].end(), last); if (itr == index[nch].end()) { ans += n; itr = index[nch].begin(); } last = *itr; } ans += last; cout << ans << endl; return 0; }
replace
16
17
16
17
-11
p02937
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<ll> vll; const ll INF = 1e18; const ll N = 1e5; const ll M = 1e9 + 7; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string s, t; cin >> s >> t; vll p[26]; for (ll i = 0; i < s.size(); ++i) p[s[i] - 'a'].push_back(i); ll a[t.size()]; a[0] = p[t[0] - 'a'][0]; for (ll i = 1; i < t.size(); ++i) { ll ch = t[i] - 'a'; if (p[ch].empty()) { cout << -1 << '\n'; return 0; } ll b = upper_bound(p[ch].begin(), p[ch].end(), a[i - 1]) - p[ch].begin(); if (b == p[ch].size()) b = 0; a[i] = p[ch][b]; } ll c = 0; for (ll i = 0; i < t.size() - 1; ++i) if (a[i + 1] <= a[i]) ++c; cout << s.size() * c + a[t.size() - 1] + 1 << '\n'; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<ll> vll; const ll INF = 1e18; const ll N = 1e5; const ll M = 1e9 + 7; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string s, t; cin >> s >> t; vll p[26]; for (ll i = 0; i < s.size(); ++i) p[s[i] - 'a'].push_back(i); ll a[t.size()]; if (p[t[0] - 'a'].empty()) { cout << -1 << '\n'; return 0; } a[0] = p[t[0] - 'a'][0]; for (ll i = 1; i < t.size(); ++i) { ll ch = t[i] - 'a'; if (p[ch].empty()) { cout << -1 << '\n'; return 0; } ll b = upper_bound(p[ch].begin(), p[ch].end(), a[i - 1]) - p[ch].begin(); if (b == p[ch].size()) b = 0; a[i] = p[ch][b]; } ll c = 0; for (ll i = 0; i < t.size() - 1; ++i) if (a[i + 1] <= a[i]) ++c; cout << s.size() * c + a[t.size() - 1] + 1 << '\n'; }
insert
22
22
22
26
0
p02937
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); ++i) typedef long long ll; typedef pair<ll, ll> P; const int MAX = 1e5 + 10; const int INF = 1e9; const int MOD = 1e9 + 7; string s, t; int main() { cin >> s >> t; map<char, vector<int>> m; rep(i, s.size()) { char c = s[i]; m[c].push_back(i); } ll ans = 0; int x = -1; rep(i, t.size()) { char c = t[i]; vector<int> index = m[c]; if (index.size() == 0) { cout << -1 << endl; return 0; } auto itr = upper_bound(index.begin(), index.end(), x); if (itr == index.end()) { ans += s.size(); x = m[c][0]; } else { x = *itr; } } ans += x + 1; cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); ++i) typedef long long ll; typedef pair<ll, ll> P; const int MAX = 1e5 + 10; const int INF = 1e9; const int MOD = 1e9 + 7; string s, t; int main() { cin >> s >> t; map<char, vector<int>> m; rep(i, s.size()) { char c = s[i]; m[c].push_back(i); } ll ans = 0; int x = -1; rep(i, t.size()) { char c = t[i]; vector<int> &index = m[c]; if (index.size() == 0) { cout << -1 << endl; return 0; } auto itr = upper_bound(index.begin(), index.end(), x); if (itr == index.end()) { ans += s.size(); x = m[c][0]; } else { x = *itr; } } ans += x + 1; cout << ans << endl; }
replace
27
28
27
28
TLE
p02937
C++
Time Limit Exceeded
#include <cstdio> #include <cstring> typedef long long ll; const int N = 100010; int ls, lt, cnt[26], v[26][N]; ll ans; char s[N], t[N]; int main() { scanf("%s%s", s + 1, t + 1); ls = strlen(s + 1); lt = strlen(t + 1); for (int i = 1; i <= ls; ++i) { int x = s[i] - 'a'; v[x][++cnt[x]] = i; } for (int i = 1; i <= lt; ++i) if (!cnt[t[i] - 'a']) return puts("-1"), 0; int j = 0, k; for (int i = 1; i <= lt; ++i) { int x = t[i] - 'a'; k = 1; while (v[x][k] <= j && k <= cnt[x]) ++k; if (k > cnt[x]) k = 1, ans += 1ll * ls; ans += 1ll * v[x][k] - j; j = v[x][k]; } printf("%lld\n", ans); return 0; }
#include <cstdio> #include <cstring> typedef long long ll; const int N = 100010; int ls, lt, cnt[26], v[26][N]; ll ans; char s[N], t[N]; int main() { scanf("%s%s", s + 1, t + 1); ls = strlen(s + 1); lt = strlen(t + 1); for (int i = 1; i <= ls; ++i) { int x = s[i] - 'a'; v[x][++cnt[x]] = i; } for (int i = 1; i <= lt; ++i) if (!cnt[t[i] - 'a']) return puts("-1"), 0; int j = 0, k; for (int i = 1; i <= lt; ++i) { int x = t[i] - 'a'; if (j >= v[x][cnt[x]]) { ans += ls - j + v[x][1]; j = v[x][1]; continue; } int l = 1, r = cnt[x]; while (l < r) { int m = l + r >> 1; v[x][m] > j ? (r = m) : (l = m + 1); } ans += 1ll * v[x][r] - j; j = v[x][r]; } printf("%lld\n", ans); return 0; }
replace
22
29
22
34
TLE
p02937
C++
Runtime Error
#include <algorithm> #include <cmath> #include <iostream> #include <map> #include <math.h> #include <queue> #include <stdio.h> #include <vector> using namespace std; #define int long long #define double long double #define rep(s, i, n) for (int i = s; i < n; i++) #define c(n) cout << n << endl; #define ic(n) \ int n; \ cin >> n; #define sc(s) \ string s; \ cin >> s; #define mod 1000000007 #define inf 1000000000000000007 #define f first #define s second #define mini(c, a, b) *min_element(c + a, c + b) #define maxi(c, a, b) *max_element(c + a, c + b) #define pi 3.141592653589793238462643383279 #define e_ 2.718281828459045235360287471352 #define P pair<int, int> #define upp(a, n, x) upper_bound(a, a + n, x) - a; #define low(a, n, x) lower_bound(a, a + n, x) - a; #define UF UnionFind // printf("%.12Lf\n",); int keta(int x) { rep(0, i, 30) { if (x < 10) { return i + 1; } x = x / 10; } } int gcd(int x, int y) { int aa = x, bb = y; rep(0, i, 1000) { aa = aa % bb; if (aa == 0) { return bb; } bb = bb % aa; if (bb == 0) { return aa; } } } int lcm(int x, int y) { int aa = x, bb = y; rep(0, i, 1000) { aa = aa % bb; if (aa == 0) { return x / bb * y; } bb = bb % aa; if (bb == 0) { return x / aa * y; } } } bool p(int x) { if (x == 1) return false; rep(2, i, sqrt(x) + 1) { if (x % i == 0 && x != i) { return false; } } return true; } int max(int a, int b) { if (a >= b) return a; else return b; } string maxst(string s, string t) { int n = s.size(); int m = t.size(); if (n > m) return s; else if (n < m) return t; else { rep(0, i, n) { if (s[i] > t[i]) return s; if (s[i] < t[i]) return t; } return s; } } int min(int a, int b) { if (a >= b) return b; else return a; } int n2[41]; int nis[41]; int nia[41]; int mody[41]; int nn; int com(int n, int y) { int ni = 1; for (int i = 0; i < 41; i++) { n2[i] = ni; ni *= 2; } int bunsi = 1, bunbo = 1; rep(0, i, y) bunsi = (bunsi * (n - i)) % mod; rep(0, i, y) bunbo = (bunbo * (i + 1)) % mod; mody[0] = bunbo; rep(1, i, 41) { bunbo = (bunbo * bunbo) % mod; mody[i] = bunbo; } rep(0, i, 41) nis[i] = 0; nn = mod - 2; for (int i = 40; i >= 0; i -= 1) { if (nn > n2[i]) { nis[i]++; nn -= n2[i]; } } nis[0]++; rep(0, i, 41) { if (nis[i] == 1) { bunsi = (bunsi * mody[i]) % mod; } } return bunsi; } int gyakugen(int n, int y) { int ni = 1; for (int i = 0; i < 41; i++) { n2[i] = ni; ni *= 2; } mody[0] = y; rep(1, i, 41) { y = (y * y) % mod; mody[i] = y; } rep(0, i, 41) nis[i] = 0; nn = mod - 2; for (int i = 40; i >= 0; i -= 1) { if (nn > n2[i]) { nis[i]++; nn -= n2[i]; } } nis[0]++; rep(0, i, 41) { if (nis[i] == 1) { n = (n * mody[i]) % mod; } } return n; } int yakuwa(int n) { int sum = 0; rep(1, i, sqrt(n + 1)) { if (n % i == 0) sum += i + n / i; if (i * i == n) sum -= i; } return sum; } bool integer(double n) { if (n == long(n)) return true; else return false; } int poow(int y, int n) { if (n == 0) return 1; n -= 1; int ni = 1; for (int i = 0; i < 41; i++) { n2[i] = ni; ni *= 2; } int yy = y; mody[0] = yy; rep(1, i, 41) { yy = (yy * yy) % mod; mody[i] = yy; } rep(0, i, 41) nis[i] = 0; nn = n; for (int i = 40; i >= 0; i -= 1) { if (nn >= n2[i]) { nis[i]++; nn -= n2[i]; } } rep(0, i, 41) { if (nis[i] == 1) { y = (y * mody[i]) % mod; } } return y; } int minpow(int x, int y) { int sum = 1; rep(0, i, y) sum *= x; return sum; } int ketawa(int x, int sinsuu) { int sum = 0; rep(0, i, 100) sum += (x % poow(sinsuu, i + 1)) / (poow(sinsuu, i)); return sum; } int sankaku(int a) { return a * (a + 1) / 2; } int same(int a[1111111], int n) { int ans = 0; rep(0, i, n) { if (a[i] == a[i + 1]) { int j = i; while (a[j + 1] == a[i] && j <= n - 2) j++; ans += sankaku(j - i); i = j; } } return ans; } bool search(int x) { int n; int a[n]; int l = 0, r = n; while (r - l >= 1) { int i = (l + r) / 2; if (a[i] == x) return true; else if (a[i] < x) l = i + 1; else r = i; } return false; } /*using Graph=vector<vector<int>>; int oya[114514]; int depth[114514]; void dfs(const Graph &G, int v, int p, int d) { depth[v] = d; oya[v]=p; for (auto nv : G[v]) { if (nv == p) continue; // nv が親 p だったらダメ dfs(G, nv, v, d+1); // d を 1 増やして子ノードへ } }*/ /*int H=10,W=10; char field[10][10]; char memo[10][10]; void dfs(int h, int w) { memo[h][w] = 'x'; // 八方向を探索 for (int dh = -1; dh <= 1; ++dh) { for (int dw = -1; dw <= 1; ++dw) { if(abs(0-dh)+abs(0-dw)==2)continue; int nh = h + dh, nw = w + dw; // 場外アウトしたり、0 だったりはスルー if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue; if (memo[nh][nw] == 'x') continue; // 再帰的に探索 dfs(nh, nw); } } }*/ int XOR(int a, int b) { int ni = 1; rep(0, i, 41) { n2[i] = ni; ni *= 2; } rep(0, i, 41) nis[i] = 0; for (int i = 40; i >= 0; i -= 1) { if (a >= n2[i]) { nis[i]++; a -= n2[i]; } if (b >= n2[i]) { nis[i]++; b -= n2[i]; } } int sum = 0; rep(0, i, 41) sum += (nis[i] % 2 * n2[i]); return sum; } // int ma[1024577][21]; // for(int bit=0;bit<(1<<n);bit++)rep(0,i,n)if(bit&(1<<i))ma[bit][i]=1; struct UnionFind { vector<int> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2 UnionFind(int N) : par(N) { // 最初は全てが根であるとして初期化 for (int i = 0; i < N; i++) par[i] = i; } int root(int x) { // データxが属する木の根を再帰で得る:root(x) = {xの木の根} if (par[x] == x) return x; return par[x] = root(par[x]); } void unite(int x, int y) { // xとyの木を併合 int rx = root(x); // xの根をrx int ry = root(y); // yの根をry if (rx == ry) return; // xとyの根が同じ(=同じ木にある)時はそのまま par[rx] = ry; // xとyの根が同じでない(=同じ木にない)時:xの根rxをyの根ryにつける } bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す int rx = root(x); int ry = root(y); return rx == ry; } }; vector<int> a[26]; signed main() { sc(s) sc(t) int m = s.size(); int n = t.size(); rep(0, i, m) a[s[i] - 'a'].push_back(i); rep(0, i, m) sort(a[i].begin(), a[i].end()); int ans = 0; int cnt = 0; rep(0, i, n) { int k = t[i] - 'a'; if (a[k].size() == 0) { c(-1) return 0; } else { if (a[k][a[k].size() - 1] <= ans) { cnt++; ans = a[k][0]; } else { int l = upper_bound(a[k].begin(), a[k].end(), ans) - a[k].begin(); ans = a[k][l]; } } } c(cnt * m + ans + 1) }
#include <algorithm> #include <cmath> #include <iostream> #include <map> #include <math.h> #include <queue> #include <stdio.h> #include <vector> using namespace std; #define int long long #define double long double #define rep(s, i, n) for (int i = s; i < n; i++) #define c(n) cout << n << endl; #define ic(n) \ int n; \ cin >> n; #define sc(s) \ string s; \ cin >> s; #define mod 1000000007 #define inf 1000000000000000007 #define f first #define s second #define mini(c, a, b) *min_element(c + a, c + b) #define maxi(c, a, b) *max_element(c + a, c + b) #define pi 3.141592653589793238462643383279 #define e_ 2.718281828459045235360287471352 #define P pair<int, int> #define upp(a, n, x) upper_bound(a, a + n, x) - a; #define low(a, n, x) lower_bound(a, a + n, x) - a; #define UF UnionFind // printf("%.12Lf\n",); int keta(int x) { rep(0, i, 30) { if (x < 10) { return i + 1; } x = x / 10; } } int gcd(int x, int y) { int aa = x, bb = y; rep(0, i, 1000) { aa = aa % bb; if (aa == 0) { return bb; } bb = bb % aa; if (bb == 0) { return aa; } } } int lcm(int x, int y) { int aa = x, bb = y; rep(0, i, 1000) { aa = aa % bb; if (aa == 0) { return x / bb * y; } bb = bb % aa; if (bb == 0) { return x / aa * y; } } } bool p(int x) { if (x == 1) return false; rep(2, i, sqrt(x) + 1) { if (x % i == 0 && x != i) { return false; } } return true; } int max(int a, int b) { if (a >= b) return a; else return b; } string maxst(string s, string t) { int n = s.size(); int m = t.size(); if (n > m) return s; else if (n < m) return t; else { rep(0, i, n) { if (s[i] > t[i]) return s; if (s[i] < t[i]) return t; } return s; } } int min(int a, int b) { if (a >= b) return b; else return a; } int n2[41]; int nis[41]; int nia[41]; int mody[41]; int nn; int com(int n, int y) { int ni = 1; for (int i = 0; i < 41; i++) { n2[i] = ni; ni *= 2; } int bunsi = 1, bunbo = 1; rep(0, i, y) bunsi = (bunsi * (n - i)) % mod; rep(0, i, y) bunbo = (bunbo * (i + 1)) % mod; mody[0] = bunbo; rep(1, i, 41) { bunbo = (bunbo * bunbo) % mod; mody[i] = bunbo; } rep(0, i, 41) nis[i] = 0; nn = mod - 2; for (int i = 40; i >= 0; i -= 1) { if (nn > n2[i]) { nis[i]++; nn -= n2[i]; } } nis[0]++; rep(0, i, 41) { if (nis[i] == 1) { bunsi = (bunsi * mody[i]) % mod; } } return bunsi; } int gyakugen(int n, int y) { int ni = 1; for (int i = 0; i < 41; i++) { n2[i] = ni; ni *= 2; } mody[0] = y; rep(1, i, 41) { y = (y * y) % mod; mody[i] = y; } rep(0, i, 41) nis[i] = 0; nn = mod - 2; for (int i = 40; i >= 0; i -= 1) { if (nn > n2[i]) { nis[i]++; nn -= n2[i]; } } nis[0]++; rep(0, i, 41) { if (nis[i] == 1) { n = (n * mody[i]) % mod; } } return n; } int yakuwa(int n) { int sum = 0; rep(1, i, sqrt(n + 1)) { if (n % i == 0) sum += i + n / i; if (i * i == n) sum -= i; } return sum; } bool integer(double n) { if (n == long(n)) return true; else return false; } int poow(int y, int n) { if (n == 0) return 1; n -= 1; int ni = 1; for (int i = 0; i < 41; i++) { n2[i] = ni; ni *= 2; } int yy = y; mody[0] = yy; rep(1, i, 41) { yy = (yy * yy) % mod; mody[i] = yy; } rep(0, i, 41) nis[i] = 0; nn = n; for (int i = 40; i >= 0; i -= 1) { if (nn >= n2[i]) { nis[i]++; nn -= n2[i]; } } rep(0, i, 41) { if (nis[i] == 1) { y = (y * mody[i]) % mod; } } return y; } int minpow(int x, int y) { int sum = 1; rep(0, i, y) sum *= x; return sum; } int ketawa(int x, int sinsuu) { int sum = 0; rep(0, i, 100) sum += (x % poow(sinsuu, i + 1)) / (poow(sinsuu, i)); return sum; } int sankaku(int a) { return a * (a + 1) / 2; } int same(int a[1111111], int n) { int ans = 0; rep(0, i, n) { if (a[i] == a[i + 1]) { int j = i; while (a[j + 1] == a[i] && j <= n - 2) j++; ans += sankaku(j - i); i = j; } } return ans; } bool search(int x) { int n; int a[n]; int l = 0, r = n; while (r - l >= 1) { int i = (l + r) / 2; if (a[i] == x) return true; else if (a[i] < x) l = i + 1; else r = i; } return false; } /*using Graph=vector<vector<int>>; int oya[114514]; int depth[114514]; void dfs(const Graph &G, int v, int p, int d) { depth[v] = d; oya[v]=p; for (auto nv : G[v]) { if (nv == p) continue; // nv が親 p だったらダメ dfs(G, nv, v, d+1); // d を 1 増やして子ノードへ } }*/ /*int H=10,W=10; char field[10][10]; char memo[10][10]; void dfs(int h, int w) { memo[h][w] = 'x'; // 八方向を探索 for (int dh = -1; dh <= 1; ++dh) { for (int dw = -1; dw <= 1; ++dw) { if(abs(0-dh)+abs(0-dw)==2)continue; int nh = h + dh, nw = w + dw; // 場外アウトしたり、0 だったりはスルー if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue; if (memo[nh][nw] == 'x') continue; // 再帰的に探索 dfs(nh, nw); } } }*/ int XOR(int a, int b) { int ni = 1; rep(0, i, 41) { n2[i] = ni; ni *= 2; } rep(0, i, 41) nis[i] = 0; for (int i = 40; i >= 0; i -= 1) { if (a >= n2[i]) { nis[i]++; a -= n2[i]; } if (b >= n2[i]) { nis[i]++; b -= n2[i]; } } int sum = 0; rep(0, i, 41) sum += (nis[i] % 2 * n2[i]); return sum; } // int ma[1024577][21]; // for(int bit=0;bit<(1<<n);bit++)rep(0,i,n)if(bit&(1<<i))ma[bit][i]=1; struct UnionFind { vector<int> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2 UnionFind(int N) : par(N) { // 最初は全てが根であるとして初期化 for (int i = 0; i < N; i++) par[i] = i; } int root(int x) { // データxが属する木の根を再帰で得る:root(x) = {xの木の根} if (par[x] == x) return x; return par[x] = root(par[x]); } void unite(int x, int y) { // xとyの木を併合 int rx = root(x); // xの根をrx int ry = root(y); // yの根をry if (rx == ry) return; // xとyの根が同じ(=同じ木にある)時はそのまま par[rx] = ry; // xとyの根が同じでない(=同じ木にない)時:xの根rxをyの根ryにつける } bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す int rx = root(x); int ry = root(y); return rx == ry; } }; vector<int> a[26]; signed main() { sc(s) sc(t) int m = s.size(); int n = t.size(); rep(0, i, m) a[s[i] - 'a'].push_back(i); rep(0, i, 26) sort(a[i].begin(), a[i].end()); int ans = -1; int cnt = 0; rep(0, i, n) { int k = t[i] - 'a'; if (a[k].size() == 0) { c(-1) return 0; } else { if (a[k][a[k].size() - 1] <= ans) { cnt++; ans = a[k][0]; } else { int l = upper_bound(a[k].begin(), a[k].end(), ans) - a[k].begin(); ans = a[k][l]; } } } c(cnt * m + ans + 1) }
replace
341
343
341
343
0
p02937
C++
Time Limit Exceeded
#include <algorithm> #include <bits/stdc++.h> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define rep(X, S, E) for (int(X) = (S); (X) < (E); ++(X)) #define rrep(X, S, E) for (int(X) = (E)-1; (X) >= (S); --(X)) #define itrep(X, Y) for (auto(X) = (Y).begin(); (X) != (Y).end(); (X)++) #define all(X) (X).begin(), (X).end() #define sortDecending(X) sort(all(X), greater<ll>()) // 降順 #define sortAscending(X) sort(all(X)) // 昇順 #define pb push_back #define mp make_pair #define fi first #define sc second #define print(x) cout << x << endl #define printDouble(x) cout << fixed << setprecision(13) << x << endl #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<bool> vb; typedef priority_queue<ll, vl> decendingQueue; // 降順 typedef priority_queue<ll, vl, greater<ll>> ascendingQueue; // 昇順 const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; const ll INF = 9 * 1e18; const ll MOD = 1e9 + 7; long long gcd(long long m, long long n) { if (m < n) return gcd(n, m); if (n == 0) return m; return gcd(n, m % n); } long long lcm(long long m, long long n) { // m * nでlong型のオーバフローを発生させないため、先に割り算から行う return m * (n / gcd(m, n)); } // a + b + cをprimeで割った値を返す long long addMod(long long a, long long b) { return (a + b) % MOD; } long long minusMod(long long a, long long b) { return (a + MOD - b) % MOD; } long long multipleMod(long long a, long long b) { return (a * b) % MOD; } vector<long long> SieveOfEratosthenes(int max) { vector<long long> sieve; vector<long long> primes; for (int i = 1; i < max + 1; ++i) { sieve.push_back(i); } sieve[0] = 0; for (int i = 2; i < max + 1; ++i) { if (sieve[i - 1] != 0) { primes.push_back(sieve[i - 1]); for (int j = 2 * sieve[i - 1]; j < max + 1; j += sieve[i - 1]) { sieve[j - 1] = 0; } } } return primes; } class Combination { private: vector<long long> fac_; vector<long long> finv_; vector<long long> inv_; long long prime_; public: Combination(long long n, long long prime) { fac_ = vector<long long>(n + 1); finv_ = vector<long long>(n + 1); inv_ = vector<long long>(n + 1); prime_ = prime; fac_[0] = fac_[1] = 1; finv_[0] = finv_[1] = 1; inv_[1] = 1; for (long long i = 2; i <= n; i++) { fac_[i] = fac_[i - 1] * i % prime_; inv_[i] = prime_ - inv_[prime_ % i] * (prime_ / i) % prime_; finv_[i] = finv_[i - 1] * inv_[i] % prime_; } } // nCk long long getCombination(long long n, long long k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac_[n] * (finv_[k] * finv_[n - k] % prime_) % prime_; } }; class UnionFindTree { private: vector<int> par; vector<int> rnk; vector<int> siz; public: UnionFindTree(int n) { par.assign(n, -1); rnk.assign(n, -1); siz.assign(n, -1); for (int i = 0; i < n; ++i) { par[i] = i; rnk[i] = 0; siz[i] = 1; } } int find(int x) { if (par[x] == x) return x; else return par[x] = find(par[x]); } bool same(int x, int y) { return find(x) == find(y); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (rnk[x] < rnk[y]) { par[x] = y; siz[y] += siz[x]; } else { par[y] = x; siz[x] += siz[y]; if (rnk[x] == rnk[y]) ++rnk[x]; } } int size(int x) { x = find(x); return siz[x]; } }; class Edge { public: ll from; ll to; ll cost; Edge() {} Edge(ll from, ll to, ll cost) { this->from = from; this->to = to; this->cost = cost; } bool operator<(const Edge &edge) const { return cost < edge.cost; // 昇順 } }; class Graph { public: ll nodes; // ノード数 vector<Edge> edges; Graph() {} Graph(ll nodes) { this->nodes = nodes; } void addEdge(ll from, ll to, ll cost) { this->edges.push_back(Edge(from, to, cost)); } }; // クラスカル法 // 連結グラフの最小全域木を求める class Kruskal { private: Graph graph; vector<Edge> MinimumSpanningTree; ll minimumCost; void searchMinimumSpanningTree() { UnionFindTree uf(graph.nodes); sort(all(graph.edges)); itrep(edge, graph.edges) { if (!uf.same(edge->from, edge->to)) { uf.unite(edge->from, edge->to); MinimumSpanningTree.push_back(*edge); } } } public: Kruskal(Graph graph) { this->graph = graph; } ll getMinimumSpanningTreeCost() { searchMinimumSpanningTree(); ll cost = 0; itrep(it, MinimumSpanningTree) { cost += it->cost; } return cost; } }; // ダイクストラ法 O((E+V)logV) // 最小経路問題を解くためのアルゴリズム。辺の重みに負数を含む場合は利用不可 class Dijkstra { private: Graph graph; map<ll, vector<Edge>> fromPaths; vl distances; vl srcs; public: Dijkstra(Graph graph) { this->graph = graph; itrep(edge, graph.edges) { fromPaths[edge->from].push_back(*edge); fromPaths[edge->to].push_back(Edge(edge->to, edge->from, edge->cost)); } } void searchMinimumPathFrom(ll src) { // 複数回呼ばれる度に計算する this->distances = vl(graph.nodes + 1, INF); this->srcs = vl(graph.nodes + 1, INF); priority_queue<ll> pq; distances[src] = 0; srcs[src] = -1; pq.push(src); while (!pq.empty()) { int u = pq.top(); pq.pop(); itrep(edge, fromPaths[u]) { int v = edge->to; int w = edge->cost; if (distances[v] > distances[u] + w) { distances[v] = distances[u] + w; srcs[v] = u; pq.push(v); } } } }; ll getDistance(ll n) { return this->distances[n]; } ll getFrom(ll n) { return this->srcs[n]; } }; // ベルマンフォード O(|V||E|) // 非負コストが含まれていても最短経路問題を解くためのアルゴリズム。閉路の検出も可能 class BellmanFord { private: Graph graph; // 閉路が含まれるかは個々のノードごとに管理する必要あり vector<bool> hasNegativeCycles; vector<ll> distances; public: BellmanFord(Graph graph) { this->graph = graph; this->distances = vector<ll>(this->graph.nodes + 1, INF); this->hasNegativeCycles = vector<bool>(this->graph.nodes + 1, false); } void searchMinimumPathFrom(ll src) { this->distances[src] = 0; for (ll i = 0; i < graph.nodes - 1; i++) { itrep(edge, graph.edges) { ll u = edge->from; ll v = edge->to; ll w = edge->cost; if (this->distances[u] + w < this->distances[v]) { this->distances[v] = this->distances[u] + w; } } } itrep(edge, graph.edges) { ll u = edge->from; ll v = edge->to; ll w = edge->cost; if (this->distances[u] + w < this->distances[v]) { this->hasNegativeCycles[v] = true; } if (this->hasNegativeCycles[u] == true) { this->hasNegativeCycles[v] = true; } } } ll getDistance(ll n) { return this->distances[n]; } bool hasNegativeCycle(ll n) { return this->hasNegativeCycles[n]; } }; void solve(std::string s, std::string t) { vector<vl> pos(26); rep(i, 0, s.size()) { pos[s[i] - 'a'].push_back(i); } rep(i, 0, 26) { pos[i].push_back(INF); } ll loop = 0; ll npos = -1; rep(i, 0, t.size()) { vl p = pos[t[i] - 'a']; if (pos[t[i] - 'a'].size() == 1) { print(-1); return; } ll next = *upper_bound(all(pos[t[i] - 'a']), npos); if (next < s.size()) { npos = next; } else { loop++; npos = pos[t[i] - 'a'].front(); } } print(loop * s.size() + npos + 1); } int main() { std::string s; std::cin >> s; std::string t; std::cin >> t; solve(s, t); return 0; }
#include <algorithm> #include <bits/stdc++.h> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define rep(X, S, E) for (int(X) = (S); (X) < (E); ++(X)) #define rrep(X, S, E) for (int(X) = (E)-1; (X) >= (S); --(X)) #define itrep(X, Y) for (auto(X) = (Y).begin(); (X) != (Y).end(); (X)++) #define all(X) (X).begin(), (X).end() #define sortDecending(X) sort(all(X), greater<ll>()) // 降順 #define sortAscending(X) sort(all(X)) // 昇順 #define pb push_back #define mp make_pair #define fi first #define sc second #define print(x) cout << x << endl #define printDouble(x) cout << fixed << setprecision(13) << x << endl #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<bool> vb; typedef priority_queue<ll, vl> decendingQueue; // 降順 typedef priority_queue<ll, vl, greater<ll>> ascendingQueue; // 昇順 const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; const ll INF = 9 * 1e18; const ll MOD = 1e9 + 7; long long gcd(long long m, long long n) { if (m < n) return gcd(n, m); if (n == 0) return m; return gcd(n, m % n); } long long lcm(long long m, long long n) { // m * nでlong型のオーバフローを発生させないため、先に割り算から行う return m * (n / gcd(m, n)); } // a + b + cをprimeで割った値を返す long long addMod(long long a, long long b) { return (a + b) % MOD; } long long minusMod(long long a, long long b) { return (a + MOD - b) % MOD; } long long multipleMod(long long a, long long b) { return (a * b) % MOD; } vector<long long> SieveOfEratosthenes(int max) { vector<long long> sieve; vector<long long> primes; for (int i = 1; i < max + 1; ++i) { sieve.push_back(i); } sieve[0] = 0; for (int i = 2; i < max + 1; ++i) { if (sieve[i - 1] != 0) { primes.push_back(sieve[i - 1]); for (int j = 2 * sieve[i - 1]; j < max + 1; j += sieve[i - 1]) { sieve[j - 1] = 0; } } } return primes; } class Combination { private: vector<long long> fac_; vector<long long> finv_; vector<long long> inv_; long long prime_; public: Combination(long long n, long long prime) { fac_ = vector<long long>(n + 1); finv_ = vector<long long>(n + 1); inv_ = vector<long long>(n + 1); prime_ = prime; fac_[0] = fac_[1] = 1; finv_[0] = finv_[1] = 1; inv_[1] = 1; for (long long i = 2; i <= n; i++) { fac_[i] = fac_[i - 1] * i % prime_; inv_[i] = prime_ - inv_[prime_ % i] * (prime_ / i) % prime_; finv_[i] = finv_[i - 1] * inv_[i] % prime_; } } // nCk long long getCombination(long long n, long long k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac_[n] * (finv_[k] * finv_[n - k] % prime_) % prime_; } }; class UnionFindTree { private: vector<int> par; vector<int> rnk; vector<int> siz; public: UnionFindTree(int n) { par.assign(n, -1); rnk.assign(n, -1); siz.assign(n, -1); for (int i = 0; i < n; ++i) { par[i] = i; rnk[i] = 0; siz[i] = 1; } } int find(int x) { if (par[x] == x) return x; else return par[x] = find(par[x]); } bool same(int x, int y) { return find(x) == find(y); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (rnk[x] < rnk[y]) { par[x] = y; siz[y] += siz[x]; } else { par[y] = x; siz[x] += siz[y]; if (rnk[x] == rnk[y]) ++rnk[x]; } } int size(int x) { x = find(x); return siz[x]; } }; class Edge { public: ll from; ll to; ll cost; Edge() {} Edge(ll from, ll to, ll cost) { this->from = from; this->to = to; this->cost = cost; } bool operator<(const Edge &edge) const { return cost < edge.cost; // 昇順 } }; class Graph { public: ll nodes; // ノード数 vector<Edge> edges; Graph() {} Graph(ll nodes) { this->nodes = nodes; } void addEdge(ll from, ll to, ll cost) { this->edges.push_back(Edge(from, to, cost)); } }; // クラスカル法 // 連結グラフの最小全域木を求める class Kruskal { private: Graph graph; vector<Edge> MinimumSpanningTree; ll minimumCost; void searchMinimumSpanningTree() { UnionFindTree uf(graph.nodes); sort(all(graph.edges)); itrep(edge, graph.edges) { if (!uf.same(edge->from, edge->to)) { uf.unite(edge->from, edge->to); MinimumSpanningTree.push_back(*edge); } } } public: Kruskal(Graph graph) { this->graph = graph; } ll getMinimumSpanningTreeCost() { searchMinimumSpanningTree(); ll cost = 0; itrep(it, MinimumSpanningTree) { cost += it->cost; } return cost; } }; // ダイクストラ法 O((E+V)logV) // 最小経路問題を解くためのアルゴリズム。辺の重みに負数を含む場合は利用不可 class Dijkstra { private: Graph graph; map<ll, vector<Edge>> fromPaths; vl distances; vl srcs; public: Dijkstra(Graph graph) { this->graph = graph; itrep(edge, graph.edges) { fromPaths[edge->from].push_back(*edge); fromPaths[edge->to].push_back(Edge(edge->to, edge->from, edge->cost)); } } void searchMinimumPathFrom(ll src) { // 複数回呼ばれる度に計算する this->distances = vl(graph.nodes + 1, INF); this->srcs = vl(graph.nodes + 1, INF); priority_queue<ll> pq; distances[src] = 0; srcs[src] = -1; pq.push(src); while (!pq.empty()) { int u = pq.top(); pq.pop(); itrep(edge, fromPaths[u]) { int v = edge->to; int w = edge->cost; if (distances[v] > distances[u] + w) { distances[v] = distances[u] + w; srcs[v] = u; pq.push(v); } } } }; ll getDistance(ll n) { return this->distances[n]; } ll getFrom(ll n) { return this->srcs[n]; } }; // ベルマンフォード O(|V||E|) // 非負コストが含まれていても最短経路問題を解くためのアルゴリズム。閉路の検出も可能 class BellmanFord { private: Graph graph; // 閉路が含まれるかは個々のノードごとに管理する必要あり vector<bool> hasNegativeCycles; vector<ll> distances; public: BellmanFord(Graph graph) { this->graph = graph; this->distances = vector<ll>(this->graph.nodes + 1, INF); this->hasNegativeCycles = vector<bool>(this->graph.nodes + 1, false); } void searchMinimumPathFrom(ll src) { this->distances[src] = 0; for (ll i = 0; i < graph.nodes - 1; i++) { itrep(edge, graph.edges) { ll u = edge->from; ll v = edge->to; ll w = edge->cost; if (this->distances[u] + w < this->distances[v]) { this->distances[v] = this->distances[u] + w; } } } itrep(edge, graph.edges) { ll u = edge->from; ll v = edge->to; ll w = edge->cost; if (this->distances[u] + w < this->distances[v]) { this->hasNegativeCycles[v] = true; } if (this->hasNegativeCycles[u] == true) { this->hasNegativeCycles[v] = true; } } } ll getDistance(ll n) { return this->distances[n]; } bool hasNegativeCycle(ll n) { return this->hasNegativeCycles[n]; } }; void solve(std::string s, std::string t) { vector<vl> pos(26); rep(i, 0, s.size()) { pos[s[i] - 'a'].push_back(i); } rep(i, 0, 26) { pos[i].push_back(INF); } ll loop = 0; ll npos = -1; rep(i, 0, t.size()) { // vl p = pos[t[i] - 'a']; // TLEになる vl &p = pos[t[i] - 'a']; // 参照渡し if (pos[t[i] - 'a'].size() == 1) { print(-1); return; } ll next = *upper_bound(all(pos[t[i] - 'a']), npos); if (next < s.size()) { npos = next; } else { loop++; npos = pos[t[i] - 'a'].front(); } } print(loop * s.size() + npos + 1); } int main() { std::string s; std::cin >> s; std::string t; std::cin >> t; solve(s, t); return 0; }
replace
329
330
329
331
TLE
p02937
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { string s; string t; cin >> s; cin >> t; const uint s_len = s.size(); const uint t_len = t.size(); vector<int> index['z' + 1]; int last_index['z' + 1]{0}; for (uint i = 0; i < s_len; i++) index[s[i]].push_back(i); uint64_t n = 0; int current_index = -1; for (uint i = 0; i < t_len;) { if (index[t[i]].size() == 0) { cout << -1 << endl; return 0; } if (current_index > index[t[i]].back()) { n += s_len - current_index - 1; current_index = -1; for (uint j = 'a'; j <= 'z'; j++) last_index[j] = 0; } for (uint j = last_index[t[i]]; j < index[t[i]].size(); j++) { if (current_index < index[t[i]][j]) { n += index[t[i]][j] - current_index; current_index = index[t[i]][j]; last_index[t[i]] = j; i++; break; } } } cout << n << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string s; string t; cin >> s; cin >> t; const uint s_len = s.size(); const uint t_len = t.size(); vector<int> index['z' + 1]; int last_index['z' + 1]{0}; for (uint i = 0; i < s_len; i++) index[s[i]].push_back(i); uint64_t n = 0; int current_index = -1; for (uint i = 0; i < t_len;) { if (index[t[i]].size() == 0) { cout << -1 << endl; return 0; } if (current_index >= index[t[i]].back()) { n += s_len - current_index - 1; current_index = -1; for (uint j = 'a'; j <= 'z'; j++) last_index[j] = 0; } for (uint j = last_index[t[i]]; j < index[t[i]].size(); j++) { if (current_index < index[t[i]][j]) { n += index[t[i]][j] - current_index; current_index = index[t[i]][j]; last_index[t[i]] = j; i++; break; } } } cout << n << endl; return 0; }
replace
26
27
26
27
TLE
p02937
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; typedef long long ll; typedef pair<ll, double> P; typedef tuple<ll, ll, ll> T; const long long INF = 1LL << 60; const int MOD = 1000000000 + 7; #define rev(s) (string((s).rbegin(), (s).rend())) template <typename T> inline string toString(const T &a) { ostringstream oss; oss << a; return oss.str(); }; // cout << fixed << setprecision(10) << ans << endl; 有効桁数指定 // *min_element(c + l, c + r) *max_element(c + l, c + r) 配列の中のmin-max // int dx[8]={1,1,0,-1,-1,-1,0,1}; // int dy[8]={0,1,1,1,0,-1,-1,-1}; // int dx[4]={1,0,-1,0}; // int dy[4]={0,1,0,-1}; // ~ は、-1の時だけfalse int b_search(vector<int> &v, int n) { int l = -1, r = v.size(), mid = (r + l) / 2; while (r - l > 1) { if (v[mid] >= n) r = mid; else l = mid; } return r; } int main() { string s, t; cin >> s >> t; vector<int> dic[26]; rep(i, s.size()) { dic[s[i] - 'a'].push_back(i); } ll cnt = 0, pos = 0; rep(i, t.size()) { if (dic[t[i] - 'a'].size() == 0) { cout << -1 << endl; return 0; } if (pos > dic[t[i] - 'a'][(dic[t[i] - 'a'].size() - 1)]) { cnt++; pos = dic[t[i] - 'a'][0] + 1; } else { // cout<<t[i]<<" "<<b_search(dic[t[i]-'a'],pos)<<endl; pos = dic[t[i] - 'a'][b_search(dic[t[i] - 'a'], pos)] + 1; } // cout<<"pos "<<pos<<endl; } ll ans = cnt * s.size() + pos; cout << ans << endl; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; typedef long long ll; typedef pair<ll, double> P; typedef tuple<ll, ll, ll> T; const long long INF = 1LL << 60; const int MOD = 1000000000 + 7; #define rev(s) (string((s).rbegin(), (s).rend())) template <typename T> inline string toString(const T &a) { ostringstream oss; oss << a; return oss.str(); }; // cout << fixed << setprecision(10) << ans << endl; 有効桁数指定 // *min_element(c + l, c + r) *max_element(c + l, c + r) 配列の中のmin-max // int dx[8]={1,1,0,-1,-1,-1,0,1}; // int dy[8]={0,1,1,1,0,-1,-1,-1}; // int dx[4]={1,0,-1,0}; // int dy[4]={0,1,0,-1}; // ~ は、-1の時だけfalse int b_search(vector<int> &v, int n) { int l = -1, r = v.size(), mid = (r + l) / 2; while (r - l > 1) { if (v[mid] >= n) r = mid; else l = mid; } return r; } int main() { string s, t; cin >> s >> t; vector<int> dic[26]; rep(i, s.size()) { dic[s[i] - 'a'].push_back(i); } ll cnt = 0, pos = 0; rep(i, t.size()) { if (dic[t[i] - 'a'].size() == 0) { cout << -1 << endl; return 0; } if (pos > dic[t[i] - 'a'][(dic[t[i] - 'a'].size() - 1)]) { cnt++; pos = dic[t[i] - 'a'][0] + 1; } else { // cout<<t[i]<<" "<<b_search(dic[t[i]-'a'],pos)<<endl; // pos = dic[t[i]-'a'][b_search(dic[t[i]-'a'],pos)] + 1; // pos = // dic[t[i]-'a'][int(lower_bound(dic[t[i]-'a'].begin(),dic[t[i]-'a'].end(),pos))] // + 1; auto it = lower_bound(dic[t[i] - 'a'].begin(), dic[t[i] - 'a'].end(), pos); pos = *it + 1; } // cout<<"pos "<<pos<<endl; } ll ans = cnt * s.size() + pos; cout << ans << endl; }
replace
49
50
49
56
TLE
p02937
C++
Runtime Error
// Bismillahirrahmanirrahim #include <bits/stdc++.h> using namespace std; string s, t; vector<int> harf_indexs[26]; void cons() { for (int i = 0; i < s.size(); i++) { int m_harf = s[i] - 'a'; harf_indexs[m_harf].push_back(i); } } int bin_search(int m_harf, int k) { int l = 0, r = harf_indexs[m_harf].size() - 1; while (l < r) { int mid = (l + r) / 2; if (harf_indexs[m_harf][mid] > k) { r = mid; } else { l = mid + 1; } } return harf_indexs[m_harf][l]; } int main() { cin >> s >> t; cons(); int k = -1; int s_sayisi = 0; for (int i = 0; i < t.size(); i++) { int m_harf = t[i] - 'a'; if (harf_indexs[m_harf].empty()) { cout << -1 << endl; return 0; } if (harf_indexs[m_harf].back() <= k) { s_sayisi++; k = harf_indexs[m_harf][0]; continue; } k = bin_search(m_harf, k); } cout << s_sayisi * s.size() + k + 1 << endl; return 9; }
// Bismillahirrahmanirrahim #include <bits/stdc++.h> using namespace std; string s, t; vector<int> harf_indexs[26]; void cons() { for (int i = 0; i < s.size(); i++) { int m_harf = s[i] - 'a'; harf_indexs[m_harf].push_back(i); } } int bin_search(int m_harf, int k) { int l = 0, r = harf_indexs[m_harf].size() - 1; while (l < r) { int mid = (l + r) / 2; if (harf_indexs[m_harf][mid] > k) { r = mid; } else { l = mid + 1; } } return harf_indexs[m_harf][l]; } int main() { cin >> s >> t; cons(); int k = -1; int s_sayisi = 0; for (int i = 0; i < t.size(); i++) { int m_harf = t[i] - 'a'; if (harf_indexs[m_harf].empty()) { cout << -1 << endl; return 0; } if (harf_indexs[m_harf].back() <= k) { s_sayisi++; k = harf_indexs[m_harf][0]; continue; } k = bin_search(m_harf, k); } cout << s_sayisi * s.size() + k + 1 << endl; return 0; }
replace
45
46
45
46
9
p02937
C++
Runtime Error
#include <cmath> #include <fstream> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> using std::vector; template <typename T> void ndarray(vector<T> &vec, int len) { vec.resize(len); } template <typename T, typename... Args> void ndarray(vector<T> &vec, int len, Args... args) { vec.resize(len); for (auto &v : vec) ndarray(v, args...); } int main() { // std::ifstream in("in01.txt"); // std::cin.rdbuf(in.rdbuf()); std::string s, t; std::cin >> s >> t; long long ans = 0; int n = s.length(); bool flag = true; vector<std::set<int>> chlist; chlist.resize(26); for (int i = 1; i <= 2 * n; i++) { int ch = s[(2 * n - i) % n] - 'a'; chlist[ch].insert(2 * n - i + 1); } vector<vector<long long>> dp; ndarray(dp, 26, 2 * n + 1); for (int ch = 0; ch < 26; ch++) { auto itr = chlist[ch].upper_bound(0); if (itr == chlist[ch].end()) { dp[ch][0] = -1; } else { dp[ch][0] = *itr; } } for (int next = 1; next <= 2 * n + 1; next++) { for (int ch = 0; ch < 26; ch++) { auto itr = chlist[ch].upper_bound(next); if (itr == chlist[ch].end()) { dp[ch][next] = -1; } else { dp[ch][next] = *itr - next; } } } int next = 0; for (auto ch : t) { long long add = dp[ch - 'a'][next]; if (add == -1) { flag = false; break; } else { ans += add; } next = (next + add - 1) % n + 1; // std::cout << add << ", " << next << ", " << ans << std::endl; } if (flag) { std::cout << ans << std::endl; } else { std::cout << -1 << std::endl; } return 0; }
#include <cmath> #include <fstream> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> using std::vector; template <typename T> void ndarray(vector<T> &vec, int len) { vec.resize(len); } template <typename T, typename... Args> void ndarray(vector<T> &vec, int len, Args... args) { vec.resize(len); for (auto &v : vec) ndarray(v, args...); } int main() { // std::ifstream in("in01.txt"); // std::cin.rdbuf(in.rdbuf()); std::string s, t; std::cin >> s >> t; long long ans = 0; int n = s.length(); bool flag = true; vector<std::set<int>> chlist; chlist.resize(26); for (int i = 1; i <= 2 * n; i++) { int ch = s[(2 * n - i) % n] - 'a'; chlist[ch].insert(2 * n - i + 1); } vector<vector<long long>> dp; ndarray(dp, 26, 2 * n + 1); for (int ch = 0; ch < 26; ch++) { auto itr = chlist[ch].upper_bound(0); if (itr == chlist[ch].end()) { dp[ch][0] = -1; } else { dp[ch][0] = *itr; } } for (int next = 1; next <= 2 * n; next++) { for (int ch = 0; ch < 26; ch++) { auto itr = chlist[ch].upper_bound(next); if (itr == chlist[ch].end()) { dp[ch][next] = -1; } else { dp[ch][next] = *itr - next; } } } int next = 0; for (auto ch : t) { long long add = dp[ch - 'a'][next]; if (add == -1) { flag = false; break; } else { ans += add; } next = (next + add - 1) % n + 1; // std::cout << add << ", " << next << ", " << ans << std::endl; } if (flag) { std::cout << ans << std::endl; } else { std::cout << -1 << std::endl; } return 0; }
replace
46
47
46
47
-6
malloc(): corrupted top size
p02937
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cassert> #include <cfloat> #include <cmath> #include <cstdio> #include <cstring> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <limits.h> #include <list> #include <map> #include <math.h> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } using namespace std; #define int long long #define ll long long #define rep(i, n) for (ll i = 0; i < (n); i++) #define P pair<ll, ll> #define sz(x) (ll) x.size() #define ALL(x) (x).begin(), (x).end() #define ALLR(x) (x).rbegin(), (x).rend() #define VE vector<ll> #define COUT(x) cout << (x) << endl #define MA map<ll, ll> #define SE set<ll> #define PQ priority_queue<ll> #define PQR priority_queue<ll, VE, greater<ll>> #define YES(n) cout << ((n) ? "YES" : "NO") << endl #define Yes(n) cout << ((n) ? "Yes" : "No") << endl #define EPS (1e-14) #define pb push_back long long MOD = 1000000007; // const long long MOD = 998244353; const long long INF = 1LL << 60; const double PI = acos(-1.0); struct mint { ll x; // typedef long long ll; mint(ll x = 0) : x((x % MOD + MOD) % MOD) {} mint operator-() const { return mint(-x); } mint &operator+=(const mint a) { if ((x += a.x) >= MOD) x -= MOD; return *this; } mint &operator-=(const mint a) { if ((x += MOD - a.x) >= MOD) x -= MOD; return *this; } mint &operator*=(const mint a) { (x *= a.x) %= MOD; return *this; } mint operator+(const mint a) const { mint res(*this); return res += a; } mint operator-(const mint a) const { mint res(*this); return res -= a; } mint operator*(const mint a) const { mint res(*this); return res *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime MOD mint inv() const { return pow(MOD - 2); } mint &operator/=(const mint a) { return (*this) *= a.inv(); } mint operator/(const mint a) const { mint res(*this); return res /= a; } }; istream &operator>>(istream &is, const mint &a) { return is >> a.x; } ostream &operator<<(ostream &os, const mint &a) { return os << a.x; } struct combination { vector<mint> fact, ifact; combination(int n) : fact(n + 1), ifact(n + 1) { assert(n < MOD); fact[0] = 1; for (int i = 1; i <= n; ++i) fact[i] = fact[i - 1] * i; ifact[n] = fact[n].inv(); for (int i = n; i >= 1; --i) ifact[i - 1] = ifact[i] * i; } mint operator()(int n, int k) { if (k < 0 || k > n) return 0; return fact[n] * ifact[k] * ifact[n - k]; } }; struct Sieve { int n; vector<int> f, primes; Sieve(int n = 1) : n(n), f(n + 1) { f[0] = f[1] = -1; for (ll i = 2; i <= n; ++i) { if (f[i]) continue; primes.push_back(i); f[i] = i; for (ll j = i * i; j <= n; j += i) { if (!f[j]) f[j] = i; } } } bool isPrime(int x) { return f[x] == x; } vector<int> factorList(int x) { vector<int> res; while (x != 1) { res.push_back(f[x]); x /= f[x]; } return res; } vector<P> factor(int x) { vector<int> fl = factorList(x); if (fl.size() == 0) return {}; vector<P> res(1, P(fl[0], 0)); for (int p : fl) { if (res.back().first == p) { res.back().second++; } else { res.emplace_back(p, 1); } } return res; } }; template <class t> t gcd(t a, t b) { return b != 0 ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { ll g = gcd(a, b); return a / g * b; } bool prime(ll n) { for (ll i = 2; i <= sqrt(n); i++) { if (n % i == 0) return false; } return n != 1; } map<ll, ll> prime_factor(ll n) { map<ll, ll> ret; for (ll i = 2; i * i <= n; i++) { while (n % i == 0) { ret[i]++; n /= i; } } if (n != 1) ret[n] = 1; return ret; } ll modinv(ll a, ll m) { ll b = m, u = 1, v = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } vector<pair<char, int>> RunLength(string s) { if (s.size() == 0) return {}; vector<pair<char, int>> res(1, pair<char, int>(s[0], 0)); for (char p : s) { if (res.back().first == p) { res.back().second++; } else { res.emplace_back(p, 1); } } return res; } // Digit Count int GetDigit(int num) { return log10(num) + 1; } // bit calculation[how many "1"] (= __builtin_popcount()) int bit_count(int n) { int cnt = 0; while (n > 0) { if (n % 2 == 1) cnt++; n /= 2; } return cnt; } vector<long long> enum_divisors(long long N) { vector<long long> res; for (long long i = 1; i * i <= N; ++i) { if (N % i == 0) { res.push_back(i); if (N / i != i) res.push_back(N / i); } } sort(res.begin(), res.end()); return res; } const ll dx[4] = {1, 0, -1, 0}; const ll dy[4] = {0, 1, 0, -1}; struct edge { ll to, cost; }; typedef long double ld; using Graph = vector<VE>; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // cout << fixed << setprecision(15); // combination com(200010); string s, t; cin >> s >> t; int n = s.size(); vector<set<int>> a(26); rep(i, n) { a[s[i] - 'a'].insert(i); a[s[i] - 'a'].insert(i + n); } int ans = 0; int now = 0; rep(i, t.size()) { int tmp = t[i] - 'a'; if (a[tmp].size() == 0) { cout << -1 << endl; return 0; } now = *lower_bound(ALL(a[tmp]), now) + 1; if (now >= n) { now -= n; ans += n; } } ans += now; cout << ans << endl; return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <cfloat> #include <cmath> #include <cstdio> #include <cstring> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <limits.h> #include <list> #include <map> #include <math.h> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } using namespace std; #define int long long #define ll long long #define rep(i, n) for (ll i = 0; i < (n); i++) #define P pair<ll, ll> #define sz(x) (ll) x.size() #define ALL(x) (x).begin(), (x).end() #define ALLR(x) (x).rbegin(), (x).rend() #define VE vector<ll> #define COUT(x) cout << (x) << endl #define MA map<ll, ll> #define SE set<ll> #define PQ priority_queue<ll> #define PQR priority_queue<ll, VE, greater<ll>> #define YES(n) cout << ((n) ? "YES" : "NO") << endl #define Yes(n) cout << ((n) ? "Yes" : "No") << endl #define EPS (1e-14) #define pb push_back long long MOD = 1000000007; // const long long MOD = 998244353; const long long INF = 1LL << 60; const double PI = acos(-1.0); struct mint { ll x; // typedef long long ll; mint(ll x = 0) : x((x % MOD + MOD) % MOD) {} mint operator-() const { return mint(-x); } mint &operator+=(const mint a) { if ((x += a.x) >= MOD) x -= MOD; return *this; } mint &operator-=(const mint a) { if ((x += MOD - a.x) >= MOD) x -= MOD; return *this; } mint &operator*=(const mint a) { (x *= a.x) %= MOD; return *this; } mint operator+(const mint a) const { mint res(*this); return res += a; } mint operator-(const mint a) const { mint res(*this); return res -= a; } mint operator*(const mint a) const { mint res(*this); return res *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime MOD mint inv() const { return pow(MOD - 2); } mint &operator/=(const mint a) { return (*this) *= a.inv(); } mint operator/(const mint a) const { mint res(*this); return res /= a; } }; istream &operator>>(istream &is, const mint &a) { return is >> a.x; } ostream &operator<<(ostream &os, const mint &a) { return os << a.x; } struct combination { vector<mint> fact, ifact; combination(int n) : fact(n + 1), ifact(n + 1) { assert(n < MOD); fact[0] = 1; for (int i = 1; i <= n; ++i) fact[i] = fact[i - 1] * i; ifact[n] = fact[n].inv(); for (int i = n; i >= 1; --i) ifact[i - 1] = ifact[i] * i; } mint operator()(int n, int k) { if (k < 0 || k > n) return 0; return fact[n] * ifact[k] * ifact[n - k]; } }; struct Sieve { int n; vector<int> f, primes; Sieve(int n = 1) : n(n), f(n + 1) { f[0] = f[1] = -1; for (ll i = 2; i <= n; ++i) { if (f[i]) continue; primes.push_back(i); f[i] = i; for (ll j = i * i; j <= n; j += i) { if (!f[j]) f[j] = i; } } } bool isPrime(int x) { return f[x] == x; } vector<int> factorList(int x) { vector<int> res; while (x != 1) { res.push_back(f[x]); x /= f[x]; } return res; } vector<P> factor(int x) { vector<int> fl = factorList(x); if (fl.size() == 0) return {}; vector<P> res(1, P(fl[0], 0)); for (int p : fl) { if (res.back().first == p) { res.back().second++; } else { res.emplace_back(p, 1); } } return res; } }; template <class t> t gcd(t a, t b) { return b != 0 ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { ll g = gcd(a, b); return a / g * b; } bool prime(ll n) { for (ll i = 2; i <= sqrt(n); i++) { if (n % i == 0) return false; } return n != 1; } map<ll, ll> prime_factor(ll n) { map<ll, ll> ret; for (ll i = 2; i * i <= n; i++) { while (n % i == 0) { ret[i]++; n /= i; } } if (n != 1) ret[n] = 1; return ret; } ll modinv(ll a, ll m) { ll b = m, u = 1, v = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } vector<pair<char, int>> RunLength(string s) { if (s.size() == 0) return {}; vector<pair<char, int>> res(1, pair<char, int>(s[0], 0)); for (char p : s) { if (res.back().first == p) { res.back().second++; } else { res.emplace_back(p, 1); } } return res; } // Digit Count int GetDigit(int num) { return log10(num) + 1; } // bit calculation[how many "1"] (= __builtin_popcount()) int bit_count(int n) { int cnt = 0; while (n > 0) { if (n % 2 == 1) cnt++; n /= 2; } return cnt; } vector<long long> enum_divisors(long long N) { vector<long long> res; for (long long i = 1; i * i <= N; ++i) { if (N % i == 0) { res.push_back(i); if (N / i != i) res.push_back(N / i); } } sort(res.begin(), res.end()); return res; } const ll dx[4] = {1, 0, -1, 0}; const ll dy[4] = {0, 1, 0, -1}; struct edge { ll to, cost; }; typedef long double ld; using Graph = vector<VE>; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // cout << fixed << setprecision(15); // combination com(200010); string s, t; cin >> s >> t; int n = s.size(); vector<VE> a(26); rep(i, n) { a[s[i] - 'a'].push_back(i); } rep(i, n) a[s[i] - 'a'].push_back(i + n); int ans = 0; int now = 0; rep(i, t.size()) { int tmp = t[i] - 'a'; if (a[tmp].size() == 0) { cout << -1 << endl; return 0; } now = *lower_bound(ALL(a[tmp]), now) + 1; if (now >= n) { now -= n; ans += n; } } ans += now; cout << ans << endl; return 0; }
replace
269
274
269
272
TLE
p02937
C++
Runtime Error
#include <cstdio> #include <cstring> #define maxn 100005 #define ll long long using namespace std; char s[maxn * 2]; int last[maxn * 2][26], v[26]; int main() { scanf("%s", s + 1); int n = strlen(s + 1); for (int i = 1; i <= n; i++) s[i + n] = s[i]; for (int i = 2 * n; i >= 0; i--) { for (int j = 0; j < 26; j++) last[i][j] = v[j]; v[s[i] - 'a'] = i; } scanf("%s", s + 1); int m = strlen(s + 1); int now = last[0][s[1] - 'a']; ll ans = now; for (int i = 2; i <= m; i++) { if (!now) return printf("-1\n"), 0; ans += last[now][s[i] - 'a'] - now; now = last[now][s[i] - 'a']; if (now > n) now -= n; } if (!now) ans = -1; printf("%lld\n", ans); return 0; }
#include <cstdio> #include <cstring> #define maxn 100005 #define ll long long using namespace std; char s[maxn * 2]; int last[maxn * 2][26], v[26]; int main() { scanf("%s", s + 1); int n = strlen(s + 1); for (int i = 1; i <= n; i++) s[i + n] = s[i]; for (int i = 2 * n; i >= 0; i--) { for (int j = 0; j < 26; j++) last[i][j] = v[j]; if (i) v[s[i] - 'a'] = i; } scanf("%s", s + 1); int m = strlen(s + 1); int now = last[0][s[1] - 'a']; ll ans = now; for (int i = 2; i <= m; i++) { if (!now) return printf("-1\n"), 0; ans += last[now][s[i] - 'a'] - now; now = last[now][s[i] - 'a']; if (now > n) now -= n; } if (!now) ans = -1; printf("%lld\n", ans); return 0; }
replace
15
16
15
17
0
p02937
C++
Runtime Error
#include <bits/stdc++.h> // g++ -std=c++11 using namespace std; typedef long long ll; #define rep(i, n) for (long long i = 0; i < (n); ++i) #define DIV 1000000007 // 10^9+7 ll memo[30]; ll memo2[100005][30]; int main() { string s, t; cin >> s >> t; string ss = s + s; rep(i, 30) memo[i] = -1; for (ll i = ss.size() - 1; i >= 0; i--) { rep(j, 30) { if (memo[j] != -1) { memo2[i][j] = memo[j] - i; } else { memo2[i][j] = -1; } } ll ch = ss[i] - 'a'; memo[ch] = i; } ll cur = -1; rep(i, s.size()) { if (t[0] == s[i]) { cur = i; break; } } if (cur == -1) { cout << "-1" << endl; return 0; } for (ll i = 1; i < t.size(); i++) { ll ch = t[i] - 'a'; if (memo2[cur % s.size()][ch] == -1) { cout << "-1" << endl; return 0; } cur += memo2[cur % s.size()][ch]; } cout << cur + 1 << endl; }
#include <bits/stdc++.h> // g++ -std=c++11 using namespace std; typedef long long ll; #define rep(i, n) for (long long i = 0; i < (n); ++i) #define DIV 1000000007 // 10^9+7 ll memo[30]; ll memo2[200005][30]; int main() { string s, t; cin >> s >> t; string ss = s + s; rep(i, 30) memo[i] = -1; for (ll i = ss.size() - 1; i >= 0; i--) { rep(j, 30) { if (memo[j] != -1) { memo2[i][j] = memo[j] - i; } else { memo2[i][j] = -1; } } ll ch = ss[i] - 'a'; memo[ch] = i; } ll cur = -1; rep(i, s.size()) { if (t[0] == s[i]) { cur = i; break; } } if (cur == -1) { cout << "-1" << endl; return 0; } for (ll i = 1; i < t.size(); i++) { ll ch = t[i] - 'a'; if (memo2[cur % s.size()][ch] == -1) { cout << "-1" << endl; return 0; } cur += memo2[cur % s.size()][ch]; } cout << cur + 1 << endl; }
replace
10
11
10
11
0
p02937
C++
Runtime Error
const int mod1 = 1000000007, mod2 = 998244353; const int mod = mod1; #include <bits/stdc++.h> #include <type_traits> using namespace std; // Andrew He's modular-arithmetic class template <int MOD_> struct modnum { static constexpr int MOD = MOD_; static_assert(MOD_ > 0, "MOD must be positive"); private: using ll = long long; int v; static int minv(int a, int m) { a %= m; assert(a); return a == 1 ? 1 : int(m - ll(minv(m, a)) * ll(m) / a); } public: modnum() : v(0) {} modnum(ll v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; } explicit operator int() const { return v; } friend std::ostream &operator<<(std::ostream &out, const modnum &n) { return out << int(n); } friend std::istream &operator>>(std::istream &in, modnum &n) { ll v_; in >> v_; n = modnum(v_); return in; } friend bool operator==(const modnum &a, const modnum &b) { return a.v == b.v; } friend bool operator!=(const modnum &a, const modnum &b) { return a.v != b.v; } modnum inv() const { modnum res; res.v = minv(v, MOD); return res; } modnum neg() const { modnum res; res.v = v ? MOD - v : 0; return res; } modnum operator-() const { return neg(); } modnum operator+() const { return modnum(*this); } modnum &operator++() { v++; if (v == MOD) v = 0; return *this; } modnum &operator--() { if (v == 0) v = MOD; v--; return *this; } modnum &operator+=(const modnum &o) { v += o.v; if (v >= MOD) v -= MOD; return *this; } modnum &operator-=(const modnum &o) { v -= o.v; if (v < 0) v += MOD; return *this; } modnum &operator*=(const modnum &o) { v = int(ll(v) * ll(o.v) % MOD); return *this; } modnum &operator/=(const modnum &o) { return *this *= o.inv(); } friend modnum operator++(modnum &a, int) { modnum r = a; ++a; return r; } friend modnum operator--(modnum &a, int) { modnum r = a; --a; return r; } friend modnum operator+(const modnum &a, const modnum &b) { return modnum(a) += b; } friend modnum operator-(const modnum &a, const modnum &b) { return modnum(a) -= b; } friend modnum operator*(const modnum &a, const modnum &b) { return modnum(a) *= b; } friend modnum operator/(const modnum &a, const modnum &b) { return modnum(a) /= b; } friend modnum pow(modnum x, size_t n) { modnum res = 1; while (n) { if (n & 1) res *= x; x *= x; n /= 2; } return res; } }; struct Modnum { private: static int MOD; // declaration, not definition using ll = long long; using modnum = Modnum; int v; static int minv(int a, int m) { a %= m; assert(a); return a == 1 ? 1 : int(m - ll(minv(m, a)) * ll(m) / a); } public: Modnum() : v(0) {} Modnum(ll v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; } static void set_mod(const int &m) { assert(m > 0); MOD = m; } explicit operator int() const { return v; } friend std::ostream &operator<<(std::ostream &out, const modnum &n) { return out << int(n); } friend std::istream &operator>>(std::istream &in, modnum &n) { ll v_; in >> v_; n = modnum(v_); return in; } friend bool operator==(const modnum &a, const modnum &b) { return a.v == b.v; } friend bool operator!=(const modnum &a, const modnum &b) { return a.v != b.v; } modnum inv() const { modnum res; res.v = minv(v, MOD); return res; } modnum neg() const { modnum res; res.v = v ? MOD - v : 0; return res; } modnum operator-() const { return neg(); } modnum operator+() const { return *this; } modnum &operator++() { v++; if (v == MOD) v = 0; return *this; } modnum &operator--() { if (v == 0) v = MOD; v--; return *this; } modnum &operator+=(const modnum &o) { v += o.v; if (v >= MOD) v -= MOD; return *this; } modnum &operator-=(const modnum &o) { v -= o.v; if (v < 0) v += MOD; return *this; } modnum &operator*=(const modnum &o) { v = int(ll(v) * ll(o.v) % MOD); return *this; } modnum &operator/=(const modnum &o) { return *this *= o.inv(); } friend modnum operator++(modnum &a, int) { modnum r = a; ++a; return r; } friend modnum operator--(modnum &a, int) { modnum r = a; --a; return r; } friend modnum operator+(const modnum &a, const modnum &b) { return modnum(a) += b; } friend modnum operator-(const modnum &a, const modnum &b) { return modnum(a) -= b; } friend modnum operator*(const modnum &a, const modnum &b) { return modnum(a) *= b; } friend modnum operator/(const modnum &a, const modnum &b) { return modnum(a) /= b; } }; int Modnum::MOD = 1; // definition #define sq(x) (x) * (x) // square #define FAST_READ \ ios::sync_with_stdio(false); \ cin.tie(nullptr); #ifdef LOCAL #define see(x) cout << "<DBG> " << #x << ": " << (x) << endl #endif #ifndef LOCAL #define see(x) #endif #define LSON(x) ((x)*2) #define RSON(x) ((x)*2 + 1) template <typename A, typename B> void Min(A &a, const B &b) { if (b < a) a = b; } template <typename A, typename B> void Max(A &a, const B &b) { if (b > a) a = b; } int cas; ostream &kase() { return cout << "Case #" << ++cas << ": "; } #if __cplusplus < 201402L template <class Iterator> std::reverse_iterator<Iterator> make_reverse_iterator(Iterator it) { return std::reverse_iterator<Iterator>(it); } #endif template <typename iter_t> struct iter_pair { iter_t _beg, _end; iter_t begin() { return _beg; } iter_t end() { return _end; } }; template <class cont> iter_pair<reverse_iterator<decltype(begin(declval<cont>()))>> reverse(cont &&r) { return {make_reverse_iterator(end(r)), make_reverse_iterator(begin(r))}; } template <typename T> void dprintln(const T &t) { cout << t << endl; } // for debug use template <typename T, typename... Args> void dprintln(const T &t, const Args &...rest) { cout << t << ' '; dprintln(rest...); } template <typename T> void println(const T &t) { cout << t << '\n'; } template <typename T, typename... Args> void println(const T &t, const Args &...rest) { cout << t << ' '; println(rest...); } template <typename T> void println(const vector<T> &vec) { if (!vec.empty()) { cout << vec[0]; for (size_t i = 1; i < vec.size(); ++i) cout << ' ' << vec[i]; } cout << endl; } template <typename T> void print(const T &t) { cout << t << ' '; } template <typename T, typename... Args> void print(const T &t, const Args &...rest) { cout << t << ' '; print(rest...); } // this overload is chosen when there's only one argument template <class T> void scan(T &t) { cin >> t; } template <class T, class... Args> void scan(T &a, Args &...rest) { cin >> a; scan(rest...); } template <typename T> void scan(vector<T> &vec) { for (T &x : vec) scan(x); } using ull = unsigned long long; using ll = long long; using ul = unsigned long; using vl = vector<ll>; using vi = vector<int>; using pii = pair<int, int>; using pip = pair<int, pii>; using pll = pair<ll, ll>; using vb = vector<bool>; using vpii = vector<pii>; using ldb = long double; template <typename int_t> inline int_t lowbit(int_t x) { return x & -x; } #define rng(i, a, b) for (int i = (int)(a); i < (int)(b); ++i) #define up(i, a, b) for (int i = int(a); i <= int(b); ++i) #define down(i, b, a) for (int i = int(b); i >= int(a); i--) #define rep(n) for (int i##n = 0, _##n = (int)n; i##n < _##n; i##n++) #define stp(i, a, b, c) for (int i = (a); i < (b); i += (c)) #define FOR(x, cont) for (const auto &x : cont) #define INC(init, x, y) for (init; x <= y; ++x) #define For(x, cont) for (auto &x : cont) #define all(x) begin(x), end(x) #define pb push_back #define mp make_pair #define eb emplace_back #define SZ(x) (int)(x).size() #define UP(i, l, r) for (i = decltype(i)(l); i <= decltype(i)(r); ++i) #define DOWN(i, r, l) for (i = decltype(i)(r); i >= decltype(i)(l); --i) #define Dec(a, b) for (; a >= b; --a) template <typename T, typename Comp = less<T>> using pq = priority_queue<T, vector<T>, Comp>; #define popcnt(x) __builtin_popcountll((x)) #define SET(arr, v) memset(arr, (v), sizeof(arr)) #define UNIQ(vec) (vec).erase(unique(all(vec)), end(vec)) #define LB(cont, x) int(lower_bound(all(cont), x) - begin(cont)) #define UB(cont, x) int(upper_bound(all(cont), x) - begin(cont)) #define AI(name, n, m) vv<int> name(n, vi(m)); #define AL(name, n, m) vv<ll> name(size_t(n), vl(size_t(m))); #define set0(arr) memset(arr, 0, sizeof arr) #define set1(arr) memset(arr, -1, sizeof arr) #define AT(T, n, m, a) vector<vector<T>> a(n, vector<T>(m)) const int dx[] = {0, 0, -1, 1}, dy[] = {-1, 1, 0, 0}; auto bet = [](const ll x, const ll y, const ll i) { return x <= i && i <= y; }; template <typename T1, typename T2> T1 ceil(T1 x, T2 y) { // y >= 1,是整数。需要注意 x + y - 1 是否会溢出 return (x + y - 1) / y; } inline int h_bit(unsigned long long x) { return sizeof(unsigned long long) * 8 - 1 - __builtin_clzll(x); } int pow2(int x) { // power of 2 return 1 << (h_bit((ull)x) + (x != lowbit(x))); } template <typename T> struct bit { vector<T> a; function<T(T, T)> bin_op; const T init; explicit bit(int n, function<T(T, T)> bin_op, T init) : bin_op(bin_op), init(init) { a.assign(n + 1, init); } T prefix(T x) { auto res = init; while (x) { res = bin_op(a[x], res); x -= x & -x; } return res; } void modify(int x, T v) { while (x < (int)a.size()) { a[x] = bin_op(a[x], v); x += x & -x; } } void clear() { fill(a.begin(), a.end(), init); } }; template <typename T> struct r_bit { vector<T> a; function<T(T, T)> bin_op; const T init; explicit r_bit(int n, function<T(T, T)> bin_op, T init) : bin_op(move(bin_op)), init(init) { a.ssign(n + 1, init); } T suffix(int x) { T res = init; while (x < SZ(a)) { res = bin_op(res, a[x]); x += x & -x; } return res; } void modify(int x, T v) { while (x > 0) { a[x] = bin_op(a[x], v); x -= x & -x; } } void clear() { fill(a.begin(), a.end(), init); } }; vi get_prime(int n) { vi minp((ul)n + 1), p; for (int i = 2; i <= n; i++) { if (!minp[i]) { minp[i] = i; p.pb(i); } FOR(x, p) { if (x <= minp[i] && x * i <= n) minp[x * i] = x; else break; } } return p; } // alias templates template <typename T> using vv = vector<vector<T>>; template <typename T1, typename T2 = T1> using vp = vector<pair<T1, T2>>; template <typename T, int n> using va = vector<array<T, n>>; // order_of_key (k) : Number of items strictly smaller than k . // find_by_order(k) : K-th element in a set (counting from zero). #ifdef __GNUC__ #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> template <typename T> using rank_tree = __gnu_pbds::tree<T, __gnu_pbds::null_type, less<T>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>; #endif // union-find 并查集 struct UF { vi par, sz; int n_tree; explicit UF(int n) { // 0-indexed par.assign(n, 0); sz.assign(n, 1); rng(i, 0, n) par[i] = i; n_tree = n; } int n_cluster() const { return n_tree; } int size(int x) { return sz[root(x)]; } int root(int x) { return x == par[x] ? x : par[x] = root(par[x]); } bool unite(int x, int y) { int rx = root(x), ry = root(y); if (rx != ry) { par[rx] = ry; --n_tree; sz[ry] += sz[rx]; return true; } return false; } }; template <typename T, typename Compare = std::less<T>> struct SparseTable { size_t n{}; // 0-indexed vv<T> a; template <typename ptr_t> SparseTable(ptr_t beg, ptr_t end) : n(end - beg) { a.resize((size_t)h_bit(n) + 1); // 注意:不能写成 h_bit(n) a[0].assign(beg, end); rng(i, 0, SZ(a) - 1) { a[i + 1].resize(n); rng(j, 0, n - (1u << i)) { a[i + 1][j] = min(a[i][j], a[i][j + (1u << i)], Compare()); } rng(j, n - (1u << i), n) { a[i + 1][j] = a[i][j]; } } } using idx_t = long; T query(idx_t l, idx_t r) { // l <= r int i = h_bit(r - l + 1ul); return min(a[i][l], a[i][r + 1 - (1u << i)], Compare()); } }; vi get_popcnt(int n) { vi res((ul)n + 1); rng(i, 0, n) { if (i * 2 <= n) res[i * 2] = res[i]; if ((i & 1) == 0) res[i + 1] = res[i] + 1; } return res; } vi get_mu(int n) { assert(n > 0); vi mu(n + 1); vi min_p(n + 1); vi prime; mu[1] = 1; rng(i, 2, n + 1) { if (!min_p[i]) { prime.pb(i); min_p[i] = i; mu[i] = -1; } FOR(p, prime) { if (p > min_p[i]) { break; } int t = p * i; if (t > n) break; min_p[t] = p; mu[t] = p == min_p[i] ? 0 : -mu[i]; } } return mu; } template <typename num> num fp(num x, ll n) { // fast power: hat off to quailty if (n < 0) { x = fp(x, mod - 2); n = -n; } num ans = 1; while (n) { if (n & 1) ans *= x; n /= 2; x *= x; } return ans; } template <typename num> void bit_reverse_swap(vector<num> &a) { int n = SZ(a); for (int i = 1, j = n >> 1, k; i < n - 1; i++) { if (i < j) swap(a[i], a[j]); // tricky for (k = n >> 1; j >= k; j -= k, k >>= 1) ; // inspect the highest "1" j += k; } } template <typename num> void FFT(vector<num> &a, int type) { bit_reverse_swap(a); int n = SZ(a); for (int i = 2; i <= n; i *= 2) { const auto wi = fp(3, type * (mod - 1) / i); // i次单位根 for (int j = 0; j < n; j += i) { num w(1); for (int k = j, h = i >> 1; k < j + h; k++) { auto t = w * a[k + h], u = a[k]; a[k] = u + t; a[k + h] = u - t; w *= wi; } } } const auto inv = num(n).inv(); if (type == -1) for (auto &x : a) x *= inv; } template <typename num> void fp(vector<num> &a, const int n) { a.resize((ul)pow2((SZ(a) - 1) * n + 1)); FFT(a, 1); for (auto &x : a) x = fp(x, n); FFT(a, -1); } // DEBUG code by tourist string to_string(const string &s) { return '"' + s + '"'; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifdef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif // end DEBUG // minimal geometry classes struct point { int x, y; void read() { scan(x, y); } ll cross(const point &b) const { return 1LL * x * b.y - 1LL * y * b.x; } point operator-(const point &b) const { return {x - b.x, y - b.y}; } bool para(const point &b) const { // parallel return 1LL * x * b.y == 1LL * y * b.x; } bool operator<(const point &b) const { return cross(b) < 0; } }; struct segment { point p, dir; bool on_same_line(const segment &other) const { return dir.para(other.dir) && (p - other.p).para(dir); } bool para(const segment &b) const { return dir.para(b.dir); } bool operator==(const segment &b) const { return this->on_same_line(b); } }; struct fraction { int n, d; bool operator<(const fraction &that) const { return 1LL * n * that.d < 1LL * that.n * d; } bool operator==(const fraction &that) const { return n == that.n && d == that.d; } bool operator>=(const fraction &that) const { return !(*this < that); } bool operator>(const fraction &that) const { return 1LL * n * that.d > 1LL * that.n * d; } fraction inverse() const { return n < 0 ? fraction{-d, -n} : fraction{d, n}; } fraction(int n, int d) { if (d < 0) n = -n, d = -d; int g = __gcd(abs(n), d); n /= g, d /= g; this->n = n, this->d = d; } explicit fraction(int x) : n(x), d(1) {} fraction operator+(const fraction &that) const { return fraction{n * that.d + that.n * d, d * that.d}; } fraction operator-(const fraction &that) const { return fraction{n * that.d - that.n * d, d * that.d}; } fraction floor() const { if (d == 0) return *this; return fraction((n < 0 ? n - (d - 1) : n) / d); } friend ostream &operator<<(ostream &out, const fraction &f) { return out << "(" << f.n << ',' << f.d << ')'; } pii to_pair() const { return {n, d}; } }; using Num = modnum<mod>; struct comb { vector<Num> f; explicit comb(int n) { f.resize(n + 1); f[0] = 1; up(i, 1, n) f[i] = f[i - 1] * i; } Num get(int x, int y) const { assert(x <= SZ(f) - 1); assert(x >= 0 && y >= 0); if (x < y) return 0; return f[x] / f[y] / f[x - y]; } }; // BEGIN 树剖模板 namespace HLD { const int max_V = 1e5 + 5; vi g[max_V]; // vertices are 1-indexed int fa[max_V]; int sz[max_V]; int heavy_son[max_V]; int dep[max_V]; void dfs(int u, int f) { fa[u] = f; sz[u] = 1; dep[u] = dep[f] + 1; int hson = 0; FOR(v, g[u]) { if (v != f) { dfs(v, u); sz[u] += sz[v]; if (sz[v] > sz[hson]) hson = v; } } heavy_son[u] = hson; } int top[max_V]; int pos[max_V]; // 0-indexed // https://codeforces.com/blog/entry/22072 void init(int n) { // 很好的写法! int idx = 0; rng(i, 1, n + 1) { if (i != heavy_son[fa[i]]) { for (int j = i; j != 0; j = heavy_son[j]) { top[j] = i; pos[j] = idx++; } } } } // 两种情况:1.修改路径上的边 2.修改路径上的点。 bool VALUE_ON_EDGE = true; // BinOpr: binary operator template <class BinOpr> void process_path(int u, int v, BinOpr op) { while (top[u] != top[v]) { if (dep[top[u]] < dep[top[v]]) swap(u, v); op(pos[top[u]], pos[u]); u = fa[top[u]]; } if (dep[u] > dep[v]) swap(u, v); op(pos[u] + VALUE_ON_EDGE, pos[v]); } } // namespace HLD // END 树剖模板 template <typename T> T get_mid(T l, T r) { assert(l <= r); return l + (r - l) / 2; } //////////////////^"^/////////////////////////////////// int main() { FAST_READ cout << fixed << setprecision(10); #ifdef LOCAL ifstream in("main.in"); cin.rdbuf(in.rdbuf()); // ofstream out("main.out"); // cout.rdbuf(out.rdbuf()); #endif string s, t; scan(s, t); vector<array<int, 26>> next(SZ(s)); int n = SZ(s); fill(all(next.back()), n); next.back()[s.back() - 'a'] = n - 1; down(i, n - 2, 0) { next[i] = next[i + 1]; next[i][s[i] - 'a'] = i; } rng(i, 1, n) { rng(j, 0, 26) { if (next[i][j] == n) { next[i][j] = next[0][j]; } } } int pos = 0; ll ans = 0; FOR(x, t) { int tmp = next[pos][x - 'a']; if (tmp == n) { println(-1); return 0; } if (tmp < pos) ans += n; pos = tmp + 1; } println(ans + pos); #ifdef LOCAL cout << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; #endif return 0; }
const int mod1 = 1000000007, mod2 = 998244353; const int mod = mod1; #include <bits/stdc++.h> #include <type_traits> using namespace std; // Andrew He's modular-arithmetic class template <int MOD_> struct modnum { static constexpr int MOD = MOD_; static_assert(MOD_ > 0, "MOD must be positive"); private: using ll = long long; int v; static int minv(int a, int m) { a %= m; assert(a); return a == 1 ? 1 : int(m - ll(minv(m, a)) * ll(m) / a); } public: modnum() : v(0) {} modnum(ll v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; } explicit operator int() const { return v; } friend std::ostream &operator<<(std::ostream &out, const modnum &n) { return out << int(n); } friend std::istream &operator>>(std::istream &in, modnum &n) { ll v_; in >> v_; n = modnum(v_); return in; } friend bool operator==(const modnum &a, const modnum &b) { return a.v == b.v; } friend bool operator!=(const modnum &a, const modnum &b) { return a.v != b.v; } modnum inv() const { modnum res; res.v = minv(v, MOD); return res; } modnum neg() const { modnum res; res.v = v ? MOD - v : 0; return res; } modnum operator-() const { return neg(); } modnum operator+() const { return modnum(*this); } modnum &operator++() { v++; if (v == MOD) v = 0; return *this; } modnum &operator--() { if (v == 0) v = MOD; v--; return *this; } modnum &operator+=(const modnum &o) { v += o.v; if (v >= MOD) v -= MOD; return *this; } modnum &operator-=(const modnum &o) { v -= o.v; if (v < 0) v += MOD; return *this; } modnum &operator*=(const modnum &o) { v = int(ll(v) * ll(o.v) % MOD); return *this; } modnum &operator/=(const modnum &o) { return *this *= o.inv(); } friend modnum operator++(modnum &a, int) { modnum r = a; ++a; return r; } friend modnum operator--(modnum &a, int) { modnum r = a; --a; return r; } friend modnum operator+(const modnum &a, const modnum &b) { return modnum(a) += b; } friend modnum operator-(const modnum &a, const modnum &b) { return modnum(a) -= b; } friend modnum operator*(const modnum &a, const modnum &b) { return modnum(a) *= b; } friend modnum operator/(const modnum &a, const modnum &b) { return modnum(a) /= b; } friend modnum pow(modnum x, size_t n) { modnum res = 1; while (n) { if (n & 1) res *= x; x *= x; n /= 2; } return res; } }; struct Modnum { private: static int MOD; // declaration, not definition using ll = long long; using modnum = Modnum; int v; static int minv(int a, int m) { a %= m; assert(a); return a == 1 ? 1 : int(m - ll(minv(m, a)) * ll(m) / a); } public: Modnum() : v(0) {} Modnum(ll v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; } static void set_mod(const int &m) { assert(m > 0); MOD = m; } explicit operator int() const { return v; } friend std::ostream &operator<<(std::ostream &out, const modnum &n) { return out << int(n); } friend std::istream &operator>>(std::istream &in, modnum &n) { ll v_; in >> v_; n = modnum(v_); return in; } friend bool operator==(const modnum &a, const modnum &b) { return a.v == b.v; } friend bool operator!=(const modnum &a, const modnum &b) { return a.v != b.v; } modnum inv() const { modnum res; res.v = minv(v, MOD); return res; } modnum neg() const { modnum res; res.v = v ? MOD - v : 0; return res; } modnum operator-() const { return neg(); } modnum operator+() const { return *this; } modnum &operator++() { v++; if (v == MOD) v = 0; return *this; } modnum &operator--() { if (v == 0) v = MOD; v--; return *this; } modnum &operator+=(const modnum &o) { v += o.v; if (v >= MOD) v -= MOD; return *this; } modnum &operator-=(const modnum &o) { v -= o.v; if (v < 0) v += MOD; return *this; } modnum &operator*=(const modnum &o) { v = int(ll(v) * ll(o.v) % MOD); return *this; } modnum &operator/=(const modnum &o) { return *this *= o.inv(); } friend modnum operator++(modnum &a, int) { modnum r = a; ++a; return r; } friend modnum operator--(modnum &a, int) { modnum r = a; --a; return r; } friend modnum operator+(const modnum &a, const modnum &b) { return modnum(a) += b; } friend modnum operator-(const modnum &a, const modnum &b) { return modnum(a) -= b; } friend modnum operator*(const modnum &a, const modnum &b) { return modnum(a) *= b; } friend modnum operator/(const modnum &a, const modnum &b) { return modnum(a) /= b; } }; int Modnum::MOD = 1; // definition #define sq(x) (x) * (x) // square #define FAST_READ \ ios::sync_with_stdio(false); \ cin.tie(nullptr); #ifdef LOCAL #define see(x) cout << "<DBG> " << #x << ": " << (x) << endl #endif #ifndef LOCAL #define see(x) #endif #define LSON(x) ((x)*2) #define RSON(x) ((x)*2 + 1) template <typename A, typename B> void Min(A &a, const B &b) { if (b < a) a = b; } template <typename A, typename B> void Max(A &a, const B &b) { if (b > a) a = b; } int cas; ostream &kase() { return cout << "Case #" << ++cas << ": "; } #if __cplusplus < 201402L template <class Iterator> std::reverse_iterator<Iterator> make_reverse_iterator(Iterator it) { return std::reverse_iterator<Iterator>(it); } #endif template <typename iter_t> struct iter_pair { iter_t _beg, _end; iter_t begin() { return _beg; } iter_t end() { return _end; } }; template <class cont> iter_pair<reverse_iterator<decltype(begin(declval<cont>()))>> reverse(cont &&r) { return {make_reverse_iterator(end(r)), make_reverse_iterator(begin(r))}; } template <typename T> void dprintln(const T &t) { cout << t << endl; } // for debug use template <typename T, typename... Args> void dprintln(const T &t, const Args &...rest) { cout << t << ' '; dprintln(rest...); } template <typename T> void println(const T &t) { cout << t << '\n'; } template <typename T, typename... Args> void println(const T &t, const Args &...rest) { cout << t << ' '; println(rest...); } template <typename T> void println(const vector<T> &vec) { if (!vec.empty()) { cout << vec[0]; for (size_t i = 1; i < vec.size(); ++i) cout << ' ' << vec[i]; } cout << endl; } template <typename T> void print(const T &t) { cout << t << ' '; } template <typename T, typename... Args> void print(const T &t, const Args &...rest) { cout << t << ' '; print(rest...); } // this overload is chosen when there's only one argument template <class T> void scan(T &t) { cin >> t; } template <class T, class... Args> void scan(T &a, Args &...rest) { cin >> a; scan(rest...); } template <typename T> void scan(vector<T> &vec) { for (T &x : vec) scan(x); } using ull = unsigned long long; using ll = long long; using ul = unsigned long; using vl = vector<ll>; using vi = vector<int>; using pii = pair<int, int>; using pip = pair<int, pii>; using pll = pair<ll, ll>; using vb = vector<bool>; using vpii = vector<pii>; using ldb = long double; template <typename int_t> inline int_t lowbit(int_t x) { return x & -x; } #define rng(i, a, b) for (int i = (int)(a); i < (int)(b); ++i) #define up(i, a, b) for (int i = int(a); i <= int(b); ++i) #define down(i, b, a) for (int i = int(b); i >= int(a); i--) #define rep(n) for (int i##n = 0, _##n = (int)n; i##n < _##n; i##n++) #define stp(i, a, b, c) for (int i = (a); i < (b); i += (c)) #define FOR(x, cont) for (const auto &x : cont) #define INC(init, x, y) for (init; x <= y; ++x) #define For(x, cont) for (auto &x : cont) #define all(x) begin(x), end(x) #define pb push_back #define mp make_pair #define eb emplace_back #define SZ(x) (int)(x).size() #define UP(i, l, r) for (i = decltype(i)(l); i <= decltype(i)(r); ++i) #define DOWN(i, r, l) for (i = decltype(i)(r); i >= decltype(i)(l); --i) #define Dec(a, b) for (; a >= b; --a) template <typename T, typename Comp = less<T>> using pq = priority_queue<T, vector<T>, Comp>; #define popcnt(x) __builtin_popcountll((x)) #define SET(arr, v) memset(arr, (v), sizeof(arr)) #define UNIQ(vec) (vec).erase(unique(all(vec)), end(vec)) #define LB(cont, x) int(lower_bound(all(cont), x) - begin(cont)) #define UB(cont, x) int(upper_bound(all(cont), x) - begin(cont)) #define AI(name, n, m) vv<int> name(n, vi(m)); #define AL(name, n, m) vv<ll> name(size_t(n), vl(size_t(m))); #define set0(arr) memset(arr, 0, sizeof arr) #define set1(arr) memset(arr, -1, sizeof arr) #define AT(T, n, m, a) vector<vector<T>> a(n, vector<T>(m)) const int dx[] = {0, 0, -1, 1}, dy[] = {-1, 1, 0, 0}; auto bet = [](const ll x, const ll y, const ll i) { return x <= i && i <= y; }; template <typename T1, typename T2> T1 ceil(T1 x, T2 y) { // y >= 1,是整数。需要注意 x + y - 1 是否会溢出 return (x + y - 1) / y; } inline int h_bit(unsigned long long x) { return sizeof(unsigned long long) * 8 - 1 - __builtin_clzll(x); } int pow2(int x) { // power of 2 return 1 << (h_bit((ull)x) + (x != lowbit(x))); } template <typename T> struct bit { vector<T> a; function<T(T, T)> bin_op; const T init; explicit bit(int n, function<T(T, T)> bin_op, T init) : bin_op(bin_op), init(init) { a.assign(n + 1, init); } T prefix(T x) { auto res = init; while (x) { res = bin_op(a[x], res); x -= x & -x; } return res; } void modify(int x, T v) { while (x < (int)a.size()) { a[x] = bin_op(a[x], v); x += x & -x; } } void clear() { fill(a.begin(), a.end(), init); } }; template <typename T> struct r_bit { vector<T> a; function<T(T, T)> bin_op; const T init; explicit r_bit(int n, function<T(T, T)> bin_op, T init) : bin_op(move(bin_op)), init(init) { a.ssign(n + 1, init); } T suffix(int x) { T res = init; while (x < SZ(a)) { res = bin_op(res, a[x]); x += x & -x; } return res; } void modify(int x, T v) { while (x > 0) { a[x] = bin_op(a[x], v); x -= x & -x; } } void clear() { fill(a.begin(), a.end(), init); } }; vi get_prime(int n) { vi minp((ul)n + 1), p; for (int i = 2; i <= n; i++) { if (!minp[i]) { minp[i] = i; p.pb(i); } FOR(x, p) { if (x <= minp[i] && x * i <= n) minp[x * i] = x; else break; } } return p; } // alias templates template <typename T> using vv = vector<vector<T>>; template <typename T1, typename T2 = T1> using vp = vector<pair<T1, T2>>; template <typename T, int n> using va = vector<array<T, n>>; // order_of_key (k) : Number of items strictly smaller than k . // find_by_order(k) : K-th element in a set (counting from zero). #ifdef __GNUC__ #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> template <typename T> using rank_tree = __gnu_pbds::tree<T, __gnu_pbds::null_type, less<T>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>; #endif // union-find 并查集 struct UF { vi par, sz; int n_tree; explicit UF(int n) { // 0-indexed par.assign(n, 0); sz.assign(n, 1); rng(i, 0, n) par[i] = i; n_tree = n; } int n_cluster() const { return n_tree; } int size(int x) { return sz[root(x)]; } int root(int x) { return x == par[x] ? x : par[x] = root(par[x]); } bool unite(int x, int y) { int rx = root(x), ry = root(y); if (rx != ry) { par[rx] = ry; --n_tree; sz[ry] += sz[rx]; return true; } return false; } }; template <typename T, typename Compare = std::less<T>> struct SparseTable { size_t n{}; // 0-indexed vv<T> a; template <typename ptr_t> SparseTable(ptr_t beg, ptr_t end) : n(end - beg) { a.resize((size_t)h_bit(n) + 1); // 注意:不能写成 h_bit(n) a[0].assign(beg, end); rng(i, 0, SZ(a) - 1) { a[i + 1].resize(n); rng(j, 0, n - (1u << i)) { a[i + 1][j] = min(a[i][j], a[i][j + (1u << i)], Compare()); } rng(j, n - (1u << i), n) { a[i + 1][j] = a[i][j]; } } } using idx_t = long; T query(idx_t l, idx_t r) { // l <= r int i = h_bit(r - l + 1ul); return min(a[i][l], a[i][r + 1 - (1u << i)], Compare()); } }; vi get_popcnt(int n) { vi res((ul)n + 1); rng(i, 0, n) { if (i * 2 <= n) res[i * 2] = res[i]; if ((i & 1) == 0) res[i + 1] = res[i] + 1; } return res; } vi get_mu(int n) { assert(n > 0); vi mu(n + 1); vi min_p(n + 1); vi prime; mu[1] = 1; rng(i, 2, n + 1) { if (!min_p[i]) { prime.pb(i); min_p[i] = i; mu[i] = -1; } FOR(p, prime) { if (p > min_p[i]) { break; } int t = p * i; if (t > n) break; min_p[t] = p; mu[t] = p == min_p[i] ? 0 : -mu[i]; } } return mu; } template <typename num> num fp(num x, ll n) { // fast power: hat off to quailty if (n < 0) { x = fp(x, mod - 2); n = -n; } num ans = 1; while (n) { if (n & 1) ans *= x; n /= 2; x *= x; } return ans; } template <typename num> void bit_reverse_swap(vector<num> &a) { int n = SZ(a); for (int i = 1, j = n >> 1, k; i < n - 1; i++) { if (i < j) swap(a[i], a[j]); // tricky for (k = n >> 1; j >= k; j -= k, k >>= 1) ; // inspect the highest "1" j += k; } } template <typename num> void FFT(vector<num> &a, int type) { bit_reverse_swap(a); int n = SZ(a); for (int i = 2; i <= n; i *= 2) { const auto wi = fp(3, type * (mod - 1) / i); // i次单位根 for (int j = 0; j < n; j += i) { num w(1); for (int k = j, h = i >> 1; k < j + h; k++) { auto t = w * a[k + h], u = a[k]; a[k] = u + t; a[k + h] = u - t; w *= wi; } } } const auto inv = num(n).inv(); if (type == -1) for (auto &x : a) x *= inv; } template <typename num> void fp(vector<num> &a, const int n) { a.resize((ul)pow2((SZ(a) - 1) * n + 1)); FFT(a, 1); for (auto &x : a) x = fp(x, n); FFT(a, -1); } // DEBUG code by tourist string to_string(const string &s) { return '"' + s + '"'; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifdef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif // end DEBUG // minimal geometry classes struct point { int x, y; void read() { scan(x, y); } ll cross(const point &b) const { return 1LL * x * b.y - 1LL * y * b.x; } point operator-(const point &b) const { return {x - b.x, y - b.y}; } bool para(const point &b) const { // parallel return 1LL * x * b.y == 1LL * y * b.x; } bool operator<(const point &b) const { return cross(b) < 0; } }; struct segment { point p, dir; bool on_same_line(const segment &other) const { return dir.para(other.dir) && (p - other.p).para(dir); } bool para(const segment &b) const { return dir.para(b.dir); } bool operator==(const segment &b) const { return this->on_same_line(b); } }; struct fraction { int n, d; bool operator<(const fraction &that) const { return 1LL * n * that.d < 1LL * that.n * d; } bool operator==(const fraction &that) const { return n == that.n && d == that.d; } bool operator>=(const fraction &that) const { return !(*this < that); } bool operator>(const fraction &that) const { return 1LL * n * that.d > 1LL * that.n * d; } fraction inverse() const { return n < 0 ? fraction{-d, -n} : fraction{d, n}; } fraction(int n, int d) { if (d < 0) n = -n, d = -d; int g = __gcd(abs(n), d); n /= g, d /= g; this->n = n, this->d = d; } explicit fraction(int x) : n(x), d(1) {} fraction operator+(const fraction &that) const { return fraction{n * that.d + that.n * d, d * that.d}; } fraction operator-(const fraction &that) const { return fraction{n * that.d - that.n * d, d * that.d}; } fraction floor() const { if (d == 0) return *this; return fraction((n < 0 ? n - (d - 1) : n) / d); } friend ostream &operator<<(ostream &out, const fraction &f) { return out << "(" << f.n << ',' << f.d << ')'; } pii to_pair() const { return {n, d}; } }; using Num = modnum<mod>; struct comb { vector<Num> f; explicit comb(int n) { f.resize(n + 1); f[0] = 1; up(i, 1, n) f[i] = f[i - 1] * i; } Num get(int x, int y) const { assert(x <= SZ(f) - 1); assert(x >= 0 && y >= 0); if (x < y) return 0; return f[x] / f[y] / f[x - y]; } }; // BEGIN 树剖模板 namespace HLD { const int max_V = 1e5 + 5; vi g[max_V]; // vertices are 1-indexed int fa[max_V]; int sz[max_V]; int heavy_son[max_V]; int dep[max_V]; void dfs(int u, int f) { fa[u] = f; sz[u] = 1; dep[u] = dep[f] + 1; int hson = 0; FOR(v, g[u]) { if (v != f) { dfs(v, u); sz[u] += sz[v]; if (sz[v] > sz[hson]) hson = v; } } heavy_son[u] = hson; } int top[max_V]; int pos[max_V]; // 0-indexed // https://codeforces.com/blog/entry/22072 void init(int n) { // 很好的写法! int idx = 0; rng(i, 1, n + 1) { if (i != heavy_son[fa[i]]) { for (int j = i; j != 0; j = heavy_son[j]) { top[j] = i; pos[j] = idx++; } } } } // 两种情况:1.修改路径上的边 2.修改路径上的点。 bool VALUE_ON_EDGE = true; // BinOpr: binary operator template <class BinOpr> void process_path(int u, int v, BinOpr op) { while (top[u] != top[v]) { if (dep[top[u]] < dep[top[v]]) swap(u, v); op(pos[top[u]], pos[u]); u = fa[top[u]]; } if (dep[u] > dep[v]) swap(u, v); op(pos[u] + VALUE_ON_EDGE, pos[v]); } } // namespace HLD // END 树剖模板 template <typename T> T get_mid(T l, T r) { assert(l <= r); return l + (r - l) / 2; } //////////////////^"^/////////////////////////////////// int main() { FAST_READ cout << fixed << setprecision(10); #ifdef LOCAL ifstream in("main.in"); cin.rdbuf(in.rdbuf()); // ofstream out("main.out"); // cout.rdbuf(out.rdbuf()); #endif string s, t; scan(s, t); vector<array<int, 26>> next(SZ(s)); int n = SZ(s); fill(all(next.back()), n); next.back()[s.back() - 'a'] = n - 1; down(i, n - 2, 0) { next[i] = next[i + 1]; next[i][s[i] - 'a'] = i; } rng(i, 1, n) { rng(j, 0, 26) { if (next[i][j] == n) { next[i][j] = next[0][j]; } } } int pos = 0; ll ans = 0; FOR(x, t) { int tmp = next[pos][x - 'a']; if (tmp == n) { println(-1); return 0; } if (tmp < pos) ans += n; pos = tmp + 1; if (pos == n) { ans += n; pos = 0; } } println(ans + pos); #ifdef LOCAL cout << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; #endif return 0; }
insert
817
817
817
821
0
p02937
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) using namespace std; typedef long long ll; typedef pair<int, int> P; const int inf = 1 << 21; const ll INF = 1LL << 60; const ll mod = 1e9 + 7; const int dx[4] = {-1, 0, 1, 0}; const int dy[4] = {0, -1, 0, 1}; // DAME: int main() { cin.tie(0); ios::sync_with_stdio(false); string s, t; cin >> s >> t; s += s; map<char, vector<int>> sm; rep(i, s.size()) { sm[s[i]].push_back(i); } rep(i, s.size()) { sort(sm[s[i]].begin(), sm[s[i]].end()); } ll ans = 0; int index = -1; rep(i, t.size()) { auto itr = lower_bound(sm[t[i]].begin(), sm[t[i]].end(), index + 1); if (itr == sm[t[i]].end()) { ans += s.size() - index; if (sm[t[i]].size() == 0) { cout << -1 << endl; return 0; } index = sm[t[i]][0]; ans += index; } else { ans += *itr - index; index = *itr; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) using namespace std; typedef long long ll; typedef pair<int, int> P; const int inf = 1 << 21; const ll INF = 1LL << 60; const ll mod = 1e9 + 7; const int dx[4] = {-1, 0, 1, 0}; const int dy[4] = {0, -1, 0, 1}; // DAME: int main() { cin.tie(0); ios::sync_with_stdio(false); string s, t; cin >> s >> t; s += s; map<char, vector<int>> sm; rep(i, s.size()) sm[s[i]].push_back(i); ll ans = 0; int index = -1; rep(i, t.size()) { auto itr = lower_bound(sm[t[i]].begin(), sm[t[i]].end(), index + 1); if (itr == sm[t[i]].end()) { ans += s.size() - index; if (sm[t[i]].size() == 0) { cout << -1 << endl; return 0; } index = sm[t[i]][0]; ans += index; } else { ans += *itr - index; index = *itr; } } cout << ans << endl; return 0; }
replace
21
23
21
22
TLE
p02937
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; string s, t; int nex[101010][27]; int main() { cin >> s >> t; int s_sz = s.size(); int t_sz = t.size(); s = s + s; for (int i = 0; i < 26; i++) nex[2 * s_sz][i] = 2 * s_sz; for (int i = 2 * s_sz - 1; 0 <= i; i--) { for (int j = 0; j < 26; j++) { nex[i][j] = nex[i + 1][j]; } nex[i][s[i] - 'a'] = i + 1; } long long cur = 0; for (int i = 0; i < t.size(); i++) { char c = t[i] - 'a'; int x = cur % s_sz; if (nex[x][c] == 2 * s_sz) { cout << -1 << endl; return 0; } cur += nex[x][c] - x; } cout << cur << endl; return 0; }
#include <bits/stdc++.h> using namespace std; string s, t; int nex[201010][27]; int main() { cin >> s >> t; int s_sz = s.size(); int t_sz = t.size(); s = s + s; for (int i = 0; i < 26; i++) nex[2 * s_sz][i] = 2 * s_sz; for (int i = 2 * s_sz - 1; 0 <= i; i--) { for (int j = 0; j < 26; j++) { nex[i][j] = nex[i + 1][j]; } nex[i][s[i] - 'a'] = i + 1; } long long cur = 0; for (int i = 0; i < t.size(); i++) { char c = t[i] - 'a'; int x = cur % s_sz; if (nex[x][c] == 2 * s_sz) { cout << -1 << endl; return 0; } cur += nex[x][c] - x; } cout << cur << endl; return 0; }
replace
5
6
5
6
0
p02937
C++
Runtime Error
#include <algorithm> #include <iostream> #include <vector> using namespace std; typedef long long ll; int main() { string s, t; cin >> s >> t; int s_len = s.length(); s = s + s; vector<vector<int>> next(s_len, vector<int>('z' - 'a', -1)); ll out = 0; int j = 0; int j0, jstop; for (int i = 0; i < t.length(); i++) { j0 = j; if (next[j0][t[i] - 'a'] >= 0) { j = next[j0][t[i] - 'a']; } else { jstop = j + s_len; for (; t[i] != s[j];) { if (j == jstop) { cout << -1 << endl; return 0; } j++; } next[j0][t[i] - 'a'] = j; } j++; out += j - j0; if (j >= s_len) j -= s_len; } cout << out << endl; return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; typedef long long ll; int main() { string s, t; cin >> s >> t; int s_len = s.length(); s = s + s; vector<vector<int>> next(s_len, vector<int>('z' - 'a' + 1, -1)); ll out = 0; int j = 0; int j0, jstop; for (int i = 0; i < t.length(); i++) { j0 = j; if (next[j0][t[i] - 'a'] >= 0) { j = next[j0][t[i] - 'a']; } else { jstop = j + s_len; for (; t[i] != s[j];) { if (j == jstop) { cout << -1 << endl; return 0; } j++; } next[j0][t[i] - 'a'] = j; } j++; out += j - j0; if (j >= s_len) j -= s_len; } cout << out << endl; return 0; }
replace
11
12
11
12
0
p02937
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; vector<vector<int>> point(26, vector<int>()); int main() { string s, t; cin >> s >> t; for (int i = 0; i < s.length(); i++) { point[s[i] - 'a'].push_back(i); } for (int i = 0; i < t.length(); i++) { if (!point[t[i] - 'a'].size()) { cout << -1 << endl; return 0; } } int now = -1; long long ans = 0; for (int i = 0; i < t.length(); i++) { int flag = 0; for (int j = 0; j < point[t[i] - 'a'].size(); j++) { if (point[t[i] - 'a'][j] > now) { ans += point[t[i] - 'a'][j] - now; now = point[t[i] - 'a'][j]; flag = 1; break; } } if (!flag) { ans += s.length() - 1 - now; now = -1; ans += point[t[i] - 'a'][0] - now; now = point[t[i] - 'a'][0]; } } cout << ans << endl; }
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; vector<vector<int>> point(26, vector<int>()); int main() { string s, t; cin >> s >> t; for (int i = 0; i < s.length(); i++) { point[s[i] - 'a'].push_back(i); } for (int i = 0; i < t.length(); i++) { if (!point[t[i] - 'a'].size()) { cout << -1 << endl; return 0; } } int now = -1; long long ans = 0; for (int i = 0; i < t.length(); i++) { auto it = lower_bound(point[t[i] - 'a'].begin(), point[t[i] - 'a'].end(), now + 1); if (it != point[t[i] - 'a'].end()) { ans += *it - now; now = *it; } else { ans += s.length() - 1 - now + point[t[i] - 'a'][0] + 1; now = point[t[i] - 'a'][0]; } } cout << ans << endl; }
replace
23
36
23
30
TLE
p02937
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { string s, t; cin >> s >> t; vector<vector<int>> positions(26); for (int i = 0; i < s.size(); ++i) positions[s[i] - 'a'].push_back(i); ll ans = 0; int tpos = 0; int pointer = -1; while (tpos < t.size()) { if (positions[t[tpos] - 'a'].size() == 0) { cout << -1 << endl; return 0; } int flag = false; for (int spos : positions[t[tpos] - 'a']) { if (pointer < spos) { // OK ans += spos - pointer; pointer = spos; flag = true; break; } } if (!flag) { // next loop ans += (positions[t[tpos] - 'a'][0] + s.size() - pointer); pointer = positions[t[tpos] - 'a'][0]; } tpos++; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { string s, t; cin >> s >> t; vector<vector<int>> positions(26); for (int i = 0; i < s.size(); ++i) positions[s[i] - 'a'].push_back(i); ll ans = 0; int tpos = 0; int pointer = -1; while (tpos < t.size()) { if (positions[t[tpos] - 'a'].size() == 0) { cout << -1 << endl; return 0; } auto itr = upper_bound(positions[t[tpos] - 'a'].begin(), positions[t[tpos] - 'a'].end(), pointer); if (itr != positions[t[tpos] - 'a'].end()) { // OK ans += *itr - pointer; pointer = *itr; } else { // next loop ans += (positions[t[tpos] - 'a'][0] + s.size() - pointer); pointer = positions[t[tpos] - 'a'][0]; } tpos++; } cout << ans << endl; return 0; }
replace
18
29
18
25
TLE
p02937
C++
Runtime Error
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; // template #define rep(i, a, b) for (int i = (a); i < (b); i++) #define rrep(i, a, b) for (int i = (a); i > (b); i--) #define ALL(v) (v).begin(), (v).end() typedef long long int ll; typedef pair<ll, ll> P; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } template <typename A, size_t N, typename T> void Fill(A (&array)[N], const T &val) { fill((T *)array, (T *)(array + N), val); } const int inf = INT_MAX / 2; const ll INF = LLONG_MAX / 2; // template end int main() { string s, t; cin >> s >> t; int n = s.size(), m = t.size(); ll nxt[1010][26]; Fill(nxt, INF); rep(i, 0, n) nxt[i][s[i] - 'a'] = 0LL; rrep(i, 2 * n, 0) rep(j, 0, 26) { chmin(nxt[(i - 1 + n) % n][j], nxt[i % n][j] + 1LL); } ll ans = 0; rep(i, 0, m) { ll val = nxt[ans % n][t[i] - 'a']; cerr << val << endl; if (val == INF) { printf("-1"); return 0; } ans += val + 1; } printf("%lld\n", ans); return 0; }
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; // template #define rep(i, a, b) for (int i = (a); i < (b); i++) #define rrep(i, a, b) for (int i = (a); i > (b); i--) #define ALL(v) (v).begin(), (v).end() typedef long long int ll; typedef pair<ll, ll> P; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } template <typename A, size_t N, typename T> void Fill(A (&array)[N], const T &val) { fill((T *)array, (T *)(array + N), val); } const int inf = INT_MAX / 2; const ll INF = LLONG_MAX / 2; // template end int main() { string s, t; cin >> s >> t; int n = s.size(), m = t.size(); ll nxt[100010][26]; Fill(nxt, INF); rep(i, 0, n) nxt[i][s[i] - 'a'] = 0LL; rrep(i, 2 * n, 0) rep(j, 0, 26) { chmin(nxt[(i - 1 + n) % n][j], nxt[i % n][j] + 1LL); } ll ans = 0; rep(i, 0, m) { ll val = nxt[ans % n][t[i] - 'a']; cerr << val << endl; if (val == INF) { printf("-1"); return 0; } ans += val + 1; } printf("%lld\n", ans); return 0; }
replace
36
37
36
37
0
5 2 0