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
p02628
C++
Runtime Error
#include <bits/stdc++.h> #define int long long int #define pb push_back #define fast \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); #define pii pair<int, int> #define vvi vector<vector<int>> #define vi vector<int> #define f first #define s second using namespace std; const int mod = 1e9 + 7; int logi(int n) { int p = 0; while (n / 2) { n = n / 2; p++; } return p; } int poww(int x, int y) { int pro = 1; while (y) { if (y & 1) pro = ((pro % mod) * (x % mod) % mod); y = y >> 1; x = ((x % mod) * (x % mod) % mod); } return pro; } int poww2(int x, int y) { int pro = 1; while (y) { if (y & 1) pro = pro * x; y = y >> 1; x = x * x; } return pro; } bool isprime(int n) { if (n == 1) return false; for (int i = 2; i <= sqrt(n); i++) if (n % i == 0) return false; return true; } /* */ int solve() { int n, k; cin >> n >> k; vi v(n); for (int i = 0; i < n; i++) { cin >> v[i]; } sort(v.begin(), v.end()); int sum = 0; for (int i = 0; i < k; i++) { sum += v[i]; } cout << sum; } int32_t main() { fast; int t = 1; // cin>>t; while (t--) { solve(); } }
#include <bits/stdc++.h> #define int long long int #define pb push_back #define fast \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); #define pii pair<int, int> #define vvi vector<vector<int>> #define vi vector<int> #define f first #define s second using namespace std; const int mod = 1e9 + 7; int logi(int n) { int p = 0; while (n / 2) { n = n / 2; p++; } return p; } int poww(int x, int y) { int pro = 1; while (y) { if (y & 1) pro = ((pro % mod) * (x % mod) % mod); y = y >> 1; x = ((x % mod) * (x % mod) % mod); } return pro; } int poww2(int x, int y) { int pro = 1; while (y) { if (y & 1) pro = pro * x; y = y >> 1; x = x * x; } return pro; } bool isprime(int n) { if (n == 1) return false; for (int i = 2; i <= sqrt(n); i++) if (n % i == 0) return false; return true; } /* */ void solve() { int n, k; cin >> n >> k; vi v(n); for (int i = 0; i < n; i++) { cin >> v[i]; } sort(v.begin(), v.end()); int sum = 0; for (int i = 0; i < k; i++) { sum += v[i]; } cout << sum; } int32_t main() { fast; int t = 1; // cin>>t; while (t--) { solve(); } }
replace
54
55
54
55
0
p02628
C++
Runtime Error
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> lol; for (int i = 0; i < n; i++) { cin >> lol[i]; } sort(lol.begin(), lol.end()); int sum = 0; for (int i = 0; i < k; i++) { sum += lol[i]; } cout << sum; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> lol(n); for (int i = 0; i < n; i++) { cin >> lol[i]; } sort(lol.begin(), lol.end()); int sum = 0; for (int i = 0; i < k; i++) { sum += lol[i]; } cout << sum; }
replace
8
9
8
9
-11
p02628
C++
Runtime Error
#include <bits/stdc++.h> #include <iostream> int main() { int n, k, sum, vec[n]; sum = 0; std::cin >> n >> k; for (int i = 0; i < n; i++) { std::cin >> vec[i]; } std::sort(vec, vec + n); for (int i = 0; i < k; i++) { sum = sum + vec[i]; } std::cout << sum; }
#include <bits/stdc++.h> #include <iostream> int main() { int n, k, sum, vec[1000]; sum = 0; std::cin >> n >> k; for (int i = 0; i < n; i++) { std::cin >> vec[i]; } std::sort(vec, vec + n); for (int i = 0; i < k; i++) { sum = sum + vec[i]; } std::cout << sum; }
replace
3
4
3
4
0
p02629
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define MAX 500000000 // Function to print Excel column name for a given column number void printString(long long int n) { char str[MAX]; // To store result (Excel column name) long long int i = 0; // To store current index in str which is result while (n > 0) { // Find remainder long long int rem = n % 26; // If remainder is 0, then a 'Z' must be there in output if (rem == 0) { str[i++] = 'z'; n = (n / 26) - 1; } else // If remainder is non-zero { str[i++] = (rem - 1) + 'a'; n = n / 26; } } str[i] = '\0'; // Reverse the string and print result reverse(str, str + strlen(str)); cout << str; return; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int n, num = 0, num1 = 0; cin >> n; printString(n); return 0; }
#include <bits/stdc++.h> using namespace std; #define MAX 50 // Function to print Excel column name for a given column number void printString(long long int n) { char str[MAX]; // To store result (Excel column name) long long int i = 0; // To store current index in str which is result while (n > 0) { // Find remainder long long int rem = n % 26; // If remainder is 0, then a 'Z' must be there in output if (rem == 0) { str[i++] = 'z'; n = (n / 26) - 1; } else // If remainder is non-zero { str[i++] = (rem - 1) + 'a'; n = n / 26; } } str[i] = '\0'; // Reverse the string and print result reverse(str, str + strlen(str)); cout << str; return; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int n, num = 0, num1 = 0; cin >> n; printString(n); return 0; }
replace
4
5
4
5
-11
p02629
C++
Runtime Error
#include <iostream> using namespace std; int main() { long n; cin >> n; int digit[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; int idx = 0; while (n > 0) { digit[idx] = n % 26; if (digit[idx] == 0) { digit[idx] = 26; n -= 26; } n /= 26; idx++; } for (; digit[idx] == 0; idx--) ; for (; idx >= 0; idx--) { cout << (char)('a' + digit[idx] - 1); } return 0; }
#include <iostream> using namespace std; int main() { long n; cin >> n; int digit[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; int idx = 0; while (n > 0) { digit[idx] = n % 26; if (digit[idx] == 0) { digit[idx] = 26; n -= 26; } n /= 26; idx++; } for (; digit[idx] == 0; idx--) ; for (; idx >= 0; idx--) { cout << (char)('a' + digit[idx] - 1); } return 0; }
replace
5
6
5
6
0
p02629
C++
Runtime Error
#include <cassert> #include <cmath> #include <iostream> #include <stdexcept> #include <string> #include <vector> using namespace std; int main() { long long N; cin >> N; assert(N >= 1); const string alphabets = "abcdefghijklmnopqrstuvwxyz"; const long long MAX_N = 1000000000000001; const int M = 26; int j = 1000000000; long long th = 0; vector<long long> ths = {0}; for (int i = 1; i < log(MAX_N) / log(M); i++) { th += pow(M, i); ths.push_back(th); if (N <= th) j = min(j, i); } // cout << "length: " << j << endl; string ans = ""; N -= 1; for (; j > 0; j--) { int x = (N - ths[j - 1]) / pow(M, j - 1); N -= (x + 1) * pow(M, j - 1); cout << alphabets[x]; } cout << endl; return 0; }
#include <cassert> #include <cmath> #include <iostream> #include <stdexcept> #include <string> #include <vector> using namespace std; int main() { long long N; cin >> N; assert(N >= 1); const string alphabets = "abcdefghijklmnopqrstuvwxyz"; const long long MAX_N = 1000000000000001; const int M = 26; int j = 1000000000; long long th = 0; vector<long long> ths = {0}; for (int i = 1; i < log(MAX_N) / log(M) + 1; i++) { th += pow(M, i); ths.push_back(th); if (N <= th) j = min(j, i); } // cout << "length: " << j << endl; string ans = ""; N -= 1; for (; j > 0; j--) { int x = (N - ths[j - 1]) / pow(M, j - 1); N -= (x + 1) * pow(M, j - 1); cout << alphabets[x]; } cout << endl; return 0; }
replace
20
21
20
21
0
p02629
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; string ans; int n, now = 1, cnt = 26, num = 26; int main() { cin >> n; while (cnt < n) { num *= 26; cnt += num, now++; } cnt -= num, num /= 26, n -= cnt + 1; // cout<<n<<" "<<num<<"\n"; for (int i = 0; i < now; i++) { putchar(n / num + 'a'); n %= num, num /= 26; } }
#include <bits/stdc++.h> using namespace std; long long n, now = 1, cnt = 26, num = 26; int main() { cin >> n; while (cnt < n) { num *= 26; cnt += num, now++; } cnt -= num, num /= 26, n -= cnt + 1; // cout<<n<<" "<<num<<"\n"; for (int i = 0; i < now; i++) { putchar(n / num + 'a'); n %= num, num /= 26; } }
replace
2
4
2
3
0
p02629
Python
Runtime Error
N = int(input()) ans = "" while N > 0: N -= 1 ans += chr(ord("a" + N % 26)) N //= 26 print(ans[::-1])
N = int(input()) ans = "" while N > 0: N -= 1 ans += chr(ord("a") + N % 26) N //= 26 print(ans[::-1])
replace
4
5
4
5
TypeError: can only concatenate str (not "int") to str
Traceback (most recent call last): File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02629/Python/s736604146.py", line 5, in <module> ans += chr(ord('a' + N % 26)) TypeError: can only concatenate str (not "int") to str
p02629
Python
Runtime Error
import string s = string.ascii_lowercase def f(x, tmp): if x == 0: return "" x -= 1 if x <= 26: return s[x] + tmp else: return f(x // 26, s[x % 26] + tmp) n = int(input()) r = f(n, "") print(r)
import string s = string.ascii_lowercase def f(x, tmp): if x == 0: return "" x -= 1 if x < 26: return s[x] + tmp else: return f(x // 26, s[x % 26] + tmp) n = int(input()) r = f(n, "") print(r)
replace
9
10
9
10
0
p02629
Python
Runtime Error
a = int(input()) c = "" while a >= 26: b = a % 26 c += chr(ord("a") + b - 1) a /= 26 c += chr(ord("a") + int(a) - 1) print(c[::-1])
a = int(input()) c = "" while a > 26: b = int(a) % 26 c += chr(ord("a") + (25 if b == 0 else int(b) - 1)) a = int(a / 26 - 1 if a % 26 == 0 else a / 26) c += chr(ord("a") + int(a) - 1) print(c[::-1])
replace
2
6
2
6
0
p02629
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { ll n; cin >> n; vector<int> bits(n); int p = 0; while (n > 0) { bits[p++] = (--n) % 26; n /= 26; } for (int i = p - 1; i >= 0; i--) { cout << char(bits[i] + 'a'); } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { ll n; cin >> n; vector<int> bits(100); int p = 0; while (n > 0) { bits[p++] = (--n) % 26; n /= 26; } for (int i = p - 1; i >= 0; i--) { cout << char(bits[i] + 'a'); } return 0; }
replace
8
9
8
9
0
p02629
C++
Runtime Error
#include <algorithm> #include <iostream> #include <numeric> #include <vector> using namespace std; int main() { string s; long long N; long long d = 1; int result[7] = {0}; int i = 0; int l; cin >> N; while (N >= 0) { N -= d; d *= 26; result[i]++; i++; } i--; l = i; d /= 26; N += d; while (d >= 1) { result[i] += N / d; N %= d; d /= 26; i--; } for (int m = l - 1; m >= 0; m--) { s += (result[m] + 'a' - 1); } cout << s; }
#include <algorithm> #include <iostream> #include <numeric> #include <vector> using namespace std; int main() { string s; long long N; long long d = 1; int result[15] = {0}; int i = 0; int l; cin >> N; while (N >= 0) { N -= d; d *= 26; result[i]++; i++; } i--; l = i; d /= 26; N += d; while (d >= 1) { result[i] += N / d; N %= d; d /= 26; i--; } for (int m = l - 1; m >= 0; m--) { s += (result[m] + 'a' - 1); } cout << s; }
replace
10
11
10
11
0
p02629
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, n) for (long long i = 0; i < (n); ++i) #define orep(i, n) for (long long i = 1; i <= (n); ++i) // one rep #define drep(i, n) for (long long i = (n)-1; i >= 0; --i) // down rep #define srep(i, s, t) for (long long i = (s); i < (t); ++i) // set rep #define rng(x) (x).begin(), (x).end() // range #define rrng(a) (a).rbegin(), (a).rend() // reverse range #define isin(x, l, r) ((l) <= (x) && (x) < (r)) #define ru(x, y) (((x) + (y)-1) / (y)) // round up #define fi first #define se second #define eb emplace_back #define fcout cout << fixed << setprecision(15) using namespace std; using ll = long long; using P = pair<int, int>; using vi = vector<int>; using vvi = vector<vi>; using vp = vector<P>; using vl = vector<ll>; template <typename T> void Yes(T flag) { cout << (flag ? "Yes" : "No") << endl; } template <typename T> void pv(vector<T> vec) { cout << "["; for (auto &v : vec) { cout << v << ","; } cout << "]" << endl; } template <> void pv(vector<P> vec) { cout << "["; for (auto &v : vec) { cout << "<" << v.fi << "," << v.se << ">" << ","; } cout << "]" << endl; } template <typename T> void pv2(vector<vector<T>> vec) { for (auto &v : vec) pv(v); } char p(char c) { vector<char> v = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; if (c == 'z') return 'a'; rep(i, 26) { if (c == v.at(i)) return v.at(i + 1); } } int main() { ll n; cin >> n; ll tmp = n; ll i = 26; int mozisuu = 1; while (true) { tmp -= i; if (tmp < 0) break; else n -= i; i *= 26; mozisuu += 1; } i = i / 26; string ans = ""; rep(i, mozisuu) ans += 'a'; n--; int now = 0; while (true) { if (n - i >= 0) { ans.at(now) = p(ans.at(now)); n -= i; } else if (n != 0) { i /= 26; now += 1; } else { break; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (long long i = 0; i < (n); ++i) #define orep(i, n) for (long long i = 1; i <= (n); ++i) // one rep #define drep(i, n) for (long long i = (n)-1; i >= 0; --i) // down rep #define srep(i, s, t) for (long long i = (s); i < (t); ++i) // set rep #define rng(x) (x).begin(), (x).end() // range #define rrng(a) (a).rbegin(), (a).rend() // reverse range #define isin(x, l, r) ((l) <= (x) && (x) < (r)) #define ru(x, y) (((x) + (y)-1) / (y)) // round up #define fi first #define se second #define eb emplace_back #define fcout cout << fixed << setprecision(15) using namespace std; using ll = long long; using P = pair<int, int>; using vi = vector<int>; using vvi = vector<vi>; using vp = vector<P>; using vl = vector<ll>; template <typename T> void Yes(T flag) { cout << (flag ? "Yes" : "No") << endl; } template <typename T> void pv(vector<T> vec) { cout << "["; for (auto &v : vec) { cout << v << ","; } cout << "]" << endl; } template <> void pv(vector<P> vec) { cout << "["; for (auto &v : vec) { cout << "<" << v.fi << "," << v.se << ">" << ","; } cout << "]" << endl; } template <typename T> void pv2(vector<vector<T>> vec) { for (auto &v : vec) pv(v); } char p(char c) { vector<char> v = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; if (c == 'z') return 'a'; rep(i, 26) { if (c == v.at(i)) return v.at(i + 1); } } int main() { ll n; cin >> n; ll tmp = n; ll i = 26; int mozisuu = 1; while (true) { tmp -= i; if (tmp <= 0) break; else n -= i; i *= 26; mozisuu += 1; } i = i / 26; string ans = ""; rep(i, mozisuu) ans += 'a'; n--; int now = 0; while (true) { if (n - i >= 0) { ans.at(now) = p(ans.at(now)); n -= i; } else if (n != 0) { i /= 26; now += 1; } else { break; } } cout << ans << endl; return 0; }
replace
70
71
70
71
TLE
p02629
C++
Time Limit Exceeded
/// Coded by Dhiman Sarker Bappi (Dhimanda) - RMSTU #include <bits/stdc++.h> using namespace std; #define in_file freopen("input.txt", "r", stdin) #define out_file freopen("output.txt", "w", stdout) #define fast ios_base ::sync_with_stdio(0), cin.tie(0), cout.tie(0) #define F first #define S second #define pb push_back #define popb pop_back #define pf push_front #define popf pop_front #define lcm(a, b) (a * b) / gcd(a, b) #define gcd(a, b) __gcd(a, b) #define pi 2 * acos(0) #define elif else if #define ll long long #define nl '\n' #define endl '\n' #define fori(i, b, e) for (int(i) = (b); (i) <= (e); (i)++) #define for0(i, n) for (int(i) = 0; (i) < (n); (i)++) #define sp fixed << setprecision #define all(x) x.begin(), x.end() // ===================== DEBUG ========================== vector<string> vec_spltr(string s) { s += ','; vector<string> res; while (!s.empty()) res.push_back(s.substr(0, s.find(','))), s = s.substr(s.find(',') + 1); return res; } void dbg_out(vector<string> __attribute__((unused)) args, __attribute__((unused)) int idx, __attribute__((unused)) int LINE_NUM) { cerr << endl; } template <typename Head, typename... Tail> void dbg_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) { if (idx > 0) cerr << ", "; else cerr << "Line(" << LINE_NUM << ") "; stringstream ss; ss << H; cerr << args[idx] << " = " << ss.str(); dbg_out(args, idx + 1, LINE_NUM, T...); } #define debug(...) dbg_out(vec_spltr(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__) // ===================== custom function ========================== ll pow_mod(ll nn, ll pp, ll mm) { ll res = 1; while (pp > 0) { if (pp & 1) res = (res * nn) % mm; pp = pp >> 1; nn = (nn * nn) % mm; } return res; } void dhimanda(); // ===================== Main function ========================== int main() { // in_file; // out_file; int t = 1; // cin >> t; fori(i, 1, t) { // printf("Case %d: ", i); dhimanda(); } main(); return 0; } void dhimanda() { ll n; cin >> n; string a = "0abcdefghijklmnopqrstuvwxyz"; string ans = ""; while (n) { ll x = n % 26; if (x == 0) x = 26; ans += a[x]; debug(x, ans, n); n /= 26; if (x == 26) n--; } reverse(all(ans)); cout << ans << endl; } // Contact : chessdhiman@gmail.com
/// Coded by Dhiman Sarker Bappi (Dhimanda) - RMSTU #include <bits/stdc++.h> using namespace std; #define in_file freopen("input.txt", "r", stdin) #define out_file freopen("output.txt", "w", stdout) #define fast ios_base ::sync_with_stdio(0), cin.tie(0), cout.tie(0) #define F first #define S second #define pb push_back #define popb pop_back #define pf push_front #define popf pop_front #define lcm(a, b) (a * b) / gcd(a, b) #define gcd(a, b) __gcd(a, b) #define pi 2 * acos(0) #define elif else if #define ll long long #define nl '\n' #define endl '\n' #define fori(i, b, e) for (int(i) = (b); (i) <= (e); (i)++) #define for0(i, n) for (int(i) = 0; (i) < (n); (i)++) #define sp fixed << setprecision #define all(x) x.begin(), x.end() // ===================== DEBUG ========================== vector<string> vec_spltr(string s) { s += ','; vector<string> res; while (!s.empty()) res.push_back(s.substr(0, s.find(','))), s = s.substr(s.find(',') + 1); return res; } void dbg_out(vector<string> __attribute__((unused)) args, __attribute__((unused)) int idx, __attribute__((unused)) int LINE_NUM) { cerr << endl; } template <typename Head, typename... Tail> void dbg_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) { if (idx > 0) cerr << ", "; else cerr << "Line(" << LINE_NUM << ") "; stringstream ss; ss << H; cerr << args[idx] << " = " << ss.str(); dbg_out(args, idx + 1, LINE_NUM, T...); } #define debug(...) dbg_out(vec_spltr(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__) // ===================== custom function ========================== ll pow_mod(ll nn, ll pp, ll mm) { ll res = 1; while (pp > 0) { if (pp & 1) res = (res * nn) % mm; pp = pp >> 1; nn = (nn * nn) % mm; } return res; } void dhimanda(); // ===================== Main function ========================== int main() { // in_file; // out_file; int t = 1; // cin >> t; fori(i, 1, t) { // printf("Case %d: ", i); dhimanda(); } // main(); return 0; } void dhimanda() { ll n; cin >> n; string a = "0abcdefghijklmnopqrstuvwxyz"; string ans = ""; while (n) { ll x = n % 26; if (x == 0) x = 26; ans += a[x]; debug(x, ans, n); n /= 26; if (x == 26) n--; } reverse(all(ans)); cout << ans << endl; } // Contact : chessdhiman@gmail.com
replace
74
75
74
75
TLE
p02629
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; void solve() { long long n, x = 26, pre = 1; cin >> n; string s; while (n > 0) { int c = n % x; if (!c) c = x; n -= c; s += ((c / pre - 1) + 'a'); x *= 26; pre *= 26; } reverse(s.begin(), s.end()); cout << s << endl; } int main() { int tt = 1; // cin >> tt; while (tt--) solve(); }
#include <bits/stdc++.h> using namespace std; void solve() { long long n, x = 26, pre = 1; cin >> n; string s; while (n > 0) { long long c = n % x; if (!c) c = x; n -= c; s += ((c / pre - 1) + 'a'); x *= 26; pre *= 26; } reverse(s.begin(), s.end()); cout << s << endl; } int main() { int tt = 1; // cin >> tt; while (tt--) solve(); }
replace
9
10
9
10
0
p02629
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; const ll MOD = 1000000007; // const ll MOD = 998244353; const ll INF = MOD * MOD; const long double EPS = 1e-12; struct mint { ll x; 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 { return mint(*this) += a; } mint operator-(const mint a) const { return mint(*this) -= a; } mint operator*(const mint a) const { return mint(*this) *= 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 { return mint(*this) /= a; } }; istream &operator>>(istream &is, const mint &a) { return is >> a.x; } ostream &operator<<(ostream &os, const mint &a) { return os << a.x; } struct edge { ll to, cost; tuple<ll, ll> get_value() { return make_tuple(to, cost); } }; struct fpoint { ld x = 0; ld y = 0; bool operator<(const fpoint &p) const { if (x == p.x) return y < p.y; return x < p.x; } bool operator==(const fpoint &p) const { if (p.x - EPS < x && x < p.x + EPS && p.y - EPS < y && y < p.y + EPS) return true; return false; } bool operator!=(const fpoint &p) const { if (p.x - EPS > x || x > p.x + EPS || p.y - EPS > y || y > p.y + EPS) return true; return false; } fpoint &operator+=(const ld a) { x += a, y += a; return *this; } fpoint &operator-=(const ld a) { x -= a, y -= a; return *this; } fpoint &operator*=(const ld a) { x *= a, y *= a; return *this; } fpoint &operator/=(const ld a) { x /= a, y /= a; return *this; } fpoint &operator+=(const fpoint p) { x += p.x, y += p.y; return *this; } fpoint &operator-=(const fpoint p) { x -= p.x, y -= p.y; return *this; } fpoint &operator*=(const fpoint p) { x *= p.x, y *= p.y; return *this; } fpoint &operator/=(const fpoint p) { x /= p.x, y /= p.y; return *this; } fpoint operator+(const fpoint p) const { return fpoint(*this) += p; } fpoint operator-(const fpoint p) const { return fpoint(*this) -= p; } fpoint operator*(const fpoint p) const { return fpoint(*this) *= p; } fpoint operator/(const fpoint p) const { return fpoint(*this) /= p; } fpoint operator+(const ld a) const { return fpoint(*this) += a; } fpoint operator-(const ld a) const { return fpoint(*this) -= a; } fpoint operator*(const ld a) const { return fpoint(*this) *= a; } fpoint operator/(const ld a) const { return fpoint(*this) /= a; } ld dot(const fpoint &p) const { return x * p.x + y * p.y; } ll cross(const fpoint &p) const { return x * p.y - y * p.x; } ld squared_norm() const { return x * x + y * y; } ld norm() const { return sqrt(x * x + y * y); } tuple<ld, ld> get_value() { return make_tuple(x, y); } ll which_quadrant() const { if (abs(x) < EPS && abs(y) < EPS) return 0; if (y > 0) return x > 0 ? 1 : 2; return x < 0 ? 3 : 4; } bool is_zero() { fpoint z = {0, 0}; return z == *this; } }; struct point { ll x = 0; ll y = 0; bool operator<(const point &p) const { if (x == p.x) return y < p.y; return x < p.x; } bool operator==(const point &p) const { if (x == p.x && y == p.y) return true; return false; } bool operator!=(const point &p) const { if (x != p.x || y != p.y) return true; return false; } point &operator+=(const ll a) { x += a, y += a; return *this; } point &operator-=(const ll a) { x -= a, y -= a; return *this; } point &operator*=(const ll a) { x *= a, y *= a; return *this; } point &operator+=(const point p) { x += p.x, y += p.y; return *this; } point &operator-=(const point p) { x -= p.x, y -= p.y; return *this; } point &operator*=(const point p) { x *= p.x, y *= p.y; return *this; } void operator++(int) { x++, y++; } void operator++() { x++, y++; } void operator--(int) { x--, y--; } void operator--() { x--, y--; } point operator+(const point p) const { return point(*this) += p; } point operator-(const point p) const { return point(*this) -= p; } point operator*(const point p) const { return point(*this) *= p; } point operator+(const ll a) const { return point(*this) += a; } point operator-(const ll a) const { return point(*this) -= a; } point operator*(const ll a) const { return point(*this) *= a; } ll dot(const point &p) const { return x * p.x + y * p.y; } ll cross(const point &p) const { return x * p.y - y * p.x; } ll squared_norm() const { return x * x + y * y; } tuple<ll, ll> get_value() { return make_tuple(x, y); } ll which_quadrant() const { if (x == 0 && y == 0) return 0; if (x >= 0 && y >= 0) return 1; else if (x <= 0 && y >= 0) return 2; else if (x <= 0 && y <= 0) return 3; else return 4; } bool is_zero() { point z = {0, 0}; return z == *this; } fpoint to_fpoint() { fpoint ret = {ld(x), ld(y)}; return ret; } }; // angle_comparator struct { template <typename T> bool operator()(const T p1, const T p2) const { ll q1 = p1.which_quadrant(); ll q2 = p2.which_quadrant(); if (q1 != q2) return q1 < q2; // judge for parallel lines // if p1 cross p2 > 0 -> sin arg(p1 -> o -> p2) > 0 return p1.cross(p2) > 0; } } angle_comparator; struct undirected_edge { ll from; ll to; ll cost; bool operator<(const undirected_edge &ue) const { return cost < ue.cost; } tuple<ll, ll, ll> get_value() { return make_tuple(from, to, cost); } }; struct event { ll loc, val, sgn; bool operator<(const event &e) const { if (loc == e.loc) return sgn == 1; return loc < e.loc; } bool operator>(const event &e) const { if (loc == e.loc) return sgn == -1; return loc > e.loc; } tuple<ll, ll, ll> get_value() { return make_tuple(loc, val, sgn); } }; typedef std::pair<ll, ll> pl; typedef std::tuple<ll, ll, ll> tp3; typedef std::tuple<ll, ll, ll, ll> tp4; typedef std::vector<ll> vl; typedef std::vector<vl> vl2; typedef std::vector<vl2> vl3; typedef std::vector<vl3> vl4; typedef std::vector<mint> vmi; typedef std::vector<vmi> vmi2; typedef std::vector<vmi2> vmi3; typedef std::vector<vmi3> vmi4; typedef std::vector<bool> vb; typedef std::vector<vb> vb2; typedef std::vector<vb2> vb3; typedef std::vector<vb3> vb4; typedef std::vector<pl> vpl; typedef std::vector<tp3> vtp3; typedef std::vector<tp4> vtp4; typedef std::vector<point> points; typedef std::vector<fpoint> fpoints; // priority queue. Taking from the higher value. Don't forget calling !q.empty() typedef std::priority_queue<ll> pq; // priority queue. Taking from the lower value typedef std::priority_queue<ll, vl, greater<ll>> pql; typedef std::vector<vector<edge>> Graph; const ll N_DIGITS = 60; const long double PI = 3.14159265358979323846; const points dirs = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}, // four directions {1, 1}, {-1, 1}, {-1, -1}, {1, -1}, // diagonal {0, 0}}; // self template <typename A, typename B> string to_string(pair<A, B> p); template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p); template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p); string to_string(const string &s) { return '"' + s + '"'; } string to_string(char c) { return string(1, c); } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } string to_string(mint a) { return to_string(a.x); } string to_string(point p) { return "{" + to_string(p.x) + ", " + to_string(p.y) + "}"; } string to_string(fpoint p) { return "{" + to_string(p.x) + ", " + to_string(p.y) + "}"; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; } template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; } string to_string(vector<bool> v) { bool first = true; string res = "{"; for (int i = 0; i < static_cast<int>(v.size()); i++) { if (!first) res += ", "; first = false; res += to_string(v[i]); } res += "}"; return res; } template <size_t N> string to_string(bitset<N> v) { string res = ""; for (size_t i = 0; i < N; i++) res += static_cast<char>('0' + v[i]); return res; } 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; } template <typename T> string to_string(priority_queue<T> &q) { priority_queue<T> copy; bool first = true; string res = "{"; while (!q.empty()) { if (!first) { res += ", "; } first = false; res += to_string(q.top()); copy.push(q.top()); q.pop(); } swap(q, copy); 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...); } #define rep(i, n) for (ll(i) = 0; (i) < (n); (i)++) #define revrep(i, n) for (ll(i) = n - 1; (i) >= 0; (i)--) #define For(i, a, b) for (ll(i) = (a); (i) < (b); (i)++) #define revFor(i, b, a) for (ll(i) = (b)-1; (i) >= (a); (i)--) #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define isUpper(c) ('a' - c > 0) #define isNum(c) (0 <= (c) - '0' && (c) - '0' <= 9) #define toLower(c) char((c) + 0x20) #define toUpper(c) char((c)-0x20) #define pb push_back #define mp make_pair #define mt make_tuple #define pr(a) std::cout << (a) #define prl(a) std::cout << (a) << endl #define prl2(a, b) std::cout << (a) << " " << (b) << endl #define prl3(a, b, c) std::cout << (a) << " " << (b) << " " << (c) << endl #define prl4(a, b, c, d) \ std::cout << (a) << " " << (b) << " " << (c) << " " << (d) << endl #define prs(a) std::cout << (a) << " " #define prs2(a, b) std::cout << (a) << " " << (b) << " " #define prs3(a, b, c) std::cout << (a) << " " << (b) << " " << (c) << " " #define prs4(a, b, c, d) \ std::cout << (a) << " " << (b) << " " << (c) << " " << (d) << " " #define yn(condition) \ if ((condition)) \ prl("Yes"); \ else \ prl("No"); #define YN(condition) \ if ((condition)) \ prl("YES"); \ else \ prl("NO"); #define in1(a) cin >> (a) #define in2(a, b) cin >> (a) >> (b) #define in3(a, b, c) cin >> (a) >> (b) >> (c) #define in4(a, b, c, d) cin >> (a) >> (b) >> (c) >> (d) #define in5(a, b, c, d, e) cin >> (a) >> (b) >> (c) >> (d) >> (e) #define in6(a, b, c, d, e, f) cin >> (a) >> (b) >> (c) >> (d) >> (e) >> (f) #define in7(a, b, c, d, e, f, g) \ cin >> (a) >> (b) >> (c) >> (d) >> (e) >> (f) >> (g) #define e1 first #define e2 second #define popcount(i) __builtin_popcountl((i)) #define Forchar(c, a, z) for (char(c) = (a); (c) <= (z); (c)++) #define cntchar(s, c) count(all((s)), c) #define substring(s, start, end) s.substr((start), (end) - (start) + 1) #define prl_nd(num, digits) \ std::cout << fixed << setprecision(digits) << (num) << endl; #define prl_time(s) \ prl3("Elapsed Time:", 1000.0 * (clock() - s) / CLOCKS_PER_SEC, "[ms]"); #define char_to_str(c) string(1, (c)) #ifdef _LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif 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; } struct MaxFlow { struct F_edge { ll to, rev, capacity; F_edge(ll to, ll rev, ll capacity) : to(to), rev(rev), capacity(capacity) {} }; typedef vector<F_edge> F_edges; vector<F_edges> graph; ll n_vertex; // level is the shortest path to get a given node from the source node. vl level, iter; MaxFlow(ll n_vertex) : n_vertex(n_vertex) { graph.resize(n_vertex); } void add_edge(ll from, ll to, ll capacity) { graph[from].pb({to, ll(graph[to].size()), capacity}); graph[to].pb({from, ll(graph[from].size()) - 1, 0}); } void bfs(ll source) { level = vl(n_vertex, -1); level[source] = 0; queue<ll> q; q.push(source); while (!q.empty()) { ll vertex = q.front(); q.pop(); rep(i, graph[vertex].size()) { ll target = graph[vertex][i].to; ll cap_target = graph[vertex][i].capacity; // if the flow can be into the target node, implement below. if (cap_target > 0 && level[target] < 0) { level[target] = level[vertex] + 1; q.push(target); } } } } ll dfs(ll vertex, ll sink, ll flow) { if (vertex == sink) return flow; for (ll &i = iter[vertex]; i < graph[vertex].size(); i++) { ll target = graph[vertex][i].to; ll cap_target = graph[vertex][i].capacity; ll rev_target = graph[vertex][i].rev; // if capasitiy is not full yet and target is farther, // then assign current flow. if (cap_target > 0 && level[vertex] < level[target]) { ll d = dfs(target, sink, min(cap_target, flow)); if (d > 0) { // if the flow successfully reaches the sink, reduce the // flow from the capacity graph[vertex][i].capacity -= d; graph[target][rev_target].capacity += d; return d; } } } return 0; } ll dinic(ll source, ll sink) { // complexity O(EV^2) ll flow = 0; while (true) { bfs(source); // if there is no path leading to the sink, the maximum flow is 0. if (level[sink] < 0) return flow; iter = vl(n_vertex, 0); ll f; while ((f = dfs(source, sink, INF)) > 0) flow += f; } } }; struct UnionFind { vl parents, set_size; set<ll> root_idx; ll n_groups; UnionFind(ll n) { parents = set_size = vl(n); n_groups = n; rep(i, n) { parents[i] = i; set_size[i] = 1LL; root_idx.insert(i); } } ll root_find(ll x) { if (parents[x] == x) return x; return parents[x] = root_find(parents[x]); } void unite(ll x, ll y) { // priority for x is larger than that of y x = root_find(x); y = root_find(y); if (x == y) return; parents[y] = x, set_size[x] += set_size[y]; root_idx.erase(y); n_groups--; } // connected or not bool is_same(ll x, ll y) { return root_find(x) == root_find(y); } ll size(ll x) { return set_size[root_find(x)]; } ll num_union() const { return n_groups; } }; struct WeightedUnionFind { vl parents, set_size, weights; set<ll> root_idx; ll n_groups; WeightedUnionFind(ll n) { parents = set_size = weights = vl(n); n_groups = n; rep(i, n) { parents[i] = i; set_size[i] = 1LL; weights[i] = 0; root_idx.insert(i); } } ll root_find(ll x) { if (parents[x] == x) return x; ll r = root_find(parents[x]); weights[x] += weights[parents[x]]; return parents[x] = r; } ll weight(ll x) { root_find(x); return weights[x]; } void unite(ll x, ll y, ll w) { // priority for x is larger than that of y w += weight(x) - weight(y); x = root_find(x); y = root_find(y); if (x == y) return; parents[y] = x, set_size[x] += set_size[y]; root_idx.erase(y); weights[y] = w; n_groups--; } // connected or not bool is_same(ll x, ll y) { return root_find(x) == root_find(y); } ll size(ll x) { return set_size[root_find(x)]; } ll num_union() const { return n_groups; } ll difference(ll x, ll y) { return weight(y) - weight(x); } }; struct Doubling { // ABC167D ll n; ll sz; vl2 next; /* next[k + 1][i] := next[k][next[k][i]] next[0][i] := edge[i] e.g. a0, a1, ..., an-1 / 0 <= ai <= n - 1 a0 -> a[a0] -> a[a[a0]] -> ... -> a[a[...[a[0]]]] (m times) Let the function repeatedly inputting a[i] m times be f[m](a[i]) - get(i, x) returns f[x](a[i]) - lower_bound(i, j) returns minimum x which satisfies f[x](a[i]) >= j. if not possible returns n. */ // edge[i]: the step size for one iteration Doubling(vl &edge) : n(edge.size()), sz(62) { next.resize(sz, vl(n, -1)); rep(i, n) next[0][i] = edge[i]; rep(k, sz - 1) rep(i, n) next[k + 1][i] = next[k][next[k][i]]; } ll get(ll i, ll x) { ll ret = i; rep(bit, sz) { if (!(x >> bit & 1)) continue; ret = next[bit][ret]; } return ret; } ll lower_bound(ll i, ll j) { ll cur = i, acc = 0; revrep(wid, sz) { if (next[wid][cur] < j) { acc += 1LL << wid; cur = next[wid][cur]; } } return min(n, acc + 1); } }; template <class T> class LowestCommonAncestor { public: ll N, logN; vl depth, len; T tree; vl2 parents; LowestCommonAncestor(ll n, T &_tree) { N = n; logN = 0; while (N > (1LL << logN)) logN++; depth = len = vl(N); parents = vl2(logN, vl(N)); tree = _tree; init(0, -1, 0, 0); build(); } void init(ll source, ll parent, ll d, ll l) { depth[source] = d; parents[0][source] = parent; len[source] = l; rep(i, tree[source].size()) { ll target = tree[source][i].to; ll cost = tree[source][i].cost; if (target == parent) continue; init(target, source, d + 1, cost + l); } } void build() { rep(k, logN - 1) rep(n, N) { // if there is no parent, -1. // otherwise, the parent of the parent is the parent. if (parents[k][n] < 0) parents[k + 1][n] = -1; else parents[k + 1][n] = parents[k][parents[k][n]]; } } ll query(ll u, ll v) { if (depth[u] > depth[v]) swap(u, v); rep(k, logN) if ((depth[v] - depth[u]) >> k & 1) v = parents[k][v]; if (u == v) return u; revrep(k, logN) { if (parents[k][u] != parents[k][v]) { u = parents[k][u]; v = parents[k][v]; } } return parents[0][u]; } ll distance(ll u, ll v) { ll w = query(u, v); return len[u] + len[v] - 2 * len[w]; } }; struct BinaryIndexedTree { ll n, ini; vl dat; BinaryIndexedTree(ll n, ll ini = 0) : dat(n + 1, ini), n(n), ini(ini){}; // x: 1001 1010 1100 1011 1101 1111 // x & - x: 0001 0010 0100 0001 0001 0001 // ->: 1010 1100 10000 1100 1100 10000 ll update_func(ll val, ll d) { // if maximum -> max(val, dat) // return max(val, d); // if cumulative sum return val + d; } ll query(ll i) { /* v[0] + v[1] + ... + v[i] e.g.) i = 10101 itr1. 10101 -> 10100 itr2. 10100 -> 10000 itr3. 10000 -> 00000 (break) */ if (i < 0) return ini; ll ret = 0; for (ll j = i; j >= 0; j = (j & (j + 1)) - 1) { ret = update_func(ret, dat[j]); } return ret; } ll query(ll l, ll r) { // a[l] + a[l + 1] + ... + a[r - 1] + a[r] return query(r) - query(l - 1); } ll lower_bound(ll key) { // v[0] + v[1] + ... + v[left - 1] < key <= v[0] + v[1] + ... + v[left] if (key <= 0) return 0; ll left = 0, right = 1; while (right <= n) right *= 2; for (ll i = right; i > 0; i /= 2) { if (left + i <= n && dat[left + i - 1] < key) { key -= dat[left + i - 1]; left += i; } } return left; } void update(ll i, ll val) { /* e.g.) i = 10101, n = 11111 itr1. i: 10101, i+1: 10110 -> 10111 itr2. i: 10111, i+1: 11000 -> 11111 (break) */ if (i < 0) return; for (ll j = i; j < n; j |= j + 1) { dat[j] = update_func(val, dat[j]); } } }; struct SegmentTree { ll n, ini, minimize; vl dat; // when seeking minimum // ini = INF // when seeking maximum // ini = -INF SegmentTree(ll n_, bool minimize_ = true) { n = 1; minimize = minimize_; if (minimize) ini = INF; else ini = -INF; while (n < n_) n *= 2; dat.resize(2 * n - 1); rep(i, 2 * n - 1) dat[i] = ini; }; void update(ll idx, ll val) { idx += n - 1; if (minimize && dat[idx] <= val) return; if (!minimize && dat[idx] >= val) return; dat[idx] = val; while (idx > 0) { idx = (idx - 1) / 2; // when seeking minimum if (minimize) dat[idx] = min(dat[idx * 2 + 1], dat[idx * 2 + 2]); // when seeking maximum else dat[idx] = max(dat[idx * 2 + 1], dat[idx * 2 + 2]); } } ll query(ll l, ll r) { // ### NOTE ### // the range is [l, r] // l, l + 1, ..., r r++; // to adjust to this method return query_segment(l, r, 0, 0, n); } ll query_segment(ll a, ll b, ll idx, ll l, ll r) { assert(a < b); if (r <= a || b <= l) return ini; if (a <= l && r <= b) return dat[idx]; else { ll seg1 = query_segment(a, b, idx * 2 + 1, l, (l + r) / 2); ll seg2 = query_segment(a, b, idx * 2 + 2, (l + r) / 2, r); // when seeking minimum if (minimize) return min(seg1, seg2); // when seeking maximum else return max(seg1, seg2); } } }; template <class Target> class RerootingTreeDP { public: using T = typename Target::type; struct DP_edge { ll to, rev; // rev is the index to trace the source node. T value; // objective value }; private: ll n; void dfs_fwd(ll source, ll parent) { ll par_idx = -1; vector<T> values; rep(i, tree[source].size()) { const DP_edge &e = tree[source][i]; if (e.to == parent) { par_idx = i; continue; } dfs_fwd(e.to, source); values.pb(e.value); } // If the parent != -1, update the value on edge from parent to source if (par_idx != -1) { ll src_idx = tree[source][par_idx].rev; // update values on the edge from parent to source tree[parent][src_idx].value = Target::merge(values); } } void dfs_bwd(ll source, ll parent) { vector<T> values; for (auto &&e : tree[source]) values.pb(e.value); values = Target::evaluate(values); rep(i, tree[source].size()) { const DP_edge &e = tree[source][i]; if (e.to == parent) continue; // tree[e.to][e.rev]: e.to -> source tree[e.to][e.rev].value = values[i]; dfs_bwd(e.to, source); } } public: UnionFind uf; vector<vector<DP_edge>> tree; RerootingTreeDP(ll n) : n(n), uf(n), tree(n) {} void add_edge(ll u, ll v, T val) { assert(!uf.is_same(u, v)); tree[u].pb({v, ll(tree[v].size()), val}); tree[v].pb({u, ll(tree[u].size()) - 1, val}); uf.unite(u, v); } void dp() { vb visited(n, false); rep(i, n) { if (visited[uf.root_find(i)]) continue; dfs_fwd(i, -1); visited[uf.root_find(i)] = true; } visited.assign(n, false); rep(i, n) { if (visited[uf.root_find(i)]) continue; dfs_bwd(i, -1); visited[uf.root_find(i)] = true; } } ll size() const { return tree.size(); } }; // ABC160F is one example // Modify the functions evaluate and merge based on given problems struct Merger { using type = ll; // This is the exaple of the number of children static type merge(const vector<type> &value) { // merge the result below the source node // each value is from each child node of the source node // value[i] := f(child i) // Here, we would like to obtain f(source) using f(child i) (i = 0, 1, ..., // n_children) ll ret = 1; for (auto &&v : value) ret += v; return ret; } static vector<type> evaluate(const vector<type> &value) { // value[i] := f(source -> child i) // we would like to obtain f(child i -> source) // child j (j != i) is the grandchildren of child i // represent f(child i -> source) using f(source -> j) (j != i) // L[i + 1] := the result using f(source -> k) (k = 0, 1, ..., i) // R[i] := the result using f(source -> k) (k = i, i + 1, ..., n_children) const ll n_children = value.size(); vl L(n_children + 1, 0), R(n_children + 1, 0); rep(i, n_children) L[i + 1] = L[i] + value[i]; revrep(i, n_children) R[i] = R[i + 1] + value[i]; vl ret(n_children); rep(i, n_children) ret[i] = L[i] + R[i + 1] + 1; return ret; } }; struct StronglyConnectedComponents { ll n, n_cmp; // dag: edges from a cmp to another cmp vl2 graph, graph_rev, dag, cmp; vl order, visited, cmp_idx; StronglyConnectedComponents() {} StronglyConnectedComponents(ll sz) : n(sz), graph(sz), graph_rev(sz), visited(sz), cmp_idx(sz) {} void add_edge(ll from, ll to) { graph[from].pb(to); graph_rev[to].pb(from); } void input(ll m, ll offset = -1) { ll a, b; rep(i, m) { in2(a, b); add_edge(a + offset, b + offset); } } ll operator[](ll k) { return cmp_idx[k]; } void dfs_fwd(ll source) { visited[source] = 1; rep(i, graph[source].size()) { ll target = graph[source][i]; if (!visited[target]) dfs_fwd(target); } order.pb(source); } void dfs_bwd(ll source, ll num) { visited[source] = 1, cmp_idx[source] = num; cmp[num].pb(source); rep(i, graph_rev[source].size()) { ll target = graph_rev[source][i]; if (!visited[target]) dfs_bwd(target, num); } } ll build() { fill(all(visited), 0); order.clear(); rep(i, n) if (!visited[i]) dfs_fwd(i); fill(all(visited), 0); ll num = 0; revrep(i, order.size()) { if (!visited[order[i]]) { dag.pb(vl()); cmp.pb(vl()); dfs_bwd(order[i], num++); } } rep(i, n) for (ll to : graph[i]) if (cmp_idx[i] != cmp_idx[to]) dag[cmp_idx[i]] .pb(cmp_idx[to]); rep(i, num) { sort(all(dag[i])); dag[i].erase(unique(all(dag[i])), dag[i].end()); } return n_cmp = num; } // if the v-th vertex is in loop or not bool in_loop(ll v) { return cmp[cmp_idx[v]].size() > 1; } // if the dag has loops or not bool has_loop() { rep(i, cmp.size()) if (cmp[i].size() > 1) return true; return false; } }; struct CombinationMemo { ll sz, mod; vl facts, facts_inv, minv; CombinationMemo(ll sz, ll _mod) : sz(sz), mod(_mod) { facts.resize(sz + 5); facts_inv.resize(sz + 5); minv.resize(sz + 5); init(); } void init() { facts[0] = facts[1] = 1; minv[1] = 1; facts_inv[0] = facts_inv[1] = 1; For(i, 2, sz + 3) { facts[i] = (i * facts[i - 1]) % mod; minv[i] = mod - minv[mod % i] * (mod / i) % mod; facts_inv[i] = facts_inv[i - 1] * minv[i] % mod; } } ll nCk(ll n, ll r) { if (n == r && n == 0) return 1; else if (n <= 0 || r < 0 || r > n) return 0; ll val = (facts[n] * facts_inv[n - r]) % mod; val *= facts_inv[r]; return val % mod; } ll nPk(ll n, ll r) { if (n == r && n == 0) return 1; else if (n <= 0 || r < 0 || r > n) return 0; ll val = (facts[n] * facts_inv[n - r]) % mod; return val % mod; } }; struct PowerMemo { ll sz, mod, base; vl powB; PowerMemo(ll sz, ll base, ll _mod) : sz(sz), base(base), mod(_mod) { powB.resize(sz + 5); init(); } void init() { powB[0] = 1; rep(i, sz + 3) powB[i + 1] = (powB[i] * base) % mod; } ll operator[](ll k) { return powB[k]; } }; struct Grid2D { Graph graph; ll Width, Height; Grid2D(ll w, ll h) : Width(w), Height(h) { graph.resize(w * h); } ll pos_to_idx(point p) { return p.y * Width + p.x; } point idx_to_pos(ll idx) { return {idx % Width, idx / Width}; } bool undefined_region(point p, vb2 &block) { if (p.x < 0 || p.x > Width - 1) return true; if (p.y < 0 || p.y > Height - 1) return true; if (block[p.x][p.y]) return true; return false; } void build(vb2 &block, ll val = 1) { rep(x, Width) rep(y, Height) { point p = {x, y}; ll idx1 = pos_to_idx(p); ll idx2; if (block[x][y]) continue; rep(i, 4) { point nxt = p + dirs[i]; idx2 = pos_to_idx(nxt); if (!undefined_region(nxt, block)) graph[idx1].pb( {idx2, val}); // dist[idx1][idx2] = val; (warshall-floyd) } } } }; struct Cumulative2D { vl2 cum; ll w, h; Cumulative2D(ll w, ll h) : w(w), h(h) { cum = vl2(w + 1, vl(h + 1, 0)); } template <typename T> void build(vector<T> &vec) { // never forget building rep(x, w + 1) cum[x][0] = 0; rep(y, h + 1) cum[0][y] = 0; rep(y, h) rep(x, w) cum[x + 1][y + 1] = cum[x][y + 1] + vec[x][y]; rep(x, w + 1) rep(y, h) cum[x][y + 1] += cum[x][y]; } ll func(ll x, ll y, ll dx, ll dy) { // 1-indexed // the rectangle of (x, y), (x + dx, y), (x, y + dy) and (x + dx, y + dy) // think about the case of (1, 1, 1, 1). if (x + dx > w || y + dy > h) return -INF; ll val = cum[x + dx][y + dy]; val += cum[x][y]; val -= cum[x][y + dy]; val -= cum[x + dx][y]; return val; } }; struct LinearSystemIncidence { struct Edge { ll idx, to; bool fwd; }; /* Ax = c A: A in R^(V * E) <- given x: x in R^E <- estimate this vector c: c in R^V <- given */ ll n_vertex, n_edge; bool mod2; vl x; vb visited; vector<vector<Edge>> graph; LinearSystemIncidence(ll n, vpl &es, bool mod2 = false) : n_vertex(n), n_edge(es.size()), mod2(mod2) { graph.resize(n); visited.resize(n); rep(i, n_edge) { auto &e = es[i]; graph[e.e1].pb({i, e.e2, true}); graph[e.e2].pb({i, e.e1, false}); } } ll dfs(vl &c, ll src) { visited[src] = true; ll ret = c[src]; for (Edge &e : graph[src]) { if (visited[e.to]) continue; ll tmp = dfs(c, e.to); x[e.idx] = e.fwd ? tmp : -tmp; ret += tmp; if (mod2) x[e.idx] = ((x[e.idx] % 2) + 2) % 2; } return mod2 ? ret % 2 : ret; } bool solve(vl &c) { x.assign(n_edge, 0); visited.assign(n_vertex, false); rep(src, n_vertex) { if (visited[src]) continue; if (dfs(c, src)) return false; } return true; } }; struct BigInt10 { string num; ll sz; map<ll, vl> mod; BigInt10(string num) : num(num), sz(num.size()) {} void compute_mod(ll _mod) { mod[_mod] = vl(sz + 1, 0); ll tenth = 1; rep(i, sz) { ll top = num[sz - 1 - i] - '0'; mod[_mod][i + 1] = (mod[_mod][i] + top * tenth) % _mod; (tenth *= 10) %= _mod; } } ll n_digits() { return num.length(); } ll operator[](ll k) { return num[k] - '0'; } ll subseq_mod(ll l, ll r, ll m) { // s1 s2 ... sl .... sr ... sn // the modulo of 00 ... 0 sl ... sr 00 ... 0 assert(l <= r); ll tot = mod[m][sz]; ll ls = (tot - mod[m][sz - l] + m) % m; ll rs = mod[m][sz - r]; return (tot - ls - rs + 2 * m) % m; } bool operator<(BigInt10 &n2) const { if (num.length() < n2.n_digits()) return true; else if (num.length() > n2.n_digits()) return false; return num < n2.num; } bool operator>(BigInt10 &n2) const { if (num.length() > n2.n_digits()) return true; else if (num.length() < n2.n_digits()) return false; return num > n2.num; } }; struct BigInt2 { string num; ll sz; map<ll, vl> mod; ll nz; BigInt2(string num) : num(num), sz(num.size()) { nz = 0; rep(i, sz) if (num[i] == '1') nz++; } void compute_mod(ll _mod) { mod[_mod] = vl(sz + 1, 0); ll base = 1; rep(i, sz) { ll top = num[sz - 1 - i] - '0'; mod[_mod][i + 1] = (mod[_mod][i] + top * base) % _mod; (base *= 2) %= _mod; } } ll n_digits() { return num.length(); } ll bit_count() { return nz; } ll operator[](ll k) { return num[k] - '0'; } ll subseq_mod(ll l, ll r, ll m) { // s1 s2 ... sl .... sr ... sn // the modulo of 00 ... 0 sl ... sr 00 ... 0 assert(l <= r); ll tot = mod[m][sz]; ll ls = (tot - mod[m][sz - l] + m) % m; ll rs = mod[m][sz - r]; return (tot - ls - rs + 2 * m) % m; } bool operator<(BigInt10 &n2) const { if (num.length() < n2.n_digits()) return true; else if (num.length() > n2.n_digits()) return false; return num < n2.num; } bool operator>(BigInt10 &n2) const { if (num.length() > n2.n_digits()) return true; else if (num.length() < n2.n_digits()) return false; return num > n2.num; } }; ll gcd(ll m, ll n) { ll a = max(m, n); ll b = min(m, n); while (b != 1 && b != 0) { a %= b; swap(a, b); } return b == 1 ? 1 : a; } ll lcm(ll m, ll n) { return m / gcd(m, n) * n; } ll power_normal(ll a, ll power) { ll value = 1; while (power != 0) { if (power & 1) value = value * a; a = a * a; power = power >> 1; } return value; } ll power_mod(ll a, ll power, ll mod) { ll value = 1; while (power != 0) { if (power & 1) value = (value * a) % mod; a = (a * a) % mod; power = power >> 1; } return value % mod; } ll modinv(ll a, ll mod) { return power_mod(a, mod - 2, mod); } ll combination(ll n, ll r, ll mod) { if (n == r && n == 0) return 1; else if (n <= 0 || r < 0 || r > n) return 0; ll numerator = 1; ll denomenator = 1; for (ll i = 0; i < r; i++) { ll num = (n - i) % mod, den = (i + 1) % mod; (numerator *= num) %= mod; (denomenator *= modinv(den, mod)) %= mod; } return (numerator * denomenator) % mod; } vl2 pascal_triangle(ll n) { /* Complexity: O(n^2) The upper bound of n is nearly 50. Parameters ---------- n; the size of returned vector Returns ------- comb[i][j]: combination(i, j). 0 <= i <= n, 0 <= j <= i */ vl2 comb(n + 1, vl(n + 1)); comb[0][0] = 1; For(i, 1, n + 1) rep(j, i + 1) { comb[i][j] += comb[i - 1][j]; if (j > 0) comb[i][j] += comb[i - 1][j - 1]; } return comb; } ld log_combination(ll n, ll r) { if (n == r && n == 0) return 0; else if (n <= 0 || r < 0 || r > n) return -INF; ld val = 0; for (ll i = 0; i < r; i++) { val += log(n - i); val -= log(i + 1); } return val; } string bin_expression(ll n, ll dig) { string s = ""; rep(i, dig) { s += to_string(n % 2); n /= 2; } reverse(all(s)); return s; } bool is_prime(ll n) { if (n <= 1) return false; for (ll i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } map<ll, ll> prime_factorization(ll n) { ll i = 2; map<ll, ll> table; while (i * i <= n) { while (n % i == 0) { table[i]++; n /= i; } i++; } if (n > 1) table[n] = 1; return table; } vl divisor_table(ll n) { vl table; ll i = 1; while (i * i <= n) { if (n % i == 0) { table.pb(i); if (i * i != n) table.pb(n / i); } i++; } sort(all(table)); return table; } vl base_converter(ll num, ll base, ll sz) { vl coefs(sz); ll cur = 0; while (num) { ll val = num % base; assert(cur + 2 <= sz); coefs[cur++] = val; num -= val, num /= base; } while (cur < sz) coefs[cur++] = 0; return coefs; } ll next_combination(ll sub) { /* nCk ll bit = (1 << k) - 1; for (; bit < (1 << n); bit = next_combination(bit)){ bool ith = bit & (1 << i); procedures... } sub & -sub: the binary which shares the last digit whose value is 1 in sub sub + x : carry up the last digit ~y : the binary whose digits are 1 if y's digit is 0. (sub & ~y) / x: reduce the same number of 0s after first 1 in x from (sub & ~y). */ ll x = sub & -sub, y = sub + x; if (x != 0) return (((sub & ~y) / x) >> 1) | y; else return INF; } // just change the input if you want to change the target. // If you want to check the common sequences in two strings, // combine them. e.g. ABC150F template <typename T> vl z_algorithm(T &s, ll n) { /* Paramters --------- T: the string or list of interest n: the size of string or list Returns ------- res[i] is the maximum number of K which satisfies s[:K] == s[i:i + K] for each i = 0, 1, 2, ..., n - 1. */ vl res(n); res[0] = n; ll i1 = 1, i2 = 0; while (i1 < n) { /* i1: the starting point i2: the length of substring */ while (i1 + i2 < n && s[i2] == s[i1 + i2]) ++i2; res[i1] = i2; if (i2 == 0) { ++i1; continue; } ll i3 = 1; // update the already seen points while (i1 + i3 < n && i3 + res[i3] < i2) { res[i1 + i3] = res[i3]; ++i3; } // update up to i1 + i3 and the next possible minimum length is i2 - i3 (= // res[i3]) i1 += i3, i2 -= i3; } return res; } ll string_to_ll(string s) { ll l = s.length(); ll idx = 0; ll val = 0; ll tenth = 1; while (idx < l) { ll m = s[l - 1 - idx] - '0'; val += (m * tenth); tenth *= 10; idx++; } return val; } string reflected_string(string s) { string t, u; ll n = s.length(); t = s; reverse(all(t)); u = substring(t, 0, n - 2) + s + substring(t, 1, n - 1); return u; } ld distance_between_point_line(point l_begin, point l_end, point p) { ll xl1 = l_begin.x, yl1 = l_begin.y; ll xl2 = l_end.x, yl2 = l_end.y; ll xp = p.x, yp = p.y; ll a = yl2 - yl1; ll b = -xl2 + xl1; ll c = -a * xl2 - b * yl2; return abs(ld(a * xp + b * yp + c)) / ld(sqrt(a * a + b * b)); } bool is_cross(point l1_begin, point l1_end, point l2_begin, point l2_end) { ll x1 = l1_begin.x, y1 = l1_begin.y; ll x2 = l1_end.x, y2 = l1_end.y; ll x3 = l2_begin.x, y3 = l2_begin.y; ll x4 = l2_end.x, y4 = l2_end.y; ll val1 = (x1 - x2) * (y3 - y1) + (y1 - y2) * (x1 - x3); ll val2 = (x1 - x2) * (y4 - y1) + (y1 - y2) * (x1 - x4); ll val3 = (x3 - x4) * (y1 - y3) + (y3 - y4) * (x3 - x1); ll val4 = (x3 - x4) * (y2 - y3) + (y3 - y4) * (x3 - x2); return val1 * val2 < 0 && val3 * val4 < 0; } template <typename T> bool isColinear(T p1, T p2, T p3) { T v1 = p2 - p1, v2 = p3 - p1; return v1.x * v2.y == v1.y * v2.x; } template <typename T> T PerpendicularBisector(T p1, T p2) { T vec = p2 - p1; assert(!vec.is_zero()); T ret = {vec.y, -vec.x}; return ret; } template <typename T> ld Distance2DPoints(T p1, T p2) { T vec = (p1 - p2) * (p1 - p2); return sqrt(vec.x + vec.y); } ll SquaredDistance2DPoints(point p1, point p2) { point vec = (p1 - p2) * (p1 - p2); return vec.x + vec.y; } ld space_of_triangle(point p1, point p2, point p3) { ll x1 = p1.x, y1 = p1.y; ll x2 = p2.x, y2 = p2.y; ll x3 = p3.x, y3 = p3.y; ll v1 = x2 - x1; ll u1 = y2 - y1; ll v2 = x3 - x1; ll u2 = y3 - y1; ld s = ld(v1 * u2 - u1 * v2) / ld(2); return abs(s); } pair<point, ll> OuterCenter(point p1, point p2, point p3) { // the center of circle on the given three points // return the determinant value and the product of center points and 2 * // determinant value point ret; if (isColinear(p1, p2, p3)) { ll d1 = SquaredDistance2DPoints(p1, p2); ll d2 = SquaredDistance2DPoints(p2, p3); ll d3 = SquaredDistance2DPoints(p3, p1); if (d1 >= d2 && d1 >= d3) { ret = p1 + p2; return mp(ret, 2); } else if (d2 >= d1 && d2 >= d3) { ret = p2 + p3; return mp(ret, 2); } else { ret = p3 + p1; return mp(ret, 2); } } point pv1 = PerpendicularBisector(p1, p2); point pv2 = PerpendicularBisector(p1, p3); point cv1_2x = p1 + p2, cv2_2x = p1 + p3; // cv1 + k pv1 == cv2 + m pv2 // (pv1x -pv2x) (k) = (cv2x - cv1x) // (pv1y -pv2y) (m) = (cv2y - cv1y) ll det_inv = -pv1.x * pv2.y + pv1.y * pv2.x; ll x1_2x = cv2_2x.x - cv1_2x.x, x2_2x = cv2_2x.y - cv1_2x.y; pl c_2x_det = {-pv2.y * x1_2x + pv2.x * x2_2x, -pv1.y * x1_2x + pv1.x * x2_2x}; // ret.x = ld(cv1_2x.x * det_inv + pv1.x * c_2x_det.e1) / ld(2 * det_inv); // ret.y = ld(cv1_2x.y * det_inv + pv1.y * c_2x_det.e1) / ld(2 * det_inv); ret.x = cv1_2x.x * det_inv + pv1.x * c_2x_det.e1; ret.y = cv1_2x.y * det_inv + pv1.y * c_2x_det.e1; ll jacobian = 2 * det_inv; return mp(ret, jacobian); } ll inversion_number(vl a, ll a_max) { /* Paramters --------- a: vector<ll> All the elements must be non-negative. Prefably the elements are compressed to reduce the computational cost. a_max: ll The maximum value of the vector a or the value bigger than the value stated previously. */ BinaryIndexedTree bit(a_max + 1); ll val = 0; rep(i, a.size()) { // i is the number of elements that have lower index than a[i]. // call the number of elements that have lower value than a[i] // by subtracting these two, the residual number is the number of elements // that have larger value. val += i - bit.query(a[i] - 1); // cumulative sum from 0 to a[i] - 1 bit.update(a[i], 1); } return val; } ld bin_search(ld left, ld right, bool lb, function<bool(ld)> judge) { ld mid; while (right - left > EPS) { mid = (right + left) / 2; if (lb) { if (judge(mid)) right = mid; else left = mid + EPS; } else { if (judge(mid)) left = mid; else right = mid - EPS; } } return right; } ll bin_search(ll left, ll right, bool lb, function<bool(ll)> judge) { ll mid; while (right > left) { if (lb) { // if true (satisfies the condition), range shifts smaller direction mid = (right + left) / 2; if (judge(mid)) right = mid; else left = mid + 1; } else { // if true (satisfies the condition), range shitfs larger direction mid = (right + left + 1) / 2; if (judge(mid)) left = mid; else right = mid - 1; } } return right; } ld trinary_search(ld left, ld right, function<ld(ld)> func) { // Care the value EPS!!! Compare to the condition while (abs(right - left) > EPS) { ld left2 = (2.0 * left + right) / 3.0; ld right2 = (left + 2.0 * right) / 3.0; ld f1 = func(left2); ld f2 = func(right2); if (f1 <= f2) right = right2; else if (f2 <= f1) left = left2; } return right; } template <typename T> vector<T> compress(vector<T> v) { // sort and remove all the duplicated values sort(all(v)); v.erase(unique(all(v)), v.end()); return v; } template <typename T> map<T, ll> dict(const vector<T> &v) { map<T, ll> d; rep(i, v.size()) d[v[i]] = i; return d; } points compress2D(vl xs, vl ys) { /* NOTE ---- Add the corner points if required */ ll n = xs.size(); vl xcs = compress(xs), ycs = compress(ys); map<ll, ll> xd = dict(xcs), yd = dict(ycs); points ps(n); rep(i, n) xs[i] = xd[xs[i]], ys[i] = yd[ys[i]]; rep(i, n) ps[i] = {xs[i], ys[i]}; sort(all(ps)); return ps; } void GaussJordanBitVector(vl &bs) { ll n = bs.size(); ll rank = 0; ll j = 0; revrep(i, N_DIGITS) { for (j = rank; j < n; j++) if (bs[j] & (1LL << i)) break; if (j == n) continue; if (j > rank) bs[rank] ^= bs[j]; for (j = 0; j < n; j++) if (j != rank) bs[j] = min(bs[j], bs[j] ^ bs[rank]); rank++; } } ll kruskal(vector<undirected_edge> &es, ll n_vertex) { // O(ElogE) sort(all(es)); UnionFind uf(n_vertex); ll min_cost = 0; rep(i, es.size()) { undirected_edge &e = es[i]; if (!uf.is_same(e.from, e.to)) { min_cost += e.cost; uf.unite(e.from, e.to); } } return min_cost; } ll LongestIncreasedSequence(vl &v) { ll n = v.size(); vl dp(n, INF); rep(i, n) * lower_bound(all(dp), v[i]) = v[i]; return lower_bound(all(dp), INF) - dp.begin(); } void dijkstra(ll start, Graph &graph, vl &dist, vl &vertex_pre, bool trace = false) { priority_queue<pl, vpl, greater<pl>> q; ll n = graph.size(); dist = vl(n, INF); if (trace) vertex_pre = vl(n, -1); dist[start] = 0; q.push(pl(0, start)); while (!q.empty()) { ll idx, cost; tie(cost, idx) = q.top(); q.pop(); if (dist[idx] < cost) continue; for (auto &e : graph[idx]) { if (chmin(dist[e.to], dist[idx] + e.cost)) { if (trace) vertex_pre[e.to] = idx; q.push(pl(dist[e.to], e.to)); } } } } vl get_predecessor(ll g, vl &vertex_pre) { vl path; for (; g != -1; g = vertex_pre[g]) path.pb(g); reverse(all(path)); return path; } void warshall_floyd(vl2 &dist) { ll n = dist.size(); // Dont forget the initialization // rep(i, n) rep(j, n) dist[i][j] = INF * (i != j); rep(k, n) rep(i, n) rep(j, n) dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); } // ABC061D bool find_negative_cycle(ll n, ll goal, Graph &graph, vl &dist) { rep(i, n) rep(v, n) rep(k, graph[v].size()) { edge e = graph[v][k]; if (dist[e.to] != INF && dist[e.to] > dist[v] + e.cost) { dist[e.to] = -INF; if (goal == -1) return true; else if (goal == e.to) return true; } } return false; } bool bellman_ford(ll start, ll goal, Graph &graph, vl &dist) { // if there is a closed circuit, it returns false. (when goal == -1) // if the distance to goal cannot be obtained, it returns false (when goal != // -1) ll n = graph.size(); dist = vl(n, INF); dist[start] = 0; rep(i, n) rep(v, n) rep(k, graph[v].size()) { edge e = graph[v][k]; if (dist[v] != INF && dist[e.to] > dist[v] + e.cost) dist[e.to] = dist[v] + e.cost; } if (find_negative_cycle(n, goal, graph, dist)) return false; return true; } void Euler_Tour(Graph &tree, vl &euler_tour, vl &in, vl &out, ll anc = 0) { // record only when we first reach a node and leave the node. ll n = tree.size(); vb visited(n, false); in = out = vl(n); auto dfs = [&](auto dfs, ll source) -> void { visited[source] = true; in[source] = euler_tour.size(); euler_tour.pb(source); bool is_leaf = true; for (auto &e : tree[source]) { ll target = e.to; if (visited[target]) continue; else is_leaf = false; dfs(dfs, target); } euler_tour.pb(-source); out[source] = euler_tour.size() - 1; }; dfs(dfs, anc); } void Euler_Tour2(Graph &tree, vl &euler_tour, vl &in, vl &out, ll anc = 0) { // record everytime we reach a node ll n = tree.size(); vb visited(n, false); in = out = vl(n); auto dfs = [&](auto dfs, ll source) -> void { visited[source] = true; in[source] = euler_tour.size(); euler_tour.pb(source); bool is_leaf = true; for (auto &e : tree[source]) { ll target = e.to; if (visited[target]) continue; else is_leaf = false; dfs(dfs, target); euler_tour.pb(source); } out[source] = euler_tour.size() - 1; }; dfs(dfs, anc); } std::mt19937 create_rand_engine() { std::random_device rnd; std::vector<std::uint_least32_t> v(10); std::generate(v.begin(), v.end(), std::ref(rnd)); std::seed_seq seed(v.begin(), v.end()); return std::mt19937(seed); } vl generate_unique_array(const size_t sz, ll vm, ll vM) { const size_t range = static_cast<size_t>(vM - vm + 1); const size_t num = static_cast<size_t>(sz * 1.2); assert(vm <= vM); assert(sz <= range); vl ret; auto engine = create_rand_engine(); std::uniform_int_distribution<ll> distribution(vm, vM); while (ret.size() < sz) { while (ret.size() < num) ret.pb(distribution(engine)); sort(all(ret)); auto unique_end = unique(all(ret)); if (sz < distance(ret.begin(), unique_end)) unique_end = next(ret.begin(), sz); ret.erase(unique_end, ret.end()); } return ret; } vl generate_array(const size_t sz, ll vm, ll vM) { const size_t range = static_cast<size_t>(vM - vm + 1); assert(vm <= vM); vl ret; auto engine = create_rand_engine(); std::uniform_int_distribution<ll> distribution(vm, vM); while (ret.size() < sz) ret.pb(distribution(engine)); return ret; } void read_vector(ll n, vl &v, ll offset = 0) { v.resize(n); rep(i, n) { in1(v[i]); v[i] += offset; } } void read_points(ll n, points &ps, ll offset = 0) { ps.resize(n); rep(i, n) { ll x, y; in2(x, y); ps[i] = {x, y}; ps[i] += offset; } } void read_2DMap(ll w, ll h, vb2 &block, char b) { block = vb2(w, vb(h, false)); string s; rep(y, h) { in1(s); rep(x, w) block[x][y] = (s[x] == b); } } /* diameter of tree Graph tree; ll dM = 0, vM = 0, v2 = 0; void dfs1(ll source, ll parent, ll d){ if (d > dM) dM = d, vM = source; rep(i, tree[source].size()){ ll target = tree[source][i].to; if (target == parent) continue; dfs1(target, source, d + 1); } } void dfs2(ll source, ll parent, ll d){ if (dM <= d) dM = d, v2 = source; rep(i, tree[source].size()){ ll target = tree[source][i].to; if (target == parent) continue; dfs2(target, source, d + 1); } } dfs(0, -1, 0); dfs2(vM, -1, 0); prl2(vM + 1, v2 + 1); // the two edges of tree */ /* #5. shakutori method (syakutori, two pointers technique) // 1. strech right side while the condition is met. // 2. renew the answer // 3. increments left side // 4. Back to 1. (l <= r must be satisfied all the time.) ll l = 0; ll r = 0; while (l < n){ if (l == r) r++; while(r < n && cond) r++; l++; } prl(answer); #11. bfs ABC146D, ABC007C 1. first create a graph. 2. start searching from a node. 3. do some processes and push nodes connected with a given target node in BFS. 4. repeat a series of procedure until queue is empty. ll n; in1(n); queue<tp3> q; vb visited(n, false); auto bfs = [&](ll src, ll par){ visited[src] = true; for (auto& e: graph[src]){ if (e.to != par && !visited[e.to]){ q.push(mt(e.to, src, ...)); } } }; q.push(mt(0, -1, 0)); while(!q.empty()){ ll src, par, ...; tie(src, par, ...) = q.front(); q.pop(); bfs(src, par, ...); } */ /* x: 1001 1010 1100 1011 1101 1111 x & ~ x: 0001 0010 0100 0001 0001 0001 sum: 1010 1100 10000 1100 1100 10000 ####### Attention ####### S | (1 << i) -> S union {i} S & ~(1 << i) -> S - {i} # inclusion-exclusion principle |U[i = 1 to n] Ai| = sum[i = 1 to n] |Ai| - sum[i < j]|Ai ^ Aj| + ... + (-1)^(n - 1) |^[i = 1 to n]Ai| */ const ll MAX_N = 200005; const size_t MAX_BIT = 160 * 160 + 10; typedef bitset<MAX_BIT> bts; typedef vector<bts> vbt; typedef vector<vbt> vbt2; typedef vector<vbt2> vbt3; void solve() { // generate_array(size, min, max); ll n; string ans = ""; in1(n); ll p = 26, leng = 1; while (n > p) { n -= p; p *= 26; leng++; } vl cs = base_converter(n - 1, 26, leng); reverse(all(cs)); rep(i, cs.size()) ans += char('a' + cs[i]); prl(ans); // ### DEBUG PART ### auto naive_solver = [&]() { }; #ifdef _LOCAL naive_solver(); #endif } void test(ll num = 0, bool verbose = false) { rep(i, max(1LL, num)) { ll t = clock(); if (verbose) prl4("\n#####", i + 1, "#####", "\n## Answer ##"); solve(); if (verbose) { prl(""); prl_time(t); } } } int main(void) { #ifdef _LOCAL test(6, true); #else test(0, false); #endif return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; const ll MOD = 1000000007; // const ll MOD = 998244353; const ll INF = MOD * MOD; const long double EPS = 1e-12; struct mint { ll x; 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 { return mint(*this) += a; } mint operator-(const mint a) const { return mint(*this) -= a; } mint operator*(const mint a) const { return mint(*this) *= 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 { return mint(*this) /= a; } }; istream &operator>>(istream &is, const mint &a) { return is >> a.x; } ostream &operator<<(ostream &os, const mint &a) { return os << a.x; } struct edge { ll to, cost; tuple<ll, ll> get_value() { return make_tuple(to, cost); } }; struct fpoint { ld x = 0; ld y = 0; bool operator<(const fpoint &p) const { if (x == p.x) return y < p.y; return x < p.x; } bool operator==(const fpoint &p) const { if (p.x - EPS < x && x < p.x + EPS && p.y - EPS < y && y < p.y + EPS) return true; return false; } bool operator!=(const fpoint &p) const { if (p.x - EPS > x || x > p.x + EPS || p.y - EPS > y || y > p.y + EPS) return true; return false; } fpoint &operator+=(const ld a) { x += a, y += a; return *this; } fpoint &operator-=(const ld a) { x -= a, y -= a; return *this; } fpoint &operator*=(const ld a) { x *= a, y *= a; return *this; } fpoint &operator/=(const ld a) { x /= a, y /= a; return *this; } fpoint &operator+=(const fpoint p) { x += p.x, y += p.y; return *this; } fpoint &operator-=(const fpoint p) { x -= p.x, y -= p.y; return *this; } fpoint &operator*=(const fpoint p) { x *= p.x, y *= p.y; return *this; } fpoint &operator/=(const fpoint p) { x /= p.x, y /= p.y; return *this; } fpoint operator+(const fpoint p) const { return fpoint(*this) += p; } fpoint operator-(const fpoint p) const { return fpoint(*this) -= p; } fpoint operator*(const fpoint p) const { return fpoint(*this) *= p; } fpoint operator/(const fpoint p) const { return fpoint(*this) /= p; } fpoint operator+(const ld a) const { return fpoint(*this) += a; } fpoint operator-(const ld a) const { return fpoint(*this) -= a; } fpoint operator*(const ld a) const { return fpoint(*this) *= a; } fpoint operator/(const ld a) const { return fpoint(*this) /= a; } ld dot(const fpoint &p) const { return x * p.x + y * p.y; } ll cross(const fpoint &p) const { return x * p.y - y * p.x; } ld squared_norm() const { return x * x + y * y; } ld norm() const { return sqrt(x * x + y * y); } tuple<ld, ld> get_value() { return make_tuple(x, y); } ll which_quadrant() const { if (abs(x) < EPS && abs(y) < EPS) return 0; if (y > 0) return x > 0 ? 1 : 2; return x < 0 ? 3 : 4; } bool is_zero() { fpoint z = {0, 0}; return z == *this; } }; struct point { ll x = 0; ll y = 0; bool operator<(const point &p) const { if (x == p.x) return y < p.y; return x < p.x; } bool operator==(const point &p) const { if (x == p.x && y == p.y) return true; return false; } bool operator!=(const point &p) const { if (x != p.x || y != p.y) return true; return false; } point &operator+=(const ll a) { x += a, y += a; return *this; } point &operator-=(const ll a) { x -= a, y -= a; return *this; } point &operator*=(const ll a) { x *= a, y *= a; return *this; } point &operator+=(const point p) { x += p.x, y += p.y; return *this; } point &operator-=(const point p) { x -= p.x, y -= p.y; return *this; } point &operator*=(const point p) { x *= p.x, y *= p.y; return *this; } void operator++(int) { x++, y++; } void operator++() { x++, y++; } void operator--(int) { x--, y--; } void operator--() { x--, y--; } point operator+(const point p) const { return point(*this) += p; } point operator-(const point p) const { return point(*this) -= p; } point operator*(const point p) const { return point(*this) *= p; } point operator+(const ll a) const { return point(*this) += a; } point operator-(const ll a) const { return point(*this) -= a; } point operator*(const ll a) const { return point(*this) *= a; } ll dot(const point &p) const { return x * p.x + y * p.y; } ll cross(const point &p) const { return x * p.y - y * p.x; } ll squared_norm() const { return x * x + y * y; } tuple<ll, ll> get_value() { return make_tuple(x, y); } ll which_quadrant() const { if (x == 0 && y == 0) return 0; if (x >= 0 && y >= 0) return 1; else if (x <= 0 && y >= 0) return 2; else if (x <= 0 && y <= 0) return 3; else return 4; } bool is_zero() { point z = {0, 0}; return z == *this; } fpoint to_fpoint() { fpoint ret = {ld(x), ld(y)}; return ret; } }; // angle_comparator struct { template <typename T> bool operator()(const T p1, const T p2) const { ll q1 = p1.which_quadrant(); ll q2 = p2.which_quadrant(); if (q1 != q2) return q1 < q2; // judge for parallel lines // if p1 cross p2 > 0 -> sin arg(p1 -> o -> p2) > 0 return p1.cross(p2) > 0; } } angle_comparator; struct undirected_edge { ll from; ll to; ll cost; bool operator<(const undirected_edge &ue) const { return cost < ue.cost; } tuple<ll, ll, ll> get_value() { return make_tuple(from, to, cost); } }; struct event { ll loc, val, sgn; bool operator<(const event &e) const { if (loc == e.loc) return sgn == 1; return loc < e.loc; } bool operator>(const event &e) const { if (loc == e.loc) return sgn == -1; return loc > e.loc; } tuple<ll, ll, ll> get_value() { return make_tuple(loc, val, sgn); } }; typedef std::pair<ll, ll> pl; typedef std::tuple<ll, ll, ll> tp3; typedef std::tuple<ll, ll, ll, ll> tp4; typedef std::vector<ll> vl; typedef std::vector<vl> vl2; typedef std::vector<vl2> vl3; typedef std::vector<vl3> vl4; typedef std::vector<mint> vmi; typedef std::vector<vmi> vmi2; typedef std::vector<vmi2> vmi3; typedef std::vector<vmi3> vmi4; typedef std::vector<bool> vb; typedef std::vector<vb> vb2; typedef std::vector<vb2> vb3; typedef std::vector<vb3> vb4; typedef std::vector<pl> vpl; typedef std::vector<tp3> vtp3; typedef std::vector<tp4> vtp4; typedef std::vector<point> points; typedef std::vector<fpoint> fpoints; // priority queue. Taking from the higher value. Don't forget calling !q.empty() typedef std::priority_queue<ll> pq; // priority queue. Taking from the lower value typedef std::priority_queue<ll, vl, greater<ll>> pql; typedef std::vector<vector<edge>> Graph; const ll N_DIGITS = 60; const long double PI = 3.14159265358979323846; const points dirs = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}, // four directions {1, 1}, {-1, 1}, {-1, -1}, {1, -1}, // diagonal {0, 0}}; // self template <typename A, typename B> string to_string(pair<A, B> p); template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p); template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p); string to_string(const string &s) { return '"' + s + '"'; } string to_string(char c) { return string(1, c); } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } string to_string(mint a) { return to_string(a.x); } string to_string(point p) { return "{" + to_string(p.x) + ", " + to_string(p.y) + "}"; } string to_string(fpoint p) { return "{" + to_string(p.x) + ", " + to_string(p.y) + "}"; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; } template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; } string to_string(vector<bool> v) { bool first = true; string res = "{"; for (int i = 0; i < static_cast<int>(v.size()); i++) { if (!first) res += ", "; first = false; res += to_string(v[i]); } res += "}"; return res; } template <size_t N> string to_string(bitset<N> v) { string res = ""; for (size_t i = 0; i < N; i++) res += static_cast<char>('0' + v[i]); return res; } 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; } template <typename T> string to_string(priority_queue<T> &q) { priority_queue<T> copy; bool first = true; string res = "{"; while (!q.empty()) { if (!first) { res += ", "; } first = false; res += to_string(q.top()); copy.push(q.top()); q.pop(); } swap(q, copy); 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...); } #define rep(i, n) for (ll(i) = 0; (i) < (n); (i)++) #define revrep(i, n) for (ll(i) = n - 1; (i) >= 0; (i)--) #define For(i, a, b) for (ll(i) = (a); (i) < (b); (i)++) #define revFor(i, b, a) for (ll(i) = (b)-1; (i) >= (a); (i)--) #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define isUpper(c) ('a' - c > 0) #define isNum(c) (0 <= (c) - '0' && (c) - '0' <= 9) #define toLower(c) char((c) + 0x20) #define toUpper(c) char((c)-0x20) #define pb push_back #define mp make_pair #define mt make_tuple #define pr(a) std::cout << (a) #define prl(a) std::cout << (a) << endl #define prl2(a, b) std::cout << (a) << " " << (b) << endl #define prl3(a, b, c) std::cout << (a) << " " << (b) << " " << (c) << endl #define prl4(a, b, c, d) \ std::cout << (a) << " " << (b) << " " << (c) << " " << (d) << endl #define prs(a) std::cout << (a) << " " #define prs2(a, b) std::cout << (a) << " " << (b) << " " #define prs3(a, b, c) std::cout << (a) << " " << (b) << " " << (c) << " " #define prs4(a, b, c, d) \ std::cout << (a) << " " << (b) << " " << (c) << " " << (d) << " " #define yn(condition) \ if ((condition)) \ prl("Yes"); \ else \ prl("No"); #define YN(condition) \ if ((condition)) \ prl("YES"); \ else \ prl("NO"); #define in1(a) cin >> (a) #define in2(a, b) cin >> (a) >> (b) #define in3(a, b, c) cin >> (a) >> (b) >> (c) #define in4(a, b, c, d) cin >> (a) >> (b) >> (c) >> (d) #define in5(a, b, c, d, e) cin >> (a) >> (b) >> (c) >> (d) >> (e) #define in6(a, b, c, d, e, f) cin >> (a) >> (b) >> (c) >> (d) >> (e) >> (f) #define in7(a, b, c, d, e, f, g) \ cin >> (a) >> (b) >> (c) >> (d) >> (e) >> (f) >> (g) #define e1 first #define e2 second #define popcount(i) __builtin_popcountl((i)) #define Forchar(c, a, z) for (char(c) = (a); (c) <= (z); (c)++) #define cntchar(s, c) count(all((s)), c) #define substring(s, start, end) s.substr((start), (end) - (start) + 1) #define prl_nd(num, digits) \ std::cout << fixed << setprecision(digits) << (num) << endl; #define prl_time(s) \ prl3("Elapsed Time:", 1000.0 * (clock() - s) / CLOCKS_PER_SEC, "[ms]"); #define char_to_str(c) string(1, (c)) #ifdef _LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif 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; } struct MaxFlow { struct F_edge { ll to, rev, capacity; F_edge(ll to, ll rev, ll capacity) : to(to), rev(rev), capacity(capacity) {} }; typedef vector<F_edge> F_edges; vector<F_edges> graph; ll n_vertex; // level is the shortest path to get a given node from the source node. vl level, iter; MaxFlow(ll n_vertex) : n_vertex(n_vertex) { graph.resize(n_vertex); } void add_edge(ll from, ll to, ll capacity) { graph[from].pb({to, ll(graph[to].size()), capacity}); graph[to].pb({from, ll(graph[from].size()) - 1, 0}); } void bfs(ll source) { level = vl(n_vertex, -1); level[source] = 0; queue<ll> q; q.push(source); while (!q.empty()) { ll vertex = q.front(); q.pop(); rep(i, graph[vertex].size()) { ll target = graph[vertex][i].to; ll cap_target = graph[vertex][i].capacity; // if the flow can be into the target node, implement below. if (cap_target > 0 && level[target] < 0) { level[target] = level[vertex] + 1; q.push(target); } } } } ll dfs(ll vertex, ll sink, ll flow) { if (vertex == sink) return flow; for (ll &i = iter[vertex]; i < graph[vertex].size(); i++) { ll target = graph[vertex][i].to; ll cap_target = graph[vertex][i].capacity; ll rev_target = graph[vertex][i].rev; // if capasitiy is not full yet and target is farther, // then assign current flow. if (cap_target > 0 && level[vertex] < level[target]) { ll d = dfs(target, sink, min(cap_target, flow)); if (d > 0) { // if the flow successfully reaches the sink, reduce the // flow from the capacity graph[vertex][i].capacity -= d; graph[target][rev_target].capacity += d; return d; } } } return 0; } ll dinic(ll source, ll sink) { // complexity O(EV^2) ll flow = 0; while (true) { bfs(source); // if there is no path leading to the sink, the maximum flow is 0. if (level[sink] < 0) return flow; iter = vl(n_vertex, 0); ll f; while ((f = dfs(source, sink, INF)) > 0) flow += f; } } }; struct UnionFind { vl parents, set_size; set<ll> root_idx; ll n_groups; UnionFind(ll n) { parents = set_size = vl(n); n_groups = n; rep(i, n) { parents[i] = i; set_size[i] = 1LL; root_idx.insert(i); } } ll root_find(ll x) { if (parents[x] == x) return x; return parents[x] = root_find(parents[x]); } void unite(ll x, ll y) { // priority for x is larger than that of y x = root_find(x); y = root_find(y); if (x == y) return; parents[y] = x, set_size[x] += set_size[y]; root_idx.erase(y); n_groups--; } // connected or not bool is_same(ll x, ll y) { return root_find(x) == root_find(y); } ll size(ll x) { return set_size[root_find(x)]; } ll num_union() const { return n_groups; } }; struct WeightedUnionFind { vl parents, set_size, weights; set<ll> root_idx; ll n_groups; WeightedUnionFind(ll n) { parents = set_size = weights = vl(n); n_groups = n; rep(i, n) { parents[i] = i; set_size[i] = 1LL; weights[i] = 0; root_idx.insert(i); } } ll root_find(ll x) { if (parents[x] == x) return x; ll r = root_find(parents[x]); weights[x] += weights[parents[x]]; return parents[x] = r; } ll weight(ll x) { root_find(x); return weights[x]; } void unite(ll x, ll y, ll w) { // priority for x is larger than that of y w += weight(x) - weight(y); x = root_find(x); y = root_find(y); if (x == y) return; parents[y] = x, set_size[x] += set_size[y]; root_idx.erase(y); weights[y] = w; n_groups--; } // connected or not bool is_same(ll x, ll y) { return root_find(x) == root_find(y); } ll size(ll x) { return set_size[root_find(x)]; } ll num_union() const { return n_groups; } ll difference(ll x, ll y) { return weight(y) - weight(x); } }; struct Doubling { // ABC167D ll n; ll sz; vl2 next; /* next[k + 1][i] := next[k][next[k][i]] next[0][i] := edge[i] e.g. a0, a1, ..., an-1 / 0 <= ai <= n - 1 a0 -> a[a0] -> a[a[a0]] -> ... -> a[a[...[a[0]]]] (m times) Let the function repeatedly inputting a[i] m times be f[m](a[i]) - get(i, x) returns f[x](a[i]) - lower_bound(i, j) returns minimum x which satisfies f[x](a[i]) >= j. if not possible returns n. */ // edge[i]: the step size for one iteration Doubling(vl &edge) : n(edge.size()), sz(62) { next.resize(sz, vl(n, -1)); rep(i, n) next[0][i] = edge[i]; rep(k, sz - 1) rep(i, n) next[k + 1][i] = next[k][next[k][i]]; } ll get(ll i, ll x) { ll ret = i; rep(bit, sz) { if (!(x >> bit & 1)) continue; ret = next[bit][ret]; } return ret; } ll lower_bound(ll i, ll j) { ll cur = i, acc = 0; revrep(wid, sz) { if (next[wid][cur] < j) { acc += 1LL << wid; cur = next[wid][cur]; } } return min(n, acc + 1); } }; template <class T> class LowestCommonAncestor { public: ll N, logN; vl depth, len; T tree; vl2 parents; LowestCommonAncestor(ll n, T &_tree) { N = n; logN = 0; while (N > (1LL << logN)) logN++; depth = len = vl(N); parents = vl2(logN, vl(N)); tree = _tree; init(0, -1, 0, 0); build(); } void init(ll source, ll parent, ll d, ll l) { depth[source] = d; parents[0][source] = parent; len[source] = l; rep(i, tree[source].size()) { ll target = tree[source][i].to; ll cost = tree[source][i].cost; if (target == parent) continue; init(target, source, d + 1, cost + l); } } void build() { rep(k, logN - 1) rep(n, N) { // if there is no parent, -1. // otherwise, the parent of the parent is the parent. if (parents[k][n] < 0) parents[k + 1][n] = -1; else parents[k + 1][n] = parents[k][parents[k][n]]; } } ll query(ll u, ll v) { if (depth[u] > depth[v]) swap(u, v); rep(k, logN) if ((depth[v] - depth[u]) >> k & 1) v = parents[k][v]; if (u == v) return u; revrep(k, logN) { if (parents[k][u] != parents[k][v]) { u = parents[k][u]; v = parents[k][v]; } } return parents[0][u]; } ll distance(ll u, ll v) { ll w = query(u, v); return len[u] + len[v] - 2 * len[w]; } }; struct BinaryIndexedTree { ll n, ini; vl dat; BinaryIndexedTree(ll n, ll ini = 0) : dat(n + 1, ini), n(n), ini(ini){}; // x: 1001 1010 1100 1011 1101 1111 // x & - x: 0001 0010 0100 0001 0001 0001 // ->: 1010 1100 10000 1100 1100 10000 ll update_func(ll val, ll d) { // if maximum -> max(val, dat) // return max(val, d); // if cumulative sum return val + d; } ll query(ll i) { /* v[0] + v[1] + ... + v[i] e.g.) i = 10101 itr1. 10101 -> 10100 itr2. 10100 -> 10000 itr3. 10000 -> 00000 (break) */ if (i < 0) return ini; ll ret = 0; for (ll j = i; j >= 0; j = (j & (j + 1)) - 1) { ret = update_func(ret, dat[j]); } return ret; } ll query(ll l, ll r) { // a[l] + a[l + 1] + ... + a[r - 1] + a[r] return query(r) - query(l - 1); } ll lower_bound(ll key) { // v[0] + v[1] + ... + v[left - 1] < key <= v[0] + v[1] + ... + v[left] if (key <= 0) return 0; ll left = 0, right = 1; while (right <= n) right *= 2; for (ll i = right; i > 0; i /= 2) { if (left + i <= n && dat[left + i - 1] < key) { key -= dat[left + i - 1]; left += i; } } return left; } void update(ll i, ll val) { /* e.g.) i = 10101, n = 11111 itr1. i: 10101, i+1: 10110 -> 10111 itr2. i: 10111, i+1: 11000 -> 11111 (break) */ if (i < 0) return; for (ll j = i; j < n; j |= j + 1) { dat[j] = update_func(val, dat[j]); } } }; struct SegmentTree { ll n, ini, minimize; vl dat; // when seeking minimum // ini = INF // when seeking maximum // ini = -INF SegmentTree(ll n_, bool minimize_ = true) { n = 1; minimize = minimize_; if (minimize) ini = INF; else ini = -INF; while (n < n_) n *= 2; dat.resize(2 * n - 1); rep(i, 2 * n - 1) dat[i] = ini; }; void update(ll idx, ll val) { idx += n - 1; if (minimize && dat[idx] <= val) return; if (!minimize && dat[idx] >= val) return; dat[idx] = val; while (idx > 0) { idx = (idx - 1) / 2; // when seeking minimum if (minimize) dat[idx] = min(dat[idx * 2 + 1], dat[idx * 2 + 2]); // when seeking maximum else dat[idx] = max(dat[idx * 2 + 1], dat[idx * 2 + 2]); } } ll query(ll l, ll r) { // ### NOTE ### // the range is [l, r] // l, l + 1, ..., r r++; // to adjust to this method return query_segment(l, r, 0, 0, n); } ll query_segment(ll a, ll b, ll idx, ll l, ll r) { assert(a < b); if (r <= a || b <= l) return ini; if (a <= l && r <= b) return dat[idx]; else { ll seg1 = query_segment(a, b, idx * 2 + 1, l, (l + r) / 2); ll seg2 = query_segment(a, b, idx * 2 + 2, (l + r) / 2, r); // when seeking minimum if (minimize) return min(seg1, seg2); // when seeking maximum else return max(seg1, seg2); } } }; template <class Target> class RerootingTreeDP { public: using T = typename Target::type; struct DP_edge { ll to, rev; // rev is the index to trace the source node. T value; // objective value }; private: ll n; void dfs_fwd(ll source, ll parent) { ll par_idx = -1; vector<T> values; rep(i, tree[source].size()) { const DP_edge &e = tree[source][i]; if (e.to == parent) { par_idx = i; continue; } dfs_fwd(e.to, source); values.pb(e.value); } // If the parent != -1, update the value on edge from parent to source if (par_idx != -1) { ll src_idx = tree[source][par_idx].rev; // update values on the edge from parent to source tree[parent][src_idx].value = Target::merge(values); } } void dfs_bwd(ll source, ll parent) { vector<T> values; for (auto &&e : tree[source]) values.pb(e.value); values = Target::evaluate(values); rep(i, tree[source].size()) { const DP_edge &e = tree[source][i]; if (e.to == parent) continue; // tree[e.to][e.rev]: e.to -> source tree[e.to][e.rev].value = values[i]; dfs_bwd(e.to, source); } } public: UnionFind uf; vector<vector<DP_edge>> tree; RerootingTreeDP(ll n) : n(n), uf(n), tree(n) {} void add_edge(ll u, ll v, T val) { assert(!uf.is_same(u, v)); tree[u].pb({v, ll(tree[v].size()), val}); tree[v].pb({u, ll(tree[u].size()) - 1, val}); uf.unite(u, v); } void dp() { vb visited(n, false); rep(i, n) { if (visited[uf.root_find(i)]) continue; dfs_fwd(i, -1); visited[uf.root_find(i)] = true; } visited.assign(n, false); rep(i, n) { if (visited[uf.root_find(i)]) continue; dfs_bwd(i, -1); visited[uf.root_find(i)] = true; } } ll size() const { return tree.size(); } }; // ABC160F is one example // Modify the functions evaluate and merge based on given problems struct Merger { using type = ll; // This is the exaple of the number of children static type merge(const vector<type> &value) { // merge the result below the source node // each value is from each child node of the source node // value[i] := f(child i) // Here, we would like to obtain f(source) using f(child i) (i = 0, 1, ..., // n_children) ll ret = 1; for (auto &&v : value) ret += v; return ret; } static vector<type> evaluate(const vector<type> &value) { // value[i] := f(source -> child i) // we would like to obtain f(child i -> source) // child j (j != i) is the grandchildren of child i // represent f(child i -> source) using f(source -> j) (j != i) // L[i + 1] := the result using f(source -> k) (k = 0, 1, ..., i) // R[i] := the result using f(source -> k) (k = i, i + 1, ..., n_children) const ll n_children = value.size(); vl L(n_children + 1, 0), R(n_children + 1, 0); rep(i, n_children) L[i + 1] = L[i] + value[i]; revrep(i, n_children) R[i] = R[i + 1] + value[i]; vl ret(n_children); rep(i, n_children) ret[i] = L[i] + R[i + 1] + 1; return ret; } }; struct StronglyConnectedComponents { ll n, n_cmp; // dag: edges from a cmp to another cmp vl2 graph, graph_rev, dag, cmp; vl order, visited, cmp_idx; StronglyConnectedComponents() {} StronglyConnectedComponents(ll sz) : n(sz), graph(sz), graph_rev(sz), visited(sz), cmp_idx(sz) {} void add_edge(ll from, ll to) { graph[from].pb(to); graph_rev[to].pb(from); } void input(ll m, ll offset = -1) { ll a, b; rep(i, m) { in2(a, b); add_edge(a + offset, b + offset); } } ll operator[](ll k) { return cmp_idx[k]; } void dfs_fwd(ll source) { visited[source] = 1; rep(i, graph[source].size()) { ll target = graph[source][i]; if (!visited[target]) dfs_fwd(target); } order.pb(source); } void dfs_bwd(ll source, ll num) { visited[source] = 1, cmp_idx[source] = num; cmp[num].pb(source); rep(i, graph_rev[source].size()) { ll target = graph_rev[source][i]; if (!visited[target]) dfs_bwd(target, num); } } ll build() { fill(all(visited), 0); order.clear(); rep(i, n) if (!visited[i]) dfs_fwd(i); fill(all(visited), 0); ll num = 0; revrep(i, order.size()) { if (!visited[order[i]]) { dag.pb(vl()); cmp.pb(vl()); dfs_bwd(order[i], num++); } } rep(i, n) for (ll to : graph[i]) if (cmp_idx[i] != cmp_idx[to]) dag[cmp_idx[i]] .pb(cmp_idx[to]); rep(i, num) { sort(all(dag[i])); dag[i].erase(unique(all(dag[i])), dag[i].end()); } return n_cmp = num; } // if the v-th vertex is in loop or not bool in_loop(ll v) { return cmp[cmp_idx[v]].size() > 1; } // if the dag has loops or not bool has_loop() { rep(i, cmp.size()) if (cmp[i].size() > 1) return true; return false; } }; struct CombinationMemo { ll sz, mod; vl facts, facts_inv, minv; CombinationMemo(ll sz, ll _mod) : sz(sz), mod(_mod) { facts.resize(sz + 5); facts_inv.resize(sz + 5); minv.resize(sz + 5); init(); } void init() { facts[0] = facts[1] = 1; minv[1] = 1; facts_inv[0] = facts_inv[1] = 1; For(i, 2, sz + 3) { facts[i] = (i * facts[i - 1]) % mod; minv[i] = mod - minv[mod % i] * (mod / i) % mod; facts_inv[i] = facts_inv[i - 1] * minv[i] % mod; } } ll nCk(ll n, ll r) { if (n == r && n == 0) return 1; else if (n <= 0 || r < 0 || r > n) return 0; ll val = (facts[n] * facts_inv[n - r]) % mod; val *= facts_inv[r]; return val % mod; } ll nPk(ll n, ll r) { if (n == r && n == 0) return 1; else if (n <= 0 || r < 0 || r > n) return 0; ll val = (facts[n] * facts_inv[n - r]) % mod; return val % mod; } }; struct PowerMemo { ll sz, mod, base; vl powB; PowerMemo(ll sz, ll base, ll _mod) : sz(sz), base(base), mod(_mod) { powB.resize(sz + 5); init(); } void init() { powB[0] = 1; rep(i, sz + 3) powB[i + 1] = (powB[i] * base) % mod; } ll operator[](ll k) { return powB[k]; } }; struct Grid2D { Graph graph; ll Width, Height; Grid2D(ll w, ll h) : Width(w), Height(h) { graph.resize(w * h); } ll pos_to_idx(point p) { return p.y * Width + p.x; } point idx_to_pos(ll idx) { return {idx % Width, idx / Width}; } bool undefined_region(point p, vb2 &block) { if (p.x < 0 || p.x > Width - 1) return true; if (p.y < 0 || p.y > Height - 1) return true; if (block[p.x][p.y]) return true; return false; } void build(vb2 &block, ll val = 1) { rep(x, Width) rep(y, Height) { point p = {x, y}; ll idx1 = pos_to_idx(p); ll idx2; if (block[x][y]) continue; rep(i, 4) { point nxt = p + dirs[i]; idx2 = pos_to_idx(nxt); if (!undefined_region(nxt, block)) graph[idx1].pb( {idx2, val}); // dist[idx1][idx2] = val; (warshall-floyd) } } } }; struct Cumulative2D { vl2 cum; ll w, h; Cumulative2D(ll w, ll h) : w(w), h(h) { cum = vl2(w + 1, vl(h + 1, 0)); } template <typename T> void build(vector<T> &vec) { // never forget building rep(x, w + 1) cum[x][0] = 0; rep(y, h + 1) cum[0][y] = 0; rep(y, h) rep(x, w) cum[x + 1][y + 1] = cum[x][y + 1] + vec[x][y]; rep(x, w + 1) rep(y, h) cum[x][y + 1] += cum[x][y]; } ll func(ll x, ll y, ll dx, ll dy) { // 1-indexed // the rectangle of (x, y), (x + dx, y), (x, y + dy) and (x + dx, y + dy) // think about the case of (1, 1, 1, 1). if (x + dx > w || y + dy > h) return -INF; ll val = cum[x + dx][y + dy]; val += cum[x][y]; val -= cum[x][y + dy]; val -= cum[x + dx][y]; return val; } }; struct LinearSystemIncidence { struct Edge { ll idx, to; bool fwd; }; /* Ax = c A: A in R^(V * E) <- given x: x in R^E <- estimate this vector c: c in R^V <- given */ ll n_vertex, n_edge; bool mod2; vl x; vb visited; vector<vector<Edge>> graph; LinearSystemIncidence(ll n, vpl &es, bool mod2 = false) : n_vertex(n), n_edge(es.size()), mod2(mod2) { graph.resize(n); visited.resize(n); rep(i, n_edge) { auto &e = es[i]; graph[e.e1].pb({i, e.e2, true}); graph[e.e2].pb({i, e.e1, false}); } } ll dfs(vl &c, ll src) { visited[src] = true; ll ret = c[src]; for (Edge &e : graph[src]) { if (visited[e.to]) continue; ll tmp = dfs(c, e.to); x[e.idx] = e.fwd ? tmp : -tmp; ret += tmp; if (mod2) x[e.idx] = ((x[e.idx] % 2) + 2) % 2; } return mod2 ? ret % 2 : ret; } bool solve(vl &c) { x.assign(n_edge, 0); visited.assign(n_vertex, false); rep(src, n_vertex) { if (visited[src]) continue; if (dfs(c, src)) return false; } return true; } }; struct BigInt10 { string num; ll sz; map<ll, vl> mod; BigInt10(string num) : num(num), sz(num.size()) {} void compute_mod(ll _mod) { mod[_mod] = vl(sz + 1, 0); ll tenth = 1; rep(i, sz) { ll top = num[sz - 1 - i] - '0'; mod[_mod][i + 1] = (mod[_mod][i] + top * tenth) % _mod; (tenth *= 10) %= _mod; } } ll n_digits() { return num.length(); } ll operator[](ll k) { return num[k] - '0'; } ll subseq_mod(ll l, ll r, ll m) { // s1 s2 ... sl .... sr ... sn // the modulo of 00 ... 0 sl ... sr 00 ... 0 assert(l <= r); ll tot = mod[m][sz]; ll ls = (tot - mod[m][sz - l] + m) % m; ll rs = mod[m][sz - r]; return (tot - ls - rs + 2 * m) % m; } bool operator<(BigInt10 &n2) const { if (num.length() < n2.n_digits()) return true; else if (num.length() > n2.n_digits()) return false; return num < n2.num; } bool operator>(BigInt10 &n2) const { if (num.length() > n2.n_digits()) return true; else if (num.length() < n2.n_digits()) return false; return num > n2.num; } }; struct BigInt2 { string num; ll sz; map<ll, vl> mod; ll nz; BigInt2(string num) : num(num), sz(num.size()) { nz = 0; rep(i, sz) if (num[i] == '1') nz++; } void compute_mod(ll _mod) { mod[_mod] = vl(sz + 1, 0); ll base = 1; rep(i, sz) { ll top = num[sz - 1 - i] - '0'; mod[_mod][i + 1] = (mod[_mod][i] + top * base) % _mod; (base *= 2) %= _mod; } } ll n_digits() { return num.length(); } ll bit_count() { return nz; } ll operator[](ll k) { return num[k] - '0'; } ll subseq_mod(ll l, ll r, ll m) { // s1 s2 ... sl .... sr ... sn // the modulo of 00 ... 0 sl ... sr 00 ... 0 assert(l <= r); ll tot = mod[m][sz]; ll ls = (tot - mod[m][sz - l] + m) % m; ll rs = mod[m][sz - r]; return (tot - ls - rs + 2 * m) % m; } bool operator<(BigInt10 &n2) const { if (num.length() < n2.n_digits()) return true; else if (num.length() > n2.n_digits()) return false; return num < n2.num; } bool operator>(BigInt10 &n2) const { if (num.length() > n2.n_digits()) return true; else if (num.length() < n2.n_digits()) return false; return num > n2.num; } }; ll gcd(ll m, ll n) { ll a = max(m, n); ll b = min(m, n); while (b != 1 && b != 0) { a %= b; swap(a, b); } return b == 1 ? 1 : a; } ll lcm(ll m, ll n) { return m / gcd(m, n) * n; } ll power_normal(ll a, ll power) { ll value = 1; while (power != 0) { if (power & 1) value = value * a; a = a * a; power = power >> 1; } return value; } ll power_mod(ll a, ll power, ll mod) { ll value = 1; while (power != 0) { if (power & 1) value = (value * a) % mod; a = (a * a) % mod; power = power >> 1; } return value % mod; } ll modinv(ll a, ll mod) { return power_mod(a, mod - 2, mod); } ll combination(ll n, ll r, ll mod) { if (n == r && n == 0) return 1; else if (n <= 0 || r < 0 || r > n) return 0; ll numerator = 1; ll denomenator = 1; for (ll i = 0; i < r; i++) { ll num = (n - i) % mod, den = (i + 1) % mod; (numerator *= num) %= mod; (denomenator *= modinv(den, mod)) %= mod; } return (numerator * denomenator) % mod; } vl2 pascal_triangle(ll n) { /* Complexity: O(n^2) The upper bound of n is nearly 50. Parameters ---------- n; the size of returned vector Returns ------- comb[i][j]: combination(i, j). 0 <= i <= n, 0 <= j <= i */ vl2 comb(n + 1, vl(n + 1)); comb[0][0] = 1; For(i, 1, n + 1) rep(j, i + 1) { comb[i][j] += comb[i - 1][j]; if (j > 0) comb[i][j] += comb[i - 1][j - 1]; } return comb; } ld log_combination(ll n, ll r) { if (n == r && n == 0) return 0; else if (n <= 0 || r < 0 || r > n) return -INF; ld val = 0; for (ll i = 0; i < r; i++) { val += log(n - i); val -= log(i + 1); } return val; } string bin_expression(ll n, ll dig) { string s = ""; rep(i, dig) { s += to_string(n % 2); n /= 2; } reverse(all(s)); return s; } bool is_prime(ll n) { if (n <= 1) return false; for (ll i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } map<ll, ll> prime_factorization(ll n) { ll i = 2; map<ll, ll> table; while (i * i <= n) { while (n % i == 0) { table[i]++; n /= i; } i++; } if (n > 1) table[n] = 1; return table; } vl divisor_table(ll n) { vl table; ll i = 1; while (i * i <= n) { if (n % i == 0) { table.pb(i); if (i * i != n) table.pb(n / i); } i++; } sort(all(table)); return table; } vl base_converter(ll num, ll base, ll sz) { vl coefs(sz); ll cur = 0; while (num) { ll val = num % base; assert(cur < sz); coefs[cur++] = val; num -= val, num /= base; } while (cur < sz) coefs[cur++] = 0; return coefs; } ll next_combination(ll sub) { /* nCk ll bit = (1 << k) - 1; for (; bit < (1 << n); bit = next_combination(bit)){ bool ith = bit & (1 << i); procedures... } sub & -sub: the binary which shares the last digit whose value is 1 in sub sub + x : carry up the last digit ~y : the binary whose digits are 1 if y's digit is 0. (sub & ~y) / x: reduce the same number of 0s after first 1 in x from (sub & ~y). */ ll x = sub & -sub, y = sub + x; if (x != 0) return (((sub & ~y) / x) >> 1) | y; else return INF; } // just change the input if you want to change the target. // If you want to check the common sequences in two strings, // combine them. e.g. ABC150F template <typename T> vl z_algorithm(T &s, ll n) { /* Paramters --------- T: the string or list of interest n: the size of string or list Returns ------- res[i] is the maximum number of K which satisfies s[:K] == s[i:i + K] for each i = 0, 1, 2, ..., n - 1. */ vl res(n); res[0] = n; ll i1 = 1, i2 = 0; while (i1 < n) { /* i1: the starting point i2: the length of substring */ while (i1 + i2 < n && s[i2] == s[i1 + i2]) ++i2; res[i1] = i2; if (i2 == 0) { ++i1; continue; } ll i3 = 1; // update the already seen points while (i1 + i3 < n && i3 + res[i3] < i2) { res[i1 + i3] = res[i3]; ++i3; } // update up to i1 + i3 and the next possible minimum length is i2 - i3 (= // res[i3]) i1 += i3, i2 -= i3; } return res; } ll string_to_ll(string s) { ll l = s.length(); ll idx = 0; ll val = 0; ll tenth = 1; while (idx < l) { ll m = s[l - 1 - idx] - '0'; val += (m * tenth); tenth *= 10; idx++; } return val; } string reflected_string(string s) { string t, u; ll n = s.length(); t = s; reverse(all(t)); u = substring(t, 0, n - 2) + s + substring(t, 1, n - 1); return u; } ld distance_between_point_line(point l_begin, point l_end, point p) { ll xl1 = l_begin.x, yl1 = l_begin.y; ll xl2 = l_end.x, yl2 = l_end.y; ll xp = p.x, yp = p.y; ll a = yl2 - yl1; ll b = -xl2 + xl1; ll c = -a * xl2 - b * yl2; return abs(ld(a * xp + b * yp + c)) / ld(sqrt(a * a + b * b)); } bool is_cross(point l1_begin, point l1_end, point l2_begin, point l2_end) { ll x1 = l1_begin.x, y1 = l1_begin.y; ll x2 = l1_end.x, y2 = l1_end.y; ll x3 = l2_begin.x, y3 = l2_begin.y; ll x4 = l2_end.x, y4 = l2_end.y; ll val1 = (x1 - x2) * (y3 - y1) + (y1 - y2) * (x1 - x3); ll val2 = (x1 - x2) * (y4 - y1) + (y1 - y2) * (x1 - x4); ll val3 = (x3 - x4) * (y1 - y3) + (y3 - y4) * (x3 - x1); ll val4 = (x3 - x4) * (y2 - y3) + (y3 - y4) * (x3 - x2); return val1 * val2 < 0 && val3 * val4 < 0; } template <typename T> bool isColinear(T p1, T p2, T p3) { T v1 = p2 - p1, v2 = p3 - p1; return v1.x * v2.y == v1.y * v2.x; } template <typename T> T PerpendicularBisector(T p1, T p2) { T vec = p2 - p1; assert(!vec.is_zero()); T ret = {vec.y, -vec.x}; return ret; } template <typename T> ld Distance2DPoints(T p1, T p2) { T vec = (p1 - p2) * (p1 - p2); return sqrt(vec.x + vec.y); } ll SquaredDistance2DPoints(point p1, point p2) { point vec = (p1 - p2) * (p1 - p2); return vec.x + vec.y; } ld space_of_triangle(point p1, point p2, point p3) { ll x1 = p1.x, y1 = p1.y; ll x2 = p2.x, y2 = p2.y; ll x3 = p3.x, y3 = p3.y; ll v1 = x2 - x1; ll u1 = y2 - y1; ll v2 = x3 - x1; ll u2 = y3 - y1; ld s = ld(v1 * u2 - u1 * v2) / ld(2); return abs(s); } pair<point, ll> OuterCenter(point p1, point p2, point p3) { // the center of circle on the given three points // return the determinant value and the product of center points and 2 * // determinant value point ret; if (isColinear(p1, p2, p3)) { ll d1 = SquaredDistance2DPoints(p1, p2); ll d2 = SquaredDistance2DPoints(p2, p3); ll d3 = SquaredDistance2DPoints(p3, p1); if (d1 >= d2 && d1 >= d3) { ret = p1 + p2; return mp(ret, 2); } else if (d2 >= d1 && d2 >= d3) { ret = p2 + p3; return mp(ret, 2); } else { ret = p3 + p1; return mp(ret, 2); } } point pv1 = PerpendicularBisector(p1, p2); point pv2 = PerpendicularBisector(p1, p3); point cv1_2x = p1 + p2, cv2_2x = p1 + p3; // cv1 + k pv1 == cv2 + m pv2 // (pv1x -pv2x) (k) = (cv2x - cv1x) // (pv1y -pv2y) (m) = (cv2y - cv1y) ll det_inv = -pv1.x * pv2.y + pv1.y * pv2.x; ll x1_2x = cv2_2x.x - cv1_2x.x, x2_2x = cv2_2x.y - cv1_2x.y; pl c_2x_det = {-pv2.y * x1_2x + pv2.x * x2_2x, -pv1.y * x1_2x + pv1.x * x2_2x}; // ret.x = ld(cv1_2x.x * det_inv + pv1.x * c_2x_det.e1) / ld(2 * det_inv); // ret.y = ld(cv1_2x.y * det_inv + pv1.y * c_2x_det.e1) / ld(2 * det_inv); ret.x = cv1_2x.x * det_inv + pv1.x * c_2x_det.e1; ret.y = cv1_2x.y * det_inv + pv1.y * c_2x_det.e1; ll jacobian = 2 * det_inv; return mp(ret, jacobian); } ll inversion_number(vl a, ll a_max) { /* Paramters --------- a: vector<ll> All the elements must be non-negative. Prefably the elements are compressed to reduce the computational cost. a_max: ll The maximum value of the vector a or the value bigger than the value stated previously. */ BinaryIndexedTree bit(a_max + 1); ll val = 0; rep(i, a.size()) { // i is the number of elements that have lower index than a[i]. // call the number of elements that have lower value than a[i] // by subtracting these two, the residual number is the number of elements // that have larger value. val += i - bit.query(a[i] - 1); // cumulative sum from 0 to a[i] - 1 bit.update(a[i], 1); } return val; } ld bin_search(ld left, ld right, bool lb, function<bool(ld)> judge) { ld mid; while (right - left > EPS) { mid = (right + left) / 2; if (lb) { if (judge(mid)) right = mid; else left = mid + EPS; } else { if (judge(mid)) left = mid; else right = mid - EPS; } } return right; } ll bin_search(ll left, ll right, bool lb, function<bool(ll)> judge) { ll mid; while (right > left) { if (lb) { // if true (satisfies the condition), range shifts smaller direction mid = (right + left) / 2; if (judge(mid)) right = mid; else left = mid + 1; } else { // if true (satisfies the condition), range shitfs larger direction mid = (right + left + 1) / 2; if (judge(mid)) left = mid; else right = mid - 1; } } return right; } ld trinary_search(ld left, ld right, function<ld(ld)> func) { // Care the value EPS!!! Compare to the condition while (abs(right - left) > EPS) { ld left2 = (2.0 * left + right) / 3.0; ld right2 = (left + 2.0 * right) / 3.0; ld f1 = func(left2); ld f2 = func(right2); if (f1 <= f2) right = right2; else if (f2 <= f1) left = left2; } return right; } template <typename T> vector<T> compress(vector<T> v) { // sort and remove all the duplicated values sort(all(v)); v.erase(unique(all(v)), v.end()); return v; } template <typename T> map<T, ll> dict(const vector<T> &v) { map<T, ll> d; rep(i, v.size()) d[v[i]] = i; return d; } points compress2D(vl xs, vl ys) { /* NOTE ---- Add the corner points if required */ ll n = xs.size(); vl xcs = compress(xs), ycs = compress(ys); map<ll, ll> xd = dict(xcs), yd = dict(ycs); points ps(n); rep(i, n) xs[i] = xd[xs[i]], ys[i] = yd[ys[i]]; rep(i, n) ps[i] = {xs[i], ys[i]}; sort(all(ps)); return ps; } void GaussJordanBitVector(vl &bs) { ll n = bs.size(); ll rank = 0; ll j = 0; revrep(i, N_DIGITS) { for (j = rank; j < n; j++) if (bs[j] & (1LL << i)) break; if (j == n) continue; if (j > rank) bs[rank] ^= bs[j]; for (j = 0; j < n; j++) if (j != rank) bs[j] = min(bs[j], bs[j] ^ bs[rank]); rank++; } } ll kruskal(vector<undirected_edge> &es, ll n_vertex) { // O(ElogE) sort(all(es)); UnionFind uf(n_vertex); ll min_cost = 0; rep(i, es.size()) { undirected_edge &e = es[i]; if (!uf.is_same(e.from, e.to)) { min_cost += e.cost; uf.unite(e.from, e.to); } } return min_cost; } ll LongestIncreasedSequence(vl &v) { ll n = v.size(); vl dp(n, INF); rep(i, n) * lower_bound(all(dp), v[i]) = v[i]; return lower_bound(all(dp), INF) - dp.begin(); } void dijkstra(ll start, Graph &graph, vl &dist, vl &vertex_pre, bool trace = false) { priority_queue<pl, vpl, greater<pl>> q; ll n = graph.size(); dist = vl(n, INF); if (trace) vertex_pre = vl(n, -1); dist[start] = 0; q.push(pl(0, start)); while (!q.empty()) { ll idx, cost; tie(cost, idx) = q.top(); q.pop(); if (dist[idx] < cost) continue; for (auto &e : graph[idx]) { if (chmin(dist[e.to], dist[idx] + e.cost)) { if (trace) vertex_pre[e.to] = idx; q.push(pl(dist[e.to], e.to)); } } } } vl get_predecessor(ll g, vl &vertex_pre) { vl path; for (; g != -1; g = vertex_pre[g]) path.pb(g); reverse(all(path)); return path; } void warshall_floyd(vl2 &dist) { ll n = dist.size(); // Dont forget the initialization // rep(i, n) rep(j, n) dist[i][j] = INF * (i != j); rep(k, n) rep(i, n) rep(j, n) dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); } // ABC061D bool find_negative_cycle(ll n, ll goal, Graph &graph, vl &dist) { rep(i, n) rep(v, n) rep(k, graph[v].size()) { edge e = graph[v][k]; if (dist[e.to] != INF && dist[e.to] > dist[v] + e.cost) { dist[e.to] = -INF; if (goal == -1) return true; else if (goal == e.to) return true; } } return false; } bool bellman_ford(ll start, ll goal, Graph &graph, vl &dist) { // if there is a closed circuit, it returns false. (when goal == -1) // if the distance to goal cannot be obtained, it returns false (when goal != // -1) ll n = graph.size(); dist = vl(n, INF); dist[start] = 0; rep(i, n) rep(v, n) rep(k, graph[v].size()) { edge e = graph[v][k]; if (dist[v] != INF && dist[e.to] > dist[v] + e.cost) dist[e.to] = dist[v] + e.cost; } if (find_negative_cycle(n, goal, graph, dist)) return false; return true; } void Euler_Tour(Graph &tree, vl &euler_tour, vl &in, vl &out, ll anc = 0) { // record only when we first reach a node and leave the node. ll n = tree.size(); vb visited(n, false); in = out = vl(n); auto dfs = [&](auto dfs, ll source) -> void { visited[source] = true; in[source] = euler_tour.size(); euler_tour.pb(source); bool is_leaf = true; for (auto &e : tree[source]) { ll target = e.to; if (visited[target]) continue; else is_leaf = false; dfs(dfs, target); } euler_tour.pb(-source); out[source] = euler_tour.size() - 1; }; dfs(dfs, anc); } void Euler_Tour2(Graph &tree, vl &euler_tour, vl &in, vl &out, ll anc = 0) { // record everytime we reach a node ll n = tree.size(); vb visited(n, false); in = out = vl(n); auto dfs = [&](auto dfs, ll source) -> void { visited[source] = true; in[source] = euler_tour.size(); euler_tour.pb(source); bool is_leaf = true; for (auto &e : tree[source]) { ll target = e.to; if (visited[target]) continue; else is_leaf = false; dfs(dfs, target); euler_tour.pb(source); } out[source] = euler_tour.size() - 1; }; dfs(dfs, anc); } std::mt19937 create_rand_engine() { std::random_device rnd; std::vector<std::uint_least32_t> v(10); std::generate(v.begin(), v.end(), std::ref(rnd)); std::seed_seq seed(v.begin(), v.end()); return std::mt19937(seed); } vl generate_unique_array(const size_t sz, ll vm, ll vM) { const size_t range = static_cast<size_t>(vM - vm + 1); const size_t num = static_cast<size_t>(sz * 1.2); assert(vm <= vM); assert(sz <= range); vl ret; auto engine = create_rand_engine(); std::uniform_int_distribution<ll> distribution(vm, vM); while (ret.size() < sz) { while (ret.size() < num) ret.pb(distribution(engine)); sort(all(ret)); auto unique_end = unique(all(ret)); if (sz < distance(ret.begin(), unique_end)) unique_end = next(ret.begin(), sz); ret.erase(unique_end, ret.end()); } return ret; } vl generate_array(const size_t sz, ll vm, ll vM) { const size_t range = static_cast<size_t>(vM - vm + 1); assert(vm <= vM); vl ret; auto engine = create_rand_engine(); std::uniform_int_distribution<ll> distribution(vm, vM); while (ret.size() < sz) ret.pb(distribution(engine)); return ret; } void read_vector(ll n, vl &v, ll offset = 0) { v.resize(n); rep(i, n) { in1(v[i]); v[i] += offset; } } void read_points(ll n, points &ps, ll offset = 0) { ps.resize(n); rep(i, n) { ll x, y; in2(x, y); ps[i] = {x, y}; ps[i] += offset; } } void read_2DMap(ll w, ll h, vb2 &block, char b) { block = vb2(w, vb(h, false)); string s; rep(y, h) { in1(s); rep(x, w) block[x][y] = (s[x] == b); } } /* diameter of tree Graph tree; ll dM = 0, vM = 0, v2 = 0; void dfs1(ll source, ll parent, ll d){ if (d > dM) dM = d, vM = source; rep(i, tree[source].size()){ ll target = tree[source][i].to; if (target == parent) continue; dfs1(target, source, d + 1); } } void dfs2(ll source, ll parent, ll d){ if (dM <= d) dM = d, v2 = source; rep(i, tree[source].size()){ ll target = tree[source][i].to; if (target == parent) continue; dfs2(target, source, d + 1); } } dfs(0, -1, 0); dfs2(vM, -1, 0); prl2(vM + 1, v2 + 1); // the two edges of tree */ /* #5. shakutori method (syakutori, two pointers technique) // 1. strech right side while the condition is met. // 2. renew the answer // 3. increments left side // 4. Back to 1. (l <= r must be satisfied all the time.) ll l = 0; ll r = 0; while (l < n){ if (l == r) r++; while(r < n && cond) r++; l++; } prl(answer); #11. bfs ABC146D, ABC007C 1. first create a graph. 2. start searching from a node. 3. do some processes and push nodes connected with a given target node in BFS. 4. repeat a series of procedure until queue is empty. ll n; in1(n); queue<tp3> q; vb visited(n, false); auto bfs = [&](ll src, ll par){ visited[src] = true; for (auto& e: graph[src]){ if (e.to != par && !visited[e.to]){ q.push(mt(e.to, src, ...)); } } }; q.push(mt(0, -1, 0)); while(!q.empty()){ ll src, par, ...; tie(src, par, ...) = q.front(); q.pop(); bfs(src, par, ...); } */ /* x: 1001 1010 1100 1011 1101 1111 x & ~ x: 0001 0010 0100 0001 0001 0001 sum: 1010 1100 10000 1100 1100 10000 ####### Attention ####### S | (1 << i) -> S union {i} S & ~(1 << i) -> S - {i} # inclusion-exclusion principle |U[i = 1 to n] Ai| = sum[i = 1 to n] |Ai| - sum[i < j]|Ai ^ Aj| + ... + (-1)^(n - 1) |^[i = 1 to n]Ai| */ const ll MAX_N = 200005; const size_t MAX_BIT = 160 * 160 + 10; typedef bitset<MAX_BIT> bts; typedef vector<bts> vbt; typedef vector<vbt> vbt2; typedef vector<vbt2> vbt3; void solve() { // generate_array(size, min, max); ll n; string ans = ""; in1(n); ll p = 26, leng = 1; while (n > p) { n -= p; p *= 26; leng++; } vl cs = base_converter(n - 1, 26, leng); reverse(all(cs)); rep(i, cs.size()) ans += char('a' + cs[i]); prl(ans); // ### DEBUG PART ### auto naive_solver = [&]() { }; #ifdef _LOCAL naive_solver(); #endif } void test(ll num = 0, bool verbose = false) { rep(i, max(1LL, num)) { ll t = clock(); if (verbose) prl4("\n#####", i + 1, "#####", "\n## Answer ##"); solve(); if (verbose) { prl(""); prl_time(t); } } } int main(void) { #ifdef _LOCAL test(6, true); #else test(0, false); #endif return 0; }
replace
1,500
1,501
1,500
1,501
-6
6541dc06-9b26-4209-a8db-31f23b18cd11.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02629/C++/s136965132.cpp:1343: vl base_converter(ll, ll, ll): Assertion `cur + 2 <= sz' failed.
p02629
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define pi acos(-1.0) // cout << fixed << setprecision(8) << a << endl; #define Fast_Input \ ios_base ::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); #define pb push_back #define mem(a, b) memset(a, b, sizeof(a)) #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; typedef long long ll; typedef tree<int, null_type, greater<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; /*sort(v.begin(), v.end(), [](const pair<int, int>& x, const pair<int, int>& y) { if (x.first != y.first) return x.first < y.first; return x.second < y.second; }); */ int const fx[] = {+1, +0, -1, +1}; int const fy[] = {-1, +1, +0, +0}; const int inf = numeric_limits<int>::max(); const int mx = 2e5; const ll mod = 1e9 + 7; ll pow(ll x, ll n) { ll res = 1; while (n) { if (n % 2) { res = res * x; n--; } x = x * x; n /= 2; } return res; } int main() { string s = "aabcdefghijklmnopqrstuvwxyz"; ll n, i, sum = 0; cin >> n; i = 0; while (sum < n) { i++; sum += pow(ll(26), i); } i--; vector<char> v(i); int j; for (j = i; j >= 0; j--) { int y = n % 26; if (y == 0) y = 26; v[j] = s[y]; n -= y; n /= 26; } for (j = 0; j <= i; j++) cout << v[j]; cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define pi acos(-1.0) // cout << fixed << setprecision(8) << a << endl; #define Fast_Input \ ios_base ::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); #define pb push_back #define mem(a, b) memset(a, b, sizeof(a)) #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; typedef long long ll; typedef tree<int, null_type, greater<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; /*sort(v.begin(), v.end(), [](const pair<int, int>& x, const pair<int, int>& y) { if (x.first != y.first) return x.first < y.first; return x.second < y.second; }); */ int const fx[] = {+1, +0, -1, +1}; int const fy[] = {-1, +1, +0, +0}; const int inf = numeric_limits<int>::max(); const int mx = 2e5; const ll mod = 1e9 + 7; ll pow(ll x, ll n) { ll res = 1; while (n) { if (n % 2) { res = res * x; n--; } x = x * x; n /= 2; } return res; } int main() { string s = "aabcdefghijklmnopqrstuvwxyz"; ll n, i, sum = 0; cin >> n; i = 0; while (sum < n) { i++; sum += pow(ll(26), i); } i--; vector<char> v(20); int j; for (j = i; j >= 0; j--) { int y = n % 26; if (y == 0) y = 26; v[j] = s[y]; n -= y; n /= 26; } for (j = 0; j <= i; j++) cout << v[j]; cout << endl; return 0; }
replace
50
51
50
51
-11
p02629
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n; cin >> n; string ans = ""; string abc = "abcdefghijklmnopqrstuvwxyz"; int rem; while (n >= 0) { n -= 1; rem = n % 26; ans += abc[rem]; n /= 26; } reverse(ans.begin(), ans.end()); cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n; cin >> n; string ans = ""; string abc = "abcdefghijklmnopqrstuvwxyz"; int rem; while (n > 0) { n -= 1; rem = n % 26; ans += abc[rem]; n /= 26; } reverse(ans.begin(), ans.end()); cout << ans; return 0; }
replace
13
14
13
14
TLE
p02629
C++
Runtime Error
#include "bits/stdc++.h" using namespace std; const long long MAX = 1000000000000001; long long ruijyo(int a, int n) { long long res = 1; while (n) { if (n & 1) res = res * a; a = a * a; n = n >> 1; } return res; } int main() { string alph = "abcdefghijklmnopqrstuvwxyz"; vector<long long> namesum; namesum.push_back(0); int i = 0; while (namesum.at(i) <= MAX) { namesum.push_back(namesum.at(i) * 26 + 26); i++; } long long N; cin >> N; for (int k = 0; k < namesum.size(); k++) { if (namesum.at(k) < N && N <= namesum.at(k + 1)) { N = N - namesum.at(k); for (int i = k; i >= 0; i--) { cout << alph.at((N - 1) / ruijyo(26, i)); N = N - ((N - 1) / ruijyo(26, i)) * ruijyo(26, i); } break; } } cout << endl; }
#include "bits/stdc++.h" using namespace std; const long long MAX = 1000000000000001; long long ruijyo(long long a, long long n) { long long res = 1; while (n) { if (n & 1) res = res * a; a = a * a; n = n >> 1; } return res; } int main() { string alph = "abcdefghijklmnopqrstuvwxyz"; vector<long long> namesum; namesum.push_back(0); int i = 0; while (namesum.at(i) <= MAX) { namesum.push_back(namesum.at(i) * 26 + 26); i++; } long long N; cin >> N; for (int k = 0; k < namesum.size(); k++) { if (namesum.at(k) < N && N <= namesum.at(k + 1)) { N = N - namesum.at(k); for (int i = k; i >= 0; i--) { cout << alph.at((N - 1) / ruijyo(26, i)); N = N - ((N - 1) / ruijyo(26, i)) * ruijyo(26, i); } break; } } cout << endl; }
replace
3
4
3
4
0
p02629
C++
Runtime Error
#include <bits/stdc++.h> #define int long long #define double long double using namespace std; const int MOD = 1000000007; const int INF = 1e15; using Graph = vector<vector<int>>; signed main() { int N; cin >> N; string alfa = "abcdefghijklmnopqrstuvwxyz"; string ans = ""; while (N > 0) { ans += alfa.at(N % 26 - 1); N /= 26; } reverse(ans.begin(), ans.end()); cout << ans << endl; }
#include <bits/stdc++.h> #define int long long #define double long double using namespace std; const int MOD = 1000000007; const int INF = 1e15; using Graph = vector<vector<int>>; signed main() { int N; cin >> N; string alfa = "abcdefghijklmnopqrstuvwxyz"; string ans = ""; while (N > 0) { int A = N % 26; A--; if (A < 0) A = 25; ans += alfa.at(A); if (A == 25) N--; N /= 26; } reverse(ans.begin(), ans.end()); cout << ans << endl; }
replace
14
15
14
21
0
p02629
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; template <typename T> T ipow(T b, long long e) { if (e == 0) { return 1; } else if (e % 2 == 0) { return ipow(b * b, e / 2); } else { return ipow(b * b, e / 2) * b; } } // aを1番目、zを26番目としてx番目のcharを返す char get_char(int x) { return 'a' - 1 + x; } int main() { long long N; cin >> N; int i = 1; while (N > ipow<long long>(26, i)) { N -= ipow<long long>(26, i); i++; } string ans; // i文字の名前のうち、N番目を計算して出力 for (int j = i; j > 0; j--) { int c = 1; while (N > ipow(26, j - 1)) { N -= ipow(26, j - 1); c++; } ans += get_char(c); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T> T ipow(T b, long long e) { if (e == 0) { return 1; } else if (e % 2 == 0) { return ipow(b * b, e / 2); } else { return ipow(b * b, e / 2) * b; } } // aを1番目、zを26番目としてx番目のcharを返す char get_char(int x) { return 'a' - 1 + x; } int main() { long long N; cin >> N; int i = 1; while (N > ipow<long long>(26, i)) { N -= ipow<long long>(26, i); i++; } string ans; // i文字の名前のうち、N番目を計算して出力 for (int j = i; j > 0; j--) { int c = 1; while (N > ipow<long long>(26, j - 1)) { N -= ipow<long long>(26, j - 1); c++; } ans += get_char(c); } cout << ans << endl; return 0; }
replace
28
30
28
30
TLE
p02629
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long #define fi first #define se second #define pb push_back #define pr pair<int, int> #define mod 1000000007 #define endl "\n" int power(int a, int b, int m = mod) { if (b == 0) return 1; if (b == 1) return a; int res = power(a, b / 2, m); res = (res * res) % m; if (b & 1) res = (res * a) % m; return res; } int modinv(int a, int m = mod) { return power(a, m - 2, m); } int add(int a, int b, int m = mod) { int c = (a % m + b % m); if (c >= m) c -= m; return c; } int sub(int a, int b, int m = mod) { int c = (a % m - b % m); if (c < 0) c += m; return c; } int mul(int a, int b, int m = mod) { return (a * b) % m; } // x<<1 => x*2 x>>1 => x/2; cout<<flush; // cout<<fixed<<setprecision(10)<<ans<<endl; void solve() { int n, k; cin >> n >> k; int a[n], i, ans = 0; for (i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); for (i = 0; i < k; i++) ans += a[i]; cout << ans << endl; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int t = 1; // cin>>t; for (int i = 1; i <= t; i++) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long #define fi first #define se second #define pb push_back #define pr pair<int, int> #define mod 1000000007 #define endl "\n" int power(int a, int b, int m = mod) { if (b == 0) return 1; if (b == 1) return a; int res = power(a, b / 2, m); res = (res * res) % m; if (b & 1) res = (res * a) % m; return res; } int modinv(int a, int m = mod) { return power(a, m - 2, m); } int add(int a, int b, int m = mod) { int c = (a % m + b % m); if (c >= m) c -= m; return c; } int sub(int a, int b, int m = mod) { int c = (a % m - b % m); if (c < 0) c += m; return c; } int mul(int a, int b, int m = mod) { return (a * b) % m; } // x<<1 => x*2 x>>1 => x/2; cout<<flush; // cout<<fixed<<setprecision(10)<<ans<<endl; void solve() { int n; cin >> n; string s; while (n) { int x = (n - 1) % 26; char c = x + 'a'; s += c; n = (n - 1) / 26; } reverse(s.begin(), s.end()); cout << s << endl; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int t = 1; // cin>>t; for (int i = 1; i <= t; i++) { solve(); } return 0; }
replace
39
48
39
50
0
p02629
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <vector> using namespace std; vector<char> word; void remainder(long long int n) { if (n != 0) { if (n / 26 < 1) { word.push_back('a' + n % 26 - 1); } else if (n / 26 >= 1) { if ((n % 26) == 0) { word.push_back('z'); if (n > 26) remainder((n - 26) / 26); else return; } else { word.push_back('a' + n % 26 - 1); remainder(n / 26); } } } else { return; } } int main() { for (int k = 0;; k++) { long long int num; cin >> num; remainder(num); for (int i = word.size() - 1; i >= 0; i--) cout << word[i]; cout << endl; word.clear(); } return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; vector<char> word; void remainder(long long int n) { if (n != 0) { if (n / 26 < 1) { word.push_back('a' + n % 26 - 1); } else if (n / 26 >= 1) { if ((n % 26) == 0) { word.push_back('z'); if (n > 26) remainder((n - 26) / 26); else return; } else { word.push_back('a' + n % 26 - 1); remainder(n / 26); } } } else { return; } } int main() { long long int num; cin >> num; remainder(num); for (int i = word.size() - 1; i >= 0; i--) cout << word[i]; return 0; }
replace
30
40
30
35
TLE
p02629
C++
Time Limit Exceeded
#line 2 "codes/header.hpp" //%snippet.set('header')% //%snippet.fold()% #ifndef HEADER_H #define HEADER_H // template version 2.0 using namespace std; #include <bits/stdc++.h> // varibable settings const long long INF = 1e18; template <class T> constexpr T inf = numeric_limits<T>::max() / 2.1; #define _overload3(_1, _2, _3, name, ...) name #define _rep(i, n) repi(i, 0, n) #define repi(i, a, b) for (ll i = (ll)(a); i < (ll)(b); ++i) #define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__) #define _rrep(i, n) rrepi(i, 0, n) #define rrepi(i, a, b) for (ll i = (ll)((b)-1); i >= (ll)(a); --i) #define r_rep(...) _overload3(__VA_ARGS__, rrepi, _rrep, )(__VA_ARGS__) #define each(i, a) for (auto &&i : a) #define all(x) (x).begin(), (x).end() #define sz(x) ((int)(x).size()) #define pb(a) push_back(a) #define mp(a, b) make_pair(a, b) #define mt(...) make_tuple(__VA_ARGS__) #define ub upper_bound #define lb lower_bound #define lpos(A, x) (lower_bound(all(A), x) - A.begin()) #define upos(A, x) (upper_bound(all(A), x) - A.begin()) template <class T> inline void chmax(T &a, const T &b) { if ((a) < (b)) (a) = (b); } template <class T> inline void chmin(T &a, const T &b) { if ((a) > (b)) (a) = (b); } template <typename X, typename T> auto make_table(X x, T a) { return vector<T>(x, a); } template <typename X, typename Y, typename Z, typename... Zs> auto make_table(X x, Y y, Z z, Zs... zs) { auto cont = make_table(y, z, zs...); return vector<decltype(cont)>(x, cont); } #define cdiv(a, b) (((a) + (b)-1) / (b)) #define is_in(x, a, b) ((a) <= (x) && (x) < (b)) #define uni(x) \ sort(all(x)); \ x.erase(unique(all(x)), x.end()) #define slice(l, r) substr(l, r - l) typedef long long ll; typedef long double ld; using vl = vector<ll>; using vvl = vector<vl>; using pll = pair<ll, ll>; template <typename T> using PQ = priority_queue<T, vector<T>, greater<T>>; void check_input() { assert(cin.eof() == 0); int tmp; cin >> tmp; assert(cin.eof() == 1); } #if defined(PCM) || defined(LOCAL) #else #define dump(...) ; #define dump_1d(...) ; #define dump_2d(...) ; #define cerrendl ; #endif #endif /* HEADER_H */ //%snippet.end()% #line 2 "codes/solve.cpp" template <class T = ll> using vec = vector<T>; struct Fast { Fast() { std::cin.tie(0); ios::sync_with_stdio(false); } } fast; int solve() { ll n; cin >> n; int k = 1; int mul = 26; ll sum = 26; while (sum < n) { mul *= 26; sum += mul; k++; } dump(k); sum -= mul; mul /= 26; n -= sum; string ans(k, '?'); rep(i, k) { char c = 'a'; while (mul < n) { n -= mul; c++; } ans[i] = c; mul /= 26; } cout << ans << endl; return 0; } int main() { /*{{{*/ solve(); check_input(); return 0; } /*}}}*/
#line 2 "codes/header.hpp" //%snippet.set('header')% //%snippet.fold()% #ifndef HEADER_H #define HEADER_H // template version 2.0 using namespace std; #include <bits/stdc++.h> // varibable settings const long long INF = 1e18; template <class T> constexpr T inf = numeric_limits<T>::max() / 2.1; #define _overload3(_1, _2, _3, name, ...) name #define _rep(i, n) repi(i, 0, n) #define repi(i, a, b) for (ll i = (ll)(a); i < (ll)(b); ++i) #define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__) #define _rrep(i, n) rrepi(i, 0, n) #define rrepi(i, a, b) for (ll i = (ll)((b)-1); i >= (ll)(a); --i) #define r_rep(...) _overload3(__VA_ARGS__, rrepi, _rrep, )(__VA_ARGS__) #define each(i, a) for (auto &&i : a) #define all(x) (x).begin(), (x).end() #define sz(x) ((int)(x).size()) #define pb(a) push_back(a) #define mp(a, b) make_pair(a, b) #define mt(...) make_tuple(__VA_ARGS__) #define ub upper_bound #define lb lower_bound #define lpos(A, x) (lower_bound(all(A), x) - A.begin()) #define upos(A, x) (upper_bound(all(A), x) - A.begin()) template <class T> inline void chmax(T &a, const T &b) { if ((a) < (b)) (a) = (b); } template <class T> inline void chmin(T &a, const T &b) { if ((a) > (b)) (a) = (b); } template <typename X, typename T> auto make_table(X x, T a) { return vector<T>(x, a); } template <typename X, typename Y, typename Z, typename... Zs> auto make_table(X x, Y y, Z z, Zs... zs) { auto cont = make_table(y, z, zs...); return vector<decltype(cont)>(x, cont); } #define cdiv(a, b) (((a) + (b)-1) / (b)) #define is_in(x, a, b) ((a) <= (x) && (x) < (b)) #define uni(x) \ sort(all(x)); \ x.erase(unique(all(x)), x.end()) #define slice(l, r) substr(l, r - l) typedef long long ll; typedef long double ld; using vl = vector<ll>; using vvl = vector<vl>; using pll = pair<ll, ll>; template <typename T> using PQ = priority_queue<T, vector<T>, greater<T>>; void check_input() { assert(cin.eof() == 0); int tmp; cin >> tmp; assert(cin.eof() == 1); } #if defined(PCM) || defined(LOCAL) #else #define dump(...) ; #define dump_1d(...) ; #define dump_2d(...) ; #define cerrendl ; #endif #endif /* HEADER_H */ //%snippet.end()% #line 2 "codes/solve.cpp" template <class T = ll> using vec = vector<T>; struct Fast { Fast() { std::cin.tie(0); ios::sync_with_stdio(false); } } fast; int solve() { ll n; cin >> n; int k = 1; ll mul = 26; ll sum = 26; while (sum < n) { mul *= 26; sum += mul; k++; } dump(k); sum -= mul; mul /= 26; n -= sum; string ans(k, '?'); rep(i, k) { char c = 'a'; while (mul < n) { n -= mul; c++; } ans[i] = c; mul /= 26; } cout << ans << endl; return 0; } int main() { /*{{{*/ solve(); check_input(); return 0; } /*}}}*/
replace
94
95
94
95
TLE
p02629
C++
Time Limit Exceeded
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; typedef long long ll; typedef pair<ll, ll> PLL; typedef vector<int> VI; typedef vector<char> VC; typedef vector<double> VD; typedef vector<string> VS; typedef vector<ll> VLL; typedef vector<PLL> VP; const static int INF = 1000000000; const static int MOD = 1000000007; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; 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; } #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 rept(i, m, n) for (ll i = m; i < n; i++) #define stl_rep(itr, x) for (auto itr = x.begin(); itr != x.end(); ++itr) #define all(x) (x).begin(), (x).end() #define F first #define S second #define PF push_front #define PB push_back #define SORT(V) sort((V).begin(), (V).end()) #define RVERSE(V) reverse((V).begin(), (V).end()) #define paired make_pair #define PRINT(V) \ for (auto v : (V)) \ cout << v << " " // charを整数に int ctoi(char c) { if (c >= '0' && c <= '9') { return c - '0'; } return 0; } // 累積和 for (int i = 0; i < N; ++i) s[i+1] = s[i] + a[i]; void cum_sum(int N, vector<double> a, vector<double> &s) { for (int i = 0; i < N; i++) { s[i + 1] = s[i] + a[i]; } } // ユークリッドの控除法 ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } // 最小公倍数 ll lcm(ll a, ll b) { ll g = gcd(a, b); return a / g * b; // Be careful not to overflow } // 素数判定 bool is_prime(long long n) { if (n <= 1) return false; for (long long p = 2; p * p <= n; ++p) { if (n % p == 0) return false; } return true; } int getdigit(ll num) { unsigned digit = 0; while (num != 0) { num /= 10; digit++; } return digit; } // 空白文字も入力 getline(cin, S); // 桁数指定 setprecision int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll N; string ans = ""; cin >> N; while (1) { N--; ans += (char)('a' + (N % 26)); N /= 26; } reverse(all(ans)); cout << ans << endl; }
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; typedef long long ll; typedef pair<ll, ll> PLL; typedef vector<int> VI; typedef vector<char> VC; typedef vector<double> VD; typedef vector<string> VS; typedef vector<ll> VLL; typedef vector<PLL> VP; const static int INF = 1000000000; const static int MOD = 1000000007; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; 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; } #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 rept(i, m, n) for (ll i = m; i < n; i++) #define stl_rep(itr, x) for (auto itr = x.begin(); itr != x.end(); ++itr) #define all(x) (x).begin(), (x).end() #define F first #define S second #define PF push_front #define PB push_back #define SORT(V) sort((V).begin(), (V).end()) #define RVERSE(V) reverse((V).begin(), (V).end()) #define paired make_pair #define PRINT(V) \ for (auto v : (V)) \ cout << v << " " // charを整数に int ctoi(char c) { if (c >= '0' && c <= '9') { return c - '0'; } return 0; } // 累積和 for (int i = 0; i < N; ++i) s[i+1] = s[i] + a[i]; void cum_sum(int N, vector<double> a, vector<double> &s) { for (int i = 0; i < N; i++) { s[i + 1] = s[i] + a[i]; } } // ユークリッドの控除法 ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } // 最小公倍数 ll lcm(ll a, ll b) { ll g = gcd(a, b); return a / g * b; // Be careful not to overflow } // 素数判定 bool is_prime(long long n) { if (n <= 1) return false; for (long long p = 2; p * p <= n; ++p) { if (n % p == 0) return false; } return true; } int getdigit(ll num) { unsigned digit = 0; while (num != 0) { num /= 10; digit++; } return digit; } // 空白文字も入力 getline(cin, S); // 桁数指定 setprecision int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll N; string ans = ""; cin >> N; while (N) { N--; ans += (char)('a' + (N % 26)); N /= 26; } reverse(all(ans)); cout << ans << endl; }
replace
96
97
96
97
TLE
p02629
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, x, n) for (int i = x; i < n; i++) #define repr(i, x, n) for (int i = x; i > n; i--) #define pii pair<int, int> #define ll long long const long long iinf = 1e18; const int inf = 1e9; const int MOD = (1e6 + 3); int dir4[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; int knight[8][2] = {{2, 1}, {2, -1}, {-2, 1}, {-2, -1}, {1, 2}, {-1, 2}, {1, -2}, {-1, -2}}; using namespace std; void solve() { long long n; cin >> n; string s = "zabcdefghijklmnopqrstuvwxy"; string ans = ""; long long x = 26; while (x <= n) { ans += s[n % x]; x = x + x * 26; // n -= 26; // n /= 26LL; } reverse(ans.begin(), ans.end()); cout << ans; } // new line is printed in the main int main() { int t = 1; // cin >> t; // comment out if no test case is required // int i = 1; while (t--) { // cout << "Case " << i++ << ": "; solve(); // printf("\n"); } return 0; }
#include <bits/stdc++.h> #define rep(i, x, n) for (int i = x; i < n; i++) #define repr(i, x, n) for (int i = x; i > n; i--) #define pii pair<int, int> #define ll long long const long long iinf = 1e18; const int inf = 1e9; const int MOD = (1e6 + 3); int dir4[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; int knight[8][2] = {{2, 1}, {2, -1}, {-2, 1}, {-2, -1}, {1, 2}, {-1, 2}, {1, -2}, {-1, -2}}; using namespace std; void solve() { long long n; cin >> n; string s = "zabcdefghijklmnopqrstuvwxy"; string ans = ""; while (n) { int rem = n % 26; ans += s[rem]; if (rem == 0) n--; n /= 26LL; } reverse(ans.begin(), ans.end()); cout << ans; } // new line is printed in the main int main() { int t = 1; // cin >> t; // comment out if no test case is required // int i = 1; while (t--) { // cout << "Case " << i++ << ": "; solve(); // printf("\n"); } return 0; }
replace
19
25
19
25
0
p02629
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define ll long long #define pb push_back using namespace std; const int mod = 1e9 + 7; const int nax = 1e7; const ll inf = 1e10 + 17; vector<int> prime(nax, -1); ll pow1(ll a, ll b) { ll res = 1; while (b > 0) { if (b % 2) res = (res * 1LL * a); a = (a * 1LL * a); b /= 2; } return res; } ll mul(ll a, ll b) { return (a * 1LL * b) % mod; } ll eval(int i) { ll ans = (26 * 1LL * (pow1(26, i) - 1)) / (25 * 1LL); } int main() { int n = 12; ll N, i; cin >> N; for (i = 1; i <= 11; i++) { if (eval(i) >= N) { break; } } // cout<<i<<endl; int y = i - 1; N -= eval(y); int len = i; vector<char> ans(len + 1); set<char> unused; // cout<<N<<endl; for (int k = 0; k < 26; k++) { unused.insert((char)(k + 'a')); } for (int j = 1; j <= len; j++) { ll ev = pow1(26, len - j); ll x = (N + ev - 1) / ev; N -= (x - 1) * ev; // cout<<N<<endl; ans[j] = (char)(x - 1 + 'a'); // unused.erase(ans[j]); } for (int i = 1; i <= len; i++) { cout << ans[i]; } }
#include <bits/stdc++.h> #define ll long long #define pb push_back using namespace std; const int mod = 1e9 + 7; const int nax = 1e7; const ll inf = 1e10 + 17; vector<int> prime(nax, -1); ll pow1(ll a, ll b) { ll res = 1; while (b > 0) { if (b % 2) res = (res * 1LL * a); a = (a * 1LL * a); b /= 2; } return res; } ll mul(ll a, ll b) { return (a * 1LL * b) % mod; } ll eval(int i) { ll ans = (26 * 1LL * (pow1(26, i) - 1)) / (25 * 1LL); return ans; } int main() { int n = 12; ll N, i; cin >> N; for (i = 1; i <= 11; i++) { if (eval(i) >= N) { break; } } // cout<<i<<endl; int y = i - 1; N -= eval(y); int len = i; vector<char> ans(len + 1); set<char> unused; // cout<<N<<endl; for (int k = 0; k < 26; k++) { unused.insert((char)(k + 'a')); } for (int j = 1; j <= len; j++) { ll ev = pow1(26, len - j); ll x = (N + ev - 1) / ev; N -= (x - 1) * ev; // cout<<N<<endl; ans[j] = (char)(x - 1 + 'a'); // unused.erase(ans[j]); } for (int i = 1; i <= len; i++) { cout << ans[i]; } }
replace
19
20
19
23
TLE
p02629
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int64_t N; cin >> N; int64_t P = N / 26; vector<char> namae(P + 1); int64_t i = 0; // cout << N << " " << i << endl ; while (N != 0) { N--; int M = N % 26; namae.at(i) = 'a' + M; N /= 26; i++; // cout << N << " " << i << endl ; } for (int j = i - 1; j >= 0; j--) { cout << namae.at(j); } }
#include <bits/stdc++.h> using namespace std; int main() { int64_t N; cin >> N; int P = N / 26; vector<char> namae(100); int i = 0; // cout << N << " " << i << endl ; while (N != 0) { N--; int M = N % 26; namae.at(i) = 'a' + M; N /= 26; i++; // cout << N << " " << i << endl ; } for (int j = i - 1; j >= 0; j--) { cout << namae.at(j); } }
replace
6
9
6
9
0
p02629
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long i64; i64 N, Len; string ans; inline void pre() { i64 l = 1, Bas = 26, Ans = 26; while (Ans < N) { Ans += (Bas *= 26); l++; } ans = string(l, 'a'); N -= Ans - Bas; Len = l; } inline void work() { pre(); N--; int Bas = 1, Cur = 0; for (int i = 1; i < Len; i++) Bas *= 26; while (Cur < ans.size()) { while (N >= Bas) N -= Bas, ans[Cur]++; Cur++; Bas /= 26; } cout << ans; } int main() { cin >> N; work(); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long i64; i64 N, Len; string ans; inline void pre() { i64 l = 1, Bas = 26, Ans = 26; while (Ans < N) { Ans += (Bas *= 26); l++; } ans = string(l, 'a'); N -= Ans - Bas; Len = l; } inline void work() { pre(); N--; i64 Bas = 1, Cur = 0; for (int i = 1; i < Len; i++) Bas *= 26; while (Cur < ans.size()) { while (N >= Bas) N -= Bas, ans[Cur]++; Cur++; Bas /= 26; } cout << ans; } int main() { cin >> N; work(); return 0; }
replace
18
19
18
19
TLE
p02629
C++
Runtime Error
#include <iostream> #include <string> using namespace std; int main() { long N; cin >> N; N--; string a_to_z = "abcdefghijklmnopqrstuvwxyz"; string name = ""; int max = 0; long x = 1; long M = N; do { name = a_to_z.substr(N % 26, 1) + name; N = (N - 26) / 26; x *= 26; max += x; } while (M >= max); cout << name; }
#include <iostream> #include <string> using namespace std; int main() { long N; cin >> N; N--; string a_to_z = "abcdefghijklmnopqrstuvwxyz"; string name = ""; long max = 0; long x = 1; long M = N; do { name = a_to_z.substr(N % 26, 1) + name; N = (N - 26) / 26; x *= 26; max += x; } while (M >= max); cout << name; }
replace
11
12
11
12
0
p02629
C++
Runtime Error
#pragma GCC target("avx") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #define _USE_MATH_DEFINES #include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <time.h> #include <tuple> #include <unordered_map> #include <vector> using namespace std; using ll = long long; using ld = long double; #define int long long #define all(a) (a).begin(), (a).end() #define fs first #define sc second #define xx first #define yy second.first #define zz second.second #define H pair<int, int> #define P pair<int, pair<int, int>> #define Q(i, j, k) mkp(i, mkp(j, k)) #define rng(i, s, n) for (int i = (s); i < (n); i++) #define rep(i, n) rng(i, 0, (n)) #define mkp make_pair #define vec vector #define vi vec<int> #define pb emplace_back #define siz(a) (int)(a).size() #define crdcomp(b) \ sort(all((b))); \ (b).erase(unique(all((b))), (b).end()) #define getidx(b, i) lower_bound(all(b), (i)) - (b).begin() #define ssp(i, n) (i == (int)(n)-1 ? "\n" : " ") #define ctoi(c) (int)(c - '0') #define itoc(c) (char)(c + '0') #define cyes printf("Yes\n") #define cno printf("No\n") #define cdf(n) \ int quetimes_ = (n); \ rep(qq123_, quetimes_) #define gcj printf("Case #%lld: ", qq123_ + 1) #define readv(a, n) \ a.resize(n, 0); \ rep(i, (n)) a[i] = read() #define found(a, x) (a.find(x) != a.end()) // #define endl "\n" constexpr int mod = 1e9 + 7; constexpr int Mod = 998244353; constexpr ld EPS = 1e-10; constexpr ll inf = 3 * 1e18; constexpr int Inf = 15 * 1e8; constexpr int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1}; 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; } ll read() { ll u, k = scanf("%lld", &u); return u; } string reads() { string s; cin >> s; return s; } H readh(bool g = 0) { H u; int k = scanf("%lld %lld", &u.fs, &u.sc); if (g) u.fs--, u.sc--; return u; } bool ina(H t, int h, int w) { return 0 <= t.fs && t.fs < h && 0 <= t.sc && t.sc < w; } bool ina(int t, int l, int r) { return l <= t && t < r; } ll gcd(ll i, ll j) { return j ? gcd(j, i % j) : i; } ll popcount(ll x) { int sum = 0; for (int i = 0; i < 60; i++) if ((1ll << i) & x) sum++; return sum; } class mint { public: ll v; mint(ll v = 0) { s(v % mod + mod); } constexpr static int mod = Mod; constexpr static int fn_ = 500005; static mint fact[fn_], comp[fn_]; mint pow(int x) const { mint b(v), c(1); while (x) { if (x & 1) c *= b; b *= b; x >>= 1; } return c; } inline mint &s(int vv) { v = vv < mod ? vv : vv - mod; return *this; } inline mint inv() const { return pow(mod - 2); } inline mint operator-() const { return mint() - *this; } inline mint &operator+=(const mint b) { return s(v + b.v); } inline mint &operator-=(const mint b) { return s(v + mod - b.v); } inline mint &operator*=(const mint b) { v = v * b.v % mod; return *this; } inline mint &operator/=(const mint b) { v = v * b.inv().v % mod; return *this; } inline mint operator+(const mint b) const { return mint(v) += b; } inline mint operator-(const mint b) const { return mint(v) -= b; } inline mint operator*(const mint b) const { return mint(v) *= b; } inline mint operator/(const mint b) const { return mint(v) /= b; } friend ostream &operator<<(ostream &os, const mint &m) { return os << m.v; } friend istream &operator>>(istream &is, mint &m) { int x; is >> x; m = mint(x); return is; } bool operator<(const mint &r) const { return v < r.v; } bool operator>(const mint &r) const { return v > r.v; } bool operator<=(const mint &r) const { return v <= r.v; } bool operator>=(const mint &r) const { return v >= r.v; } bool operator==(const mint &r) const { return v == r.v; } bool operator!=(const mint &r) const { return v != r.v; } explicit operator bool() const { return v; } explicit operator int() const { return v; } mint comb(mint k) { if (k > *this) return mint(); if (!fact[0]) combinit(); if (v >= fn_) { if (k > *this - k) k = *this - k; mint tmp(1); for (int i = v; i >= v - k.v + 1; i--) tmp *= mint(i); return tmp * comp[k.v]; } else return fact[v] * comp[k.v] * comp[v - k.v]; } // nCk static void combinit() { fact[0] = 1; for (int i = 1; i < fn_; i++) fact[i] = fact[i - 1] * mint(i); comp[fn_ - 1] = fact[fn_ - 1].inv(); for (int i = fn_ - 2; i >= 0; i--) comp[i] = comp[i + 1] * mint(i + 1); } }; mint mint::fact[fn_], mint::comp[fn_]; //-------------------------------------------------------------- struct st { int i, x, y, t; // バス会社(載っていないときは-1)、座標、時間 bool operator<(st a) const { return t < a.t; } bool operator>(st a) const { return t > a.t; } }; //--------------------------------------------------------------------- signed main() { int n; cin >> n; int si = 1, t = 1; vi a; rep(i, 10) { a.pb(t); t *= 26; } n--; while (n >= a[si]) { n -= a[si]; si++; } string s = ""; while (si > 0) { s += (char)(n / a[si - 1] + 'a'); n %= a[si - 1]; si--; } cout << s << endl; }
#pragma GCC target("avx") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #define _USE_MATH_DEFINES #include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <time.h> #include <tuple> #include <unordered_map> #include <vector> using namespace std; using ll = long long; using ld = long double; #define int long long #define all(a) (a).begin(), (a).end() #define fs first #define sc second #define xx first #define yy second.first #define zz second.second #define H pair<int, int> #define P pair<int, pair<int, int>> #define Q(i, j, k) mkp(i, mkp(j, k)) #define rng(i, s, n) for (int i = (s); i < (n); i++) #define rep(i, n) rng(i, 0, (n)) #define mkp make_pair #define vec vector #define vi vec<int> #define pb emplace_back #define siz(a) (int)(a).size() #define crdcomp(b) \ sort(all((b))); \ (b).erase(unique(all((b))), (b).end()) #define getidx(b, i) lower_bound(all(b), (i)) - (b).begin() #define ssp(i, n) (i == (int)(n)-1 ? "\n" : " ") #define ctoi(c) (int)(c - '0') #define itoc(c) (char)(c + '0') #define cyes printf("Yes\n") #define cno printf("No\n") #define cdf(n) \ int quetimes_ = (n); \ rep(qq123_, quetimes_) #define gcj printf("Case #%lld: ", qq123_ + 1) #define readv(a, n) \ a.resize(n, 0); \ rep(i, (n)) a[i] = read() #define found(a, x) (a.find(x) != a.end()) // #define endl "\n" constexpr int mod = 1e9 + 7; constexpr int Mod = 998244353; constexpr ld EPS = 1e-10; constexpr ll inf = 3 * 1e18; constexpr int Inf = 15 * 1e8; constexpr int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1}; 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; } ll read() { ll u, k = scanf("%lld", &u); return u; } string reads() { string s; cin >> s; return s; } H readh(bool g = 0) { H u; int k = scanf("%lld %lld", &u.fs, &u.sc); if (g) u.fs--, u.sc--; return u; } bool ina(H t, int h, int w) { return 0 <= t.fs && t.fs < h && 0 <= t.sc && t.sc < w; } bool ina(int t, int l, int r) { return l <= t && t < r; } ll gcd(ll i, ll j) { return j ? gcd(j, i % j) : i; } ll popcount(ll x) { int sum = 0; for (int i = 0; i < 60; i++) if ((1ll << i) & x) sum++; return sum; } class mint { public: ll v; mint(ll v = 0) { s(v % mod + mod); } constexpr static int mod = Mod; constexpr static int fn_ = 500005; static mint fact[fn_], comp[fn_]; mint pow(int x) const { mint b(v), c(1); while (x) { if (x & 1) c *= b; b *= b; x >>= 1; } return c; } inline mint &s(int vv) { v = vv < mod ? vv : vv - mod; return *this; } inline mint inv() const { return pow(mod - 2); } inline mint operator-() const { return mint() - *this; } inline mint &operator+=(const mint b) { return s(v + b.v); } inline mint &operator-=(const mint b) { return s(v + mod - b.v); } inline mint &operator*=(const mint b) { v = v * b.v % mod; return *this; } inline mint &operator/=(const mint b) { v = v * b.inv().v % mod; return *this; } inline mint operator+(const mint b) const { return mint(v) += b; } inline mint operator-(const mint b) const { return mint(v) -= b; } inline mint operator*(const mint b) const { return mint(v) *= b; } inline mint operator/(const mint b) const { return mint(v) /= b; } friend ostream &operator<<(ostream &os, const mint &m) { return os << m.v; } friend istream &operator>>(istream &is, mint &m) { int x; is >> x; m = mint(x); return is; } bool operator<(const mint &r) const { return v < r.v; } bool operator>(const mint &r) const { return v > r.v; } bool operator<=(const mint &r) const { return v <= r.v; } bool operator>=(const mint &r) const { return v >= r.v; } bool operator==(const mint &r) const { return v == r.v; } bool operator!=(const mint &r) const { return v != r.v; } explicit operator bool() const { return v; } explicit operator int() const { return v; } mint comb(mint k) { if (k > *this) return mint(); if (!fact[0]) combinit(); if (v >= fn_) { if (k > *this - k) k = *this - k; mint tmp(1); for (int i = v; i >= v - k.v + 1; i--) tmp *= mint(i); return tmp * comp[k.v]; } else return fact[v] * comp[k.v] * comp[v - k.v]; } // nCk static void combinit() { fact[0] = 1; for (int i = 1; i < fn_; i++) fact[i] = fact[i - 1] * mint(i); comp[fn_ - 1] = fact[fn_ - 1].inv(); for (int i = fn_ - 2; i >= 0; i--) comp[i] = comp[i + 1] * mint(i + 1); } }; mint mint::fact[fn_], mint::comp[fn_]; //-------------------------------------------------------------- struct st { int i, x, y, t; // バス会社(載っていないときは-1)、座標、時間 bool operator<(st a) const { return t < a.t; } bool operator>(st a) const { return t > a.t; } }; //--------------------------------------------------------------------- signed main() { int n; cin >> n; int si = 1, t = 1; vi a; while (t < 1e16) { a.pb(t); t *= 26; } n--; while (n >= a[si]) { n -= a[si]; si++; } string s = ""; while (si > 0) { s += (char)(n / a[si - 1] + 'a'); n %= a[si - 1]; si--; } cout << s << endl; }
replace
208
209
208
209
0
p02629
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, N) for (int i = 0; i < N; i++) typedef long long ll; #define dump(x) cerr << #x << "=" << x << endl using P = pair<int, int>; int main() { ll n; cin >> n; vector<char> alpha; alpha.push_back('z'); for (char i = 'a'; i <= 'z'; i++) { alpha.push_back(i); } string ans; if (n > 26LL) { n -= 1; } else { cout << alpha.at(n % 26) << endl; ; return 0; } while (n > 26LL) { ll tmp = (n + 1) % 26LL; if (tmp != 0) ans.push_back(alpha.at(tmp)); else ans.push_back(alpha.at(26)); n -= 26; n /= 26; } dump(n); ans += alpha.at(n + 1); reverse(ans.begin(), ans.end()); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, N) for (int i = 0; i < N; i++) typedef long long ll; #define dump(x) cerr << #x << "=" << x << endl using P = pair<int, int>; int main() { ll n; cin >> n; vector<char> alpha; alpha.push_back('z'); for (char i = 'a'; i <= 'z'; i++) { alpha.push_back(i); } string ans; if (n > 26LL) { n -= 1; } else { cout << alpha.at(n % 26) << endl; ; return 0; } while (n >= 26LL) { ll tmp = (n + 1) % 26LL; if (tmp != 0) ans.push_back(alpha.at(tmp)); else ans.push_back(alpha.at(26)); n -= 26; n /= 26; } dump(n); ans += alpha.at(n + 1); reverse(ans.begin(), ans.end()); cout << ans << endl; return 0; }
replace
25
26
25
26
0
p02629
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using ull = unsigned long long; using uint = unsigned; using pcc = pair<char, char>; using pii = pair<int, int>; using pll = pair<ll, ll>; using pdd = pair<double, double>; using tuplis = array<ll, 3>; template <class T> using pq = priority_queue<T, vector<T>, greater<T>>; const ll LINF = 0x1fffffffffffffff; const ll MINF = 0x7fffffffffff; const int INF = 0x3fffffff; const int MOD = 1000000007; const int MODD = 998244353; const ld DINF = numeric_limits<ld>::infinity(); const ld EPS = 1e-9; const ld PI = 3.1415926535897932; const ll dx[] = {0, 1, 0, -1, 1, -1, 1, -1}; const ll dy[] = {1, 0, -1, 0, 1, 1, -1, -1}; #define overload4(_1, _2, _3, _4, name, ...) name #define overload3(_1, _2, _3, name, ...) name #define rep1(n) for (ll i = 0; i < n; ++i) #define rep2(i, n) for (ll i = 0; i < n; ++i) #define rep3(i, a, b) for (ll i = a; i < b; ++i) #define rep4(i, a, b, c) for (ll i = a; i < b; i += c) #define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__) #define rrep1(n) for (ll i = (n); i--;) #define rrep2(i, n) for (ll i = (n); i--;) #define rrep3(i, a, b) for (ll i = (b); i-- > (a);) #define rrep4(i, a, b, c) for (ll i = a + (b - a - 1) / c * c; i >= a; i -= c) #define rrep(...) \ overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__) #define each1(i, a) for (auto &&i : a) #define each2(x, y, a) for (auto &&[x, y] : a) #define each3(x, y, z, a) for (auto &&[x, y, z] : a) #define each(...) overload4(__VA_ARGS__, each3, each2, each1)(__VA_ARGS__) #define all1(i) begin(i), end(i) #define all2(i, a) begin(i), begin(i) + a #define all3(i, a, b) begin(i) + a, begin(i) + b #define all(...) overload3(__VA_ARGS__, all3, all2, all1)(__VA_ARGS__) #define rall1(i) (i).rbegin(), (i).rend() #define rall2(i, k) (i).rbegin(), (i).rbegin() + k #define rall3(i, a, b) (i).rbegin() + a, (i).rbegin() + b #define rall(...) overload3(__VA_ARGS__, rall3, rall2, rall1)(__VA_ARGS__) #define sum(...) accumulate(all(__VA_ARGS__), 0LL) #define dsum(...) accumulate(all(__VA_ARGS__), 0.0L) #define elif else if #define unless(a) if (!(a)) #define mp make_pair #define mt make_tuple #define INT(...) \ int __VA_ARGS__; \ in(__VA_ARGS__) #define LL(...) \ ll __VA_ARGS__; \ in(__VA_ARGS__) #define ULL(...) \ ull __VA_ARGS__; \ in(__VA_ARGS__) #define STR(...) \ string __VA_ARGS__; \ in(__VA_ARGS__) #define CHR(...) \ char __VA_ARGS__; \ in(__VA_ARGS__) #define DBL(...) \ double __VA_ARGS__; \ in(__VA_ARGS__) #define LD(...) \ ld __VA_ARGS__; \ in(__VA_ARGS__) #define Sort(a) sort(all(a)) #define Rev(a) reverse(all(a)) #define Uniq(a) \ sort(all(a)); \ a.erase(unique(all(a)), end(a)) #define vec(type, name, ...) vector<type> name(__VA_ARGS__) #define VEC(type, name, size) \ vector<type> name(size); \ in(name) #define vv(type, name, h, ...) \ vector<vector<type>> name(h, vector<type>(__VA_ARGS__)) #define VV(type, name, h, w) \ vector<vector<type>> name(h, vector<type>(w)); \ in(name) #define vvv(type, name, h, w, ...) \ vector<vector<vector<type>>> name( \ h, vector<vector<type>>(w, vector<type>(__VA_ARGS__))) template <class T> auto min(const T &a) { return *min_element(all(a)); } template <class T> auto max(const T &a) { return *max_element(all(a)); } inline ll popcnt(ull a) { return __builtin_popcountll(a); } ll gcd(ll a, ll b) { while (b) { ll c = b; b = a % b; a = c; } return a; } ll lcm(ll a, ll b) { if (!a || !b) return 0; return a * b / gcd(a, b); } ll intpow(ll a, ll b) { ll ans = 1; while (b) { if (b & 1) ans *= a; a *= a; b /= 2; } return ans; } ll modpow(ll a, ll b, ll p) { ll ans = 1; while (b) { if (b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; } template <class T, class U> bool chmin(T &a, const U &b) { if (a > b) { a = b; return 1; } return 0; } template <class T, class U> bool chmax(T &a, const U &b) { if (a < b) { a = b; return 1; } return 0; } vector<ll> iota(ll n) { vector<ll> a(n); iota(a.begin(), a.end(), 0); return a; } vector<pll> factor(ull x) { vector<pll> ans; for (ll i = 2; i * i <= x; i++) if (x % i == 0) { ans.push_back({i, 1}); while ((x /= i) % i == 0) ans.back().second++; } if (x != 1) ans.push_back({x, 1}); return ans; } map<ll, ll> factor_map(ull x) { map<ll, ll> ans; for (ll i = 2; i * i <= x; i++) if (x % i == 0) { ans[i] = 1; while ((x /= i) % i == 0) ans[i]++; } if (x != 1) ans[x] = 1; return ans; } vector<uint> divisor(uint x) { vector<uint> ans; for (uint i = 1; i * i <= x; i++) if (x % i == 0) ans.push_back(i); rrep(ans.size() - (ans.back() * ans.back() == x)) ans.push_back(x / ans[i]); return ans; } template <class T> unordered_map<T, ll> press(vector<T> &a) { auto b = a; sort(all(b)); b.erase(unique(all(b)), b.end()); unordered_map<T, ll> ans; rep(b.size()) ans[b[i]] = i; each(i, a) i = ans[i]; return ans; } template <class T> map<T, ll> press_map(vector<T> &a) { auto b = a; sort(all(b)); b.erase(unique(all(b)), b.end()); map<T, ll> ans; rep(b.size()) ans[b[i]] = i; each(i, a) i = ans[i]; return ans; } int scan() { return getchar(); } void scan(int &a) { scanf("%d", &a); } void scan(unsigned &a) { scanf("%u", &a); } void scan(long &a) { scanf("%ld", &a); } void scan(long long &a) { scanf("%lld", &a); } void scan(unsigned long long &a) { scanf("%llu", &a); } void scan(char &a) { do { a = getchar(); } while (a == ' ' || a == '\n'); } void scan(float &a) { scanf("%f", &a); } void scan(double &a) { scanf("%lf", &a); } void scan(long double &a) { scanf("%Lf", &a); } void scan(vector<bool> &a) { for (unsigned i = 0; i < a.size(); i++) { int b; scan(b); a[i] = b; } } void scan(char a[]) { scanf("%s", a); } void scan(string &a) { cin >> a; } template <class T> void scan(vector<T> &); template <class T, size_t size> void scan(array<T, size> &); template <class T, class L> void scan(pair<T, L> &); template <class T, size_t size> void scan(T (&)[size]); template <class T> void scan(vector<T> &a) { for (auto &&i : a) scan(i); } template <class T> void scan(deque<T> &a) { for (auto &&i : a) scan(i); } template <class T, size_t size> void scan(array<T, size> &a) { for (auto &&i : a) scan(i); } template <class T, class L> void scan(pair<T, L> &p) { scan(p.first); scan(p.second); } template <class T, size_t size> void scan(T (&a)[size]) { for (auto &&i : a) scan(i); } template <class T> void scan(T &a) { cin >> a; } void in() {} template <class Head, class... Tail> void in(Head &head, Tail &...tail) { scan(head); in(tail...); } void print() { putchar(' '); } void print(bool a) { printf("%d", a); } void print(int a) { printf("%d", a); } void print(unsigned a) { printf("%u", a); } void print(long a) { printf("%ld", a); } void print(long long a) { printf("%lld", a); } void print(unsigned long long a) { printf("%llu", a); } void print(char a) { printf("%c", a); } void print(char a[]) { printf("%s", a); } void print(const char a[]) { printf("%s", a); } void print(float a) { printf("%.15f", a); } void print(double a) { printf("%.15f", a); } void print(long double a) { printf("%.15Lf", a); } void print(const string &a) { for (auto &&i : a) print(i); } template <class T> void print(const vector<T> &); template <class T, size_t size> void print(const array<T, size> &); template <class T, class L> void print(const pair<T, L> &p); template <class T, size_t size> void print(const T (&)[size]); template <class T> void print(const vector<T> &a) { if (a.empty()) return; print(a[0]); for (auto i = a.begin(); ++i != a.end();) { putchar(' '); print(*i); } } template <class T> void print(const deque<T> &a) { if (a.empty()) return; print(a[0]); for (auto i = a.begin(); ++i != a.end();) { putchar(' '); print(*i); } } template <class T, size_t size> void print(const array<T, size> &a) { print(a[0]); for (auto i = a.begin(); ++i != a.end();) { putchar(' '); print(*i); } } template <class T, class L> void print(const pair<T, L> &p) { print(p.first); putchar(' '); print(p.second); } template <class T, size_t size> void print(const T (&a)[size]) { print(a[0]); for (auto i = a; ++i != end(a);) { putchar(' '); print(*i); } } template <class T> void print(const T &a) { cout << a; } int out() { putchar('\n'); return 0; } template <class T> int out(const T &t) { print(t); putchar('\n'); return 0; } template <class Head, class... Tail> int out(const Head &head, const Tail &...tail) { print(head); putchar(' '); out(tail...); return 0; } #ifdef DEBUG inline ll __lg(ull __n) { return sizeof(ull) * __CHAR_BIT__ - 1 - __builtin_clzll(__n); } #define debug(...) \ { \ print(#__VA_ARGS__); \ print(":"); \ out(__VA_ARGS__); \ } #else #define debug(...) void(0) #endif int first(bool i = true) { return out(i ? "first" : "second"); } int yes(bool i = true) { return out(i ? "yes" : "no"); } int Yes(bool i = true) { return out(i ? "Yes" : "No"); } int No() { return out("No"); } int YES(bool i = true) { return out(i ? "YES" : "NO"); } int NO() { return out("NO"); } int Yay(bool i = true) { return out(i ? "Yay!" : ":("); } int possible(bool i = true) { return out(i ? "possible" : "impossible"); } int Possible(bool i = true) { return out(i ? "Possible" : "Impossible"); } int POSSIBLE(bool i = true) { return out(i ? "POSSIBLE" : "IMPOSSIBLE"); } void Case(ll i) { printf("Case #%lld: ", i); } signed main() { LL(n); n--; vec(ll, a, 0); while (n) { a.push_back(n % 26); n /= 26; } a[0]++; Rev(a); each(i, a) print(char('a' - 1 + i)); }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using ull = unsigned long long; using uint = unsigned; using pcc = pair<char, char>; using pii = pair<int, int>; using pll = pair<ll, ll>; using pdd = pair<double, double>; using tuplis = array<ll, 3>; template <class T> using pq = priority_queue<T, vector<T>, greater<T>>; const ll LINF = 0x1fffffffffffffff; const ll MINF = 0x7fffffffffff; const int INF = 0x3fffffff; const int MOD = 1000000007; const int MODD = 998244353; const ld DINF = numeric_limits<ld>::infinity(); const ld EPS = 1e-9; const ld PI = 3.1415926535897932; const ll dx[] = {0, 1, 0, -1, 1, -1, 1, -1}; const ll dy[] = {1, 0, -1, 0, 1, 1, -1, -1}; #define overload4(_1, _2, _3, _4, name, ...) name #define overload3(_1, _2, _3, name, ...) name #define rep1(n) for (ll i = 0; i < n; ++i) #define rep2(i, n) for (ll i = 0; i < n; ++i) #define rep3(i, a, b) for (ll i = a; i < b; ++i) #define rep4(i, a, b, c) for (ll i = a; i < b; i += c) #define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__) #define rrep1(n) for (ll i = (n); i--;) #define rrep2(i, n) for (ll i = (n); i--;) #define rrep3(i, a, b) for (ll i = (b); i-- > (a);) #define rrep4(i, a, b, c) for (ll i = a + (b - a - 1) / c * c; i >= a; i -= c) #define rrep(...) \ overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__) #define each1(i, a) for (auto &&i : a) #define each2(x, y, a) for (auto &&[x, y] : a) #define each3(x, y, z, a) for (auto &&[x, y, z] : a) #define each(...) overload4(__VA_ARGS__, each3, each2, each1)(__VA_ARGS__) #define all1(i) begin(i), end(i) #define all2(i, a) begin(i), begin(i) + a #define all3(i, a, b) begin(i) + a, begin(i) + b #define all(...) overload3(__VA_ARGS__, all3, all2, all1)(__VA_ARGS__) #define rall1(i) (i).rbegin(), (i).rend() #define rall2(i, k) (i).rbegin(), (i).rbegin() + k #define rall3(i, a, b) (i).rbegin() + a, (i).rbegin() + b #define rall(...) overload3(__VA_ARGS__, rall3, rall2, rall1)(__VA_ARGS__) #define sum(...) accumulate(all(__VA_ARGS__), 0LL) #define dsum(...) accumulate(all(__VA_ARGS__), 0.0L) #define elif else if #define unless(a) if (!(a)) #define mp make_pair #define mt make_tuple #define INT(...) \ int __VA_ARGS__; \ in(__VA_ARGS__) #define LL(...) \ ll __VA_ARGS__; \ in(__VA_ARGS__) #define ULL(...) \ ull __VA_ARGS__; \ in(__VA_ARGS__) #define STR(...) \ string __VA_ARGS__; \ in(__VA_ARGS__) #define CHR(...) \ char __VA_ARGS__; \ in(__VA_ARGS__) #define DBL(...) \ double __VA_ARGS__; \ in(__VA_ARGS__) #define LD(...) \ ld __VA_ARGS__; \ in(__VA_ARGS__) #define Sort(a) sort(all(a)) #define Rev(a) reverse(all(a)) #define Uniq(a) \ sort(all(a)); \ a.erase(unique(all(a)), end(a)) #define vec(type, name, ...) vector<type> name(__VA_ARGS__) #define VEC(type, name, size) \ vector<type> name(size); \ in(name) #define vv(type, name, h, ...) \ vector<vector<type>> name(h, vector<type>(__VA_ARGS__)) #define VV(type, name, h, w) \ vector<vector<type>> name(h, vector<type>(w)); \ in(name) #define vvv(type, name, h, w, ...) \ vector<vector<vector<type>>> name( \ h, vector<vector<type>>(w, vector<type>(__VA_ARGS__))) template <class T> auto min(const T &a) { return *min_element(all(a)); } template <class T> auto max(const T &a) { return *max_element(all(a)); } inline ll popcnt(ull a) { return __builtin_popcountll(a); } ll gcd(ll a, ll b) { while (b) { ll c = b; b = a % b; a = c; } return a; } ll lcm(ll a, ll b) { if (!a || !b) return 0; return a * b / gcd(a, b); } ll intpow(ll a, ll b) { ll ans = 1; while (b) { if (b & 1) ans *= a; a *= a; b /= 2; } return ans; } ll modpow(ll a, ll b, ll p) { ll ans = 1; while (b) { if (b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; } template <class T, class U> bool chmin(T &a, const U &b) { if (a > b) { a = b; return 1; } return 0; } template <class T, class U> bool chmax(T &a, const U &b) { if (a < b) { a = b; return 1; } return 0; } vector<ll> iota(ll n) { vector<ll> a(n); iota(a.begin(), a.end(), 0); return a; } vector<pll> factor(ull x) { vector<pll> ans; for (ll i = 2; i * i <= x; i++) if (x % i == 0) { ans.push_back({i, 1}); while ((x /= i) % i == 0) ans.back().second++; } if (x != 1) ans.push_back({x, 1}); return ans; } map<ll, ll> factor_map(ull x) { map<ll, ll> ans; for (ll i = 2; i * i <= x; i++) if (x % i == 0) { ans[i] = 1; while ((x /= i) % i == 0) ans[i]++; } if (x != 1) ans[x] = 1; return ans; } vector<uint> divisor(uint x) { vector<uint> ans; for (uint i = 1; i * i <= x; i++) if (x % i == 0) ans.push_back(i); rrep(ans.size() - (ans.back() * ans.back() == x)) ans.push_back(x / ans[i]); return ans; } template <class T> unordered_map<T, ll> press(vector<T> &a) { auto b = a; sort(all(b)); b.erase(unique(all(b)), b.end()); unordered_map<T, ll> ans; rep(b.size()) ans[b[i]] = i; each(i, a) i = ans[i]; return ans; } template <class T> map<T, ll> press_map(vector<T> &a) { auto b = a; sort(all(b)); b.erase(unique(all(b)), b.end()); map<T, ll> ans; rep(b.size()) ans[b[i]] = i; each(i, a) i = ans[i]; return ans; } int scan() { return getchar(); } void scan(int &a) { scanf("%d", &a); } void scan(unsigned &a) { scanf("%u", &a); } void scan(long &a) { scanf("%ld", &a); } void scan(long long &a) { scanf("%lld", &a); } void scan(unsigned long long &a) { scanf("%llu", &a); } void scan(char &a) { do { a = getchar(); } while (a == ' ' || a == '\n'); } void scan(float &a) { scanf("%f", &a); } void scan(double &a) { scanf("%lf", &a); } void scan(long double &a) { scanf("%Lf", &a); } void scan(vector<bool> &a) { for (unsigned i = 0; i < a.size(); i++) { int b; scan(b); a[i] = b; } } void scan(char a[]) { scanf("%s", a); } void scan(string &a) { cin >> a; } template <class T> void scan(vector<T> &); template <class T, size_t size> void scan(array<T, size> &); template <class T, class L> void scan(pair<T, L> &); template <class T, size_t size> void scan(T (&)[size]); template <class T> void scan(vector<T> &a) { for (auto &&i : a) scan(i); } template <class T> void scan(deque<T> &a) { for (auto &&i : a) scan(i); } template <class T, size_t size> void scan(array<T, size> &a) { for (auto &&i : a) scan(i); } template <class T, class L> void scan(pair<T, L> &p) { scan(p.first); scan(p.second); } template <class T, size_t size> void scan(T (&a)[size]) { for (auto &&i : a) scan(i); } template <class T> void scan(T &a) { cin >> a; } void in() {} template <class Head, class... Tail> void in(Head &head, Tail &...tail) { scan(head); in(tail...); } void print() { putchar(' '); } void print(bool a) { printf("%d", a); } void print(int a) { printf("%d", a); } void print(unsigned a) { printf("%u", a); } void print(long a) { printf("%ld", a); } void print(long long a) { printf("%lld", a); } void print(unsigned long long a) { printf("%llu", a); } void print(char a) { printf("%c", a); } void print(char a[]) { printf("%s", a); } void print(const char a[]) { printf("%s", a); } void print(float a) { printf("%.15f", a); } void print(double a) { printf("%.15f", a); } void print(long double a) { printf("%.15Lf", a); } void print(const string &a) { for (auto &&i : a) print(i); } template <class T> void print(const vector<T> &); template <class T, size_t size> void print(const array<T, size> &); template <class T, class L> void print(const pair<T, L> &p); template <class T, size_t size> void print(const T (&)[size]); template <class T> void print(const vector<T> &a) { if (a.empty()) return; print(a[0]); for (auto i = a.begin(); ++i != a.end();) { putchar(' '); print(*i); } } template <class T> void print(const deque<T> &a) { if (a.empty()) return; print(a[0]); for (auto i = a.begin(); ++i != a.end();) { putchar(' '); print(*i); } } template <class T, size_t size> void print(const array<T, size> &a) { print(a[0]); for (auto i = a.begin(); ++i != a.end();) { putchar(' '); print(*i); } } template <class T, class L> void print(const pair<T, L> &p) { print(p.first); putchar(' '); print(p.second); } template <class T, size_t size> void print(const T (&a)[size]) { print(a[0]); for (auto i = a; ++i != end(a);) { putchar(' '); print(*i); } } template <class T> void print(const T &a) { cout << a; } int out() { putchar('\n'); return 0; } template <class T> int out(const T &t) { print(t); putchar('\n'); return 0; } template <class Head, class... Tail> int out(const Head &head, const Tail &...tail) { print(head); putchar(' '); out(tail...); return 0; } #ifdef DEBUG inline ll __lg(ull __n) { return sizeof(ull) * __CHAR_BIT__ - 1 - __builtin_clzll(__n); } #define debug(...) \ { \ print(#__VA_ARGS__); \ print(":"); \ out(__VA_ARGS__); \ } #else #define debug(...) void(0) #endif int first(bool i = true) { return out(i ? "first" : "second"); } int yes(bool i = true) { return out(i ? "yes" : "no"); } int Yes(bool i = true) { return out(i ? "Yes" : "No"); } int No() { return out("No"); } int YES(bool i = true) { return out(i ? "YES" : "NO"); } int NO() { return out("NO"); } int Yay(bool i = true) { return out(i ? "Yay!" : ":("); } int possible(bool i = true) { return out(i ? "possible" : "impossible"); } int Possible(bool i = true) { return out(i ? "Possible" : "Impossible"); } int POSSIBLE(bool i = true) { return out(i ? "POSSIBLE" : "IMPOSSIBLE"); } void Case(ll i) { printf("Case #%lld: ", i); } signed main() { LL(n); vec(ll, a, 13, 1); rep(12) a[i + 1] = a[i] * 26; rep(i, 13) { if (n >= a[i]) n -= a[i]; else { rrep(j, i) { ll k = n / a[j]; print(char('a' + k)); n -= k * a[j]; } return out(); } } }
replace
351
361
351
366
0
p02629
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int a = 26, k = 1; long long N; cin >> N; while (N > a) { N -= a; a *= 26; k++; } N--; for (int i = 0; i < k; i++) { a /= 26; int b = N / a; cout << (char)('a' + b); N %= a; } cout << endl; }
#include <bits/stdc++.h> using namespace std; int main() { long long N, a = 26, k = 1; cin >> N; while (N > a) { N -= a; a *= 26; k++; } N--; for (int i = 0; i < k; i++) { a /= 26; int b = N / a; cout << (char)('a' + b); N %= a; } cout << endl; }
replace
4
6
4
5
0
p02630
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; #define REP(i, n) for (int i = 0; i < (n); i++) #define REPR(i, n) for (int i = n; i >= 0; i--) #define FOR(i, m, n) for (int i = m; i < n; i++) #define ALL(n) begin(n), end(n) #define IN(a, x, b) (a <= x && x < b) #define INIT \ std::ios::sync_with_stdio(false); \ std::cin.tie(0); template <class T> inline T CHMAX(T &a, const T b) { return a = (a < b) ? b : a; } template <class T> inline T CHMIN(T &a, const T b) { return a = (a > b) ? b : a; } const long double EPS = 1e-10; const long long INF = 1e18; const long double PI = acos(-1.0L); int main() { INIT; int n, q; cin >> n; vector<int> m(105, 0); ll total = 0; REP(i, n) { int a; cin >> a; m[a] += 1; total += a; } cin >> q; int b; int c; REP(i, q) { cin >> b >> c; int s = c - b; total = total + (m[b] * s); cout << total << "\n"; m[c] += m[b]; m[b] = 0; } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; #define REP(i, n) for (int i = 0; i < (n); i++) #define REPR(i, n) for (int i = n; i >= 0; i--) #define FOR(i, m, n) for (int i = m; i < n; i++) #define ALL(n) begin(n), end(n) #define IN(a, x, b) (a <= x && x < b) #define INIT \ std::ios::sync_with_stdio(false); \ std::cin.tie(0); template <class T> inline T CHMAX(T &a, const T b) { return a = (a < b) ? b : a; } template <class T> inline T CHMIN(T &a, const T b) { return a = (a > b) ? b : a; } const long double EPS = 1e-10; const long long INF = 1e18; const long double PI = acos(-1.0L); int main() { INIT; int n, q; cin >> n; vector<int> m(100005, 0); ll total = 0; REP(i, n) { int a; cin >> a; m[a] += 1; total += a; } cin >> q; int b; int c; REP(i, q) { cin >> b >> c; int s = c - b; total = total + (m[b] * s); cout << total << "\n"; m[c] += m[b]; m[b] = 0; } cout << endl; return 0; }
replace
28
29
28
29
0
p02630
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> vec(n + 1); vec[0] = 0; vector<int> a(n + 1, 0); int sum = 0; for (int i = 1; i <= n; i++) { cin >> vec[i]; a[vec[i]]++; sum += vec[i]; } int q; cin >> q; for (int i = 1; i <= q; i++) { vector<int> totori(2); cin >> totori[0] >> totori[1]; sum = sum + a[totori[0]] * (totori[1] - totori[0]); a[totori[1]] += a[totori[0]]; a[totori[0]] = 0; cout << sum << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> vec(n + 1); vec[0] = 0; vector<int> a(100001, 0); long long sum = 0; for (int i = 1; i <= n; i++) { cin >> vec[i]; a[vec[i]]++; sum += vec[i]; } int q; cin >> q; for (int i = 1; i <= q; i++) { vector<int> totori(2); cin >> totori[0] >> totori[1]; sum = sum + a[totori[0]] * (totori[1] - totori[0]); a[totori[1]] += a[totori[0]]; a[totori[0]] = 0; cout << sum << endl; } }
replace
8
10
8
10
0
p02630
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> P; int dx[] = {-1, 1, 0, 0}; int dy[] = {0, 0, 1, -1}; int main() { int n; cin >> n; ll a[10005] = {}, s = 0; for (int i = 0; i < n; i++) { ll ai; cin >> ai; a[ai]++; s += ai; } int q; cin >> q; for (int i = 0; i < q; i++) { ll b, c; cin >> b >> c; s -= b * a[b]; s += c * a[b]; a[c] += a[b]; a[b] = 0; cout << s << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> P; int dx[] = {-1, 1, 0, 0}; int dy[] = {0, 0, 1, -1}; int main() { int n; cin >> n; ll a[100005] = {}, s = 0; for (int i = 0; i < n; i++) { ll ai; cin >> ai; a[ai]++; s += ai; } int q; cin >> q; for (int i = 0; i < q; i++) { ll b, c; cin >> b >> c; s -= b * a[b]; s += c * a[b]; a[c] += a[b]; a[b] = 0; cout << s << endl; } return 0; }
replace
11
12
11
12
0
p02630
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } typedef long long ll; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<long long> VL; typedef vector<vector<long long>> VVL; typedef vector<string> VS; typedef pair<int, int> P; typedef tuple<int, int, int> tpl; #define ALL(a) (a).begin(), (a).end() #define SORT(c) sort((c).begin(), (c).end()) #define REVERSE(c) reverse((c).begin(), (c).end()) #define EXIST(m, v) (m).find((v)) != (m).end() #define LB(a, x) lower_bound((a).begin(), (a).end(), x) - (a).begin() #define UB(a, x) upper_bound((a).begin(), (a).end(), x) - (a).begin() #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) #define RFOR(i, a, b) for (int i = (a)-1; i >= (b); --i) #define RREP(i, n) RFOR(i, n, 0) #define en "\n" constexpr double EPS = 1e-9; constexpr double PI = 3.1415926535897932; constexpr int INF = 2147483647; constexpr long long LINF = 1LL << 60; constexpr long long MOD = 1000000007; // 998244353; #define dump(x) cerr << #x << " = " << (x) << endl; #define debug(x) \ cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << 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; } void Main() { int n; cin >> n; VI av(10001, 0); int k; ll ans = 0; REP(i, n) { cin >> k; av[k]++; ans += k; } int q; cin >> q; REP(i, q) { int b, c; cin >> b >> c; ans += (c - b) * av[b]; av[c] += av[b]; av[b] = 0; cout << ans << en; } return; } int main(void) { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); cout << fixed << setprecision(15); int t = 1; // cin>>t; REP(_, t) Main(); return 0; }
#include <bits/stdc++.h> using namespace std; inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } typedef long long ll; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<long long> VL; typedef vector<vector<long long>> VVL; typedef vector<string> VS; typedef pair<int, int> P; typedef tuple<int, int, int> tpl; #define ALL(a) (a).begin(), (a).end() #define SORT(c) sort((c).begin(), (c).end()) #define REVERSE(c) reverse((c).begin(), (c).end()) #define EXIST(m, v) (m).find((v)) != (m).end() #define LB(a, x) lower_bound((a).begin(), (a).end(), x) - (a).begin() #define UB(a, x) upper_bound((a).begin(), (a).end(), x) - (a).begin() #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) #define RFOR(i, a, b) for (int i = (a)-1; i >= (b); --i) #define RREP(i, n) RFOR(i, n, 0) #define en "\n" constexpr double EPS = 1e-9; constexpr double PI = 3.1415926535897932; constexpr int INF = 2147483647; constexpr long long LINF = 1LL << 60; constexpr long long MOD = 1000000007; // 998244353; #define dump(x) cerr << #x << " = " << (x) << endl; #define debug(x) \ cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << 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; } void Main() { int n; cin >> n; VI av(100001, 0); int k; ll ans = 0; REP(i, n) { cin >> k; av[k]++; ans += k; } int q; cin >> q; REP(i, q) { int b, c; cin >> b >> c; ans += (c - b) * av[b]; av[c] += av[b]; av[b] = 0; cout << ans << en; } return; } int main(void) { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); cout << fixed << setprecision(15); int t = 1; // cin>>t; REP(_, t) Main(); return 0; }
replace
67
68
67
68
0
p02630
C++
Runtime Error
#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>; const int MX = 10005; int main() { int n, q, b, c; ll ans; cin >> n; vector<ll> k(MX); rep(i, n) { int a; cin >> a; k[a]++; } ans = 0; rep(i, MX) ans += i * k[i]; cin >> q; rep(i, q) { cin >> b >> c; ans -= b * k[b]; ans -= c * k[c]; k[c] += k[b]; k[b] = 0; ans += c * k[c]; 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>; const int MX = 100005; int main() { int n, q, b, c; ll ans; cin >> n; vector<ll> k(MX); rep(i, n) { int a; cin >> a; k[a]++; } ans = 0; rep(i, MX) ans += i * k[i]; cin >> q; rep(i, q) { cin >> b >> c; ans -= b * k[b]; ans -= c * k[c]; k[c] += k[b]; k[b] = 0; ans += c * k[c]; cout << ans << endl; } }
replace
5
6
5
6
0
p02630
C++
Runtime Error
// include //------------------------------------------ #include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <chrono> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <locale> #include <map> #include <numeric> #include <queue> #include <random> #include <regex> #include <set> #include <sstream> #include <stack> #include <string> #include <type_traits> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; // typedef //------------------------------------------ typedef long long LL; typedef vector<int> VI; typedef vector<bool> VB; typedef vector<char> VC; typedef vector<double> VD; typedef vector<string> VS; typedef vector<LL> VLL; typedef vector<VI> VVI; typedef vector<VB> VVB; typedef vector<VS> VVS; typedef vector<VLL> VVLL; typedef vector<VVI> VVVI; typedef vector<VVLL> VVVLL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; typedef pair<int, string> PIS; typedef pair<string, int> PSI; typedef pair<string, string> PSS; typedef vector<PII> VPII; typedef vector<PLL> VPLL; typedef vector<VPII> VVPII; typedef vector<VPLL> VVPLL; typedef vector<VS> VVS; typedef map<int, int> MII; typedef map<LL, LL> MLL; typedef map<string, int> MSI; typedef map<int, string> MIS; // container util //------------------------------------------ typedef int pInt[4]; #define ALL(a) (a).begin(), (a).end() #define SZ(a) int((a).size()) #define EACH(i, arr) \ for (typeof((arr).begin()) i = (arr).begin(); i != (arr).end(); ++i) #define EXIST(str, e) ((str).find(e) != (str).end()) #define COUNT(arr, v) count((arr).begin(), (arr).end(), v) #define SEARCH(v, w) search((v).begin(), (v).end(), (w).begin(), (w).end()) #define SORT(c) sort((c).begin(), (c).end()) #define RSORT(c) sort((c).rbegin(), (c).rend()) #define REVERSE(c) reverse((c).begin(), (c).end()) #define ROTATE_LEFT(arr, c) \ rotate((arr).begin(), (arr).begin() + (c), (arr).end()) #define ROTATE_RIGHT(arr, c) \ rotate((arr).rbegin(), (arr).rbegin() + (c), (arr).rend()) #define SUMI(arr) accumulate((arr).begin(), (arr).end(), 0) #define SUMD(arr) accumulate((arr).begin(), (arr).end(), 0.) #define SUMLL(arr) accumulate((arr).begin(), (arr).end(), 0LL) #define MULD(arr) \ accumulate((arr).begin(), (arr).end(), 1., multiplies<double>()) #define UB(arr, n) upper_bound((arr).begin(), (arr).end(), n) #define LB(arr, n) lower_bound((arr).begin(), (arr).end(), n) #define PB push_back #define MP make_pair #define ft first #define sd second // input output //------------------------------------------ #define GL(s) getline(cin, (s)) #define INIT() \ std::ios::sync_with_stdio(false); \ std::cin.tie(0) #define OUT(d) std::cout << (d) #define OUT_L(d) std::cout << (d) << endl #define FOUT(n, data) std::cout << std::fixed << std::setprecision(n) << (data) #define FOUT_L(n, data) \ std::cout << std::fixed << std::setprecision(n) << (data) << "\n" #define EL() printf("\n") #define SHOW_VECTOR(v) \ { \ std::cerr << #v << "\t:"; \ for (const auto &xxx : v) { \ std::cerr << xxx << " "; \ } \ std::cerr << "\n"; \ } #define SHOW_MAP(v) \ { \ std::cerr << #v << endl; \ for (const auto &xxx : v) { \ std::cerr << xxx.first << " " << xxx.second << "\n"; \ } \ } #define Yes() printf("Yes\n") #define No() printf("No\n") #define YES() printf("YES\n") #define NO() printf("NO\n") #define Yay() printf("Yay!\n") #define Nnn() printf(":(\n") #define CE(x, y) ((x + y - 1) / (y)) template <typename T1, typename T2> istream &operator>>(istream &in, pair<T1, T2> &p) { in >> p.first >> p.second; return in; } template <typename T> istream &operator>>(istream &in, vector<T> &v) { for (auto &x : v) in >> x; return in; } template <typename T1, typename T2> ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) { out << "[" << p.first << ", " << p.second << "]" << "\n"; return out; } template <class T1, class T2> inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return true; } return false; } template <class T1, class T2> inline bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return true; } return false; } // repetition //------------------------------------------ #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(i, 0, n) #define RREP(i, n) for (int i = n - 1; i >= 0; i--) #define FORLL(i, a, b) for (LL i = LL(a); i < LL(b); ++i) #define RFORLL(i, a, b) for (LL i = LL(b) - 1; i >= LL(a); --i) #define REPLL(i, n) for (LL i = 0; i < LL(n); ++i) #define RREPLL(i, n) for (LL i = LL(n) - 1; i >= 0; --i) #define FOREACH(x, arr) for (auto &(x) : (arr)) // Option + Control + K Terminal // Command + Control + K Run // Command + b EOF //------------------------------------------ //------------------------------------------ constexpr LL MAX = 100010; int main() { LL N; cin >> N; VLL A(N); cin >> A; LL s = SUMLL(A); VLL cnt(N, 0); for (auto x : A) cnt[x]++; int Q; cin >> Q; while (Q--) { LL b, c; cin >> b >> c; LL diff = (c - b); s += diff * cnt[b]; cnt[c] += cnt[b]; cnt[b] = 0; cout << s << endl; } }
// include //------------------------------------------ #include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <chrono> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <locale> #include <map> #include <numeric> #include <queue> #include <random> #include <regex> #include <set> #include <sstream> #include <stack> #include <string> #include <type_traits> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; // typedef //------------------------------------------ typedef long long LL; typedef vector<int> VI; typedef vector<bool> VB; typedef vector<char> VC; typedef vector<double> VD; typedef vector<string> VS; typedef vector<LL> VLL; typedef vector<VI> VVI; typedef vector<VB> VVB; typedef vector<VS> VVS; typedef vector<VLL> VVLL; typedef vector<VVI> VVVI; typedef vector<VVLL> VVVLL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; typedef pair<int, string> PIS; typedef pair<string, int> PSI; typedef pair<string, string> PSS; typedef vector<PII> VPII; typedef vector<PLL> VPLL; typedef vector<VPII> VVPII; typedef vector<VPLL> VVPLL; typedef vector<VS> VVS; typedef map<int, int> MII; typedef map<LL, LL> MLL; typedef map<string, int> MSI; typedef map<int, string> MIS; // container util //------------------------------------------ typedef int pInt[4]; #define ALL(a) (a).begin(), (a).end() #define SZ(a) int((a).size()) #define EACH(i, arr) \ for (typeof((arr).begin()) i = (arr).begin(); i != (arr).end(); ++i) #define EXIST(str, e) ((str).find(e) != (str).end()) #define COUNT(arr, v) count((arr).begin(), (arr).end(), v) #define SEARCH(v, w) search((v).begin(), (v).end(), (w).begin(), (w).end()) #define SORT(c) sort((c).begin(), (c).end()) #define RSORT(c) sort((c).rbegin(), (c).rend()) #define REVERSE(c) reverse((c).begin(), (c).end()) #define ROTATE_LEFT(arr, c) \ rotate((arr).begin(), (arr).begin() + (c), (arr).end()) #define ROTATE_RIGHT(arr, c) \ rotate((arr).rbegin(), (arr).rbegin() + (c), (arr).rend()) #define SUMI(arr) accumulate((arr).begin(), (arr).end(), 0) #define SUMD(arr) accumulate((arr).begin(), (arr).end(), 0.) #define SUMLL(arr) accumulate((arr).begin(), (arr).end(), 0LL) #define MULD(arr) \ accumulate((arr).begin(), (arr).end(), 1., multiplies<double>()) #define UB(arr, n) upper_bound((arr).begin(), (arr).end(), n) #define LB(arr, n) lower_bound((arr).begin(), (arr).end(), n) #define PB push_back #define MP make_pair #define ft first #define sd second // input output //------------------------------------------ #define GL(s) getline(cin, (s)) #define INIT() \ std::ios::sync_with_stdio(false); \ std::cin.tie(0) #define OUT(d) std::cout << (d) #define OUT_L(d) std::cout << (d) << endl #define FOUT(n, data) std::cout << std::fixed << std::setprecision(n) << (data) #define FOUT_L(n, data) \ std::cout << std::fixed << std::setprecision(n) << (data) << "\n" #define EL() printf("\n") #define SHOW_VECTOR(v) \ { \ std::cerr << #v << "\t:"; \ for (const auto &xxx : v) { \ std::cerr << xxx << " "; \ } \ std::cerr << "\n"; \ } #define SHOW_MAP(v) \ { \ std::cerr << #v << endl; \ for (const auto &xxx : v) { \ std::cerr << xxx.first << " " << xxx.second << "\n"; \ } \ } #define Yes() printf("Yes\n") #define No() printf("No\n") #define YES() printf("YES\n") #define NO() printf("NO\n") #define Yay() printf("Yay!\n") #define Nnn() printf(":(\n") #define CE(x, y) ((x + y - 1) / (y)) template <typename T1, typename T2> istream &operator>>(istream &in, pair<T1, T2> &p) { in >> p.first >> p.second; return in; } template <typename T> istream &operator>>(istream &in, vector<T> &v) { for (auto &x : v) in >> x; return in; } template <typename T1, typename T2> ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) { out << "[" << p.first << ", " << p.second << "]" << "\n"; return out; } template <class T1, class T2> inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return true; } return false; } template <class T1, class T2> inline bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return true; } return false; } // repetition //------------------------------------------ #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(i, 0, n) #define RREP(i, n) for (int i = n - 1; i >= 0; i--) #define FORLL(i, a, b) for (LL i = LL(a); i < LL(b); ++i) #define RFORLL(i, a, b) for (LL i = LL(b) - 1; i >= LL(a); --i) #define REPLL(i, n) for (LL i = 0; i < LL(n); ++i) #define RREPLL(i, n) for (LL i = LL(n) - 1; i >= 0; --i) #define FOREACH(x, arr) for (auto &(x) : (arr)) // Option + Control + K Terminal // Command + Control + K Run // Command + b EOF //------------------------------------------ //------------------------------------------ constexpr LL MAX = 100010; int main() { LL N; cin >> N; VLL A(N); cin >> A; LL s = SUMLL(A); VLL cnt(MAX, 0); for (auto x : A) cnt[x]++; int Q; cin >> Q; while (Q--) { LL b, c; cin >> b >> c; LL diff = (c - b); s += diff * cnt[b]; cnt[c] += cnt[b]; cnt[b] = 0; cout << s << endl; } }
replace
197
198
197
198
0
p02630
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef pair<ll, ll> p_ll; typedef vector<pair<ll, ll>> vec_p; // vector<pair<ll, ll>> pairs(n) ,pairs.at(i) = make_pair(i*i, i) #define ture ture #define flase false #define falg flag #define REP(i, x) for (ll i = 0; i < (ll)(x); i++) #define REPS(i, x) for (ll i = 1; i <= (ll)(x); i++) #define RREP(i, x) for (ll i = ((ll)(x)-1); i >= 0; i--) #define RREPS(i, x) for (ll i = ((ll)(x)); i > 0; i--) #define all(x) (x).begin(), (x).end() const ll MOD = pow(10, 9) + 7; const ll LLINF = pow(2, 61) - 1; // llの最大9*10^18 const int INF = pow(2, 30) - 1; ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } vector<ll> p(10000 + 10); int main() { ios::sync_with_stdio(false); cin.tie(0); ll N; string S; cin >> N; vector<ll> A(N); REP(i, N) { cin >> A.at(i); } ll Q; cin >> Q; vector<ll> B(Q), C(Q); REP(i, Q) { cin >> B.at(i) >> C.at(i); } REP(i, N) { p.at(A.at(i))++; } ll ans = 0; REP(i, N) { ans += A.at(i); } REP(i, Q) { ll tmp = p.at(B.at(i)); p.at(C.at(i)) += tmp; ll tmp1 = (C.at(i) - B.at(i)) * tmp; ans += tmp1; cout << ans << endl; p.at(B.at(i)) = 0; } }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef pair<ll, ll> p_ll; typedef vector<pair<ll, ll>> vec_p; // vector<pair<ll, ll>> pairs(n) ,pairs.at(i) = make_pair(i*i, i) #define ture ture #define flase false #define falg flag #define REP(i, x) for (ll i = 0; i < (ll)(x); i++) #define REPS(i, x) for (ll i = 1; i <= (ll)(x); i++) #define RREP(i, x) for (ll i = ((ll)(x)-1); i >= 0; i--) #define RREPS(i, x) for (ll i = ((ll)(x)); i > 0; i--) #define all(x) (x).begin(), (x).end() const ll MOD = pow(10, 9) + 7; const ll LLINF = pow(2, 61) - 1; // llの最大9*10^18 const int INF = pow(2, 30) - 1; ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } vector<ll> p(100000 + 10); int main() { ios::sync_with_stdio(false); cin.tie(0); ll N; string S; cin >> N; vector<ll> A(N); REP(i, N) { cin >> A.at(i); } ll Q; cin >> Q; vector<ll> B(Q), C(Q); REP(i, Q) { cin >> B.at(i) >> C.at(i); } REP(i, N) { p.at(A.at(i))++; } ll ans = 0; REP(i, N) { ans += A.at(i); } REP(i, Q) { ll tmp = p.at(B.at(i)); p.at(C.at(i)) += tmp; ll tmp1 = (C.at(i) - B.at(i)) * tmp; ans += tmp1; cout << ans << endl; p.at(B.at(i)) = 0; } }
replace
24
25
24
25
0
p02630
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <cstdlib> #include <iostream> #include <map> #include <queue> using namespace std; #define N 100010 int has[N]; int long long n; int main() { int n, q, b, c; long long sum, a; while (scanf("%d", &n) != EOF) { sum = 0; for (int i = 0; i <= 100000; i++) has[i] = 0; for (int i = 0; i < n; i++) { scanf("%d", &a); has[a]++; sum += a; } scanf("%d", &q); while (q--) { scanf("%d %d", &b, &c); sum -= b * has[b]; sum += c * has[b]; has[c] += has[b]; has[b] = 0; printf("%lld\n", sum); } } return 0; }
#include <algorithm> #include <cstdio> #include <cstdlib> #include <iostream> #include <map> #include <queue> using namespace std; #define N 100010 int has[N]; int long long n; int main() { int n, q, a, b, c; long long sum; while (scanf("%d", &n) != EOF) { sum = 0; for (int i = 0; i <= 100000; i++) has[i] = 0; for (int i = 0; i < n; i++) { scanf("%d", &a); has[a]++; sum += a; } scanf("%d", &q); while (q--) { scanf("%d %d", &b, &c); sum -= b * has[b]; sum += c * has[b]; has[c] += has[b]; has[b] = 0; printf("%lld\n", sum); } } return 0; }
replace
11
13
11
13
-11
p02630
C++
Time Limit Exceeded
/****************************************************************************** Online C++ Compiler. Code, Compile, Run and Debug C++ program online. Write your code in this editor and press "Run" button to compile and execute it. *******************************************************************************/ #include <bits/stdc++.h> using namespace std; typedef long long ll; const ll mod = 1000000007; #define MAX 50 int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); ll n; cin >> n; ll a[n]; map<ll, ll> m; ll sum = 0; for (ll i = 0; i < n; i++) { cin >> a[i]; m[a[i]]++; sum += a[i]; } ll q; cin >> q; while (q--) { ll c = 0; ll x, y; cin >> x >> y; for (int i = 0; i < n; i++) { if (a[i] == x) a[i] = y; c += a[i]; } cout << c << endl; } return 0; }
/****************************************************************************** Online C++ Compiler. Code, Compile, Run and Debug C++ program online. Write your code in this editor and press "Run" button to compile and execute it. *******************************************************************************/ #include <bits/stdc++.h> using namespace std; typedef long long ll; const ll mod = 1000000007; #define MAX 50 int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); ll n; cin >> n; ll a[n]; map<ll, ll> m; ll sum = 0; for (ll i = 0; i < n; i++) { cin >> a[i]; m[a[i]]++; sum += a[i]; } ll q; cin >> q; while (q--) { ll c = 0; ll x, y; cin >> x >> y; // cout<<m[x]<<" "<<m[y]; sum -= (m[x] * x); sum += (m[x] * y); m[y] += m[x]; m[x] = 0; /*for(int i=0;i<n;i++) { if(a[i]==x) a[i]=y; c+=a[i]; }*/ cout << sum << endl; } return 0; }
replace
35
41
35
50
TLE
p02630
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; typedef pair<ll, ll> P; typedef pair<ll, P> P1; typedef pair<P, P> P2; #define pu push #define pb push_back #define mp make_pair #define eps 1e-7 #define INF 1000000000 #define fi first #define sc second #define rep(i, x) for (ll i = 0; i < x; i++) #define repn(i, x) for (ll i = 1; i <= x; i++) #define SORT(x) sort(x.begin(), x.end()) #define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end()) #define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin()) #define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin()) const int MAX = 510000; const int MOD = 1000000007; int main() { ll N; cin >> N; vector<ll> a(N); rep(i, N) cin >> a[i]; ll Q; cin >> Q; vector<ll> b(N); vector<ll> c(N); rep(i, Q) cin >> b[i] >> c[i]; ll ans = 0; rep(i, N) ans += a[i]; map<ll, ll> m; rep(i, N) m[a[i]]++; rep(i, Q) { if (m[b[i]] > 0) { // 変更あり ans += (c[i] - b[i]) * m[b[i]]; m[c[i]] += m[b[i]]; m[b[i]] = 0; } cout << ans << endl; } }
#include <algorithm> #include <bitset> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; typedef pair<ll, ll> P; typedef pair<ll, P> P1; typedef pair<P, P> P2; #define pu push #define pb push_back #define mp make_pair #define eps 1e-7 #define INF 1000000000 #define fi first #define sc second #define rep(i, x) for (ll i = 0; i < x; i++) #define repn(i, x) for (ll i = 1; i <= x; i++) #define SORT(x) sort(x.begin(), x.end()) #define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end()) #define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin()) #define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin()) const int MAX = 510000; const int MOD = 1000000007; int main() { ll N; cin >> N; vector<ll> a(N); rep(i, N) cin >> a[i]; ll Q; cin >> Q; vector<ll> b(Q); vector<ll> c(Q); rep(i, Q) cin >> b[i] >> c[i]; ll ans = 0; rep(i, N) ans += a[i]; map<ll, ll> m; rep(i, N) m[a[i]]++; rep(i, Q) { if (m[b[i]] > 0) { // 変更あり ans += (c[i] - b[i]) * m[b[i]]; m[c[i]] += m[b[i]]; m[b[i]] = 0; } cout << ans << endl; } }
replace
40
42
40
42
0
p02630
C++
Runtime Error
#include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; vector<long long> a(n + 1, 0); long long sum = 0; for (int i = 0; i < n; ++i) { int t; cin >> t; a[t]++; sum += t; } int q; cin >> q; while (q--) { int b, c; cin >> b >> c; a[c] += a[b]; sum -= a[b] * b; sum += a[b] * c; a[b] = 0; cout << sum << '\n'; } }
#include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; vector<long long> a(100005, 0); long long sum = 0; for (int i = 0; i < n; ++i) { int t; cin >> t; a[t]++; sum += t; } int q; cin >> q; while (q--) { int b, c; cin >> b >> c; a[c] += a[b]; sum -= a[b] * b; sum += a[b] * c; a[b] = 0; cout << sum << '\n'; } }
replace
8
9
8
9
0
p02630
C++
Runtime Error
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/tree_policy.hpp> #define ll long long #define pb push_back #define vi vector<long long> #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/tree_policy.hpp> #include <functional> // for less using namespace __gnu_pbds; using namespace std; typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> set1; ll mod = 1000000007; ll large = 1e18; ll neg = -1e18; ll N = 500000; ll mod1 = 998244353; struct pii { ll x; ll y; }; struct edge { ll from; ll to; ll w; ll id; }; ll find(ll a, ll p[]) { // cout<<"find "<<a<<endl; if (a == p[a]) return a; else return p[a] = find(p[a], p); } void join(ll a, ll b, ll p[], ll sz[]) { a = find(a, p); b = find(b, p); if (a != b) { if (sz[a] < sz[b]) swap(a, b); sz[a] += sz[b]; p[b] = a; } } ll power(ll a, ll b, ll mod) { ll res = 1; while (b > 0) { if (b & 1) res = (res * a) % mod; b = b / 2; a = (a * a) % mod; } return res; } ll inverse(ll x, ll p) { return power(x, p - 2, p); } ll hash1(ll x, ll y) { return x + y + x * y; } /*ll f[200000]={0}; void ini() { f[0]=1; for(ll i=1;i<=200000;i++) f[i]=(i*f[i-1])%mod; }*/ void build(ll tree[], ll v, ll l, ll r, ll dp[]) { if (l == r) tree[v] = dp[l]; else { ll m = (l + r) / 2; build(tree, 2 * v, l, m, dp); build(tree, 2 * v + 1, m + 1, r, dp); tree[v] = max(tree[2 * v], tree[2 * v + 1]); } } ll query(ll tree[], ll v, ll l, ll r, ll tl, ll tr) { if (l == tl && tr == r) { // cout<<"max of "<<tl<<" "<<tr<<" "<<tree[v]<<endl; return tree[v]; } else { ll m = (l + r) / 2; if (tr <= m) { ll z1 = query(tree, 2 * v, l, m, tl, tr); // cout<<"max of "<<tl<<" "<<tr<<" "<<z1<<endl; return z1; } else if (tl > m) { ll z2 = query(tree, 2 * v + 1, m + 1, r, tl, tr); // cout<<"max of "<<tl<<" "<<tr<<" "<<z2<<endl; return z2; } else { ll z = query(tree, 2 * v, l, m, tl, m); ll z1 = query(tree, 2 * v + 1, m + 1, r, m + 1, tr); // cout<<"max of "<<tl<<" "<<tr<<" "<<max(z1,z)<<endl; return max(z1, z); } } } void update(ll tree[], ll v, ll l, ll r, ll pos, ll element) { if (l == r && l == pos) tree[v] = element; else { ll m = (l + r) / 2; if (pos <= m) update(tree, 2 * v, l, m, pos, element); else update(tree, 2 * v + 1, m + 1, r, pos, element); tree[v] = max(tree[2 * v], tree[2 * v + 1]); } } /*ll ncr(ll n,ll r) { ll res=1; ll k=f[n]; ll v=inverse(f[n-r],mod); ll u=inverse(f[r],mod); ll x=(u*v)%mod; k=(k*x)%mod; return k; }*/ ll max_n = 1e6 + 100; ll hash2(ll x, ll y, ll W) { return (x - 1) * W + y; } pii rev(ll u, ll W) { pii ans; if (u % W == 0) { ans.y = W; ans.x = (u - W) / W + 1; } return ans; } ll MAX_N = 200000; string tobin(ll a, ll sz) { string s; ll r = 0; while (a > 0) { if (a % 2 == 0) s.pb('0'); else s.pb('1'); a /= 2; r++; } while (r < sz) { s.pb('0'); r++; } reverse(s.begin(), s.end()); return s; } ll tolong(string s) { ll sz = s.size(); ll ans = 0; for (ll i = 0; i < sz; i++) { if (s.at(i) == '1') ans += (ll)pow(2, sz - i - 1); } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll n; cin >> n; ll f[10001] = {0}; ll sum = 0; for (ll i = 0; i < n; i++) { ll x; cin >> x; f[x]++; sum += x; } ll q; cin >> q; for (ll i = 0; i < q; i++) { ll b, c; cin >> b >> c; ll r = f[b]; f[b] = 0; f[c] += r; sum = sum + (c - b) * r; cout << sum << endl; } return 0; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/tree_policy.hpp> #define ll long long #define pb push_back #define vi vector<long long> #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/tree_policy.hpp> #include <functional> // for less using namespace __gnu_pbds; using namespace std; typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> set1; ll mod = 1000000007; ll large = 1e18; ll neg = -1e18; ll N = 500000; ll mod1 = 998244353; struct pii { ll x; ll y; }; struct edge { ll from; ll to; ll w; ll id; }; ll find(ll a, ll p[]) { // cout<<"find "<<a<<endl; if (a == p[a]) return a; else return p[a] = find(p[a], p); } void join(ll a, ll b, ll p[], ll sz[]) { a = find(a, p); b = find(b, p); if (a != b) { if (sz[a] < sz[b]) swap(a, b); sz[a] += sz[b]; p[b] = a; } } ll power(ll a, ll b, ll mod) { ll res = 1; while (b > 0) { if (b & 1) res = (res * a) % mod; b = b / 2; a = (a * a) % mod; } return res; } ll inverse(ll x, ll p) { return power(x, p - 2, p); } ll hash1(ll x, ll y) { return x + y + x * y; } /*ll f[200000]={0}; void ini() { f[0]=1; for(ll i=1;i<=200000;i++) f[i]=(i*f[i-1])%mod; }*/ void build(ll tree[], ll v, ll l, ll r, ll dp[]) { if (l == r) tree[v] = dp[l]; else { ll m = (l + r) / 2; build(tree, 2 * v, l, m, dp); build(tree, 2 * v + 1, m + 1, r, dp); tree[v] = max(tree[2 * v], tree[2 * v + 1]); } } ll query(ll tree[], ll v, ll l, ll r, ll tl, ll tr) { if (l == tl && tr == r) { // cout<<"max of "<<tl<<" "<<tr<<" "<<tree[v]<<endl; return tree[v]; } else { ll m = (l + r) / 2; if (tr <= m) { ll z1 = query(tree, 2 * v, l, m, tl, tr); // cout<<"max of "<<tl<<" "<<tr<<" "<<z1<<endl; return z1; } else if (tl > m) { ll z2 = query(tree, 2 * v + 1, m + 1, r, tl, tr); // cout<<"max of "<<tl<<" "<<tr<<" "<<z2<<endl; return z2; } else { ll z = query(tree, 2 * v, l, m, tl, m); ll z1 = query(tree, 2 * v + 1, m + 1, r, m + 1, tr); // cout<<"max of "<<tl<<" "<<tr<<" "<<max(z1,z)<<endl; return max(z1, z); } } } void update(ll tree[], ll v, ll l, ll r, ll pos, ll element) { if (l == r && l == pos) tree[v] = element; else { ll m = (l + r) / 2; if (pos <= m) update(tree, 2 * v, l, m, pos, element); else update(tree, 2 * v + 1, m + 1, r, pos, element); tree[v] = max(tree[2 * v], tree[2 * v + 1]); } } /*ll ncr(ll n,ll r) { ll res=1; ll k=f[n]; ll v=inverse(f[n-r],mod); ll u=inverse(f[r],mod); ll x=(u*v)%mod; k=(k*x)%mod; return k; }*/ ll max_n = 1e6 + 100; ll hash2(ll x, ll y, ll W) { return (x - 1) * W + y; } pii rev(ll u, ll W) { pii ans; if (u % W == 0) { ans.y = W; ans.x = (u - W) / W + 1; } return ans; } ll MAX_N = 200000; string tobin(ll a, ll sz) { string s; ll r = 0; while (a > 0) { if (a % 2 == 0) s.pb('0'); else s.pb('1'); a /= 2; r++; } while (r < sz) { s.pb('0'); r++; } reverse(s.begin(), s.end()); return s; } ll tolong(string s) { ll sz = s.size(); ll ans = 0; for (ll i = 0; i < sz; i++) { if (s.at(i) == '1') ans += (ll)pow(2, sz - i - 1); } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll n; cin >> n; ll f[100001] = {0}; ll sum = 0; for (ll i = 0; i < n; i++) { ll x; cin >> x; f[x]++; sum += x; } ll q; cin >> q; for (ll i = 0; i < q; i++) { ll b, c; cin >> b >> c; ll r = f[b]; f[b] = 0; f[c] += r; sum = sum + (c - b) * r; cout << sum << endl; } return 0; }
replace
165
166
165
166
0
p02630
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; const double pi = 3.141592653589793; typedef unsigned long long ull; typedef long double ldouble; const ll INF = 1e18; int main() { ull n, sum = 0, q; cin >> n; vector<ull> a(100000); fill(a.begin(), a.end(), 0); for (int i = 0; i < n; i++) { int y; cin >> y; sum += y; a.at(y)++; } cin >> q; for (int i = 0; i < q; i++) { int b, c; cin >> b >> c; ull count = a.at(b); a.at(b) = 0; sum += (c - b) * count; a.at(c) += count; cout << sum << endl; } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const double pi = 3.141592653589793; typedef unsigned long long ull; typedef long double ldouble; const ll INF = 1e18; int main() { ull n, sum = 0, q; cin >> n; vector<ull> a(100001); fill(a.begin(), a.end(), 0); for (int i = 0; i < n; i++) { int y; cin >> y; sum += y; a.at(y)++; } cin >> q; for (int i = 0; i < q; i++) { int b, c; cin >> b >> c; ull count = a.at(b); a.at(b) = 0; sum += (c - b) * count; a.at(c) += count; cout << sum << endl; } }
replace
11
12
11
12
0
p02630
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long int ll; int seive[10000]; int main() { int t; cin >> t; ll sum = 0; for (int i = 0; i < t; i++) { ll k; cin >> k; sum += k; seive[k]++; } int k; cin >> k; while (k--) { ll a, b; cin >> a >> b; sum += ((ll)seive[a] * (b - a)); seive[b] += seive[a]; seive[a] = 0; cout << sum << '\n'; } }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; ll seive[(int)2e5]; int main() { int t; cin >> t; ll sum = 0; for (int i = 0; i < t; i++) { ll k; cin >> k; sum += k; seive[k]++; } int k; cin >> k; while (k--) { ll a, b; cin >> a >> b; sum += ((ll)seive[a] * (b - a)); seive[b] += seive[a]; seive[a] = 0; cout << sum << '\n'; } }
replace
3
4
3
4
0
p02630
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { ll n, q; cin >> n; ll ans = 0; vector<ll> cnt(11000, 0); for (ll i = 0; i < n; i++) { ll tmp; cin >> tmp; ans += tmp; cnt[tmp]++; } cin >> q; for (ll i = 0; i < q; i++) { ll x, y; cin >> x >> y; ans += (y - x) * cnt[x]; cnt[y] += cnt[x]; cnt[x] = 0; cout << ans << endl; } }
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { ll n, q; cin >> n; ll ans = 0; vector<ll> cnt(110000, 0); for (ll i = 0; i < n; i++) { ll tmp; cin >> tmp; ans += tmp; cnt[tmp]++; } cin >> q; for (ll i = 0; i < q; i++) { ll x, y; cin >> x >> y; ans += (y - x) * cnt[x]; cnt[y] += cnt[x]; cnt[x] = 0; cout << ans << endl; } }
replace
7
8
7
8
0
p02630
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int> backet(N, 0); long long sum = 0LL; for (int i = 0; i < N; i++) { int A; cin >> A; sum += A; backet.at(A - 1)++; } int Q; cin >> Q; for (int i = 0; i < Q; i++) { int B, C; cin >> B >> C; sum += backet.at(B - 1) * (C - B); backet.at(C - 1) += backet.at(B - 1); backet.at(B - 1) = 0; cout << sum << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int> backet(100000, 0); long long sum = 0LL; for (int i = 0; i < N; i++) { int A; cin >> A; sum += A; backet.at(A - 1)++; } int Q; cin >> Q; for (int i = 0; i < Q; i++) { int B, C; cin >> B >> C; sum += backet.at(B - 1) * (C - B); backet.at(C - 1) += backet.at(B - 1); backet.at(B - 1) = 0; cout << sum << endl; } }
replace
6
7
6
7
0
p02630
C++
Runtime Error
#include <algorithm> #include <cmath> #include <iostream> #include <queue> #include <string> #include <vector> #define IOS \ ios::sync_with_stdio(false); \ cin.tie(0) using namespace std; int main() { int n; int i; long long sum = 0; cin >> n; vector<long long> cnt(n + 1); for (i = 0; i < n; i++) { int tmp; cin >> tmp; cnt[tmp]++; sum += tmp; } int q; cin >> q; for (i = 0; i < q; i++) { long long t, f; cin >> t >> f; sum -= t * cnt[t]; sum += f * cnt[t]; cnt[f] += cnt[t]; cnt[t] = 0; cout << sum << "\n"; } }
#include <algorithm> #include <cmath> #include <iostream> #include <queue> #include <string> #include <vector> #define IOS \ ios::sync_with_stdio(false); \ cin.tie(0) using namespace std; int main() { int n; int i; long long sum = 0; cin >> n; vector<long long> cnt(100002); for (i = 0; i < n; i++) { int tmp; cin >> tmp; cnt[tmp]++; sum += tmp; } int q; cin >> q; for (i = 0; i < q; i++) { long long t, f; cin >> t >> f; sum -= t * cnt[t]; sum += f * cnt[t]; cnt[f] += cnt[t]; cnt[t] = 0; cout << sum << "\n"; } }
replace
18
19
18
19
0
p02630
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; map<int, int> mp; long long sum = 0; vector<int> a(10001, 0); for (int i = 0; i < n; i++) { int b; cin >> b; a[b]++; sum += b; } int q; cin >> q; vector<long long> ans; for (int i = 0; i < q; i++) { int b, c; cin >> b >> c; sum -= a[b] * b; sum += a[b] * c; a[c] += a[b]; a[b] = 0; ans.push_back(sum); } for (int i = 0; i < ans.size(); i++) { cout << ans[i] << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; map<int, int> mp; long long sum = 0; vector<long long> a(100001, 0); for (int i = 0; i < n; i++) { int b; cin >> b; a[b]++; sum += b; } int q; cin >> q; vector<long long> ans; for (int i = 0; i < q; i++) { int b, c; cin >> b >> c; sum -= a[b] * b; sum += a[b] * c; a[c] += a[b]; a[b] = 0; ans.push_back(sum); } for (int i = 0; i < ans.size(); i++) { cout << ans[i] << endl; } return 0; }
replace
8
9
8
9
0
p02630
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using i64 = long long; i64 table[10001]; int main() { i64 n, q, sum{}; cin >> n; for (i64 i = 0; i < n; i++) { i64 a; cin >> a; sum += a; table[a]++; } cin >> q; i64 ans[q]; for (i64 i = 0; i < q; i++) { i64 b, c; cin >> b >> c; sum += (c - b) * table[b]; table[c] += table[b]; table[b] = 0; ans[i] = sum; } for (i64 i = 0; i < q; i++) { cout << ans[i] << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; using i64 = long long; i64 table[100001]; int main() { i64 n, q, sum{}; cin >> n; for (i64 i = 0; i < n; i++) { i64 a; cin >> a; sum += a; table[a]++; } cin >> q; i64 ans[q]; for (i64 i = 0; i < q; i++) { i64 b, c; cin >> b >> c; sum += (c - b) * table[b]; table[c] += table[b]; table[b] = 0; ans[i] = sum; } for (i64 i = 0; i < q; i++) { cout << ans[i] << endl; } return 0; }
replace
5
6
5
6
0
p02630
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int(i) = 0; (i) < (n); ++(i)) #define all(x) (x).begin(), (x).end() #define dump(x) cout << #x << " = " << (x) << endl using namespace std; using ll = long long; int main() { const int MAX = 1000; vector<int> cnt(MAX, 0); int n; cin >> n; vector<int> a(n); rep(i, n) { cin >> a[i]; cnt[a[i]]++; } sort(all(a)); ll s = accumulate(all(a), 0ll); int q; cin >> q; vector<pair<int, int>> bc; rep(i, q) { int b, c; cin >> b >> c; bc.push_back({b, c}); } rep(i, q) { int before = bc[i].first; int after = bc[i].second; int sa = after - before; int c = cnt[before]; s += (c * sa); cout << s << endl; cnt[before] = 0; cnt[after] += c; } }
#include <bits/stdc++.h> #define rep(i, n) for (int(i) = 0; (i) < (n); ++(i)) #define all(x) (x).begin(), (x).end() #define dump(x) cout << #x << " = " << (x) << endl using namespace std; using ll = long long; int main() { const int MAX = 100100; vector<int> cnt(MAX, 0); int n; cin >> n; vector<int> a(n); rep(i, n) { cin >> a[i]; cnt[a[i]]++; } sort(all(a)); ll s = accumulate(all(a), 0ll); int q; cin >> q; vector<pair<int, int>> bc; rep(i, q) { int b, c; cin >> b >> c; bc.push_back({b, c}); } rep(i, q) { int before = bc[i].first; int after = bc[i].second; int sa = after - before; int c = cnt[before]; s += (c * sa); cout << s << endl; cnt[before] = 0; cnt[after] += c; } }
replace
9
10
9
10
0
p02630
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int> A(10010, 0); int long long sum = 0; for (int i = 0; i < N; i++) { int a; cin >> a; A[a] += 1; sum += a; } int Q; cin >> Q; for (int i = 0; i < Q; i++) { int b, c; cin >> b >> c; sum += c * A[b] - b * A[b]; A[c] += A[b]; A[b] = 0; cout << sum << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int> A(100010, 0); int long long sum = 0; for (int i = 0; i < N; i++) { int a; cin >> a; A[a] += 1; sum += a; } int Q; cin >> Q; for (int i = 0; i < Q; i++) { int b, c; cin >> b >> c; sum += c * A[b] - b * A[b]; A[c] += A[b]; A[b] = 0; cout << sum << endl; } }
replace
6
7
6
7
0
p02630
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define MOD 998244353 #define ff first #define ss second typedef long long ll; ll power(ll a, ll b) { // a^b ll res = 1; a = a % MOD; while (b > 0) { if (b & 1) { res = (res * a) % MOD; b--; } a = (a * a) % MOD; b >>= 1; } return res; } ll fermat_inv(ll y) { return power(y, MOD - 2); } ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll t = 1; // cin>>t; while (t--) { int n; cin >> n; vector<ll> ctr(n + 1, 0); ll val = 0ll; for (int i = 1; i <= n; i++) { ll a; cin >> a; ctr[a]++; val += a; } ll q; cin >> q; while (q--) { ll b, c; cin >> b >> c; val += (c - b) * ctr[b]; ctr[c] += ctr[b]; ctr[b] = 0; cout << val << "\n"; } } return 0; }
#include <bits/stdc++.h> using namespace std; #define MOD 998244353 #define ff first #define ss second typedef long long ll; ll power(ll a, ll b) { // a^b ll res = 1; a = a % MOD; while (b > 0) { if (b & 1) { res = (res * a) % MOD; b--; } a = (a * a) % MOD; b >>= 1; } return res; } ll fermat_inv(ll y) { return power(y, MOD - 2); } ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll t = 1; // cin>>t; while (t--) { int n; cin >> n; vector<ll> ctr(100001, 0); ll val = 0ll; for (int i = 1; i <= n; i++) { ll a; cin >> a; ctr[a]++; val += a; } ll q; cin >> q; while (q--) { ll b, c; cin >> b >> c; val += (c - b) * ctr[b]; ctr[c] += ctr[b]; ctr[b] = 0; cout << val << "\n"; } } return 0; }
replace
29
30
29
30
0
p02630
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define ll long long #define loop(var, start, stop, step) \ for (long long var = start; var < stop; var += step) #define vi vector<int> #define vl vector<long long> #define fio \ cin.tie(nullptr); \ ios::sync_with_stdio(false); \ cout.tie(nullptr); int main() { fio; ll n; cin >> n; vi v(n); ll sum = 0; map<ll, ll> m; loop(i, 0, n, 1) { cin >> v[i]; sum += v[i]; m[v[i]]++; } ll q; cin >> q; loop(i, 0, q, 1) { ll old, ne; cin >> old >> ne; replace(v.begin(), v.end(), old, ne); sum -= m[old] * old; sum += ne * m[old]; cout << sum << endl; m[ne] += m[old]; m.erase(old); } return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define loop(var, start, stop, step) \ for (long long var = start; var < stop; var += step) #define vi vector<int> #define vl vector<long long> #define fio \ cin.tie(nullptr); \ ios::sync_with_stdio(false); \ cout.tie(nullptr); int main() { fio; ll n; cin >> n; vi v(n); ll sum = 0; map<ll, ll> m; loop(i, 0, n, 1) { cin >> v[i]; sum += v[i]; m[v[i]]++; } ll q; cin >> q; loop(i, 0, q, 1) { ll old, ne; cin >> old >> ne; sum -= m[old] * old; sum += ne * m[old]; cout << sum << endl; m[ne] += m[old]; m.erase(old); } return 0; }
delete
29
30
29
29
TLE
p02630
C++
Runtime Error
#include <bits/stdc++.h> #define fastio \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define fi first #define se second #define pb push_back #define mk make_pair using namespace std; typedef long long ll; typedef long double lld; const ll INF = 1e18; const ll MOD = 1e9 + 7; ll fastpowMOD(ll a, ll p, ll MOD); ll fastpowMOD(ll a, ll p, ll MOD) { if (p == 0) return 1; a %= MOD; ll z = fastpowMOD(a, p / 2, MOD); z = (z * z) % MOD; if (p % 2) z = (z * a) % MOD; return z; } ll lcm(ll a, ll b) { return (a * b) / __gcd(a, b); } void solve() { ll n, q; cin >> n; vector<ll> a(q, 0); map<ll, ll> mp; ll s = 0; for (ll i = 0; i < n; i++) { cin >> a[i]; s += a[i]; mp[a[i]]++; } cin >> q; for (ll i = 0; i < q; i++) { ll x1, x2; cin >> x1 >> x2; s -= x1 * mp[x1]; s += x2 * mp[x1]; mp[x2] += mp[x1]; mp[x1] = 0; cout << s << "\n"; } } int main() { fastio; int t; // cin>>t; t = 1; for (int i = 1; i <= t; i++) solve(); }
#include <bits/stdc++.h> #define fastio \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define fi first #define se second #define pb push_back #define mk make_pair using namespace std; typedef long long ll; typedef long double lld; const ll INF = 1e18; const ll MOD = 1e9 + 7; ll fastpowMOD(ll a, ll p, ll MOD); ll fastpowMOD(ll a, ll p, ll MOD) { if (p == 0) return 1; a %= MOD; ll z = fastpowMOD(a, p / 2, MOD); z = (z * z) % MOD; if (p % 2) z = (z * a) % MOD; return z; } ll lcm(ll a, ll b) { return (a * b) / __gcd(a, b); } void solve() { ll n, q; cin >> n; vector<ll> a(n, 0); map<ll, ll> mp; ll s = 0; for (ll i = 0; i < n; i++) { cin >> a[i]; s += a[i]; mp[a[i]]++; } cin >> q; for (ll i = 0; i < q; i++) { ll x1, x2; cin >> x1 >> x2; s -= x1 * mp[x1]; s += x2 * mp[x1]; mp[x2] += mp[x1]; mp[x1] = 0; cout << s << "\n"; } } int main() { fastio; int t; // cin>>t; t = 1; for (int i = 1; i <= t; i++) solve(); }
replace
32
33
32
33
-6
terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc
p02630
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() { ll n; cin >> n; vector<ll> a(n); vector<ll> d(10001); ll sum = 0; REP(i, n) { cin >> a[i]; sum += a[i]; d[a[i]]++; } ll q; cin >> q; ll b, c; REP(i, q) { cin >> b >> c; sum = sum - b * d[b] + c * d[b]; d[c] += d[b]; d[b] = 0; cout << sum << endl; } }
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < n; i++) using namespace std; typedef long long ll; int main() { ll n; cin >> n; vector<ll> a(n); vector<ll> d(100001); ll sum = 0; REP(i, n) { cin >> a[i]; sum += a[i]; d[a[i]]++; } ll q; cin >> q; ll b, c; REP(i, q) { cin >> b >> c; sum = sum - b * d[b] + c * d[b]; d[c] += d[b]; d[b] = 0; cout << sum << endl; } }
replace
11
12
11
12
0
p02630
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) #define all(v) v.begin(), v.end() using namespace std; using ll = long long; using p = pair<int, int>; int main() { int N; cin >> N; vector<int> A(100000); ll sum = 0; rep(i, N) { int tmp; cin >> tmp; A.at(tmp)++; sum += tmp; } int Q; cin >> Q; vector<vector<int>> bc(Q, vector<int>(2)); rep(i, Q) { cin >> bc.at(i).at(0) >> bc.at(i).at(1); } rep(i, Q) { sum += (bc.at(i).at(1) - bc.at(i).at(0)) * A.at(bc.at(i).at(0)); cout << sum << endl; A.at(bc.at(i).at(1)) += A.at(bc.at(i).at(0)); A.at(bc.at(i).at(0)) = 0; } }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) #define all(v) v.begin(), v.end() using namespace std; using ll = long long; using p = pair<int, int>; int main() { int N; cin >> N; vector<int> A(1000000); ll sum = 0; rep(i, N) { int tmp; cin >> tmp; A.at(tmp)++; sum += tmp; } int Q; cin >> Q; vector<vector<int>> bc(Q, vector<int>(2)); rep(i, Q) { cin >> bc.at(i).at(0) >> bc.at(i).at(1); } rep(i, Q) { sum += (bc.at(i).at(1) - bc.at(i).at(0)) * A.at(bc.at(i).at(0)); cout << sum << endl; A.at(bc.at(i).at(1)) += A.at(bc.at(i).at(0)); A.at(bc.at(i).at(0)) = 0; } }
replace
11
12
11
12
0
p02630
C++
Runtime Error
#include <algorithm> #include <iostream> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < n; ++i) typedef long long ll; int main() { int N, Q, a; cin >> N; vector<int> A(N), count(N); rep(i, N) { cin >> a; A[i] = a; ++count[a]; } cin >> Q; vector<ll> B(Q), C(Q); rep(i, Q) cin >> B[i] >> C[i]; ll ans = 0; rep(i, N) { ans += A[i]; } rep(i, Q) { ans -= count[B[i]] * B[i]; ans -= count[C[i]] * C[i]; count[C[i]] += count[B[i]]; ans += count[C[i]] * C[i]; count[B[i]] = 0; cout << ans << endl; } }
#include <algorithm> #include <iostream> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < n; ++i) typedef long long ll; int main() { int N, Q, a; cin >> N; vector<int> A(N), count(100001); rep(i, N) { cin >> a; A[i] = a; ++count[a]; } cin >> Q; vector<ll> B(Q), C(Q); rep(i, Q) cin >> B[i] >> C[i]; ll ans = 0; rep(i, N) { ans += A[i]; } rep(i, Q) { ans -= count[B[i]] * B[i]; ans -= count[C[i]] * C[i]; count[C[i]] += count[B[i]]; ans += count[C[i]] * C[i]; count[B[i]] = 0; cout << ans << endl; } }
replace
11
12
11
12
0
p02630
C++
Runtime Error
#pragma target("avx") #pragma optimize("O3") #include <bits/stdc++.h> using ll = int_fast64_t; using P = std::pair<ll, ll>; using PP = std::pair<ll, P>; using V = std::vector<ll>; template <typename T> using pq = std::priority_queue<T>; template <typename T> using rpq = std::priority_queue<T, std::vector<T>, std::greater<T>>; #define REP(i, b, e) for (ll i = b; i < e; i++) #define ALL(vec) vec.begin(), vec.end() #define PRINT(vec) \ printf("[ "); \ for (auto &i : vec) \ printf("%ld ", i); \ puts("]"); #define SCAN(vec) \ for (auto &i : vec) \ scanf("%ld", &i) #define fi first #define se second const int MOD = 1e9 + 7; const ll INF = 1e18; int dx[] = {0, 1, 0, -1, 1, 1, -1, -1}, dy[] = {1, 0, -1, 0, 1, -1, -1, 1}; int main() { int n; scanf("%d", &n); ll cnt[10010] = {}, ans = 0; REP(i, 0, n) { int a; scanf("%d", &a); cnt[a]++; ans += a; } int q; scanf("%d", &q); while (q--) { int b, c; scanf("%d %d", &b, &c); ans += cnt[b] * (c - b); printf("%ld\n", ans); cnt[c] += cnt[b]; cnt[b] = 0; } return 0; }
#pragma target("avx") #pragma optimize("O3") #include <bits/stdc++.h> using ll = int_fast64_t; using P = std::pair<ll, ll>; using PP = std::pair<ll, P>; using V = std::vector<ll>; template <typename T> using pq = std::priority_queue<T>; template <typename T> using rpq = std::priority_queue<T, std::vector<T>, std::greater<T>>; #define REP(i, b, e) for (ll i = b; i < e; i++) #define ALL(vec) vec.begin(), vec.end() #define PRINT(vec) \ printf("[ "); \ for (auto &i : vec) \ printf("%ld ", i); \ puts("]"); #define SCAN(vec) \ for (auto &i : vec) \ scanf("%ld", &i) #define fi first #define se second const int MOD = 1e9 + 7; const ll INF = 1e18; int dx[] = {0, 1, 0, -1, 1, 1, -1, -1}, dy[] = {1, 0, -1, 0, 1, -1, -1, 1}; int main() { int n; scanf("%d", &n); ll cnt[100010] = {}, ans = 0; REP(i, 0, n) { int a; scanf("%d", &a); cnt[a]++; ans += a; } int q; scanf("%d", &q); while (q--) { int b, c; scanf("%d %d", &b, &c); ans += cnt[b] * (c - b); printf("%ld\n", ans); cnt[c] += cnt[b]; cnt[b] = 0; } return 0; }
replace
31
32
31
32
0
p02630
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { long long i, j, n, k = 0, x, y, q; cin >> n; long long a[n]; unordered_map<long long, long long> mp; for (i = 0; i < n; i++) { cin >> a[i]; mp[a[i]]++; k += a[i]; } cin >> q; while (q--) { cin >> x >> y; if (mp.find(x) != mp.end()) { j = mp[x]; while (mp[x]--) mp[y]++; k -= x * j; k += y * j; } cout << k << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { long long i, j, n, k = 0, x, y, q; cin >> n; long long a[n]; unordered_map<long long, long long> mp; for (i = 0; i < n; i++) { cin >> a[i]; mp[a[i]]++; k += a[i]; } cin >> q; while (q--) { cin >> x >> y; if (mp.find(x) != mp.end()) { j = mp[x]; mp.erase(x); mp[y] += j; k -= x * j; k += y * j; } cout << k << endl; } }
replace
18
20
18
20
TLE
p02630
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int> A(N); vector<int> D(100000); for (int i = 0; i < N; i++) { cin >> A.at(i); D.at(A.at(i))++; } long S = 0; for (int i = 0; i < N; i++) { S = S + A.at(i); } int Q; cin >> Q; // int sw=0; vector<int> B(Q); vector<int> C(Q); for (int i = 0; i < Q; i++) { cin >> B.at(i) >> C.at(i); S = S + (C.at(i) - B.at(i)) * D.at(B.at(i)); D.at(C.at(i)) = D.at(C.at(i)) + D.at(B.at(i)); D.at(B.at(i)) = 0; cout << S << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int> A(N); vector<int> D(200000); for (int i = 0; i < N; i++) { cin >> A.at(i); D.at(A.at(i))++; } long S = 0; for (int i = 0; i < N; i++) { S = S + A.at(i); } int Q; cin >> Q; // int sw=0; vector<int> B(Q); vector<int> C(Q); for (int i = 0; i < Q; i++) { cin >> B.at(i) >> C.at(i); S = S + (C.at(i) - B.at(i)) * D.at(B.at(i)); D.at(C.at(i)) = D.at(C.at(i)) + D.at(B.at(i)); D.at(B.at(i)) = 0; cout << S << endl; } }
replace
8
9
8
9
0
p02630
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <string> #include <tuple> #include <utility> #include <vector> using namespace std; using ll = long long; const int INF = 1000000009; const ll LINF = 1e18; int main() { int n; cin >> n; vector<int> a_kind(n + 1, 0); ll sum = 0; for (int i = 0; i < n; ++i) { int inp; cin >> inp; ++a_kind[inp]; sum += inp; } int q; cin >> q; vector<ll> ans(q); for (int i = 0; i < q; ++i) { int b, c; cin >> b >> c; sum += (ll)(-1 * (ll)a_kind[b] * (ll)b) + (ll)((ll)a_kind[b] * (ll)c); a_kind[c] += a_kind[b]; a_kind[b] = 0; ans[i] = sum; } for (int i = 0; i < q; ++i) cout << ans[i] << endl; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <string> #include <tuple> #include <utility> #include <vector> using namespace std; using ll = long long; const int INF = 1000000009; const ll LINF = 1e18; int main() { int n; cin >> n; vector<int> a_kind(100001, 0); ll sum = 0; for (int i = 0; i < n; ++i) { int inp; cin >> inp; ++a_kind[inp]; sum += inp; } int q; cin >> q; vector<ll> ans(q); for (int i = 0; i < q; ++i) { int b, c; cin >> b >> c; sum += (ll)(-1 * (ll)a_kind[b] * (ll)b) + (ll)((ll)a_kind[b] * (ll)c); a_kind[c] += a_kind[b]; a_kind[b] = 0; ans[i] = sum; } for (int i = 0; i < q; ++i) cout << ans[i] << endl; }
replace
22
23
22
23
0
p02630
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<ll, ll> l_l; typedef pair<int, int> i_i; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define EPS (1e-7) #define INF (1e9) #define PI (acos(-1)) const ll mod = 1000000007; int main() { int n; cin >> n; vector<int> cntMemo(n + 1, 0); ll sum = 0; rep(i, n) { int a; cin >> a; cntMemo[a]++; sum += (ll)a; } int q; cin >> q; rep(i, q) { int b, c; cin >> b >> c; sum += (ll)cntMemo[b] * (c - b); cntMemo[c] += cntMemo[b]; cntMemo[b] = 0; cout << sum << endl; } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<ll, ll> l_l; typedef pair<int, int> i_i; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define EPS (1e-7) #define INF (1e9) #define PI (acos(-1)) const ll mod = 1000000007; int main() { int n; cin >> n; vector<int> cntMemo(100010, 0); ll sum = 0; rep(i, n) { int a; cin >> a; cntMemo[a]++; sum += (ll)a; } int q; cin >> q; rep(i, q) { int b, c; cin >> b >> c; sum += (ll)cntMemo[b] * (c - b); cntMemo[c] += cntMemo[b]; cntMemo[b] = 0; cout << sum << endl; } }
replace
16
17
16
17
0
p02630
C++
Runtime Error
#include <algorithm> #include <cmath> #include <iostream> #include <string> #include <vector> using namespace std; int main() { int N; cin >> N; vector<int> A(N); for (int i = 0; i < N; ++i) { cin >> A[i]; } int Q; cin >> Q; vector<int> B(Q), C(Q); for (int i = 0; i < Q; ++i) { cin >> B[i] >> C[i]; } long long sum = 0; vector<long long> cnt(10005, 0); for (int i = 0; i < N; ++i) { cnt[A[i]]++; sum += A[i]; } for (int i = 0; i < Q; ++i) { long long old_num = cnt[B[i]]; long long minus = (long long)B[i] * (long long)old_num; long long plus = (long long)C[i] * (long long)old_num; cnt[B[i]] = 0; cnt[C[i]] += (long long)old_num; sum = sum - minus + plus; cout << sum << endl; } }
#include <algorithm> #include <cmath> #include <iostream> #include <string> #include <vector> using namespace std; int main() { int N; cin >> N; vector<int> A(N); for (int i = 0; i < N; ++i) { cin >> A[i]; } int Q; cin >> Q; vector<int> B(Q), C(Q); for (int i = 0; i < Q; ++i) { cin >> B[i] >> C[i]; } long long sum = 0; vector<long long> cnt(100005, 0); for (int i = 0; i < N; ++i) { cnt[A[i]]++; sum += A[i]; } for (int i = 0; i < Q; ++i) { long long old_num = cnt[B[i]]; long long minus = (long long)B[i] * (long long)old_num; long long plus = (long long)C[i] * (long long)old_num; cnt[B[i]] = 0; cnt[C[i]] += (long long)old_num; sum = sum - minus + plus; cout << sum << endl; } }
replace
25
26
25
26
0
p02630
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); ++i) typedef long long ll; int main() { ll N, Q, s = 0; cin >> N; map<ll, ll> m; rep(i, N) { ll t; cin >> t; if (m.count(t)) ++m.at(t); else m[t] = 1; s += t; } cin >> Q; rep(i, Q) { ll B, C; cin >> B >> C; s = s - B * m.at(B) + C * m.at(B); if (m.count(C)) m.at(C) += m.at(B); else m[C] = m.at(B); m.erase(B); cout << s << endl; } }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); ++i) typedef long long ll; int main() { ll N, Q, s = 0; cin >> N; map<ll, ll> m; rep(i, N) { ll t; cin >> t; if (m.count(t)) ++m.at(t); else m[t] = 1; s += t; } cin >> Q; rep(i, Q) { ll B, C; cin >> B >> C; if (m.count(B)) { s = s - B * m.at(B) + C * m.at(B); if (m.count(C)) m.at(C) += m.at(B); else m[C] = m.at(B); m.erase(B); } cout << s << endl; } }
replace
24
30
24
33
0
p02630
C++
Time Limit Exceeded
// Problem : D - Replacing // Contest : AtCoder - AtCoder Beginner Contest 171 // URL : https://atcoder.jp/contests/abc171/tasks/abc171_d // Memory Limit : 1024 MB // Time Limit : 2000 ms // Powered by CP Editor (https://github.com/cpeditor/cpeditor) /* ID: vqt LANG: C++14 PROB: namenum */ #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #pragma GCC optimize("Ofast") #include <bits/stdc++.h> #include <unistd.h> using namespace std; // for PDBS use only: // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; // template <typename T> using o_set = tree<T, null_type, greater<T>, // rb_tree_tag, tree_order_statistics_node_update>; typedef long long ll; // #define ordered_set tree<pair<ll,ll>, null_type,less<pair<ll,ll>>, // rb_tree_tag,tree_order_statistics_node_update> // defines #define int long long // #define double long double #define vec vector #define vi vector<int> #define vvi vector<vector<int>> #define ii pair<int, int> #define F first #define S second #define MP make_pair #define vii vector<ii> #define PQ priority_queue #define minPQ priority_queue<int, vt<int>, greater<int>> #define PB push_back #define LB lower_bound #define UB upper_bound #define ER equal_range #define ALL(x) begin(x), end(x) #define SZ(x) ((int)(x).size()) #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define FORE(i, a, b) for (int i = (a); i <= (b); i++) #define FORD(i, a, b) for (int i = (a); i >= (b); i--) #define UNI(c) c.resize(distance(c.begin(), unique(c.begin(), c.end()))); #define SORT_UNI(c) \ (sort(c.begin(), c.end()), \ c.resize(distance(c.begin(), unique(c.begin(), c.end())))) #define POS(c, x) (lower_bound(c.begin(), c.end(), (x)) - c.begin()) #define max(a, b) ((a) < (b) ? b : a) #define min(a, b) ((a) < (b) ? a : b) #define setmin(a, b) a = min(a, b) #define setmax(a, b) a = max(a, b) #define MINMAX(x) minmax_element(begin(x), end(x)) // permanent constants const long long INF64 = 1e18; const int INF32 = 1e9; const double EPS = static_cast<double>(1e-10); const double PI = 2.0 * acos(0.0); // acos(-1.0L); const int MOD = (int)1e9 + 7; // 998244353 ll powMod(ll a, ll b) { ll res = 1; a %= MOD; assert(b >= 0); for (; b; b >>= 1) { if (b & 1) res = res * a % MOD; a = a * a % MOD; } return res; } ll invMod(ll a) { ll b = MOD, u = 1, v = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } return (MOD + u) % MOD; } const int day[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // random mt19937 rnd( 239); //(chrono::high_resolution_clock::now().time_since_epoch().count()); // Tricks: // Sort w Lambda Function: sort(begin(v), end(v), [] (int a, int b) { return a // > b; }); Sort vector cua bo ba so: struct MyStr {int a, b, c;}; // vector<MyStr> v; ...fill v... sort(all(v), [](MyStr &A, MyStr &B) { return // tie(A.a, A.b, A.c) < tie(B.a, B.b, B.c); }); #define odd(x) ((x)&1) #define even(x) !((x)&1) // vec - vector, p(x) - predicate so that p(x) = true for all elements of some // prefix of vector vec and false on all others. To find the first place where // p(x) doesn't hold one can simply use: #define FIRST_FAIL(vec, p) partition_point(ALL(vec), p) - vec.begin() // partition_point(begin, end, p) returns the first ITERATOR it that p(*it) = // false #define isIn(vec, key) binary_search(ALL(vec), key) #define toBool(num) !!(num) #define bin(N, num) cerr << bitset<N>(num) << "\n" // vector a: tinh XOR sum tu l den r: #define XORsum(a, l, r) \ accumulate(a.begin() + l - 1, a.begin() + r, 0, \ [](int x, int y) { return x ^ y; }) // BASIC STRING: just like vector but allows to use a few string member // functions/operators: operator+ and operator+=.: /* int n; basic_string<int> a; cin >> n; FOR(i, 0, n) { int x; cin >> x; a += x; } a += a; a = a.substr(n/2, n); cout << (a + a).find({1, 2, 1}) << '\n'; */ // count(a.begin(), a.end(), val) - count how many val in an array a vector<int> prefixSum(vector<int> &a) { vi b; std::partial_sum(a.begin(), a.end(), b.begin()); return b; } int mostSignDigit(int n) { double k = log10(n); return (int)pow(10, k - floor(k)); } int numOfDigits(int n) { return (int)floor(log10(n)) + 1; } bool isPowerOfTwo(int x) { return x && (!(x & (x - 1))); } // first x is for the case x=0 bool allPositive(vector<int> &a) { return all_of(begin(a), end(a), [](int x) { return x > 0; }); } // are all of the elements positive? // CHECK there is all numbers in array is positive: bool ok=true; for(auto z: a) // ok &= (z>0); bool anyPositive(vector<int> &a) { return any_of(begin(a), end(a), [](int x) { return x > 0; }); } // is there at least one positive element? bool nonePositive(vector<int> &a) { return none_of(begin(a), end(a), [](int x) { return x > 0; }); } // are none of the elements positive? void FILEIO() { freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); } // void FILEIO() { freopen("berries.in", "r", stdin); freopen("berries.out", // "w", stdout); } // USACO /****************** DEBUG *******************/ // Universal DEBUG: https://codeforces.com/blog/entry/68809 void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } // void __print(long long x) {cerr << x;} void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "T" : "F"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } // #define ONLINE_JUDGE #ifndef ONLINE_JUDGE #define debug(x...) \ cerr << "[" << #x << "] = ["; \ _print(x) #else #define debug(x...) #endif //********************** Codes go here *********************/ void solve() { int n; cin >> n; map<int, int> mp; FOR(i, 0, n) { int t; cin >> t; mp[t]++; } int sum = 0; for (auto z : mp) sum += z.F * z.S; int q; cin >> q; FOR(i, 0, q) { int b, c; cin >> b >> c; if (mp.find(b) == mp.end()) cout << sum << "\n"; else { sum -= b * mp[b]; FOR(i, 0, mp[b]) mp[c]++; sum += c * mp[b]; mp[b] = 0; cout << sum << "\n"; } } cout.flush(); } signed main() { ios_base::sync_with_stdio(false); cin.tie(); // FILEIO(); // auto beginProgram = chrono::steady_clock::now(); // preCalc(); // any 1-time precalculation need to do before many test cases? // int t; cin >> t; FORE(i, 1, t) { // cout << "Case #" << i << ": "; // Google CodeJam style solve(); } // auto endProgram = chrono::steady_clock::now(); // cerr << chrono::duration_cast<chrono::milliseconds>(endProgram - // beginProgram).count() << " ms" << endl; return 0; }
// Problem : D - Replacing // Contest : AtCoder - AtCoder Beginner Contest 171 // URL : https://atcoder.jp/contests/abc171/tasks/abc171_d // Memory Limit : 1024 MB // Time Limit : 2000 ms // Powered by CP Editor (https://github.com/cpeditor/cpeditor) /* ID: vqt LANG: C++14 PROB: namenum */ #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #pragma GCC optimize("Ofast") #include <bits/stdc++.h> #include <unistd.h> using namespace std; // for PDBS use only: // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; // template <typename T> using o_set = tree<T, null_type, greater<T>, // rb_tree_tag, tree_order_statistics_node_update>; typedef long long ll; // #define ordered_set tree<pair<ll,ll>, null_type,less<pair<ll,ll>>, // rb_tree_tag,tree_order_statistics_node_update> // defines #define int long long // #define double long double #define vec vector #define vi vector<int> #define vvi vector<vector<int>> #define ii pair<int, int> #define F first #define S second #define MP make_pair #define vii vector<ii> #define PQ priority_queue #define minPQ priority_queue<int, vt<int>, greater<int>> #define PB push_back #define LB lower_bound #define UB upper_bound #define ER equal_range #define ALL(x) begin(x), end(x) #define SZ(x) ((int)(x).size()) #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define FORE(i, a, b) for (int i = (a); i <= (b); i++) #define FORD(i, a, b) for (int i = (a); i >= (b); i--) #define UNI(c) c.resize(distance(c.begin(), unique(c.begin(), c.end()))); #define SORT_UNI(c) \ (sort(c.begin(), c.end()), \ c.resize(distance(c.begin(), unique(c.begin(), c.end())))) #define POS(c, x) (lower_bound(c.begin(), c.end(), (x)) - c.begin()) #define max(a, b) ((a) < (b) ? b : a) #define min(a, b) ((a) < (b) ? a : b) #define setmin(a, b) a = min(a, b) #define setmax(a, b) a = max(a, b) #define MINMAX(x) minmax_element(begin(x), end(x)) // permanent constants const long long INF64 = 1e18; const int INF32 = 1e9; const double EPS = static_cast<double>(1e-10); const double PI = 2.0 * acos(0.0); // acos(-1.0L); const int MOD = (int)1e9 + 7; // 998244353 ll powMod(ll a, ll b) { ll res = 1; a %= MOD; assert(b >= 0); for (; b; b >>= 1) { if (b & 1) res = res * a % MOD; a = a * a % MOD; } return res; } ll invMod(ll a) { ll b = MOD, u = 1, v = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } return (MOD + u) % MOD; } const int day[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // random mt19937 rnd( 239); //(chrono::high_resolution_clock::now().time_since_epoch().count()); // Tricks: // Sort w Lambda Function: sort(begin(v), end(v), [] (int a, int b) { return a // > b; }); Sort vector cua bo ba so: struct MyStr {int a, b, c;}; // vector<MyStr> v; ...fill v... sort(all(v), [](MyStr &A, MyStr &B) { return // tie(A.a, A.b, A.c) < tie(B.a, B.b, B.c); }); #define odd(x) ((x)&1) #define even(x) !((x)&1) // vec - vector, p(x) - predicate so that p(x) = true for all elements of some // prefix of vector vec and false on all others. To find the first place where // p(x) doesn't hold one can simply use: #define FIRST_FAIL(vec, p) partition_point(ALL(vec), p) - vec.begin() // partition_point(begin, end, p) returns the first ITERATOR it that p(*it) = // false #define isIn(vec, key) binary_search(ALL(vec), key) #define toBool(num) !!(num) #define bin(N, num) cerr << bitset<N>(num) << "\n" // vector a: tinh XOR sum tu l den r: #define XORsum(a, l, r) \ accumulate(a.begin() + l - 1, a.begin() + r, 0, \ [](int x, int y) { return x ^ y; }) // BASIC STRING: just like vector but allows to use a few string member // functions/operators: operator+ and operator+=.: /* int n; basic_string<int> a; cin >> n; FOR(i, 0, n) { int x; cin >> x; a += x; } a += a; a = a.substr(n/2, n); cout << (a + a).find({1, 2, 1}) << '\n'; */ // count(a.begin(), a.end(), val) - count how many val in an array a vector<int> prefixSum(vector<int> &a) { vi b; std::partial_sum(a.begin(), a.end(), b.begin()); return b; } int mostSignDigit(int n) { double k = log10(n); return (int)pow(10, k - floor(k)); } int numOfDigits(int n) { return (int)floor(log10(n)) + 1; } bool isPowerOfTwo(int x) { return x && (!(x & (x - 1))); } // first x is for the case x=0 bool allPositive(vector<int> &a) { return all_of(begin(a), end(a), [](int x) { return x > 0; }); } // are all of the elements positive? // CHECK there is all numbers in array is positive: bool ok=true; for(auto z: a) // ok &= (z>0); bool anyPositive(vector<int> &a) { return any_of(begin(a), end(a), [](int x) { return x > 0; }); } // is there at least one positive element? bool nonePositive(vector<int> &a) { return none_of(begin(a), end(a), [](int x) { return x > 0; }); } // are none of the elements positive? void FILEIO() { freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); } // void FILEIO() { freopen("berries.in", "r", stdin); freopen("berries.out", // "w", stdout); } // USACO /****************** DEBUG *******************/ // Universal DEBUG: https://codeforces.com/blog/entry/68809 void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } // void __print(long long x) {cerr << x;} void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "T" : "F"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } // #define ONLINE_JUDGE #ifndef ONLINE_JUDGE #define debug(x...) \ cerr << "[" << #x << "] = ["; \ _print(x) #else #define debug(x...) #endif //********************** Codes go here *********************/ void solve() { int n; cin >> n; map<int, int> mp; FOR(i, 0, n) { int t; cin >> t; mp[t]++; } int sum = 0; for (auto z : mp) sum += z.F * z.S; int q; cin >> q; FOR(i, 0, q) { int b, c; cin >> b >> c; if (mp.find(b) == mp.end()) cout << sum << "\n"; else { sum -= b * mp[b]; mp[c] += mp[b]; sum += c * mp[b]; mp[b] = 0; cout << sum << "\n"; } } cout.flush(); } signed main() { ios_base::sync_with_stdio(false); cin.tie(); // FILEIO(); // auto beginProgram = chrono::steady_clock::now(); // preCalc(); // any 1-time precalculation need to do before many test cases? // int t; cin >> t; FORE(i, 1, t) { // cout << "Case #" << i << ": "; // Google CodeJam style solve(); } // auto endProgram = chrono::steady_clock::now(); // cerr << chrono::duration_cast<chrono::milliseconds>(endProgram - // beginProgram).count() << " ms" << endl; return 0; }
replace
229
230
229
230
TLE
p02630
C++
Runtime Error
#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() { ll n, q, b, c, w, ans = 0; cin >> n; vector<int> a(n); rep(i, n) { cin >> w; a[w]++; ans += w; } cin >> q; rep(i, q) { cin >> b >> c; ans -= a[b] * (b - c); a[c] += a[b]; a[b] = 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() { ll n, q, b, c, w, ans = 0; cin >> n; vector<int> a(500010); rep(i, n) { cin >> w; a[w]++; ans += w; } cin >> q; rep(i, q) { cin >> b >> c; ans -= a[b] * (b - c); a[c] += a[b]; a[b] = 0; cout << ans << endl; } }
replace
9
10
9
10
0
p02630
C++
Runtime Error
// abc171_d #include <algorithm> #include <bitset> #include <complex> #include <deque> #include <exception> #include <fstream> #include <functional> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <iterator> #include <limits> #include <list> #include <locale> #include <map> #include <memory> #include <new> #include <numeric> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdexcept> #include <streambuf> #include <string> #include <typeinfo> #include <utility> #include <valarray> #include <vector> #if __cplusplus >= 201103L #include <array> #include <atomic> #include <chrono> #include <condition_variable> #include <forward_list> #include <future> #include <initializer_list> #include <mutex> #include <random> #include <ratio> #include <regex> #include <scoped_allocator> #include <system_error> #include <thread> #include <tuple> #include <type_traits> #include <typeindex> #include <unordered_map> #include <unordered_set> #endif template <typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; } template <typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; } const double PI = acos(-1); const double EPS = 1e-9; int inf = sizeof(int) == sizeof(long long) ? 2e18 : 1e9 + 10; int dx[] = {0, 1, 0, -1}; int dy[] = {1, 0, -1, 0}; #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() { int N; cin >> N; vector<ll> A(N + 1, 0); ll A_sum = 0; rep(i, N) { int tmp; cin >> tmp; A[tmp]++; A_sum += tmp; } int Q; cin >> Q; rep(i, Q) { ll B, C; cin >> B >> C; ll value = A[B]; A[B] = 0, A[C] += value; A_sum = A_sum + (C - B) * value; cout << A_sum << endl; } return 0; }
// abc171_d #include <algorithm> #include <bitset> #include <complex> #include <deque> #include <exception> #include <fstream> #include <functional> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <iterator> #include <limits> #include <list> #include <locale> #include <map> #include <memory> #include <new> #include <numeric> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdexcept> #include <streambuf> #include <string> #include <typeinfo> #include <utility> #include <valarray> #include <vector> #if __cplusplus >= 201103L #include <array> #include <atomic> #include <chrono> #include <condition_variable> #include <forward_list> #include <future> #include <initializer_list> #include <mutex> #include <random> #include <ratio> #include <regex> #include <scoped_allocator> #include <system_error> #include <thread> #include <tuple> #include <type_traits> #include <typeindex> #include <unordered_map> #include <unordered_set> #endif template <typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; } template <typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; } const double PI = acos(-1); const double EPS = 1e-9; int inf = sizeof(int) == sizeof(long long) ? 2e18 : 1e9 + 10; int dx[] = {0, 1, 0, -1}; int dy[] = {1, 0, -1, 0}; #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() { int N; cin >> N; vector<ll> A(100005, 0); ll A_sum = 0; rep(i, N) { int tmp; cin >> tmp; A[tmp]++; A_sum += tmp; } int Q; cin >> Q; rep(i, Q) { ll B, C; cin >> B >> C; ll value = A[B]; A[B] = 0, A[C] += value; A_sum = A_sum + (C - B) * value; cout << A_sum << endl; } return 0; }
replace
75
76
75
76
0
p02630
C++
Runtime Error
#include "bits/stdc++.h" using namespace std; const int MOD = 1e9 + 7; typedef long long lint; typedef pair<int, int> P; #define all(x) (x).begin(), (x).end() #define pb push_back #define rep(i, n) for (lint i = 0; i < (n); i++) #define INF LLONG_MAX #define all(x) (x).begin(), (x).end() #define pb push_back int main() { int n; cin >> n; vector<int> a(n); vector<int> count(10001); lint sum = 0; rep(i, n) { cin >> a.at(i); sum += a.at(i); count.at(a.at(i))++; } int q; cin >> q; rep(i, q) { int a, b; cin >> a >> b; sum = sum + (b - a) * count.at(a); count.at(b) += count.at(a); count.at(a) = 0; cout << sum << endl; } return 0; }
#include "bits/stdc++.h" using namespace std; const int MOD = 1e9 + 7; typedef long long lint; typedef pair<int, int> P; #define all(x) (x).begin(), (x).end() #define pb push_back #define rep(i, n) for (lint i = 0; i < (n); i++) #define INF LLONG_MAX #define all(x) (x).begin(), (x).end() #define pb push_back int main() { int n; cin >> n; vector<int> a(n); vector<int> count(100001); lint sum = 0; rep(i, n) { cin >> a.at(i); sum += a.at(i); count.at(a.at(i))++; } int q; cin >> q; rep(i, q) { int a, b; cin >> a >> b; sum = sum + (b - a) * count.at(a); count.at(b) += count.at(a); count.at(a) = 0; cout << sum << endl; } return 0; }
replace
15
16
15
16
0
p02630
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdlib> #include <iostream> #include <queue> #include <set> #include <string> #include <vector> using namespace std; typedef long long ll; int main() { int n; cin >> n; vector<ll> cou(n, 0); ll sum = 0; for (int i = 0; i < n; ++i) { int a; cin >> a; cou[a]++; sum += a; } int q; cin >> q; for (int i = 0; i < q; ++i) { int b, c; cin >> b >> c; sum += (c - b) * cou[b]; cou[c] += cou[b]; cou[b] = 0; cout << sum << endl; } }
#include <algorithm> #include <cmath> #include <cstdlib> #include <iostream> #include <queue> #include <set> #include <string> #include <vector> using namespace std; typedef long long ll; int main() { int n; cin >> n; vector<ll> cou(100001, 0); ll sum = 0; for (int i = 0; i < n; ++i) { int a; cin >> a; cou[a]++; sum += a; } int q; cin >> q; for (int i = 0; i < q; ++i) { int b, c; cin >> b >> c; sum += (c - b) * cou[b]; cou[c] += cou[b]; cou[b] = 0; cout << sum << endl; } }
replace
15
16
15
16
0
p02630
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) using ll = long long; using ull = unsigned long long; using P = pair<int, int>; using PP = pair<int, pair<int, int>>; using T = tuple<string, int, int>; const string EMP = " "; const ll INF = 1LL << 60; const ll MOD = 1000000007; int main() { int n; cin >> n; vector<ull> a(n + 1); ull ans = 0; rep(i, n) { int aa; cin >> aa; ans += aa; a[aa]++; } int q; cin >> q; for (int i = 0; i < q; i++) { int b, c; cin >> b >> c; ans -= b * a[b]; ans += c * a[b]; a[c] += a[b]; a[b] = 0; cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) using ll = long long; using ull = unsigned long long; using P = pair<int, int>; using PP = pair<int, pair<int, int>>; using T = tuple<string, int, int>; const string EMP = " "; const ll INF = 1LL << 60; const ll MOD = 1000000007; int main() { int n; cin >> n; vector<ull> a(1000001); ull ans = 0; rep(i, n) { int aa; cin >> aa; ans += aa; a[aa]++; } int q; cin >> q; for (int i = 0; i < q; i++) { int b, c; cin >> b >> c; ans -= b * a[b]; ans += c * a[b]; a[c] += a[b]; a[b] = 0; cout << ans << endl; } return 0; }
replace
15
16
15
16
0
p02630
C++
Runtime Error
#include <algorithm> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <string> #include <vector> using namespace std; using Graph = vector<vector<int>>; 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; } int main() { long long sum = 0; vector<long long> t(10010, 0); int n; cin >> n; for (int i = 0; i < n; ++i) { int temp; cin >> temp; ++t[temp]; sum += temp; } int q; cin >> q; vector<long long> ans(q); for (int i = 0; i < q; ++i) { int b, c; cin >> b >> c; sum += (c - b) * t[b]; ans[i] = sum; t[c] += t[b]; t[b] = 0; } // for(int i=0;i<q;++i) cout<<ans[i]<<endl; for (int i = 0; i < q; ++i) printf("%lld \n", ans[i]); }
#include <algorithm> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <string> #include <vector> using namespace std; using Graph = vector<vector<int>>; 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; } int main() { long long sum = 0; vector<long long> t(110010, 0); int n; cin >> n; for (int i = 0; i < n; ++i) { int temp; cin >> temp; ++t[temp]; sum += temp; } int q; cin >> q; vector<long long> ans(q); for (int i = 0; i < q; ++i) { int b, c; cin >> b >> c; sum += (c - b) * t[b]; ans[i] = sum; t[c] += t[b]; t[b] = 0; } // for(int i=0;i<q;++i) cout<<ans[i]<<endl; for (int i = 0; i < q; ++i) printf("%lld \n", ans[i]); }
replace
28
29
28
29
0
p02630
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { map<long long, long long> h; long long N; cin >> N; long long sum = 0; for (int i = 0; i < N; i++) { long long a; cin >> a; sum += a; h[a]++; } int Q; cin >> Q; long long BC[N][2]; for (int i = 0; i < Q; i++) { cin >> BC[i][0] >> BC[i][1]; } for (int i = 0; i < Q; i++) { long long B, C; B = BC[i][0]; C = BC[i][1]; // cout << h[B] << endl; // cout << h[C] << endl; sum += C * h[B] - B * h[B]; h[C] += h[B]; h[B] = 0; cout << sum << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { map<long long, long long> h; long long N; cin >> N; long long sum = 0; for (int i = 0; i < N; i++) { long long a; cin >> a; sum += a; h[a]++; } int Q; cin >> Q; long long BC[Q][2]; for (int i = 0; i < Q; i++) { cin >> BC[i][0] >> BC[i][1]; } for (int i = 0; i < Q; i++) { long long B, C; B = BC[i][0]; C = BC[i][1]; // cout << h[B] << endl; // cout << h[C] << endl; sum += C * h[B] - B * h[B]; h[C] += h[B]; h[B] = 0; cout << sum << endl; } }
replace
16
18
16
17
0
p02630
C++
Runtime Error
#include <algorithm> #include <cmath> #include <iostream> #include <map> #include <stdio.h> #include <vector> using namespace std; int main() { long long int a[10005] = {}; long long int n; cin >> n; long long int temp; long long int ans = 0; for (int i = 0; i < n; i++) { cin >> temp; a[temp]++; ans += temp; } long long int q; cin >> q; vector<long long int> b; vector<long long int> c; for (int i = 0; i < q; i++) { cin >> temp; b.push_back(temp); cin >> temp; c.push_back(temp); } for (int i = 0; i < q; i++) { ans += a[b[i]] * (c[i] - b[i]); cout << ans; if (i != q - 1) cout << "\n"; a[c[i]] += a[b[i]]; a[b[i]] = 0; } return 0; }
#include <algorithm> #include <cmath> #include <iostream> #include <map> #include <stdio.h> #include <vector> using namespace std; int main() { long long int a[100005] = {}; long long int n; cin >> n; long long int temp; long long int ans = 0; for (int i = 0; i < n; i++) { cin >> temp; a[temp]++; ans += temp; } long long int q; cin >> q; vector<long long int> b; vector<long long int> c; for (int i = 0; i < q; i++) { cin >> temp; b.push_back(temp); cin >> temp; c.push_back(temp); } for (int i = 0; i < q; i++) { ans += a[b[i]] * (c[i] - b[i]); cout << ans; if (i != q - 1) cout << "\n"; a[c[i]] += a[b[i]]; a[b[i]] = 0; } return 0; }
replace
10
11
10
11
0
p02630
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long #define ld long double using namespace std; #define pi 3.1415926535 int main() { int n; cin >> n; vector<int> a(10001, 0); int tmp; ll sum = 0; for (int i = 0; i < n; i++) { cin >> tmp; a[tmp]++; sum += tmp; } int q; cin >> q; vector<ll> ans(q, 0); int b, c; for (int i = 0; i < q; i++) { cin >> b >> c; a[c] += a[b]; sum += a[b] * (c - b); a[b] = 0; ans[i] = sum; } for (int i = 0; i < q; i++) { cout << ans[i] << endl; } return 0; }
#include <bits/stdc++.h> #define ll long long #define ld long double using namespace std; #define pi 3.1415926535 int main() { int n; cin >> n; vector<int> a(100001, 0); int tmp; ll sum = 0; for (int i = 0; i < n; i++) { cin >> tmp; a[tmp]++; sum += tmp; } int q; cin >> q; vector<ll> ans(q, 0); int b, c; for (int i = 0; i < q; i++) { cin >> b >> c; a[c] += a[b]; sum += a[b] * (c - b); a[b] = 0; ans[i] = sum; } for (int i = 0; i < q; i++) { cout << ans[i] << endl; } return 0; }
replace
11
12
11
12
0
p02630
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define rep1(i, n) for (int i = 1; i < n + 1; i++) #define all(A) A.begin(), A.end() typedef long long ll; int main() { int n; cin >> n; vector<ll> buc(10101); ll ans = 0; rep(i, n) { ll x; cin >> x; buc[x]++; ans += x; } int q; cin >> q; rep(i, q) { ll b, c; cin >> b >> c; ans += c * buc[b] - buc[b] * b; buc[c] += buc[b]; buc[b] = 0; cout << ans << endl; } }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define rep1(i, n) for (int i = 1; i < n + 1; i++) #define all(A) A.begin(), A.end() typedef long long ll; int main() { int n; cin >> n; vector<ll> buc(101010); ll ans = 0; rep(i, n) { ll x; cin >> x; buc[x]++; ans += x; } int q; cin >> q; rep(i, q) { ll b, c; cin >> b >> c; ans += c * buc[b] - buc[b] * b; buc[c] += buc[b]; buc[b] = 0; cout << ans << endl; } }
replace
10
11
10
11
0
p02630
C++
Runtime Error
#include <algorithm> #include <cmath> #include <iostream> #include <map> #include <numeric> #include <queue> #include <string> #include <vector> int main() { int n; std::cin >> n; std::vector<int> num(n + 1); long long int sum = 0; for (int i = 0; i < n; i++) { int a; std::cin >> a; sum += a; num[a]++; } int q = 0; std::cin >> q; for (int i = 0; i < q; i++) { int b, c; std::cin >> b >> c; sum += (c - b) * num[b]; num[c] += num[b]; num[b] = 0; std::cout << sum << std::endl; } return 0; }
#include <algorithm> #include <cmath> #include <iostream> #include <map> #include <numeric> #include <queue> #include <string> #include <vector> int main() { int n; std::cin >> n; std::vector<int> num(100000); long long int sum = 0; for (int i = 0; i < n; i++) { int a; std::cin >> a; sum += a; num[a]++; } int q = 0; std::cin >> q; for (int i = 0; i < q; i++) { int b, c; std::cin >> b >> c; sum += (c - b) * num[b]; num[c] += num[b]; num[b] = 0; std::cout << sum << std::endl; } return 0; }
replace
12
13
12
13
0
p02630
C++
Time Limit Exceeded
#include <iostream> #include <unordered_map> using namespace std; typedef long long ll; int main() { int N; cin >> N; unordered_map<ll, ll> mp; ll ans = 0; for (int i = 0; i < N; i++) { int A; cin >> A; mp[A]++; ans += A; } int Q; cin >> Q; for (int i = 0; i < Q; i++) { int B, C; cin >> B >> C; ll n = 0; auto ite = mp.begin(); for (ite; ite != mp.end(); ite++) { if (ite->first == B) break; } if (ite != mp.end()) { ans += (C - B) * ite->second; mp[C] += ite->second; ite->second = 0; } cout << ans << endl; } }
#include <iostream> #include <unordered_map> using namespace std; typedef long long ll; int main() { int N; cin >> N; unordered_map<ll, ll> mp; ll ans = 0; for (int i = 0; i < N; i++) { int A; cin >> A; mp[A]++; ans += A; } int Q; cin >> Q; for (int i = 0; i < Q; i++) { int B, C; cin >> B >> C; ll n = 0; auto ite = mp.find(B); if (ite != mp.end()) { ans += (C - B) * ite->second; mp[C] += ite->second; ite->second = 0; } cout << ans << endl; } }
replace
22
27
22
23
TLE
p02630
C++
Runtime Error
#include <algorithm> #include <bits/stdc++.h> #include <cctype> #include <cmath> #include <cstdio> #include <iostream> #include <map> #include <math.h> #include <stdio.h> #include <string.h> #include <unordered_map> #define Maxn 5001 #define ll long long using namespace std; ll sum; int num[Maxn]; int a[Maxn]; int main() { int n, q, b, c; scanf("%d", &n); sum = 0; for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); sum += a[i]; num[a[i]]++; // a[i]出现的次数 } scanf("%d", &q); for (int i = 1; i <= q; i++) { scanf("%d%d", &b, &c); sum += (c - b) * num[b]; num[c] += num[b]; num[b] = 0; printf("%lld\n", sum); } return 0; }
#include <algorithm> #include <bits/stdc++.h> #include <cctype> #include <cmath> #include <cstdio> #include <iostream> #include <map> #include <math.h> #include <stdio.h> #include <string.h> #include <unordered_map> #define Maxn 100005 #define ll long long using namespace std; ll sum; int num[Maxn]; int a[Maxn]; int main() { int n, q, b, c; scanf("%d", &n); sum = 0; for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); sum += a[i]; num[a[i]]++; // a[i]出现的次数 } scanf("%d", &q); for (int i = 1; i <= q; i++) { scanf("%d%d", &b, &c); sum += (c - b) * num[b]; num[c] += num[b]; num[b] = 0; printf("%lld\n", sum); } return 0; }
replace
11
12
11
12
0
p02630
C++
Runtime Error
#pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> #define speedX \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL) using namespace std; // #define ONLINE_JUDGE #ifndef ONLINE_JUDGE template <typename T> void __p(T a) { cout << a << " "; } template <typename T, typename F> void __p(pair<T, F> a) { cout << "{ "; __p(a.first); __p(a.second); cout << "}"; } template <typename T> void __f(const char *s, T t) { cout << s << " : "; __p(t); } template <typename T> void __t(const char *s, const T &x) { cout << s << " : { "; for (auto it : x) __p(it); cout << "} "; } template <typename T, typename... F> void __f(const char *s, T t, F... f) { int i = 0; for (;; ++i) if (s[i] == ',') break; cout.write(s, i) << " : "; __p(t); cout << " | "; __f(s + i + 1, f...); } #define trace(...) \ { \ cout << "LINE: " << __LINE__ << " || "; \ __f(#__VA_ARGS__, __VA_ARGS__); \ cout << "\n\n"; \ } #define debug(...) \ { \ cout << "LINE: " << __LINE__ << " || "; \ __t(#__VA_ARGS__, __VA_ARGS__); \ cout << "\n\n"; \ } int begtime = clock(); #define end_time() \ cout << "\n\nTime of execution: " \ << (clock() - begtime) * 1000 / CLOCKS_PER_SEC << " ms\n\n"; #else #define trace(...) #define debug(...) #define end_time() #endif #define int long long typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<pii, int> piii; typedef vector<int> vi; typedef vector<pii> vii; inline bool equals(ld a, ld b) { return fabs(a - b) < 1e-9; } inline int gcd(int a, int b) { a = abs(a); b = abs(b); while (a > 0 && b > 0) (a > b ? a %= b : b %= a); return (a == 0 ? b : a); } inline int power(int x, int n, int m = LLONG_MAX) { int res = 1; x = (x % m + m) % m; while (n) { if (n & 1) res = (res * x) % m; x = (x * x) % m; n >>= 1; } return res; } #define pb push_back #define mp make_pair #define mt make_tuple #define ff first #define ss second #define uset unordered_set #define umap unordered_map #define all(x) x.begin(), x.end() #define mod 1000000007 void solve() { int n; cin >> n; vi v((10 ^ 5) + 1, 0); // memset(v,0,v.size()); int sum = 0; for (int i = 0; i < n; i++) { int temp; cin >> temp; sum += temp; v[temp]++; } int q; cin >> q; vii p; for (int i = 0; i < q; i++) { int f, s; cin >> f >> s; p.pb(mp(f, s)); } int ans = sum; for (int i = 0; i < q; i++) { int temp; temp = v[p[i].ff]; ans = (ans - (temp * p[i].ff) + (temp * p[i].ss)); v[p[i].ss] += temp; v[p[i].ff] = 0; cout << ans << "\n"; } } signed main() { speedX; #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int t = 1; // cin>>t; for (int i = 1; i <= t; ++i) { // cout<<"Case #"<<i<<": "; solve(); } end_time(); return 0; }
#pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> #define speedX \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL) using namespace std; // #define ONLINE_JUDGE #ifndef ONLINE_JUDGE template <typename T> void __p(T a) { cout << a << " "; } template <typename T, typename F> void __p(pair<T, F> a) { cout << "{ "; __p(a.first); __p(a.second); cout << "}"; } template <typename T> void __f(const char *s, T t) { cout << s << " : "; __p(t); } template <typename T> void __t(const char *s, const T &x) { cout << s << " : { "; for (auto it : x) __p(it); cout << "} "; } template <typename T, typename... F> void __f(const char *s, T t, F... f) { int i = 0; for (;; ++i) if (s[i] == ',') break; cout.write(s, i) << " : "; __p(t); cout << " | "; __f(s + i + 1, f...); } #define trace(...) \ { \ cout << "LINE: " << __LINE__ << " || "; \ __f(#__VA_ARGS__, __VA_ARGS__); \ cout << "\n\n"; \ } #define debug(...) \ { \ cout << "LINE: " << __LINE__ << " || "; \ __t(#__VA_ARGS__, __VA_ARGS__); \ cout << "\n\n"; \ } int begtime = clock(); #define end_time() \ cout << "\n\nTime of execution: " \ << (clock() - begtime) * 1000 / CLOCKS_PER_SEC << " ms\n\n"; #else #define trace(...) #define debug(...) #define end_time() #endif #define int long long typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<pii, int> piii; typedef vector<int> vi; typedef vector<pii> vii; inline bool equals(ld a, ld b) { return fabs(a - b) < 1e-9; } inline int gcd(int a, int b) { a = abs(a); b = abs(b); while (a > 0 && b > 0) (a > b ? a %= b : b %= a); return (a == 0 ? b : a); } inline int power(int x, int n, int m = LLONG_MAX) { int res = 1; x = (x % m + m) % m; while (n) { if (n & 1) res = (res * x) % m; x = (x * x) % m; n >>= 1; } return res; } #define pb push_back #define mp make_pair #define mt make_tuple #define ff first #define ss second #define uset unordered_set #define umap unordered_map #define all(x) x.begin(), x.end() #define mod 1000000007 void solve() { int n; cin >> n; vi v(pow(10, 5), 0); // memset(v,0,v.size()); int sum = 0; for (int i = 0; i < n; i++) { int temp; cin >> temp; sum += temp; v[temp]++; } int q; cin >> q; vii p; for (int i = 0; i < q; i++) { int f, s; cin >> f >> s; p.pb(mp(f, s)); } int ans = sum; for (int i = 0; i < q; i++) { int temp; temp = v[p[i].ff]; ans = (ans - (temp * p[i].ff) + (temp * p[i].ss)); v[p[i].ss] += temp; v[p[i].ff] = 0; cout << ans << "\n"; } } signed main() { speedX; #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int t = 1; // cin>>t; for (int i = 1; i <= t; ++i) { // cout<<"Case #"<<i<<": "; solve(); } end_time(); return 0; }
replace
96
97
96
97
TLE
p02630
C++
Runtime Error
/** * Bismillahir Rahmanir Rahim. * Imtiaz_rafi * PCIU, CHITTAGONG **/ #include <bits/stdc++.h> using namespace std; #define speed \ ios::sync_with_stdio(0); \ cin.tie(0); #define filein freopen("input.txt", "r", stdin) #define fileout freopen("output.txt", "w", stdout) #define rep(i, a) for (ll i = 0; i < a; i++) #define rep1(i, begin, end) for (ll i = begin; i <= end; i++) #define all(v) v.begin(), v.end() #define pb push_back #define pf push_front #define mp make_pair #define pi acos(-1) // bitshift (1LL<<(3)) *2^3 typedef long long int ll; typedef vector<ll> vi; typedef map<ll, ll> m; typedef map<char, ll> mc; typedef set<ll> st; typedef set<char> sc; const ll mx = 1e5 + 123; ll binarySearch(ll arr[], ll p, ll r, ll num) { if (p <= r) { ll mid = (p + r) / 2; if (arr[mid] == num) return mid; if (arr[mid] > num) return binarySearch(arr, p, mid - 1, num); if (arr[mid] < num) return binarySearch(arr, mid + 1, r, num); } return -1; } int main() { speed; ll t, i, j, a, b, x, y, e = 0, ans = 0, sum = 0; cin >> a; vi c(a + 5, 0); vi d(a); rep(i, a) { cin >> d[i]; sum += d[i]; c[d[i]]++; } cin >> b; while (b--) { cin >> x >> y; sum += (y - x) * c[x]; c[y] += c[x]; c[x] = 0; cout << sum << endl; } }
/** * Bismillahir Rahmanir Rahim. * Imtiaz_rafi * PCIU, CHITTAGONG **/ #include <bits/stdc++.h> using namespace std; #define speed \ ios::sync_with_stdio(0); \ cin.tie(0); #define filein freopen("input.txt", "r", stdin) #define fileout freopen("output.txt", "w", stdout) #define rep(i, a) for (ll i = 0; i < a; i++) #define rep1(i, begin, end) for (ll i = begin; i <= end; i++) #define all(v) v.begin(), v.end() #define pb push_back #define pf push_front #define mp make_pair #define pi acos(-1) // bitshift (1LL<<(3)) *2^3 typedef long long int ll; typedef vector<ll> vi; typedef map<ll, ll> m; typedef map<char, ll> mc; typedef set<ll> st; typedef set<char> sc; const ll mx = 1e5 + 123; ll binarySearch(ll arr[], ll p, ll r, ll num) { if (p <= r) { ll mid = (p + r) / 2; if (arr[mid] == num) return mid; if (arr[mid] > num) return binarySearch(arr, p, mid - 1, num); if (arr[mid] < num) return binarySearch(arr, mid + 1, r, num); } return -1; } int main() { speed; ll t, i, j, a, b, x, y, e = 0, ans = 0, sum = 0; cin >> a; vi c(mx, 0); vi d(a); rep(i, a) { cin >> d[i]; sum += d[i]; c[d[i]]++; } cin >> b; while (b--) { cin >> x >> y; sum += (y - x) * c[x]; c[y] += c[x]; c[x] = 0; cout << sum << endl; } }
replace
43
44
43
44
0
p02630
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int> A(N), memo(100000); int64_t ans = 0; for (int i = 0; i < N; i++) { cin >> A.at(i); memo.at(A.at(i))++; ans += A.at(i); } int Q; cin >> Q; for (int i = 0; i < Q; i++) { int64_t B, C; cin >> B >> C; ans += memo.at(B) * (C - B); memo.at(C) += memo.at(B); memo.at(B) = 0; cout << ans << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int> A(N), memo(100001); int64_t ans = 0; for (int i = 0; i < N; i++) { cin >> A.at(i); memo.at(A.at(i))++; ans += A.at(i); } int Q; cin >> Q; for (int i = 0; i < Q; i++) { int64_t B, C; cin >> B >> C; ans += memo.at(B) * (C - B); memo.at(C) += memo.at(B); memo.at(B) = 0; cout << ans << endl; } }
replace
6
7
6
7
0
p02630
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main(void) { int n; cin >> n; ll tot = 0; vector<ll> cnt(n); for (int i = 0; i < n; i++) { int a; cin >> a; cnt[a]++; tot += a; } int q; cin >> q; for (int i = 0; i < q; i++) { int b, c; cin >> b >> c; tot += (c - b) * cnt[b]; cnt[c] += cnt[b]; cnt[b] = 0; cout << tot << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main(void) { int n; cin >> n; ll tot = 0; vector<ll> cnt(100005); for (int i = 0; i < n; i++) { int a; cin >> a; cnt[a]++; tot += a; } int q; cin >> q; for (int i = 0; i < q; i++) { int b, c; cin >> b >> c; tot += (c - b) * cnt[b]; cnt[c] += cnt[b]; cnt[b] = 0; cout << tot << endl; } return 0; }
replace
8
9
8
9
0
p02630
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, f, n) for (ll i = (f); (i) < (n); i++) #define repe(i, f, n) for (ll i = (f); (i) <= (n); i++) using namespace std; using ll = long long; ll INF = 1LL << 60; using G = vector<map<int, int>>; int main() { int N; cin >> N; vector<ll> num(10); ll acum = 0; rep(i, 0, N) { ll tmp; cin >> tmp; num[tmp]++; acum += tmp; } int Q; cin >> Q; vector<pair<int, int>> q(Q); rep(i, 0, Q) cin >> q[i].first >> q[i].second; rep(i, 0, Q) { int B = q[i].first; int C = q[i].second; num[C] += num[B]; acum = acum + (num[B] * (C - B)); num[B] = 0; cout << acum << endl; } }
#include <bits/stdc++.h> #define rep(i, f, n) for (ll i = (f); (i) < (n); i++) #define repe(i, f, n) for (ll i = (f); (i) <= (n); i++) using namespace std; using ll = long long; ll INF = 1LL << 60; using G = vector<map<int, int>>; int main() { int N; cin >> N; vector<ll> num(100001); ll acum = 0; rep(i, 0, N) { ll tmp; cin >> tmp; num[tmp]++; acum += tmp; } int Q; cin >> Q; vector<pair<int, int>> q(Q); rep(i, 0, Q) cin >> q[i].first >> q[i].second; rep(i, 0, Q) { int B = q[i].first; int C = q[i].second; num[C] += num[B]; acum = acum + (num[B] * (C - B)); num[B] = 0; cout << acum << endl; } }
replace
12
13
12
13
0
p02630
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) using ll = long long; int main() { int n; cin >> n; int a; vector<int> t(n, 0); ll s = 0; rep(i, n) { cin >> a; t[a]++; s += a; } int q; cin >> q; int b, c; rep(i, q) { cin >> b >> c; s += (c - b) * t[b]; t[c] += t[b]; t[b] = 0; cout << s << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) using ll = long long; int main() { int n; cin >> n; int a; int t[100010]; rep(i, 100010) { t[i] = 0; } ll s = 0; rep(i, n) { cin >> a; t[a]++; s += a; } int q; cin >> q; int b, c; rep(i, q) { cin >> b >> c; s += (c - b) * t[b]; t[c] += t[b]; t[b] = 0; cout << s << endl; } return 0; }
replace
9
10
9
11
0
p02630
C++
Runtime Error
#include <algorithm> #include <iostream> #include <math.h> #include <vector> using namespace std; int main() { vector<int> num(10001, 0); vector<long long> ans; int n, k, q, b, c; long long sum = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> k; num[k]++; sum += k; } cin >> q; for (int i = 0; i < q; i++) { cin >> b >> c; k = num[b]; num[b] = 0; num[c] += k; sum += (long long)((long long)c - (long long)b) * (long long)k; ans.emplace_back(sum); } for (int i = 0; i < ans.size(); i++) { cout << ans[i] << "\n"; } return 0; }
#include <algorithm> #include <iostream> #include <math.h> #include <vector> using namespace std; int main() { vector<int> num(100001, 0); vector<long long> ans; int n, k, q, b, c; long long sum = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> k; num[k]++; sum += k; } cin >> q; for (int i = 0; i < q; i++) { cin >> b >> c; k = num[b]; num[b] = 0; num[c] += k; sum += (long long)((long long)c - (long long)b) * (long long)k; ans.emplace_back(sum); } for (int i = 0; i < ans.size(); i++) { cout << ans[i] << "\n"; } return 0; }
replace
8
9
8
9
0
p02630
C++
Runtime Error
// By Zank100 #include <bits/stdc++.h> using namespace std; #define ll long long #define ull unsigned long long #define mod 1000000007 #define din(t) \ long long t; \ cin >> t; #define in(t) cin >> t; ll mul(ll a, ll b) { return (((a % mod) * (b % mod)) % mod); } ll modexp(ll b, ll e) { ll res = 1; for (; e; b = b * b % mod, e >>= 1) if (e & 1) res = res * b % mod; return res; } ll modinv(ll a) { ll x = mod - 2; return modexp(a, x); } ll power(ll x, ll y) { ll temp; if (y == 0) return 1; temp = power(x, y / 2); if (y % 2 == 0) return temp * temp; else { if (y > 0) return x * temp * temp; else return (temp * temp) / x; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); din(n) ll a[n]; ll sum = 0, max = 0; for (ll i = 0; i < n; i++) { in(a[i]); sum += a[i]; } ll hash[10001] = {0}; for (ll i = 0; i < n; i++) hash[a[i]]++; din(q); vector<ll> ans; for (ll i = 0; i < q; i++) { din(b); din(c); sum -= hash[b] * b; sum += hash[b] * c; hash[c] += hash[b]; hash[b] = 0; ans.push_back(sum); } for (ll i = 0; i < q; i++) cout << ans[i] << endl; return 0; }
// By Zank100 #include <bits/stdc++.h> using namespace std; #define ll long long #define ull unsigned long long #define mod 1000000007 #define din(t) \ long long t; \ cin >> t; #define in(t) cin >> t; ll mul(ll a, ll b) { return (((a % mod) * (b % mod)) % mod); } ll modexp(ll b, ll e) { ll res = 1; for (; e; b = b * b % mod, e >>= 1) if (e & 1) res = res * b % mod; return res; } ll modinv(ll a) { ll x = mod - 2; return modexp(a, x); } ll power(ll x, ll y) { ll temp; if (y == 0) return 1; temp = power(x, y / 2); if (y % 2 == 0) return temp * temp; else { if (y > 0) return x * temp * temp; else return (temp * temp) / x; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); din(n) ll a[n]; ll sum = 0, max = 0; for (ll i = 0; i < n; i++) { in(a[i]); sum += a[i]; } ll hash[100001] = {0}; for (ll i = 0; i < n; i++) hash[a[i]]++; din(q); vector<ll> ans; for (ll i = 0; i < q; i++) { din(b); din(c); sum -= hash[b] * b; sum += hash[b] * c; hash[c] += hash[b]; hash[b] = 0; ans.push_back(sum); } for (ll i = 0; i < q; i++) cout << ans[i] << endl; return 0; }
replace
45
46
45
46
0
p02630
C++
Runtime Error
#include <iostream> #include <memory.h> using namespace std; int a[10001]; int main() { memset(a, 0, sizeof(a)); int n; cin >> n; long long int count = 0; for (int i = 0; i < n; i++) { int b; cin >> b; count += b; a[b]++; } int q; cin >> q; for (int i = 0; i < q; i++) { int m, n; cin >> m >> n; a[n] += a[m]; int c = a[m]; a[m] = 0; count = count + c * (n - m); cout << count << endl; } }
#include <iostream> #include <memory.h> using namespace std; int a[1000001]; int main() { memset(a, 0, sizeof(a)); int n; cin >> n; long long int count = 0; for (int i = 0; i < n; i++) { int b; cin >> b; count += b; a[b]++; } int q; cin >> q; for (int i = 0; i < q; i++) { int m, n; cin >> m >> n; a[n] += a[m]; int c = a[m]; a[m] = 0; count = count + c * (n - m); cout << count << endl; } }
replace
3
4
3
4
0
p02630
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { ios::sync_with_stdio(false); cin.tie(0); ll n; cin >> n; vector<ll> a(n, 0); for (ll i = 0; i < n; i++) cin >> a[i]; ll sum = 0; sum = accumulate(a.begin(), a.end(), 0LL); ll q; cin >> q; vector<ll> b(n, 0), c(n, 0); for (ll i = 0; i < q; i++) cin >> b[i] >> c[i]; vector<ll> dp(1000000, 0); for (ll i = 0; i < n; i++) { dp[a[i]] += 1; } ll ans = sum; for (ll i = 0; i < q; i++) { ans = ans - (b[i] * dp[b[i]]) + (c[i] * dp[b[i]]); dp[c[i]] = dp[c[i]] + dp[b[i]]; dp[b[i]] = 0; cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { ios::sync_with_stdio(false); cin.tie(0); ll n; cin >> n; vector<ll> a(n, 0); for (ll i = 0; i < n; i++) cin >> a[i]; ll sum = 0; sum = accumulate(a.begin(), a.end(), 0LL); ll q; cin >> q; vector<ll> b(q, 0), c(q, 0); for (ll i = 0; i < q; i++) cin >> b[i] >> c[i]; vector<ll> dp(1000000, 0); for (ll i = 0; i < n; i++) { dp[a[i]] += 1; } ll ans = sum; for (ll i = 0; i < q; i++) { ans = ans - (b[i] * dp[b[i]]) + (c[i] * dp[b[i]]); dp[c[i]] = dp[c[i]] + dp[b[i]]; dp[b[i]] = 0; cout << ans << endl; } return 0; }
replace
17
18
17
18
0
p02630
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) #define rep2(i, n) for (int i = 1; i <= (n); ++i) #define all(x) (x).begin(), (x).end() #define Sort(x) sort((x).begin(), (x).end()) #define Sort2(x) sort((x).begin(), (x).end(), greater<int>()) #define pb push_back using namespace std; using ll = long long; using P = pair<int, int>; using Graph = vector<vector<int>>; int main() { ll n; cin >> n; vector<ll> a(n + 1000); rep(i, n) cin >> a[i]; ll q; cin >> q; ll sum = 0; rep(i, n) sum += a[i]; vector<ll> cnt(n + 1000, 0); rep(i, n) { cnt[a[i]]++; } rep(i, q) { // rep(i,n) cout << cnt[i] << " "; // cout << endl; ll b, c; cin >> b >> c; ll d = c - b; // bの数を数える sum += cnt[b] * d; cout << sum << endl; cnt[c] += cnt[b]; cnt[b] = 0; } } /* Ctrl+@ でコンソールを開け、以下で実行。 g++ d.cpp ./a.out サンプルのダウンロードは oj d https://atcoder.jp/contests/abc171/tasks/abc171_d テストは oj t */
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) #define rep2(i, n) for (int i = 1; i <= (n); ++i) #define all(x) (x).begin(), (x).end() #define Sort(x) sort((x).begin(), (x).end()) #define Sort2(x) sort((x).begin(), (x).end(), greater<int>()) #define pb push_back using namespace std; using ll = long long; using P = pair<int, int>; using Graph = vector<vector<int>>; int main() { ll n; cin >> n; vector<ll> a(n + 1000); rep(i, n) cin >> a[i]; ll q; cin >> q; ll sum = 0; rep(i, n) sum += a[i]; vector<ll> cnt(100007, 0); rep(i, n) { cnt[a[i]]++; } rep(i, q) { // rep(i,n) cout << cnt[i] << " "; // cout << endl; ll b, c; cin >> b >> c; ll d = c - b; // bの数を数える sum += cnt[b] * d; cout << sum << endl; cnt[c] += cnt[b]; cnt[b] = 0; } } /* Ctrl+@ でコンソールを開け、以下で実行。 g++ d.cpp ./a.out サンプルのダウンロードは oj d https://atcoder.jp/contests/abc171/tasks/abc171_d テストは oj t */
replace
21
22
21
22
0
p02630
C++
Runtime Error
#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() { int n; cin >> n; vector<int> a(n); vector<int> num(100005); ll sum = 0; rep(i, n) { cin >> a[i]; sum += a[i]; num[a[i]]++; } int q; cin >> q; vector<int> b(n), c(n); rep(i, q) cin >> b[i] >> c[i]; rep(i, q) { sum += num[b[i]] * (c[i] - b[i]); num[c[i]] += num[b[i]]; num[b[i]] = 0; cout << sum << endl; } return 0; }
#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() { int n; cin >> n; vector<int> a(n); vector<int> num(100005); ll sum = 0; rep(i, n) { cin >> a[i]; sum += a[i]; num[a[i]]++; } int q; cin >> q; vector<int> b(q), c(q); rep(i, q) cin >> b[i] >> c[i]; rep(i, q) { sum += num[b[i]] * (c[i] - b[i]); num[c[i]] += num[b[i]]; num[b[i]] = 0; cout << sum << endl; } return 0; }
replace
19
20
19
20
0
p02630
C++
Runtime Error
#include <bits/stdc++.h> #include <cmath> #include <math.h> using namespace std; using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vl = vector<ll>; using vvl = vector<vl>; using vs = vector<string>; using vvs = vector<vs>; using vc = vector<char>; using vvc = vector<vc>; using vb = vector<bool>; using vvb = vector<vb>; #define rep(i, n) \ ; \ for (int i = 0; i < n; i++) #define all(a) a.begin(), a.end() #define pb(a) push_back(a) #define pd(a) printf("%.10f\n", a) #define mem(a) memset(a, 0, sizeof(a)) #define f(i, a, b) for (int i = a; i < b; i++) #define pri(a) printf("%.14lf\n", a); #define sz(a) ((int)a.size()) #define MOD 1000000007 bool is_int_lround(double x) { return std::lround(x) == x; } ll keta(ll x) { ll n = 0; while (x > 0) { x /= 10; n++; } return n; } ll conbi(int n, int m) { cin >> n >> m; vector<ll> a(100); a[0] = 1; for (int i = 0; i < 14; i++) { a[i + 1] = a[i] * (i + 1); } return a[n] / (a[m] * a[n - m]); } long long modpow(long long a, long long n, long long mod) { long long res = 1; // 繰り返し二乗法 while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } ll kaijoMOD(ll x) { ll z = 1; if (x == 0) { return 1; } while (x > 0) { z *= x; z %= MOD; x--; } return z; } ll div_num(ll n) { vl div(n + 1, 1); for (ll i = 2; i * i <= n; i++) { while (n % i == 0) { n /= i; div[i]++; } } if (n != 1) div[n]++; ll num = 1; for (ll i = 0; i <= n; i++) { num *= div[i]; } return num; } vector<ll> divisor(ll n) { vector<ll> ret; for (ll i = 1; i * i <= n; i++) { if (n % i == 0) { ret.push_back(i); if (i * i != n) ret.push_back(n / i); } } sort(begin(ret), end(ret)); return (ret); } int keta_wa(int x) { int tot = 0; while (x > 0) { tot += x % 10; x /= 10; } return tot; } long long lopow(ll a, long long n) { long long res = 1; while (n > 0) { if (n & 1) res = res * a; a = a * a; n >>= 1; } return res; } // vc alp // ={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; // vc Alp // ={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}; // cout<<""<<endl; int main() { ll N, Q; cin >> N; vl A(N); rep(i, N) cin >> A[i]; cin >> Q; vl B(Q), C(Q); rep(i, Q) cin >> B[i] >> C[i]; ll tot = 0; rep(i, N) tot += A[i]; vl num(N + 1); rep(i, N) { num[A[i]]++; } rep(i, Q) { num[C[i]] += num[B[i]]; tot += (C[i] - B[i]) * num[B[i]]; num[B[i]] = 0; cout << tot << endl; } }
#include <bits/stdc++.h> #include <cmath> #include <math.h> using namespace std; using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vl = vector<ll>; using vvl = vector<vl>; using vs = vector<string>; using vvs = vector<vs>; using vc = vector<char>; using vvc = vector<vc>; using vb = vector<bool>; using vvb = vector<vb>; #define rep(i, n) \ ; \ for (int i = 0; i < n; i++) #define all(a) a.begin(), a.end() #define pb(a) push_back(a) #define pd(a) printf("%.10f\n", a) #define mem(a) memset(a, 0, sizeof(a)) #define f(i, a, b) for (int i = a; i < b; i++) #define pri(a) printf("%.14lf\n", a); #define sz(a) ((int)a.size()) #define MOD 1000000007 bool is_int_lround(double x) { return std::lround(x) == x; } ll keta(ll x) { ll n = 0; while (x > 0) { x /= 10; n++; } return n; } ll conbi(int n, int m) { cin >> n >> m; vector<ll> a(100); a[0] = 1; for (int i = 0; i < 14; i++) { a[i + 1] = a[i] * (i + 1); } return a[n] / (a[m] * a[n - m]); } long long modpow(long long a, long long n, long long mod) { long long res = 1; // 繰り返し二乗法 while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } ll kaijoMOD(ll x) { ll z = 1; if (x == 0) { return 1; } while (x > 0) { z *= x; z %= MOD; x--; } return z; } ll div_num(ll n) { vl div(n + 1, 1); for (ll i = 2; i * i <= n; i++) { while (n % i == 0) { n /= i; div[i]++; } } if (n != 1) div[n]++; ll num = 1; for (ll i = 0; i <= n; i++) { num *= div[i]; } return num; } vector<ll> divisor(ll n) { vector<ll> ret; for (ll i = 1; i * i <= n; i++) { if (n % i == 0) { ret.push_back(i); if (i * i != n) ret.push_back(n / i); } } sort(begin(ret), end(ret)); return (ret); } int keta_wa(int x) { int tot = 0; while (x > 0) { tot += x % 10; x /= 10; } return tot; } long long lopow(ll a, long long n) { long long res = 1; while (n > 0) { if (n & 1) res = res * a; a = a * a; n >>= 1; } return res; } // vc alp // ={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; // vc Alp // ={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}; // cout<<""<<endl; int main() { ll N, Q; cin >> N; vl A(N); rep(i, N) cin >> A[i]; cin >> Q; vl B(Q), C(Q); rep(i, Q) cin >> B[i] >> C[i]; ll tot = 0; rep(i, N) tot += A[i]; vl num(100001); rep(i, N) { num[A[i]]++; } rep(i, Q) { num[C[i]] += num[B[i]]; tot += (C[i] - B[i]) * num[B[i]]; num[B[i]] = 0; cout << tot << endl; } }
replace
135
136
135
136
0
p02630
C++
Runtime Error
#include <stdio.h> #include <string.h> using ll = long long; int main() { int N; scanf("%d", &N); static ll dat[100001]; ll ans = 0; for (int i = 0; i < N; i++) { int A; scanf("%lld", &A); dat[A]++; ans += A; } int Q; scanf("%d", &Q); for (int q = 0; q < Q; q++) { int B, C; scanf("%d%d", &B, &C); ll diff = dat[B] * (C - B); ans += diff; if (B != C) { dat[C] += dat[B]; dat[B] = 0; } printf("%lld\n", ans); } return 0; }
#include <stdio.h> #include <string.h> using ll = long long; int main() { int N; scanf("%d", &N); static ll dat[100001]; ll ans = 0; for (int i = 0; i < N; i++) { ll A; scanf("%lld", &A); dat[A]++; ans += A; } int Q; scanf("%d", &Q); for (int q = 0; q < Q; q++) { int B, C; scanf("%d%d", &B, &C); ll diff = dat[B] * (C - B); ans += diff; if (B != C) { dat[C] += dat[B]; dat[B] = 0; } printf("%lld\n", ans); } return 0; }
replace
11
12
11
12
0
p02630
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) int main() { int N, Q, B, C, T; long long Sum = 0; cin >> N; vector<int> A(100000, 0); rep(i, N) { cin >> T; A.at(T)++; Sum += T; } cin >> Q; rep(i, Q) { cin >> B >> C; Sum += A.at(B) * (C - B); A.at(C) += A.at(B); A.at(B) = 0; cout << Sum << endl; } }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) int main() { int N, Q, B, C, T; long long Sum = 0; cin >> N; vector<int> A(100001, 0); rep(i, N) { cin >> T; A.at(T)++; Sum += T; } cin >> Q; rep(i, Q) { cin >> B >> C; Sum += A.at(B) * (C - B); A.at(C) += A.at(B); A.at(B) = 0; cout << Sum << endl; } }
replace
8
9
8
9
0
p02630
C++
Runtime Error
#include <algorithm> #include <bits/stdc++.h> #include <string> using namespace std; int main() { long long n; cin >> n; vector<int> count(10001, 0); vector<int> v(n); long long sum = 0; for (auto i : v) { cin >> i; sum += i; count[i]++; } int q; cin >> q; for (int i = 0; i < q; i++) { int b, c; cin >> b >> c; count[c] += count[b]; sum += (c - b) * count[b]; count[b] = 0; cout << sum << endl; } return 0; }
#include <algorithm> #include <bits/stdc++.h> #include <string> using namespace std; int main() { long long n; cin >> n; vector<int> count(100001, 0); vector<int> v(n); long long sum = 0; for (auto i : v) { cin >> i; sum += i; count[i]++; } int q; cin >> q; for (int i = 0; i < q; i++) { int b, c; cin >> b >> c; count[c] += count[b]; sum += (c - b) * count[b]; count[b] = 0; cout << sum << endl; } return 0; }
replace
9
10
9
10
0
p02630
C++
Runtime Error
#include <bits/stdc++.h> #define drep(i, cc, n) for (int i = (cc); i <= (n); ++i) #define rep(i, n) drep(i, 0, n - 1) #define drrep(i, cc, n) for (int i = (cc); i >= (n); --i) #define rrep(i, n) drrep(i, 0, n - 1) #define sz(s) (int)(s.size()) #define vecprint(v) \ rep(i, v.size()) if (i == 0) cout << v[i]; \ else cout << " " << v[i]; \ cout << endl; #define arrprint(v, n) \ rep(i, n) if (i == 0) cout << v[i]; \ else cout << " " << v[i]; \ cout << endl; #define matprint(v, n, m) \ rep(i, n) { \ rep(j, m) { cout << v[i][j] << " "; } \ cout << endl; \ } #define SORT(v) sort(v.begin(), v.end()) #define REV(v) reverse(v.begin(), v.end()) using namespace std; const int mod = 1000000007; const int INF = 1001001001; using ll = long long; using P = pair<int, int>; template <typename T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>; // 小さいものから取り出す int main() { int n; cin >> n; vector<int> a(n); rep(i, n) cin >> a[i]; int q; cin >> q; vector<int> b(q), c(q); rep(i, q) cin >> b[i] >> c[i]; vector<int> cnt(n + 1); ll ans = 0; rep(i, n) { cnt[a[i]]++; ans += a[i]; } rep(i, q) { ll minus = (ll)b[i] * cnt[b[i]]; ll add = (ll)c[i] * cnt[b[i]]; cnt[c[i]] += cnt[b[i]]; cnt[b[i]] = 0; ans = ans + add - minus; cout << ans << endl; } // cout << ans << endl; // printf("%.10f\n",ans); return 0; }
#include <bits/stdc++.h> #define drep(i, cc, n) for (int i = (cc); i <= (n); ++i) #define rep(i, n) drep(i, 0, n - 1) #define drrep(i, cc, n) for (int i = (cc); i >= (n); --i) #define rrep(i, n) drrep(i, 0, n - 1) #define sz(s) (int)(s.size()) #define vecprint(v) \ rep(i, v.size()) if (i == 0) cout << v[i]; \ else cout << " " << v[i]; \ cout << endl; #define arrprint(v, n) \ rep(i, n) if (i == 0) cout << v[i]; \ else cout << " " << v[i]; \ cout << endl; #define matprint(v, n, m) \ rep(i, n) { \ rep(j, m) { cout << v[i][j] << " "; } \ cout << endl; \ } #define SORT(v) sort(v.begin(), v.end()) #define REV(v) reverse(v.begin(), v.end()) using namespace std; const int mod = 1000000007; const int INF = 1001001001; using ll = long long; using P = pair<int, int>; template <typename T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>; // 小さいものから取り出す int main() { int n; cin >> n; vector<int> a(n); rep(i, n) cin >> a[i]; int q; cin >> q; vector<int> b(q), c(q); rep(i, q) cin >> b[i] >> c[i]; vector<int> cnt(100005); ll ans = 0; rep(i, n) { cnt[a[i]]++; ans += a[i]; } rep(i, q) { ll minus = (ll)b[i] * cnt[b[i]]; ll add = (ll)c[i] * cnt[b[i]]; cnt[c[i]] += cnt[b[i]]; cnt[b[i]] = 0; ans = ans + add - minus; cout << ans << endl; } // cout << ans << endl; // printf("%.10f\n",ans); return 0; }
replace
39
40
39
40
0
p02630
C++
Time Limit Exceeded
/* ------------------------------------------CAUTIOUS WARNING ------------------------------------------ This is a personalised Template for Nitin under MIT Licence Act 1840 : Do not use it for own use. */ // AUTHOR Nitin // TASK : Practice //--------------------------------HEADERS-------------------------------------------------------------// #include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <limits> #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> using namespace std; //-------------------------------------------------------Typedefs----------------------------------------------// typedef long long int ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<string, string> pss; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<pii> vii; typedef vector<ll> vl; typedef vector<vl> vvl; double EPS = 1e-9; int INF = 1000000005; long long INFF = 1000000000000000005LL; double PI = acos(-1); int dirx[8] = {-1, 0, 0, 1, -1, -1, 1, 1}; int diry[8] = {0, 1, -1, 0, -1, 1, -1, 1}; //---------------------------------------------------------Hash //defines--------------------------------------------------// #define PII 3.14159265358979323846 #ifdef TESTING #define DEBUG fprintf(stderr, "====TESTING====\n") #define VALUE(x) cerr << "The value of " << #x << " is " << x << endl #define debug(...) fprintf(stderr, __VA_ARGS__) #else #define DEBUG #define VALUE(x) #define debug(...) #endif #define FOR(a, b, c) for (int(a) = (b); (a) < (c); ++(a)) #define FORN(a, b, c) for (int(a) = (b); (a) <= (c); ++(a)) #define FORD(a, b, c) for (int(a) = (b); (a) >= (c); --(a)) #define FORSQ(a, b, c) for (int(a) = (b); (a) * (a) <= (c); ++(a)) #define FORC(a, b, c) for (char(a) = (b); (a) <= (c); ++(a)) #define FOREACH(a, b) for (auto &(a) : (b)) #define REP(i, n) FOR(i, 0, n) #define REPN(i, n) FORN(i, 1, n) #define MAX(a, b) a = max(a, b) #define MIN(a, b) a = min(a, b) #define SQR(x) ((ll)(x) * (x)) #define RESET(a, b) memset(a, b, sizeof(a)) #define ff first #define ss second #define mp make_pair #define pb push_back #define ALL(v) v.begin(), v.end() #define ALLA(arr, sz) arr, arr + sz #define SIZE(v) (int)v.size() #define SORT(v) sort(ALL(v)) #define REVERSE(v) reverse(ALL(v)) #define SORTA(arr, sz) sort(ALLA(arr, sz)) #define REVERSEA(arr, sz) reverse(ALLA(arr, sz)) #define PERMUTE next_permutation #define T(a) \ ll a; \ cin >> a; #define TT(t) \ T(t); \ while (t--) #define IOS \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define f(i, x, y) for (int i = x; i < y; i++) #define array(a) ll a[n]; #define line "\n"; #define out cout #define meh << //-------------------------------------------------TEMPLATES----------------------------------------------------------// template <class T> T max(T a, T b, T c) { return max(a, max(b, c)); } template <class T> T min(T a, T b, T c) { return min(a, min(b, c)); } void SieveOfEratosthenes(ll n) // O(nloglogn) { ll prime[n]; memset(prime, true, sizeof(prime)); for (ll p = 2; p * p <= n; p++) { if (prime[p] == true) { for (ll i = p * p; i <= n; i += p) prime[i] = false; } } } bool isPrime(ll n) // O(sqrt(n)) { if (n < 2) return false; for (ll i = 2; i * i <= n; i++) if (n % i == 0) return false; return true; } std::vector<ll> generatePrimeFactors(ll n) { std::vector<ll> prime; for (ll i = 2; i * i <= n; i++) { if (n % i == 0) { prime.pb(i); while (n % i == 0) n = n / i; } } if (n != 1) prime.pb(n); return prime; } std::vector<ll> generateFactors(ll n) { std::vector<ll> fact; for (ll i = 2; i * i <= n; i++) { if (n % i == 0) { fact.pb(i); if (n / i != i) fact.pb(n / i); // 24 - 1,2,3,4,6,8,12 } } fact.pb(1); if (n != 1) fact.pb(n); sort(fact.begin(), fact.end()); return fact; } ll extendedGCD(ll a, ll b, ll &x, ll &y) { // produces correct results for negative integers as well if (a == 0) { x = 0; y = 1; return b; } ll x1, y1, d; d = extendedGCD(b % a, a, x1, y1); x = y1 - (b / a) * x1; y = x1; return d; } ll gcd(ll a, ll b) // O(log min(a,b)) { /* recursive implementation below if(!b) return a; return gcd(b,a%b); */ // non-recursive implementation below while (b) { a %= b; swap(a, b); } return a; } ll multiply(ll a, ll b, ll m); ll singlemod(ll a, ll m); ll modpow(ll x, ll n, ll m) // O(logn)// used for calculating (x^n)%m { if (n == 0) return 1; ll res = 1; while (n) { if (n % 2) res = multiply(res, x, m); x = multiply(x, x, m); n = n >> 1; } return singlemod(res, m); } ll fastpow(ll x, ll n) { if (n == 0) return 1; ll res = 1; while (n) { if (n % 2) res = res * x; x = x * x; n = n >> 1; } return res; } ll modinverse(ll x, ll m) // O(logn) { return modpow(x, m - 2, m); } ll add(ll a, ll b, ll m) { return (((a % m) + (b % m)) % m); } ll substract(ll a, ll b, ll m) { // if (a < b) // swap(a, b); return (((a % m) - (b % m) + m) % m); } ll multiply(ll a, ll b, ll m) { return (((a % m) * (b % m)) % m); } ll divide(ll a, ll b, ll m) { ll temp = modinverse(b, m); return multiply(a, temp, m); } bool isprime(ll n) { for (int i = 2; i <= sqrt(n); i++) { if (n % i == 0) return false; } return true; } ll singlemod(ll a, ll m) { return (a % m); } ll eulerstotientfunction(ll n) // O(sqrt(n)) { ll ans = n; for (ll i = 2; i * i <= n; i++) { if (n % i == 0) { while (n % i == 0) n = n / i; ans -= ans / i; } } if (n > 1) ans -= ans / n; return ans; } ll ncr(ll n, ll k, ll m) { if (k > n) return 0; if (m == 0) { ll res = 1; k = min(k, n - k); for (ll i = 1; i <= k; i++) { res *= (n - i + 1); res /= i; } return res; } else { ll res = 1; k = min(k, n - k); for (ll i = 1; i <= k; i++) { res = multiply(res, n - i + 1, m); res = divide(res, i, m); } return singlemod(res, m); } } ll kadaneAlgo(const std::vector<ll> &arr) { ll size = arr.size(); ll currmax = arr[0], maxsofar = arr[0]; for (ll i = 1; i < size; i++) { // debug2(currmax,maxsofar); currmax = max(arr[i], currmax + arr[i]); maxsofar = max(maxsofar, currmax); } return maxsofar; } ll getDigitSum(ll n) { ll co = 0; while (n > 0) { co += n % 10; n = n / 10; } return co; } template <class T> void printVector(const std::vector<T> arr) { for (auto i : arr) cout << i << " "; cout << endl; } template <class T> void printArray(T arr[], ll n) { for (ll i = 0; i < n; i++) cout << arr[i] << " "; cout << endl; } // const int p1 = 13331,m1 = 1e9+9,p2 = 7919, m2 = 1e9+9; // const int p1 = 97266353,m1 = 972663749,p2 = 97336357, m2 = 1000000007; // const int p1 = 31 ,m1 = 1e9+7; // grid cells // D U R L ll dx[] = {1, -1, 0, 0}; ll dy[] = {0, 0, 1, -1}; bool is_perfect(int x) { long double y = sqrt(x); return ((y - floor(y)) == 0); } // this is a practice header done during Practice session -- ll nofdigit(ll n) { // formula used number of digits in N = log10(N) + 1 .//O(1) return floor(log10(n) + 1); } ll gp_lastterm(ll a1, ll n, ll r) { return a1 * pow(r, n - 1); } ll gp_sum(ll a1, ll n, ll r) { if (r > 1) return (a1 * (pow(r, n) - 1)) / (r - 1); else return (a1 * (1 - pow(r, n))) / (1 - r); } //------------------------------------------------------------------------------------------------------------ void wiley() { T(n) array(a) REP(i, n) cin >> a[i]; ll sum = 0; T(q) while (q--) { T(b) T(c) REP(i, n) { if (a[i] == b) { a[i] = c; } } REP(i, n) { sum = sum + a[i]; } cout << sum meh line sum = 0; } } int main() { // Fast I/O --- Vroom Vroom // IOS //// TT(t){ // read(); wiley(); // By Order of Peaky Blinders. // } return 0; }
/* ------------------------------------------CAUTIOUS WARNING ------------------------------------------ This is a personalised Template for Nitin under MIT Licence Act 1840 : Do not use it for own use. */ // AUTHOR Nitin // TASK : Practice //--------------------------------HEADERS-------------------------------------------------------------// #include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <limits> #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> using namespace std; //-------------------------------------------------------Typedefs----------------------------------------------// typedef long long int ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<string, string> pss; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<pii> vii; typedef vector<ll> vl; typedef vector<vl> vvl; double EPS = 1e-9; int INF = 1000000005; long long INFF = 1000000000000000005LL; double PI = acos(-1); int dirx[8] = {-1, 0, 0, 1, -1, -1, 1, 1}; int diry[8] = {0, 1, -1, 0, -1, 1, -1, 1}; //---------------------------------------------------------Hash //defines--------------------------------------------------// #define PII 3.14159265358979323846 #ifdef TESTING #define DEBUG fprintf(stderr, "====TESTING====\n") #define VALUE(x) cerr << "The value of " << #x << " is " << x << endl #define debug(...) fprintf(stderr, __VA_ARGS__) #else #define DEBUG #define VALUE(x) #define debug(...) #endif #define FOR(a, b, c) for (int(a) = (b); (a) < (c); ++(a)) #define FORN(a, b, c) for (int(a) = (b); (a) <= (c); ++(a)) #define FORD(a, b, c) for (int(a) = (b); (a) >= (c); --(a)) #define FORSQ(a, b, c) for (int(a) = (b); (a) * (a) <= (c); ++(a)) #define FORC(a, b, c) for (char(a) = (b); (a) <= (c); ++(a)) #define FOREACH(a, b) for (auto &(a) : (b)) #define REP(i, n) FOR(i, 0, n) #define REPN(i, n) FORN(i, 1, n) #define MAX(a, b) a = max(a, b) #define MIN(a, b) a = min(a, b) #define SQR(x) ((ll)(x) * (x)) #define RESET(a, b) memset(a, b, sizeof(a)) #define ff first #define ss second #define mp make_pair #define pb push_back #define ALL(v) v.begin(), v.end() #define ALLA(arr, sz) arr, arr + sz #define SIZE(v) (int)v.size() #define SORT(v) sort(ALL(v)) #define REVERSE(v) reverse(ALL(v)) #define SORTA(arr, sz) sort(ALLA(arr, sz)) #define REVERSEA(arr, sz) reverse(ALLA(arr, sz)) #define PERMUTE next_permutation #define T(a) \ ll a; \ cin >> a; #define TT(t) \ T(t); \ while (t--) #define IOS \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define f(i, x, y) for (int i = x; i < y; i++) #define array(a) ll a[n]; #define line "\n"; #define out cout #define meh << //-------------------------------------------------TEMPLATES----------------------------------------------------------// template <class T> T max(T a, T b, T c) { return max(a, max(b, c)); } template <class T> T min(T a, T b, T c) { return min(a, min(b, c)); } void SieveOfEratosthenes(ll n) // O(nloglogn) { ll prime[n]; memset(prime, true, sizeof(prime)); for (ll p = 2; p * p <= n; p++) { if (prime[p] == true) { for (ll i = p * p; i <= n; i += p) prime[i] = false; } } } bool isPrime(ll n) // O(sqrt(n)) { if (n < 2) return false; for (ll i = 2; i * i <= n; i++) if (n % i == 0) return false; return true; } std::vector<ll> generatePrimeFactors(ll n) { std::vector<ll> prime; for (ll i = 2; i * i <= n; i++) { if (n % i == 0) { prime.pb(i); while (n % i == 0) n = n / i; } } if (n != 1) prime.pb(n); return prime; } std::vector<ll> generateFactors(ll n) { std::vector<ll> fact; for (ll i = 2; i * i <= n; i++) { if (n % i == 0) { fact.pb(i); if (n / i != i) fact.pb(n / i); // 24 - 1,2,3,4,6,8,12 } } fact.pb(1); if (n != 1) fact.pb(n); sort(fact.begin(), fact.end()); return fact; } ll extendedGCD(ll a, ll b, ll &x, ll &y) { // produces correct results for negative integers as well if (a == 0) { x = 0; y = 1; return b; } ll x1, y1, d; d = extendedGCD(b % a, a, x1, y1); x = y1 - (b / a) * x1; y = x1; return d; } ll gcd(ll a, ll b) // O(log min(a,b)) { /* recursive implementation below if(!b) return a; return gcd(b,a%b); */ // non-recursive implementation below while (b) { a %= b; swap(a, b); } return a; } ll multiply(ll a, ll b, ll m); ll singlemod(ll a, ll m); ll modpow(ll x, ll n, ll m) // O(logn)// used for calculating (x^n)%m { if (n == 0) return 1; ll res = 1; while (n) { if (n % 2) res = multiply(res, x, m); x = multiply(x, x, m); n = n >> 1; } return singlemod(res, m); } ll fastpow(ll x, ll n) { if (n == 0) return 1; ll res = 1; while (n) { if (n % 2) res = res * x; x = x * x; n = n >> 1; } return res; } ll modinverse(ll x, ll m) // O(logn) { return modpow(x, m - 2, m); } ll add(ll a, ll b, ll m) { return (((a % m) + (b % m)) % m); } ll substract(ll a, ll b, ll m) { // if (a < b) // swap(a, b); return (((a % m) - (b % m) + m) % m); } ll multiply(ll a, ll b, ll m) { return (((a % m) * (b % m)) % m); } ll divide(ll a, ll b, ll m) { ll temp = modinverse(b, m); return multiply(a, temp, m); } bool isprime(ll n) { for (int i = 2; i <= sqrt(n); i++) { if (n % i == 0) return false; } return true; } ll singlemod(ll a, ll m) { return (a % m); } ll eulerstotientfunction(ll n) // O(sqrt(n)) { ll ans = n; for (ll i = 2; i * i <= n; i++) { if (n % i == 0) { while (n % i == 0) n = n / i; ans -= ans / i; } } if (n > 1) ans -= ans / n; return ans; } ll ncr(ll n, ll k, ll m) { if (k > n) return 0; if (m == 0) { ll res = 1; k = min(k, n - k); for (ll i = 1; i <= k; i++) { res *= (n - i + 1); res /= i; } return res; } else { ll res = 1; k = min(k, n - k); for (ll i = 1; i <= k; i++) { res = multiply(res, n - i + 1, m); res = divide(res, i, m); } return singlemod(res, m); } } ll kadaneAlgo(const std::vector<ll> &arr) { ll size = arr.size(); ll currmax = arr[0], maxsofar = arr[0]; for (ll i = 1; i < size; i++) { // debug2(currmax,maxsofar); currmax = max(arr[i], currmax + arr[i]); maxsofar = max(maxsofar, currmax); } return maxsofar; } ll getDigitSum(ll n) { ll co = 0; while (n > 0) { co += n % 10; n = n / 10; } return co; } template <class T> void printVector(const std::vector<T> arr) { for (auto i : arr) cout << i << " "; cout << endl; } template <class T> void printArray(T arr[], ll n) { for (ll i = 0; i < n; i++) cout << arr[i] << " "; cout << endl; } // const int p1 = 13331,m1 = 1e9+9,p2 = 7919, m2 = 1e9+9; // const int p1 = 97266353,m1 = 972663749,p2 = 97336357, m2 = 1000000007; // const int p1 = 31 ,m1 = 1e9+7; // grid cells // D U R L ll dx[] = {1, -1, 0, 0}; ll dy[] = {0, 0, 1, -1}; bool is_perfect(int x) { long double y = sqrt(x); return ((y - floor(y)) == 0); } // this is a practice header done during Practice session -- ll nofdigit(ll n) { // formula used number of digits in N = log10(N) + 1 .//O(1) return floor(log10(n) + 1); } ll gp_lastterm(ll a1, ll n, ll r) { return a1 * pow(r, n - 1); } ll gp_sum(ll a1, ll n, ll r) { if (r > 1) return (a1 * (pow(r, n) - 1)) / (r - 1); else return (a1 * (1 - pow(r, n))) / (1 - r); } //------------------------------------------------------------------------------------------------------------ void wiley() { map<int, int> mp; ll n, sum = 0, x, y; cin >> n; ll a[n]; REP(i, n) { cin >> a[i]; mp[a[i]]++; sum += a[i]; } int q; cin >> q; REP(i, q) { cin >> x >> y; mp[y] += mp[x]; sum -= (mp[x] * x); sum += (mp[x] * y); mp[x] = 0; cout << sum << endl; } } int main() { // Fast I/O --- Vroom Vroom // IOS //// TT(t){ // read(); wiley(); // By Order of Peaky Blinders. // } return 0; }
replace
350
364
350
368
TLE