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
p02983
C++
Time Limit Exceeded
#define DEBUG 1 #include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <cstring> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <tuple> #include <utility> #include <vector> using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; using vll = vector<ll>; using vvll = vector<vll>; using pll = pair<ll, ll>; using vpll = vector<pll>; using vvpll = vector<vpll>; using tll = tuple<ll, ll, ll>; using vtll = vector<tll>; using vvtll = vector<vtll>; #define all(v) (v).begin(), (v).end() #define for1(i, n) for (ll i = 0; i < (n); i++) #define for2(i, m, n) for (ll i = (m); i < (n); i++) #define for3(i, m, n, d) for (ll i = (m); i < (n); i += (d)) #define rfor2(i, m, n) for (ll i = (m); i > (n); i--) #define rfor3(i, m, n, d) for (ll i = (m); i > (n); i += (d)) #define PI 3.1415926535897932384626433832795028841971693993751L #define INF 1111111111111111111LL #define print(...) print_1(__VA_ARGS__) #define in(...) in_1(__VA_ARGS__) #if DEBUG #define dump(...) dump_1(#__VA_ARGS__, __VA_ARGS__) #define dumpa(...) dumpa_1(#__VA_ARGS__, __VA_ARGS__) #else #define dump(...) #define dumpa(...) #endif template <typename Head> void dump_1(const char *str, Head &&h) { cerr << str << ": " << h << '\n'; } template <typename Head, typename... Tail> void dump_1(const char *str, Head &&h, Tail &&...t) { while (*str != ',') { cerr << *str++; } cerr << ": " << h << ' '; dump_1(str + 1, t...); } template <typename T> void dumpa_1(const char *str, const T v[], const ll size) { while (*str != ',') { cerr << *str++; } cerr << ": "; for1(i, size) { if (i != 0) { cerr << ' '; } cerr << v[i]; } cerr << '\n'; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &v) { os << v.first << ' ' << v.second; return os; } template <typename T1, typename T2, typename T3> ostream &operator<<(ostream &os, const tuple<T1, T2, T3> &v) { os << get<0>(v) << ' ' << get<1>(v) << ' ' << get<2>(v); return os; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { for (auto it = v.begin(); it != v.end(); it++) { if (it != v.begin()) { os << ' '; } os << *it; } return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &v) { for (auto it = v.begin(); it != v.end(); it++) { if (it != v.begin()) { os << ' '; } os << *it; } return os; } template <typename T> ostream &operator<<(ostream &os, const multiset<T> &v) { for (auto it = v.begin(); it != v.end(); it++) { if (it != v.begin()) { os << ' '; } os << *it; } return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const map<T1, T2> &v) { os << '{'; for (auto it = v.begin(); it != v.end(); it++) { if (it != v.begin()) { os << ", "; } os << it->first << ':' << it->second; } os << '}'; return os; } ll divup(ll nume, ll deno) { assert(nume >= 0); assert(deno > 0); return (nume + deno - 1) / deno; } void Yes(void) { cout << "Yes\n"; } void No(void) { cout << "No\n"; } void YES(void) { cout << "YES\n"; } void NO(void) { cout << "NO\n"; } template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } template <typename T> void vin(vector<T> &v, ll len) { for1(i, len) { cin >> v[i]; } } template <typename Head> void in_1(Head &h) { cin >> h; } template <typename Head, typename... Tail> void in_1(Head &h, Tail &...t) { cin >> h; in_1(t...); } template <typename Head> void print_1(Head &&h) { cout << h << '\n'; } template <typename Head, typename... Tail> void print_1(Head &&h, Tail &&...t) { cout << h << ' '; print_1(t...); } //--------------------------------------------------------- void solve() { ll L, R; in(L, R); if (R - L >= 2019) { print(0); } ll ans = INF; for2(i, L, R + 1) { for2(j, i + 1, R + 1) { chmin(ans, i * j % 2019); } } print(ans); } //--------------------------------------------------------- int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(16); cerr << fixed << setprecision(16); solve(); }
#define DEBUG 1 #include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <cstring> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <tuple> #include <utility> #include <vector> using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; using vll = vector<ll>; using vvll = vector<vll>; using pll = pair<ll, ll>; using vpll = vector<pll>; using vvpll = vector<vpll>; using tll = tuple<ll, ll, ll>; using vtll = vector<tll>; using vvtll = vector<vtll>; #define all(v) (v).begin(), (v).end() #define for1(i, n) for (ll i = 0; i < (n); i++) #define for2(i, m, n) for (ll i = (m); i < (n); i++) #define for3(i, m, n, d) for (ll i = (m); i < (n); i += (d)) #define rfor2(i, m, n) for (ll i = (m); i > (n); i--) #define rfor3(i, m, n, d) for (ll i = (m); i > (n); i += (d)) #define PI 3.1415926535897932384626433832795028841971693993751L #define INF 1111111111111111111LL #define print(...) print_1(__VA_ARGS__) #define in(...) in_1(__VA_ARGS__) #if DEBUG #define dump(...) dump_1(#__VA_ARGS__, __VA_ARGS__) #define dumpa(...) dumpa_1(#__VA_ARGS__, __VA_ARGS__) #else #define dump(...) #define dumpa(...) #endif template <typename Head> void dump_1(const char *str, Head &&h) { cerr << str << ": " << h << '\n'; } template <typename Head, typename... Tail> void dump_1(const char *str, Head &&h, Tail &&...t) { while (*str != ',') { cerr << *str++; } cerr << ": " << h << ' '; dump_1(str + 1, t...); } template <typename T> void dumpa_1(const char *str, const T v[], const ll size) { while (*str != ',') { cerr << *str++; } cerr << ": "; for1(i, size) { if (i != 0) { cerr << ' '; } cerr << v[i]; } cerr << '\n'; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &v) { os << v.first << ' ' << v.second; return os; } template <typename T1, typename T2, typename T3> ostream &operator<<(ostream &os, const tuple<T1, T2, T3> &v) { os << get<0>(v) << ' ' << get<1>(v) << ' ' << get<2>(v); return os; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { for (auto it = v.begin(); it != v.end(); it++) { if (it != v.begin()) { os << ' '; } os << *it; } return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &v) { for (auto it = v.begin(); it != v.end(); it++) { if (it != v.begin()) { os << ' '; } os << *it; } return os; } template <typename T> ostream &operator<<(ostream &os, const multiset<T> &v) { for (auto it = v.begin(); it != v.end(); it++) { if (it != v.begin()) { os << ' '; } os << *it; } return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const map<T1, T2> &v) { os << '{'; for (auto it = v.begin(); it != v.end(); it++) { if (it != v.begin()) { os << ", "; } os << it->first << ':' << it->second; } os << '}'; return os; } ll divup(ll nume, ll deno) { assert(nume >= 0); assert(deno > 0); return (nume + deno - 1) / deno; } void Yes(void) { cout << "Yes\n"; } void No(void) { cout << "No\n"; } void YES(void) { cout << "YES\n"; } void NO(void) { cout << "NO\n"; } template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } template <typename T> void vin(vector<T> &v, ll len) { for1(i, len) { cin >> v[i]; } } template <typename Head> void in_1(Head &h) { cin >> h; } template <typename Head, typename... Tail> void in_1(Head &h, Tail &...t) { cin >> h; in_1(t...); } template <typename Head> void print_1(Head &&h) { cout << h << '\n'; } template <typename Head, typename... Tail> void print_1(Head &&h, Tail &&...t) { cout << h << ' '; print_1(t...); } //--------------------------------------------------------- void solve() { ll L, R; in(L, R); if (R - L >= 2019) { print(0); return; } ll ans = INF; for2(i, L, R + 1) { for2(j, i + 1, R + 1) { chmin(ans, i * j % 2019); } } print(ans); } //--------------------------------------------------------- int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(16); cerr << fixed << setprecision(16); solve(); }
insert
163
163
163
164
TLE
p02983
C++
Time Limit Exceeded
#define _USE_MATH_DEFINES #define _CRT_SECURE_NO_WARNINGS #include <algorithm> #include <bitset> #include <cmath> #include <cstdlib> #include <cstring> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; using ll = long long; using ull = unsigned long long; const ll inf = (ll)2e9 + 1; const ll mod = (ll)1e9 + 7; int main() { ll l, r; cin >> l >> r; ull ans = inf; for (ll i = l; i <= r; i++) { for (ll j = i + 1; j <= r; j++) { ull x = (i * j) % 2019; ans = min(ans, x); if (x == 0) { break; } } } cout << ans << endl; }
#define _USE_MATH_DEFINES #define _CRT_SECURE_NO_WARNINGS #include <algorithm> #include <bitset> #include <cmath> #include <cstdlib> #include <cstring> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; using ll = long long; using ull = unsigned long long; const ll inf = (ll)2e9 + 1; const ll mod = (ll)1e9 + 7; int main() { ll l, r; cin >> l >> r; ull ans = inf; for (ll i = l; i <= r; i++) { for (ll j = i + 1; j <= r; j++) { ull x = (i * j) % 2019; ans = min(ans, x); if (x == 0) { cout << ans << endl; return 0; } } } cout << ans << endl; }
replace
39
40
39
41
TLE
p02983
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define LL long long #define reg register #define debug(x) cerr << #x << " = " << x << endl; #define rep(a, b, c) for (reg int a = (b), a##_end_ = (c); a <= a##_end_; ++a) #define ret(a, b, c) for (reg int a = (b), a##_end_ = (c); a < a##_end_; ++a) #define drep(a, b, c) for (reg int a = (b), a##_end_ = (c); a >= a##_end_; --a) inline int Read() { int res = 0, f = 1; char c; while (c = getchar(), c < 48 || c > 57) if (c == '-') f = 0; do res = (res << 3) + (res << 1) + (c ^ 48); while (c = getchar(), c >= 48 && c <= 57); return f ? res : -res; } template <class T> inline bool Min(T &a, T const &b) { return a > b ? a = b, 1 : 0; } template <class T> inline bool Max(T &a, T const &b) { return a < b ? a = b, 1 : 0; } const int N = 15; char S[5]; int L, R; int main() { L = Read(), R = Read(); if (L + 2018 <= R) return puts("0"); int Ans = 1e9; ret(i, L, R) rep(j, i + 1, R) Min(Ans, ((i % 2019) * (j % 2019)) % 2019); cout << Ans; return 0; }
#include <bits/stdc++.h> using namespace std; #define LL long long #define reg register #define debug(x) cerr << #x << " = " << x << endl; #define rep(a, b, c) for (reg int a = (b), a##_end_ = (c); a <= a##_end_; ++a) #define ret(a, b, c) for (reg int a = (b), a##_end_ = (c); a < a##_end_; ++a) #define drep(a, b, c) for (reg int a = (b), a##_end_ = (c); a >= a##_end_; --a) inline int Read() { int res = 0, f = 1; char c; while (c = getchar(), c < 48 || c > 57) if (c == '-') f = 0; do res = (res << 3) + (res << 1) + (c ^ 48); while (c = getchar(), c >= 48 && c <= 57); return f ? res : -res; } template <class T> inline bool Min(T &a, T const &b) { return a > b ? a = b, 1 : 0; } template <class T> inline bool Max(T &a, T const &b) { return a < b ? a = b, 1 : 0; } const int N = 15; char S[5]; int L, R; int main() { L = Read(), R = Read(); if (L + 2018 <= R) return !puts("0"); int Ans = 1e9; ret(i, L, R) rep(j, i + 1, R) Min(Ans, ((i % 2019) * (j % 2019)) % 2019); cout << Ans; return 0; }
replace
39
40
39
40
0
p02983
C++
Time Limit Exceeded
#include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <complex> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #include <cassert> #include <functional> typedef long long ll; using namespace std; #ifndef LOCAL #define debug(x) ; #else #define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl; template <typename T1, typename T2> ostream &operator<<(ostream &out, const pair<T1, T2> &p) { out << "{" << p.first << ", " << p.second << "}"; return out; } template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) { out << '{'; for (const T &item : v) out << item << ", "; out << "\b\b}"; return out; } #endif #define mod 1000000007 // 1e9+7(prime number) #define INF 1000000000 // 1e9 #define LLINF 2000000000000000000LL // 2e18 #define SIZE 200010 int main() { int L, R; cin >> L >> R; ll ans = LLINF; for (int i = L; i <= R; i++) { for (int j = i + 1; j <= R; j++) { ans = min(ans, (ll)i * j % 2019); if (ans == 0) break; } } cout << ans << endl; return 0; }
#include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <complex> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #include <cassert> #include <functional> typedef long long ll; using namespace std; #ifndef LOCAL #define debug(x) ; #else #define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl; template <typename T1, typename T2> ostream &operator<<(ostream &out, const pair<T1, T2> &p) { out << "{" << p.first << ", " << p.second << "}"; return out; } template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) { out << '{'; for (const T &item : v) out << item << ", "; out << "\b\b}"; return out; } #endif #define mod 1000000007 // 1e9+7(prime number) #define INF 1000000000 // 1e9 #define LLINF 2000000000000000000LL // 2e18 #define SIZE 200010 int main() { int L, R; cin >> L >> R; ll ans = LLINF; for (int i = L; i <= R; i++) { for (int j = i + 1; j <= R; j++) { ans = min(ans, (ll)i * j % 2019); if (ans == 0) break; } if (ans == 0) break; } cout << ans << endl; return 0; }
insert
62
62
62
64
TLE
p02983
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { long long l, r, i, val, j; cin >> l >> r; long long minn = ((l % 2019) * ((l + 1) % 2019)) % 2019; for (i = l; i < r; i++) { for (j = i + 1; j <= r; j++) { val = ((i % 2019) * (j % 2019)) % 2019; if (val < minn) { minn = val; } if (minn == 0) { break; } } } cout << minn; return 0; } // long long result = ((l%2019)*((l+1)%2019))%2019; // for(i=l;i<=r-1;i++){ // if(result == 0){ // break; // } // for(j=i+1;j<=r;j++){ // if(result == 0){ // break; // } // if(result > ((i%2019)*(j%2019))%2019){ // result = ((i%2019)*(j%2019))%2019; // cout<<"i "<<i<<"j: "<<j<<endl; // } // } // } // cout<<result; // return 0; // }
#include <bits/stdc++.h> using namespace std; int main() { long long l, r, i, val, j; cin >> l >> r; long long minn = ((l % 2019) * ((l + 1) % 2019)) % 2019; for (i = l; i < r; i++) { if (minn == 0) { break; } for (j = i + 1; j <= r; j++) { val = ((i % 2019) * (j % 2019)) % 2019; if (val < minn) { minn = val; } if (minn == 0) { break; } } } cout << minn; return 0; } // long long result = ((l%2019)*((l+1)%2019))%2019; // for(i=l;i<=r-1;i++){ // if(result == 0){ // break; // } // for(j=i+1;j<=r;j++){ // if(result == 0){ // break; // } // if(result > ((i%2019)*(j%2019))%2019){ // result = ((i%2019)*(j%2019))%2019; // cout<<"i "<<i<<"j: "<<j<<endl; // } // } // } // cout<<result; // return 0; // }
insert
7
7
7
10
TLE
p02983
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <string> #include <vector> using namespace std; int main() { long long L, R; cin >> L >> R; int N = R - L + 1; // vector<int> mod_list(N,0); int min_mod = 1e9; int max_mod = 0; int buf; for (int ni = 0; ni < N; ni++) { // mod_list[ni] = (L + ni) % 2019; buf = (L + ni) % 2019; if (buf < min_mod) { min_mod = buf; } if (buf > max_mod) { max_mod = buf; } } int ans = 1e9; ; if (min_mod == 0) { ans = 0; } else { for (int i = min_mod; i <= max_mod - 1; i++) { for (int j = i + 1; j <= max_mod; j++) { if ((i * j) % 2019 < ans) { ans = (i * j) % 2019; } } } } printf("%d", ans); return 0; }
#include <algorithm> #include <cmath> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <string> #include <vector> using namespace std; int main() { long long L, R; cin >> L >> R; int N = R - L + 1; // vector<int> mod_list(N,0); int min_mod = 1e9; int max_mod = 0; int buf; for (int ni = 0; ni < N; ni++) { // mod_list[ni] = (L + ni) % 2019; buf = (L + ni) % 2019; if (buf < min_mod) { min_mod = buf; if (min_mod == 0) break; } if (buf > max_mod) { max_mod = buf; } } int ans = 1e9; ; if (min_mod == 0) { ans = 0; } else { for (int i = min_mod; i <= max_mod - 1; i++) { for (int j = i + 1; j <= max_mod; j++) { if ((i * j) % 2019 < ans) { ans = (i * j) % 2019; } } } } printf("%d", ans); return 0; }
insert
29
29
29
31
TLE
p02983
C++
Runtime Error
#include <algorithm> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <numeric> #include <queue> #include <random> #include <set> #include <string.h> #include <tuple> #include <vector> using namespace std; typedef long long ll; const int INF = (1 << 30) - 1; const ll INFLL = (1LL << 61) - 1; const int MOD = 1000000007; #define ALL(a) (a).begin(), (a).end() #define rALL(a) (a).rbegin(), (a).rend() int main() { cin.tie(0); ios::sync_with_stdio(false); ll L, R; cin >> L >> R; vector<int> mp; for (ll i = L; i <= min(L + 2019 + 2018, R); i++) { mp[i % 2019]++; } int ans = INF; for (int i = 0; i < 2019; i++) { for (int j = 0; j < 2019; j++) { if (i == j) { if (mp[i] >= 2) { ans = min(ans, i * j % 2019); } } else { if (mp[j] > 0 && mp[i] > 0) { ans = min(ans, i * j % 2019); } } } } cout << ans << endl; }
#include <algorithm> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <numeric> #include <queue> #include <random> #include <set> #include <string.h> #include <tuple> #include <vector> using namespace std; typedef long long ll; const int INF = (1 << 30) - 1; const ll INFLL = (1LL << 61) - 1; const int MOD = 1000000007; #define ALL(a) (a).begin(), (a).end() #define rALL(a) (a).rbegin(), (a).rend() int main() { cin.tie(0); ios::sync_with_stdio(false); ll L, R; cin >> L >> R; map<int, int> mp; for (ll i = L; i <= min(L + 2019 + 2018, R); i++) { mp[i % 2019]++; } int ans = INF; for (int i = 0; i < 2019; i++) { for (int j = 0; j < 2019; j++) { if (i == j) { if (mp[i] >= 2) { ans = min(ans, i * j % 2019); } } else { if (mp[j] > 0 && mp[i] > 0) { ans = min(ans, i * j % 2019); } } } } cout << ans << endl; }
replace
28
29
28
29
-11
p02983
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cmath> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <string> #include <tuple> #include <utility> #include <vector> using ll = long long; using ld = long double; using namespace std; const int INF = 1e9 + 100; const ll INF64 = 7e18l; const int mod = 1000000007; #define rep(i, a, b) for (int i = (a); i < (b); i++) #define ALL(a) (a).begin(), (a).end() #define FIN \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); int main() { FIN; ll l, r; cin >> l >> r; ll minmum = 1000000; for (ll i = 0; i < 2030; i++) { for (ll j = l; j < r; j++) { for (ll k = l + 1; k <= r; k++) { minmum = min(minmum, (j * k) % 2019); if (minmum == 0) break; } } } cout << minmum << endl; return 0; }
#include <algorithm> #include <bitset> #include <cmath> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <string> #include <tuple> #include <utility> #include <vector> using ll = long long; using ld = long double; using namespace std; const int INF = 1e9 + 100; const ll INF64 = 7e18l; const int mod = 1000000007; #define rep(i, a, b) for (int i = (a); i < (b); i++) #define ALL(a) (a).begin(), (a).end() #define FIN \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); int main() { FIN; ll l, r; cin >> l >> r; ll minmum = 1000000; if (r - l > 2030) { cout << 0 << endl; return 0; } for (ll j = l; j < r; j++) { for (ll k = l + 1; k <= r; k++) { minmum = min(minmum, (j * k) % 2019); } } cout << minmum << endl; return 0; }
replace
34
41
34
41
TLE
p02983
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { long long int L, R; cin >> L >> R; int min = 9999; long long int tmp; for (long long int i = L; i < R; i++) { for (long long int j = i + 1; j <= R; j++) { tmp = i * j; tmp %= 2019; if (min > tmp) { min = tmp; } } } cout << min << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long int L, R; cin >> L >> R; if (R - L >= 2019) { cout << 0 << endl; return 0; } int min = 9999; long long int tmp; for (long long int i = L; i < R; i++) { for (long long int j = i + 1; j <= R; j++) { tmp = i * j; tmp %= 2019; if (min > tmp) { min = tmp; } } } cout << min << endl; return 0; }
insert
6
6
6
11
TLE
p02983
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define mfill(x, y) memset(x, y, sizeof(x)) #define all(v) v.begin(), v.end() #define in(x, y, h, w) if (0 <= x && x < h && 0 <= y && y < w) #define y0 y12345 #define y1 y54321 #ifdef LOCAL #define debug(...) fprintf(stderr, __VA_ARGS__) #else #define debug(...) 42 #endif using ul = unsigned long; using ll = long long; using P = pair<int, int>; using vint = vector<int>; using vvint = vector<vector<int>>; using vll = vector<ll>; using vvll = vector<vector<ll>>; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } template <class T> void initvv(vector<vector<T>> &v, int a, int b, const T &t = T()) { v.assign(a, vector<T>(b, t)); } template <class T> T gcd(T &a, T &b) { if (a < b) { swap(a, b); } T r = a % b; while (r != 0) { a = b; b = r; r = a % b; } return b; } vint dx = {-1, 0, 1, 0}, dy = {0, -1, 0, 1}; vint dx8 = {-1, -1, -1, 0, 1, 1, 1, 0}, dy8 = {-1, 0, 1, 1, 1, 0, -1, -1}; int main() { cin.tie(0); ios::sync_with_stdio(false); debug("debug test\n"); int n, m; ll l, r; cin >> l >> r; if ((r - l) >= 2020) { cout << 0 << endl; } ll ans = 2019ll; for (ll i = l; i < r; i++) { for (ll j = i + 1; j <= r; j++) { chmin(ans, i * j % 2019ll); } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define mfill(x, y) memset(x, y, sizeof(x)) #define all(v) v.begin(), v.end() #define in(x, y, h, w) if (0 <= x && x < h && 0 <= y && y < w) #define y0 y12345 #define y1 y54321 #ifdef LOCAL #define debug(...) fprintf(stderr, __VA_ARGS__) #else #define debug(...) 42 #endif using ul = unsigned long; using ll = long long; using P = pair<int, int>; using vint = vector<int>; using vvint = vector<vector<int>>; using vll = vector<ll>; using vvll = vector<vector<ll>>; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } template <class T> void initvv(vector<vector<T>> &v, int a, int b, const T &t = T()) { v.assign(a, vector<T>(b, t)); } template <class T> T gcd(T &a, T &b) { if (a < b) { swap(a, b); } T r = a % b; while (r != 0) { a = b; b = r; r = a % b; } return b; } vint dx = {-1, 0, 1, 0}, dy = {0, -1, 0, 1}; vint dx8 = {-1, -1, -1, 0, 1, 1, 1, 0}, dy8 = {-1, 0, 1, 1, 1, 0, -1, -1}; int main() { cin.tie(0); ios::sync_with_stdio(false); debug("debug test\n"); int n, m; ll l, r; cin >> l >> r; if ((r - l) >= 2020) { cout << 0 << endl; return 0; } ll ans = 2019ll; for (ll i = l; i < r; i++) { for (ll j = i + 1; j <= r; j++) { chmin(ans, i * j % 2019ll); } } cout << ans << endl; return 0; }
insert
65
65
65
66
TLE
p02983
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { long long l, r; cin >> l >> r; if (l - r >= 2019) cout << 0 << endl; else { long long kotae = LLONG_MAX; for (long long i = l; i < r; i++) { for (long long j = i + 1; j <= r; j++) { kotae = min(kotae, (i % 2019 * j) % 2019); } } cout << kotae << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long l, r; cin >> l >> r; if (r - l >= 2019) cout << 0 << endl; else { long long kotae = LLONG_MAX; for (long long i = l; i < r; i++) { for (long long j = i + 1; j <= r; j++) { kotae = min(kotae, (i % 2019 * j) % 2019); } } cout << kotae << endl; } return 0; }
replace
6
7
6
7
TLE
p02983
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define uLL unsigned long long using namespace std; #define LL long long int mark[2333]; int main() { int L, R; cin >> L >> R; for (int i = L, s = 1; i <= R && s <= 2019 * 3; i++) mark[i % 2019]++; int ans = 2019; for (int i = 0; i <= 2019; i++) { if (!mark[i]) continue; mark[i]--; for (int j = 0; j <= 2019; j++) if (mark[j]) ans = min(ans, i * j % 2019); mark[i]++; } cout << ans; return 0; }
#include <bits/stdc++.h> #define uLL unsigned long long using namespace std; #define LL long long int mark[2333]; int main() { int L, R; cin >> L >> R; for (int i = L, s = 1; i <= R && s <= 2019 * 3; i++, s++) mark[i % 2019]++; int ans = 2019; for (int i = 0; i <= 2019; i++) { if (!mark[i]) continue; mark[i]--; for (int j = 0; j <= 2019; j++) if (mark[j]) ans = min(ans, i * j % 2019); mark[i]++; } cout << ans; return 0; }
replace
8
9
8
9
TLE
p02983
C++
Time Limit Exceeded
#include <cmath> #include <iostream> #include <vector> using namespace std; int main() { int l, r; cin >> l >> r; if (l - r > 3000) cout << 0 << endl; else { int ret = 1145141919; for (int i = l; i <= r; i++) { for (int j = i + 1; j <= r; j++) { int a = i % 2019; int b = j % 2019; ret = min(ret, (a * b) % 2019); } } cout << ret << endl; } return 0; }
#include <cmath> #include <iostream> #include <vector> using namespace std; int main() { int l, r; cin >> l >> r; if (r - l > 3000) cout << 0 << endl; else { int ret = 1145141919; for (int i = l; i <= r; i++) { for (int j = i + 1; j <= r; j++) { int a = i % 2019; int b = j % 2019; ret = min(ret, (a * b) % 2019); } } cout << ret << endl; } return 0; }
replace
9
10
9
10
TLE
p02983
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) using P = pair<int, int>; using ll = long long; const int INF = 1001001001; int main() { int l, r; cin >> l >> r; int ans = INF; if (l - r > 2019) { cout << 0 << endl; return 0; } for (int i = l; i < r; i++) { for (int j = i + 1; j <= r; j++) { int ii = i % 2019; int jj = j % 2019; ans = min(ans, ii * jj % 2019); if (ans == 0) break; } } cout << ans % 2019 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) using P = pair<int, int>; using ll = long long; const int INF = 1001001001; int main() { int l, r; cin >> l >> r; int ans = INF; if (l - r > 2019) { cout << 0 << endl; return 0; } for (int i = l; i < r; i++) { for (int j = i + 1; j <= r; j++) { int ii = i % 2019; int jj = j % 2019; ans = min(ans, ii * jj % 2019); if (ans == 0) break; } if (ans == 0) break; } cout << ans % 2019 << endl; return 0; }
insert
23
23
23
25
TLE
p02984
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ALL(v) v.begin(), v.end() #define V vector #define P pair typedef long long ll; const int INT_INF = 1e9; const ll INF = 1LL << 30; const ll MOD = 1e9 + 7; int main() { int n; cin >> n; V<ll> a(n); ll sum = 0; for (int i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; } ll tmp = 0; for (int i = 1; i < n; i += 2) { tmp += 2 * a[i]; } V<ll> ans(n); ans[0] = sum - tmp; for (int i = 0; i < n; i++) { ans[i + 1] = 2 * a[i] - ans[i]; } for (int i = 0; i < n; i++) { if (i != n - 1) cout << ans[i] << " "; else cout << ans[i] << endl; } }
#include <bits/stdc++.h> using namespace std; #define ALL(v) v.begin(), v.end() #define V vector #define P pair typedef long long ll; const int INT_INF = 1e9; const ll INF = 1LL << 30; const ll MOD = 1e9 + 7; int main() { int n; cin >> n; V<ll> a(n); ll sum = 0; for (int i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; } ll tmp = 0; for (int i = 1; i < n; i += 2) { tmp += 2 * a[i]; } V<ll> ans(n); ans[0] = sum - tmp; for (int i = 1; i < n; i++) { ans[i] = 2 * a[i - 1] - ans[i - 1]; } for (int i = 0; i < n; i++) { if (i != n - 1) cout << ans[i] << " "; else cout << ans[i] << endl; } }
replace
28
30
28
30
-6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)
p02984
C++
Runtime Error
#include <iostream> #include <vector> using namespace std; int main(void) { int n; long box = 0; cin >> n; vector<int> a(n); vector<int> mo(n - 1); vector<int> al(n); for (int i = 0; i < n; i++) { cin >> a[i]; al[i] = a[i]; } while (al.size() != 1) { box = al[0] + al[2] - al[1]; al.erase(al.begin()); al.erase(al.begin()); al[0] = box; } mo[0] = al[0]; cout << mo[0] << " "; for (int i = 0; i < n - 1; i++) { mo[i + 1] = 2 * (a[i] - mo[i] / 2); cout << mo[i + 1] << " "; } cout << "" << endl; }
#include <iostream> #include <vector> using namespace std; int main(void) { int n; long box = 0; cin >> n; vector<int> a(n); vector<int> mo(n); vector<int> al(n); for (int i = 0; i < n; i++) { cin >> a[i]; al[i] = a[i]; } while (al.size() != 1) { box = al[0] + al[2] - al[1]; al.erase(al.begin()); al.erase(al.begin()); al[0] = box; } mo[0] = al[0]; cout << mo[0] << " "; for (int i = 0; i < n - 1; i++) { mo[i + 1] = 2 * (a[i] - mo[i] / 2); cout << mo[i + 1] << " "; } cout << "" << endl; }
replace
10
11
10
11
0
p02984
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <fstream> #include <iostream> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <string> #include <vector> using namespace std; #define I_MAX 2147483647 #define LL_MAX 9223372036854775807 #define ll long long #define ld long double struct XX { ll a; int i; }; class xxGreater { public: bool operator()(const XX &riLeft, const XX &riRight) const { // 第2条件 if ((riLeft.a) == (riRight.a)) { return riLeft.i < riRight.i; //<:昇順(小さいものから順番)、>:降順(大きいものから順番) // プライオリティキューの場合は > で、top()すると値の小さいものがとれる } // 第1条件 return (riLeft.a) > (riRight.a); } }; // map<long long,long long> prime_f(long long n){ // map<long long,long long>res; // for(int i=2;i*i<=n;i++){ // while(n%i==0){ // ++res[i]; // n/=i; // } // } // if(n!=1)res[n]=1; // return res; // } // int n; ////int dat[2*10000000]; ////int dat2[2*10000000]; // int dat[10]; // int dat2[10]; // // void init(int n_){ // n=1; // while(n<n_)n*=2; // for(int i=0;i<2*n-1;i++){ // dat[i]=0; // dat2[i]=0; // } // } // // void initset(int k,int a){ // k+=n-1; // dat[k]=a; // while(k>0){ // k=(k-1)/2; // dat[k]=dat[k*2+1]+dat[k*2+2]; // } // } // ////[a,b)の間を[l,r]区間で比較しアップデート ////引数のindexに注意 ////nは固定。initで計算すみ ////update2(L[i],R[i]+1,0,0,n,D[i]); // void update2(int a,int b,int k,int l,int r,int v){//v更新値、区間は0-index // if(r<=a || b<=l)return; // if(a<=l && r<=b){ // dat[k]+=dat2[k]; // if(r-l>1){ // dat2[k*2+1]+=dat2[k]/2; // dat2[k*2+1]+=dat2[k]/2; // } // dat2[k]=v*(r-l); // return; // }else{ // update2(a,b,k*2+1,l,(l+r)/2,v); // update2(a,b,k*2+2,(l+r)/2,r,v); // return; // } // } // // int query(int a,int b,int k,int l,int r){ // if(r<=a || b<=l)return 0; // if(a<=l && r<=b){ // dat[k]+=dat2[k]; // if(r-l>1){ // dat2[k*2+1]+=dat2[k]/2; // dat2[k*2+1]+=dat2[k]/2; // } // dat2[k]=0; // return dat[k]; // } // else{ // int vl=query(a,b,k*2+1,l,(l+r)/2); // int vr=query(a,b,k*2+2,(l+r)/2,r); // return vl+vr; // } // } // void printb(unsigned int v) { // unsigned int mask = (int)1 << (sizeof(v) * CHAR_BIT - 1); // do putchar(mask & v ? '1' : '0'); // while (mask >>= 1); // } #ifdef DEBUG ll a[101]; #else ll a[100001]; #endif int main(int argc, const char *argv[]) { // scanf("%s",S); // scanf("%d",&N); // scanf("%lld %lld",&target1,&target2); // sscanf(tmp.c_str(),"%dd%d%d",&time[i], &dice[i], &z[i]); // getline(cin, target); // ifstream ifs("01");//テスト用 // ifs >> a; // ここから // 入力高速化 ios::sync_with_stdio(false); cin.tie(0); int N; cin >> N; for (int i = 0; i < N; i++) { cin >> a[i]; } ll l = 0; ll r = 1000000000; while (l < r) { int f = 0; ll target = (l + r) / 2; ll next = a[0] - target; for (int i = 1; i < N; i++) { if (next < 0) { f = 1; // break; } next = a[i] - next; } if (next == target) { for (int i = 0; i < N; i++) { cout << 2 * target << " "; target = a[i] - target; } cout << endl; return 0; } else if (next > target) { l = (l + r) / 2; } else { r = (l + r) / 2; } } // ここまで // cout << "ans" << endl; // cout << " " << "ans" << endl; // printf("%.0f\n",ans);//小数点以下表示なし // printf("%.7f\n",p); return 0; }
#include <algorithm> #include <cmath> #include <fstream> #include <iostream> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <string> #include <vector> using namespace std; #define I_MAX 2147483647 #define LL_MAX 9223372036854775807 #define ll long long #define ld long double struct XX { ll a; int i; }; class xxGreater { public: bool operator()(const XX &riLeft, const XX &riRight) const { // 第2条件 if ((riLeft.a) == (riRight.a)) { return riLeft.i < riRight.i; //<:昇順(小さいものから順番)、>:降順(大きいものから順番) // プライオリティキューの場合は > で、top()すると値の小さいものがとれる } // 第1条件 return (riLeft.a) > (riRight.a); } }; // map<long long,long long> prime_f(long long n){ // map<long long,long long>res; // for(int i=2;i*i<=n;i++){ // while(n%i==0){ // ++res[i]; // n/=i; // } // } // if(n!=1)res[n]=1; // return res; // } // int n; ////int dat[2*10000000]; ////int dat2[2*10000000]; // int dat[10]; // int dat2[10]; // // void init(int n_){ // n=1; // while(n<n_)n*=2; // for(int i=0;i<2*n-1;i++){ // dat[i]=0; // dat2[i]=0; // } // } // // void initset(int k,int a){ // k+=n-1; // dat[k]=a; // while(k>0){ // k=(k-1)/2; // dat[k]=dat[k*2+1]+dat[k*2+2]; // } // } // ////[a,b)の間を[l,r]区間で比較しアップデート ////引数のindexに注意 ////nは固定。initで計算すみ ////update2(L[i],R[i]+1,0,0,n,D[i]); // void update2(int a,int b,int k,int l,int r,int v){//v更新値、区間は0-index // if(r<=a || b<=l)return; // if(a<=l && r<=b){ // dat[k]+=dat2[k]; // if(r-l>1){ // dat2[k*2+1]+=dat2[k]/2; // dat2[k*2+1]+=dat2[k]/2; // } // dat2[k]=v*(r-l); // return; // }else{ // update2(a,b,k*2+1,l,(l+r)/2,v); // update2(a,b,k*2+2,(l+r)/2,r,v); // return; // } // } // // int query(int a,int b,int k,int l,int r){ // if(r<=a || b<=l)return 0; // if(a<=l && r<=b){ // dat[k]+=dat2[k]; // if(r-l>1){ // dat2[k*2+1]+=dat2[k]/2; // dat2[k*2+1]+=dat2[k]/2; // } // dat2[k]=0; // return dat[k]; // } // else{ // int vl=query(a,b,k*2+1,l,(l+r)/2); // int vr=query(a,b,k*2+2,(l+r)/2,r); // return vl+vr; // } // } // void printb(unsigned int v) { // unsigned int mask = (int)1 << (sizeof(v) * CHAR_BIT - 1); // do putchar(mask & v ? '1' : '0'); // while (mask >>= 1); // } #ifdef DEBUG ll a[101]; #else ll a[100001]; #endif int main(int argc, const char *argv[]) { // scanf("%s",S); // scanf("%d",&N); // scanf("%lld %lld",&target1,&target2); // sscanf(tmp.c_str(),"%dd%d%d",&time[i], &dice[i], &z[i]); // getline(cin, target); // ifstream ifs("01");//テスト用 // ifs >> a; // ここから // 入力高速化 ios::sync_with_stdio(false); cin.tie(0); int N; cin >> N; for (int i = 0; i < N; i++) { cin >> a[i]; } ll l = -1; ll r = 1000000001; while (r - l > 1) { int f = 0; ll target = (l + r) / 2; ll next = a[0] - target; for (int i = 1; i < N; i++) { if (next < 0) { f = 1; // break; } next = a[i] - next; } if (next == target) { for (int i = 0; i < N; i++) { cout << 2 * target << " "; target = a[i] - target; } cout << endl; return 0; } else if (next > target) { l = (l + r) / 2; } else { r = (l + r) / 2; } } // ここまで // cout << "ans" << endl; // cout << " " << "ans" << endl; // printf("%.0f\n",ans);//小数点以下表示なし // printf("%.7f\n",p); return 0; }
replace
150
153
150
153
TLE
p02984
C++
Time Limit Exceeded
#include <iostream> #include <vector> using namespace std; using ll = long long; int main() { int n; cin >> n; vector<ll> a(n); for (int i = 0; i < n; i++) cin >> a[i]; ll sum = 0; for (int i = 0; i < n; i++) sum += a[i]; int r = n / 2; vector<ll> m(n); for (int i = 0; i < n; i++) { m[i] = sum; for (int j = (i + 1) % n;; j = (j + 2) % n) { m[i] -= a[j] * 2; if (j == (i + 2 * r - 1) % n) { break; } } } for (int i = 0; i < n; i++) { printf("%lld%c", m[i], i == n - 1 ? '\n' : ' '); } }
#include <iostream> #include <vector> using namespace std; using ll = long long; int main() { int n; cin >> n; vector<ll> a(n); for (int i = 0; i < n; i++) cin >> a[i]; ll sum = 0; for (int i = 0; i < n; i++) sum += a[i]; int r = n / 2; vector<ll> m(n); m[0] = sum; for (int j = 1; j < 2 * r; j = j + 2) { m[0] -= a[j] * 2; } for (int i = 1; i < n; i++) { m[i] = 2 * a[i - 1] - m[i - 1]; } for (int i = 0; i < n; i++) { printf("%lld%c", m[i], i == n - 1 ? '\n' : ' '); } }
replace
17
25
17
23
TLE
p02984
C++
Runtime Error
#include <algorithm> #include <climits> #include <cmath> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; long long A[9999]; long long y[9999]; int main() { int N; cin >> N; for (int i = 0; i < N; i++) { cin >> A[i]; } long long y1minusyn = A[0]; for (int i = 1; i < N - 1; i++) { if (i % 2 == 1) { y1minusyn -= A[i]; } else { y1minusyn += A[i]; } } y[0] = A[N - 1] + y1minusyn; for (int i = 1; i < N; i++) { y[i] = 2 * A[i - 1] - y[i - 1]; } for (int i = 0; i < N; i++) { if (i != N - 1) { cout << y[i] << " "; } else { cout << y[i] << endl; } } return 0; }
#include <algorithm> #include <climits> #include <cmath> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; long long A[99999]; long long y[99999]; int main() { int N; cin >> N; for (int i = 0; i < N; i++) { cin >> A[i]; } long long y1minusyn = A[0]; for (int i = 1; i < N - 1; i++) { if (i % 2 == 1) { y1minusyn -= A[i]; } else { y1minusyn += A[i]; } } y[0] = A[N - 1] + y1minusyn; for (int i = 1; i < N; i++) { y[i] = 2 * A[i - 1] - y[i - 1]; } for (int i = 0; i < N; i++) { if (i != N - 1) { cout << y[i] << " "; } else { cout << y[i] << endl; } } return 0; }
replace
15
17
15
17
0
p02984
C++
Runtime Error
// include // ------------------------------------------------ #include <bits/stdc++.h> #include <math.h> using namespace std; // func // ------------------------------------------------ int CalcSumOfDigit(int n); // 各桁の和を計算する。 int getDigit(int n); // 数字の桁数を取得する。 string upper(string str); // 英字を大文字に変換する。 string lower(string str); // 英字を小文字に変換する。 vector<pair<long long, long long>> prime_factorize(long long p); // 素因数分解 // class // ------------------------------------------------ class Combi { public: Combi(); long long Combination(long long n, long long k); long long nPk_modp(long long n, long long k, long long p); private: map<long long, map<long long, long long>> memo; long long n_num; long long k_num; }; // define // ------------------------------------------------ #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define sz(a) int((a).size()) #define rep(i, n) for (long long(i) = 0; (i) < (n); (i)++) #define repe(i, n) for (long long(i) = 0; (i) <= (n); (i)++) #define vsort(v) sort((v).begin(), (v).end()) #define rvsort(v) sort(rall((v))) #define vi vector<int> #define GCD(a, b) __gcd((a), (b)) #define LCM(a, b) (a) / GCD((a), (b)) * (b) #define kiriage(a, b) ((a) + (b)-1) / (b) const int INF = 1e9; typedef long long ll; typedef unsigned long long ull; typedef vector<long> vll; // code // ------------------------------------------------ int main() { ll n; cin >> n; vll a(n); rep(i, n) cin >> a[i]; ll sum = 0; rep(i, n) { sum += a[i]; } ll sum2 = 0; for (ll i = 1; i < n; i = i + 2) { sum2 += a[i]; } vll x(n); x[0] = sum - sum2 * 2; rep(i, n) { x[i + 1] = 2 * a[i] - x[i]; } rep(i, n) { if (i == 0) cout << x[i]; else cout << " " << x[i]; } cout << endl; return 0; } // funcの実体 // ------------------------------------------------ int getDigit(int n) { int i = 1; while (1) { n = n / 10; if (n == 1) break; i++; } return i; } int CalcSumOfDigit(int n) { int s = 0; while (n) { s += n % 10; n = n / 10; } return s; } string upper(string str) { for (auto itr = str.begin(); itr != str.end(); itr++) { if (97 <= *itr && *itr <= 122) { *itr = *itr - 32; } } return str; } string lower(string str) { for (auto itr = str.begin(); itr != str.end(); itr++) { if (65 <= *itr && *itr <= 90) { *itr = *itr + 32; } } return str; } Combi::Combi() { n_num = -1; k_num = -1; }; ll Combi::Combination(ll n, ll k) { ll ret; if (memo[n][k] != 0) { ret = memo[n][k]; } else if (n == k || k == 0) { memo[n][k] = 1; ret = 1; } else { ret = Combination(n - 1, k - 1) + Combination(n - 1, k); memo[n][k] = ret; } return ret; } long long Combi::nPk_modp(long long n, long long k, long long p) { ll ans = 1; for (long long i = k; i <= n; i++) { ans = (ans * i) % p; } return ans; }; vector<pair<long long, long long>> prime_factorize(long long p) { vector<pair<long long, long long>> ret; for (long long x = 2; x * x <= p; ++x) { if (p % x != 0) continue; long long num = 0; while (p % x == 0) { num++; p /= x; } ret.push_back(make_pair(x, num)); } if (p != 1) ret.push_back(make_pair(p, 1)); return ret; }
// include // ------------------------------------------------ #include <bits/stdc++.h> #include <math.h> using namespace std; // func // ------------------------------------------------ int CalcSumOfDigit(int n); // 各桁の和を計算する。 int getDigit(int n); // 数字の桁数を取得する。 string upper(string str); // 英字を大文字に変換する。 string lower(string str); // 英字を小文字に変換する。 vector<pair<long long, long long>> prime_factorize(long long p); // 素因数分解 // class // ------------------------------------------------ class Combi { public: Combi(); long long Combination(long long n, long long k); long long nPk_modp(long long n, long long k, long long p); private: map<long long, map<long long, long long>> memo; long long n_num; long long k_num; }; // define // ------------------------------------------------ #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define sz(a) int((a).size()) #define rep(i, n) for (long long(i) = 0; (i) < (n); (i)++) #define repe(i, n) for (long long(i) = 0; (i) <= (n); (i)++) #define vsort(v) sort((v).begin(), (v).end()) #define rvsort(v) sort(rall((v))) #define vi vector<int> #define GCD(a, b) __gcd((a), (b)) #define LCM(a, b) (a) / GCD((a), (b)) * (b) #define kiriage(a, b) ((a) + (b)-1) / (b) const int INF = 1e9; typedef long long ll; typedef unsigned long long ull; typedef vector<long> vll; // code // ------------------------------------------------ int main() { ll n; cin >> n; vll a(n); rep(i, n) cin >> a[i]; ll sum = 0; rep(i, n) { sum += a[i]; } ll sum2 = 0; for (ll i = 1; i < n; i = i + 2) { sum2 += a[i]; } vll x(n + 1); x[0] = sum - sum2 * 2; rep(i, n) { x[i + 1] = 2 * a[i] - x[i]; } rep(i, n) { if (i == 0) cout << x[i]; else cout << " " << x[i]; } cout << endl; return 0; } // funcの実体 // ------------------------------------------------ int getDigit(int n) { int i = 1; while (1) { n = n / 10; if (n == 1) break; i++; } return i; } int CalcSumOfDigit(int n) { int s = 0; while (n) { s += n % 10; n = n / 10; } return s; } string upper(string str) { for (auto itr = str.begin(); itr != str.end(); itr++) { if (97 <= *itr && *itr <= 122) { *itr = *itr - 32; } } return str; } string lower(string str) { for (auto itr = str.begin(); itr != str.end(); itr++) { if (65 <= *itr && *itr <= 90) { *itr = *itr + 32; } } return str; } Combi::Combi() { n_num = -1; k_num = -1; }; ll Combi::Combination(ll n, ll k) { ll ret; if (memo[n][k] != 0) { ret = memo[n][k]; } else if (n == k || k == 0) { memo[n][k] = 1; ret = 1; } else { ret = Combination(n - 1, k - 1) + Combination(n - 1, k); memo[n][k] = ret; } return ret; } long long Combi::nPk_modp(long long n, long long k, long long p) { ll ans = 1; for (long long i = k; i <= n; i++) { ans = (ans * i) % p; } return ans; }; vector<pair<long long, long long>> prime_factorize(long long p) { vector<pair<long long, long long>> ret; for (long long x = 2; x * x <= p; ++x) { if (p % x != 0) continue; long long num = 0; while (p % x == 0) { num++; p /= x; } ret.push_back(make_pair(x, num)); } if (p != 1) ret.push_back(make_pair(p, 1)); return ret; }
replace
66
67
66
67
-6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)
p02984
C++
Time Limit Exceeded
#include <algorithm> #include <cctype> #include <climits> #include <complex> #include <cstdio> #include <cstring> #include <functional> #include <iostream> #include <map> #include <math.h> #include <queue> #include <stdio.h> #include <string> #include <vector> using namespace std; using ll = long long; // typedef pair<string,string> P; // double dat[100][100]; // int dp[6][1010];//動的計画法 // int prime[100001]; // char str[1010][1010]; // typedef pair<ll,ll> A; // vector<A> pc; // int ABS(int a){return max(a,-a);} ll a[100000]; ll dom[100001]; int main() { int n; ll su = 0; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; su += a[i]; } ll cnt = 0; ll tmp = 0; for (ll i = 1; i <= n; i++) { dom[i] = su; cnt = 0; tmp = i + 1; while (cnt != (n - 1) / 2) { if (tmp > n) tmp %= n; dom[i] -= (2 * a[tmp]); // cout<<dom[i]<<endl; tmp = (tmp + 2); cnt++; } cout << dom[i] << " "; } return 0; }
#include <algorithm> #include <cctype> #include <climits> #include <complex> #include <cstdio> #include <cstring> #include <functional> #include <iostream> #include <map> #include <math.h> #include <queue> #include <stdio.h> #include <string> #include <vector> using namespace std; using ll = long long; // typedef pair<string,string> P; // double dat[100][100]; // int dp[6][1010];//動的計画法 // int prime[100001]; // char str[1010][1010]; // typedef pair<ll,ll> A; // vector<A> pc; // int ABS(int a){return max(a,-a);} ll a[100000]; ll dom[100001]; int main() { int n; ll su = 0; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; su += a[i]; } ll cnt = 0; ll tmp = 0; for (ll i = 1; i <= n; i++) { dom[i] = su; cnt = 0; tmp = i + 1; if (i == 1) { while (cnt != (n - 1) / 2) { if (tmp > n) tmp %= n; dom[i] -= (2 * a[tmp]); // cout<<dom[i]<<endl; tmp = (tmp + 2); cnt++; } } else { dom[i] = 2 * a[i - 1] - dom[i - 1]; } cout << dom[i] << " "; } return 0; }
replace
40
47
40
51
TLE
p02984
C++
Runtime Error
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #pragma GCC optimize("unroll-loops,no-stack-protector") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace __gnu_pbds; using namespace std; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; typedef long long ll; typedef long double ld; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll n; cin >> n; vector<ll> arr(n); ll s = 0; for (int z = 0; z < n; z++) { cin >> arr[z]; s += arr[z]; } vector<ll> pre(n); pre[0] = 2 * arr[0]; pre[1] = 2 * arr[0]; for (int z = 2; z < n - 1; z += 2) { pre[z] = pre[z - 2] + 2 * arr[z]; pre[z + 1] = pre[z]; } vector<ll> suff(n); suff[n - 1] = 2 * arr[n - 2]; suff[n - 2] = 2 * arr[n - 2]; for (int z = n - 3; z > 0; z -= 2) { suff[z] = suff[z + 2] + 2 * arr[z - 1]; suff[z - 1] = suff[z]; } vector<ll> ans(n); for (int z = 0; z < n; z += 2) { ans[z] = s; if (z < n - 1) ans[z] -= suff[z + 1]; if (z > 0) ans[z] -= pre[z - 1]; ans[z + 1] = 2 * arr[z] - ans[z]; } for (ll i : ans) { cout << i << " "; } cout << endl; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #pragma GCC optimize("unroll-loops,no-stack-protector") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace __gnu_pbds; using namespace std; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; typedef long long ll; typedef long double ld; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll n; cin >> n; vector<ll> arr(n); ll s = 0; for (int z = 0; z < n; z++) { cin >> arr[z]; s += arr[z]; } vector<ll> pre(n); pre[0] = 2 * arr[0]; pre[1] = 2 * arr[0]; for (int z = 2; z < n - 1; z += 2) { pre[z] = pre[z - 2] + 2 * arr[z]; pre[z + 1] = pre[z]; } vector<ll> suff(n); suff[n - 1] = 2 * arr[n - 2]; suff[n - 2] = 2 * arr[n - 2]; for (int z = n - 3; z > 0; z -= 2) { suff[z] = suff[z + 2] + 2 * arr[z - 1]; suff[z - 1] = suff[z]; } vector<ll> ans(n); for (int z = 0; z < n; z += 2) { ans[z] = s; if (z < n - 1) ans[z] -= suff[z + 1]; if (z > 0) ans[z] -= pre[z - 1]; if (z < n - 1) ans[z + 1] = 2 * arr[z] - ans[z]; } for (ll i : ans) { cout << i << " "; } cout << endl; }
replace
48
49
48
50
0
p02984
C++
Time Limit Exceeded
#include <iostream> #include <limits> #include <numeric> #include <vector> int main() { int N; std::cin >> N; std::vector<long long> a; a.reserve(N); long long x; for (int i = 0; i < N; ++i) { std::cin >> x; a.push_back(x); } const auto N_half = N / 2LL; const auto b_sum = std::accumulate(a.cbegin(), a.cend(), 0LL); std::vector<long long> b; b.reserve(N); for (int i = 0; i < N; ++i) { auto others = 0LL; for (int j = 0; j < N_half; ++j) { const auto idx = (i + 1 + 2 * j) % N; others += a[idx]; } b.push_back(b_sum - 2 * others); } for (int i = 0; i < N; ++i) { std::cout << b[i]; if (i < N) { std::cout << " "; } else { std::cout << std::endl; } } }
#include <iostream> #include <limits> #include <numeric> #include <vector> int main() { int N; std::cin >> N; std::vector<long long> a; a.reserve(N); long long x; for (int i = 0; i < N; ++i) { std::cin >> x; a.push_back(x); } const auto N_half = N / 2LL; const auto b_sum = std::accumulate(a.cbegin(), a.cend(), 0LL); std::vector<long long> b; b.reserve(N); auto others = 0LL; for (int j = 0; j < N_half; ++j) { others += a[1 + 2 * j]; } b.push_back(b_sum - 2 * others); for (int i = 1; i < N; ++i) { b.push_back(2LL * a[i - 1] - b[i - 1]); } for (int i = 0; i < N; ++i) { std::cout << b[i]; if (i < N) { std::cout << " "; } else { std::cout << std::endl; } } }
replace
23
30
23
31
TLE
p02984
C++
Runtime Error
#include <cstdio> #include <iostream> using namespace std; typedef long long ll; const ll M = 1e4 + 5; ll a[M], ans[M]; inline ll read() { ll x = 0, f = 1; char ch = 0; while (!isdigit(ch)) f = (ch == '-' ? -1 : 1), ch = getchar(); while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar(); return x * f; } int main() { ll n = read(), sum = 0, s = 0; for (register ll i = 1; i <= n; i++) { a[i] = read(); a[i] *= 2; sum += a[i]; if (i % 2) s += a[i]; } sum = sum / 2 - s + a[n]; ans[n] = sum; for (register ll i = n - 1; i >= 1; i--) { ans[i] = a[i] - ans[i + 1]; } for (register ll i = 1; i <= n; i++) { printf("%lld ", ans[i]); } return 0; }
#include <cstdio> #include <iostream> using namespace std; typedef long long ll; const ll M = 1e5 + 5; ll a[M], ans[M]; inline ll read() { ll x = 0, f = 1; char ch = 0; while (!isdigit(ch)) f = (ch == '-' ? -1 : 1), ch = getchar(); while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar(); return x * f; } int main() { ll n = read(), sum = 0, s = 0; for (register ll i = 1; i <= n; i++) { a[i] = read(); a[i] *= 2; sum += a[i]; if (i % 2) s += a[i]; } sum = sum / 2 - s + a[n]; ans[n] = sum; for (register ll i = n - 1; i >= 1; i--) { ans[i] = a[i] - ans[i + 1]; } for (register ll i = 1; i <= n; i++) { printf("%lld ", ans[i]); } return 0; }
replace
4
5
4
5
0
p02984
C++
Runtime Error
#include <cstdio> #include <iostream> using namespace std; long long int a[10000]; long long arr[10000]; int main() { int n; // scanf("%d",&n); cin >> n; for (int i = 1; i <= n; i++) { // scanf("%d",&a[i]); cin >> a[i]; } long long sum = 0; for (int i = 1; i <= n; i++) { if (i % 2 != 0) { sum += a[i]; } else { sum -= a[i]; } } arr[1] = sum / 2; // printf("%d\n",sum); for (int i = 2; i <= n; i++) { arr[i] = a[i - 1] - arr[i - 1]; } for (int i = 1; i <= n; i++) { if (i == 1) { cout << 2 * arr[i]; // printf("%d",2*arr[i]); } else { cout << " " << 2 * arr[i]; // printf(" %d",2*arr[i]); } } return 0; }
#include <cstdio> #include <iostream> using namespace std; long long int a[100000000]; long long arr[100000000]; int main() { int n; // scanf("%d",&n); cin >> n; for (int i = 1; i <= n; i++) { // scanf("%d",&a[i]); cin >> a[i]; } long long sum = 0; for (int i = 1; i <= n; i++) { if (i % 2 != 0) { sum += a[i]; } else { sum -= a[i]; } } arr[1] = sum / 2; // printf("%d\n",sum); for (int i = 2; i <= n; i++) { arr[i] = a[i - 1] - arr[i - 1]; } for (int i = 1; i <= n; i++) { if (i == 1) { cout << 2 * arr[i]; // printf("%d",2*arr[i]); } else { cout << " " << 2 * arr[i]; // printf(" %d",2*arr[i]); } } return 0; }
replace
4
6
4
6
0
p02984
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define MAX 5 int main() { long long N; cin >> N; long long ans[MAX]; long long A[MAX]; long long sum = 0; for (int i = 0; i < N; i++) { cin >> A[i]; sum += A[i]; } for (int i = 1; i < N; i += 2) { sum -= 2 * A[i]; } ans[0] = sum; for (int i = 1; i < N; i++) { ans[i] = 2 * A[i - 1] - ans[i - 1]; } for (int i = 0; i < N; i++) { cout << ans[i] << " "; } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define MAX 100000 int main() { long long N; cin >> N; long long ans[MAX]; long long A[MAX]; long long sum = 0; for (int i = 0; i < N; i++) { cin >> A[i]; sum += A[i]; } for (int i = 1; i < N; i += 2) { sum -= 2 * A[i]; } ans[0] = sum; for (int i = 1; i < N; i++) { ans[i] = 2 * A[i - 1] - ans[i - 1]; } for (int i = 0; i < N; i++) { cout << ans[i] << " "; } cout << endl; return 0; }
replace
3
4
3
4
0
p02984
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() { int n; vector<int> a(n); rep(i, n) cin >> a[i]; ll x2 = 0; rep(i, n) { if (i % 2) x2 -= a[i]; else x2 += a[i]; } vector<int> ans(n); ans[0] = x2 / 2; rep(i, n - 1) { ans[i + 1] = a[i] - ans[i]; } rep(i, n) ans[i] *= 2; rep(i, n) printf("%d%c", ans[i], i == n - 1 ? '\n' : ' '); return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; typedef long long ll; int main() { int n; cin >> n; vector<int> a(n); rep(i, n) cin >> a[i]; ll x2 = 0; rep(i, n) { if (i % 2) x2 -= a[i]; else x2 += a[i]; } vector<int> ans(n); ans[0] = x2 / 2; rep(i, n - 1) { ans[i + 1] = a[i] - ans[i]; } rep(i, n) ans[i] *= 2; rep(i, n) printf("%d%c", ans[i], i == n - 1 ? '\n' : ' '); return 0; }
insert
7
7
7
8
0
p02984
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define MAX(a, b) (a > b ? a : b) #define MIN(a, b) (a < b ? a : b) typedef long long ll; typedef unsigned long long ull; typedef unsigned int ui; typedef pair<int, int> pii; int gi() { int a; scanf("%d", &a); return a; } ll gli() { ll a; scanf("%lld", &a); return a; } int a[10000]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int s = 0; int n = gi(); for (int i = 0; i < n; i++) { a[i] = gi(); s += (i % 2 ? -1 : 1) * a[i]; } printf("%d ", s); s /= 2; for (int i = 1; i < n; i++) { s = a[i - 1] - s; printf("%d%c", s * 2, (i == n - 1 ? '\n' : ' ')); } return 0; }
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define MAX(a, b) (a > b ? a : b) #define MIN(a, b) (a < b ? a : b) typedef long long ll; typedef unsigned long long ull; typedef unsigned int ui; typedef pair<int, int> pii; int gi() { int a; scanf("%d", &a); return a; } ll gli() { ll a; scanf("%lld", &a); return a; } int a[100000]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int s = 0; int n = gi(); for (int i = 0; i < n; i++) { a[i] = gi(); s += (i % 2 ? -1 : 1) * a[i]; } printf("%d ", s); s /= 2; for (int i = 1; i < n; i++) { s = a[i - 1] - s; printf("%d%c", s * 2, (i == n - 1 ? '\n' : ' ')); } return 0; }
replace
34
35
34
35
0
p02984
C++
Time Limit Exceeded
// // Created by kuroneko on 2019-07-06. // #include <iostream> typedef long long ll; using namespace std; int main() { int N; cin >> N; ll A[N]; for (int i = 0; i < N; i++) { cin >> A[i]; } ll x[N]; for (int i = 0; i < N; i++) { x[i] = A[i]; for (int j = 1; j < N; j++) { if (j % 2 == 1) x[i] -= A[(i + j) % N]; else x[i] += A[(i + j) % N]; } } for (int i = 0; i < N; i++) { cout << x[i]; if (i != N - 1) cout << ' '; } cout << endl; return 0; }
// // Created by kuroneko on 2019-07-06. // #include <iostream> typedef long long ll; using namespace std; int main() { int N; cin >> N; ll A[N]; for (int i = 0; i < N; i++) { cin >> A[i]; } ll x[N]; x[0] = A[0]; for (int j = 1; j < N; j++) { if (j % 2 == 1) x[0] -= A[j % N]; else x[0] += A[j % N]; } for (int i = 0; i < N - 1; i++) { x[i + 1] = -x[i] + 2 * A[i]; } for (int i = 0; i < N; i++) { cout << x[i]; if (i != N - 1) cout << ' '; } cout << endl; return 0; }
replace
18
26
18
28
TLE
p02984
C++
Time Limit Exceeded
#define ll long long #define FOR(i, begin, end) for (ll i = (begin); i < (end); i++) #define REP(i, n) FOR(i, 0, n) #define SORT(a) sort(a.begin(), a.end()) #define SORTD(a) sort(a.rbegin(), a.rend()); #define UNIQUE(a) unique(a.begin(), a.end()) #define INF 1000000000000000000 #define INT_MAX 2147483647 #include <algorithm> #include <cctype> #include <cmath> #include <iomanip> #include <ios> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <string> #include <utility> #include <vector> using namespace std; using pii = pair<int, int>; using pll = pair<ll, ll>; using vi = vector<int>; using vl = vector<ll>; const ll mod = 1e9 + 7; int main() { ll n; cin >> n; vl a(n); REP(i, n) { cin >> a[i]; } ll sum = 0; REP(i, n) { sum = 0; REP(j, n) { sum += a[(i + j) % n] * pow(-1, j % 2); } cout << sum << " "; } cout << endl; }
#define ll long long #define FOR(i, begin, end) for (ll i = (begin); i < (end); i++) #define REP(i, n) FOR(i, 0, n) #define SORT(a) sort(a.begin(), a.end()) #define SORTD(a) sort(a.rbegin(), a.rend()); #define UNIQUE(a) unique(a.begin(), a.end()) #define INF 1000000000000000000 #define INT_MAX 2147483647 #include <algorithm> #include <cctype> #include <cmath> #include <iomanip> #include <ios> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <string> #include <utility> #include <vector> using namespace std; using pii = pair<int, int>; using pll = pair<ll, ll>; using vi = vector<int>; using vl = vector<ll>; const ll mod = 1e9 + 7; int main() { ll n; cin >> n; vl a(n); REP(i, n) { cin >> a[i]; } ll sum = 0; REP(j, n) { sum += a[j % n] * pow(-1, j % 2); } cout << sum << " "; FOR(i, 1, n) { sum = 2 * a[i - 1] - sum; cout << sum << " "; } cout << endl; }
replace
39
42
39
44
TLE
p02984
C++
Runtime Error
// includes #include "bits/stdc++.h" using namespace std; // macros #define ll long long #define MOD 1000000007 // 998244353 //100000000 // #define pii pair<ll, ll> #define piii pair<ll, pii> #define sz(x) ((ll)(x).size()) #define ft first #define sd second #define pb push_back #define rep(i, n) for (ll i = 0; i < n; i++) #define repr(i, n) for (ll i = n - 1; i >= 0; i--) #define itr(it, x) for (auto it = x.begin(); it != x.end(); it++) #define mem(a, b) memset(a, (ll)b, sizeof(a)) #define all(v) v.begin(), v.end() #define allr(v) v.rbegin(), v.rend() #define edge(v, x, y) \ v[x].pb(y); \ v[y].pb(x); #define popc __builtin_popcount #define ANS(s) \ { \ cout << s << "\n"; \ return; \ } // functions template <typename T> void unique(T &c) { c.erase(std::unique(c.begin(), c.end()), c.end()); } template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { for (int i = 0; i < vec.size(); i++) { os << vec[i]; if (i + 1 != vec.size()) os << " "; } return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &st) { for (auto itr = st.begin(); itr != st.end(); ++itr) { os << *itr; auto titr = itr; if (++titr != st.end()) os << " "; } return os; } template <typename T> ostream &operator<<(ostream &os, const unordered_set<T> &st) { for (auto itr = st.begin(); itr != st.end(); ++itr) { os << *itr; auto titr = itr; if (++titr != st.end()) os << " "; } return os; } template <typename T> ostream &operator<<(ostream &os, const multiset<T> &st) { for (auto itr = st.begin(); itr != st.end(); ++itr) { os << *itr; auto titr = itr; if (++titr != st.end()) os << " "; } return os; } template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &st) { for (auto itr = st.begin(); itr != st.end(); ++itr) { os << *itr; auto titr = itr; if (++titr != st.end()) os << " "; } return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << "(" << p.first << ", " << p.second << ")"; return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const map<T1, T2> &mp) { for (auto itr = mp.begin(); itr != mp.end(); ++itr) { os << "(" << itr->first << ", " << itr->second << ")"; auto titr = itr; if (++titr != mp.end()) os << " "; } return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const unordered_map<T1, T2> &mp) { for (auto itr = mp.begin(); itr != mp.end(); ++itr) { os << "(" << itr->first << ", " << itr->second << ")"; auto titr = itr; if (++titr != mp.end()) os << " "; } return os; } // constants const ll N = 1e5 + 5, M = 1e6 + 5, A = 1e7 + 5, inf = 1e9; const long long linf = 1LL << 60; const double er = 1e-10, pi = 3.141592653589793238463; const ll lx[4] = {0, 1, -1, 0}; const ll ly[4] = {1, 0, 0, -1}; const ll dx[8] = {0, 0, 1, -1, 1, -1, 1, -1}; const ll dy[8] = {1, -1, 0, 0, 1, 1, -1, -1}; // io struct fast_io { fast_io() { ios_base::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(20); } } fast_io_; void solve() { ll n, tot = 0; cin >> n; vector<ll> a(n), ans(n); cin >> a; vector<ll> b; rep(i, n) b.pb(a[i]), tot += a[i]; rep(i, n) b.pb(a[i]); rep(i, n) b.pb(a[i]); // cout << b; ll k = 0; for (ll i = 0; i < n; i += 2) { k += b[i]; } for (ll i = 0; i < n; i += 2) { ans[i] = k * 2 - tot; k -= b[i]; k += b[i + n + 1]; // cout << ans << endl; } k = 0; for (ll i = 1; i <= n; i += 2) { k += b[i]; } for (ll i = 1; i <= n; i += 2) { ans[i] = k * 2 - tot; k -= b[i]; k += b[i + n + 1]; // cout << ans << endl; } cout << ans << endl; } int main(int argc, char const *argv[]) { ll t = 1; // cin >> t; while (t--) { solve(); } return 0; }
// includes #include "bits/stdc++.h" using namespace std; // macros #define ll long long #define MOD 1000000007 // 998244353 //100000000 // #define pii pair<ll, ll> #define piii pair<ll, pii> #define sz(x) ((ll)(x).size()) #define ft first #define sd second #define pb push_back #define rep(i, n) for (ll i = 0; i < n; i++) #define repr(i, n) for (ll i = n - 1; i >= 0; i--) #define itr(it, x) for (auto it = x.begin(); it != x.end(); it++) #define mem(a, b) memset(a, (ll)b, sizeof(a)) #define all(v) v.begin(), v.end() #define allr(v) v.rbegin(), v.rend() #define edge(v, x, y) \ v[x].pb(y); \ v[y].pb(x); #define popc __builtin_popcount #define ANS(s) \ { \ cout << s << "\n"; \ return; \ } // functions template <typename T> void unique(T &c) { c.erase(std::unique(c.begin(), c.end()), c.end()); } template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { for (int i = 0; i < vec.size(); i++) { os << vec[i]; if (i + 1 != vec.size()) os << " "; } return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &st) { for (auto itr = st.begin(); itr != st.end(); ++itr) { os << *itr; auto titr = itr; if (++titr != st.end()) os << " "; } return os; } template <typename T> ostream &operator<<(ostream &os, const unordered_set<T> &st) { for (auto itr = st.begin(); itr != st.end(); ++itr) { os << *itr; auto titr = itr; if (++titr != st.end()) os << " "; } return os; } template <typename T> ostream &operator<<(ostream &os, const multiset<T> &st) { for (auto itr = st.begin(); itr != st.end(); ++itr) { os << *itr; auto titr = itr; if (++titr != st.end()) os << " "; } return os; } template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &st) { for (auto itr = st.begin(); itr != st.end(); ++itr) { os << *itr; auto titr = itr; if (++titr != st.end()) os << " "; } return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << "(" << p.first << ", " << p.second << ")"; return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const map<T1, T2> &mp) { for (auto itr = mp.begin(); itr != mp.end(); ++itr) { os << "(" << itr->first << ", " << itr->second << ")"; auto titr = itr; if (++titr != mp.end()) os << " "; } return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const unordered_map<T1, T2> &mp) { for (auto itr = mp.begin(); itr != mp.end(); ++itr) { os << "(" << itr->first << ", " << itr->second << ")"; auto titr = itr; if (++titr != mp.end()) os << " "; } return os; } // constants const ll N = 1e5 + 5, M = 1e6 + 5, A = 1e7 + 5, inf = 1e9; const long long linf = 1LL << 60; const double er = 1e-10, pi = 3.141592653589793238463; const ll lx[4] = {0, 1, -1, 0}; const ll ly[4] = {1, 0, 0, -1}; const ll dx[8] = {0, 0, 1, -1, 1, -1, 1, -1}; const ll dy[8] = {1, -1, 0, 0, 1, 1, -1, -1}; // io struct fast_io { fast_io() { ios_base::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(20); } } fast_io_; void solve() { ll n, tot = 0; cin >> n; vector<ll> a(n), ans(n); cin >> a; vector<ll> b; rep(i, n) b.pb(a[i]), tot += a[i]; rep(i, n) b.pb(a[i]); rep(i, n) b.pb(a[i]); // cout << b; ll k = 0; for (ll i = 0; i < n; i += 2) { k += b[i]; } for (ll i = 0; i < n; i += 2) { ans[i] = k * 2 - tot; k -= b[i]; k += b[i + n + 1]; // cout << ans << endl; } k = 0; for (ll i = 1; i <= n; i += 2) { k += b[i]; } for (ll i = 1; i <= n; i += 2) { if (i < n) ans[i] = k * 2 - tot; k -= b[i]; k += b[i + n + 1]; // cout << ans << endl; } cout << ans << endl; } int main(int argc, char const *argv[]) { ll t = 1; // cin >> t; while (t--) { solve(); } return 0; }
replace
163
164
163
165
0
p02984
C++
Runtime Error
#include <math.h> #include <queue> #include <stdio.h> #include <stdlib.h> #include <string.h> using namespace std; typedef unsigned int uint; typedef long long ll; typedef pair<int, int> P; const int c_INF = 11111111111; const int c_mINF = -11111111111; const int c_YET = -1; const int c_Dx[4] = {0, 0, 1, -1}; const int c_Dy[4] = {1, -1, 0, 0}; int main() { int N, A[10000], X[10000]; // 入力 scanf("%d", &N); for (int i = 0; i < N; i++) scanf("%d", &A[i]); for (int i = 0; i < N; i++) X[i] = 0; for (int i = 0; i < N / 2; i++) X[0] += A[2 * i] - A[2 * i + 1]; X[0] += A[N - 1]; X[0] /= 2; // printf("%d\n",X[0]); for (int i = 0; i < N - 1; i++) { X[i + 1] = A[i] - X[i]; } for (int i = 0; i < N; i++) printf("%d ", 2 * X[i]); /* for i in range(N - 1) : X.aappend(A[i] - X[i]) print(X) */ } /* 提出前の確認事項 ・デバッグ用の関数は<<コメント化>>しているか? ・すべての<<入力例>>は試したか? ・<<限界値分析>>はしたか? ・<<出力の改行>>の有無は確認したか?         ↓ <<提出先>>に注意して提出!! */
#include <math.h> #include <queue> #include <stdio.h> #include <stdlib.h> #include <string.h> using namespace std; typedef unsigned int uint; typedef long long ll; typedef pair<int, int> P; const int c_INF = 11111111111; const int c_mINF = -11111111111; const int c_YET = -1; const int c_Dx[4] = {0, 0, 1, -1}; const int c_Dy[4] = {1, -1, 0, 0}; int main() { int N, A[100000], X[100000]; // 入力 scanf("%d", &N); for (int i = 0; i < N; i++) scanf("%d", &A[i]); for (int i = 0; i < N; i++) X[i] = 0; for (int i = 0; i < N / 2; i++) X[0] += A[2 * i] - A[2 * i + 1]; X[0] += A[N - 1]; X[0] /= 2; // printf("%d\n",X[0]); for (int i = 0; i < N - 1; i++) { X[i + 1] = A[i] - X[i]; } for (int i = 0; i < N; i++) printf("%d ", 2 * X[i]); /* for i in range(N - 1) : X.aappend(A[i] - X[i]) print(X) */ } /* 提出前の確認事項 ・デバッグ用の関数は<<コメント化>>しているか? ・すべての<<入力例>>は試したか? ・<<限界値分析>>はしたか? ・<<出力の改行>>の有無は確認したか?         ↓ <<提出先>>に注意して提出!! */
replace
16
17
16
17
0
p02984
C++
Runtime Error
#include <bits/stdc++.h> // #include <ext/numeric> #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update #define oo 0x3f3f3f3f #define OO 0x3f3f3f3f3f3f3f3f #define popcount(n) __builtin_popcount(n) #define popcountll(n) __builtin_popcountll(n) // bool flag = __builtin_add_overflow(a , b , &tmp); // bool flag = __builtin_mul_overflow(a , b , &tmp); // bool flag3= __builtin_sub_overflow(a , b , &tmp); using namespace std; // using namespace __gnu_cxx; using namespace __gnu_pbds; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; const double PI = acos(-1.0), EPS = 1e-6; const long long inf = 2e12 + 12; const int MAXN = 10004, MAXM = 102, Mod = 1000000007, MAXLog = 20; int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "rt", stdin); // freopen("output.txt", "w", stdout); #endif ios::sync_with_stdio(false), cin.tie(0), cout.tie(0), cout.precision(9), cout << fixed; int n; cin >> n; vector<long long> arr(n); long long sum = 0; for (int i = 0; i < n; ++i) { cin >> arr[i]; arr[i] *= 2; if (i & 1) { sum -= arr[i]; } else { sum += arr[i]; } } sum /= 2; cout << sum << ' '; for (int i = 0; i < n - 1; ++i) { cout << arr[i] - sum << ' '; sum = arr[i] - sum; } cout << '\n'; return 0; }
#include <bits/stdc++.h> // #include <ext/numeric> #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update #define oo 0x3f3f3f3f #define OO 0x3f3f3f3f3f3f3f3f #define popcount(n) __builtin_popcount(n) #define popcountll(n) __builtin_popcountll(n) // bool flag = __builtin_add_overflow(a , b , &tmp); // bool flag = __builtin_mul_overflow(a , b , &tmp); // bool flag3= __builtin_sub_overflow(a , b , &tmp); using namespace std; // using namespace __gnu_cxx; using namespace __gnu_pbds; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; const double PI = acos(-1.0), EPS = 1e-6; const long long inf = 2e12 + 12; const int MAXN = 10004, MAXM = 102, Mod = 1000000007, MAXLog = 20; int main() { #ifndef ONLINE_JUDGE // freopen("input.txt", "rt", stdin); // freopen("output.txt", "w", stdout); #endif ios::sync_with_stdio(false), cin.tie(0), cout.tie(0), cout.precision(9), cout << fixed; int n; cin >> n; vector<long long> arr(n); long long sum = 0; for (int i = 0; i < n; ++i) { cin >> arr[i]; arr[i] *= 2; if (i & 1) { sum -= arr[i]; } else { sum += arr[i]; } } sum /= 2; cout << sum << ' '; for (int i = 0; i < n - 1; ++i) { cout << arr[i] - sum << ' '; sum = arr[i] - sum; } cout << '\n'; return 0; }
replace
26
27
26
27
0
p02984
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; typedef long long ll; typedef pair<int, int> P; typedef vector<int> vi; const int m = 1000000007; int main() { int n; cin >> n; vi a(n); rep(i, n) cin >> a[i]; ll sum = 0; rep(i, n) sum += a[i]; int m = (n - 1) / 2; rep(i, n) { ll sum2 = 0; int k = i + 1; rep(j, m) { if (k >= n) k -= n; sum2 += a[k]; k += 2; } sum2 *= 2; cout << sum - sum2 << "\n"; } return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; typedef long long ll; typedef pair<int, int> P; typedef vector<int> vi; const int m = 1000000007; int main() { int n; cin >> n; vi a(n); rep(i, n) cin >> a[i]; ll sum = 0; rep(i, n) sum += a[i]; int m = (n - 1) / 2; for (int i = 1; i < n; i += 2) { sum -= a[i] * 2; } sum /= 2; cout << sum * 2 << "\n"; for (int i = 0; i < n - 1; i++) { sum = (a[i] - sum); cout << sum * 2 << "\n"; } return 0; }
replace
17
28
17
25
TLE
p02984
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; long long n; vector<long long> a, ans; void solve(); int main() { cin >> n; a.resize(n); ans.assign(n, 0); for (int i = 0; i < n; ++i) cin >> a[i]; solve(); for (int i = 0; i < n; ++i) { if (i != 0) cout << " "; cout << ans[i]; } cout << endl; return 0; } void solve() { long long sum = 0; for (int i = 0; i < n; ++i) if (i % 2) sum -= a[i]; else sum += a[i]; ans[0] = sum; for (int i = 0; i < n; ++i) { ans[i + 1] = (a[i] - ans[i] / 2) * 2; } }
#include <bits/stdc++.h> using namespace std; long long n; vector<long long> a, ans; void solve(); int main() { cin >> n; a.resize(n); ans.assign(n, 0); for (int i = 0; i < n; ++i) cin >> a[i]; solve(); for (int i = 0; i < n; ++i) { if (i != 0) cout << " "; cout << ans[i]; } cout << endl; return 0; } void solve() { long long sum = 0; for (int i = 0; i < n; ++i) if (i % 2) sum -= a[i]; else sum += a[i]; ans[0] = sum; for (int i = 0; i < n; ++i) { ans[(i + 1) % n] = (a[i] - ans[i] / 2) * 2; } }
replace
33
34
33
34
-6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)
p02984
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) const long long mod = 2019; using namespace std; using ll = long long; int main() { int n; ll a_sum = 0; cin >> n; vector<ll> a(n); vector<ll> b(n); rep(i, n) { cin >> a[i]; a_sum += a[i]; } rep(i, n) { if (i == 0) { for (int j = i + 1; j < n; j += 2) { a_sum -= 2 * a[j]; } b[0] = a_sum; } b[i + 1] = 2 * a[i] - b[i]; cout << b[i]; if (i < n - 1) { cout << " "; } } cout << endl; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) const long long mod = 2019; using namespace std; using ll = long long; int main() { int n; ll a_sum = 0; cin >> n; vector<ll> a(n); vector<ll> b(n); rep(i, n) { cin >> a[i]; a_sum += a[i]; } rep(i, n) { if (i == 0) { for (int j = i + 1; j < n; j += 2) { a_sum -= 2 * a[j]; } b[0] = a_sum; } if (i < n - 1) { b[i + 1] = 2 * a[i] - b[i]; } cout << b[i]; if (i < n - 1) { cout << " "; } } cout << endl; }
replace
23
24
23
26
0
p02984
C++
Time Limit Exceeded
// #define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(v) v.begin(), v.end() #define Graph vector<vector<int>> typedef long long ll; typedef pair<int, int> P; const int INF = 1000000007; int main() { int N; cin >> N; vector<ll> A(N), B(N); ll sum = 0; rep(i, N) { cin >> A[i]; sum += A[i]; } rep(i, N) { B[i] = sum; int sub = i + 1; rep(j, N / 2) { B[i] -= 2 * A[sub % N]; sub += 2; } } rep(i, N) cout << B[i] << ' '; cout << endl; }
// #define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(v) v.begin(), v.end() #define Graph vector<vector<int>> typedef long long ll; typedef pair<int, int> P; const int INF = 1000000007; int main() { int N; cin >> N; vector<ll> A(N), B(N); ll sum = 0; rep(i, N) { cin >> A[i]; sum += A[i]; } B[0] = sum; for (int i = 1; i < N; i += 2) B[0] -= 2 * A[i]; for (int i = 1; i < N; i++) { B[i] = 2 * A[i - 1] - B[i - 1]; } rep(i, N) cout << B[i] << ' '; cout << endl; }
replace
19
26
19
26
TLE
p02984
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> #define inf 2147483647 #define rep(i, a, b) for (int i = a; i < b; i++) #define rr read() #define ll long long #define repb(i, a, b) for (int i = a; i <= b; i++) #define CloseIo ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0) using namespace std; int read() { char ch = getchar(); int x = 0, f = 1; while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while ('0' <= ch && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } const int N = 1e5 + 5; ll a[N] = {0}; int main() { ll n; CloseIo; cin >> n; rep(i, 0, n) cin >> a[i]; rep(i, 0, n) { ll ans = 0; ll flag = 1; rep(j, 0, n) { ans += (a[((i + j) % n)] * flag); flag = -flag; } printf("%lld%s", ans, i == n - 1 ? "" : " "); } }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> #define inf 2147483647 #define rep(i, a, b) for (int i = a; i < b; i++) #define rr read() #define ll long long #define repb(i, a, b) for (int i = a; i <= b; i++) #define CloseIo ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0) using namespace std; int read() { char ch = getchar(); int x = 0, f = 1; while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while ('0' <= ch && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } const int N = 1e5 + 5; ll a[N] = {0}; int main() { ll n; CloseIo; cin >> n; rep(i, 0, n) cin >> a[i]; ll ans = 0; ll flag = 1; rep(j, 0, n) { ans += (a[j] * flag); flag = -flag; } vector<ll> v; v.push_back(ans); for (int i = 0; i < n - 1; i++) { ans = 2 * a[i] - ans; // printf("%lld",ans); v.push_back(ans); } for (int i = 0; i < v.size(); i++) { printf("%lld%s", v[i], i == v.size() - 1 ? "" : " "); } }
replace
40
48
40
56
TLE
p02984
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int64_t> A(N); vector<int64_t> x(N); for (int i = 0; i < N; i++) { cin >> A[i]; } int64_t sum = 0; for (int i = 0; i < N; i++) { if (i % 2 == 0) { sum += A[i]; } else { sum -= A[i]; } } x[0] = sum; for (int i = 1; i < N; i--) { x[i] = A[i - 1] * 2 - x[i - 1]; } for (int i = 0; i < N - 1; i++) { cout << x[i] << " "; } cout << x[N - 1] << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int64_t> A(N); vector<int64_t> x(N); for (int i = 0; i < N; i++) { cin >> A[i]; } int64_t sum = 0; for (int i = 0; i < N; i++) { if (i % 2 == 0) { sum += A[i]; } else { sum -= A[i]; } } x[0] = sum; for (int i = 1; i < N; i++) { x[i] = A[i - 1] * 2 - x[i - 1]; } for (int i = 0; i < N - 1; i++) { cout << x[i] << " "; } cout << x[N - 1] << endl; }
replace
20
21
20
21
-11
p02984
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; #define INF (1LL << 60) #define REP(i, m, n) for (ll(i) = (m), i##_len = (n); (i) < (i##_len); ++(i)) #define FORR(i, v) for (auto(i) : v) #define ALL(x) (x).begin(), (x).end() #define _PR(x) cout << (x) << endl #define _PS(x) cout << (x) << " " #define SZ(x) ((int)(x).size()) #define MAX(a, b) (((a) > (b)) ? (a) : (b)) #define MIN(a, b) (((a) < (b)) ? (a) : (b)) #define pb push_back int main() { ll N; cin >> N; vector<ll> A(N); REP(i, 0, N) cin >> A[i]; ll S = 0; REP(i, 0, N) S += A[i]; ll T; REP(i, 0, N) { T = 0; if (i % 2 == 0) { for (ll j = 0; j <= i - 2; j += 2) T += A[j]; for (ll j = i + 1; j <= N - 2; j += 2) T += A[j]; } else { for (ll j = 1; j <= i - 2; j += 2) T += A[j]; for (ll j = i + 1; j <= N - 1; j += 2) T += A[j]; } _PS(S - T * 2); } return 0; } /* */
#include <bits/stdc++.h> using namespace std; using ll = long long; #define INF (1LL << 60) #define REP(i, m, n) for (ll(i) = (m), i##_len = (n); (i) < (i##_len); ++(i)) #define FORR(i, v) for (auto(i) : v) #define ALL(x) (x).begin(), (x).end() #define _PR(x) cout << (x) << endl #define _PS(x) cout << (x) << " " #define SZ(x) ((int)(x).size()) #define MAX(a, b) (((a) > (b)) ? (a) : (b)) #define MIN(a, b) (((a) < (b)) ? (a) : (b)) #define pb push_back int main() { ll N; cin >> N; vector<ll> A(N); REP(i, 0, N) cin >> A[i]; ll S = 0; REP(i, 0, N) S += A[i]; ll T = 0; for (ll i = 1; i <= N - 2; i += 2) T += A[i]; ll x = S - T * 2; _PS(x); REP(i, 1, N) { x = 2 * A[i - 1] - x; _PS(x); } return 0; } /* */
replace
26
41
26
34
TLE
p02984
Python
Runtime Error
import sys import numpy as np def input(): return sys.stdin.readline().rstrip() def main(): n = int(input()) a = [int(e) for e in input().split()] va = np.array(a) m = np.identity(n, dtype=int) for i in range(n): if i < n - 1: m[i][i + 1] = 1 else: m[i][0] = 1 vx = np.linalg.solve(m, va).astype(np.int) * 2 print(*vx) main()
import sys import numpy as np def input(): return sys.stdin.readline().rstrip() def main(): n = int(input()) a = [int(e) for e in input().split()] x = [0 for _ in range(n)] x[0] = sum(a) - (sum(a[1:n:2]) * 2) for i in range(2, n, 2): x[i] = x[i - 2] + ((a[i - 1] - a[i - 2]) * 2) x[1] = sum(a) - (sum(a[2:n:2]) * 2) for i in range(3, n, 2): x[i] = x[i - 2] + ((a[i - 1] - a[i - 2]) * 2) print(*x) main()
replace
11
20
11
19
TLE
p02984
Python
Time Limit Exceeded
from sys import stdin, setrecursionlimit def main(): input = stdin.buffer.readline n = int(input()) a = list(map(int, input().split())) x_sum = sum(a) // 2 x = [0] * n for i in range(n): tmp = x_sum idx = (i + 1) % n for _ in range((n - 1) // 2): tmp -= a[idx] idx += 2 idx %= n x[i] = 2 * tmp print(*x) if __name__ == "__main__": setrecursionlimit(10000) main()
from sys import stdin, setrecursionlimit def main(): input = stdin.buffer.readline n = int(input()) a = list(map(int, input().split())) x_sum = sum(a) // 2 x = [0] * n tmp = x_sum idx = 1 for _ in range((n - 1) // 2): tmp -= a[idx] idx += 2 idx %= n x[0] = 2 * tmp for i in range(n - 1): x[i + 1] = 2 * (a[i] - x[i] // 2) print(*x) if __name__ == "__main__": setrecursionlimit(10000) main()
replace
9
17
9
18
TLE
p02984
Python
Time Limit Exceeded
n = int(input()) a = list(map(int, input().split())) b = sum(a) / 2 p = int((n - 1) / 2) x = [] for i in range(n): t = b for j in range(p): t -= a[(2 * j + i + 1) % n] x.append(t * 2) for i in range(n): print(int(x[i]), end=" ")
n = int(input()) a = list(map(int, input().split())) b = sum(a) / 2 p = int((n - 1) / 2) x = [] t = b for j in range(p): t -= a[2 * j + 1] x.append(t * 2) for i in range(n - 1): t = a[i] - x[i] / 2 x.append(t * 2) for i in range(n): print(int(x[i]), end=" ")
replace
7
11
7
14
TLE
p02984
Python
Time Limit Exceeded
#!/usr/bin/env python3 n = int(input()) a = [int(x) for x in input().split()] ans = [sum(a[(i + j) % n] * (-1) ** (j % 2) for j in range(n)) for i in range(n)] print(" ".join(str(x) for x in ans))
#!/usr/bin/env python3 n = int(input()) a = [int(x) for x in input().split()] v = sum(a[i] * (-1) ** (i % 2) for i in range(n)) ans = [0] * n for i in range(n): ans[i] = v v = -v + 2 * a[i] print(" ".join(str(x) for x in ans))
replace
4
5
4
11
TLE
p02984
Python
Time Limit Exceeded
#!/usr/bin/env python3 # -*- coding: utf-8 -*- def solve(): N = int(input()) A = list(map(int, input().split())) ans = [] for i in range(N): s = 0 for j, a in enumerate(A): s += a * (1 if (((j - i) % N) % 2 == 0) else -1) ans.append(str(s)) return ans def main(): print(" ".join(solve())) if __name__ == "__main__": main()
#!/usr/bin/env python3 # -*- coding: utf-8 -*- def solve(): N = int(input()) A = list(map(int, input().split())) ans = [] s = 0 for j, a in enumerate(A): s += a * (1 if ((j % N) % 2 == 0) else -1) ans.append(str(s)) for i in range(1, N): s = -s + 2 * A[i - 1] ans.append(str(s)) return ans def main(): print(" ".join(solve())) if __name__ == "__main__": main()
replace
8
12
8
14
TLE
p02984
Python
Time Limit Exceeded
from collections import deque n = int(input()) A = list(map(int, input().split())) sum_even = sum(A[::2]) sum_odd = sum(A[1::2]) + A[0] ANS = A[:] A = deque(A) minus = False for _ in range(n - 1): A.rotate(-1) for i in range(n): if minus: ANS[i] -= A[i] else: ANS[i] += A[i] minus = not minus ANS = deque(ANS) ANS.rotate() print(*ANS)
from collections import deque n = int(input()) A = list(map(int, input().split())) B = [0] * n B[0] = A[0] B[1] = A[1] for i in range(2, n): B[i] = B[i - 2] + A[i] ANS = [0] * n for i in range(n): ans = 0 if i % 2 == 0: ans += B[-1] - B[-2] else: ans += B[-2] - B[-1] if i - 2 >= 0: ans -= B[i - 2] * 2 if i - 1 >= 0: ans += B[i - 1] * 2 ANS[i] = ans print(*ANS)
replace
5
20
5
27
TLE
p02984
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; // Optimisations #pragma GCC target("avx2") #pragma GCC optimization("unroll-loops") #pragma GCC optimize("O2") // shortcuts for functions #define pb push_back #define mp make_pair #define ff first #define ss second #define endl "\n" #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() #define th(n) cout << n << endl #define gc getchar_unlocked #define ms(s, n) memset(s, n, sizeof(s)) #define prec(n) fixed << setprecision(n) #define n_l '\n' // make it python #define gcd __gcd #define append push_back #define str to_string #define upper(s) transform(s.begin(), s.end(), s.begin(), ::toupper) #define lower(s) transform(s.begin(), s.end(), s.begin(), ::tolower) #define print(arr) \ for (auto el : arr) \ cout << el << " "; \ cout << endl // utility functions shortcuts #define max3(a, b, c) max(a, max(b, c)) #define min3(a, b, c) min(a, min(b, c)) #define sswap(a, b) \ { \ a = a ^ b; \ b = a ^ b; \ a = a ^ b; \ } #define swap(a, b) \ { \ auto temp = a; \ a = b; \ b = temp; \ } #define init(dp) memset(dp, -1, sizeof(dp)); #define set0(dp) memset(dp, 0, sizeof(dp)); #define bits(x) __builtin_popcount(x) #define SORT(v) sort(all(v)) #define endl "\n" #define forr(i, n) for (ll i = 0; i < n; i++) #define formatrix(i, n) for (ll i = 0; i < n; i++, cout << "\n") #define eof (scanf("%d", &n)) != EOF // declaration shortcuts #define vi vector<int> #define vll vector<ll> #define vvi vector<vector<int>> #define vvl vector<vector<ll>> #define pll pair<ll, ll> #define ppl pair<ll, pp> #define ull unsigned long long #define ll long long #define mll map<ll, ll> #define sll set<ll> #define uni(v) v.erase(unique(v.begin(), v.end()), v.end()); #define ini(a, v) memset(a, v, sizeof(a)) // Constants constexpr int dx[] = {-1, 0, 1, 0, 1, 1, -1, -1}; constexpr int dy[] = {0, -1, 0, 1, 1, -1, 1, -1}; constexpr ll INF = 1999999999999999997; constexpr int inf = INT_MAX; constexpr int MAXSIZE = int(1e6) + 5; constexpr auto PI = 3.14159265358979323846L; constexpr auto oo = numeric_limits<int>::max() / 2 - 2; constexpr auto eps = 1e-6; constexpr auto mod = 1000000007; constexpr auto MOD = 1000000007; constexpr auto MOD9 = 1000000009; // Debugging // For reference: https://codeforces.com/blog/entry/65311 #define dbg(...) \ cout << "[" << #__VA_ARGS__ << "]: "; \ cout << to_string(__VA_ARGS__) << endl template <typename T, size_t N> int SIZE(const T (&t)[N]) { return N; } template <typename T> int SIZE(const T &t) { return t.size(); } string to_string(string s, int x1 = 0, int x2 = 1e9) { return '"' + ((x1 < s.size()) ? s.substr(x1, x2 - x1 + 1) : "") + '"'; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } string to_string(char c) { return string({c}); } template <size_t N> string to_string(bitset<N> &b, int x1 = 0, int x2 = 1e9) { string t = ""; for (int __iii__ = min(x1, SIZE(b)), __jjj__ = min(x2, SIZE(b) - 1); __iii__ <= __jjj__; ++__iii__) { t += b[__iii__] + '0'; } return '"' + t + '"'; } template <typename A, typename... C> string to_string(A(&v), int x1 = 0, int x2 = 1e9, C... coords); int l_v_l_v_l = 0, t_a_b_s = 0; template <typename A, typename B> string to_string(pair<A, B> &p) { l_v_l_v_l++; string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; l_v_l_v_l--; return res; } template <typename A, typename... C> string to_string(A(&v), int x1, int x2, C... coords) { int rnk = rank<A>::value; string tab(t_a_b_s, ' '); string res = ""; bool first = true; if (l_v_l_v_l == 0) res += n_l; res += tab + "["; x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v)); auto l = begin(v); advance(l, x1); auto r = l; advance(r, (x2 - x1) + (x2 < SIZE(v))); for (auto e = l; e != r; e = next(e)) { if (!first) { res += ", "; } first = false; l_v_l_v_l++; if (e != l) { if (rnk > 1) { res += n_l; t_a_b_s = l_v_l_v_l; }; } else { t_a_b_s = 0; } res += to_string(*e, coords...); l_v_l_v_l--; } res += "]"; if (l_v_l_v_l == 0) res += n_l; return res; } void dbgs() { ; } template <typename Heads, typename... Tails> void dbgs(Heads H, Tails... T) { cout << to_string(H) << " | "; dbgs(T...); } #define dbgm(...) \ cout << "[" << #__VA_ARGS__ << "]: "; \ dbgs(__VA_ARGS__); \ cout << endl; #define n_l '\n' int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll n; cin >> n; vll v(n); vll ans(n); ll sum = 0; ll sign = -1; forr(i, n) cin >> v[i]; for (int i = 0; i < n - 1; i++) { sum += sign * v[i]; sign *= -1; } ans[0] = (v[n - 1] - sum) / 2; for (int i = 0; i < n; i++) ans[i + 1] = v[i] - ans[i]; for (auto i : ans) cout << i * 2 << " "; }
#include <bits/stdc++.h> using namespace std; // Optimisations #pragma GCC target("avx2") #pragma GCC optimization("unroll-loops") #pragma GCC optimize("O2") // shortcuts for functions #define pb push_back #define mp make_pair #define ff first #define ss second #define endl "\n" #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() #define th(n) cout << n << endl #define gc getchar_unlocked #define ms(s, n) memset(s, n, sizeof(s)) #define prec(n) fixed << setprecision(n) #define n_l '\n' // make it python #define gcd __gcd #define append push_back #define str to_string #define upper(s) transform(s.begin(), s.end(), s.begin(), ::toupper) #define lower(s) transform(s.begin(), s.end(), s.begin(), ::tolower) #define print(arr) \ for (auto el : arr) \ cout << el << " "; \ cout << endl // utility functions shortcuts #define max3(a, b, c) max(a, max(b, c)) #define min3(a, b, c) min(a, min(b, c)) #define sswap(a, b) \ { \ a = a ^ b; \ b = a ^ b; \ a = a ^ b; \ } #define swap(a, b) \ { \ auto temp = a; \ a = b; \ b = temp; \ } #define init(dp) memset(dp, -1, sizeof(dp)); #define set0(dp) memset(dp, 0, sizeof(dp)); #define bits(x) __builtin_popcount(x) #define SORT(v) sort(all(v)) #define endl "\n" #define forr(i, n) for (ll i = 0; i < n; i++) #define formatrix(i, n) for (ll i = 0; i < n; i++, cout << "\n") #define eof (scanf("%d", &n)) != EOF // declaration shortcuts #define vi vector<int> #define vll vector<ll> #define vvi vector<vector<int>> #define vvl vector<vector<ll>> #define pll pair<ll, ll> #define ppl pair<ll, pp> #define ull unsigned long long #define ll long long #define mll map<ll, ll> #define sll set<ll> #define uni(v) v.erase(unique(v.begin(), v.end()), v.end()); #define ini(a, v) memset(a, v, sizeof(a)) // Constants constexpr int dx[] = {-1, 0, 1, 0, 1, 1, -1, -1}; constexpr int dy[] = {0, -1, 0, 1, 1, -1, 1, -1}; constexpr ll INF = 1999999999999999997; constexpr int inf = INT_MAX; constexpr int MAXSIZE = int(1e6) + 5; constexpr auto PI = 3.14159265358979323846L; constexpr auto oo = numeric_limits<int>::max() / 2 - 2; constexpr auto eps = 1e-6; constexpr auto mod = 1000000007; constexpr auto MOD = 1000000007; constexpr auto MOD9 = 1000000009; // Debugging // For reference: https://codeforces.com/blog/entry/65311 #define dbg(...) \ cout << "[" << #__VA_ARGS__ << "]: "; \ cout << to_string(__VA_ARGS__) << endl template <typename T, size_t N> int SIZE(const T (&t)[N]) { return N; } template <typename T> int SIZE(const T &t) { return t.size(); } string to_string(string s, int x1 = 0, int x2 = 1e9) { return '"' + ((x1 < s.size()) ? s.substr(x1, x2 - x1 + 1) : "") + '"'; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } string to_string(char c) { return string({c}); } template <size_t N> string to_string(bitset<N> &b, int x1 = 0, int x2 = 1e9) { string t = ""; for (int __iii__ = min(x1, SIZE(b)), __jjj__ = min(x2, SIZE(b) - 1); __iii__ <= __jjj__; ++__iii__) { t += b[__iii__] + '0'; } return '"' + t + '"'; } template <typename A, typename... C> string to_string(A(&v), int x1 = 0, int x2 = 1e9, C... coords); int l_v_l_v_l = 0, t_a_b_s = 0; template <typename A, typename B> string to_string(pair<A, B> &p) { l_v_l_v_l++; string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; l_v_l_v_l--; return res; } template <typename A, typename... C> string to_string(A(&v), int x1, int x2, C... coords) { int rnk = rank<A>::value; string tab(t_a_b_s, ' '); string res = ""; bool first = true; if (l_v_l_v_l == 0) res += n_l; res += tab + "["; x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v)); auto l = begin(v); advance(l, x1); auto r = l; advance(r, (x2 - x1) + (x2 < SIZE(v))); for (auto e = l; e != r; e = next(e)) { if (!first) { res += ", "; } first = false; l_v_l_v_l++; if (e != l) { if (rnk > 1) { res += n_l; t_a_b_s = l_v_l_v_l; }; } else { t_a_b_s = 0; } res += to_string(*e, coords...); l_v_l_v_l--; } res += "]"; if (l_v_l_v_l == 0) res += n_l; return res; } void dbgs() { ; } template <typename Heads, typename... Tails> void dbgs(Heads H, Tails... T) { cout << to_string(H) << " | "; dbgs(T...); } #define dbgm(...) \ cout << "[" << #__VA_ARGS__ << "]: "; \ dbgs(__VA_ARGS__); \ cout << endl; #define n_l '\n' int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll n; cin >> n; vll v(n); vll ans(n); ll sum = 0; ll sign = -1; forr(i, n) cin >> v[i]; for (int i = 0; i < n - 1; i++) { sum += sign * v[i]; sign *= -1; } ans[0] = (v[n - 1] - sum) / 2; for (int i = 0; i < n - 1; i++) ans[i + 1] = v[i] - ans[i]; for (auto i : ans) cout << i * 2 << " "; }
replace
170
171
170
171
0
p02984
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; #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 rep(i, n) for (int i = 0; i < (n); ++i) #define repe(i, n) for (int i = 0; i <= (n); ++i) #define ALL(v) (v).begin(), (v).end() #define SP cout << fixed << setprecision(10) typedef pair<int, int> P; const int INF = (int)1e9; const int MOD = (int)1e9 + 7; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int main(void) { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; ll a[n]; rep(i, n) cin >> a[i]; ll l = -1, r = 2e9; while (1) { long double mid = (l + r) / 2; long double b[n]; long long memo[n]; rep(i, n) b[i] = 0; b[n - 1] += mid / 2; b[0] += mid / 2; memo[0] = mid; rep(i, n - 1) { long double rain = (long double)a[i] - b[i]; b[i] += rain; b[i + 1] += rain; memo[i + 1] = rain * 2; } if (a[n - 1] == b[n - 1]) { rep(i, n) { if (i == n - 1) cout << memo[i] << endl; else cout << memo[i] << " "; } return 0; } else if (a[n - 1] > b[n - 1]) { l = mid; } else { r = mid; } } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #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 rep(i, n) for (int i = 0; i < (n); ++i) #define repe(i, n) for (int i = 0; i <= (n); ++i) #define ALL(v) (v).begin(), (v).end() #define SP cout << fixed << setprecision(10) typedef pair<int, int> P; const int INF = (int)1e9; const int MOD = (int)1e9 + 7; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int main(void) { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; ll a[n]; rep(i, n) cin >> a[i]; ll l = -1LL, r = (ll)2e9 + (ll)10; while (1) { long double mid = (l + r) / 2; long double b[n]; long long memo[n]; rep(i, n) b[i] = 0; b[n - 1] += mid / 2; b[0] += mid / 2; memo[0] = mid; rep(i, n - 1) { long double rain = (long double)a[i] - b[i]; b[i] += rain; b[i + 1] += rain; memo[i + 1] = rain * 2; } if (a[n - 1] == b[n - 1]) { rep(i, n) { if (i == n - 1) cout << memo[i] << endl; else cout << memo[i] << " "; } return 0; } else if (a[n - 1] > b[n - 1]) { l = mid; } else { r = mid; } } return 0; }
replace
25
26
25
26
TLE
p02984
C++
Runtime Error
// #pragma GCC optimize("Ofast") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace std::chrono; #define int long long #define ll long long auto start_time = system_clock::now(); //@formatter:off #ifdef _DEBUG // 区間削除は出来ない template <class T> struct my_pbds_tree { set<T> s; auto begin() { return s.begin(); } auto end() { return s.end(); } auto rbegin() { return s.rbegin(); } auto rend() { return s.rend(); } auto empty() { return s.empty(); } auto size() { return s.size(); } void clear() { s.clear(); } template <class U> void insert(U v) { s.insert(v); } template <class U> void operator+=(U v) { insert(v); } template <class F> auto erase(F v) { return s.erase(v); } template <class U> auto find(U v) { return s.find(v); } template <class U> auto lower_bound(U v) { return s.lower_bound(v); } template <class U> auto upper_bound(U v) { return s.upper_bound(v); } auto find_by_order(ll k) { auto it = s.begin(); for (ll i = 0; i < k; i++) it++; return it; } auto order_of_key(ll v) { auto it = s.begin(); ll i = 0; for (; it != s.end() && *it < v; i++) it++; return i; } }; #define pbds(T) my_pbds_tree<T> #else #define unordered_map __gnu_pbds::gp_hash_table // find_by_order(k) k番目のイテレーター // order_of_key(k) k以上が前から何番目か #define pbds(U) \ __gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag, \ __gnu_pbds::tree_order_statistics_node_update> #endif struct xorshift { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } size_t operator()(std::pair<ll, ll> x) const { ll v = ((x.first) << 32) | x.second; static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(v + FIXED_RANDOM); } }; template <class U, class L> void operator+=( __gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update> &s, L v) { s.insert(v); } // 衝突対策 #define ws wszzzz template <class A, class B, class C> struct T2 { A f; B s; C t; T2() { f = 0, s = 0, t = 0; } T2(A f, B s, C t) : f(f), s(s), t(t) {} bool operator<(const T2 &r) const { return f != r.f ? f < r.f : s != r.s ? s < r.s : t < r.t; /*return f != r.f ? f > r.f : s != r.s ?n s > r.s : t > r.t; 大きい順 */ } bool operator>(const T2 &r) const { return f != r.f ? f > r.f : s != r.s ? s > r.s : t > r.t; /*return f != r.f ? f > r.f : s != r.s ? s > r.s : t > r.t; 小さい順 */ } bool operator==(const T2 &r) const { return f == r.f && s == r.s && t == r.t; } bool operator!=(const T2 &r) const { return f != r.f || s != r.s || t != r.t; } }; template <class A, class B, class C, class D> struct F2 { A a; B b; C c; D d; F2() { a = 0, b = 0, c = 0, d = 0; } F2(A a, B b, C c, D d) : a(a), b(b), c(c), d(d) {} bool operator<(const F2 &r) const { return a != r.a ? a < r.a : b != r.b ? b < r.b : c != r.c ? c < r.c : d < r.d; /* return a != r.a ? a > r.a : b != r.b ? b > r.b : c != r.c ? c > r.c : d > r.d;*/ } bool operator>(const F2 &r) const { return a != r.a ? a > r.a : b != r.b ? b > r.b : c != r.c ? c > r.c : d > r.d; /* return a != r.a ? a < r.a : b != r.b ? b < r.b : c != r.c ? c < r.c : d < r.d;*/ } bool operator==(const F2 &r) const { return a == r.a && b == r.b && c == r.c && d == r.d; } bool operator!=(const F2 &r) const { return a != r.a || b != r.b || c != r.c || d != r.d; } ll operator[](ll i) { assert(i < 4); return i == 0 ? a : i == 1 ? b : i == 2 ? c : d; } }; typedef T2<ll, ll, ll> T; typedef F2<ll, ll, ll, ll> F; T mt(ll a, ll b, ll c) { return T(a, b, c); } //@マクロ省略系 型,構造 #define double long double #define ull unsigned long long using dou = double; using itn = int; using str = string; using bo = bool; #define au auto using P = pair<ll, ll>; using pd = pair<dou, dou>; #define fi first #define se second #define beg begin #define rbeg rbegin #define con continue #define bre break #define brk break #define is == #define el else #define elf else if #define wh while #define maxq 1 #define minq -1 #define ZERO(a) memset(a, 0, sizeof(a)) #define MINUS(a) memset(a, 0xff, sizeof(a)) #define MALLOC(type, len) (type *)malloc((len) * sizeof(type)) #define lam(right) [&](ll &p) { return p right; } // マクロ省略系 コンテナ using vi = vector<ll>; using vb = vector<bool>; using vs = vector<string>; using vd = vector<double>; using vc = vector<char>; using vp = vector<P>; using vt = vector<T>; #define V vector #define o_vvt(o1, o2, o3, o4, name, ...) name #define vvt0(t) V<V<t>> #define vvt1(t, a) V<V<t>> a #define vvt2(t, a, b) V<V<t>> a(b) #define vvt3(t, a, b, c) V<V<t>> a(b, V<t>(c)) #define vvt4(t, a, b, c, d) V<V<t>> a(b, V<t>(c, d)) #define vvi(...) \ o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(ll, __VA_ARGS__) #define vvb(...) \ o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(bool, __VA_ARGS__) #define vvs(...) \ o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(string, __VA_ARGS__) #define vvd(...) \ o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(double, __VA_ARGS__) #define vvc(...) \ o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(char, __VA_ARGS__) #define vvp(...) \ o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(P, __VA_ARGS__) template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); } template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); } #define vni(name, ...) auto name = make_v<ll>(__VA_ARGS__) #define vnb(name, ...) auto name = make_v<bool>(__VA_ARGS__) #define vns(name, ...) auto name = make_v<string>(__VA_ARGS__) #define vnd(name, ...) auto name = make_v<double>(__VA_ARGS__) #define vnc(name, ...) auto name = make_v<char>(__VA_ARGS__) #define vnp(name, ...) auto name = make_v<P>(__VA_ARGS__) #define PQ priority_queue<ll, vector<ll>, greater<ll>> #define tos to_string using mapi = map<ll, ll>; using mapp = map<P, ll>; using mapd = map<dou, ll>; using mapc = map<char, ll>; using maps = map<str, ll>; using seti = set<ll>; using setd = set<dou>; using setc = set<char>; using sets = set<str>; using qui = queue<ll>; #define bset bitset #define uset unordered_set #define useti unordered_set<ll, ll, xorshift> #define mset multiset #define mseti multiset<ll> #define umap unordered_map #define umapi unordered_map<ll, ll, xorshift> #define umapp unordered_map<P, ll, xorshift> #define mmap multimap template <class T> struct pq { priority_queue<T, vector<T>, greater<T>> q; /*小さい順*/ T su = 0; void clear() { q = priority_queue<T, vector<T>, greater<T>>(); su = 0; } void operator+=(T v) { su += v; q.push(v); } T sum() { return su; } T top() { return q.top(); } void pop() { su -= q.top(); q.pop(); } T poll() { T ret = q.top(); su -= ret; q.pop(); return ret; } ll size() { return q.size(); } }; template <class T> struct pqg { priority_queue<T> q; /*大きい順*/ T su = 0; void clear() { q = priority_queue<T>(); su = 0; } void operator+=(T v) { su += v; q.push(v); } T sum() { return su; } T top() { return q.top(); } void pop() { su -= q.top(); q.pop(); } T poll() { T ret = q.top(); su -= ret; q.pop(); return ret; } ll size() { return q.size(); } }; #define pqi pq<ll> #define pqgi pqg<ll> // マクロ 繰り返し #define o_rep(o1, o2, o3, o4, name, ...) name #define rep1(n) for (ll rep1i = 0, rep1lim = n; rep1i < rep1lim; ++rep1i) #define rep2(i, n) for (ll i = 0, rep2lim = n; i < rep2lim; ++i) #define rep3(i, m, n) for (ll i = m, rep3lim = n; i < rep3lim; ++i) #define rep4(i, m, n, ad) for (ll i = m, rep4lim = n; i < rep4lim; i += ad) #define rep(...) o_rep(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__) #define rer2(i, n) for (ll i = n; i >= 0; i--) #define rer3(i, m, n) for (ll i = m, rer3lim = n; i >= rer3lim; i--) #define rer4(i, m, n, dec) for (ll i = m, rer4lim = n; i >= rer4lim; i -= dec) #define rer(...) o_rep(__VA_ARGS__, rer4, rer3, rer2, )(__VA_ARGS__) #define reps2(i, j, n) \ for (ll i = 0, reps2lim = n; i < reps2lim; ++i) \ for (ll j = 0; j < reps2lim; ++j) #define reps3(i, j, k, n) \ for (ll i = 0, reps3lim = n; i < reps3lim; ++i) \ for (ll j = 0; j < reps3lim; ++j) \ for (ll k = 0; k < reps3lim; ++k) #define reps4(i, j, k, l, n) \ for (ll i = 0, reps4lim = n; i < reps4lim; ++i) \ for (ll j = 0; j < reps4lim; ++j) \ for (ll k = 0; k < reps4lim; ++k) \ for (ll l = 0; l < reps4lim; ++l) #define o_reps(o1, o2, o3, o4, o5, name, ...) name #define reps(...) o_reps(__VA_ARGS__, reps4, reps3, reps2, rep2, )(__VA_ARGS__) #define repss(i, j, k, a, b, c) \ for (ll i = 0; i < a; ++i) \ for (ll j = 0; j < b; ++j) \ for (ll k = 0; k < c; ++k) #define fora(a, b) for (auto &&a : b) #define forg(gi, ve) \ for (ll gi = 0, forglim = ve.size(), f, t, c; \ gi < forglim && (f = ve[gi].f, t = ve[gi].t, c = ve[gi].c, true); ++gi) #define fort(gi, ve) \ for (ll gi = 0, f, t, c; \ gi < ve.size() && (f = ve[gi].f, t = ve[gi].t, c = ve[gi].c, true); \ ++gi) \ if (t != p) #define form(st, l, r) \ for (auto &&it = st.lower_bound(l); it != st.end() && (*it).fi < r; ++it) #define forit(st, l, r) \ for (auto &&it = st.lower_bound(l); it != st.end() && (*it) < r;) // マクロ 定数 #define k3 1010 #define k4 10101 #define k5 101010 #define k6 1010101 #define k7 10101010 const ll inf = (ll)1e9 + 100; const ll linf = (ll)1e18 + 100; const char infc = '{'; const string infs = "{"; const double eps = 1e-9; const double PI = 3.1415926535897932384626433832795029L; ll ma = numeric_limits<ll>::min(); ll mi = numeric_limits<ll>::max(); // マクロ省略形 関数等 #define arsz(a) (sizeof(a) / sizeof(a[0])) #define sz(a) ((ll)(a).size()) #define mp make_pair #define pb pop_back #define pf push_front #define eb emplace_back #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() constexpr bool ev(ll a) { return !(a & 1); } constexpr bool od(ll a) { return (a & 1); } //@拡張系 こう出来るべきというもの // 埋め込み 存在を意識せずに機能を増やされているもの //@formatter:on namespace std { template <> class hash<std::pair<signed, signed>> { public: size_t operator()(const std::pair<signed, signed> &x) const { return hash<ll>()(((ll)x.first << 32) | x.second); } }; template <> class hash<std::pair<ll, ll>> { public : /*大きいllが渡されると、<<32でオーバーフローするがとりあえず問題ないと判断*/ size_t operator()(const std::pair<ll, ll> &x) const { return hash<ll>()(((ll)x.first << 32) | x.second); } }; } // namespace std //@formatter:off // stream まとめ istream &operator>>(istream &iss, P &a) { iss >> a.first >> a.second; return iss; } template <typename T> istream &operator>>(istream &iss, vector<T> &vec) { for (T &x : vec) iss >> x; return iss; } template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> p) { os << p.fi << " " << p.se << endl; return os; } ostream &operator<<(ostream &os, T p) { os << p.f << " " << p.s << " " << p.t; return os; } ostream &operator<<(ostream &os, F p) { os << p.a << " " << p.b << " " << p.c << " " << p.d; return os; } template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) { for (ll i = 0; i < vec.size(); ++i) os << vec[i] << (i + 1 == vec.size() ? "" : " "); return os; } template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) { for (ll i = 0; i < vec.size(); ++i) { for (ll j = 0; j < vec[i].size(); ++j) { os << vec[i][j] << " "; } os << endl; } return os; } template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &m) { for (auto &&v : m) os << v; return os; } template <typename W, typename H> void resize(vector<W> &vec, const H head) { vec.resize(head); } template <typename W, typename H, typename... T> void resize(vector<W> &vec, const H &head, const T... tail) { vec.resize(head); for (auto &v : vec) resize(v, tail...); } template <typename T, typename F> bool all_of2(T &v, F f) { return f(v); } template <typename T, typename F> bool all_of2(vector<T> &v, F f) { rep(i, sz(v)) { if (!all_of2(v[i], f)) return false; } return true; } template <typename T, typename F> bool any_of2(T &v, F f) { return f(v); } template <typename T, typename F> bool any_of2(vector<T> &v, F f) { rep(i, sz(v)) { if (any_of2(v[i], f)) return true; } return false; } template <typename T, typename F> bool none_of2(T &v, F f) { return f(v); } template <typename T, typename F> bool none_of2(vector<T> &v, F f) { rep(i, sz(v)) { if (none_of2(v[i], f)) return false; } return true; } template <typename T, typename F> bool find_if2(T &v, F f) { return f(v); } template <typename T, typename F> ll find_if2(vector<T> &v, F f) { rep(i, sz(v)) { if (find_if2(v[i], f)) return i; } return sz(v); } template <typename T, typename F> bool rfind_if2(T &v, F f) { return f(v); } template <typename T, typename F> ll rfind_if2(vector<T> &v, F f) { rer(i, sz(v) - 1) { if (rfind_if2(v[i], f)) return i; } return -1; } template <class T> bool contains(string &s, const T &v) { return s.find(v) != string::npos; } template <typename T> bool contains(vector<T> &v, const T &val) { return std::find(v.begin(), v.end(), val) != v.end(); } template <typename T, typename F> bool contains_if2(vector<T> &v, F f) { return find_if(v.begin(), v.end(), f) != v.end(); } template <typename T, typename F> ll count_if2(T &v, F f) { return f(v); } template <typename T, typename F> ll count_if2(vector<T> &vec, F f) { ll ret = 0; fora(v, vec) ret += count_if2(v, f); return ret; } template <typename T, typename F> void for_each2(T &v, F f) { f(v); } template <typename T, typename F> void for_each2(vector<T> &vec, F f) { fora(v, vec) for_each2(v, f); } template <typename W> ll count_od(vector<W> &a) { return count_if2(a, [](ll v) { return v & 1; }); } template <typename W> ll count_ev(vector<W> &a) { return count_if2(a, [](ll v) { return !(v & 1); }); } #define all_of(a, right) all_of2(a, lam(right)) #define any_of(a, right) any_of2(a, lam(right)) #define none_of(a, right) none_of2(a, lam(right)) #define find_if(a, right) find_if2(a, lam(right)) #define rfind_if(a, right) rfind_if2(a, lam(right)) #define contains_if(a, right) contains_if2(a, lam(right)) #define count_if(a, right) count_if2(a, lam(right)) #define for_each(a, right) \ do { \ fora(v, a) { v right; } \ } while (0) template <class T, class U> void replace(vector<T> &a, T key, U v) { replace(a.begin(), a.end(), key, v); } void replace(str &a, char key, str v) { if (v == "") a.erase(remove(all(a), key), a.end()); } void replace(str &a, char key, char v) { replace(all(a), key, v); } // keyと同じかどうか01で置き換える template <class T, class U> void replace(vector<T> &a, U k) { rep(i, sz(a)) a[i] = a[i] == k; } template <class T, class U> void replace(vector<vector<T>> &a, U k) { rep(i, sz(a)) rep(j, sz(a[0])) a[i][j] = a[i][j] == k; } template <class T> void replace(T &a) { replace(a, '#'); } void replace(str &a, str key, str v) { stringstream t; ll kn = sz(key); std::string::size_type Pos(a.find(key)); ll l = 0; while (Pos != std::string::npos) { t << a.substr(l, Pos - l); t << v; l = Pos + kn; Pos = a.find(key, Pos + kn); } t << a.substr(l, sz(a) - l); a = t.str(); } template <class T> bool includes(vector<T> &a, vector<T> &b) { vi c = a; vi d = b; sort(all(c)); sort(all(d)); return includes(all(c), all(d)); } template <class T> bool is_permutation(vector<T> &a, vector<T> &b) { return is_permutation(all(a), all(b)); } template <class T> bool next_permutation(vector<T> &a) { return next_permutation(all(a)); } void iota(vector<ll> &ve, ll s, ll n) { ve.resize(n); iota(all(ve), s); } vi iota(ll s, ll len) { vi ve(len); iota(all(ve), s); return ve; } template <class A, class B> auto vtop(vector<A> &a, vector<B> &b) { assert(sz(a) == sz(b)); /*stringを0で初期化できない */ vector<pair<A, B>> res; rep(i, sz(a)) res.eb(a[i], b[i]); return res; } template <class A, class B> void ptov(vector<pair<A, B>> &p, vector<A> &a, vector<B> &b) { a.resize(sz(p)), b.resize(sz(p)); rep(i, sz(p)) a[i] = p[i].fi, b[i] = p[i].se; } template <class A, class B, class C> auto vtot(vector<A> &a, vector<B> &b, vector<C> &c) { assert(sz(a) == sz(b) && sz(b) == sz(c)); vector<T2<A, B, C>> res; rep(i, sz(a)) res.eb(a[i], b[i], c[i]); return res; } template <class A, class B, class C, class D> auto vtof(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { assert(sz(a) == sz(b) && sz(b) == sz(c) && sz(c) == sz(d)); vector<F2<A, B, C, D>> res; rep(i, sz(a)) res.eb(a[i], b[i], c[i], d[i]); return res; } enum pcomparator { fisi, fisd, fdsi, fdsd, sifi, sifd, sdfi, sdfd }; enum tcomparator { fisiti, fisitd, fisdti, fisdtd, fdsiti, fdsitd, fdsdti, fdsdtd, fitisi, fitisd, fitdsi, fitdsd, fdtisi, fdtisd, fdtdsi, fdtdsd, sifiti, sifitd, sifdti, sifdtd, sdfiti, sdfitd, sdfdti, sdfdtd, sitifi, sitifd, sitdfi, sitdfd, sdtifi, sdtifd, sdtdfi, sdfdfd, tifisi, tifisd, tifdsi, tifdsd, tdfisi, tdfisd, tdfdsi, tdfdsd, tisifi, tisifd, tisdfi, tisdfd, tdsifi, tdsifd, tdsdfi, tdsdfd }; template <class A, class B> void sort(vector<pair<A, B>> &a, pcomparator type) { typedef pair<A, B> U; if (type == fisi) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se < r.se; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se > r.se; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se < r.se; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se > r.se; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi < r.fi; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi > r.fi; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi < r.fi; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi > r.fi; }); }; template <class U> void sort(vector<U> &a, pcomparator type) { if (type == fisi) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f > r.f; }); }; template <class A, class B, class C, class D> void sort(vector<F2<A, B, C, D>> &a, pcomparator type) { typedef F2<A, B, C, D> U; if (type == fisi) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a > r.a; }); }; template <class U> void sort(vector<U> &a, tcomparator type) { if (type == 0) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t < r.t; }); else if (type == 1) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t > r.t; }); else if (type == 2) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t < r.t; }); else if (type == 3) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t > r.t; }); else if (type == 4) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t < r.t; }); else if (type == 5) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t > r.t; }); else if (type == 6) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t < r.t; }); else if (type == 7) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t > r.t; }); else if (type == 8) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s < r.s; }); else if (type == 9) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s > r.s; }); else if (type == 10) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s < r.s; }); else if (type == 11) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s > r.s; }); else if (type == 12) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s < r.s; }); else if (type == 13) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s > r.s; }); else if (type == 14) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s < r.s; }); else if (type == 15) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s > r.s; }); else if (type == 16) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t < r.t; }); else if (type == 17) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t > r.t; }); else if (type == 18) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t < r.t; }); else if (type == 19) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t > r.t; }); else if (type == 20) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t < r.t; }); else if (type == 21) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t > r.t; }); else if (type == 22) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t < r.t; }); else if (type == 23) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t > r.t; }); else if (type == 24) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f < r.f; }); else if (type == 25) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f > r.f; }); else if (type == 26) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f < r.f; }); else if (type == 27) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f > r.f; }); else if (type == 28) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f < r.f; }); else if (type == 29) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f > r.f; }); else if (type == 30) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f < r.f; }); else if (type == 31) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f > r.f; }); else if (type == 32) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == 33) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == 34) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == 35) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == 36) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == 37) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == 38) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == 39) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == 40) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == 41) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == 42) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == 43) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f > r.f; }); else if (type == 44) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == 45) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == 46) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == 47) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f > r.f; }); } template <class A, class B, class C, class D> void sort(vector<F2<A, B, C, D>> &a, tcomparator type) { typedef F2<A, B, C, D> U; if (type == 0) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c < r.c; }); else if (type == 1) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c > r.c; }); else if (type == 2) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c < r.c; }); else if (type == 3) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c > r.c; }); else if (type == 4) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c < r.c; }); else if (type == 5) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c > r.c; }); else if (type == 6) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c < r.c; }); else if (type == 7) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c > r.c; }); else if (type == 8) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b < r.b; }); else if (type == 9) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b > r.b; }); else if (type == 10) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b < r.b; }); else if (type == 11) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b > r.b; }); else if (type == 12) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b < r.b; }); else if (type == 13) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b > r.b; }); else if (type == 14) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b < r.b; }); else if (type == 15) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b > r.b; }); else if (type == 16) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c < r.c; }); else if (type == 17) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c > r.c; }); else if (type == 18) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c < r.c; }); else if (type == 19) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c > r.c; }); else if (type == 20) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c < r.c; }); else if (type == 21) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c > r.c; }); else if (type == 22) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c < r.c; }); else if (type == 23) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c > r.c; }); else if (type == 24) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a < r.a; }); else if (type == 25) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a > r.a; }); else if (type == 26) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a < r.a; }); else if (type == 27) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a > r.a; }); else if (type == 28) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a < r.a; }); else if (type == 29) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a > r.a; }); else if (type == 30) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a < r.a; }); else if (type == 31) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a > r.a; }); else if (type == 32) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == 33) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == 34) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == 35) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == 36) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == 37) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == 38) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == 39) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == 40) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == 41) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == 42) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == 43) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a > r.a; }); else if (type == 44) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == 45) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == 46) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == 47) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a > r.a; }); } void sort(string &a) { sort(all(a)); } template <class T> void sort(vector<T> &a) { sort(all(a)); } // P l, P rで f(P) の形で渡す template <class U, class F> void sort(vector<U> &a, F f) { sort(all(a), [&](U l, U r) { return f(l) < f(r); }); }; template <class T> void rsort(vector<T> &a) { sort(all(a), greater<T>()); }; template <class U, class F> void rsort(vector<U> &a, F f) { sort(all(a), [&](U l, U r) { return f(l) > f(r); }); }; // F = T<T> // 例えばreturn p.fi + p.se; template <class A, class B> void sortp(vector<A> &a, vector<B> &b) { auto c = vtop(a, b); sort(c); rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se; } template <class A, class B, class F> void sortp(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); sort(c, f); rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se; } template <class A, class B> void rsortp(vector<A> &a, vector<B> &b) { auto c = vtop(a, b); rsort(c); rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second; } template <class A, class B, class F> void rsortp(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); rsort(c, f); rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second; } template <class A, class B, class C> void sortt(vector<A> &a, vector<B> &b, vector<C> &c) { auto d = vtot(a, b, c); sort(d); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class F> void sortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); sort(d, f); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C> void rsortt(vector<A> &a, vector<B> &b, vector<C> &c) { auto d = vtot(a, b, c); rsort(d); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class F> void rsortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); rsort(d, f); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class D> void sortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { auto e = vtof(a, b, c, d); sort(e); rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d; } template <class A, class B, class C, class D> void rsortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { auto e = vtof(a, b, c, d); rsort(e); rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d; } // sortindex 元のvectorはソートしない template <class T> vi sorti(vector<T> &a) { auto b = a; vi ind = iota(0, sz(a)); sortp(b, ind); return ind; } /*indexの分で型が変わるためpcomparatorが必要*/ template <class T> vi sorti(vector<T> &a, pcomparator f) { auto b = a; vi ind = iota(0, sz(a)); sortp(b, ind, f); return ind; } template <class T, class F> vi sorti(vector<T> &a, F f) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(a[x]) < f(a[y]); }); return ind; } template <class T> vi rsorti(vector<T> &a) { auto b = a; vi ind = iota(0, sz(a)); rsortp(b, ind); return ind; } template <class T, class F> vi rsorti(vector<T> &a, F f) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(a[x]) > f(a[y]); }); return ind; } template <class A, class B, class F> vi sortpi(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(c[x]) < f(c[y]); }); return ind; } template <class A, class B> vi sortpi(vector<A> &a, vector<B> &b, pcomparator f) { vi ind = iota(0, sz(a)); auto c = a; auto d = b; sortt(c, d, ind, f); return ind; } template <class A, class B> vi sortpi(vector<A> &a, vector<B> &b) { return sortpi(a, b, fisi); }; template <class A, class B, class F> vi rsortpi(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(c[x]) > f(c[y]); }); return ind; } template <class A, class B> vi rsortpi(vector<A> &a, vector<B> &b) { return sortpi(a, b, fdsd); }; template <class A, class B, class C, class F> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(d[x]) < f(d[y]); }); return ind; } template <class A, class B, class C> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, pcomparator f) { vi ind = iota(0, sz(a)); auto d = vtof(a, b, c, ind); sort(d, f); rep(i, sz(a)) ind[i] = d[i].d; return ind; } template <class A, class B, class C> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { if (a[x] == a[y]) { if (b[x] == b[y]) return c[x] < c[y]; else return b[x] < b[y]; } else { return a[x] < a[y]; } }); return ind; } template <class A, class B, class C, class F> vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(d[x]) > f(d[y]); }); return ind; } template <class A, class B, class C> vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { if (a[x] == a[y]) { if (b[x] == b[y]) return c[x] > c[y]; else return b[x] > b[y]; } else { return a[x] > a[y]; } }); return ind; } template <class T> void sort2(vector<vector<T>> &a) { for (ll i = 0, n = a.size(); i < n; ++i) sort(a[i]); } template <class T> void rsort2(vector<vector<T>> &a) { for (ll i = 0, n = a.size(); i < n; ++i) rsort(a[i]); } template <typename A, size_t N, typename T> void fill(A (&a)[N], const T &v) { rep(i, N) a[i] = v; } template <typename A, size_t N, size_t O, typename T> void fill(A (&a)[N][O], const T &v) { rep(i, N) rep(j, O) a[i][j] = v; } template <typename A, size_t N, size_t O, size_t P, typename T> void fill(A (&a)[N][O][P], const T &v) { rep(i, N) rep(j, O) rep(k, P) a[i][j][k] = v; } template <typename A, size_t N, size_t O, size_t P, size_t Q, typename T> void fill(A (&a)[N][O][P][Q], const T &v) { rep(i, N) rep(j, O) rep(k, P) rep(l, Q) a[i][j][k][l] = v; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, typename T> void fill(A (&a)[N][O][P][Q][R], const T &v) { rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) a[i][j][k][l][m] = v; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S, typename T> void fill(A (&a)[N][O][P][Q][R][S], const T &v) { rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) rep(n, S) a[i][j][k][l][m][n] = v; } template <typename W, typename T> void fill(W &xx, const T vall) { xx = vall; } template <typename W, typename T> void fill(vector<W> &vecc, const T vall) { for (auto &&vx : vecc) fill(vx, vall); } template <typename W, typename T> void fill(vector<W> &xx, const T v, ll len) { rep(i, len) xx[i] = v; } template <typename W, typename T> void fill(vector<vector<W>> &xx, const T v, ll lh, ll lw) { rep(i, lh) rep(j, lw) xx[i][j] = v; } template <class T, class U> void fill(vector<T> &a, U val, vi &ind) { fora(v, ind) a[v] = val; } template <typename A, size_t N> A sum(A (&a)[N]) { A res = 0; rep(i, N) res += a[i]; return res; } template <typename A, size_t N, size_t O> A sum(A (&a)[N][O]) { A res = 0; rep(i, N) rep(j, O) res += a[i][j]; return res; } template <typename A, size_t N, size_t O, size_t P> A sum(A (&a)[N][O][P]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) res += a[i][j][k]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A sum(A (&a)[N][O][P][Q]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) res += a[i][j][k][l]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A sum(A (&a)[N][O][P][Q][R]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) res += a[i][j][k][l][m]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A sum(A (&a)[N][O][P][Q][R][S]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) rep(n, S) res += a[i][j][k][l][m][n]; return res; } //@汎用便利関数 入力 ll in() { ll ret; cin >> ret; return ret; } string sin() { string ret; cin >> ret; return ret; } template <class T> void in(T &head) { cin >> head; } template <class T, class... U> void in(T &head, U &...tail) { cin >> head; in(tail...); } #define o_din(o1, o2, o3, o4, o5, o6, name, ...) name #define din1(a) \ ll a; \ cin >> a #define din2(a, b) \ ll a, b; \ cin >> a >> b #define din3(a, b, c) \ ll a, b, c; \ cin >> a >> b >> c #define din4(a, b, c, d) \ ll a, b, c, d; \ cin >> a >> b >> c >> d #define din5(a, b, c, d, e) \ ll a, b, c, d, e; \ cin >> a >> b >> c >> d >> e #define din6(a, b, c, d, e, f) \ ll a, b, c, d, e, f; \ cin >> a >> b >> c >> d >> e >> f #define din(...) \ o_din(__VA_ARGS__, din6, din5, din4, din3, din2, din1)(__VA_ARGS__) #define o_dind(o1, o2, o3, o4, name, ...) name #define din1d(a) \ din1(a); \ a-- #define din2d(a, b) \ din2(a, b); \ a--, b-- #define din3d(a, b, c) \ din3(a, b, c); \ a--, b--, c-- #define din4d(a, b, c, d) \ din4(a, b, c, d); \ a--, b--, c--, d-- #define dind(...) o_dind(__VA_ARGS__, din4d, din3d, din2d, din1d)(__VA_ARGS__) template <class T> void out2(T &&head) { cout << head; } template <class T, class... U> void out2(T &&head, U &&...tail) { cout << head << " "; out2(tail...); } template <class T, class... U> void out(T &&head, U &&...tail) { cout << head << " "; out2(tail...); cout << "" << endl; } template <class T> void out(T &&head) { cout << head << endl; } #ifdef _DEBUG template <class T> void outv(vector<T> &a, ll W) { rep(i, W) { cerr << a[i] << " "; } cerr << "" << endl; } template <class T> void outv(vector<vector<T>> &a, ll H = linf, ll W = linf) { rep(h, min(H, sz(a))) { rep(w, min(W, sz(a[0]))) { if (a[h][w] == linf) cerr << "e" << " "; else cerr << a[h][w] << " "; } cerr << "" << endl; } } #else template <class T> void outv(vector<T> &a, ll W) { ; } template <class T> void outv(vector<vector<T>> &a, ll H = linf, ll W = linf) { ; } #endif template <class T> void outl(vector<T> &a) { fora(v, a) cout << v << endl; } template <class T> void na(vector<T> &a, ll n) { a.resize(n); rep(i, n) cin >> a[i]; } #define dna(a, n) \ vi a(n); \ rep(dnai, n) cin >> a[dnai]; template <class T> void nao(vector<T> &a, ll n) { a.resize(n + 1); a[0] = 0; rep(i, n) cin >> a[i + 1]; } template <class T> void naod(vector<T> &a, ll n) { a.resize(n + 1); a[0] = 0; rep(i, n) cin >> a[i + 1], a[i + 1]--; } template <class T> void nad(vector<T> &a, ll n) { a.resize(n); rep(i, n) cin >> a[i], a[i]--; } template <class T, class U> void na2(vector<T> &a, vector<U> &b, ll n) { a.resize(n); b.resize(n); rep(i, n) cin >> a[i] >> b[i]; } #define dna2(a, b, n) \ vi a(n), b(n); \ rep(dna2i, n) cin >> a[dna2i] >> b[dna2i]; template <class T, class U> void nao2(vector<T> &a, vector<U> &b, ll n) { a.resize(n + 1); b.resize(n + 1); a[0] = b[0] = 0; rep(i, n) cin >> a[i + 1] >> b[i + 1]; } #define dna2d(a, b, n) \ vi a(n), b(n); \ rep(dna2di, n) { \ cin >> a[dna2di] >> b[dna2di]; \ a[dna2di]--, b[dna2di]--; \ } template <class T, class U> void na2d(vector<T> &a, vector<U> &b, ll n) { a.resize(n); b.resize(n); rep(i, n) cin >> a[i] >> b[i], a[i]--, b[i]--; } template <class T, class U, class W> void na3(vector<T> &a, vector<U> &b, vector<W> &c, ll n) { a.resize(n); b.resize(n); c.resize(n); rep(i, n) cin >> a[i] >> b[i] >> c[i]; } #define dna3(a, b, c, n) \ vi a(n), b(n), c(n); \ rep(dna3i, n) cin >> a[dna3i] >> b[dna3i] >> c[dna3i]; template <class T, class U, class W> void na3d(vector<T> &a, vector<U> &b, vector<W> &c, ll n) { a.resize(n); b.resize(n); c.resize(n); rep(i, n) cin >> a[i] >> b[i] >> c[i], a[i]--, b[i]--, c[i]--; } #define dna3d(a, b, c, n) \ vi a(n), b(n), c(n); \ rep(dna3di, n) { \ cin >> a[dna3di] >> b[dna3di] >> c[dna3di]; \ a[dna3di]--, b[dna3di]--, c[dna3di]--; \ } #define nt(a, h, w) \ resize(a, h, w); \ rep(nthi, h) rep(ntwi, w) cin >> a[nthi][ntwi]; #define ntd(a, h, w) \ resize(a, h, w); \ rep(ntdhi, h) rep(ntdwi, w) cin >> a[ntdhi][ntdwi], a[ntdhi][ntdwi]--; #define ntp(a, h, w) \ resize(a, h + 2, w + 2); \ fill(a, '#'); \ rep(ntphi, 1, h + 1) rep(ntpwi, 1, w + 1) cin >> a[ntphi][ntpwi]; // デバッグ #define sp << " " << #define debugName(VariableName) #VariableName #define deb1(x) debugName(x) << " = " << x #define deb2(x, ...) deb1(x) << ", " << deb1(__VA_ARGS__) #define deb3(x, ...) deb1(x) << ", " << deb2(__VA_ARGS__) #define deb4(x, ...) deb1(x) << ", " << deb3(__VA_ARGS__) #define deb5(x, ...) deb1(x) << ", " << deb4(__VA_ARGS__) #define deb6(x, ...) deb1(x) << ", " << deb5(__VA_ARGS__) #define deb7(x, ...) deb1(x) << ", " << deb6(__VA_ARGS__) #define deb8(x, ...) deb1(x) << ", " << deb7(__VA_ARGS__) #define deb9(x, ...) deb1(x) << ", " << deb8(__VA_ARGS__) #define deb10(x, ...) deb1(x) << ", " << deb9(__VA_ARGS__) #define o_ebug(o1, o2, o3, o4, o5, o6, o7, o8, o9, o10, name, ...) name #ifdef _DEBUG #define deb(...) \ cerr << o_ebug(__VA_ARGS__, deb10, deb9, deb8, deb7, deb6, deb5, deb4, deb3, \ deb2, deb1)(__VA_ARGS__) \ << endl #else #define deb(...) ; #endif #define debugline(x) \ cerr << x << " " \ << "(L:" << __LINE__ << ")" << '\n' //@formatter:off // よく使うクラス、構造体 struct unionfind { vector<ll> par; vector<ll> siz; vector<ll> es; ll n, trees; // 連結グループの数(親の種類) unionfind(ll n) : n(n), trees(n) { par.resize(n); siz.resize(n); es.resize(n); for (ll i = 0; i < n; i++) { par[i] = i; siz[i] = 1; } } ll root(ll x) { if (par[x] == x) { return x; } else { return par[x] = root(par[x]); } } bool unite(ll x, ll y) { x = root(x); y = root(y); es[x]++; if (x == y) return false; if (siz[x] > siz[y]) swap(x, y); trees--; par[x] = y; siz[y] += siz[x]; es[y] += es[x]; return true; } bool same(ll x, ll y) { return root(x) == root(y); } ll size(ll x) { return siz[root(x)]; } ll esize(ll x) { return es[root(x)]; } vi sizes() { vi cou(n); vi ret; ret.reserve(n); rep(i, n) { cou[root(i)]++; } rep(i, n) { if (cou[i]) ret.push_back(cou[i]); } return ret; } // つながりを無向グラフと見なし、xが閉路に含まれるか判定 bool close(ll x) { return esize(x) >= size(x); } V<vi> sets() { vi ind(n, -1); ll i = 0; vvi(res, trees); rep(j, n) { ll r = root(j); if (ind[r] == -1) ind[r] = i++; res[ind[r]].push_back(j); } rep(i, trees) { ll r = root(res[i][0]); if (res[i][0] == r) continue; rep(j, 1, sz(res[i])) { if (res[i][j] == r) { swap(res[i][0], res[i][j]); break; } } } return res; } }; //@formatter:off using bll = __int128; using u32 = unsigned; using u64 = unsigned long long; using u128 = __uint128_t; std::ostream &operator<<(std::ostream &dest, __int128_t value) { std::ostream::sentry s(dest); if (s) { __uint128_t tmp = value < 0 ? -value : value; char buffer[128]; char *d = std::end(buffer); do { --d; *d = "0123456789"[tmp % 10]; tmp /= 10; } while (tmp != 0); if (value < 0) { --d; *d = '-'; } ll len = std::end(buffer) - d; if (dest.rdbuf()->sputn(d, len) != len) { dest.setstate(std::ios_base::badbit); } } return dest; } //__int128 toi128(string &s) { __int128 ret = 0; for (ll i = 0; i < //s.length(); ++i) if ('0' <= s[i] && s[i] <= '9') ret = 10 * //ret + s[i] - '0'; return ret;} // エラー void ole() { #ifdef _DEBUG debugline("ole"); exit(0); #endif string a = "a"; rep(i, 30) a += a; rep(i, 1 << 17) cout << a << endl; cout << "OLE 出力長制限超過" << endl; exit(0); } void re() { assert(0 == 1); exit(0); } void tle() { while (inf) cout << inf << endl; } // 便利関数 // テスト用 char ranc() { return (char)('a' + rand() % 26); } ll rand(ll min, ll max) { assert(min <= max); if (min >= 0 && max >= 0) { return rand() % (max + 1 - min) + min; } else if (max < 0) { return -rand(-max, -min); } else { if (rand() % 2) { return rand(0, max); } else { return -rand(0, -min); } } } vi ranv(ll n, ll min, ll max) { vi v(n); rep(i, n) v[i] = rand(min, max); return v; } str ransu(ll n) { str s; rep(i, n) s += (char)rand('A', 'Z'); return s; } str ransl(ll n) { str s; rep(i, n) s += (char)rand('a', 'z'); return s; } // 単調増加 vi ranvinc(ll n, ll min, ll max) { vi v(n); bool bad = 1; while (bad) { bad = 0; v.resize(n); rep(i, n) { if (i && min > max - v[i - 1]) { bad = 1; break; } if (i) v[i] = v[i - 1] + rand(min, max - v[i - 1]); else v[i] = rand(min, max); } } return v; } // 便利 汎用 void ranvlr(ll n, ll min, ll max, vi &l, vi &r) { l.resize(n); r.resize(n); rep(i, n) { l[i] = rand(min, max); r[i] = l[i] + rand(0, max - l[i]); } } vp run_length(vi &a) { vp ret; ret.eb(a[0], 1); rep(i, 1, sz(a)) { if (ret.back().fi == a[i]) { ret.back().se++; } else { ret.eb(a[i], 1); } } return ret; } vector<pair<char, ll>> run_length(string &a) { vector<pair<char, ll>> ret; ret.eb(a[0], 1); rep(i, 1, sz(a)) { if (ret.back().fi == a[i]) { ret.back().se++; } else { ret.eb(a[i], 1); } } return ret; } template <class F> ll mgr(ll ok, ll ng, F f) { if (ok < ng) while (ng - ok > 1) { ll mid = (ok + ng) / 2; if (f(mid)) ok = mid; else ng = mid; } else while (ok - ng > 1) { ll mid = (ok + ng) / 2; if (f(mid)) ok = mid; else ng = mid; } return ok; } // strを整数として比較 string smax(str &a, str b) { if (sz(a) < sz(b)) { return b; } else if (sz(a) > sz(b)) { return a; } else if (a < b) return b; else return a; } // strを整数として比較 string smin(str &a, str b) { if (sz(a) > sz(b)) { return b; } else if (sz(a) < sz(b)) { return a; } else if (a > b) return b; else return a; } template <typename W, typename T> ll find(vector<W> &a, const T key) { rep(i, sz(a)) if (a[i] == key) return i; return -1; } template <typename W, typename T> P find(vector<vector<W>> &a, const T key) { rep(i, sz(a)) rep(j, sz(a[0])) if (a[i][j] == key) return mp(i, j); return mp(-1, -1); } template <typename W, typename U> T find(vector<vector<vector<W>>> &a, const U key) { rep(i, sz(a)) rep(j, sz(a[0])) rep(k, sz(a[0][0])) if (a[i][j][k] == key) return mt(i, j, k); return mt(-1, -1, -1); } template <typename W, typename T> ll count2(W &a, const T k) { return a == k; } template <typename W, typename T> ll count2(vector<W> &a, const T k) { ll ret = 0; fora(v, a) ret += count2(v, k); return ret; } template <typename W, typename T> ll count(vector<W> &a, const T k) { ll ret = 0; fora(v, a) ret += count2(v, k); return ret; } ll count(str &a, str k) { ll ret = 0, len = k.length(); auto pos = a.find(k); while (pos != string::npos) pos = a.find(k, pos + len), ++ret; return ret; } vi count(str &a) { vi cou(26); char c = 'a'; if ('A' <= a[0] && a[0] <= 'Z') c = 'A'; rep(i, sz(a))++ cou[a[i] - c]; return cou; } #define couif count_if // algorythm ll rev(ll a) { ll res = 0; while (a) { res *= 10; res += a % 10; a /= 10; } return res; } template <class T> void rev(vector<T> &a) { reverse(all(a)); } template <class U> void rev(vector<vector<U>> &a) { vector<vector<U>> b(sz(a[0]), vector<U>(sz(a))); rep(h, sz(a)) rep(w, sz(a[0])) b[w][h] = a[h][w]; a = b; } void rev(string &a) { reverse(all(a)); } constexpr ll p10[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000ll, 100000000000ll, 1000000000000ll, 10000000000000ll, 100000000000000ll, 1000000000000000ll, 10000000000000000ll, 100000000000000000ll, 1000000000000000000ll}; ll get(ll a, ll keta) { return (a / (ll)pow(10, keta)) % 10; } ll keta(ll v) { if (v < p10[9]) { if (v < p10[4]) { if (v < p10[2]) { if (v < p10[1]) return 1; else return 2; } else { if (v < p10[3]) return 3; else return 4; } } else { if (v < p10[7]) { if (v < p10[5]) return 5; else if (v < p10[6]) return 6; else return 7; } else { if (v < p10[8]) return 8; else return 9; } } } else { if (v < p10[13]) { if (v < p10[11]) { if (v < p10[10]) return 10; else return 11; } else { if (v < p10[12]) return 12; else return 13; } } else { if (v < p10[15]) { if (v < p10[14]) return 14; else if (v < p10[15]) return 15; else return 16; } else { if (v < p10[17]) return 17; else return 18; } } } } ll dsum(ll v, ll sin = 10) { ll ret = 0; for (; v; v /= sin) ret += v % sin; return ret; } struct sint { ll v; sint(ll v) : v(v) {} operator ll() { return v; } // 下からi番目 ll operator[](ll i) { return (v / p10[i]) % 10; } ll back(ll i) { return operator[](i); } // 上からi番目 ll top(ll i) { ll len = keta(v); return operator[](len - 1 - i); } // 先頭からi番目にセット ll settop(ll i, ll k) { ll len = keta(v); return set(len - 1 - i, k); } ll set(ll i, ll k) { if (i < 0) return settop(abs(i) - 1, k); return v += p10[i] * (k - (v / p10[i]) % 10); } ll add(ll i, ll k = 1) { return v += p10[i] * k; } ll addtop(ll i, ll k = 1) { return v += p10[keta(v) - i - 1] * k; } ll dec(ll i, ll k = 1) { return v -= p10[i] * k; } ll dectop(ll i, ll k = 1) { return v -= p10[keta(v) - i - 1] * k; } #define op(t, o) \ template <class T> t operator o(T r) { return v o r; } op(ll, +=); op(ll, -=); op(ll, *=); op(ll, /=); op(ll, %=); op(ll, +); op(ll, -); op(ll, *); op(ll, /); op(ll, %); op(bool, ==); op(bool, !=); op(bool, <); op(bool, <=); op(bool, >); op(bool, >=); #undef op template <class T> ll operator<<=(T r) { return v *= p10[r]; } template <class T> ll operator<<(T r) { return v * p10[r]; } template <class T> ll operator>>=(T r) { return v /= p10[r]; } template <class T> ll operator>>(T r) { return v / p10[r]; } }; ll mask10(ll v) { return p10[v] - 1; } // 変換系 template <class T> auto keys(T a) { vector<decltype((a.begin())->fi)> res; for (auto &&k : a) res.push_back(k.fi); return res; } template <class T> auto values(T a) { vector<decltype((a.begin())->se)> res; for (auto &&k : a) res.push_back(k.se); return res; } template <class T, class U> bool chma(T &a, const U &b) { if (a < b) { a = b; return true; } return false; } template <class U> bool chma(const U &b) { return chma(ma, b); } template <class T, class U> bool chmi(T &a, const U &b) { if (b < a) { a = b; return true; } return false; } template <class U> bool chmi(const U &b) { return chmi(mi, b); } template <class T> T min(T a, signed b) { return a < b ? a : b; } template <class T> T max(T a, signed b) { return a < b ? b : a; } template <class T> T min(T a, T b, T c) { return a >= b ? b >= c ? c : b : a >= c ? c : a; } template <class T> T max(T a, T b, T c) { return a <= b ? b <= c ? c : b : a <= c ? c : a; } template <class T> T min(vector<T> &a) { return *min_element(all(a)); } template <class T> T mini(vector<T> &a) { return min_element(all(a)) - a.begin(); } template <class T> T min(vector<T> &a, ll n) { return *min_element(a.begin(), a.begin() + min(n, sz(a))); } template <class T> T min(vector<T> &a, ll s, ll n) { return *min_element(a.begin() + s, a.begin() + min(n, sz(a))); } template <class T> T max(vector<T> &a) { return *max_element(all(a)); } template <class T, class U> T max(vector<T> &a, vector<U> &b) { return max(*max_element(all(a)), *max_element(all(b))); } template <class T> T maxi(vector<T> &a) { return max_element(all(a)) - a.begin(); } template <class T> T max(vector<T> &a, ll n) { return *max_element(a.begin(), a.begin() + min(n, sz(a))); } template <class T> T max(vector<T> &a, ll s, ll n) { return *max_element(a.begin() + s, a.begin() + min(n, sz(a))); } template <typename A, size_t N> A max(A (&a)[N]) { A res = a[0]; rep(i, N) res = max(res, a[i]); return res; } template <typename A, size_t N, size_t O> A max(A (&a)[N][O]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P> A max(A (&a)[N][O][P]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A max(A (&a)[N][O][P][Q], const T &v) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A max(A (&a)[N][O][P][Q][R]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A max(A (&a)[N][O][P][Q][R][S]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N> A min(A (&a)[N]) { A res = a[0]; rep(i, N) res = min(res, a[i]); return res; } template <typename A, size_t N, size_t O> A min(A (&a)[N][O]) { A res = min(a[0]); rep(i, N) res = min(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P> A min(A (&a)[N][O][P]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A min(A (&a)[N][O][P][Q], const T &v) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A min(A (&a)[N][O][P][Q][R]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A min(A (&a)[N][O][P][Q][R][S]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <class T> T sum(vector<T> &v, ll s = 0, ll t = inf) { T ret = 0; rep(i, s, min(sz(v), t)) ret += v[i]; return ret; } template <class T> T sum(vector<vector<T>> &v) { T ret = 0; rep(i, sz(v)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<T>>> &v) { T ret = 0; rep(i, sz(v)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<vector<T>>>> &v) { T ret = 0; rep(i, sz(v)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<vector<vector<T>>>>> &v) { T ret = 0; rep(i, sz(v)) ret += sum(v[i]); return ret; } template <class T> auto sum(priority_queue<T, vector<T>, greater<T>> &r) { auto q = r; T ret = 0; while (sz(q)) { ret += q.top(); q.pop(); } return ret; } template <class T> auto sum(priority_queue<T> &r) { auto q = r; T ret = 0; while (sz(q)) { ret += q.top(); q.pop(); } return ret; } // template<class T, class U, class... W> auto sumn(vector<T> &v, U head, W... // tail) { auto ret = sum(v[0], tail...); rep(i, 1, min(sz(v), head))ret // += sum(v[i], tail...); return ret;} void clear(PQ &q) { q = PQ(); } template <class T> void clear(queue<T> &q) { while (q.size()) q.pop(); } template <class T> T *negarr(ll size) { T *body = (T *)malloc((size * 2 + 1) * sizeof(T)); return body + size; } template <class T> T *negarr2(ll h, ll w) { double **dummy1 = new double *[2 * h + 1]; double *dummy2 = new double[(2 * h + 1) * (2 * w + 1)]; dummy1[0] = dummy2 + w; for (ll i = 1; i <= 2 * h + 1; ++i) { dummy1[i] = dummy1[i - 1] + 2 * w + 1; } double **a = dummy1 + h; return a; } // imoは0-indexed // ruiは1-indexed template <class T> vector<T> imo(vector<T> &v) { vector<T> ret = v; rep(i, sz(ret) - 1) ret[i + 1] += ret[i]; return ret; } // kと同じものの数 template <class T, class U> vi imo(vector<T> &a, U k) { vector<T> ret = a; rep(i, sz(ret)) ret[i] = a[i] == k; rep(i, sz(ret) - 1) ret[i + 1] += ret[i]; return ret; } template <class T> vector<T> imox(vector<T> &v) { vector<T> ret = v; rep(i, sz(ret) - 1) ret[i + 1] ^= ret[i]; return ret; } // 漸化的に最小を持つ template <class T> vector<T> imi(vector<T> &v) { vector<T> ret = v; rep(i, sz(ret) - 1) chmi(ret[i + 1], ret[i]); return ret; } template <class T> struct ruiC { const vector<T> rui; ruiC(vector<T> &ru) : rui(ru) {} T operator()(ll l, ll r) { if (l > r) { cerr << "ruic "; deb(l, r); assert(0); } return rui[r] - rui[l]; } T operator[](ll i) { return rui[i]; } T back() { return rui.back(); } ll size() { return rui.size(); } }; template <class T> struct rruic { const T *rrui; rruic(T *ru) : rrui(ru) {} T operator()(ll l, ll r) { assert(l >= r); return rrui[r] - rrui[l]; } T operator[](ll i) { return rrui[i]; } }; template <class T> vector<T> ruiv(vector<T> &a) { vector<T> ret(a.size() + 1); rep(i, a.size()) ret[i + 1] = ret[i] + a[i]; return ret; } template <class T> ruiC<T> ruic(vector<T> &a) { vector<T> ret = ruiv(a); return ruiC<T>(ret); } vector<ll> ruiv(string &a) { if (sz(a) == 0) return vi(1); ll dec = ('0' <= a[0] && a[0] <= '9') ? '0' : 0; vector<ll> ret(a.size() + 1); rep(i, a.size()) ret[i + 1] = ret[i] + a[i] - dec; return ret; } ruiC<ll> ruic(string &a) { vector<ll> ret = ruiv(a); return ruiC<ll>(ret); } // kと同じものの数 template <class T, class U> vi ruiv(T &a, U k) { vi ret(a.size() + 1); rep(i, a.size()) ret[i + 1] = ret[i] + (a[i] == k); return ret; } template <class T, class U> ruiC<ll> ruic(T &a, U k) { vi ret = ruiv(a, k); return ruiC<ll>(ret); } // xor template <class T> vector<T> ruix(vector<T> &a) { vector<T> ret(a.size() + 1); rep(i, a.size()) ret[i + 1] = ret[i] ^ a[i]; return ret; } template <class T> vector<T> ruim(vector<T> &a) { vector<T> res(a.size() + 1, 1); rep(i, a.size()) res[i + 1] = res[i] * a[i]; return res; } // 漸化的に最小を1indexで持つ template <class T> vector<T> ruimi(vector<T> &a) { ll n = sz(a); vector<T> ret(n + 1); rep(i, 1, n) { ret[i] = a[i - 1]; chmi(ret[i + 1], ret[i]); } return ret; } // template<class T> T *rrui(vector<T> &a) { // 右から左にかけての半開区間 (-1 n-1] template <class T> rruic<T> rrui(vector<T> &a) { ll len = a.size(); T *body = (T *)malloc((len + 1) * sizeof(T)); T *res = body + 1; rer(i, len - 1) res[i - 1] = res[i] + a[i]; return rruic<T>(res); } // 掛け算 template <class T> T *rruim(vector<T> &a) { ll len = a.size(); T *body = (T *)malloc((len + 1) * sizeof(T)); T *res = body + 1; res[len - 1] = 1; rer(i, len - 1) res[i - 1] = res[i] * a[i]; return res; } template <class T, class U> void inc(T &a, U v = 1) { a += v; } template <class T, class U> void inc(vector<T> &a, U v = 1) { for (auto &u : a) inc(u, v); } template <class T, class U> void dec(T &a, U v = 1) { a -= v; } template <class T, class U> void dec(vector<T> &a, U v = 1) { for (auto &u : a) dec(u, v); } template <class U> void dec(string &a, U v = 1) { for (auto &u : a) dec(u, v); } template <class T> void dec(vector<T> &a) { for (auto &u : a) dec(u, 1); } bool ins(ll h, ll w, ll H, ll W) { return h >= 0 && w >= 0 && h < H && w < W; } bool ins(ll l, ll v, ll r) { return l <= v && v < r; } template <class T> bool ins(vector<T> &a, ll i, ll j = 0) { return ins(0, i, sz(a)) && ins(0, j, sz(a)); } ll u(ll a) { return a < 0 ? 0 : a; } template <class T> vector<T> u(const vector<T> &a) { vector<T> ret = a; fora(v, ret) v = u(v); return ret; } #define MIN(a) numeric_limits<a>::min() #define MAX(a) numeric_limits<a>::max() template <class F> ll goldd_l(ll left, ll right, F calc) { double GRATIO = 1.6180339887498948482045868343656; ll lm = left + (ll)((right - left) / (GRATIO + 1.0)); ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); ll fl = calc(lm); ll fr = calc(rm); while (right - left > 10) { if (fl < fr) { right = rm; rm = lm; fr = fl; lm = left + (ll)((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } ll minScore = MAX(ll); ll resIndex = left; for (ll i = left; i < right + 1; ++i) { ll score = calc(i); if (minScore > score) { minScore = score; resIndex = i; } } return resIndex; } template <class F> ll goldt_l(ll left, ll right, F calc) { double GRATIO = 1.6180339887498948482045868343656; ll lm = left + (ll)((right - left) / (GRATIO + 1.0)); ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); ll fl = calc(lm); ll fr = calc(rm); while (right - left > 10) { if (fl > fr) { right = rm; rm = lm; fr = fl; lm = left + (ll)((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } if (left > right) { ll l = left; left = right; right = l; } ll maxScore = MIN(ll); ll resIndex = left; for (ll i = left; i < right + 1; ++i) { ll score = calc(i); if (maxScore < score) { maxScore = score; resIndex = i; } } return resIndex; } /*loopは200にすればおそらく大丈夫 余裕なら300に*/ template <class F> dou goldd_d(dou left, dou right, F calc, ll loop = 140) { dou GRATIO = 1.6180339887498948482045868343656; dou lm = left + ((right - left) / (GRATIO + 1.0)); dou rm = lm + ((right - lm) / (GRATIO + 1.0)); dou fl = calc(lm); dou fr = calc(rm); /*200にすればおそらく大丈夫*/ /*余裕なら300に*/ ll k = 141; loop++; while (--loop) { if (fl < fr) { right = rm; rm = lm; fr = fl; lm = left + ((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + ((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } return left; } template <class F> dou goldt_d(dou left, dou right, F calc, ll loop = 140) { double GRATIO = 1.6180339887498948482045868343656; dou lm = left + ((right - left) / (GRATIO + 1.0)); dou rm = lm + ((right - lm) / (GRATIO + 1.0)); dou fl = calc(lm); dou fr = calc(rm); loop++; while (--loop) { if (fl > fr) { right = rm; rm = lm; fr = fl; lm = left + ((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + ((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } return left; } // l ~ rを複数の区間に分割し、極致を与えるiを返す time-20 msまで探索 template <class F> ll goldd_ls(ll l, ll r, F calc, ll time = 2000) { auto lim = milliseconds(time - 20); ll mini = 0, minv = MAX(ll); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/ ll nl = l; ll nr = l + haba; rep(i, k) { ll ni = goldd_l(nl, nr, calc); if (chmi(minv, calc(ni))) mini = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return mini; } template <class F> ll goldt_ls(ll l, ll r, F calc, ll time = 2000) { auto lim = milliseconds(time - 20); ll maxi = 0, maxv = MIN(ll); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/ ll nl = l; ll nr = l + haba; rep(i, k) { ll ni = goldt_l(nl, nr, calc); if (chma(maxv, calc(ni))) maxi = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return maxi; } template <class F> dou goldd_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を持つ*/ auto lim = milliseconds(time - 20); dou mini = 0, minv = MAX(dou); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); dou haba = (r - l) / k; dou nl = l; dou nr = l + haba; rep(i, k) { dou ni = goldd_d(nl, nr, calc); if (chmi(minv, calc(ni))) mini = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return mini; } template <class F> dou goldt_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を残している*/ auto lim = milliseconds(time - 20); dou maxi = 0, maxv = MIN(dou); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); dou haba = (r - l) / k; dou nl = l; dou nr = l + haba; rep(i, k) { dou ni = goldt_d(nl, nr, calc); if (chma(maxv, calc(ni))) maxi = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return maxi; } template <class T> T min(vector<vector<T>> &a) { T res = MAX(T); rep(i, a.size()) chmi(res, *min_element(all(a[i]))); return res; } template <class T> T max(vector<vector<T>> &a) { T res = MIN(T); rep(i, a.size()) chma(res, *max_element(all(a[i]))); return res; } constexpr bool bget(ll m, ll keta) { return (m >> keta) & 1; } ll bget(ll m, ll keta, ll sinsuu) { m /= (ll)pow(sinsuu, keta); return m % sinsuu; } ll bit(ll n) { return (1LL << (n)); } ll bit(ll n, ll sinsuu) { return (ll)pow(sinsuu, n); } ll mask(ll n) { return (1ll << n) - 1; } #define bcou __builtin_popcountll // 最下位ビット ll lbit(ll n) { return n & -n; } // 最上位ビット ll hbit(ll n) { n |= (n >> 1); n |= (n >> 2); n |= (n >> 4); n |= (n >> 8); n |= (n >> 16); n |= (n >> 32); return n - (n >> 1); } ll hbitk(ll n) { ll k = 0; rer(i, 5) { ll a = k + (1ll << i); ll b = 1ll << a; if (b <= n) k += 1ll << i; } return k; } // 初期化は0を渡す ll nextComb(ll &mask, ll n, ll r) { if (!mask) return mask = (1LL << r) - 1; ll x = mask & -mask; /*最下位の1*/ ll y = mask + x; /*連続した下の1を繰り上がらせる*/ ll res = ((mask & ~y) / x >> 1) | y; if (bget(res, n)) return mask = 0; else return mask = res; } // n桁以下でビットがr個立っているもののvectorを返す vi bitCombList(ll n, ll r) { vi res; ll m = 0; while (nextComb(m, n, r)) { res.push_back(m); } return res; } char itoal(ll i) { return 'a' + i; } char itoaL(ll i) { return 'A' + i; } ll altoi(char c) { if ('A' <= c && c <= 'Z') return c - 'A'; return c - 'a'; } ll ctoi(char c) { return c - '0'; } char itoc(ll i) { return i + '0'; } ll vtoi(vi &v) { ll res = 0; if (sz(v) > 18) { debugline("vtoi"); deb(sz(v)); ole(); } rep(i, sz(v)) { res *= 10; res += v[i]; } return res; } vi itov(ll i) { vi res; while (i) { res.push_back(i % 10); i /= 10; } rev(res); return res; } vi stov(string &a) { ll n = sz(a); vi ret(n); rep(i, n) { ret[i] = a[i] - '0'; } return ret; } // 基準を満たさないものは0になる vi stov(string &a, char one) { ll n = sz(a); vi ret(n); rep(i, n) ret[i] = a[i] == one; return ret; } vector<vector<ll>> ctoi(vector<vector<char>> s, char c) { ll n = sz(s), m = sz(s[0]); vector<vector<ll>> res(n, vector<ll>(m)); rep(i, n) rep(j, m) res[i][j] = s[i][j] == c; return res; } #define unique(v) v.erase(unique(v.begin(), v.end()), v.end()); //[i] := i番として圧縮されたものを返す vi compress(vi &a) { vi b; ll len = a.size(); for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { a[i] = lower_bound(all(b), a[i]) - b.begin(); } ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, umap<ll, ll> &map) { vi b; ll len = a.size(); for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { ll v = a[i]; a[i] = lower_bound(all(b), a[i]) - b.begin(); map[v] = a[i]; } ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, vi &r) { vi b; ll len = a.size(); fora(v, a) b.push_back(v); fora(v, r) b.push_back(v); sort(b); unique(b); for (ll i = 0; i < len; ++i) a[i] = lower_bound(all(b), a[i]) - b.begin(); for (ll i = 0; i < sz(r); ++i) r[i] = lower_bound(all(b), r[i]) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, vi &r, vi &s) { vi b; ll len = a.size(); fora(v, a) b.push_back(v); fora(v, r) b.push_back(v); fora(v, s) b.push_back(v); sort(b); unique(b); for (ll i = 0; i < len; ++i) a[i] = lower_bound(all(b), a[i]) - b.begin(); for (ll i = 0; i < sz(r); ++i) r[i] = lower_bound(all(b), r[i]) - b.begin(); for (ll i = 0; i < sz(s); ++i) r[i] = lower_bound(all(b), s[i]) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(V<vi> &a) { vi b; fora(vv, a) fora(v, vv) b.push_back(v); sort(b); unique(b); fora(vv, a) fora(v, vv) v = lower_bound(all(b), v) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vector<vector<vi>> &a) { vi b; fora(vvv, a) fora(vv, vvv) fora(v, vv) b.push_back(v); sort(b); unique(b); fora(vvv, a) fora(vv, vvv) fora(v, vv) v = lower_bound(all(b), v) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } void compress(ll a[], ll len) { vi b; for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { a[i] = lower_bound(all(b), a[i]) - b.begin(); } } // 要素が見つからなかったときに困る #define binarySearch(a, v) (binary_search(all(a), v)) #define lowerIndex(a, v) (lower_bound(all(a), v) - a.begin()) #define lowerBound(a, v) (*lower_bound(all(a), v)) #define upperIndex(a, v) (upper_bound(all(a), v) - a.begin()) #define upperBound(a, v) (*upper_bound(all(a), v)) template <class T> void fin(T s) { cout << s << endl, exit(0); } // 便利 数学 math ll mod(ll a, ll m) { return (a % m + m) % m; } ll pow(ll a) { return a * a; }; ll fact(ll v) { return v <= 1 ? 1 : v * fact(v - 1); } ll comi(ll n, ll r) { assert(n < 100); static vvi(pas, 100, 100); if (pas[0][0]) return pas[n][r]; pas[0][0] = 1; rep(i, 1, 100) { pas[i][0] = 1; rep(j, 1, i + 1) pas[i][j] = pas[i - 1][j - 1] + pas[i - 1][j]; } return pas[n][r]; } double comd(ll n, ll r) { assert(n < 2020); static vvd(comb, 2020, 2020); if (comb[0][0] == 0) { comb[0][0] = 1; rep(i, 2000) { comb[i + 1][0] = 1; rep(j, 1, i + 2) { comb[i + 1][j] = comb[i][j] + comb[i][j - 1]; } } } return comb[n][r]; } ll gcd(ll a, ll b) { while (b) a %= b, swap(a, b); return abs(a); } ll gcd(vi b) { ll res = b[0]; rep(i, 1, sz(b)) res = gcd(b[i], res); return res; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll lcm(vi a) { ll res = a[0]; rep(i, 1, sz(a)) res = lcm(a[i], res); return res; } ll ceil(ll a, ll b) { if (b == 0) { debugline("ceil"); deb(a, b); ole(); return -1; } else if (a < 0) { return 0; } else { return (a + b - 1) / b; } } ll lower_remi__bx_a(ll kei, ll rem, ll x) { if (rem >= x) return 0; return (x - rem + kei - 1) / kei; } ll lower_remv__bx_a(ll kei, ll rem, ll x) { if (rem >= x) return rem; return (x - rem + kei - 1) / kei * kei + rem; } ll upper_remi__bx_a(ll kei, ll rem, ll x) { if (rem > x) return 0; return (x - rem + kei) / kei; } ll upper_remv__bx_a(ll kei, ll rem, ll x) { if (rem > x) return rem; return (x - rem + kei) / kei * kei + rem; } // v * v >= aとなる最小のvを返す ll sqrt(ll a) { if (a < 0) { debugline("sqrt"); deb(a); ole(); } ll res = (ll)std::sqrt(a); while (res * res < a) ++res; return res; } double log(double e, double x) { return log(x) / log(e); } ll sig(ll t) { return ((1 + t) * t) >> 1; } ll sig(ll s, ll t) { return ((s + t) * (t - s + 1)) >> 1; } // 幾何 Pをcomplexとして扱う template <class T, class U> bool eq(T a, U b) { return fabs(a - b) < eps; } dou atan2(pd a) { return atan2(a.se, a.fi); } dou angle(pd f, pd t) { return atan2(t.se - f.se, t.fi - f.fi); } dou distance(pd a, pd b) { return hypot(a.fi - b.fi, a.se - b.se); } // bを中心とするabcのtheta aからcにかけて時計回り dou angle(pd a, pd b, pd c) { dou ax = a.fi - b.fi; dou ay = a.se - b.se; dou cx = c.fi - b.fi; dou cy = c.se - b.se; double ret = atan2(cy, cx) - atan2(ay, ax); if (ret < 0) ret += 2 * PI; return ret; } dou dot(pd a, pd b) { return a.fi * b.fi + a.se + b.se; } dou cro(pd a, pd b) { return a.fi * b.se - a.se + b.fi; } // 機能拡張 template <typename CharT, typename Traits, typename Alloc> basic_string<CharT, Traits, Alloc> operator+(const basic_string<CharT, Traits, Alloc> &lhs, const int rv) { basic_string<CharT, Traits, Alloc> str(lhs); str.append(to_string(rv)); return str; } template <typename CharT, typename Traits, typename Alloc> void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const int rv) { lhs += to_string(rv); } template <typename CharT, typename Traits, typename Alloc> basic_string<CharT, Traits, Alloc> operator+(const basic_string<CharT, Traits, Alloc> &lhs, const signed rv) { basic_string<CharT, Traits, Alloc> str(lhs); str.append(to_string(rv)); return str; } template <typename CharT, typename Traits, typename Alloc> void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const signed rv) { lhs += to_string(rv); } template <class T, class U> void operator+=(queue<T> &a, U v) { a.push(v); } template <class T, class U> void operator+=(deque<T> &a, U v) { a.push_back(v); } template <class T> priority_queue<T, vector<T>, greater<T>> & operator+=(priority_queue<T, vector<T>, greater<T>> &a, vector<T> &v) { fora(d, v) a.push(d); return a; } template <class T, class U> priority_queue<T, vector<T>, greater<T>> & operator+=(priority_queue<T, vector<T>, greater<T>> &a, U v) { a.push(v); return a; } template <class T, class U> priority_queue<T> &operator+=(priority_queue<T> &a, U v) { a.push(v); return a; } template <class T> set<T> &operator+=(set<T> &a, vector<T> v) { fora(d, v) a.insert(d); return a; } template <class T, class U> auto operator+=(set<T> &a, U v) { return a.insert(v); } template <class T, class U> auto operator-=(set<T> &a, U v) { return a.erase(v); } template <class T, class U> auto operator+=(mset<T> &a, U v) { return a.insert(v); } template <class T, class U> set<T, greater<T>> &operator+=(set<T, greater<T>> &a, U v) { a.insert(v); return a; } template <class T, class U> vector<T> &operator+=(vector<T> &a, U v) { a.push_back(v); return a; } template <class T, class U> vector<T> operator+(const vector<T> &a, U v) { vector<T> ret = a; ret += v; return ret; } template <class T, class U> vector<T> operator+(U v, const vector<T> &a) { vector<T> ret = a; ret.insert(ret.begin(), v); return ret; } template <class T> vector<T> operator+(vector<T> a, vector<T> b) { vector<T> ret; ret = a; fora(v, b) ret += v; return ret; } template <class T> vector<T> &operator+=(vector<T> &a, vector<T> &b) { fora(v, b) a += v; return a; } template <class T> vector<T> &operator-=(vector<T> &a, vector<T> &b) { if (sz(a) != sz(b)) { debugline("vector<T> operator-="); deb(a); deb(b); exit(0); } rep(i, sz(a)) a[i] -= b[i]; return a; } template <class T> vector<T> operator-(vector<T> &a, vector<T> &b) { if (sz(a) != sz(b)) { debugline("vector<T> operator-"); deb(a); deb(b); ole(); } vector<T> res(sz(a)); rep(i, sz(a)) res[i] = a[i] - b[i]; return res; } template <class T, class U> vector<T> operator*(vector<T> &a, U b) { vector<T> ret; fora(v, a) ret += v * b; return ret; } template <class T, class U> vector<T> operator/(vector<T> &a, U b) { vector<T> ret; fora(v, a) ret += v / b; return ret; } template <class T, class U> vector<T> operator*=(vector<T> &a, U b) { fora(v, a) v *= b; return a; } template <class T, class U> vector<T> operator/=(vector<T> &a, U b) { fora(v, a) v /= b; return a; } template <typename T> void erase(vector<T> &v, unsigned ll i) { v.erase(v.begin() + i); } template <typename T> void erase(vector<T> &v, unsigned ll s, unsigned ll e) { v.erase(v.begin() + s, v.begin() + e); } template <class T, class U> void erase(map<T, U> &m, ll okl, ll ngr) { m.erase(m.lower_bound(okl), m.lower_bound(ngr)); } template <class T> void erase(set<T> &m, ll okl, ll ngr) { m.erase(m.lower_bound(okl), m.lower_bound(ngr)); } template <typename T> void erasen(vector<T> &v, unsigned ll s, unsigned ll n) { v.erase(v.begin() + s, v.begin() + s + n); } template <typename T, typename U> void insert(vector<T> &v, unsigned ll i, U t) { v.insert(v.begin() + i, t); } template <typename T, typename U> void push_front(vector<T> &v, U t) { v.insert(v.begin(), t); } template <typename T, typename U> void insert(vector<T> &v, unsigned ll i, vector<T> list) { for (auto &&va : list) v.insert(v.begin() + i++, va); } template <typename T> void insert(set<T> &v, vector<T> list) { for (auto &&va : list) v.insert(va); } vector<string> split(const string a, const char deli) { string b = a + deli; ll l = 0, r = 0, n = b.size(); vector<string> res; rep(i, n) { if (b[i] == deli) { r = i; if (l < r) res.push_back(b.substr(l, r - l)); l = i + 1; } } return res; } vector<string> split(const string a, const string deli) { vector<string> res; ll kn = sz(deli); std::string::size_type Pos(a.find(deli)); ll l = 0; while (Pos != std::string::npos) { if (Pos - l) res.push_back(a.substr(l, Pos - l)); l = Pos + kn; Pos = a.find(deli, Pos + kn); } if (sz(a) - l) res.push_back(a.substr(l, sz(a) - l)); return res; } void yn(bool a) { if (a) cout << "yes" << endl; else cout << "no" << endl; } void Yn(bool a) { if (a) cout << "Yes" << endl; else cout << "No" << endl; } void YN(bool a) { if (a) cout << "YES" << endl; else cout << "NO" << endl; } void fyn(bool a) { if (a) cout << "yes" << endl; else cout << "no" << endl; exit(0); } void fYn(bool a) { if (a) cout << "Yes" << endl; else cout << "No" << endl; exit(0); } void fYN(bool a) { if (a) cout << "YES" << endl; else cout << "NO" << endl; exit(0); } void Possible(bool a) { if (a) cout << "Possible" << endl; else cout << "Impossible" << endl; exit(0); } void POSSIBLE(bool a) { if (a) cout << "POSSIBLE" << endl; else cout << "IMPOSSIBLE" << endl; exit(0); } //@formatter:off template <typename T> T minv(T a, T m); template <typename T> T minv(T a); template <typename T> class Modular { public: using Type = typename decay<decltype(T::value)>::type; constexpr Modular() : value() {} template <typename U> Modular(const U &x) { value = normalize(x); } template <typename U> static Type normalize(const U &x) { Type v; if (-mod() <= x && x < mod()) v = static_cast<Type>(x); else v = static_cast<Type>(x % mod()); if (v < 0) v += mod(); return v; } const Type &operator()() const { return value; } template <typename U> explicit operator U() const { return static_cast<U>(value); } constexpr static Type mod() { return T::value; } Modular &operator+=(const Modular &other) { if ((value += other.value) >= mod()) value -= mod(); return *this; } Modular &operator-=(const Modular &other) { if ((value -= other.value) < 0) value += mod(); return *this; } template <typename U> Modular &operator+=(const U &other) { return *this += Modular(other); } template <typename U> Modular &operator-=(const U &other) { return *this -= Modular(other); } Modular &operator++() { return *this += 1; } Modular &operator--() { return *this -= 1; } Modular operator++(signed) { Modular result(*this); *this += 1; return result; } Modular operator--(signed) { Modular result(*this); *this -= 1; return result; } Modular operator-() const { return Modular(-value); } template <typename U = T> typename enable_if<is_same<typename Modular<U>::Type, signed>::value, Modular>::type & operator*=(const Modular &rhs) { #ifdef _WIN32 uint64_t x = static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value); uint32_t xh = static_cast<uint32_t>(x >> 32), xl = static_cast<uint32_t>(x), d, m; asm("divl %4; \n\t" : "=a"(d), "=d"(m) : "d"(xh), "a"(xl), "r"(mod())); value = m; #else value = normalize(static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value)); #endif return *this; } template <typename U = T> typename enable_if<is_same<typename Modular<U>::Type, int64_t>::value, Modular>::type & operator*=(const Modular &rhs) { int64_t q = static_cast<int64_t>(static_cast<double>(value) * rhs.value / mod()); value = normalize(value * rhs.value - q * mod()); return *this; } template <typename U = T> typename enable_if<!is_integral<typename Modular<U>::Type>::value, Modular>::type & operator*=(const Modular &rhs) { value = normalize(value * rhs.value); return *this; } Modular &operator/=(const Modular &other) { return *this *= Modular(minv(other.value)); } template <typename U> friend bool operator==(const Modular<U> &lhs, const Modular<U> &rhs); template <typename U> friend bool operator<(const Modular<U> &lhs, const Modular<U> &rhs); template <typename U> friend std::istream &operator>>(std::istream &stream, Modular<U> &number); operator int() { return value; } private: Type value; }; template <typename T> bool operator==(const Modular<T> &lhs, const Modular<T> &rhs) { return lhs.value == rhs.value; } template <typename T, typename U> bool operator==(const Modular<T> &lhs, U rhs) { return lhs == Modular<T>(rhs); } template <typename T, typename U> bool operator==(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) == rhs; } template <typename T> bool operator!=(const Modular<T> &lhs, const Modular<T> &rhs) { return !(lhs == rhs); } template <typename T, typename U> bool operator!=(const Modular<T> &lhs, U rhs) { return !(lhs == rhs); } template <typename T, typename U> bool operator!=(U lhs, const Modular<T> &rhs) { return !(lhs == rhs); } template <typename T> bool operator<(const Modular<T> &lhs, const Modular<T> &rhs) { return lhs.value < rhs.value; } template <typename T> Modular<T> operator+(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) += rhs; } template <typename T, typename U> Modular<T> operator+(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) += rhs; } template <typename T, typename U> Modular<T> operator+(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) += rhs; } template <typename T> Modular<T> operator-(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) -= rhs; } template <typename T, typename U> Modular<T> operator-(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) -= rhs; } template <typename T, typename U> Modular<T> operator-(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) -= rhs; } template <typename T> Modular<T> operator*(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) *= rhs; } template <typename T, typename U> Modular<T> operator*(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) *= rhs; } template <typename T, typename U> Modular<T> operator*(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) *= rhs; } template <typename T> Modular<T> operator/(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) /= rhs; } template <typename T, typename U> Modular<T> operator/(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) /= rhs; } template <typename T, typename U> Modular<T> operator/(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) /= rhs; } constexpr signed MOD = // 998244353; 1e9 + 7; // MOD using mint = Modular<std::integral_constant<decay<decltype(MOD)>::type, MOD>>; constexpr int mint_len = 1400001; vi fac, finv, inv; vi p2; mint com(int n, int r) { if (r < 0 || r > n) return 0; return mint(finv[r] * fac[n] % MOD * finv[n - r]); } mint pom(int n, int r) { /* if (!sz(fac)) com(0, -1);*/ if (r < 0 || r > n) return 0; return mint(fac[n] * finv[n - 1]); } mint npr(int n, int r) { /* if (!sz(fac)) com(0, -1);*/ if (r < 0 || r > n) return 0; return mint(fac[n] * finv[n - r]); } int nprin(int n, int r) { /* if (!sz(fac)) com(0, -1);*/ if (r < 0 || r > n) return 0; return fac[n] * finv[n - r] % MOD; } int icom(int n, int r) { const int NUM_ = 1400001; static ll fac[NUM_ + 1], finv[NUM_ + 1], inv[NUM_ + 1]; if (fac[0] == 0) { inv[1] = fac[0] = finv[0] = 1; for (int i = 2; i <= NUM_; ++i) inv[i] = inv[MOD % i] * (MOD - MOD / i) % MOD; for (int i = 1; i <= NUM_; ++i) fac[i] = fac[i - 1] * i % MOD, finv[i] = finv[i - 1] * inv[i] % MOD; } if (r < 0 || r > n) return 0; return ((finv[r] * fac[n] % MOD) * finv[n - r]) % MOD; } #define ncr com #define ncri icom // n個の場所にr個の物を置く mint nhr(int n, int r) { return com(n + r - 1, r); } mint hom(int n, int r) { return com(n + r - 1, r); } int nhri(int n, int r) { return icom(n + r - 1, r); } template <typename T> T minv(T a, T m) { T u = 0, v = 1; while (a != 0) { T t = m / a; m -= t * a; swap(a, m); u -= t * v; swap(u, v); } assert(m == 1); return u; } template <typename T> T minv(T a) { if (a < mint_len) return inv[a]; T u = 0, v = 1; T m = MOD; while (a != 0) { T t = m / a; m -= t * a; swap(a, m); u -= t * v; swap(u, v); } assert(m == 1); return u; } template <typename T, typename U> Modular<T> mpow(const Modular<T> &a, const U &b) { assert(b >= 0); int x = a(), res = 1; U p = b; while (p > 0) { if (p & 1) (res *= x) %= MOD; (x *= x) %= MOD; p >>= 1; } return res; } template <typename T, typename U, typename V> mint mpow(const T a, const U b, const V m = MOD) { assert(b >= 0); int x = a, res = 1; U p = b; while (p > 0) { if (p & 1) (res *= x) %= m; (x *= x) %= m; p >>= 1; } return res; } template <typename T, typename U> mint mpow(const T a, const U b) { assert(b >= 0); int x = a, res = 1; U p = b; while (p > 0) { if (p & 1) (res *= x) %= MOD; (x *= x) %= MOD; p >>= 1; } return res; } template <typename T, typename U, typename V> int mpowi(const T &a, const U &b, const V &m = MOD) { assert(b >= 0); int x = a, res = 1; U p = b; while (p > 0) { if (p & 1) (res *= x) %= m; (x *= x) %= m; p >>= 1; } return res; } template <typename T> string to_string(const Modular<T> &number) { return to_string(number()); } string yuri(const mint &a) { stringstream st; rep(i, 300) { rep(j, 300) { if ((mint)i / j == a) { st << i << " / " << j; return st.str(); } } } rep(i, 1000) { rep(j, 1000) { if ((mint)i / j == a) { st << i << " / " << j; return st.str(); } } } return st.str(); } template <typename T> std::ostream &operator<<(std::ostream &stream, const Modular<T> &number) { stream << number(); #ifdef _DEBUG // stream << " -> " << yuri(number); #endif return stream; } //@formatter:off template <typename T> std::istream &operator>>(std::istream &stream, Modular<T> &number) { typename common_type<typename Modular<T>::Type, int64_t>::type x; stream >> x; number.value = Modular<T>::normalize(x); return stream; } using PM = pair<mint, mint>; using vm = vector<mint>; using mapm = map<int, mint>; using umapm = umap<int, mint>; #define vvm(...) \ o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(mint, __VA_ARGS__) #define vnm(name, ...) auto name = make_v<mint>(__VA_ARGS__) struct setmod { setmod() { // p2.resize(mint_len);p2[0] = 1; for (int i = 1; i < mint_len; ++i) // p2[i] = p2[i - 1] * 2 % MOD; fac.resize(mint_len); finv.resize(mint_len); inv.resize(mint_len); inv[1] = fac[0] = finv[0] = 1; for (int i = 2; i < mint_len; ++i) inv[i] = inv[MOD % i] * (MOD - MOD / i) % MOD; for (int i = 1; i < mint_len; ++i) fac[i] = fac[i - 1] * i % MOD, finv[i] = finv[i - 1] * inv[i] % MOD; } } setmodv; //@formatter:on // nhr n個の場所にr個の物を分ける //@起動時 struct initon { initon() { cin.tie(0); ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(16); srand((unsigned)clock() + (unsigned)time(NULL)); }; } initonv; //@formatter:on // gra mll pr // 上下左右 const string udlr = "udlr"; string UDLR = "UDLR"; // x4と連動 UDLR.find('U') := x4[0] // 右、上が正 constexpr ll y4[] = {1, -1, 0, 0}; constexpr ll x4[] = {0, 0, -1, 1}; constexpr ll y8[] = {0, 1, 0, -1, -1, 1, 1, -1}; constexpr ll x8[] = {1, 0, -1, 0, 1, -1, 1, -1}; ll n, m, k, d, H, W, x, y, z, q; ll cou; vi a, b, c; // vvi (s, 0, 0); vvc(ba, 0, 0); vp p; str s; void solve() { cin >> n; rep(i, n) cin >> a[i]; int v = 0; // ダム0の雨を0とする rep(i, n) { v = a[i] * 2 - v; // ダムi+1の雨の量 } int dum = v / 2; cout << dum << " "; rep(i, n - 1) { dum = a[i] * 2 - dum; cout << dum << " "; } cout << "" << endl; } auto my(ll n, vi &a) { return 0; } auto sister(ll n, vi &a) { ll ret = 0; return ret; } signed main() { solve(); #define arg n, a #ifdef _DEBUG bool bad = 0; for (ll i = 0, ok = 1; i < k5 && ok; ++i) { ll n = rand(1, 8); vi a = ranv(n, 1, 10); auto myres = my(arg); auto res = sister(arg); ok = myres == res; if (!ok) { out(arg); cerr << "AC : " << res << endl; cerr << "MY : " << myres << endl; bad = 1; break; } } if (!bad) { // cout << "完璧 : solveを書き直そう" << endl; // cout << " : そして、solve()を呼び出すのだ" << endl; // cout << " : cin>>n; na(a,n);も忘れるな" << endl; } #endif return 0; };
// #pragma GCC optimize("Ofast") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace std::chrono; #define int long long #define ll long long auto start_time = system_clock::now(); //@formatter:off #ifdef _DEBUG // 区間削除は出来ない template <class T> struct my_pbds_tree { set<T> s; auto begin() { return s.begin(); } auto end() { return s.end(); } auto rbegin() { return s.rbegin(); } auto rend() { return s.rend(); } auto empty() { return s.empty(); } auto size() { return s.size(); } void clear() { s.clear(); } template <class U> void insert(U v) { s.insert(v); } template <class U> void operator+=(U v) { insert(v); } template <class F> auto erase(F v) { return s.erase(v); } template <class U> auto find(U v) { return s.find(v); } template <class U> auto lower_bound(U v) { return s.lower_bound(v); } template <class U> auto upper_bound(U v) { return s.upper_bound(v); } auto find_by_order(ll k) { auto it = s.begin(); for (ll i = 0; i < k; i++) it++; return it; } auto order_of_key(ll v) { auto it = s.begin(); ll i = 0; for (; it != s.end() && *it < v; i++) it++; return i; } }; #define pbds(T) my_pbds_tree<T> #else #define unordered_map __gnu_pbds::gp_hash_table // find_by_order(k) k番目のイテレーター // order_of_key(k) k以上が前から何番目か #define pbds(U) \ __gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag, \ __gnu_pbds::tree_order_statistics_node_update> #endif struct xorshift { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } size_t operator()(std::pair<ll, ll> x) const { ll v = ((x.first) << 32) | x.second; static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(v + FIXED_RANDOM); } }; template <class U, class L> void operator+=( __gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update> &s, L v) { s.insert(v); } // 衝突対策 #define ws wszzzz template <class A, class B, class C> struct T2 { A f; B s; C t; T2() { f = 0, s = 0, t = 0; } T2(A f, B s, C t) : f(f), s(s), t(t) {} bool operator<(const T2 &r) const { return f != r.f ? f < r.f : s != r.s ? s < r.s : t < r.t; /*return f != r.f ? f > r.f : s != r.s ?n s > r.s : t > r.t; 大きい順 */ } bool operator>(const T2 &r) const { return f != r.f ? f > r.f : s != r.s ? s > r.s : t > r.t; /*return f != r.f ? f > r.f : s != r.s ? s > r.s : t > r.t; 小さい順 */ } bool operator==(const T2 &r) const { return f == r.f && s == r.s && t == r.t; } bool operator!=(const T2 &r) const { return f != r.f || s != r.s || t != r.t; } }; template <class A, class B, class C, class D> struct F2 { A a; B b; C c; D d; F2() { a = 0, b = 0, c = 0, d = 0; } F2(A a, B b, C c, D d) : a(a), b(b), c(c), d(d) {} bool operator<(const F2 &r) const { return a != r.a ? a < r.a : b != r.b ? b < r.b : c != r.c ? c < r.c : d < r.d; /* return a != r.a ? a > r.a : b != r.b ? b > r.b : c != r.c ? c > r.c : d > r.d;*/ } bool operator>(const F2 &r) const { return a != r.a ? a > r.a : b != r.b ? b > r.b : c != r.c ? c > r.c : d > r.d; /* return a != r.a ? a < r.a : b != r.b ? b < r.b : c != r.c ? c < r.c : d < r.d;*/ } bool operator==(const F2 &r) const { return a == r.a && b == r.b && c == r.c && d == r.d; } bool operator!=(const F2 &r) const { return a != r.a || b != r.b || c != r.c || d != r.d; } ll operator[](ll i) { assert(i < 4); return i == 0 ? a : i == 1 ? b : i == 2 ? c : d; } }; typedef T2<ll, ll, ll> T; typedef F2<ll, ll, ll, ll> F; T mt(ll a, ll b, ll c) { return T(a, b, c); } //@マクロ省略系 型,構造 #define double long double #define ull unsigned long long using dou = double; using itn = int; using str = string; using bo = bool; #define au auto using P = pair<ll, ll>; using pd = pair<dou, dou>; #define fi first #define se second #define beg begin #define rbeg rbegin #define con continue #define bre break #define brk break #define is == #define el else #define elf else if #define wh while #define maxq 1 #define minq -1 #define ZERO(a) memset(a, 0, sizeof(a)) #define MINUS(a) memset(a, 0xff, sizeof(a)) #define MALLOC(type, len) (type *)malloc((len) * sizeof(type)) #define lam(right) [&](ll &p) { return p right; } // マクロ省略系 コンテナ using vi = vector<ll>; using vb = vector<bool>; using vs = vector<string>; using vd = vector<double>; using vc = vector<char>; using vp = vector<P>; using vt = vector<T>; #define V vector #define o_vvt(o1, o2, o3, o4, name, ...) name #define vvt0(t) V<V<t>> #define vvt1(t, a) V<V<t>> a #define vvt2(t, a, b) V<V<t>> a(b) #define vvt3(t, a, b, c) V<V<t>> a(b, V<t>(c)) #define vvt4(t, a, b, c, d) V<V<t>> a(b, V<t>(c, d)) #define vvi(...) \ o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(ll, __VA_ARGS__) #define vvb(...) \ o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(bool, __VA_ARGS__) #define vvs(...) \ o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(string, __VA_ARGS__) #define vvd(...) \ o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(double, __VA_ARGS__) #define vvc(...) \ o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(char, __VA_ARGS__) #define vvp(...) \ o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(P, __VA_ARGS__) template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); } template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); } #define vni(name, ...) auto name = make_v<ll>(__VA_ARGS__) #define vnb(name, ...) auto name = make_v<bool>(__VA_ARGS__) #define vns(name, ...) auto name = make_v<string>(__VA_ARGS__) #define vnd(name, ...) auto name = make_v<double>(__VA_ARGS__) #define vnc(name, ...) auto name = make_v<char>(__VA_ARGS__) #define vnp(name, ...) auto name = make_v<P>(__VA_ARGS__) #define PQ priority_queue<ll, vector<ll>, greater<ll>> #define tos to_string using mapi = map<ll, ll>; using mapp = map<P, ll>; using mapd = map<dou, ll>; using mapc = map<char, ll>; using maps = map<str, ll>; using seti = set<ll>; using setd = set<dou>; using setc = set<char>; using sets = set<str>; using qui = queue<ll>; #define bset bitset #define uset unordered_set #define useti unordered_set<ll, ll, xorshift> #define mset multiset #define mseti multiset<ll> #define umap unordered_map #define umapi unordered_map<ll, ll, xorshift> #define umapp unordered_map<P, ll, xorshift> #define mmap multimap template <class T> struct pq { priority_queue<T, vector<T>, greater<T>> q; /*小さい順*/ T su = 0; void clear() { q = priority_queue<T, vector<T>, greater<T>>(); su = 0; } void operator+=(T v) { su += v; q.push(v); } T sum() { return su; } T top() { return q.top(); } void pop() { su -= q.top(); q.pop(); } T poll() { T ret = q.top(); su -= ret; q.pop(); return ret; } ll size() { return q.size(); } }; template <class T> struct pqg { priority_queue<T> q; /*大きい順*/ T su = 0; void clear() { q = priority_queue<T>(); su = 0; } void operator+=(T v) { su += v; q.push(v); } T sum() { return su; } T top() { return q.top(); } void pop() { su -= q.top(); q.pop(); } T poll() { T ret = q.top(); su -= ret; q.pop(); return ret; } ll size() { return q.size(); } }; #define pqi pq<ll> #define pqgi pqg<ll> // マクロ 繰り返し #define o_rep(o1, o2, o3, o4, name, ...) name #define rep1(n) for (ll rep1i = 0, rep1lim = n; rep1i < rep1lim; ++rep1i) #define rep2(i, n) for (ll i = 0, rep2lim = n; i < rep2lim; ++i) #define rep3(i, m, n) for (ll i = m, rep3lim = n; i < rep3lim; ++i) #define rep4(i, m, n, ad) for (ll i = m, rep4lim = n; i < rep4lim; i += ad) #define rep(...) o_rep(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__) #define rer2(i, n) for (ll i = n; i >= 0; i--) #define rer3(i, m, n) for (ll i = m, rer3lim = n; i >= rer3lim; i--) #define rer4(i, m, n, dec) for (ll i = m, rer4lim = n; i >= rer4lim; i -= dec) #define rer(...) o_rep(__VA_ARGS__, rer4, rer3, rer2, )(__VA_ARGS__) #define reps2(i, j, n) \ for (ll i = 0, reps2lim = n; i < reps2lim; ++i) \ for (ll j = 0; j < reps2lim; ++j) #define reps3(i, j, k, n) \ for (ll i = 0, reps3lim = n; i < reps3lim; ++i) \ for (ll j = 0; j < reps3lim; ++j) \ for (ll k = 0; k < reps3lim; ++k) #define reps4(i, j, k, l, n) \ for (ll i = 0, reps4lim = n; i < reps4lim; ++i) \ for (ll j = 0; j < reps4lim; ++j) \ for (ll k = 0; k < reps4lim; ++k) \ for (ll l = 0; l < reps4lim; ++l) #define o_reps(o1, o2, o3, o4, o5, name, ...) name #define reps(...) o_reps(__VA_ARGS__, reps4, reps3, reps2, rep2, )(__VA_ARGS__) #define repss(i, j, k, a, b, c) \ for (ll i = 0; i < a; ++i) \ for (ll j = 0; j < b; ++j) \ for (ll k = 0; k < c; ++k) #define fora(a, b) for (auto &&a : b) #define forg(gi, ve) \ for (ll gi = 0, forglim = ve.size(), f, t, c; \ gi < forglim && (f = ve[gi].f, t = ve[gi].t, c = ve[gi].c, true); ++gi) #define fort(gi, ve) \ for (ll gi = 0, f, t, c; \ gi < ve.size() && (f = ve[gi].f, t = ve[gi].t, c = ve[gi].c, true); \ ++gi) \ if (t != p) #define form(st, l, r) \ for (auto &&it = st.lower_bound(l); it != st.end() && (*it).fi < r; ++it) #define forit(st, l, r) \ for (auto &&it = st.lower_bound(l); it != st.end() && (*it) < r;) // マクロ 定数 #define k3 1010 #define k4 10101 #define k5 101010 #define k6 1010101 #define k7 10101010 const ll inf = (ll)1e9 + 100; const ll linf = (ll)1e18 + 100; const char infc = '{'; const string infs = "{"; const double eps = 1e-9; const double PI = 3.1415926535897932384626433832795029L; ll ma = numeric_limits<ll>::min(); ll mi = numeric_limits<ll>::max(); // マクロ省略形 関数等 #define arsz(a) (sizeof(a) / sizeof(a[0])) #define sz(a) ((ll)(a).size()) #define mp make_pair #define pb pop_back #define pf push_front #define eb emplace_back #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() constexpr bool ev(ll a) { return !(a & 1); } constexpr bool od(ll a) { return (a & 1); } //@拡張系 こう出来るべきというもの // 埋め込み 存在を意識せずに機能を増やされているもの //@formatter:on namespace std { template <> class hash<std::pair<signed, signed>> { public: size_t operator()(const std::pair<signed, signed> &x) const { return hash<ll>()(((ll)x.first << 32) | x.second); } }; template <> class hash<std::pair<ll, ll>> { public : /*大きいllが渡されると、<<32でオーバーフローするがとりあえず問題ないと判断*/ size_t operator()(const std::pair<ll, ll> &x) const { return hash<ll>()(((ll)x.first << 32) | x.second); } }; } // namespace std //@formatter:off // stream まとめ istream &operator>>(istream &iss, P &a) { iss >> a.first >> a.second; return iss; } template <typename T> istream &operator>>(istream &iss, vector<T> &vec) { for (T &x : vec) iss >> x; return iss; } template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> p) { os << p.fi << " " << p.se << endl; return os; } ostream &operator<<(ostream &os, T p) { os << p.f << " " << p.s << " " << p.t; return os; } ostream &operator<<(ostream &os, F p) { os << p.a << " " << p.b << " " << p.c << " " << p.d; return os; } template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) { for (ll i = 0; i < vec.size(); ++i) os << vec[i] << (i + 1 == vec.size() ? "" : " "); return os; } template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) { for (ll i = 0; i < vec.size(); ++i) { for (ll j = 0; j < vec[i].size(); ++j) { os << vec[i][j] << " "; } os << endl; } return os; } template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &m) { for (auto &&v : m) os << v; return os; } template <typename W, typename H> void resize(vector<W> &vec, const H head) { vec.resize(head); } template <typename W, typename H, typename... T> void resize(vector<W> &vec, const H &head, const T... tail) { vec.resize(head); for (auto &v : vec) resize(v, tail...); } template <typename T, typename F> bool all_of2(T &v, F f) { return f(v); } template <typename T, typename F> bool all_of2(vector<T> &v, F f) { rep(i, sz(v)) { if (!all_of2(v[i], f)) return false; } return true; } template <typename T, typename F> bool any_of2(T &v, F f) { return f(v); } template <typename T, typename F> bool any_of2(vector<T> &v, F f) { rep(i, sz(v)) { if (any_of2(v[i], f)) return true; } return false; } template <typename T, typename F> bool none_of2(T &v, F f) { return f(v); } template <typename T, typename F> bool none_of2(vector<T> &v, F f) { rep(i, sz(v)) { if (none_of2(v[i], f)) return false; } return true; } template <typename T, typename F> bool find_if2(T &v, F f) { return f(v); } template <typename T, typename F> ll find_if2(vector<T> &v, F f) { rep(i, sz(v)) { if (find_if2(v[i], f)) return i; } return sz(v); } template <typename T, typename F> bool rfind_if2(T &v, F f) { return f(v); } template <typename T, typename F> ll rfind_if2(vector<T> &v, F f) { rer(i, sz(v) - 1) { if (rfind_if2(v[i], f)) return i; } return -1; } template <class T> bool contains(string &s, const T &v) { return s.find(v) != string::npos; } template <typename T> bool contains(vector<T> &v, const T &val) { return std::find(v.begin(), v.end(), val) != v.end(); } template <typename T, typename F> bool contains_if2(vector<T> &v, F f) { return find_if(v.begin(), v.end(), f) != v.end(); } template <typename T, typename F> ll count_if2(T &v, F f) { return f(v); } template <typename T, typename F> ll count_if2(vector<T> &vec, F f) { ll ret = 0; fora(v, vec) ret += count_if2(v, f); return ret; } template <typename T, typename F> void for_each2(T &v, F f) { f(v); } template <typename T, typename F> void for_each2(vector<T> &vec, F f) { fora(v, vec) for_each2(v, f); } template <typename W> ll count_od(vector<W> &a) { return count_if2(a, [](ll v) { return v & 1; }); } template <typename W> ll count_ev(vector<W> &a) { return count_if2(a, [](ll v) { return !(v & 1); }); } #define all_of(a, right) all_of2(a, lam(right)) #define any_of(a, right) any_of2(a, lam(right)) #define none_of(a, right) none_of2(a, lam(right)) #define find_if(a, right) find_if2(a, lam(right)) #define rfind_if(a, right) rfind_if2(a, lam(right)) #define contains_if(a, right) contains_if2(a, lam(right)) #define count_if(a, right) count_if2(a, lam(right)) #define for_each(a, right) \ do { \ fora(v, a) { v right; } \ } while (0) template <class T, class U> void replace(vector<T> &a, T key, U v) { replace(a.begin(), a.end(), key, v); } void replace(str &a, char key, str v) { if (v == "") a.erase(remove(all(a), key), a.end()); } void replace(str &a, char key, char v) { replace(all(a), key, v); } // keyと同じかどうか01で置き換える template <class T, class U> void replace(vector<T> &a, U k) { rep(i, sz(a)) a[i] = a[i] == k; } template <class T, class U> void replace(vector<vector<T>> &a, U k) { rep(i, sz(a)) rep(j, sz(a[0])) a[i][j] = a[i][j] == k; } template <class T> void replace(T &a) { replace(a, '#'); } void replace(str &a, str key, str v) { stringstream t; ll kn = sz(key); std::string::size_type Pos(a.find(key)); ll l = 0; while (Pos != std::string::npos) { t << a.substr(l, Pos - l); t << v; l = Pos + kn; Pos = a.find(key, Pos + kn); } t << a.substr(l, sz(a) - l); a = t.str(); } template <class T> bool includes(vector<T> &a, vector<T> &b) { vi c = a; vi d = b; sort(all(c)); sort(all(d)); return includes(all(c), all(d)); } template <class T> bool is_permutation(vector<T> &a, vector<T> &b) { return is_permutation(all(a), all(b)); } template <class T> bool next_permutation(vector<T> &a) { return next_permutation(all(a)); } void iota(vector<ll> &ve, ll s, ll n) { ve.resize(n); iota(all(ve), s); } vi iota(ll s, ll len) { vi ve(len); iota(all(ve), s); return ve; } template <class A, class B> auto vtop(vector<A> &a, vector<B> &b) { assert(sz(a) == sz(b)); /*stringを0で初期化できない */ vector<pair<A, B>> res; rep(i, sz(a)) res.eb(a[i], b[i]); return res; } template <class A, class B> void ptov(vector<pair<A, B>> &p, vector<A> &a, vector<B> &b) { a.resize(sz(p)), b.resize(sz(p)); rep(i, sz(p)) a[i] = p[i].fi, b[i] = p[i].se; } template <class A, class B, class C> auto vtot(vector<A> &a, vector<B> &b, vector<C> &c) { assert(sz(a) == sz(b) && sz(b) == sz(c)); vector<T2<A, B, C>> res; rep(i, sz(a)) res.eb(a[i], b[i], c[i]); return res; } template <class A, class B, class C, class D> auto vtof(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { assert(sz(a) == sz(b) && sz(b) == sz(c) && sz(c) == sz(d)); vector<F2<A, B, C, D>> res; rep(i, sz(a)) res.eb(a[i], b[i], c[i], d[i]); return res; } enum pcomparator { fisi, fisd, fdsi, fdsd, sifi, sifd, sdfi, sdfd }; enum tcomparator { fisiti, fisitd, fisdti, fisdtd, fdsiti, fdsitd, fdsdti, fdsdtd, fitisi, fitisd, fitdsi, fitdsd, fdtisi, fdtisd, fdtdsi, fdtdsd, sifiti, sifitd, sifdti, sifdtd, sdfiti, sdfitd, sdfdti, sdfdtd, sitifi, sitifd, sitdfi, sitdfd, sdtifi, sdtifd, sdtdfi, sdfdfd, tifisi, tifisd, tifdsi, tifdsd, tdfisi, tdfisd, tdfdsi, tdfdsd, tisifi, tisifd, tisdfi, tisdfd, tdsifi, tdsifd, tdsdfi, tdsdfd }; template <class A, class B> void sort(vector<pair<A, B>> &a, pcomparator type) { typedef pair<A, B> U; if (type == fisi) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se < r.se; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se > r.se; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se < r.se; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se > r.se; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi < r.fi; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi > r.fi; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi < r.fi; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi > r.fi; }); }; template <class U> void sort(vector<U> &a, pcomparator type) { if (type == fisi) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f > r.f; }); }; template <class A, class B, class C, class D> void sort(vector<F2<A, B, C, D>> &a, pcomparator type) { typedef F2<A, B, C, D> U; if (type == fisi) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a > r.a; }); }; template <class U> void sort(vector<U> &a, tcomparator type) { if (type == 0) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t < r.t; }); else if (type == 1) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t > r.t; }); else if (type == 2) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t < r.t; }); else if (type == 3) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t > r.t; }); else if (type == 4) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t < r.t; }); else if (type == 5) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t > r.t; }); else if (type == 6) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t < r.t; }); else if (type == 7) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t > r.t; }); else if (type == 8) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s < r.s; }); else if (type == 9) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s > r.s; }); else if (type == 10) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s < r.s; }); else if (type == 11) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s > r.s; }); else if (type == 12) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s < r.s; }); else if (type == 13) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s > r.s; }); else if (type == 14) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s < r.s; }); else if (type == 15) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s > r.s; }); else if (type == 16) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t < r.t; }); else if (type == 17) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t > r.t; }); else if (type == 18) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t < r.t; }); else if (type == 19) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t > r.t; }); else if (type == 20) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t < r.t; }); else if (type == 21) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t > r.t; }); else if (type == 22) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t < r.t; }); else if (type == 23) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t > r.t; }); else if (type == 24) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f < r.f; }); else if (type == 25) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f > r.f; }); else if (type == 26) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f < r.f; }); else if (type == 27) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f > r.f; }); else if (type == 28) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f < r.f; }); else if (type == 29) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f > r.f; }); else if (type == 30) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f < r.f; }); else if (type == 31) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f > r.f; }); else if (type == 32) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == 33) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == 34) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == 35) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == 36) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == 37) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == 38) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == 39) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == 40) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == 41) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == 42) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == 43) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f > r.f; }); else if (type == 44) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == 45) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == 46) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == 47) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f > r.f; }); } template <class A, class B, class C, class D> void sort(vector<F2<A, B, C, D>> &a, tcomparator type) { typedef F2<A, B, C, D> U; if (type == 0) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c < r.c; }); else if (type == 1) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c > r.c; }); else if (type == 2) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c < r.c; }); else if (type == 3) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c > r.c; }); else if (type == 4) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c < r.c; }); else if (type == 5) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c > r.c; }); else if (type == 6) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c < r.c; }); else if (type == 7) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c > r.c; }); else if (type == 8) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b < r.b; }); else if (type == 9) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b > r.b; }); else if (type == 10) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b < r.b; }); else if (type == 11) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b > r.b; }); else if (type == 12) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b < r.b; }); else if (type == 13) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b > r.b; }); else if (type == 14) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b < r.b; }); else if (type == 15) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b > r.b; }); else if (type == 16) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c < r.c; }); else if (type == 17) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c > r.c; }); else if (type == 18) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c < r.c; }); else if (type == 19) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c > r.c; }); else if (type == 20) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c < r.c; }); else if (type == 21) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c > r.c; }); else if (type == 22) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c < r.c; }); else if (type == 23) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c > r.c; }); else if (type == 24) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a < r.a; }); else if (type == 25) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a > r.a; }); else if (type == 26) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a < r.a; }); else if (type == 27) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a > r.a; }); else if (type == 28) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a < r.a; }); else if (type == 29) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a > r.a; }); else if (type == 30) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a < r.a; }); else if (type == 31) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a > r.a; }); else if (type == 32) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == 33) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == 34) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == 35) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == 36) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == 37) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == 38) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == 39) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == 40) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == 41) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == 42) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == 43) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a > r.a; }); else if (type == 44) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == 45) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == 46) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == 47) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a > r.a; }); } void sort(string &a) { sort(all(a)); } template <class T> void sort(vector<T> &a) { sort(all(a)); } // P l, P rで f(P) の形で渡す template <class U, class F> void sort(vector<U> &a, F f) { sort(all(a), [&](U l, U r) { return f(l) < f(r); }); }; template <class T> void rsort(vector<T> &a) { sort(all(a), greater<T>()); }; template <class U, class F> void rsort(vector<U> &a, F f) { sort(all(a), [&](U l, U r) { return f(l) > f(r); }); }; // F = T<T> // 例えばreturn p.fi + p.se; template <class A, class B> void sortp(vector<A> &a, vector<B> &b) { auto c = vtop(a, b); sort(c); rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se; } template <class A, class B, class F> void sortp(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); sort(c, f); rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se; } template <class A, class B> void rsortp(vector<A> &a, vector<B> &b) { auto c = vtop(a, b); rsort(c); rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second; } template <class A, class B, class F> void rsortp(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); rsort(c, f); rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second; } template <class A, class B, class C> void sortt(vector<A> &a, vector<B> &b, vector<C> &c) { auto d = vtot(a, b, c); sort(d); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class F> void sortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); sort(d, f); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C> void rsortt(vector<A> &a, vector<B> &b, vector<C> &c) { auto d = vtot(a, b, c); rsort(d); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class F> void rsortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); rsort(d, f); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class D> void sortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { auto e = vtof(a, b, c, d); sort(e); rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d; } template <class A, class B, class C, class D> void rsortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { auto e = vtof(a, b, c, d); rsort(e); rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d; } // sortindex 元のvectorはソートしない template <class T> vi sorti(vector<T> &a) { auto b = a; vi ind = iota(0, sz(a)); sortp(b, ind); return ind; } /*indexの分で型が変わるためpcomparatorが必要*/ template <class T> vi sorti(vector<T> &a, pcomparator f) { auto b = a; vi ind = iota(0, sz(a)); sortp(b, ind, f); return ind; } template <class T, class F> vi sorti(vector<T> &a, F f) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(a[x]) < f(a[y]); }); return ind; } template <class T> vi rsorti(vector<T> &a) { auto b = a; vi ind = iota(0, sz(a)); rsortp(b, ind); return ind; } template <class T, class F> vi rsorti(vector<T> &a, F f) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(a[x]) > f(a[y]); }); return ind; } template <class A, class B, class F> vi sortpi(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(c[x]) < f(c[y]); }); return ind; } template <class A, class B> vi sortpi(vector<A> &a, vector<B> &b, pcomparator f) { vi ind = iota(0, sz(a)); auto c = a; auto d = b; sortt(c, d, ind, f); return ind; } template <class A, class B> vi sortpi(vector<A> &a, vector<B> &b) { return sortpi(a, b, fisi); }; template <class A, class B, class F> vi rsortpi(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(c[x]) > f(c[y]); }); return ind; } template <class A, class B> vi rsortpi(vector<A> &a, vector<B> &b) { return sortpi(a, b, fdsd); }; template <class A, class B, class C, class F> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(d[x]) < f(d[y]); }); return ind; } template <class A, class B, class C> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, pcomparator f) { vi ind = iota(0, sz(a)); auto d = vtof(a, b, c, ind); sort(d, f); rep(i, sz(a)) ind[i] = d[i].d; return ind; } template <class A, class B, class C> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { if (a[x] == a[y]) { if (b[x] == b[y]) return c[x] < c[y]; else return b[x] < b[y]; } else { return a[x] < a[y]; } }); return ind; } template <class A, class B, class C, class F> vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(d[x]) > f(d[y]); }); return ind; } template <class A, class B, class C> vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { if (a[x] == a[y]) { if (b[x] == b[y]) return c[x] > c[y]; else return b[x] > b[y]; } else { return a[x] > a[y]; } }); return ind; } template <class T> void sort2(vector<vector<T>> &a) { for (ll i = 0, n = a.size(); i < n; ++i) sort(a[i]); } template <class T> void rsort2(vector<vector<T>> &a) { for (ll i = 0, n = a.size(); i < n; ++i) rsort(a[i]); } template <typename A, size_t N, typename T> void fill(A (&a)[N], const T &v) { rep(i, N) a[i] = v; } template <typename A, size_t N, size_t O, typename T> void fill(A (&a)[N][O], const T &v) { rep(i, N) rep(j, O) a[i][j] = v; } template <typename A, size_t N, size_t O, size_t P, typename T> void fill(A (&a)[N][O][P], const T &v) { rep(i, N) rep(j, O) rep(k, P) a[i][j][k] = v; } template <typename A, size_t N, size_t O, size_t P, size_t Q, typename T> void fill(A (&a)[N][O][P][Q], const T &v) { rep(i, N) rep(j, O) rep(k, P) rep(l, Q) a[i][j][k][l] = v; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, typename T> void fill(A (&a)[N][O][P][Q][R], const T &v) { rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) a[i][j][k][l][m] = v; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S, typename T> void fill(A (&a)[N][O][P][Q][R][S], const T &v) { rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) rep(n, S) a[i][j][k][l][m][n] = v; } template <typename W, typename T> void fill(W &xx, const T vall) { xx = vall; } template <typename W, typename T> void fill(vector<W> &vecc, const T vall) { for (auto &&vx : vecc) fill(vx, vall); } template <typename W, typename T> void fill(vector<W> &xx, const T v, ll len) { rep(i, len) xx[i] = v; } template <typename W, typename T> void fill(vector<vector<W>> &xx, const T v, ll lh, ll lw) { rep(i, lh) rep(j, lw) xx[i][j] = v; } template <class T, class U> void fill(vector<T> &a, U val, vi &ind) { fora(v, ind) a[v] = val; } template <typename A, size_t N> A sum(A (&a)[N]) { A res = 0; rep(i, N) res += a[i]; return res; } template <typename A, size_t N, size_t O> A sum(A (&a)[N][O]) { A res = 0; rep(i, N) rep(j, O) res += a[i][j]; return res; } template <typename A, size_t N, size_t O, size_t P> A sum(A (&a)[N][O][P]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) res += a[i][j][k]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A sum(A (&a)[N][O][P][Q]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) res += a[i][j][k][l]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A sum(A (&a)[N][O][P][Q][R]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) res += a[i][j][k][l][m]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A sum(A (&a)[N][O][P][Q][R][S]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) rep(n, S) res += a[i][j][k][l][m][n]; return res; } //@汎用便利関数 入力 ll in() { ll ret; cin >> ret; return ret; } string sin() { string ret; cin >> ret; return ret; } template <class T> void in(T &head) { cin >> head; } template <class T, class... U> void in(T &head, U &...tail) { cin >> head; in(tail...); } #define o_din(o1, o2, o3, o4, o5, o6, name, ...) name #define din1(a) \ ll a; \ cin >> a #define din2(a, b) \ ll a, b; \ cin >> a >> b #define din3(a, b, c) \ ll a, b, c; \ cin >> a >> b >> c #define din4(a, b, c, d) \ ll a, b, c, d; \ cin >> a >> b >> c >> d #define din5(a, b, c, d, e) \ ll a, b, c, d, e; \ cin >> a >> b >> c >> d >> e #define din6(a, b, c, d, e, f) \ ll a, b, c, d, e, f; \ cin >> a >> b >> c >> d >> e >> f #define din(...) \ o_din(__VA_ARGS__, din6, din5, din4, din3, din2, din1)(__VA_ARGS__) #define o_dind(o1, o2, o3, o4, name, ...) name #define din1d(a) \ din1(a); \ a-- #define din2d(a, b) \ din2(a, b); \ a--, b-- #define din3d(a, b, c) \ din3(a, b, c); \ a--, b--, c-- #define din4d(a, b, c, d) \ din4(a, b, c, d); \ a--, b--, c--, d-- #define dind(...) o_dind(__VA_ARGS__, din4d, din3d, din2d, din1d)(__VA_ARGS__) template <class T> void out2(T &&head) { cout << head; } template <class T, class... U> void out2(T &&head, U &&...tail) { cout << head << " "; out2(tail...); } template <class T, class... U> void out(T &&head, U &&...tail) { cout << head << " "; out2(tail...); cout << "" << endl; } template <class T> void out(T &&head) { cout << head << endl; } #ifdef _DEBUG template <class T> void outv(vector<T> &a, ll W) { rep(i, W) { cerr << a[i] << " "; } cerr << "" << endl; } template <class T> void outv(vector<vector<T>> &a, ll H = linf, ll W = linf) { rep(h, min(H, sz(a))) { rep(w, min(W, sz(a[0]))) { if (a[h][w] == linf) cerr << "e" << " "; else cerr << a[h][w] << " "; } cerr << "" << endl; } } #else template <class T> void outv(vector<T> &a, ll W) { ; } template <class T> void outv(vector<vector<T>> &a, ll H = linf, ll W = linf) { ; } #endif template <class T> void outl(vector<T> &a) { fora(v, a) cout << v << endl; } template <class T> void na(vector<T> &a, ll n) { a.resize(n); rep(i, n) cin >> a[i]; } #define dna(a, n) \ vi a(n); \ rep(dnai, n) cin >> a[dnai]; template <class T> void nao(vector<T> &a, ll n) { a.resize(n + 1); a[0] = 0; rep(i, n) cin >> a[i + 1]; } template <class T> void naod(vector<T> &a, ll n) { a.resize(n + 1); a[0] = 0; rep(i, n) cin >> a[i + 1], a[i + 1]--; } template <class T> void nad(vector<T> &a, ll n) { a.resize(n); rep(i, n) cin >> a[i], a[i]--; } template <class T, class U> void na2(vector<T> &a, vector<U> &b, ll n) { a.resize(n); b.resize(n); rep(i, n) cin >> a[i] >> b[i]; } #define dna2(a, b, n) \ vi a(n), b(n); \ rep(dna2i, n) cin >> a[dna2i] >> b[dna2i]; template <class T, class U> void nao2(vector<T> &a, vector<U> &b, ll n) { a.resize(n + 1); b.resize(n + 1); a[0] = b[0] = 0; rep(i, n) cin >> a[i + 1] >> b[i + 1]; } #define dna2d(a, b, n) \ vi a(n), b(n); \ rep(dna2di, n) { \ cin >> a[dna2di] >> b[dna2di]; \ a[dna2di]--, b[dna2di]--; \ } template <class T, class U> void na2d(vector<T> &a, vector<U> &b, ll n) { a.resize(n); b.resize(n); rep(i, n) cin >> a[i] >> b[i], a[i]--, b[i]--; } template <class T, class U, class W> void na3(vector<T> &a, vector<U> &b, vector<W> &c, ll n) { a.resize(n); b.resize(n); c.resize(n); rep(i, n) cin >> a[i] >> b[i] >> c[i]; } #define dna3(a, b, c, n) \ vi a(n), b(n), c(n); \ rep(dna3i, n) cin >> a[dna3i] >> b[dna3i] >> c[dna3i]; template <class T, class U, class W> void na3d(vector<T> &a, vector<U> &b, vector<W> &c, ll n) { a.resize(n); b.resize(n); c.resize(n); rep(i, n) cin >> a[i] >> b[i] >> c[i], a[i]--, b[i]--, c[i]--; } #define dna3d(a, b, c, n) \ vi a(n), b(n), c(n); \ rep(dna3di, n) { \ cin >> a[dna3di] >> b[dna3di] >> c[dna3di]; \ a[dna3di]--, b[dna3di]--, c[dna3di]--; \ } #define nt(a, h, w) \ resize(a, h, w); \ rep(nthi, h) rep(ntwi, w) cin >> a[nthi][ntwi]; #define ntd(a, h, w) \ resize(a, h, w); \ rep(ntdhi, h) rep(ntdwi, w) cin >> a[ntdhi][ntdwi], a[ntdhi][ntdwi]--; #define ntp(a, h, w) \ resize(a, h + 2, w + 2); \ fill(a, '#'); \ rep(ntphi, 1, h + 1) rep(ntpwi, 1, w + 1) cin >> a[ntphi][ntpwi]; // デバッグ #define sp << " " << #define debugName(VariableName) #VariableName #define deb1(x) debugName(x) << " = " << x #define deb2(x, ...) deb1(x) << ", " << deb1(__VA_ARGS__) #define deb3(x, ...) deb1(x) << ", " << deb2(__VA_ARGS__) #define deb4(x, ...) deb1(x) << ", " << deb3(__VA_ARGS__) #define deb5(x, ...) deb1(x) << ", " << deb4(__VA_ARGS__) #define deb6(x, ...) deb1(x) << ", " << deb5(__VA_ARGS__) #define deb7(x, ...) deb1(x) << ", " << deb6(__VA_ARGS__) #define deb8(x, ...) deb1(x) << ", " << deb7(__VA_ARGS__) #define deb9(x, ...) deb1(x) << ", " << deb8(__VA_ARGS__) #define deb10(x, ...) deb1(x) << ", " << deb9(__VA_ARGS__) #define o_ebug(o1, o2, o3, o4, o5, o6, o7, o8, o9, o10, name, ...) name #ifdef _DEBUG #define deb(...) \ cerr << o_ebug(__VA_ARGS__, deb10, deb9, deb8, deb7, deb6, deb5, deb4, deb3, \ deb2, deb1)(__VA_ARGS__) \ << endl #else #define deb(...) ; #endif #define debugline(x) \ cerr << x << " " \ << "(L:" << __LINE__ << ")" << '\n' //@formatter:off // よく使うクラス、構造体 struct unionfind { vector<ll> par; vector<ll> siz; vector<ll> es; ll n, trees; // 連結グループの数(親の種類) unionfind(ll n) : n(n), trees(n) { par.resize(n); siz.resize(n); es.resize(n); for (ll i = 0; i < n; i++) { par[i] = i; siz[i] = 1; } } ll root(ll x) { if (par[x] == x) { return x; } else { return par[x] = root(par[x]); } } bool unite(ll x, ll y) { x = root(x); y = root(y); es[x]++; if (x == y) return false; if (siz[x] > siz[y]) swap(x, y); trees--; par[x] = y; siz[y] += siz[x]; es[y] += es[x]; return true; } bool same(ll x, ll y) { return root(x) == root(y); } ll size(ll x) { return siz[root(x)]; } ll esize(ll x) { return es[root(x)]; } vi sizes() { vi cou(n); vi ret; ret.reserve(n); rep(i, n) { cou[root(i)]++; } rep(i, n) { if (cou[i]) ret.push_back(cou[i]); } return ret; } // つながりを無向グラフと見なし、xが閉路に含まれるか判定 bool close(ll x) { return esize(x) >= size(x); } V<vi> sets() { vi ind(n, -1); ll i = 0; vvi(res, trees); rep(j, n) { ll r = root(j); if (ind[r] == -1) ind[r] = i++; res[ind[r]].push_back(j); } rep(i, trees) { ll r = root(res[i][0]); if (res[i][0] == r) continue; rep(j, 1, sz(res[i])) { if (res[i][j] == r) { swap(res[i][0], res[i][j]); break; } } } return res; } }; //@formatter:off using bll = __int128; using u32 = unsigned; using u64 = unsigned long long; using u128 = __uint128_t; std::ostream &operator<<(std::ostream &dest, __int128_t value) { std::ostream::sentry s(dest); if (s) { __uint128_t tmp = value < 0 ? -value : value; char buffer[128]; char *d = std::end(buffer); do { --d; *d = "0123456789"[tmp % 10]; tmp /= 10; } while (tmp != 0); if (value < 0) { --d; *d = '-'; } ll len = std::end(buffer) - d; if (dest.rdbuf()->sputn(d, len) != len) { dest.setstate(std::ios_base::badbit); } } return dest; } //__int128 toi128(string &s) { __int128 ret = 0; for (ll i = 0; i < //s.length(); ++i) if ('0' <= s[i] && s[i] <= '9') ret = 10 * //ret + s[i] - '0'; return ret;} // エラー void ole() { #ifdef _DEBUG debugline("ole"); exit(0); #endif string a = "a"; rep(i, 30) a += a; rep(i, 1 << 17) cout << a << endl; cout << "OLE 出力長制限超過" << endl; exit(0); } void re() { assert(0 == 1); exit(0); } void tle() { while (inf) cout << inf << endl; } // 便利関数 // テスト用 char ranc() { return (char)('a' + rand() % 26); } ll rand(ll min, ll max) { assert(min <= max); if (min >= 0 && max >= 0) { return rand() % (max + 1 - min) + min; } else if (max < 0) { return -rand(-max, -min); } else { if (rand() % 2) { return rand(0, max); } else { return -rand(0, -min); } } } vi ranv(ll n, ll min, ll max) { vi v(n); rep(i, n) v[i] = rand(min, max); return v; } str ransu(ll n) { str s; rep(i, n) s += (char)rand('A', 'Z'); return s; } str ransl(ll n) { str s; rep(i, n) s += (char)rand('a', 'z'); return s; } // 単調増加 vi ranvinc(ll n, ll min, ll max) { vi v(n); bool bad = 1; while (bad) { bad = 0; v.resize(n); rep(i, n) { if (i && min > max - v[i - 1]) { bad = 1; break; } if (i) v[i] = v[i - 1] + rand(min, max - v[i - 1]); else v[i] = rand(min, max); } } return v; } // 便利 汎用 void ranvlr(ll n, ll min, ll max, vi &l, vi &r) { l.resize(n); r.resize(n); rep(i, n) { l[i] = rand(min, max); r[i] = l[i] + rand(0, max - l[i]); } } vp run_length(vi &a) { vp ret; ret.eb(a[0], 1); rep(i, 1, sz(a)) { if (ret.back().fi == a[i]) { ret.back().se++; } else { ret.eb(a[i], 1); } } return ret; } vector<pair<char, ll>> run_length(string &a) { vector<pair<char, ll>> ret; ret.eb(a[0], 1); rep(i, 1, sz(a)) { if (ret.back().fi == a[i]) { ret.back().se++; } else { ret.eb(a[i], 1); } } return ret; } template <class F> ll mgr(ll ok, ll ng, F f) { if (ok < ng) while (ng - ok > 1) { ll mid = (ok + ng) / 2; if (f(mid)) ok = mid; else ng = mid; } else while (ok - ng > 1) { ll mid = (ok + ng) / 2; if (f(mid)) ok = mid; else ng = mid; } return ok; } // strを整数として比較 string smax(str &a, str b) { if (sz(a) < sz(b)) { return b; } else if (sz(a) > sz(b)) { return a; } else if (a < b) return b; else return a; } // strを整数として比較 string smin(str &a, str b) { if (sz(a) > sz(b)) { return b; } else if (sz(a) < sz(b)) { return a; } else if (a > b) return b; else return a; } template <typename W, typename T> ll find(vector<W> &a, const T key) { rep(i, sz(a)) if (a[i] == key) return i; return -1; } template <typename W, typename T> P find(vector<vector<W>> &a, const T key) { rep(i, sz(a)) rep(j, sz(a[0])) if (a[i][j] == key) return mp(i, j); return mp(-1, -1); } template <typename W, typename U> T find(vector<vector<vector<W>>> &a, const U key) { rep(i, sz(a)) rep(j, sz(a[0])) rep(k, sz(a[0][0])) if (a[i][j][k] == key) return mt(i, j, k); return mt(-1, -1, -1); } template <typename W, typename T> ll count2(W &a, const T k) { return a == k; } template <typename W, typename T> ll count2(vector<W> &a, const T k) { ll ret = 0; fora(v, a) ret += count2(v, k); return ret; } template <typename W, typename T> ll count(vector<W> &a, const T k) { ll ret = 0; fora(v, a) ret += count2(v, k); return ret; } ll count(str &a, str k) { ll ret = 0, len = k.length(); auto pos = a.find(k); while (pos != string::npos) pos = a.find(k, pos + len), ++ret; return ret; } vi count(str &a) { vi cou(26); char c = 'a'; if ('A' <= a[0] && a[0] <= 'Z') c = 'A'; rep(i, sz(a))++ cou[a[i] - c]; return cou; } #define couif count_if // algorythm ll rev(ll a) { ll res = 0; while (a) { res *= 10; res += a % 10; a /= 10; } return res; } template <class T> void rev(vector<T> &a) { reverse(all(a)); } template <class U> void rev(vector<vector<U>> &a) { vector<vector<U>> b(sz(a[0]), vector<U>(sz(a))); rep(h, sz(a)) rep(w, sz(a[0])) b[w][h] = a[h][w]; a = b; } void rev(string &a) { reverse(all(a)); } constexpr ll p10[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000ll, 100000000000ll, 1000000000000ll, 10000000000000ll, 100000000000000ll, 1000000000000000ll, 10000000000000000ll, 100000000000000000ll, 1000000000000000000ll}; ll get(ll a, ll keta) { return (a / (ll)pow(10, keta)) % 10; } ll keta(ll v) { if (v < p10[9]) { if (v < p10[4]) { if (v < p10[2]) { if (v < p10[1]) return 1; else return 2; } else { if (v < p10[3]) return 3; else return 4; } } else { if (v < p10[7]) { if (v < p10[5]) return 5; else if (v < p10[6]) return 6; else return 7; } else { if (v < p10[8]) return 8; else return 9; } } } else { if (v < p10[13]) { if (v < p10[11]) { if (v < p10[10]) return 10; else return 11; } else { if (v < p10[12]) return 12; else return 13; } } else { if (v < p10[15]) { if (v < p10[14]) return 14; else if (v < p10[15]) return 15; else return 16; } else { if (v < p10[17]) return 17; else return 18; } } } } ll dsum(ll v, ll sin = 10) { ll ret = 0; for (; v; v /= sin) ret += v % sin; return ret; } struct sint { ll v; sint(ll v) : v(v) {} operator ll() { return v; } // 下からi番目 ll operator[](ll i) { return (v / p10[i]) % 10; } ll back(ll i) { return operator[](i); } // 上からi番目 ll top(ll i) { ll len = keta(v); return operator[](len - 1 - i); } // 先頭からi番目にセット ll settop(ll i, ll k) { ll len = keta(v); return set(len - 1 - i, k); } ll set(ll i, ll k) { if (i < 0) return settop(abs(i) - 1, k); return v += p10[i] * (k - (v / p10[i]) % 10); } ll add(ll i, ll k = 1) { return v += p10[i] * k; } ll addtop(ll i, ll k = 1) { return v += p10[keta(v) - i - 1] * k; } ll dec(ll i, ll k = 1) { return v -= p10[i] * k; } ll dectop(ll i, ll k = 1) { return v -= p10[keta(v) - i - 1] * k; } #define op(t, o) \ template <class T> t operator o(T r) { return v o r; } op(ll, +=); op(ll, -=); op(ll, *=); op(ll, /=); op(ll, %=); op(ll, +); op(ll, -); op(ll, *); op(ll, /); op(ll, %); op(bool, ==); op(bool, !=); op(bool, <); op(bool, <=); op(bool, >); op(bool, >=); #undef op template <class T> ll operator<<=(T r) { return v *= p10[r]; } template <class T> ll operator<<(T r) { return v * p10[r]; } template <class T> ll operator>>=(T r) { return v /= p10[r]; } template <class T> ll operator>>(T r) { return v / p10[r]; } }; ll mask10(ll v) { return p10[v] - 1; } // 変換系 template <class T> auto keys(T a) { vector<decltype((a.begin())->fi)> res; for (auto &&k : a) res.push_back(k.fi); return res; } template <class T> auto values(T a) { vector<decltype((a.begin())->se)> res; for (auto &&k : a) res.push_back(k.se); return res; } template <class T, class U> bool chma(T &a, const U &b) { if (a < b) { a = b; return true; } return false; } template <class U> bool chma(const U &b) { return chma(ma, b); } template <class T, class U> bool chmi(T &a, const U &b) { if (b < a) { a = b; return true; } return false; } template <class U> bool chmi(const U &b) { return chmi(mi, b); } template <class T> T min(T a, signed b) { return a < b ? a : b; } template <class T> T max(T a, signed b) { return a < b ? b : a; } template <class T> T min(T a, T b, T c) { return a >= b ? b >= c ? c : b : a >= c ? c : a; } template <class T> T max(T a, T b, T c) { return a <= b ? b <= c ? c : b : a <= c ? c : a; } template <class T> T min(vector<T> &a) { return *min_element(all(a)); } template <class T> T mini(vector<T> &a) { return min_element(all(a)) - a.begin(); } template <class T> T min(vector<T> &a, ll n) { return *min_element(a.begin(), a.begin() + min(n, sz(a))); } template <class T> T min(vector<T> &a, ll s, ll n) { return *min_element(a.begin() + s, a.begin() + min(n, sz(a))); } template <class T> T max(vector<T> &a) { return *max_element(all(a)); } template <class T, class U> T max(vector<T> &a, vector<U> &b) { return max(*max_element(all(a)), *max_element(all(b))); } template <class T> T maxi(vector<T> &a) { return max_element(all(a)) - a.begin(); } template <class T> T max(vector<T> &a, ll n) { return *max_element(a.begin(), a.begin() + min(n, sz(a))); } template <class T> T max(vector<T> &a, ll s, ll n) { return *max_element(a.begin() + s, a.begin() + min(n, sz(a))); } template <typename A, size_t N> A max(A (&a)[N]) { A res = a[0]; rep(i, N) res = max(res, a[i]); return res; } template <typename A, size_t N, size_t O> A max(A (&a)[N][O]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P> A max(A (&a)[N][O][P]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A max(A (&a)[N][O][P][Q], const T &v) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A max(A (&a)[N][O][P][Q][R]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A max(A (&a)[N][O][P][Q][R][S]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N> A min(A (&a)[N]) { A res = a[0]; rep(i, N) res = min(res, a[i]); return res; } template <typename A, size_t N, size_t O> A min(A (&a)[N][O]) { A res = min(a[0]); rep(i, N) res = min(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P> A min(A (&a)[N][O][P]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A min(A (&a)[N][O][P][Q], const T &v) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A min(A (&a)[N][O][P][Q][R]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A min(A (&a)[N][O][P][Q][R][S]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <class T> T sum(vector<T> &v, ll s = 0, ll t = inf) { T ret = 0; rep(i, s, min(sz(v), t)) ret += v[i]; return ret; } template <class T> T sum(vector<vector<T>> &v) { T ret = 0; rep(i, sz(v)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<T>>> &v) { T ret = 0; rep(i, sz(v)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<vector<T>>>> &v) { T ret = 0; rep(i, sz(v)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<vector<vector<T>>>>> &v) { T ret = 0; rep(i, sz(v)) ret += sum(v[i]); return ret; } template <class T> auto sum(priority_queue<T, vector<T>, greater<T>> &r) { auto q = r; T ret = 0; while (sz(q)) { ret += q.top(); q.pop(); } return ret; } template <class T> auto sum(priority_queue<T> &r) { auto q = r; T ret = 0; while (sz(q)) { ret += q.top(); q.pop(); } return ret; } // template<class T, class U, class... W> auto sumn(vector<T> &v, U head, W... // tail) { auto ret = sum(v[0], tail...); rep(i, 1, min(sz(v), head))ret // += sum(v[i], tail...); return ret;} void clear(PQ &q) { q = PQ(); } template <class T> void clear(queue<T> &q) { while (q.size()) q.pop(); } template <class T> T *negarr(ll size) { T *body = (T *)malloc((size * 2 + 1) * sizeof(T)); return body + size; } template <class T> T *negarr2(ll h, ll w) { double **dummy1 = new double *[2 * h + 1]; double *dummy2 = new double[(2 * h + 1) * (2 * w + 1)]; dummy1[0] = dummy2 + w; for (ll i = 1; i <= 2 * h + 1; ++i) { dummy1[i] = dummy1[i - 1] + 2 * w + 1; } double **a = dummy1 + h; return a; } // imoは0-indexed // ruiは1-indexed template <class T> vector<T> imo(vector<T> &v) { vector<T> ret = v; rep(i, sz(ret) - 1) ret[i + 1] += ret[i]; return ret; } // kと同じものの数 template <class T, class U> vi imo(vector<T> &a, U k) { vector<T> ret = a; rep(i, sz(ret)) ret[i] = a[i] == k; rep(i, sz(ret) - 1) ret[i + 1] += ret[i]; return ret; } template <class T> vector<T> imox(vector<T> &v) { vector<T> ret = v; rep(i, sz(ret) - 1) ret[i + 1] ^= ret[i]; return ret; } // 漸化的に最小を持つ template <class T> vector<T> imi(vector<T> &v) { vector<T> ret = v; rep(i, sz(ret) - 1) chmi(ret[i + 1], ret[i]); return ret; } template <class T> struct ruiC { const vector<T> rui; ruiC(vector<T> &ru) : rui(ru) {} T operator()(ll l, ll r) { if (l > r) { cerr << "ruic "; deb(l, r); assert(0); } return rui[r] - rui[l]; } T operator[](ll i) { return rui[i]; } T back() { return rui.back(); } ll size() { return rui.size(); } }; template <class T> struct rruic { const T *rrui; rruic(T *ru) : rrui(ru) {} T operator()(ll l, ll r) { assert(l >= r); return rrui[r] - rrui[l]; } T operator[](ll i) { return rrui[i]; } }; template <class T> vector<T> ruiv(vector<T> &a) { vector<T> ret(a.size() + 1); rep(i, a.size()) ret[i + 1] = ret[i] + a[i]; return ret; } template <class T> ruiC<T> ruic(vector<T> &a) { vector<T> ret = ruiv(a); return ruiC<T>(ret); } vector<ll> ruiv(string &a) { if (sz(a) == 0) return vi(1); ll dec = ('0' <= a[0] && a[0] <= '9') ? '0' : 0; vector<ll> ret(a.size() + 1); rep(i, a.size()) ret[i + 1] = ret[i] + a[i] - dec; return ret; } ruiC<ll> ruic(string &a) { vector<ll> ret = ruiv(a); return ruiC<ll>(ret); } // kと同じものの数 template <class T, class U> vi ruiv(T &a, U k) { vi ret(a.size() + 1); rep(i, a.size()) ret[i + 1] = ret[i] + (a[i] == k); return ret; } template <class T, class U> ruiC<ll> ruic(T &a, U k) { vi ret = ruiv(a, k); return ruiC<ll>(ret); } // xor template <class T> vector<T> ruix(vector<T> &a) { vector<T> ret(a.size() + 1); rep(i, a.size()) ret[i + 1] = ret[i] ^ a[i]; return ret; } template <class T> vector<T> ruim(vector<T> &a) { vector<T> res(a.size() + 1, 1); rep(i, a.size()) res[i + 1] = res[i] * a[i]; return res; } // 漸化的に最小を1indexで持つ template <class T> vector<T> ruimi(vector<T> &a) { ll n = sz(a); vector<T> ret(n + 1); rep(i, 1, n) { ret[i] = a[i - 1]; chmi(ret[i + 1], ret[i]); } return ret; } // template<class T> T *rrui(vector<T> &a) { // 右から左にかけての半開区間 (-1 n-1] template <class T> rruic<T> rrui(vector<T> &a) { ll len = a.size(); T *body = (T *)malloc((len + 1) * sizeof(T)); T *res = body + 1; rer(i, len - 1) res[i - 1] = res[i] + a[i]; return rruic<T>(res); } // 掛け算 template <class T> T *rruim(vector<T> &a) { ll len = a.size(); T *body = (T *)malloc((len + 1) * sizeof(T)); T *res = body + 1; res[len - 1] = 1; rer(i, len - 1) res[i - 1] = res[i] * a[i]; return res; } template <class T, class U> void inc(T &a, U v = 1) { a += v; } template <class T, class U> void inc(vector<T> &a, U v = 1) { for (auto &u : a) inc(u, v); } template <class T, class U> void dec(T &a, U v = 1) { a -= v; } template <class T, class U> void dec(vector<T> &a, U v = 1) { for (auto &u : a) dec(u, v); } template <class U> void dec(string &a, U v = 1) { for (auto &u : a) dec(u, v); } template <class T> void dec(vector<T> &a) { for (auto &u : a) dec(u, 1); } bool ins(ll h, ll w, ll H, ll W) { return h >= 0 && w >= 0 && h < H && w < W; } bool ins(ll l, ll v, ll r) { return l <= v && v < r; } template <class T> bool ins(vector<T> &a, ll i, ll j = 0) { return ins(0, i, sz(a)) && ins(0, j, sz(a)); } ll u(ll a) { return a < 0 ? 0 : a; } template <class T> vector<T> u(const vector<T> &a) { vector<T> ret = a; fora(v, ret) v = u(v); return ret; } #define MIN(a) numeric_limits<a>::min() #define MAX(a) numeric_limits<a>::max() template <class F> ll goldd_l(ll left, ll right, F calc) { double GRATIO = 1.6180339887498948482045868343656; ll lm = left + (ll)((right - left) / (GRATIO + 1.0)); ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); ll fl = calc(lm); ll fr = calc(rm); while (right - left > 10) { if (fl < fr) { right = rm; rm = lm; fr = fl; lm = left + (ll)((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } ll minScore = MAX(ll); ll resIndex = left; for (ll i = left; i < right + 1; ++i) { ll score = calc(i); if (minScore > score) { minScore = score; resIndex = i; } } return resIndex; } template <class F> ll goldt_l(ll left, ll right, F calc) { double GRATIO = 1.6180339887498948482045868343656; ll lm = left + (ll)((right - left) / (GRATIO + 1.0)); ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); ll fl = calc(lm); ll fr = calc(rm); while (right - left > 10) { if (fl > fr) { right = rm; rm = lm; fr = fl; lm = left + (ll)((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } if (left > right) { ll l = left; left = right; right = l; } ll maxScore = MIN(ll); ll resIndex = left; for (ll i = left; i < right + 1; ++i) { ll score = calc(i); if (maxScore < score) { maxScore = score; resIndex = i; } } return resIndex; } /*loopは200にすればおそらく大丈夫 余裕なら300に*/ template <class F> dou goldd_d(dou left, dou right, F calc, ll loop = 140) { dou GRATIO = 1.6180339887498948482045868343656; dou lm = left + ((right - left) / (GRATIO + 1.0)); dou rm = lm + ((right - lm) / (GRATIO + 1.0)); dou fl = calc(lm); dou fr = calc(rm); /*200にすればおそらく大丈夫*/ /*余裕なら300に*/ ll k = 141; loop++; while (--loop) { if (fl < fr) { right = rm; rm = lm; fr = fl; lm = left + ((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + ((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } return left; } template <class F> dou goldt_d(dou left, dou right, F calc, ll loop = 140) { double GRATIO = 1.6180339887498948482045868343656; dou lm = left + ((right - left) / (GRATIO + 1.0)); dou rm = lm + ((right - lm) / (GRATIO + 1.0)); dou fl = calc(lm); dou fr = calc(rm); loop++; while (--loop) { if (fl > fr) { right = rm; rm = lm; fr = fl; lm = left + ((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + ((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } return left; } // l ~ rを複数の区間に分割し、極致を与えるiを返す time-20 msまで探索 template <class F> ll goldd_ls(ll l, ll r, F calc, ll time = 2000) { auto lim = milliseconds(time - 20); ll mini = 0, minv = MAX(ll); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/ ll nl = l; ll nr = l + haba; rep(i, k) { ll ni = goldd_l(nl, nr, calc); if (chmi(minv, calc(ni))) mini = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return mini; } template <class F> ll goldt_ls(ll l, ll r, F calc, ll time = 2000) { auto lim = milliseconds(time - 20); ll maxi = 0, maxv = MIN(ll); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/ ll nl = l; ll nr = l + haba; rep(i, k) { ll ni = goldt_l(nl, nr, calc); if (chma(maxv, calc(ni))) maxi = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return maxi; } template <class F> dou goldd_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を持つ*/ auto lim = milliseconds(time - 20); dou mini = 0, minv = MAX(dou); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); dou haba = (r - l) / k; dou nl = l; dou nr = l + haba; rep(i, k) { dou ni = goldd_d(nl, nr, calc); if (chmi(minv, calc(ni))) mini = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return mini; } template <class F> dou goldt_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を残している*/ auto lim = milliseconds(time - 20); dou maxi = 0, maxv = MIN(dou); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); dou haba = (r - l) / k; dou nl = l; dou nr = l + haba; rep(i, k) { dou ni = goldt_d(nl, nr, calc); if (chma(maxv, calc(ni))) maxi = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return maxi; } template <class T> T min(vector<vector<T>> &a) { T res = MAX(T); rep(i, a.size()) chmi(res, *min_element(all(a[i]))); return res; } template <class T> T max(vector<vector<T>> &a) { T res = MIN(T); rep(i, a.size()) chma(res, *max_element(all(a[i]))); return res; } constexpr bool bget(ll m, ll keta) { return (m >> keta) & 1; } ll bget(ll m, ll keta, ll sinsuu) { m /= (ll)pow(sinsuu, keta); return m % sinsuu; } ll bit(ll n) { return (1LL << (n)); } ll bit(ll n, ll sinsuu) { return (ll)pow(sinsuu, n); } ll mask(ll n) { return (1ll << n) - 1; } #define bcou __builtin_popcountll // 最下位ビット ll lbit(ll n) { return n & -n; } // 最上位ビット ll hbit(ll n) { n |= (n >> 1); n |= (n >> 2); n |= (n >> 4); n |= (n >> 8); n |= (n >> 16); n |= (n >> 32); return n - (n >> 1); } ll hbitk(ll n) { ll k = 0; rer(i, 5) { ll a = k + (1ll << i); ll b = 1ll << a; if (b <= n) k += 1ll << i; } return k; } // 初期化は0を渡す ll nextComb(ll &mask, ll n, ll r) { if (!mask) return mask = (1LL << r) - 1; ll x = mask & -mask; /*最下位の1*/ ll y = mask + x; /*連続した下の1を繰り上がらせる*/ ll res = ((mask & ~y) / x >> 1) | y; if (bget(res, n)) return mask = 0; else return mask = res; } // n桁以下でビットがr個立っているもののvectorを返す vi bitCombList(ll n, ll r) { vi res; ll m = 0; while (nextComb(m, n, r)) { res.push_back(m); } return res; } char itoal(ll i) { return 'a' + i; } char itoaL(ll i) { return 'A' + i; } ll altoi(char c) { if ('A' <= c && c <= 'Z') return c - 'A'; return c - 'a'; } ll ctoi(char c) { return c - '0'; } char itoc(ll i) { return i + '0'; } ll vtoi(vi &v) { ll res = 0; if (sz(v) > 18) { debugline("vtoi"); deb(sz(v)); ole(); } rep(i, sz(v)) { res *= 10; res += v[i]; } return res; } vi itov(ll i) { vi res; while (i) { res.push_back(i % 10); i /= 10; } rev(res); return res; } vi stov(string &a) { ll n = sz(a); vi ret(n); rep(i, n) { ret[i] = a[i] - '0'; } return ret; } // 基準を満たさないものは0になる vi stov(string &a, char one) { ll n = sz(a); vi ret(n); rep(i, n) ret[i] = a[i] == one; return ret; } vector<vector<ll>> ctoi(vector<vector<char>> s, char c) { ll n = sz(s), m = sz(s[0]); vector<vector<ll>> res(n, vector<ll>(m)); rep(i, n) rep(j, m) res[i][j] = s[i][j] == c; return res; } #define unique(v) v.erase(unique(v.begin(), v.end()), v.end()); //[i] := i番として圧縮されたものを返す vi compress(vi &a) { vi b; ll len = a.size(); for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { a[i] = lower_bound(all(b), a[i]) - b.begin(); } ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, umap<ll, ll> &map) { vi b; ll len = a.size(); for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { ll v = a[i]; a[i] = lower_bound(all(b), a[i]) - b.begin(); map[v] = a[i]; } ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, vi &r) { vi b; ll len = a.size(); fora(v, a) b.push_back(v); fora(v, r) b.push_back(v); sort(b); unique(b); for (ll i = 0; i < len; ++i) a[i] = lower_bound(all(b), a[i]) - b.begin(); for (ll i = 0; i < sz(r); ++i) r[i] = lower_bound(all(b), r[i]) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, vi &r, vi &s) { vi b; ll len = a.size(); fora(v, a) b.push_back(v); fora(v, r) b.push_back(v); fora(v, s) b.push_back(v); sort(b); unique(b); for (ll i = 0; i < len; ++i) a[i] = lower_bound(all(b), a[i]) - b.begin(); for (ll i = 0; i < sz(r); ++i) r[i] = lower_bound(all(b), r[i]) - b.begin(); for (ll i = 0; i < sz(s); ++i) r[i] = lower_bound(all(b), s[i]) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(V<vi> &a) { vi b; fora(vv, a) fora(v, vv) b.push_back(v); sort(b); unique(b); fora(vv, a) fora(v, vv) v = lower_bound(all(b), v) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vector<vector<vi>> &a) { vi b; fora(vvv, a) fora(vv, vvv) fora(v, vv) b.push_back(v); sort(b); unique(b); fora(vvv, a) fora(vv, vvv) fora(v, vv) v = lower_bound(all(b), v) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } void compress(ll a[], ll len) { vi b; for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { a[i] = lower_bound(all(b), a[i]) - b.begin(); } } // 要素が見つからなかったときに困る #define binarySearch(a, v) (binary_search(all(a), v)) #define lowerIndex(a, v) (lower_bound(all(a), v) - a.begin()) #define lowerBound(a, v) (*lower_bound(all(a), v)) #define upperIndex(a, v) (upper_bound(all(a), v) - a.begin()) #define upperBound(a, v) (*upper_bound(all(a), v)) template <class T> void fin(T s) { cout << s << endl, exit(0); } // 便利 数学 math ll mod(ll a, ll m) { return (a % m + m) % m; } ll pow(ll a) { return a * a; }; ll fact(ll v) { return v <= 1 ? 1 : v * fact(v - 1); } ll comi(ll n, ll r) { assert(n < 100); static vvi(pas, 100, 100); if (pas[0][0]) return pas[n][r]; pas[0][0] = 1; rep(i, 1, 100) { pas[i][0] = 1; rep(j, 1, i + 1) pas[i][j] = pas[i - 1][j - 1] + pas[i - 1][j]; } return pas[n][r]; } double comd(ll n, ll r) { assert(n < 2020); static vvd(comb, 2020, 2020); if (comb[0][0] == 0) { comb[0][0] = 1; rep(i, 2000) { comb[i + 1][0] = 1; rep(j, 1, i + 2) { comb[i + 1][j] = comb[i][j] + comb[i][j - 1]; } } } return comb[n][r]; } ll gcd(ll a, ll b) { while (b) a %= b, swap(a, b); return abs(a); } ll gcd(vi b) { ll res = b[0]; rep(i, 1, sz(b)) res = gcd(b[i], res); return res; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll lcm(vi a) { ll res = a[0]; rep(i, 1, sz(a)) res = lcm(a[i], res); return res; } ll ceil(ll a, ll b) { if (b == 0) { debugline("ceil"); deb(a, b); ole(); return -1; } else if (a < 0) { return 0; } else { return (a + b - 1) / b; } } ll lower_remi__bx_a(ll kei, ll rem, ll x) { if (rem >= x) return 0; return (x - rem + kei - 1) / kei; } ll lower_remv__bx_a(ll kei, ll rem, ll x) { if (rem >= x) return rem; return (x - rem + kei - 1) / kei * kei + rem; } ll upper_remi__bx_a(ll kei, ll rem, ll x) { if (rem > x) return 0; return (x - rem + kei) / kei; } ll upper_remv__bx_a(ll kei, ll rem, ll x) { if (rem > x) return rem; return (x - rem + kei) / kei * kei + rem; } // v * v >= aとなる最小のvを返す ll sqrt(ll a) { if (a < 0) { debugline("sqrt"); deb(a); ole(); } ll res = (ll)std::sqrt(a); while (res * res < a) ++res; return res; } double log(double e, double x) { return log(x) / log(e); } ll sig(ll t) { return ((1 + t) * t) >> 1; } ll sig(ll s, ll t) { return ((s + t) * (t - s + 1)) >> 1; } // 幾何 Pをcomplexとして扱う template <class T, class U> bool eq(T a, U b) { return fabs(a - b) < eps; } dou atan2(pd a) { return atan2(a.se, a.fi); } dou angle(pd f, pd t) { return atan2(t.se - f.se, t.fi - f.fi); } dou distance(pd a, pd b) { return hypot(a.fi - b.fi, a.se - b.se); } // bを中心とするabcのtheta aからcにかけて時計回り dou angle(pd a, pd b, pd c) { dou ax = a.fi - b.fi; dou ay = a.se - b.se; dou cx = c.fi - b.fi; dou cy = c.se - b.se; double ret = atan2(cy, cx) - atan2(ay, ax); if (ret < 0) ret += 2 * PI; return ret; } dou dot(pd a, pd b) { return a.fi * b.fi + a.se + b.se; } dou cro(pd a, pd b) { return a.fi * b.se - a.se + b.fi; } // 機能拡張 template <typename CharT, typename Traits, typename Alloc> basic_string<CharT, Traits, Alloc> operator+(const basic_string<CharT, Traits, Alloc> &lhs, const int rv) { basic_string<CharT, Traits, Alloc> str(lhs); str.append(to_string(rv)); return str; } template <typename CharT, typename Traits, typename Alloc> void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const int rv) { lhs += to_string(rv); } template <typename CharT, typename Traits, typename Alloc> basic_string<CharT, Traits, Alloc> operator+(const basic_string<CharT, Traits, Alloc> &lhs, const signed rv) { basic_string<CharT, Traits, Alloc> str(lhs); str.append(to_string(rv)); return str; } template <typename CharT, typename Traits, typename Alloc> void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const signed rv) { lhs += to_string(rv); } template <class T, class U> void operator+=(queue<T> &a, U v) { a.push(v); } template <class T, class U> void operator+=(deque<T> &a, U v) { a.push_back(v); } template <class T> priority_queue<T, vector<T>, greater<T>> & operator+=(priority_queue<T, vector<T>, greater<T>> &a, vector<T> &v) { fora(d, v) a.push(d); return a; } template <class T, class U> priority_queue<T, vector<T>, greater<T>> & operator+=(priority_queue<T, vector<T>, greater<T>> &a, U v) { a.push(v); return a; } template <class T, class U> priority_queue<T> &operator+=(priority_queue<T> &a, U v) { a.push(v); return a; } template <class T> set<T> &operator+=(set<T> &a, vector<T> v) { fora(d, v) a.insert(d); return a; } template <class T, class U> auto operator+=(set<T> &a, U v) { return a.insert(v); } template <class T, class U> auto operator-=(set<T> &a, U v) { return a.erase(v); } template <class T, class U> auto operator+=(mset<T> &a, U v) { return a.insert(v); } template <class T, class U> set<T, greater<T>> &operator+=(set<T, greater<T>> &a, U v) { a.insert(v); return a; } template <class T, class U> vector<T> &operator+=(vector<T> &a, U v) { a.push_back(v); return a; } template <class T, class U> vector<T> operator+(const vector<T> &a, U v) { vector<T> ret = a; ret += v; return ret; } template <class T, class U> vector<T> operator+(U v, const vector<T> &a) { vector<T> ret = a; ret.insert(ret.begin(), v); return ret; } template <class T> vector<T> operator+(vector<T> a, vector<T> b) { vector<T> ret; ret = a; fora(v, b) ret += v; return ret; } template <class T> vector<T> &operator+=(vector<T> &a, vector<T> &b) { fora(v, b) a += v; return a; } template <class T> vector<T> &operator-=(vector<T> &a, vector<T> &b) { if (sz(a) != sz(b)) { debugline("vector<T> operator-="); deb(a); deb(b); exit(0); } rep(i, sz(a)) a[i] -= b[i]; return a; } template <class T> vector<T> operator-(vector<T> &a, vector<T> &b) { if (sz(a) != sz(b)) { debugline("vector<T> operator-"); deb(a); deb(b); ole(); } vector<T> res(sz(a)); rep(i, sz(a)) res[i] = a[i] - b[i]; return res; } template <class T, class U> vector<T> operator*(vector<T> &a, U b) { vector<T> ret; fora(v, a) ret += v * b; return ret; } template <class T, class U> vector<T> operator/(vector<T> &a, U b) { vector<T> ret; fora(v, a) ret += v / b; return ret; } template <class T, class U> vector<T> operator*=(vector<T> &a, U b) { fora(v, a) v *= b; return a; } template <class T, class U> vector<T> operator/=(vector<T> &a, U b) { fora(v, a) v /= b; return a; } template <typename T> void erase(vector<T> &v, unsigned ll i) { v.erase(v.begin() + i); } template <typename T> void erase(vector<T> &v, unsigned ll s, unsigned ll e) { v.erase(v.begin() + s, v.begin() + e); } template <class T, class U> void erase(map<T, U> &m, ll okl, ll ngr) { m.erase(m.lower_bound(okl), m.lower_bound(ngr)); } template <class T> void erase(set<T> &m, ll okl, ll ngr) { m.erase(m.lower_bound(okl), m.lower_bound(ngr)); } template <typename T> void erasen(vector<T> &v, unsigned ll s, unsigned ll n) { v.erase(v.begin() + s, v.begin() + s + n); } template <typename T, typename U> void insert(vector<T> &v, unsigned ll i, U t) { v.insert(v.begin() + i, t); } template <typename T, typename U> void push_front(vector<T> &v, U t) { v.insert(v.begin(), t); } template <typename T, typename U> void insert(vector<T> &v, unsigned ll i, vector<T> list) { for (auto &&va : list) v.insert(v.begin() + i++, va); } template <typename T> void insert(set<T> &v, vector<T> list) { for (auto &&va : list) v.insert(va); } vector<string> split(const string a, const char deli) { string b = a + deli; ll l = 0, r = 0, n = b.size(); vector<string> res; rep(i, n) { if (b[i] == deli) { r = i; if (l < r) res.push_back(b.substr(l, r - l)); l = i + 1; } } return res; } vector<string> split(const string a, const string deli) { vector<string> res; ll kn = sz(deli); std::string::size_type Pos(a.find(deli)); ll l = 0; while (Pos != std::string::npos) { if (Pos - l) res.push_back(a.substr(l, Pos - l)); l = Pos + kn; Pos = a.find(deli, Pos + kn); } if (sz(a) - l) res.push_back(a.substr(l, sz(a) - l)); return res; } void yn(bool a) { if (a) cout << "yes" << endl; else cout << "no" << endl; } void Yn(bool a) { if (a) cout << "Yes" << endl; else cout << "No" << endl; } void YN(bool a) { if (a) cout << "YES" << endl; else cout << "NO" << endl; } void fyn(bool a) { if (a) cout << "yes" << endl; else cout << "no" << endl; exit(0); } void fYn(bool a) { if (a) cout << "Yes" << endl; else cout << "No" << endl; exit(0); } void fYN(bool a) { if (a) cout << "YES" << endl; else cout << "NO" << endl; exit(0); } void Possible(bool a) { if (a) cout << "Possible" << endl; else cout << "Impossible" << endl; exit(0); } void POSSIBLE(bool a) { if (a) cout << "POSSIBLE" << endl; else cout << "IMPOSSIBLE" << endl; exit(0); } //@formatter:off template <typename T> T minv(T a, T m); template <typename T> T minv(T a); template <typename T> class Modular { public: using Type = typename decay<decltype(T::value)>::type; constexpr Modular() : value() {} template <typename U> Modular(const U &x) { value = normalize(x); } template <typename U> static Type normalize(const U &x) { Type v; if (-mod() <= x && x < mod()) v = static_cast<Type>(x); else v = static_cast<Type>(x % mod()); if (v < 0) v += mod(); return v; } const Type &operator()() const { return value; } template <typename U> explicit operator U() const { return static_cast<U>(value); } constexpr static Type mod() { return T::value; } Modular &operator+=(const Modular &other) { if ((value += other.value) >= mod()) value -= mod(); return *this; } Modular &operator-=(const Modular &other) { if ((value -= other.value) < 0) value += mod(); return *this; } template <typename U> Modular &operator+=(const U &other) { return *this += Modular(other); } template <typename U> Modular &operator-=(const U &other) { return *this -= Modular(other); } Modular &operator++() { return *this += 1; } Modular &operator--() { return *this -= 1; } Modular operator++(signed) { Modular result(*this); *this += 1; return result; } Modular operator--(signed) { Modular result(*this); *this -= 1; return result; } Modular operator-() const { return Modular(-value); } template <typename U = T> typename enable_if<is_same<typename Modular<U>::Type, signed>::value, Modular>::type & operator*=(const Modular &rhs) { #ifdef _WIN32 uint64_t x = static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value); uint32_t xh = static_cast<uint32_t>(x >> 32), xl = static_cast<uint32_t>(x), d, m; asm("divl %4; \n\t" : "=a"(d), "=d"(m) : "d"(xh), "a"(xl), "r"(mod())); value = m; #else value = normalize(static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value)); #endif return *this; } template <typename U = T> typename enable_if<is_same<typename Modular<U>::Type, int64_t>::value, Modular>::type & operator*=(const Modular &rhs) { int64_t q = static_cast<int64_t>(static_cast<double>(value) * rhs.value / mod()); value = normalize(value * rhs.value - q * mod()); return *this; } template <typename U = T> typename enable_if<!is_integral<typename Modular<U>::Type>::value, Modular>::type & operator*=(const Modular &rhs) { value = normalize(value * rhs.value); return *this; } Modular &operator/=(const Modular &other) { return *this *= Modular(minv(other.value)); } template <typename U> friend bool operator==(const Modular<U> &lhs, const Modular<U> &rhs); template <typename U> friend bool operator<(const Modular<U> &lhs, const Modular<U> &rhs); template <typename U> friend std::istream &operator>>(std::istream &stream, Modular<U> &number); operator int() { return value; } private: Type value; }; template <typename T> bool operator==(const Modular<T> &lhs, const Modular<T> &rhs) { return lhs.value == rhs.value; } template <typename T, typename U> bool operator==(const Modular<T> &lhs, U rhs) { return lhs == Modular<T>(rhs); } template <typename T, typename U> bool operator==(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) == rhs; } template <typename T> bool operator!=(const Modular<T> &lhs, const Modular<T> &rhs) { return !(lhs == rhs); } template <typename T, typename U> bool operator!=(const Modular<T> &lhs, U rhs) { return !(lhs == rhs); } template <typename T, typename U> bool operator!=(U lhs, const Modular<T> &rhs) { return !(lhs == rhs); } template <typename T> bool operator<(const Modular<T> &lhs, const Modular<T> &rhs) { return lhs.value < rhs.value; } template <typename T> Modular<T> operator+(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) += rhs; } template <typename T, typename U> Modular<T> operator+(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) += rhs; } template <typename T, typename U> Modular<T> operator+(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) += rhs; } template <typename T> Modular<T> operator-(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) -= rhs; } template <typename T, typename U> Modular<T> operator-(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) -= rhs; } template <typename T, typename U> Modular<T> operator-(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) -= rhs; } template <typename T> Modular<T> operator*(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) *= rhs; } template <typename T, typename U> Modular<T> operator*(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) *= rhs; } template <typename T, typename U> Modular<T> operator*(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) *= rhs; } template <typename T> Modular<T> operator/(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) /= rhs; } template <typename T, typename U> Modular<T> operator/(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) /= rhs; } template <typename T, typename U> Modular<T> operator/(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) /= rhs; } constexpr signed MOD = // 998244353; 1e9 + 7; // MOD using mint = Modular<std::integral_constant<decay<decltype(MOD)>::type, MOD>>; constexpr int mint_len = 1400001; vi fac, finv, inv; vi p2; mint com(int n, int r) { if (r < 0 || r > n) return 0; return mint(finv[r] * fac[n] % MOD * finv[n - r]); } mint pom(int n, int r) { /* if (!sz(fac)) com(0, -1);*/ if (r < 0 || r > n) return 0; return mint(fac[n] * finv[n - 1]); } mint npr(int n, int r) { /* if (!sz(fac)) com(0, -1);*/ if (r < 0 || r > n) return 0; return mint(fac[n] * finv[n - r]); } int nprin(int n, int r) { /* if (!sz(fac)) com(0, -1);*/ if (r < 0 || r > n) return 0; return fac[n] * finv[n - r] % MOD; } int icom(int n, int r) { const int NUM_ = 1400001; static ll fac[NUM_ + 1], finv[NUM_ + 1], inv[NUM_ + 1]; if (fac[0] == 0) { inv[1] = fac[0] = finv[0] = 1; for (int i = 2; i <= NUM_; ++i) inv[i] = inv[MOD % i] * (MOD - MOD / i) % MOD; for (int i = 1; i <= NUM_; ++i) fac[i] = fac[i - 1] * i % MOD, finv[i] = finv[i - 1] * inv[i] % MOD; } if (r < 0 || r > n) return 0; return ((finv[r] * fac[n] % MOD) * finv[n - r]) % MOD; } #define ncr com #define ncri icom // n個の場所にr個の物を置く mint nhr(int n, int r) { return com(n + r - 1, r); } mint hom(int n, int r) { return com(n + r - 1, r); } int nhri(int n, int r) { return icom(n + r - 1, r); } template <typename T> T minv(T a, T m) { T u = 0, v = 1; while (a != 0) { T t = m / a; m -= t * a; swap(a, m); u -= t * v; swap(u, v); } assert(m == 1); return u; } template <typename T> T minv(T a) { if (a < mint_len) return inv[a]; T u = 0, v = 1; T m = MOD; while (a != 0) { T t = m / a; m -= t * a; swap(a, m); u -= t * v; swap(u, v); } assert(m == 1); return u; } template <typename T, typename U> Modular<T> mpow(const Modular<T> &a, const U &b) { assert(b >= 0); int x = a(), res = 1; U p = b; while (p > 0) { if (p & 1) (res *= x) %= MOD; (x *= x) %= MOD; p >>= 1; } return res; } template <typename T, typename U, typename V> mint mpow(const T a, const U b, const V m = MOD) { assert(b >= 0); int x = a, res = 1; U p = b; while (p > 0) { if (p & 1) (res *= x) %= m; (x *= x) %= m; p >>= 1; } return res; } template <typename T, typename U> mint mpow(const T a, const U b) { assert(b >= 0); int x = a, res = 1; U p = b; while (p > 0) { if (p & 1) (res *= x) %= MOD; (x *= x) %= MOD; p >>= 1; } return res; } template <typename T, typename U, typename V> int mpowi(const T &a, const U &b, const V &m = MOD) { assert(b >= 0); int x = a, res = 1; U p = b; while (p > 0) { if (p & 1) (res *= x) %= m; (x *= x) %= m; p >>= 1; } return res; } template <typename T> string to_string(const Modular<T> &number) { return to_string(number()); } string yuri(const mint &a) { stringstream st; rep(i, 300) { rep(j, 300) { if ((mint)i / j == a) { st << i << " / " << j; return st.str(); } } } rep(i, 1000) { rep(j, 1000) { if ((mint)i / j == a) { st << i << " / " << j; return st.str(); } } } return st.str(); } template <typename T> std::ostream &operator<<(std::ostream &stream, const Modular<T> &number) { stream << number(); #ifdef _DEBUG // stream << " -> " << yuri(number); #endif return stream; } //@formatter:off template <typename T> std::istream &operator>>(std::istream &stream, Modular<T> &number) { typename common_type<typename Modular<T>::Type, int64_t>::type x; stream >> x; number.value = Modular<T>::normalize(x); return stream; } using PM = pair<mint, mint>; using vm = vector<mint>; using mapm = map<int, mint>; using umapm = umap<int, mint>; #define vvm(...) \ o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(mint, __VA_ARGS__) #define vnm(name, ...) auto name = make_v<mint>(__VA_ARGS__) struct setmod { setmod() { // p2.resize(mint_len);p2[0] = 1; for (int i = 1; i < mint_len; ++i) // p2[i] = p2[i - 1] * 2 % MOD; fac.resize(mint_len); finv.resize(mint_len); inv.resize(mint_len); inv[1] = fac[0] = finv[0] = 1; for (int i = 2; i < mint_len; ++i) inv[i] = inv[MOD % i] * (MOD - MOD / i) % MOD; for (int i = 1; i < mint_len; ++i) fac[i] = fac[i - 1] * i % MOD, finv[i] = finv[i - 1] * inv[i] % MOD; } } setmodv; //@formatter:on // nhr n個の場所にr個の物を分ける //@起動時 struct initon { initon() { cin.tie(0); ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(16); srand((unsigned)clock() + (unsigned)time(NULL)); }; } initonv; //@formatter:on // gra mll pr // 上下左右 const string udlr = "udlr"; string UDLR = "UDLR"; // x4と連動 UDLR.find('U') := x4[0] // 右、上が正 constexpr ll y4[] = {1, -1, 0, 0}; constexpr ll x4[] = {0, 0, -1, 1}; constexpr ll y8[] = {0, 1, 0, -1, -1, 1, 1, -1}; constexpr ll x8[] = {1, 0, -1, 0, 1, -1, 1, -1}; ll n, m, k, d, H, W, x, y, z, q; ll cou; vi a, b, c; // vvi (s, 0, 0); vvc(ba, 0, 0); vp p; str s; void solve() { in(n); na(a, n); int dum = 0; rep(i, n) { dum = a[i] * 2 - dum; } dum /= 2; cout << dum << " "; rep(i, n - 1) { dum = a[i] * 2 - dum; cout << dum << " "; } cout << "" << endl; } auto my(ll n, vi &a) { return 0; } auto sister(ll n, vi &a) { ll ret = 0; return ret; } signed main() { solve(); #define arg n, a #ifdef _DEBUG bool bad = 0; for (ll i = 0, ok = 1; i < k5 && ok; ++i) { ll n = rand(1, 8); vi a = ranv(n, 1, 10); auto myres = my(arg); auto res = sister(arg); ok = myres == res; if (!ok) { out(arg); cerr << "AC : " << res << endl; cerr << "MY : " << myres << endl; bad = 1; break; } } if (!bad) { // cout << "完璧 : solveを書き直そう" << endl; // cout << " : そして、solve()を呼び出すのだ" << endl; // cout << " : cin>>n; na(a,n);も忘れるな" << endl; } #endif return 0; };
replace
3,543
3,550
3,543
3,548
-11
p02984
C++
Runtime Error
#include <algorithm> #include <cstdlib> #include <iostream> #include <math.h> #include <stdio.h> #include <string> #include <vector> using namespace std; int main() { int n; long long int a[10000], s = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; if (i % 2 == 0) s += a[i]; else s -= a[i]; } cout << s; s = s / 2; for (int i = 0; i < n - 1; i++) { s = a[i] - s; cout << " " << 2 * s; } cout << endl; }
#include <algorithm> #include <cstdlib> #include <iostream> #include <math.h> #include <stdio.h> #include <string> #include <vector> using namespace std; int main() { int n; long long int a[100002], s = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; if (i % 2 == 0) s += a[i]; else s -= a[i]; } cout << s; s = s / 2; for (int i = 0; i < n - 1; i++) { s = a[i] - s; cout << " " << 2 * s; } cout << endl; }
replace
11
12
11
12
0
p02984
C++
Runtime Error
#include <iostream> using namespace std; int main() { int N, A[100000]; int i, j; long S = 0, ans; long long sum[200000]; for (i = 0; i < 200000; i++) sum[i] = 0; cin >> N; for (i = 0; i < N; i++) cin >> A[i]; for (i = 0; i < 2 * N; i++) { sum[i + 1] = sum[i] + A[(2 * i + 1) % N]; } S = sum[N] / 2; for (i = 0; i < N; i++) { ans = S - sum[i * (N / 2 + 1) % N + N / 2] + sum[i * (N / 2 + 1) % N]; cout << ans * 2 << endl; } }
#include <iostream> using namespace std; int main() { int N, A[100000]; long i, j; long S = 0, ans; long long sum[200000]; for (i = 0; i < 200000; i++) sum[i] = 0; cin >> N; for (i = 0; i < N; i++) cin >> A[i]; for (i = 0; i < 2 * N; i++) { sum[i + 1] = sum[i] + A[(2 * i + 1) % N]; } S = sum[N] / 2; for (i = 0; i < N; i++) { ans = S - sum[i * (N / 2 + 1) % N + N / 2] + sum[i * (N / 2 + 1) % N]; cout << ans * 2 << endl; } }
replace
4
5
4
5
0
p02984
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <math.h> #include <string> #include <utility> #include <vector> #define lli long long #define all(i) i.begin(), i.end() #define rall(i) i.rbegin(), i.rend() #define rep0(i, j) for (int i = 0; i < j; i++) #define rep1(i, j) for (int i = 1; i <= j; i++) #define rep0d(i, j) for (int i = j - 1; i >= 0; i--) #define MAX 1000000007 int a[2001][2001]; using namespace std; int main() { lli n; cin >> n; vector<lli> vec(n), ans(n + 1); rep0(i, n) cin >> vec[i]; lli temp; ans[0] = temp = min(vec[0], vec[n - 1]) / 2; while (1) { int i = 0; for (; i < n && ans[i] >= 0; i++) { ans[i + 1] = vec[i] - ans[i]; } temp /= 2; if (ans[0] == ans[n]) break; if (ans[0] > ans[n] || i % 2 == 1) ans[0] = ans[0] + temp; else ans[0] = ans[0] - temp + 1; } rep0(i, n) cout << ans[i] * 2 << '\n'; }
#include <algorithm> #include <iostream> #include <math.h> #include <string> #include <utility> #include <vector> #define lli long long #define all(i) i.begin(), i.end() #define rall(i) i.rbegin(), i.rend() #define rep0(i, j) for (int i = 0; i < j; i++) #define rep1(i, j) for (int i = 1; i <= j; i++) #define rep0d(i, j) for (int i = j - 1; i >= 0; i--) #define MAX 1000000007 int a[2001][2001]; using namespace std; int main() { lli n; cin >> n; vector<lli> vec(n), ans(n + 1); rep0(i, n) cin >> vec[i]; lli temp; ans[0] = temp = min(vec[0], vec[n - 1]) / 2; int i = 0; for (; i < n; i++) { ans[i + 1] = vec[i] - ans[i]; } ans[0] = (ans[0] + ans[n]) / 2; for (int i = 0; i < n; i++) { ans[i + 1] = vec[i] - ans[i]; } rep0(i, n) cout << ans[i] * 2 << '\n'; }
replace
24
36
24
33
TLE
p02984
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { int N; vector<int> A; cin >> N; for (int i = 0; i < N; i++) { int a; cin >> a; A.push_back(a); } for (int i = 0; i < N; i++) { int a = 0; int p = 1; for (int j = 0; j < N; j++) { a += A[i + j < N ? i + j : i + j - N] * p; p *= -1; } cout << a << endl; } }
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { int N; vector<int> A; cin >> N; for (int i = 0; i < N; i++) { int a; cin >> a; A.push_back(a); } int a = 0; int p = 1; for (int j = 0; j < N; j++) { a += A[j] * p; p *= -1; } cout << a << endl; for (int j = 0; j < N - 1; j++) { a = 2 * (A[j] - a / 2); cout << a << endl; } }
replace
16
23
16
26
TLE
p02984
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; typedef long long ll; int main(void) { int n; cin >> n; int dam[n + 1]; for (int i = 1; i <= n; i++) { int x; cin >> x; dam[i] = x; } int ans[n + 1]; for (int i = 1; i <= n; i++) { ans[i] = 0; } vector<int> num; int c = 0; c = dam[1]; while (c) { num.push_back(2 * c); c--; } int sum = 0; for (int i = 1; i <= n; i++) { sum += dam[i]; } int first = n - 1; while (first) { sum -= 2 * dam[first]; first -= 2; } ans[1] = sum; for (int i = 1; i < n; i++) { ans[i + 1] = dam[i] * 2 - ans[i]; } for (int i = 1; i <= n; i++) { cout << ans[i] << " "; } return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; typedef long long ll; int main(void) { int n; cin >> n; int dam[n + 1]; for (int i = 1; i <= n; i++) { int x; cin >> x; dam[i] = x; } int ans[n + 1]; for (int i = 1; i <= n; i++) { ans[i] = 0; } int sum = 0; for (int i = 1; i <= n; i++) { sum += dam[i]; } int first = n - 1; while (first) { sum -= 2 * dam[first]; first -= 2; } ans[1] = sum; for (int i = 1; i < n; i++) { ans[i + 1] = dam[i] * 2 - ans[i]; } for (int i = 1; i <= n; i++) { cout << ans[i] << " "; } return 0; }
replace
18
25
18
19
TLE
p02984
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #ifdef LOCAL #include "../../dump.hpp" #else #define dump(...) #endif #define int ll #define rep(i, n) for (int i = 0, i##_cond = (n); i < i##_cond; ++i) #define FOR(i, a, b) for (int i = (a), i##_cond = (b); i < i##_cond; ++i) #define ROF(i, a, b) \ for (int i = (a)-1, i##_cond = (b); i >= i##_cond; --i) // ROF(i,n,0) #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() // sortで降順 #define pb push_back #define eb emplace_back #define fst first #define snd second using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vs = vector<string>; using pii = pair<int, int>; constexpr ll inf = 1ll << 61; constexpr ll mod = 1e9 + 7; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <class T> T add(const T &a, const T &b) { return (a + b) % mod; } signed main() { int n; cin >> n; vi a(n); rep(i, n) cin >> a[i]; int sum = accumulate(all(a), 0); vi ans(n); for (int i = 0; i < n - 1; i += 2) { sum -= 2 * a[i]; } dump(sum); ans[n - 1] = sum; ROF(i, n - 1, 0) { ans[i] = 2 * (a[i] - ans[i + 1] / 2); } assert(ans[0] + ans[n - 1] == a[n - 1] * 2); dump(ans); cout << ans[0]; FOR(i, 1, n) cout << " " << ans[i]; cout << endl; }
#include <bits/stdc++.h> using namespace std; #ifdef LOCAL #include "../../dump.hpp" #else #define dump(...) #endif #define int ll #define rep(i, n) for (int i = 0, i##_cond = (n); i < i##_cond; ++i) #define FOR(i, a, b) for (int i = (a), i##_cond = (b); i < i##_cond; ++i) #define ROF(i, a, b) \ for (int i = (a)-1, i##_cond = (b); i >= i##_cond; --i) // ROF(i,n,0) #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() // sortで降順 #define pb push_back #define eb emplace_back #define fst first #define snd second using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vs = vector<string>; using pii = pair<int, int>; constexpr ll inf = 1ll << 61; constexpr ll mod = 1e9 + 7; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <class T> T add(const T &a, const T &b) { return (a + b) % mod; } signed main() { int n; cin >> n; vi a(n); rep(i, n) cin >> a[i]; int sum = accumulate(all(a), 0ll); vi ans(n); for (int i = 0; i < n - 1; i += 2) { sum -= 2 * a[i]; } dump(sum); ans[n - 1] = sum; ROF(i, n - 1, 0) { ans[i] = 2 * (a[i] - ans[i + 1] / 2); } assert(ans[0] + ans[n - 1] == a[n - 1] * 2); dump(ans); cout << ans[0]; FOR(i, 1, n) cout << " " << ans[i]; cout << endl; }
replace
41
42
41
42
0
p02984
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define pb push_back const int maxn = 2e3 + 5; const ll p = 998244353; ll a[maxn], b[maxn]; int main() { ios::sync_with_stdio(false); int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } ll sum = 0, f = 1; for (int i = 1; i <= n; i++) { sum += f * a[i]; f *= -1; } b[1] = sum; for (int i = 2; i <= n; i++) { b[i] = 2 * a[i - 1] - b[i - 1]; } for (int i = 1; i <= n; i++) { cout << b[i] << " "; } }
#include <bits/stdc++.h> using namespace std; #define ll long long #define pb push_back const int maxn = 2e6 + 5; const ll p = 998244353; ll a[maxn], b[maxn]; int main() { ios::sync_with_stdio(false); int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } ll sum = 0, f = 1; for (int i = 1; i <= n; i++) { sum += f * a[i]; f *= -1; } b[1] = sum; for (int i = 2; i <= n; i++) { b[i] = 2 * a[i - 1] - b[i - 1]; } for (int i = 1; i <= n; i++) { cout << b[i] << " "; } }
replace
4
5
4
5
0
p02984
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define all(a) a.begin(), a.end() using namespace std; const int nax = 1e5; int d[nax]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; for (int i = 0; i < n; ++i) { cin >> d[i]; } for (int i = 0; i < n; ++i) { long long ai = 0; for (int c = 0, p = i; c < n; ++c) { if (c & 1) ai -= d[p]; else ai += d[p]; ++p; p %= n; } cout << ai << " "; } cout << "\n"; return 0; }
#include <bits/stdc++.h> #define all(a) a.begin(), a.end() using namespace std; const int nax = 1e5; int d[nax]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; for (int i = 0; i < n; ++i) { cin >> d[i]; } long long ai = 0; for (int c = 0; c < n; ++c) { if (c & 1) ai -= d[c]; else ai += d[c]; } cout << ai << " "; for (int i = 1; i < n; ++i) { cout << 2 * d[i - 1] - ai << " "; ai = 2 * d[i - 1] - ai; } cout << "\n"; return 0; }
replace
16
27
16
27
TLE
p02984
C++
Time Limit Exceeded
// https://atcoder.jp/contests/abc133/tasks/abc133_d #include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <map> #include <numeric> #include <set> #include <string> #include <tuple> #include <vector> using namespace std; typedef long long ll; typedef pair<int, int> PII; typedef pair<ll, ll> PLL; #define INF (1e9) #define INFL (1e18) #define MOD (1000000007) #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) for (int i = 0; i < (n); ++i) #define REPR(i, n) for (int i = n; i >= 0; i--) #define FOREACH(x, a) for (auto &(x) : (a)) #define ALL(obj) (obj).begin(), (obj).end() #define ALLR(obj) (obj).rbegin(), (obj).rend() #define LEN(x) (sizeof(x) / sizeof(*(x))) // ll gcd(long a, long b) { return b ? gcd(b, a % b) : a; } // ll lcm(long a, long b) { return a * b / gcd(a, b); } int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<ll> a(n), b(n, 0); for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i <= a[0] * 2; i++) { b[0] = i; for (int j = 1; j < n; j++) { b[j] = a[j - 1] * 2 - b[j - 1]; } if (b[0] + b[n - 1] == a[n - 1] * 2) { break; } } for (int i = 0; i < n; i++) { cout << b[i]; if (i < n - 1) { cout << " "; } } cout << endl; return 0; }
// https://atcoder.jp/contests/abc133/tasks/abc133_d #include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <map> #include <numeric> #include <set> #include <string> #include <tuple> #include <vector> using namespace std; typedef long long ll; typedef pair<int, int> PII; typedef pair<ll, ll> PLL; #define INF (1e9) #define INFL (1e18) #define MOD (1000000007) #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) for (int i = 0; i < (n); ++i) #define REPR(i, n) for (int i = n; i >= 0; i--) #define FOREACH(x, a) for (auto &(x) : (a)) #define ALL(obj) (obj).begin(), (obj).end() #define ALLR(obj) (obj).rbegin(), (obj).rend() #define LEN(x) (sizeof(x) / sizeof(*(x))) // ll gcd(long a, long b) { return b ? gcd(b, a % b) : a; } // ll lcm(long a, long b) { return a * b / gcd(a, b); } int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<ll> a(n), b(n, 0); for (int i = 0; i < n; i++) { cin >> a[i]; } b[0] = 0; for (int j = 1; j < n; j++) { b[j] = a[j - 1] * 2 - b[j - 1]; } ll t1 = (a[n - 1] * 2); ll t2 = (b[0] + b[n - 1]); b[0] = (t1 - t2) / 2; for (int j = 1; j < n; j++) { b[j] = a[j - 1] * 2 - b[j - 1]; } for (int i = 0; i < n; i++) { cout << b[i]; if (i < n - 1) { cout << " "; } } cout << endl; return 0; }
replace
39
47
39
49
TLE
p02984
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { int n; cin >> n; vector<ll> a(n); for (auto &e : a) cin >> e; vector<ll> x(n); ll l = 0, r = 1e9 / 2 + 1, m; while (true) { m = (l + r) / 2; x[0] = 2 * m; for (int i = 0; i + 1 < n; i++) { x[i + 1] = 2 * (a[i] - x[i] / 2); } if (x[0] / 2 + x[n - 1] / 2 < a[n - 1]) { l = m + 1; } else if (x[0] / 2 + x[n - 1] / 2 > a[n - 1]) { r = m; } else { break; } } for (auto e : x) cout << e << ' '; cout << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { int n; cin >> n; vector<ll> a(n); for (auto &e : a) cin >> e; vector<ll> x(n); ll l = 0, r = 1e9 + 1, m; while (true) { m = (l + r) / 2; x[0] = 2 * m; for (int i = 0; i + 1 < n; i++) { x[i + 1] = 2 * (a[i] - x[i] / 2); } if (x[0] / 2 + x[n - 1] / 2 < a[n - 1]) { l = m + 1; } else if (x[0] / 2 + x[n - 1] / 2 > a[n - 1]) { r = m; } else { break; } } for (auto e : x) cout << e << ' '; cout << endl; }
replace
12
13
12
13
TLE
p02984
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; long long int A[100001]; long long int W[100001]; for (int i = 0; i < N; i++) { cin >> A[i]; } for (int i = 0; i < N; i++) { W[i] = A[i]; for (int j = 1; j < N; j++) { if (j % 2 == 0) W[i] += A[(i + j) % N]; if (j % 2 == 1) W[i] -= A[(i + j) % N]; } } for (int i = 0; i < N; i++) { cout << W[i] << " "; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; long long int A[100001]; long long int W[100001]; for (int i = 0; i < N; i++) { cin >> A[i]; } W[0] = A[0]; for (int i = 1; i < N; i++) { if (i % 2 == 0) W[0] += A[i]; if (i % 2 == 1) W[0] -= A[i]; } for (int i = 1; i < N; i++) { W[i] = 2 * A[i - 1] - W[i - 1]; } for (int i = 0; i < N; i++) { cout << W[i] << " "; } return 0; }
replace
12
20
12
21
TLE
p02984
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <string> #include <utility> #include <vector> using namespace std; typedef long long ll; typedef vector<ll> vl; typedef vector<vector<ll>> vvl; typedef pair<ll, ll> P; #define rep(i, n) for (ll i = 0; i < n; i++) #define exrep(i, a, b) for (ll i = a; i <= b; i++) #define out(x) cout << x << endl #define exout(x) printf("%.10f\n", x) #define chmax(x, y) x = max(x, y) #define chmin(x, y) x = min(x, y) #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define pb push_back #define re0 return 0 const ll mod = 1000000007; const ll INF = 1e16; const ll MAX_N = 100010; int main() { ll n; cin >> n; vl a(n); rep(i, n) { cin >> a[i]; } vl b(n); rep(i, n) { if (i % 2 == 0) { b[0] += a[i]; } else { b[0] -= a[i]; } } rep(i, n) { b[i + 1] = 2 * a[i] - b[i]; } rep(i, n) { cout << b[i] << " "; } cout << endl; re0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <string> #include <utility> #include <vector> using namespace std; typedef long long ll; typedef vector<ll> vl; typedef vector<vector<ll>> vvl; typedef pair<ll, ll> P; #define rep(i, n) for (ll i = 0; i < n; i++) #define exrep(i, a, b) for (ll i = a; i <= b; i++) #define out(x) cout << x << endl #define exout(x) printf("%.10f\n", x) #define chmax(x, y) x = max(x, y) #define chmin(x, y) x = min(x, y) #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define pb push_back #define re0 return 0 const ll mod = 1000000007; const ll INF = 1e16; const ll MAX_N = 100010; int main() { ll n; cin >> n; vl a(n); rep(i, n) { cin >> a[i]; } vl b(n); rep(i, n) { if (i % 2 == 0) { b[0] += a[i]; } else { b[0] -= a[i]; } } rep(i, n - 1) { b[i + 1] = 2 * a[i] - b[i]; } rep(i, n) { cout << b[i] << " "; } cout << endl; re0; }
replace
47
48
47
48
-6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)
p02984
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using pii = pair<int, int>; using ll = long long; #define fi first #define se second #define pb push_back #define rep(i, n) for (int i = 0; i < (int)(n); i++) template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } int n; ll calc(vector<ll> a, ll p) { ll temp = p; rep(i, n) { // cout << "i:" << i << " temp:" << temp << " a[i]:" << a[i] << endl; if (a[i] < temp && i % 2 == 0) return -1; else if (a[i] < temp && i % 2 == 1) return -2; else temp = a[i] - temp; } // cout << "p:" << p << " temp:" << temp << endl; if (p == temp) return 1; /*else if (p < temp) { cout << "p:" << p << " temp:" << temp << endl; return -1; }*/ /*else if (p > temp) { cout << "p:" << p << " temp:" << temp << endl; return -2; }*/ } ll binary_search(vector<ll> a, ll max) { ll left = 0, right = max; while (right >= left) { int mid = (right + left) / 2; // cout << "left:" << left << " right: " << right << " mid:" << mid << endl; // cout << "calc: " << calc(a,mid) << endl; ll temp = calc(a, mid); if (temp == 1) return mid; else if (temp == -1) right = mid - 1; else if (temp == -2) left = mid + 1; } } int main() { cin >> n; vector<ll> a(n); ll max = -1e18; rep(i, n) { cin >> a[i]; chmax(max, a[i]); } vector<ll> ans(n); ans[0] = binary_search(a, max); for (int i = 1; i < n; i++) { ans[i] = a[i - 1] - ans[i - 1]; } rep(i, n) { cout << ans[i] * 2; if (i != n - 1) cout << " "; else cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; using pii = pair<int, int>; using ll = long long; #define fi first #define se second #define pb push_back #define rep(i, n) for (int i = 0; i < (int)(n); i++) template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } int n; ll calc(vector<ll> a, ll p) { ll temp = p; rep(i, n) { // cout << "i:" << i << " temp:" << temp << " a[i]:" << a[i] << endl; if (a[i] < temp && i % 2 == 0) return -1; else if (a[i] < temp && i % 2 == 1) return -2; else temp = a[i] - temp; } // cout << "p:" << p << " temp:" << temp << endl; if (p == temp) return 1; else if (p < temp) { // cout << "p:" << p << " temp:" << temp << endl; return -2; } else if (p > temp) { // cout << "p:" << p << " temp:" << temp << endl; return -1; } } ll binary_search(vector<ll> a, ll max) { ll left = 0, right = max; while (right >= left) { int mid = (right + left) / 2; // cout << "left:" << left << " right: " << right << " mid:" << mid << endl; // cout << "calc: " << calc(a,mid) << endl; ll temp = calc(a, mid); if (temp == 1) return mid; else if (temp == -1) right = mid - 1; else if (temp == -2) left = mid + 1; } } int main() { cin >> n; vector<ll> a(n); ll max = -1e18; rep(i, n) { cin >> a[i]; chmax(max, a[i]); } vector<ll> ans(n); ans[0] = binary_search(a, max); for (int i = 1; i < n; i++) { ans[i] = a[i - 1] - ans[i - 1]; } rep(i, n) { cout << ans[i] * 2; if (i != n - 1) cout << " "; else cout << endl; } return 0; }
replace
41
49
41
48
TLE
p02984
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); ++i) #define reps(i, n) for (int i = 1; i <= (n); ++i) #define all(x) (x).begin(), (x).end() #define Fixed fixed << setprecision(12) #define int int_fast64_t using pii = pair<int, int>; constexpr int INF = 0x3f3f3f3f; constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL; constexpr int mod1 = 1e9 + 7; constexpr int mod2 = 998244353; template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); } template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); } template <class T> using min_heap = priority_queue<T, vector<T>, greater<T>>; template <class T> using max_heap = priority_queue<T>; template <class A, class B> using umap = unordered_map<A, B>; inline int updiv(int a, int b) { return (a + b - 1) / b; } constexpr int dx[] = {1, 0, -1, 0, 1, 1, -1, -1}; constexpr int dy[] = {0, -1, 0, 1, 1, -1, -1, 1}; signed main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout.setf(ios_base::fixed); cout.precision(12); int n; cin >> n; vector<int> a(n); rep(i, n) { cin >> a[i]; } auto Isok = [&](int key) { int now = key; auto x = a; x[0] -= key / 2; rep(i, n) { x[(i + 1) % n] -= now / 2; now = x[(i + 1) % n] * 2; x[(i + 1) % n] -= now / 2; /* rep(j,n){ cout << x[j] << ' '; } cout << now << '\n'; */ if (x[(i + 1) % n] < 0) { return false; } } if (now < 0) return false; return (true); }; int low = -1, high = a[1] + 1; while (high - low > 1) { int mid = (high + low) / 2; if (Isok(mid * 2)) low = mid; else high = mid; } vector<int> res(n); int now = low * 2; a[0] -= now / 2; res[0] = now; rep(i, n) { a[(i + 1) % n] -= now / 2; now = a[(i + 1) % n] * 2; res[i + 1] = now; a[(i + 1) % n] -= now / 2; } cout << res.back(); for (int i = 0; i < n - 1; ++i) { cout << ' ' << res[i]; } cout << '\n'; return (0); }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); ++i) #define reps(i, n) for (int i = 1; i <= (n); ++i) #define all(x) (x).begin(), (x).end() #define Fixed fixed << setprecision(12) #define int int_fast64_t using pii = pair<int, int>; constexpr int INF = 0x3f3f3f3f; constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL; constexpr int mod1 = 1e9 + 7; constexpr int mod2 = 998244353; template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); } template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); } template <class T> using min_heap = priority_queue<T, vector<T>, greater<T>>; template <class T> using max_heap = priority_queue<T>; template <class A, class B> using umap = unordered_map<A, B>; inline int updiv(int a, int b) { return (a + b - 1) / b; } constexpr int dx[] = {1, 0, -1, 0, 1, 1, -1, -1}; constexpr int dy[] = {0, -1, 0, 1, 1, -1, -1, 1}; signed main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout.setf(ios_base::fixed); cout.precision(12); int n; cin >> n; vector<int> a(n); rep(i, n) { cin >> a[i]; } auto Isok = [&](int key) { int now = key; auto x = a; x[0] -= key / 2; rep(i, n) { x[(i + 1) % n] -= now / 2; now = x[(i + 1) % n] * 2; x[(i + 1) % n] -= now / 2; /* rep(j,n){ cout << x[j] << ' '; } cout << now << '\n'; */ if (x[(i + 1) % n] < 0) { return false; } } if (now < 0) return false; return (true); }; int low = -1, high = a[1] + 1; while (high - low > 1) { int mid = (high + low) / 2; if (Isok(mid * 2)) low = mid; else high = mid; } vector<int> res(n); int now = low * 2; a[0] -= now / 2; res[0] = now; rep(i, n) { a[(i + 1) % n] -= now / 2; now = a[(i + 1) % n] * 2; if (i != n - 1) { res[i + 1] = now; } a[(i + 1) % n] -= now / 2; } cout << res.back(); for (int i = 0; i < n - 1; ++i) { cout << ' ' << res[i]; } cout << '\n'; return (0); }
replace
86
87
86
89
0
p02984
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <iostream> #include <numeric> #include <string> #include <vector> using namespace std; int N; int A[100000]; int ans[100000]; int sum; int main() { cin >> N; for (int i = 0; i < N; i++) { cin >> A[i]; } for (int j = 0; j < N; j++) { ans[j] = 0; for (int i = 0; i < N; i++) { ans[j] += A[(i + j) % N] * pow(-1, i % 2); } } for (int i = 0; i < N; i++) { cout << ans[i] << " "; } return 0; }
#include <algorithm> #include <cmath> #include <iostream> #include <numeric> #include <string> #include <vector> using namespace std; int N; int A[100000]; int ans[100000]; int sum; int main() { cin >> N; for (int i = 0; i < N; i++) { cin >> A[i]; } // 1回目だけ別処理 ans[0] = 0; for (int i = 0; i < N; i++) { ans[0] += A[(i) % N] * pow(-1, i % 2); } // 2回目以降 for (int i = 1; i < N; i++) { ans[i] += 2 * A[i - 1] - ans[i - 1]; } for (int i = 0; i < N; i++) { cout << ans[i] << " "; } return 0; }
replace
17
22
17
25
TLE
p02984
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; using vec = vector<ll>; using mat = vector<vec>; #define INF (1LL << 60) #define MOD 1000000007 #define PI 3.14159265358979323846 #define REP(i, m, n) for (ll(i) = (m), (i_len) = (n); (i) < (i_len); ++(i)) #define FORR(i, v) for (auto(i) : v) #define ALL(x) (x).begin(), (x).end() #define PR(x) cout << (x) << endl #define PS(x) cout << (x) << " " #define SZ(x) ((ll)(x).size()) #define MAX(a, b) (((a) > (b)) ? (a) : (b)) #define MIN(a, b) (((a) < (b)) ? (a) : (b)) #define ASC(x) sort(ALL((x))) #define DESC(x) sort(ALL((x)), greater<ll>()) #define pb push_back int main() { ll N; cin >> N; vec A(N + 1, 0); REP(i, 1, N + 1) cin >> A[i]; vec B(N + 1, 0), C((N + 1) / 2, 0), D((N - 1) / 2, 0); ll S = 0; for (ll i = 1; i <= N; ++i) S += A[i]; S /= 2; for (ll i = 1; i <= (N + 1) / 2; ++i) C[i] = C[i - 1] + A[2 * i - 1]; for (ll i = 1; i <= (N - 1) / 2; ++i) D[i] = D[i - 1] + A[2 * i]; for (ll i = 1; i <= (N + 1) / 2; ++i) B[2 * i - 1] = S - C[i - 1] - D[(N - 1) / 2] + D[i - 1]; for (ll i = 1; i <= (N - 1) / 2; ++i) B[2 * i] = S - D[i - 1] - C[(N + 1) / 2] + C[i]; for (ll i = 1; i <= N; ++i) PR(B[i] * 2); return 0; } /* */
#include <bits/stdc++.h> using namespace std; using ll = long long; using vec = vector<ll>; using mat = vector<vec>; #define INF (1LL << 60) #define MOD 1000000007 #define PI 3.14159265358979323846 #define REP(i, m, n) for (ll(i) = (m), (i_len) = (n); (i) < (i_len); ++(i)) #define FORR(i, v) for (auto(i) : v) #define ALL(x) (x).begin(), (x).end() #define PR(x) cout << (x) << endl #define PS(x) cout << (x) << " " #define SZ(x) ((ll)(x).size()) #define MAX(a, b) (((a) > (b)) ? (a) : (b)) #define MIN(a, b) (((a) < (b)) ? (a) : (b)) #define ASC(x) sort(ALL((x))) #define DESC(x) sort(ALL((x)), greater<ll>()) #define pb push_back int main() { ll N; cin >> N; vec A(N + 1, 0); REP(i, 1, N + 1) cin >> A[i]; vec B(N + 1, 0), C((N + 1) / 2 + 1, 0), D((N - 1) / 2 + 1, 0); ll S = 0; for (ll i = 1; i <= N; ++i) S += A[i]; S /= 2; for (ll i = 1; i <= (N + 1) / 2; ++i) C[i] = C[i - 1] + A[2 * i - 1]; for (ll i = 1; i <= (N - 1) / 2; ++i) D[i] = D[i - 1] + A[2 * i]; for (ll i = 1; i <= (N + 1) / 2; ++i) B[2 * i - 1] = S - C[i - 1] - D[(N - 1) / 2] + D[i - 1]; for (ll i = 1; i <= (N - 1) / 2; ++i) B[2 * i] = S - D[i - 1] - C[(N + 1) / 2] + C[i]; for (ll i = 1; i <= N; ++i) PR(B[i] * 2); return 0; } /* */
replace
29
30
29
30
0
p02984
C++
Time Limit Exceeded
#include <algorithm> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <vector> using namespace std; int n; int t[100002]; int main() { while (cin >> n) { for (int i = 0; i < n; i++) { cin >> t[i]; } // 2x int xMin = 0, xMax = 1000000001; while (true) { int x = (xMin + xMax) / 2; int status = 0; int preX = x; for (int i = 0; i < n; i++) { if (preX > t[i]) { if (i % 2 == 0) { status = 1; break; } else { status = -1; break; } } preX = t[i] - preX; } if (status == 0 && preX == x) { xMin = x; xMax = x + 1; break; } if (status == 1 || preX < x) { xMax = x; } else { xMin = x + 1; } } cout << xMin * 2; int preX = t[0] - xMin; for (int i = 0; i < n - 1; i++) { cout << " " << preX * 2; preX = t[i + 1] - preX; } cout << endl; } }
#include <algorithm> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <vector> using namespace std; int n; int t[100002]; int main() { while (cin >> n) { for (int i = 0; i < n; i++) { cin >> t[i]; } // 2x int xMin = 0, xMax = 1000000001; while (true) { int x = (xMin + xMax) / 2; int status = 0; int preX = x; for (int i = 0; i < n; i++) { if (preX > t[i]) { if (i % 2 == 0) { status = 1; break; } else { status = -1; break; } } preX = t[i] - preX; } if (status == 0 && preX == x) { xMin = x; xMax = x + 1; break; } if (status == 1 || (status == 0 && preX < x)) { xMax = x; } else { xMin = x + 1; } } cout << xMin * 2; int preX = t[0] - xMin; for (int i = 0; i < n - 1; i++) { cout << " " << preX * 2; preX = t[i + 1] - preX; } cout << endl; } }
replace
51
52
51
52
TLE
p02984
C++
Time Limit Exceeded
#include <iostream> #include <numeric> #include <vector> int main() { std::size_t n; std::cin >> n; std::vector<size_t> a(n, 0); for (auto &&ai : a) std::cin >> ai; const auto sum = std::accumulate(a.begin(), a.end(), size_t(0)) / 2; std::vector<size_t> x(n, sum); for (size_t i = 0; i < n; ++i) { size_t k = i + 1; for (size_t j = 0; j < (n - 1) / 2; ++j) { if (k == n) k = 0; else if (k == n + 1) k = 1; x[i] -= a[k]; k = k + 2; } } for (auto &&xi : x) std::cout << 2 * xi << ' '; std::cout << std::flush; }
#include <iostream> #include <numeric> #include <vector> int main() { std::size_t n; std::cin >> n; std::vector<size_t> a(n, 0); for (auto &&ai : a) std::cin >> ai; const auto sum = std::accumulate(a.begin(), a.end(), size_t(0)) / 2; std::vector<size_t> x(n, 0); x[0] = sum; for (size_t i = 1; i < n; i = i + 2) x[0] -= a[i]; for (size_t i = 1; i < n; ++i) x[i] = a[i - 1] - x[i - 1]; for (auto &&xi : x) std::cout << 2 * xi << ' '; std::cout << std::flush; }
replace
13
26
13
19
TLE
p02984
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i) #define REPS(i, s, n) for (int i = (s), i##_len = (n); i < i##_len; ++i) #define ALL(obj) (obj).begin(), (obj).end() typedef unsigned int uint; typedef unsigned long long int ull; typedef long long int ll; typedef pair<int, int> P; #define LINF ((ll)1 << 63 - 1) #define INF 2000000007 #define MINF -2000000007 #define MAX 100005 const int MOD = 1e9 + 7; // テンプレ終了 //====================================================================// // int main() { int N; cin >> N; vector<int> A(N); REP(i, N) cin >> A[i]; vector<int> x(N, 0); REP(i, N) { REP(j, N) { x[i] += (j % 2 ? -A[(j + i) % N] : A[(j + i) % N]); } } cout << x[0]; REPS(i, 1, N) cout << " " << x[i]; cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i) #define REPS(i, s, n) for (int i = (s), i##_len = (n); i < i##_len; ++i) #define ALL(obj) (obj).begin(), (obj).end() typedef unsigned int uint; typedef unsigned long long int ull; typedef long long int ll; typedef pair<int, int> P; #define LINF ((ll)1 << 63 - 1) #define INF 2000000007 #define MINF -2000000007 #define MAX 100005 const int MOD = 1e9 + 7; // テンプレ終了 //====================================================================// // int main() { int N; cin >> N; vector<int> A(N); REP(i, N) cin >> A[i]; int x = 0; REP(j, N) { x += (j % 2 ? -A[j] : A[j]); } cout << x; REP(i, N - 1) cout << " " << (x = 2 * A[i] - x); cout << endl; return 0; }
replace
31
37
31
35
TLE
p02984
C++
Runtime Error
#include <algorithm> #include <iostream> #include <vector> typedef long long ll; using namespace std; vector<ll> A(10000, 0); int main() { int N; cin >> N; for (int i = 0; i < N; i++) cin >> A[i]; ll B = 0; for (int i = 0; i < N; i++) { if (i % 2 == 0) { B += A[i]; } else { B -= A[i]; } } for (int i = 0; i < N; i++) { cout << B << " "; B = 2 * A[i] - B; } cout << "\n"; }
#include <algorithm> #include <iostream> #include <vector> typedef long long ll; using namespace std; vector<ll> A(100000, 0); int main() { int N; cin >> N; for (int i = 0; i < N; i++) cin >> A[i]; ll B = 0; for (int i = 0; i < N; i++) { if (i % 2 == 0) { B += A[i]; } else { B -= A[i]; } } for (int i = 0; i < N; i++) { cout << B << " "; B = 2 * A[i] - B; } cout << "\n"; }
replace
6
7
6
7
0
p02984
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long int main() { int n; cin >> n; vector<ll> r(n, 0); vector<ll> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; r[0] += (i & 1 ? -a[i] : a[i]); } for (int i = 0; i < n; i++) { r[i + 1] = 2 * (a[i] - r[i] / 2); } for (int i = 0; i < n; i++) cout << r[i] << " "; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long int main() { int n; cin >> n; vector<ll> r(n, 0); vector<ll> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; r[0] += (i & 1 ? -a[i] : a[i]); } for (int i = 0; i < n - 1; i++) { r[i + 1] = 2 * (a[i] - r[i] / 2); } for (int i = 0; i < n; i++) cout << r[i] << " "; return 0; }
replace
12
13
12
13
-11
p02984
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define int long long typedef long long ll; using namespace std; const ll MAXN = 200000; ll N, flag = 0, tmp = 0, mid = 0; vector<ll> A(MAXN, 0); signed main() { cin >> N; for (int i = 0; i < N; i++) cin >> A[i]; ll ng = 0; ll ok = 1000000000; while (1) { // ll ngnum = ng*2; // ll oknum = ok*2; // cout << "ng : " << ng << " ok : " << ok << endl; // ll mid = (ng+ok)/2; // ll tmp = (ng+ok)/2; mid = (ng + ok) / 2; tmp = (ng + ok) / 2; for (int i = 0; i < N - 1; i++) { tmp = A[i] - tmp; } if (tmp + mid == A[N - 1]) break; else if (tmp + mid > A[N - 1]) { ok = mid; } else { ng = mid; } // cout << " mid : " << mid << " tmp : " << tmp << " A : " << A[N-1] << // endl; cout << endl; } tmp = mid; for (int i = 0; i < N; i++) { if (i) cout << " "; cout << 2 * tmp; tmp = A[i] - tmp; } return 0; }
#include <bits/stdc++.h> #define int long long typedef long long ll; using namespace std; const ll MAXN = 200000; ll N, flag = 0, tmp = 0, mid = 0; vector<ll> A(MAXN, 0); signed main() { cin >> N; for (int i = 0; i < N; i++) cin >> A[i]; ll ng = -1; ll ok = 1000000001; while (1) { // ll ngnum = ng*2; // ll oknum = ok*2; // cout << "ng : " << ng << " ok : " << ok << endl; // ll mid = (ng+ok)/2; // ll tmp = (ng+ok)/2; mid = (ng + ok) / 2; tmp = (ng + ok) / 2; for (int i = 0; i < N - 1; i++) { tmp = A[i] - tmp; } if (tmp + mid == A[N - 1]) break; else if (tmp + mid > A[N - 1]) { ok = mid; } else { ng = mid; } // cout << " mid : " << mid << " tmp : " << tmp << " A : " << A[N-1] << // endl; cout << endl; } tmp = mid; for (int i = 0; i < N; i++) { if (i) cout << " "; cout << 2 * tmp; tmp = A[i] - tmp; } return 0; }
replace
14
16
14
16
TLE
p02984
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int N; cin >> N; long long a[1010], m[1010]; long long su = 0; for (int i = 0; i < N; i++) { cin >> a[i]; su += a[i]; } m[0] = 0; for (int i = 0; i < 2 * N; i += 2) { m[(i + 2) % N] = 2 * (a[(i + 1) % N] - a[i % N]) + m[i % N]; su -= m[(i + 2) % N]; } su /= N; for (int i = 0; i < N; i++) { cout << su + m[i] << " "; } }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int N; cin >> N; long long a[100010], m[100010]; long long su = 0; for (int i = 0; i < N; i++) { cin >> a[i]; su += a[i]; } m[0] = 0; for (int i = 0; i < 2 * N; i += 2) { m[(i + 2) % N] = 2 * (a[(i + 1) % N] - a[i % N]) + m[i % N]; su -= m[(i + 2) % N]; } su /= N; for (int i = 0; i < N; i++) { cout << su + m[i] << " "; } }
replace
9
10
9
10
0
p02984
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll INF = 1LL << 60; int main() { ll N; cin >> N; vector<ll> A(N); ll sum = 0; vector<ll> s(N, 0), ans(N, 0); for (int i = 0; i < N; i++) { cin >> A[i]; sum += A[i]; } for (int i = 0; i < N; i++) { ll tmp = sum; for (int j = 0; j < N; j++) { if (j == 0 && (N - i - 1) % 2) continue; if (i != j && i != j + 1) { tmp -= (2 * A[j]); j++; } } cout << tmp << endl; } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll INF = 1LL << 60; int main() { ll N; cin >> N; vector<ll> A(N); ll sum = 0; vector<ll> s(N, 0), ans(N, 0); for (int i = 0; i < N; i++) { cin >> A[i]; sum += A[i]; } ll tmp = sum; for (int i = 1; i < N; i += 2) { tmp -= (2 * A[i]); } ans[0] = tmp; cout << ans[0] << endl; for (int i = 0; i < N - 1; i++) { ans[i + 1] = 2 * A[i] - ans[i]; cout << ans[i + 1] << endl; } }
replace
16
27
16
25
TLE
p02984
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <map> #include <queue> #include <string> #include <vector> using namespace std; static const uint64_t MOD = 1000000007LL; /* 10^9 + 7 */ uint64_t dp[100005][13]; /* * (X1 + X2) / 2 == A1; * (X2 + X3) / 2 == A2; * (X3 + X1) / 2 == A3; * .... */ int main() { int n; cin >> n; vector<long> a(n + 1); vector<long> x(n + 1); for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = 1; i <= n; i++) { long tmp = 0; for (int j = 1; j <= n; j++) { int k = i + j - 1; if (k > n) k -= n; if (j % 2 == 1) tmp += a[k]; else tmp -= a[k]; } x[i] = tmp; } for (int i = 1; i <= n; i++) { cout << x[i] << " "; } cout << endl; }
#include <algorithm> #include <iostream> #include <map> #include <queue> #include <string> #include <vector> using namespace std; static const uint64_t MOD = 1000000007LL; /* 10^9 + 7 */ uint64_t dp[100005][13]; /* * (X1 + X2) / 2 == A1; * (X2 + X3) / 2 == A2; * (X3 + X1) / 2 == A3; * .... */ int main() { int n; cin >> n; vector<long> a(n + 1); vector<long> x(n + 1); for (int i = 1; i <= n; i++) { cin >> a[i]; } long tmp = 0; for (int j = 1; j <= n; j++) { if (j % 2 == 1) tmp += a[j]; else tmp -= a[j]; } x[1] = tmp; for (int i = 2; i <= n; i++) { x[i] = 2 * a[i - 1] - x[i - 1]; } for (int i = 1; i <= n; i++) { cout << x[i] << " "; } cout << endl; }
replace
25
37
25
35
TLE
p02984
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<ll, ll>; using vi = vector<ll>; using vvi = vector<vi>; using vb = vector<bool>; using vs = vector<string>; using vp = vector<pii>; #define pb push_back #define mp make_pair #define fi first #define se second #define sz(x) (ll)((x).size()) #define all(x) (x).begin(), (x).end() #define each(a, x) for (auto &&a : (x)) #define _overload3(_1, _2, _3, name, ...) name #define rep1(n) rep2(_, n) #define rep2(i, n) rep3(i, 0, n) #define rep3(i, a, n) for (ll i = (a); i < (n); ++i) #define rep(...) _overload3(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__) const int INF = 1e9; const int MOD = 1e9 + 7; const double EPS = 1e-9; const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1}; const int dy[] = {0, 1, 0, -1, 1, -1, 1, -1}; template <class T> inline void print(const T &x) { cout << x << "\n"; } template <class T> inline bool chmax(T &a, const T &b) { if (a < b) a = b; return a < b; } template <class T> inline bool chmin(T &a, const T &b) { if (a > b) a = b; return a > b; } struct Init { Init() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(10); } } init; int main() { ll n; cin >> n; vi a(n); ll sall = 0; ll sodd = 0; rep(i, n) { cin >> a[i]; sall += a[i]; if (i & 1) sodd += a[i]; } vi x(n); x[0] = sall - 2 * sodd; rep(i, n) x[i + 1] = 2 * a[i] - x[i]; each(a, x) cout << a << " "; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<ll, ll>; using vi = vector<ll>; using vvi = vector<vi>; using vb = vector<bool>; using vs = vector<string>; using vp = vector<pii>; #define pb push_back #define mp make_pair #define fi first #define se second #define sz(x) (ll)((x).size()) #define all(x) (x).begin(), (x).end() #define each(a, x) for (auto &&a : (x)) #define _overload3(_1, _2, _3, name, ...) name #define rep1(n) rep2(_, n) #define rep2(i, n) rep3(i, 0, n) #define rep3(i, a, n) for (ll i = (a); i < (n); ++i) #define rep(...) _overload3(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__) const int INF = 1e9; const int MOD = 1e9 + 7; const double EPS = 1e-9; const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1}; const int dy[] = {0, 1, 0, -1, 1, -1, 1, -1}; template <class T> inline void print(const T &x) { cout << x << "\n"; } template <class T> inline bool chmax(T &a, const T &b) { if (a < b) a = b; return a < b; } template <class T> inline bool chmin(T &a, const T &b) { if (a > b) a = b; return a > b; } struct Init { Init() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(10); } } init; int main() { ll n; cin >> n; vi a(n); ll sall = 0; ll sodd = 0; rep(i, n) { cin >> a[i]; sall += a[i]; if (i & 1) sodd += a[i]; } vi x(n); x[0] = sall - 2 * sodd; rep(i, n - 1) x[i + 1] = 2 * a[i] - x[i]; each(a, x) cout << a << " "; }
replace
59
60
59
60
0
p02984
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define For(i, a, b) for (int i = a; i <= b; i++) #define F first #define S second #define all(x) x.begin(), x.end() #define sz(x) ((int)x.size()) #define int ll #define MOD (ll)(1e9 + 7) using namespace std; using ll = long long; using pii = pair<int, int>; int32_t main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> v(n); int s = 0; for (auto &i : v) { cin >> i; s += i; } For(i, 0, n - 1) { int tmp = 0; for (int j = (i + 1) % n; j != i; j = (j + 2) % n) { tmp += 2 * v[j]; } cout << s - tmp << " \n"[i == n - 1]; } return 0; }
#include <bits/stdc++.h> #define For(i, a, b) for (int i = a; i <= b; i++) #define F first #define S second #define all(x) x.begin(), x.end() #define sz(x) ((int)x.size()) #define int ll #define MOD (ll)(1e9 + 7) using namespace std; using ll = long long; using pii = pair<int, int>; int32_t main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> v(n); int s = 0; for (auto &i : v) { cin >> i; s += i; } int tmp = s; for (int j = 1; j != 0; j = (j + 2) % n) { tmp -= 2 * v[j]; } cout << tmp; For(i, 0, n - 2) { cout << " " << (v[i] - tmp / 2) * 2; tmp = (v[i] - tmp / 2) * 2; } return 0; }
replace
24
30
24
32
TLE
p02984
C++
Time Limit Exceeded
#include <algorithm> #include <cctype> #include <cmath> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <regex> #include <set> #include <stack> #include <stdio.h> #include <string> #include <vector> #define rep(i, n) for (int i = 0; i < n; i++) typedef int long long ll; using namespace std; typedef pair<int, int> P; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; const ll MOD = 1e9 + 7; static const int MAX = 100; static const int INF = (1 << 23); int main() { ll n; cin >> n; vector<ll> a(n); rep(i, n) cin >> a[i]; vector<ll> ans; ll t = a[0] + 1; ll u = -1; while (true) { vector<ll> kari; ll mid = (t + u) / 2; ll pre = mid; ll s = mid; ll next = a[0] - pre; bool f = true; bool f2 = true; kari.push_back(mid * 2); for (int i = 0; i < n; i++) { next = a[i] - pre; if (next < 0) { if (i % 2) f2 = false; else f = false; break; } else { kari.push_back(next * 2); pre = next; } } if (next == s) { ans = kari; break; } else if (f == false || f2 == false || next > s) { if (f2 == false || next > s) u = mid; else t = mid; } else { u = mid; } // cout<<t<<u<<endl; } rep(i, ans.size() - 1) { cout << ans[i] << " "; } return 0; }
#include <algorithm> #include <cctype> #include <cmath> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <regex> #include <set> #include <stack> #include <stdio.h> #include <string> #include <vector> #define rep(i, n) for (int i = 0; i < n; i++) typedef int long long ll; using namespace std; typedef pair<int, int> P; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; const ll MOD = 1e9 + 7; static const int MAX = 100; static const int INF = (1 << 23); int main() { ll n; cin >> n; vector<ll> a(n); rep(i, n) cin >> a[i]; vector<ll> ans; ll t = a[0] + 1; ll u = -1; while (true) { vector<ll> kari; ll mid = (t + u) / 2; ll pre = mid; ll s = mid; ll next = a[0] - pre; bool f = true; bool f2 = true; kari.push_back(mid * 2); for (int i = 0; i < n; i++) { next = a[i] - pre; if (next < 0) { if (i % 2) f2 = false; else f = false; break; } else { kari.push_back(next * 2); pre = next; } } if (next == s) { ans = kari; break; } else if (f == false || f2 == false || next > s) { if (f2 == false || next > s) u = mid; else t = mid; } else { t = mid; } // cout<<t<<u<<endl; } rep(i, ans.size() - 1) { cout << ans[i] << " "; } return 0; }
replace
69
70
69
70
TLE
p02984
C++
Runtime Error
#include <iostream> #include <vector> using namespace std; typedef vector<int> VI; typedef long long LL; #define ALL(a) (a).begin(), (a).end() #define PB push_back #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) #define OUT(v) cout << v << endl int main() { int N; cin >> N; VI A; LL S = 0; REP(i, N) { int a; cin >> a; A.PB(a); S += a; } LL x_1 = S; REP(i, (N - 1) / 2) { x_1 -= 2 * A[i * 2 + 1]; } VI X; X.PB(x_1); REP(i, N - 1) { X[i + 1] = 2 * A[i] - X[i]; cout << X[i] << " "; } cout << X[N - 1] << endl; return 0; }
#include <iostream> #include <vector> using namespace std; typedef vector<int> VI; typedef long long LL; #define ALL(a) (a).begin(), (a).end() #define PB push_back #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) #define OUT(v) cout << v << endl int main() { int N; cin >> N; VI A; LL S = 0; REP(i, N) { int a; cin >> a; A.PB(a); S += a; } LL x_1 = S; REP(i, (N - 1) / 2) { x_1 -= 2 * A[2 * i + 1]; } int X[100000]; X[0] = x_1; REP(i, N) X[i + 1] = 2 * A[i] - X[i]; REP(i, N - 1) cout << X[i] << " "; cout << X[N - 1] << endl; return 0; }
replace
26
33
26
33
0
p02984
C++
Runtime Error
#include <algorithm> #include <cstdlib> #include <iostream> #include <math.h> #include <stdio.h> #include <string> #include <vector> using namespace std; int main() { int n; long long int a[10000], s = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; if (i % 2 == 0) s += a[i]; else s -= a[i]; } cout << s; s = s / 2; for (int i = 0; i < n - 1; i++) { s = a[i] - s; cout << " " << 2 * s; } cout << endl; }
#include <algorithm> #include <cstdlib> #include <iostream> #include <math.h> #include <stdio.h> #include <string> #include <vector> using namespace std; int main() { int n; long long int a[100000], s = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; if (i % 2 == 0) s += a[i]; else s -= a[i]; } cout << s; s = s / 2; for (int i = 0; i < n - 1; i++) { s = a[i] - s; cout << " " << 2 * s; } cout << endl; }
replace
11
12
11
12
0
p02984
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) #define sz(x) int(x.size()) using namespace std; typedef long long ll; const int MOD = 1000000007; int main() { int n; ll sum = 0; cin >> n; vector<ll> a(n); vector<ll> ans(n); rep(i, n) { cin >> a[i]; sum += a[i]; } rep(i, n) { a.push_back(a[i]); } rep(i, n) { ans[i] = sum; rep(j, n / 2) { ans[i] -= 2 * a[i + 1 + 2 * j]; } } rep(i, n) { cout << ans[i] << ' '; } cout << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) #define sz(x) int(x.size()) using namespace std; typedef long long ll; const int MOD = 1000000007; int main() { int n; ll sum = 0; cin >> n; vector<ll> a(n); vector<ll> ans(n); rep(i, n) { cin >> a[i]; sum += a[i]; } rep(i, n) { a.push_back(a[i]); } ans[0] = sum; rep(j, n / 2) { ans[0] -= 2 * a[1 + 2 * j]; } rep(i, n - 1) { ans[i + 1] = 2 * a[i] - ans[i]; } rep(i, n) { cout << ans[i] << ' '; } cout << endl; return 0; }
replace
18
22
18
21
TLE
p02986
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; ostream &operator<<(ostream &out, string str) { for (char c : str) cout << c; return out; } template <class L, class R> ostream &operator<<(ostream &out, pair<L, R> p) { return out << "(" << p.first << ", " << p.second << ")"; } template <class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) { out << "{"; for (auto it = a.begin(); it != a.end(); it = next(it)) out << (it != a.begin() ? ", " : "") << *it; return out << "}"; } void dump() { cerr << "\n"; } template <class T, class... Ts> void dump(T a, Ts... x) { cerr << a << ", "; dump(x...); } #ifdef DEBUG #define debug(...) cerr << "[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__) #else #define debug(...) false #endif #define REP(i, n) for (int i = 0; i < n; i++) #define FOR(i, a, b) for (int i = a; i <= b; i++) #define ST first #define ND second template <class T> int size(T &&a) { return a.size(); } using LL = long long; using PII = pair<int, int>; struct edge { int to, col, len; }; vector<vector<edge>> graph; vector<int> par, dist; vector<int> pre, pos; int t = 0; void set_par(int v = 0, int p = -1) { pre[v] = t++; par[v] = p; for (edge &e : graph[v]) { int u = e.to; int d = e.len; if (u != p) { dist[u] = dist[v] + d; set_par(u, v); } } pos[v] = t++; } bool is_par(int v, int u) { return pre[v] <= pre[u] && pos[u] <= pos[v]; } vector<vector<int>> jump; int LCA(int v, int u) { if (is_par(v, u)) return v; if (is_par(u, v)) return u; for (int i = 19; i >= 0; i--) { if (jump[i][v] != -1 && !is_par(jump[i][v], u)) v = jump[i][v]; } return jump[0][v]; } vector<vector<PII>> dists; void dist_dfs(int c, int v = 0) { for (edge &e : graph[v]) { int u = e.to; int d = e.len; int col = e.col; if (u != par[v]) { dists[c][u] = dists[c][v]; if (col == c) dists[c][u].ND++; else dists[c][u].ST += d; dist_dfs(c, u); } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, q; cin >> n >> q; struct just_edge { int a, b, d; }; vector<vector<just_edge>> edges(n - 1); vector<int> color_cnt(n - 1); graph.resize(n); REP(i, n - 1) { int a, b, c, d; cin >> a >> b >> c >> d; a--, b--, c--; color_cnt[c]++; graph[a].emplace_back(edge{b, c, d}); graph[b].emplace_back(edge{a, c, d}); edges[c].emplace_back(just_edge{a, b, d}); } par.resize(n); dist.resize(n); pre.resize(n); pos.resize(n); set_par(); int log = 20; jump.resize(20, vector<int>(n)); jump[0] = par; FOR(i, 1, log - 1) { REP(j, n) jump[i][j] = (jump[i - 1][j] == -1 ? -1 : jump[i - 1][jump[i - 1][j]]); } int S = 0; dists.resize(n - 1); REP(i, n - 1) { if (color_cnt[i] >= S) { dists[i].resize(n); dist_dfs(i); } } auto add = [&](PII a, PII b) -> PII { return {a.ST + b.ST, a.ND + b.ND}; }; auto rem = [&](PII a, PII b) -> PII { return {a.ST - b.ST, a.ND - b.ND}; }; REP(i, q) { int x, y, u, v; cin >> x >> y >> u >> v; x--, u--, v--; if (color_cnt[x] >= S) { int lca = LCA(u, v); PII ret = add(rem(dists[x][u], dists[x][lca]), rem(dists[x][v], dists[x][lca])); cout << ret.ST + ret.ND * y << "\n"; } else { int lca = LCA(u, v); int ret = dist[u] + dist[v] - dist[lca] * 2; for (auto &e : edges[x]) { int a = e.a; int b = e.b; int d = e.d; if (is_par(a, b)) swap(a, b); if (!is_par(a, lca) && (is_par(a, u) || is_par(a, v))) ret += y - d; } cout << ret << "\n"; } } }
#include <bits/stdc++.h> using namespace std; ostream &operator<<(ostream &out, string str) { for (char c : str) cout << c; return out; } template <class L, class R> ostream &operator<<(ostream &out, pair<L, R> p) { return out << "(" << p.first << ", " << p.second << ")"; } template <class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) { out << "{"; for (auto it = a.begin(); it != a.end(); it = next(it)) out << (it != a.begin() ? ", " : "") << *it; return out << "}"; } void dump() { cerr << "\n"; } template <class T, class... Ts> void dump(T a, Ts... x) { cerr << a << ", "; dump(x...); } #ifdef DEBUG #define debug(...) cerr << "[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__) #else #define debug(...) false #endif #define REP(i, n) for (int i = 0; i < n; i++) #define FOR(i, a, b) for (int i = a; i <= b; i++) #define ST first #define ND second template <class T> int size(T &&a) { return a.size(); } using LL = long long; using PII = pair<int, int>; struct edge { int to, col, len; }; vector<vector<edge>> graph; vector<int> par, dist; vector<int> pre, pos; int t = 0; void set_par(int v = 0, int p = -1) { pre[v] = t++; par[v] = p; for (edge &e : graph[v]) { int u = e.to; int d = e.len; if (u != p) { dist[u] = dist[v] + d; set_par(u, v); } } pos[v] = t++; } bool is_par(int v, int u) { return pre[v] <= pre[u] && pos[u] <= pos[v]; } vector<vector<int>> jump; int LCA(int v, int u) { if (is_par(v, u)) return v; if (is_par(u, v)) return u; for (int i = 19; i >= 0; i--) { if (jump[i][v] != -1 && !is_par(jump[i][v], u)) v = jump[i][v]; } return jump[0][v]; } vector<vector<PII>> dists; void dist_dfs(int c, int v = 0) { for (edge &e : graph[v]) { int u = e.to; int d = e.len; int col = e.col; if (u != par[v]) { dists[c][u] = dists[c][v]; if (col == c) dists[c][u].ND++; else dists[c][u].ST += d; dist_dfs(c, u); } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, q; cin >> n >> q; struct just_edge { int a, b, d; }; vector<vector<just_edge>> edges(n - 1); vector<int> color_cnt(n - 1); graph.resize(n); REP(i, n - 1) { int a, b, c, d; cin >> a >> b >> c >> d; a--, b--, c--; color_cnt[c]++; graph[a].emplace_back(edge{b, c, d}); graph[b].emplace_back(edge{a, c, d}); edges[c].emplace_back(just_edge{a, b, d}); } par.resize(n); dist.resize(n); pre.resize(n); pos.resize(n); set_par(); int log = 20; jump.resize(20, vector<int>(n)); jump[0] = par; FOR(i, 1, log - 1) { REP(j, n) jump[i][j] = (jump[i - 1][j] == -1 ? -1 : jump[i - 1][jump[i - 1][j]]); } int S = 320; dists.resize(n - 1); REP(i, n - 1) { if (color_cnt[i] >= S) { dists[i].resize(n); dist_dfs(i); } } auto add = [&](PII a, PII b) -> PII { return {a.ST + b.ST, a.ND + b.ND}; }; auto rem = [&](PII a, PII b) -> PII { return {a.ST - b.ST, a.ND - b.ND}; }; REP(i, q) { int x, y, u, v; cin >> x >> y >> u >> v; x--, u--, v--; if (color_cnt[x] >= S) { int lca = LCA(u, v); PII ret = add(rem(dists[x][u], dists[x][lca]), rem(dists[x][v], dists[x][lca])); cout << ret.ST + ret.ND * y << "\n"; } else { int lca = LCA(u, v); int ret = dist[u] + dist[v] - dist[lca] * 2; for (auto &e : edges[x]) { int a = e.a; int b = e.b; int d = e.d; if (is_par(a, b)) swap(a, b); if (!is_par(a, lca) && (is_par(a, u) || is_par(a, v))) ret += y - d; } cout << ret << "\n"; } } }
replace
139
140
139
140
TLE
p02986
C++
Runtime Error
#include "bits/stdc++.h" using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using vi = vector<int>; using vl = vector<ll>; using vvi = vector<vi>; using vvl = vector<vl>; const ll INF = 1LL << 60; const ll MOD = 1000000007; template <class T> bool chmax(T &a, const T &b) { return (a < b) ? (a = b, 1) : 0; } template <class T> bool chmin(T &a, const T &b) { return (b < a) ? (a = b, 1) : 0; } template <class C> void print(const C &c, std::ostream &os = std::cout) { std::copy(std::begin(c), std::end(c), std::ostream_iterator<typename C::value_type>(os, " ")); os << std::endl; } // 0-indexed bottom up Segment Tree // UNIT is the identity element of operation func template <typename T = int> struct SegmentTree { using F = function<T(T, T)>; int n; vector<T> dat; F func; T UNIT; SegmentTree(int n_, F func_, T UNIT_) : func(func_), UNIT(UNIT_) { n = 1; // full binary tree: num of leaves = n = 2^k >= n_ while (n < n_) n *= 2; dat.assign(2 * n - 1, UNIT); } SegmentTree(vector<T> v_, F func_, T UNIT_) : func(func_), UNIT(UNIT_) { n = 1; while (n < v_.size()) n *= 2; dat.assign(2 * n - 1, UNIT); for (int i = 0; i < v_.size(); ++i) { dat[n - 1 + i] = v_[i]; } for (int i = n - 2; i >= 0; --i) { dat[i] = func(dat[2 * i + 1], dat[2 * i + 2]); } } void update(int k, T a) { // leaves are at index n-1 to 2*n-2 k += n - 1; dat[k] = a; while (k > 0) { // k -> parent node k = (k - 1) / 2; // func(child nodes) dat[k] = func(dat[2 * k + 1], dat[2 * k + 2]); } } // get result of func() in [l, r) T query(int l, int r) { l += n - 1; r += n - 1; T ret = UNIT; while (l < r) { if ((l & 1) == 0) ret = func(ret, dat[l]); if ((r & 1) == 0) ret = func(ret, dat[r - 1]); l = l / 2; r = (r - 1) / 2; } return ret; } }; // get LCA on the tree // constructor: n = size of the graph // addEdge: // build: after adding all edge, build Euler tour and seg tree struct LCA { int n; int k; vector<int> depth; vector<int> eulerTour; vector<int> firstVisit; vector<vector<int>> edges; SegmentTree<pair<int, int>> segTree; static pair<int, int> pairComp(pair<int, int> a, pair<int, int> b) { if (a.first < b.first) return a; else return b; } LCA(int n) : n(n), k(0), depth(2 * n - 1), eulerTour(2 * n - 1), firstVisit(n), edges(n), segTree(2 * n, pairComp, make_pair(numeric_limits<int>::max(), 0)){}; void addEdge(int from, int to) { edges[from].push_back(to); edges[to].push_back(from); } void dfs(int curr = 0, int par = -1, int d = 0) { firstVisit[curr] = k; eulerTour[k] = curr; depth[k++] = d; for (auto &e : edges[curr]) { if (e != par) { dfs(e, curr, d + 1); eulerTour[k] = curr; depth[k++] = d; } } } void build(int root = 0) { dfs(root); for (int i = 0; i < 2 * n - 1; ++i) { segTree.update(i, make_pair(depth[i], eulerTour[i])); } } // return LCA node of l and r int operator()(int l, int r) { int left = firstVisit[l]; int right = firstVisit[r]; if (left > right) swap(left, right); return segTree.query(left, right + 1).second; } int getDepth(int k) { return depth[firstVisit[k]]; } }; int main() { int n, q; cin >> n >> q; LCA lca(n); vector<vector<pair<int, pii>>> edges(n); for (int i = 0; i < n - 1; ++i) { int a, b, c, d; cin >> a >> b >> c >> d; a--, b--; lca.addEdge(a, b); edges[a].push_back({b, {c, d}}); edges[b].push_back({a, {c, d}}); } lca.build(); vl ret(q); struct query { int i, x, y, sign; }; vector<vector<query>> vq(n); for (int i = 0; i < q; ++i) { int x, y, u, v; cin >> x >> y >> u >> v; u--, v--; int l = lca(x, y); vq[u].push_back({i, x, y, 1}); vq[v].push_back({i, x, y, 1}); vq[l].push_back({i, x, y, -2}); } vl len(n, 0), cnt(n, 0); ll tot = 0; function<void(int, int)> dfs = [&](int c, int p) { for (auto &q : vq[c]) { ll cost = tot - len[q.x] + cnt[q.x] * q.y; ret[q.i] += cost * q.sign; } for (auto &e : edges[c]) { if (e.first == p) continue; tot += e.second.second; len[e.second.first] += e.second.second; cnt[e.second.first]++; dfs(e.first, c); tot -= e.second.second; len[e.second.first] -= e.second.second; cnt[e.second.first]--; } }; dfs(0, -1); for (int i = 0; i < q; ++i) { cout << ret[i] << "\n"; } return 0; }
#include "bits/stdc++.h" using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using vi = vector<int>; using vl = vector<ll>; using vvi = vector<vi>; using vvl = vector<vl>; const ll INF = 1LL << 60; const ll MOD = 1000000007; template <class T> bool chmax(T &a, const T &b) { return (a < b) ? (a = b, 1) : 0; } template <class T> bool chmin(T &a, const T &b) { return (b < a) ? (a = b, 1) : 0; } template <class C> void print(const C &c, std::ostream &os = std::cout) { std::copy(std::begin(c), std::end(c), std::ostream_iterator<typename C::value_type>(os, " ")); os << std::endl; } // 0-indexed bottom up Segment Tree // UNIT is the identity element of operation func template <typename T = int> struct SegmentTree { using F = function<T(T, T)>; int n; vector<T> dat; F func; T UNIT; SegmentTree(int n_, F func_, T UNIT_) : func(func_), UNIT(UNIT_) { n = 1; // full binary tree: num of leaves = n = 2^k >= n_ while (n < n_) n *= 2; dat.assign(2 * n - 1, UNIT); } SegmentTree(vector<T> v_, F func_, T UNIT_) : func(func_), UNIT(UNIT_) { n = 1; while (n < v_.size()) n *= 2; dat.assign(2 * n - 1, UNIT); for (int i = 0; i < v_.size(); ++i) { dat[n - 1 + i] = v_[i]; } for (int i = n - 2; i >= 0; --i) { dat[i] = func(dat[2 * i + 1], dat[2 * i + 2]); } } void update(int k, T a) { // leaves are at index n-1 to 2*n-2 k += n - 1; dat[k] = a; while (k > 0) { // k -> parent node k = (k - 1) / 2; // func(child nodes) dat[k] = func(dat[2 * k + 1], dat[2 * k + 2]); } } // get result of func() in [l, r) T query(int l, int r) { l += n - 1; r += n - 1; T ret = UNIT; while (l < r) { if ((l & 1) == 0) ret = func(ret, dat[l]); if ((r & 1) == 0) ret = func(ret, dat[r - 1]); l = l / 2; r = (r - 1) / 2; } return ret; } }; // get LCA on the tree // constructor: n = size of the graph // addEdge: // build: after adding all edge, build Euler tour and seg tree struct LCA { int n; int k; vector<int> depth; vector<int> eulerTour; vector<int> firstVisit; vector<vector<int>> edges; SegmentTree<pair<int, int>> segTree; static pair<int, int> pairComp(pair<int, int> a, pair<int, int> b) { if (a.first < b.first) return a; else return b; } LCA(int n) : n(n), k(0), depth(2 * n - 1), eulerTour(2 * n - 1), firstVisit(n), edges(n), segTree(2 * n, pairComp, make_pair(numeric_limits<int>::max(), 0)){}; void addEdge(int from, int to) { edges[from].push_back(to); edges[to].push_back(from); } void dfs(int curr = 0, int par = -1, int d = 0) { firstVisit[curr] = k; eulerTour[k] = curr; depth[k++] = d; for (auto &e : edges[curr]) { if (e != par) { dfs(e, curr, d + 1); eulerTour[k] = curr; depth[k++] = d; } } } void build(int root = 0) { dfs(root); for (int i = 0; i < 2 * n - 1; ++i) { segTree.update(i, make_pair(depth[i], eulerTour[i])); } } // return LCA node of l and r int operator()(int l, int r) { int left = firstVisit[l]; int right = firstVisit[r]; if (left > right) swap(left, right); return segTree.query(left, right + 1).second; } int getDepth(int k) { return depth[firstVisit[k]]; } }; int main() { int n, q; cin >> n >> q; LCA lca(n); vector<vector<pair<int, pii>>> edges(n); for (int i = 0; i < n - 1; ++i) { int a, b, c, d; cin >> a >> b >> c >> d; a--, b--; lca.addEdge(a, b); edges[a].push_back({b, {c, d}}); edges[b].push_back({a, {c, d}}); } lca.build(); vl ret(q); struct query { int i, x, y, sign; }; vector<vector<query>> vq(n); for (int i = 0; i < q; ++i) { int x, y, u, v; cin >> x >> y >> u >> v; u--, v--; int l = lca(u, v); vq[u].push_back({i, x, y, 1}); vq[v].push_back({i, x, y, 1}); vq[l].push_back({i, x, y, -2}); } vl len(n, 0), cnt(n, 0); ll tot = 0; function<void(int, int)> dfs = [&](int c, int p) { for (auto &q : vq[c]) { ll cost = tot - len[q.x] + cnt[q.x] * q.y; ret[q.i] += cost * q.sign; } for (auto &e : edges[c]) { if (e.first == p) continue; tot += e.second.second; len[e.second.first] += e.second.second; cnt[e.second.first]++; dfs(e.first, c); tot -= e.second.second; len[e.second.first] -= e.second.second; cnt[e.second.first]--; } }; dfs(0, -1); for (int i = 0; i < q; ++i) { cout << ret[i] << "\n"; } return 0; }
replace
157
158
157
158
0
p02986
C++
Runtime Error
#pragma region #include <bits/stdc++.h> using namespace std; /* #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; */ #define int long long #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define RREP(i, n) for (int i = (int)n - 1; i >= 0; i--) #define FOR(i, s, n) for (int i = s; i < (int)n; i++) #define RFOR(i, s, n) for (int i = (int)n - 1; i >= s; i--) #define ALL(a) a.begin(), a.end() #define IN(a, x, b) (a <= x && x < b) 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; } template <class T> istream &operator>>(istream &is, vector<T> &vec) { for (T &x : vec) is >> x; return is; } template <class T> inline void in(T &t) { cin >> t; } template <class T, class... Ts> inline void in(T &t, Ts &...ts) { cin >> t; in(ts...); } template <class T> inline void out(T t) { cout << t << "\n"; } template <class T, class... Ts> inline void out(T t, Ts... ts) { cout << t << " "; out(ts...); } template <typename T = int> vector<T> mv(size_t a) { return vector<T>(a); } template <typename T = int, typename... Ts> auto mv(size_t a, Ts... ts) { return vector<decltype(mv<T>(ts...))>(a, mv<T>(ts...)); } template <typename T, typename V> typename enable_if<is_class<T>::value == 0>::type fill(T &t, const V &v) { t = v; } template <typename T, typename V> typename enable_if<is_class<T>::value != 0>::type fill(T &t, const V &v) { for (auto &e : t) fill(e, v); } constexpr long long INF = 1e18; template <int MOD> struct Fp { long long val; constexpr Fp(long long v = 0) noexcept : val(v % MOD) { if (val < 0) val += MOD; } constexpr int getmod() { return MOD; } constexpr Fp operator-() const noexcept { return val ? MOD - val : 0; } constexpr Fp operator+(const Fp &r) const noexcept { return Fp(*this) += r; } constexpr Fp operator-(const Fp &r) const noexcept { return Fp(*this) -= r; } constexpr Fp operator*(const Fp &r) const noexcept { return Fp(*this) *= r; } constexpr Fp operator/(const Fp &r) const noexcept { return Fp(*this) /= r; } constexpr Fp &operator+=(const Fp &r) noexcept { val += r.val; if (val >= MOD) val -= MOD; return *this; } constexpr Fp &operator-=(const Fp &r) noexcept { val -= r.val; if (val < 0) val += MOD; return *this; } constexpr Fp &operator*=(const Fp &r) noexcept { val = val * r.val % MOD; return *this; } constexpr Fp &operator/=(const Fp &r) noexcept { long long a = r.val, b = MOD, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } val = val * u % MOD; if (val < 0) val += MOD; return *this; } constexpr bool operator==(const Fp &r) const noexcept { return this->val == r.val; } constexpr bool operator!=(const Fp &r) const noexcept { return this->val != r.val; } friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept { return os << x.val; } friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept { if (n == 0) return 1; auto t = modpow(a, n / 2); t = t * t; if (n & 1) t = t * a; return t; } }; // 二項係数ライブラリ template <class T> struct BiCoef { vector<T> fact_, inv_, finv_; constexpr BiCoef() {} constexpr BiCoef(int n) noexcept : fact_(n, 1), inv_(n, 1), finv_(n, 1) { init(n); } constexpr void init(int n) noexcept { fact_.assign(n, 1), inv_.assign(n, 1), finv_.assign(n, 1); int MOD = fact_[0].getmod(); for (int i = 2; i < n; i++) { fact_[i] = fact_[i - 1] * i; inv_[i] = -inv_[MOD % i] * (MOD / i); finv_[i] = finv_[i - 1] * inv_[i]; } } constexpr T com(int n, int k) const noexcept { if (n < k || n < 0 || k < 0) return 0; return fact_[n] * finv_[k] * finv_[n - k]; } constexpr T fact(int n) const noexcept { if (n < 0) return 0; return fact_[n]; } constexpr T inv(int n) const noexcept { if (n < 0) return 0; return inv_[n]; } constexpr T finv(int n) const noexcept { if (n < 0) return 0; return finv_[n]; } }; // const int MOD = 1000000007; // const int MOD = 998244353; // using mint = Fp<MOD>; // BiCoef<mint> bc; // bc.init(500050); #pragma endregion template <typename T> struct edge { int src, to; T cost; edge(int to, T cost) : src(-1), to(to), cost(cost) {} edge(int src, int to, T cost) : src(src), to(to), cost(cost) {} edge &operator=(const int &x) { to = x; return *this; } operator int() const { return to; } }; template <typename T> using Edges = vector<edge<T>>; template <typename T> using WeightedGraph = vector<Edges<T>>; using UnWeightedGraph = vector<vector<int>>; template <typename T> using Matrix = vector<vector<T>>; template <typename G> struct DoublingLowestCommonAncestor { const int LOG; vector<int> dep; const G &g; vector<vector<int>> table; DoublingLowestCommonAncestor(const G &g) : g(g), dep(g.size()), LOG(32 - __builtin_clz(g.size())) { table.assign(LOG, vector<int>(g.size(), -1)); } void dfs(int idx, int par, int d) { table[0][idx] = par; dep[idx] = d; for (auto &to : g[idx]) { if (to != par) dfs(to, idx, d + 1); } } void build() { dfs(0, -1, 0); // ここを書き換えれば親を選択できる for (int k = 0; k + 1 < LOG; k++) { for (int i = 0; i < table[k].size(); i++) { if (table[k][i] == -1) table[k + 1][i] = -1; else table[k + 1][i] = table[k][table[k][i]]; } } } int query(int u, int v) { if (dep[u] > dep[v]) swap(u, v); for (int i = LOG - 1; i >= 0; i--) { if (((dep[v] - dep[u]) >> i) & 1) v = table[i][v]; } if (u == v) return u; for (int i = LOG - 1; i >= 0; i--) { if (table[i][u] != table[i][v]) { u = table[i][u]; v = table[i][v]; } } return table[0][u]; } }; struct SegmentTree { int n; pair<int, int> ti = pair<int, int>(0, 0); vector<map<int, pair<int, int>>> dat; SegmentTree(){}; void init(int n_) { n = 1; while (n < n_) n <<= 1; dat.resize(n << 1); } void build(int N) { int n_ = N; init(n_); } pair<int, int> f(pair<int, int> a, pair<int, int> b) { return {a.first + b.first, a.second + b.second}; } void set_val(int k, pair<int, int> x, int col) { dat[k += n][col] = x; while (k >>= 1) dat[k][col] = f(dat[(k << 1) | 0][col], dat[(k << 1) | 1][col]); } pair<int, int> query(int a, int b, int col) { pair<int, int> vl = ti, vr = ti; for (int l = a + n, r = b + n; l < r; l >>= 1, r >>= 1) { if (l & 1) vl = f(vl, dat[l++][col]); if (r & 1) vr = f(dat[--r][col], vr); } return f(vl, vr); } }; // auto f = [](){}; // auto seg = SegmentTree<T,decltype(f)>(f,Ti); signed main() { int N, Q; in(N, Q); WeightedGraph<int> g(N); auto c = mv(N - 1), d = c; REP(i, N - 1) { int a, b; in(a, b, c[i], d[i]); --a; --b; --c[i]; g[a].emplace_back(b, i); g[b].emplace_back(a, i); } DoublingLowestCommonAncestor<WeightedGraph<int>> lca(g); lca.build(); auto ds = mv(N), us = ds; fill(ds, -1); fill(us, -1); SegmentTree seg; seg.build(N); int idx = 0; vector<int> sum; sum.push_back(0); auto dfs = [&](auto &&f, int now, int par) -> void { for (auto e : g[now]) { if (e == par) continue; ds[e] = idx; seg.set_val(idx, {d[e.cost], 1}, c[e.cost]); sum.push_back(d[e.cost]); ++idx; f(f, e, now); us[e] = idx; seg.set_val(idx, {-d[e.cost], -1}, c[e.cost]); sum.push_back(-d[e.cost]); ++idx; } }; dfs(dfs, 0, -1); REP(i, sum.size() - 1) sum[i + 1] += sum[i]; vector<int> ans; while (Q--) { int x, y, a, b; in(x, y, a, b); x--; a--; b--; int p = lca.query(a, b); int ret = 0; auto f = [&](int child) { int l = ds[p] + 1, r = ds[child] + 1; int res = sum[r] - sum[l]; auto p = seg.query(l, r, x); res -= p.first; res += p.second * y; return res; }; ret += f(a); ret += f(b); ans.push_back(ret); } REP(i, ans.size()) { out(ans[i]); } }
#pragma region #include <bits/stdc++.h> using namespace std; /* #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; */ #define int long long #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define RREP(i, n) for (int i = (int)n - 1; i >= 0; i--) #define FOR(i, s, n) for (int i = s; i < (int)n; i++) #define RFOR(i, s, n) for (int i = (int)n - 1; i >= s; i--) #define ALL(a) a.begin(), a.end() #define IN(a, x, b) (a <= x && x < b) 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; } template <class T> istream &operator>>(istream &is, vector<T> &vec) { for (T &x : vec) is >> x; return is; } template <class T> inline void in(T &t) { cin >> t; } template <class T, class... Ts> inline void in(T &t, Ts &...ts) { cin >> t; in(ts...); } template <class T> inline void out(T t) { cout << t << "\n"; } template <class T, class... Ts> inline void out(T t, Ts... ts) { cout << t << " "; out(ts...); } template <typename T = int> vector<T> mv(size_t a) { return vector<T>(a); } template <typename T = int, typename... Ts> auto mv(size_t a, Ts... ts) { return vector<decltype(mv<T>(ts...))>(a, mv<T>(ts...)); } template <typename T, typename V> typename enable_if<is_class<T>::value == 0>::type fill(T &t, const V &v) { t = v; } template <typename T, typename V> typename enable_if<is_class<T>::value != 0>::type fill(T &t, const V &v) { for (auto &e : t) fill(e, v); } constexpr long long INF = 1e18; template <int MOD> struct Fp { long long val; constexpr Fp(long long v = 0) noexcept : val(v % MOD) { if (val < 0) val += MOD; } constexpr int getmod() { return MOD; } constexpr Fp operator-() const noexcept { return val ? MOD - val : 0; } constexpr Fp operator+(const Fp &r) const noexcept { return Fp(*this) += r; } constexpr Fp operator-(const Fp &r) const noexcept { return Fp(*this) -= r; } constexpr Fp operator*(const Fp &r) const noexcept { return Fp(*this) *= r; } constexpr Fp operator/(const Fp &r) const noexcept { return Fp(*this) /= r; } constexpr Fp &operator+=(const Fp &r) noexcept { val += r.val; if (val >= MOD) val -= MOD; return *this; } constexpr Fp &operator-=(const Fp &r) noexcept { val -= r.val; if (val < 0) val += MOD; return *this; } constexpr Fp &operator*=(const Fp &r) noexcept { val = val * r.val % MOD; return *this; } constexpr Fp &operator/=(const Fp &r) noexcept { long long a = r.val, b = MOD, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } val = val * u % MOD; if (val < 0) val += MOD; return *this; } constexpr bool operator==(const Fp &r) const noexcept { return this->val == r.val; } constexpr bool operator!=(const Fp &r) const noexcept { return this->val != r.val; } friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept { return os << x.val; } friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept { if (n == 0) return 1; auto t = modpow(a, n / 2); t = t * t; if (n & 1) t = t * a; return t; } }; // 二項係数ライブラリ template <class T> struct BiCoef { vector<T> fact_, inv_, finv_; constexpr BiCoef() {} constexpr BiCoef(int n) noexcept : fact_(n, 1), inv_(n, 1), finv_(n, 1) { init(n); } constexpr void init(int n) noexcept { fact_.assign(n, 1), inv_.assign(n, 1), finv_.assign(n, 1); int MOD = fact_[0].getmod(); for (int i = 2; i < n; i++) { fact_[i] = fact_[i - 1] * i; inv_[i] = -inv_[MOD % i] * (MOD / i); finv_[i] = finv_[i - 1] * inv_[i]; } } constexpr T com(int n, int k) const noexcept { if (n < k || n < 0 || k < 0) return 0; return fact_[n] * finv_[k] * finv_[n - k]; } constexpr T fact(int n) const noexcept { if (n < 0) return 0; return fact_[n]; } constexpr T inv(int n) const noexcept { if (n < 0) return 0; return inv_[n]; } constexpr T finv(int n) const noexcept { if (n < 0) return 0; return finv_[n]; } }; // const int MOD = 1000000007; // const int MOD = 998244353; // using mint = Fp<MOD>; // BiCoef<mint> bc; // bc.init(500050); #pragma endregion template <typename T> struct edge { int src, to; T cost; edge(int to, T cost) : src(-1), to(to), cost(cost) {} edge(int src, int to, T cost) : src(src), to(to), cost(cost) {} edge &operator=(const int &x) { to = x; return *this; } operator int() const { return to; } }; template <typename T> using Edges = vector<edge<T>>; template <typename T> using WeightedGraph = vector<Edges<T>>; using UnWeightedGraph = vector<vector<int>>; template <typename T> using Matrix = vector<vector<T>>; template <typename G> struct DoublingLowestCommonAncestor { const int LOG; vector<int> dep; const G &g; vector<vector<int>> table; DoublingLowestCommonAncestor(const G &g) : g(g), dep(g.size()), LOG(32 - __builtin_clz(g.size())) { table.assign(LOG, vector<int>(g.size(), -1)); } void dfs(int idx, int par, int d) { table[0][idx] = par; dep[idx] = d; for (auto &to : g[idx]) { if (to != par) dfs(to, idx, d + 1); } } void build() { dfs(0, -1, 0); // ここを書き換えれば親を選択できる for (int k = 0; k + 1 < LOG; k++) { for (int i = 0; i < table[k].size(); i++) { if (table[k][i] == -1) table[k + 1][i] = -1; else table[k + 1][i] = table[k][table[k][i]]; } } } int query(int u, int v) { if (dep[u] > dep[v]) swap(u, v); for (int i = LOG - 1; i >= 0; i--) { if (((dep[v] - dep[u]) >> i) & 1) v = table[i][v]; } if (u == v) return u; for (int i = LOG - 1; i >= 0; i--) { if (table[i][u] != table[i][v]) { u = table[i][u]; v = table[i][v]; } } return table[0][u]; } }; struct SegmentTree { int n; pair<int, int> ti = pair<int, int>(0, 0); vector<map<int, pair<int, int>>> dat; SegmentTree(){}; void init(int n_) { n = 1; while (n < n_) n <<= 1; dat.resize(n << 1); } void build(int N) { int n_ = N; init(n_); } pair<int, int> f(pair<int, int> a, pair<int, int> b) { return {a.first + b.first, a.second + b.second}; } void set_val(int k, pair<int, int> x, int col) { dat[k += n][col] = x; while (k >>= 1) dat[k][col] = f(dat[(k << 1) | 0][col], dat[(k << 1) | 1][col]); } pair<int, int> query(int a, int b, int col) { pair<int, int> vl = ti, vr = ti; for (int l = a + n, r = b + n; l < r; l >>= 1, r >>= 1) { if (l & 1) vl = f(vl, dat[l++][col]); if (r & 1) vr = f(dat[--r][col], vr); } return f(vl, vr); } }; // auto f = [](){}; // auto seg = SegmentTree<T,decltype(f)>(f,Ti); signed main() { int N, Q; in(N, Q); WeightedGraph<int> g(N); auto c = mv(N - 1), d = c; REP(i, N - 1) { int a, b; in(a, b, c[i], d[i]); --a; --b; --c[i]; g[a].emplace_back(b, i); g[b].emplace_back(a, i); } DoublingLowestCommonAncestor<WeightedGraph<int>> lca(g); lca.build(); auto ds = mv(N), us = ds; fill(ds, -1); fill(us, -1); SegmentTree seg; seg.build(2 * N); int idx = 0; vector<int> sum; sum.push_back(0); auto dfs = [&](auto &&f, int now, int par) -> void { for (auto e : g[now]) { if (e == par) continue; ds[e] = idx; seg.set_val(idx, {d[e.cost], 1}, c[e.cost]); sum.push_back(d[e.cost]); ++idx; f(f, e, now); us[e] = idx; seg.set_val(idx, {-d[e.cost], -1}, c[e.cost]); sum.push_back(-d[e.cost]); ++idx; } }; dfs(dfs, 0, -1); REP(i, sum.size() - 1) sum[i + 1] += sum[i]; vector<int> ans; while (Q--) { int x, y, a, b; in(x, y, a, b); x--; a--; b--; int p = lca.query(a, b); int ret = 0; auto f = [&](int child) { int l = ds[p] + 1, r = ds[child] + 1; int res = sum[r] - sum[l]; auto p = seg.query(l, r, x); res -= p.first; res += p.second * y; return res; }; ret += f(a); ret += f(b); ans.push_back(ret); } REP(i, ans.size()) { out(ans[i]); } }
replace
293
294
293
294
0
p02986
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using PP = pair<int, int>; struct Query { int a; int b; int c; int d; int m; }; int N, Q; vector<PP> G[100000]; int C[100000]; int D[100000]; Query V[100000]; vector<int> qs[100000]; int memo_dist[100000]; unordered_map<int, int> memo_cnt[100000]; unordered_map<int, int> memo_sum[100000]; int dep[100000]; int par[21][100000]; void pre_rec(int from, int prev, int d) { par[0][from] = prev; dep[from] = d; for (PP e : G[from]) { int to = e.first; if (to == prev) continue; pre_rec(to, from, d + 1); } } void pre() { pre_rec(0, -1, 0); for (int k = 0; k < 20; ++k) { for (int i = 0; i < N; ++i) { int p = par[k][i]; par[k + 1][i] = p == -1 ? -1 : par[k][p]; } } } int lca(int u, int v) { if (dep[u] > dep[v]) lca(v, u); int diff = dep[v] - dep[u]; for (int k = 20; k >= 0; --k) { if ((1 << k) <= diff) { v = par[k][v]; diff -= (1 << k); } } if (u == v) return u; for (int k = 20; k >= 0; --k) { if (par[k][u] != par[k][v]) { u = par[k][u]; v = par[k][v]; } } return par[0][u]; } int cnt[100000]; int sum[100000]; void rec(int from, int prev, int dist) { memo_dist[from] = dist; for (int q : qs[from]) { memo_cnt[from][q] = cnt[q]; memo_sum[from][q] = sum[q]; } for (PP e : G[from]) { int to = e.first; int idx = e.second; if (to == prev) continue; int c = C[idx]; int d = D[idx]; ++cnt[c]; sum[c] += d; rec(to, from, dist + d); sum[c] -= d; --cnt[c]; } } long val(int v, int c, int d) { return memo_dist[v] - memo_sum[v][c] + d * memo_cnt[v][c]; } int main() { cin >> N >> Q; for (int i = 0; i < N - 1; ++i) { int a, b, c, d; cin >> a >> b >> c >> d; --a; --b; --c; G[a].push_back(PP(b, i)); G[b].push_back(PP(a, i)); C[i] = c; D[i] = d; } for (int k = 0; k < Q; ++k) { int a, b, c, d; cin >> c >> d >> a >> b; --a; --b; --c; V[k] = Query{a, b, c, d, 0}; } pre(); for (int k = 0; k < Q; ++k) { int a = V[k].a; int b = V[k].b; int m = lca(a, b); V[k].m = m; int c = V[k].c; qs[a].push_back(c); qs[b].push_back(c); qs[m].push_back(c); } rec(0, -1, 0); for (int k = 0; k < Q; ++k) { int a = V[k].a; int b = V[k].b; int c = V[k].c; int d = V[k].d; int m = V[k].m; long ans = val(a, c, d) + val(b, c, d) - 2 * val(m, c, d); cout << ans << endl; } }
#include <bits/stdc++.h> using namespace std; using PP = pair<int, int>; struct Query { int a; int b; int c; int d; int m; }; int N, Q; vector<PP> G[100000]; int C[100000]; int D[100000]; Query V[100000]; vector<int> qs[100000]; int memo_dist[100000]; unordered_map<int, int> memo_cnt[100000]; unordered_map<int, int> memo_sum[100000]; int dep[100000]; int par[21][100000]; void pre_rec(int from, int prev, int d) { par[0][from] = prev; dep[from] = d; for (PP e : G[from]) { int to = e.first; if (to == prev) continue; pre_rec(to, from, d + 1); } } void pre() { pre_rec(0, -1, 0); for (int k = 0; k < 20; ++k) { for (int i = 0; i < N; ++i) { int p = par[k][i]; par[k + 1][i] = p == -1 ? -1 : par[k][p]; } } } int lca(int u, int v) { if (dep[u] > dep[v]) return lca(v, u); int diff = dep[v] - dep[u]; for (int k = 20; k >= 0; --k) { if ((1 << k) <= diff) { v = par[k][v]; diff -= (1 << k); } } if (u == v) return u; for (int k = 20; k >= 0; --k) { if (par[k][u] != par[k][v]) { u = par[k][u]; v = par[k][v]; } } return par[0][u]; } int cnt[100000]; int sum[100000]; void rec(int from, int prev, int dist) { memo_dist[from] = dist; for (int q : qs[from]) { memo_cnt[from][q] = cnt[q]; memo_sum[from][q] = sum[q]; } for (PP e : G[from]) { int to = e.first; int idx = e.second; if (to == prev) continue; int c = C[idx]; int d = D[idx]; ++cnt[c]; sum[c] += d; rec(to, from, dist + d); sum[c] -= d; --cnt[c]; } } long val(int v, int c, int d) { return memo_dist[v] - memo_sum[v][c] + d * memo_cnt[v][c]; } int main() { cin >> N >> Q; for (int i = 0; i < N - 1; ++i) { int a, b, c, d; cin >> a >> b >> c >> d; --a; --b; --c; G[a].push_back(PP(b, i)); G[b].push_back(PP(a, i)); C[i] = c; D[i] = d; } for (int k = 0; k < Q; ++k) { int a, b, c, d; cin >> c >> d >> a >> b; --a; --b; --c; V[k] = Query{a, b, c, d, 0}; } pre(); for (int k = 0; k < Q; ++k) { int a = V[k].a; int b = V[k].b; int m = lca(a, b); V[k].m = m; int c = V[k].c; qs[a].push_back(c); qs[b].push_back(c); qs[m].push_back(c); } rec(0, -1, 0); for (int k = 0; k < Q; ++k) { int a = V[k].a; int b = V[k].b; int c = V[k].c; int d = V[k].d; int m = V[k].m; long ans = val(a, c, d) + val(b, c, d) - 2 * val(m, c, d); cout << ans << endl; } }
replace
48
49
48
49
0
p02986
C++
Runtime Error
#include <bits/stdc++.h> #define For(i, a, b) for (int(i) = (a); (i) < (b); ++(i)) #define rFor(i, a, b) for (int(i) = (a)-1; (i) >= (b); --(i)) #define rep(i, n) For((i), 0, (n)) #define rrep(i, n) rFor((i), (n), 0) #define fi first #define se second using namespace std; typedef long long lint; typedef pair<int, int> pii; typedef complex<double> xy_t; const lint mod = 1e9 + 7; const int MAX = 100010; typedef struct edge { int to, col; lint dist; edge(int t, int c, lint d) { to = t; col = c; dist = d; } } edge; typedef struct Segtree { int n = 1; vector<int> node; vector<int> depth; Segtree(int n_, vector<int> d) { while (n < n_) n *= 2; node.resize(2 * n - 1, n - 1); depth.resize(n, MAX); rep(i, d.size()) depth[i] = d[i]; } void update(int i, int x) { i += n - 1; node[i] = x; while (i) { i = (i - 1) / 2; if (depth[node[2 * i + 1]] < depth[node[2 * i + 2]]) node[i] = node[2 * i + 1]; else node[i] = node[2 * i + 2]; } } int getmin(int a, int b, int i, int l, int r) { if (r <= a || b <= l) return n - 1; if (a <= l && r <= b) return node[i]; int lid = getmin(a, b, 2 * i + 1, l, (l + r) / 2); int rid = getmin(a, b, 2 * i + 2, (l + r) / 2, r); if (depth[lid] < depth[rid]) return lid; else return rid; } } Segtree; int n, q; int u[MAX], v[MAX], lca[MAX]; int x[MAX]; lint y[MAX]; map<pii, lint> col_num, col_dist; lint dv[MAX]; vector<edge> G[MAX]; lint now_col[MAX], now_dist[MAX]; vector<int> used_col[MAX]; void lca_dfs(int nowv, int prev, int &cur, int dep, vector<int> &id, vector<int> &vs, vector<int> &depth) { if (id[nowv] < 0) id[nowv] = cur; vs.push_back(nowv); depth.push_back(dep); ++cur; for (edge &e : G[nowv]) if (e.to != prev) { lca_dfs(e.to, nowv, cur, dep + 1, id, vs, depth); vs.push_back(nowv); depth.push_back(dep); ++cur; } } void getlca() { vector<int> id(n, -1); vector<int> vs, depth; int cur = 0; lca_dfs(0, -1, cur, 0, id, vs, depth); Segtree st(MAX, depth); rep(i, depth.size()) st.update(i, i); rep(i, q) { lca[i] = vs[st.getmin(min(id[u[i]], id[v[i]]), max(id[u[i]], id[v[i]]) + 1, 0, 0, st.n)]; } } void dfs(int nowv, lint nowd) { dv[nowv] = nowd; for (int tc : used_col[nowv]) { col_num[pii(nowv, tc)] = now_col[tc]; col_dist[pii(nowv, tc)] = now_dist[tc]; } for (edge &e : G[nowv]) if (dv[e.to] < 0) { ++now_col[e.col]; now_dist[e.col] += e.dist; dfs(e.to, nowd + e.dist); --now_col[e.col]; now_dist[e.col] -= e.dist; } } int main() { scanf("%d%d", &n, &q); rep(i, n - 1) { int a, b, c; lint d; scanf("%d%d%d%lld", &a, &b, &c, &d); G[--a].emplace_back(--b, --c, d); G[b].emplace_back(a, c, d); } rep(i, q) { scanf("%d%lld%d%d", &x[i], &y[i], &u[i], &v[i]); --x[i]; --u[i]; --v[i]; } getlca(); rep(i, q) { used_col[u[i]].push_back(x[i]); used_col[v[i]].push_back(x[i]); used_col[lca[i]].push_back(x[i]); } rep(i, n) { dv[i] = -1; now_col[i] = now_dist[i] = 0; } dfs(0, 0); rep(i, q) { lint d_u = dv[u[i]] - col_dist[pii(u[i], x[i])] + col_num[pii(u[i], x[i])] * y[i]; lint d_v = dv[v[i]] - col_dist[pii(v[i], x[i])] + col_num[pii(v[i], x[i])] * y[i]; lint d_lca = dv[lca[i]] - col_dist[pii(lca[i], x[i])] + col_num[pii(lca[i], x[i])] * y[i]; printf("%lld\n", d_u + d_v - 2 * d_lca); } }
#include <bits/stdc++.h> #define For(i, a, b) for (int(i) = (a); (i) < (b); ++(i)) #define rFor(i, a, b) for (int(i) = (a)-1; (i) >= (b); --(i)) #define rep(i, n) For((i), 0, (n)) #define rrep(i, n) rFor((i), (n), 0) #define fi first #define se second using namespace std; typedef long long lint; typedef pair<int, int> pii; typedef complex<double> xy_t; const lint mod = 1e9 + 7; const int MAX = 100010; typedef struct edge { int to, col; lint dist; edge(int t, int c, lint d) { to = t; col = c; dist = d; } } edge; typedef struct Segtree { int n = 1; vector<int> node; vector<int> depth; Segtree(int n_, vector<int> d) { while (n < n_) n *= 2; node.resize(2 * n - 1, n - 1); depth.resize(n, MAX); rep(i, d.size()) depth[i] = d[i]; } void update(int i, int x) { i += n - 1; node[i] = x; while (i) { i = (i - 1) / 2; if (depth[node[2 * i + 1]] < depth[node[2 * i + 2]]) node[i] = node[2 * i + 1]; else node[i] = node[2 * i + 2]; } } int getmin(int a, int b, int i, int l, int r) { if (r <= a || b <= l) return n - 1; if (a <= l && r <= b) return node[i]; int lid = getmin(a, b, 2 * i + 1, l, (l + r) / 2); int rid = getmin(a, b, 2 * i + 2, (l + r) / 2, r); if (depth[lid] < depth[rid]) return lid; else return rid; } } Segtree; int n, q; int u[MAX], v[MAX], lca[MAX]; int x[MAX]; lint y[MAX]; map<pii, lint> col_num, col_dist; lint dv[MAX]; vector<edge> G[MAX]; lint now_col[MAX], now_dist[MAX]; vector<int> used_col[MAX]; void lca_dfs(int nowv, int prev, int &cur, int dep, vector<int> &id, vector<int> &vs, vector<int> &depth) { if (id[nowv] < 0) id[nowv] = cur; vs.push_back(nowv); depth.push_back(dep); ++cur; for (edge &e : G[nowv]) if (e.to != prev) { lca_dfs(e.to, nowv, cur, dep + 1, id, vs, depth); vs.push_back(nowv); depth.push_back(dep); ++cur; } } void getlca() { vector<int> id(n, -1); vector<int> vs, depth; int cur = 0; lca_dfs(0, -1, cur, 0, id, vs, depth); Segtree st(2 * MAX, depth); rep(i, depth.size()) st.update(i, i); rep(i, q) { lca[i] = vs[st.getmin(min(id[u[i]], id[v[i]]), max(id[u[i]], id[v[i]]) + 1, 0, 0, st.n)]; } } void dfs(int nowv, lint nowd) { dv[nowv] = nowd; for (int tc : used_col[nowv]) { col_num[pii(nowv, tc)] = now_col[tc]; col_dist[pii(nowv, tc)] = now_dist[tc]; } for (edge &e : G[nowv]) if (dv[e.to] < 0) { ++now_col[e.col]; now_dist[e.col] += e.dist; dfs(e.to, nowd + e.dist); --now_col[e.col]; now_dist[e.col] -= e.dist; } } int main() { scanf("%d%d", &n, &q); rep(i, n - 1) { int a, b, c; lint d; scanf("%d%d%d%lld", &a, &b, &c, &d); G[--a].emplace_back(--b, --c, d); G[b].emplace_back(a, c, d); } rep(i, q) { scanf("%d%lld%d%d", &x[i], &y[i], &u[i], &v[i]); --x[i]; --u[i]; --v[i]; } getlca(); rep(i, q) { used_col[u[i]].push_back(x[i]); used_col[v[i]].push_back(x[i]); used_col[lca[i]].push_back(x[i]); } rep(i, n) { dv[i] = -1; now_col[i] = now_dist[i] = 0; } dfs(0, 0); rep(i, q) { lint d_u = dv[u[i]] - col_dist[pii(u[i], x[i])] + col_num[pii(u[i], x[i])] * y[i]; lint d_v = dv[v[i]] - col_dist[pii(v[i], x[i])] + col_num[pii(v[i], x[i])] * y[i]; lint d_lca = dv[lca[i]] - col_dist[pii(lca[i], x[i])] + col_num[pii(lca[i], x[i])] * y[i]; printf("%lld\n", d_u + d_v - 2 * d_lca); } }
replace
94
95
94
95
0
p02986
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> using namespace std; #define ll long long #define FOR(i, a, b) for (int i = a; i <= b; i++) #define FORD(i, a, b) for (int i = a; i >= b; i--) #define FORL(i, x) for (int i = head[x]; i; i = nxt[i]) #define clr(x, y) memset(x, y, sizeof(x)) #define in(a) a = read() #define out(a) printf("%d\n", a) inline ll read() { char c = getchar(); ll f = 1, x = 0; while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') x = x * 10 + (c - '0'), c = getchar(); return x * f; } #define mod 1000000007 void MOD(int &x) { if (x >= mod) x -= mod; } #define maxn 100010 #define inf (1 << 30) int l, nxt[maxn * 2], head[maxn], to[maxn * 2], cl[maxn * 2], v[maxn * 2]; void add(int x, int y, int c, int z) { l++; nxt[l] = head[x]; head[x] = l; to[l] = y; cl[l] = c; v[l] = z; } int bin[maxn]; int fa[maxn][20], d[maxn], s[maxn]; void dfs(int x) { FOR(i, 1, 20) fa[x][i] = fa[fa[x][i - 1]][i - 1]; FORL(i, x) { int c = to[i]; if (c == fa[x][0]) continue; fa[c][0] = x; d[c] = d[x] + 1; s[c] = s[x] + v[i]; dfs(c); } } int lca(int x, int y) { if (d[x] < d[y]) swap(x, y); int k = d[x] - d[y]; FOR(i, 0, 20) if (k & (1 << i)) x = fa[x][i]; if (x == y) return x; FORD(i, 20, 0) if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i]; return x == y ? x : fa[x][0]; } struct A { int cl, v, c, id; }; #include <vector> vector<A> ve[maxn]; int ans[maxn]; int sz[maxn], sum[maxn]; void dfs2(int x) { int l = ve[x].size(); FOR(i, 0, l - 1) { A q = ve[x][i]; ans[q.id] -= q.c * sum[q.cl]; ans[q.id] += q.c * sz[q.cl] * q.v; } FORL(i, x) { int c = to[i]; if (c == fa[x][0]) continue; sum[cl[i]] += v[i]; sz[cl[i]]++; dfs2(c); sum[cl[i]] -= v[i]; sz[cl[i]]--; } } int main() { int n, m; in(n); in(m); FOR(i, 2, n) bin[i] = bin[i / 2] + 1; FOR(i, 1, n - 1) { int x, y, C, D; in(x); in(y); in(C); in(D); add(x, y, C, D); add(y, x, C, D); } dfs(1); FOR(i, 1, m) { int x, y, u, v; in(x); in(y); in(u); in(v); int z = lca(u, v); ans[i] = s[u] + s[v] - 2 * s[z]; ve[u].push_back((A){x, y, 1, i}); ve[v].push_back((A){x, y, 1, i}); ve[z].push_back((A){x, y, -2, i}); } dfs2(1); FOR(i, 1, m) out(ans[i]); }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> using namespace std; #define ll long long #define FOR(i, a, b) for (int i = a; i <= b; i++) #define FORD(i, a, b) for (int i = a; i >= b; i--) #define FORL(i, x) for (int i = head[x]; i; i = nxt[i]) #define clr(x, y) memset(x, y, sizeof(x)) #define in(a) a = read() #define out(a) printf("%d\n", a) inline ll read() { char c = getchar(); ll f = 1, x = 0; while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') x = x * 10 + (c - '0'), c = getchar(); return x * f; } #define mod 1000000007 void MOD(int &x) { if (x >= mod) x -= mod; } #define maxn 100010 #define inf (1 << 30) int l, nxt[maxn * 2], head[maxn], to[maxn * 2], cl[maxn * 2], v[maxn * 2]; void add(int x, int y, int c, int z) { l++; nxt[l] = head[x]; head[x] = l; to[l] = y; cl[l] = c; v[l] = z; } int bin[maxn]; int fa[maxn][30], d[maxn], s[maxn]; void dfs(int x) { FOR(i, 1, 20) fa[x][i] = fa[fa[x][i - 1]][i - 1]; FORL(i, x) { int c = to[i]; if (c == fa[x][0]) continue; fa[c][0] = x; d[c] = d[x] + 1; s[c] = s[x] + v[i]; dfs(c); } } int lca(int x, int y) { if (d[x] < d[y]) swap(x, y); int k = d[x] - d[y]; FOR(i, 0, 20) if (k & (1 << i)) x = fa[x][i]; if (x == y) return x; FORD(i, 20, 0) if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i]; return x == y ? x : fa[x][0]; } struct A { int cl, v, c, id; }; #include <vector> vector<A> ve[maxn]; int ans[maxn]; int sz[maxn], sum[maxn]; void dfs2(int x) { int l = ve[x].size(); FOR(i, 0, l - 1) { A q = ve[x][i]; ans[q.id] -= q.c * sum[q.cl]; ans[q.id] += q.c * sz[q.cl] * q.v; } FORL(i, x) { int c = to[i]; if (c == fa[x][0]) continue; sum[cl[i]] += v[i]; sz[cl[i]]++; dfs2(c); sum[cl[i]] -= v[i]; sz[cl[i]]--; } } int main() { int n, m; in(n); in(m); FOR(i, 2, n) bin[i] = bin[i / 2] + 1; FOR(i, 1, n - 1) { int x, y, C, D; in(x); in(y); in(C); in(D); add(x, y, C, D); add(y, x, C, D); } dfs(1); FOR(i, 1, m) { int x, y, u, v; in(x); in(y); in(u); in(v); int z = lca(u, v); ans[i] = s[u] + s[v] - 2 * s[z]; ve[u].push_back((A){x, y, 1, i}); ve[v].push_back((A){x, y, 1, i}); ve[z].push_back((A){x, y, -2, i}); } dfs2(1); FOR(i, 1, m) out(ans[i]); }
replace
42
43
42
43
-11
p02986
C++
Runtime Error
#include <algorithm> #include <bits/stdc++.h> #include <cassert> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <string> #include <vector> using namespace std; typedef long long ll; typedef long double ld; using edge = struct { ll to; ll cost; }; using point = struct { ll x; ll y; }; typedef string str; typedef std::pair<ll, ll> pl; typedef std::pair<ll, pl> pl3; typedef std::map<string, ll> msl; typedef std::map<char, ll> mcl; typedef std::map<ll, ll> mll; typedef std::vector<ll> vl; typedef std::vector<pl> vpl; typedef std::vector<point> points; typedef std::vector<pl3> vpl3; typedef std::priority_queue<ll> pq; typedef std::priority_queue<ll, vl, greater<ll>> pql; // priority queue taking from the lower value. typedef std::vector<edge> gr; const ll MOD = 1e9 + 7; // const ll MOD = 998244353; const ll INF = MOD * MOD; const long double EPS = 1e-9; const long double PI = 3.14159265358979323846; vpl dirs = { {1, 0}, {0, 1}, {-1, 0}, {0, -1}, // four directions {1, 1}, {-1, 1}, {-1, -1}, {1, -1}, // diagonal {0, 0} // self }; #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 pr(a) cout << (a) #define prl(a) cout << (a) << endl #define prl2(a, b) cout << (a) << " " << (b) << endl #define prl3(a, b, c) cout << (a) << " " << (b) << " " << (c) << endl #define prl4(a, b, c, d) \ cout << (a) << " " << (b) << " " << (c) << " " << (d) << endl #define prs(a) cout << (a) << " " #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 e1 first #define e2 second #define ctol(c) ll((c)) - ll('0') #define ltos(n) to_string((n)) #define items(kv, v) for (auto &(kv) : (v)) #define ndig(N, n) ctol(ll(ltos((N))[ll(ltos((N)).length()) - (n)])) #define rsort(a, n) sort(a, a + n, greater<>()) #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) \ cout << fixed << setprecision(digits) << (num) << endl; #define XOR(a, b) (a) ^ (b) #define prl_time(s) \ prl3("Elapsed Time:", 1000.0 * (clock() - s) / CLOCKS_PER_SEC, "[ms]"); #define char_to_str(c) string(1, (c)) #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) #define DEBUG(x) cerr << #x << ": " << (x) << endl #define DEBUG_VEC(v) \ cerr << #v << ": "; \ rep(__i, (v).size()) cerr << ((v)[__i]) << ", "; \ cerr << endl 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) { 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; } } }; class UnionFind { vl parents, set_size; public: UnionFind() {} UnionFind(ll n) { parents = set_size = vl(n); rep(i, n) { parents[i] = i; set_size[i] = 1LL; } } ll root_find(ll x) { if (parents[x] == x) return x; return parents[x] = root_find(parents[x]); } void unite(ll x, ll y) { x = root_find(x); y = root_find(y); if (x == y) return; if (set_size[x] < set_size[y]) { parents[y] = x; set_size[x] += set_size[y]; } else { parents[x] = y; set_size[y] += set_size[x]; } } bool is_same(ll x, ll y) { // connected or not return root_find(x) == root_find(y); } ll size(ll x) { return set_size[root_find(x)]; } }; /* class LCA{ public: ll N, logN; vl depth, len; gr tree[200005]; // global declaration later. vector<vl> parents; LCA(ll n){ N = n; logN = 0; while (N > (1LL << logN)) logN++; depth = vl(N); len = vl(N); parents = vector<vl>(logN, vl(N)); 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 BIT { ll n; vl tree_dat; BIT(ll n) : tree_dat(n + 1, 0), n(n){}; // 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 dat) { // if maximum -> max(val, dat) // return max(val, dat); // if cumulative sum return val + dat; } ll query(ll i) { /* e.g.) i = 10101 itr1. 10101 -> 10100 itr2. 10100 -> 10000 itr3. 10000 -> 00000 (break) */ ll ret = 0; for (ll j = i; j >= 0; j = (j & (j + 1)) - 1) { ret = update_func(ret, tree_dat[j]); } return ret; } ll lower_bound(ll key) { 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 && tree_dat[left + i - 1] < key) { key -= tree_dat[left + i - 1]; left += i; } } return left; } void update(ll i, ll val) { /* e.g.) i = 10101, n = 11111 itr1. 10101 -> 10110 itr2. 10110 -> 11000 itr3. 11000 -> 100000 (break) */ if (i < 0) return; for (ll j = i; j < n; j |= j + 1) { tree_dat[j] = update_func(val, tree_dat[j]); } } }; 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_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 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 comb_memo[55][55]; ll pascal_triangle(ll n) { comb_memo[0][0] = 1; For(i, 1, n + 1) rep(j, i + 1) { comb_memo[i][j] += comb_memo[i - 1][j]; if (j > 0) comb_memo[i][j] += comb_memo[i - 1][j - 1]; } } 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; } ll combination_memo(ll n, ll r, ll pre, ll mod) { // pre = nCr-1 // return nCr if (n == r && n == 0) return 1; else if (n <= 0 || r < 0 || r > n) return 0; ll numerator = ll(n - r + 1) % mod; ll denomenator = modinv(r, mod) % mod; ll val = (numerator * denomenator) % mod; val *= pre; return val % mod; } ll combination_no_mod(ll n, ll r) { 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++) { numerator *= n - i; denomenator *= i + 1; ll g = gcd(numerator, denomenator); numerator /= g; denomenator /= g; } return numerator; } 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; } ll bin_search(ll key, ll A[], ll left, ll right) { // return the index idx where A[idx] = key. // A[left] is start and A[right] is end.. // In other words, A[right], not A[right - 1], must be defined. while (right >= left) { ll mid = left + (right - left) / 2; if (A[mid] == key) return mid; else if (A[mid] > key) right = mid - 1; else if (A[mid] < key) left = mid + 1; } return -1; } /* ll bin_search_temp(ll left, ll right, callable judge){ while(right > left){ // when seeking lower bound ll mid = (right + left) / 2; if (judge(mid)) right = mid; else left = mid + 1; // when seeking upper bound ll mid = (right + left + 1) / 2; if (judge(mid)) left = mid; else right = mid - 1; } return right; } trinary_search // Care the value EPS!!! Compare to the condition ld left = 0; ld right = p; while(abs(right - left) > EPS){ ld left2 = (2 * left + right) / 3.0; ld right2 = (left + 2 * right) / 3.0; ld f1 = tak_func(left2); ld f2 = tak_func(right2); if (f1 <= f2) right = right2; else if (f2 <= f1) left = left2; } */ ll lower_bound_bin_search_temp(ll key, ll A[], ll left, ll right) { // Pay attention to the return value -1. // For example, sometimes you may want the value right instead of -1. if (A[left] >= key) return left; ll mid = left + (right - left) / 2; while (right >= left) { mid = left + (right - left) / 2; if (mid == left) { if (A[left] < key && key <= A[left + 1]) return left + 1; else return -1; } if (A[mid - 1] < key && key <= A[mid]) return mid; else if (A[mid - 1] >= key) right = mid - 1; else if (A[mid] < key) left = mid; } return -1; // all the elements < key } ll inf_bin_search_temp(ll key, ll A[], ll left, ll right) { // Pay attention to the return value -1. // For example, sometimes you may want the value right instead of -1. if (A[left] > key) return left; ll mid = left + (right - left) / 2; while (right >= left) { mid = left + (right - left) / 2; if (mid == left) { if (A[left] <= key && key < A[left + 1]) return left + 1; else return -1; } if (A[mid - 1] <= key && key < A[mid]) return mid; else if (A[mid - 1] > key) right = mid - 1; else if (A[mid] <= key) left = mid; } return -1; // all the elements <= key } ll upper_bound_bin_search_temp(ll key, ll A[], ll left, ll right) { // Pay attention to the return value -1. // For example, sometimes you may want the value left instead of -1. if (A[right] <= key) return right; ll mid = left + (right - left) / 2; while (right >= left) { mid = left + (right - left) / 2; if (mid == right) { if (A[right - 1] <= key && key < A[right]) return right - 1; else return -1; } if (A[mid] <= key && key < A[mid + 1]) return mid; else if (A[mid] > key) right = mid; else if (A[mid + 1] <= key) left = mid + 1; } return -1; // all the elements > key } ll sup_bin_search_temp(ll key, ll A[], ll left, ll right) { // Pay attention to the return value -1. // For example, sometimes you may want the value left instead of -1. if (A[right] < key) return right; ll mid = left + (right - left) / 2; while (right >= left) { mid = left + (right - left) / 2; if (mid == right) { if (A[right - 1] < key && key <= A[right]) return right - 1; else return -1; } if (A[mid] < key && key <= A[mid + 1]) return mid; else if (A[mid] >= key) right = mid; else if (A[mid + 1] < key) left = mid + 1; } return -1; // all the elements >= key } ll bin_search_vector(ll key, vl v, ll left, ll right) { // return the index idx where v[idx] = key. // v[left] is start and v[right] is end.. // In other words, v[right], not v[right - 1], must be defined. while (right >= left) { ll mid = left + (right - left) / 2; if (v[mid] == key) return mid; else if (v[mid] > key) right = mid - 1; else if (v[mid] < key) left = mid + 1; } return -1; } ll lower_bound_bin_search_vector(ll key, vl v) { // the return value N satisfies // v[N - 1] < key <= v[N] // N == -1 if all the elements < key return lower_bound(all(v), key) - v.begin(); } ll inf_bin_search_vector(ll key, vl v) { // the return value N satisfies // v[N - 1] <= key < v[N] <= key + 1 // N == -1 if all the elements <= key return lower_bound(all(v), key + 1) - v.begin(); } ll upper_bound_bin_search_vector(ll key, vl v) { // the return value N satisfies // v[N] <= key < v[N + 1] // N == -1 if all the elements > key return upper_bound(all(v), key) - v.begin(); // (- 1) } ll sup_bin_search_vector(ll key, vl v) { // the return value N satisfies // v[N] <= key - 1 < key <= v[N + 1] // N == -1 if all the elements >= key return upper_bound(all(v), key - 1) - v.begin() - 1; } ll fact(ll n) { if (n == 0) return 1; return n * fact(n - 1); } ll fact_mod(ll n, ll mod) { if (n == 0) return 1; return n * fact_mod(n - 1, mod); } 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; } ll bool_sum(ll a1, ll a2) { if (a1 == 1 || a2 == 1) return 1; return 0; } mll prime_factorization(ll n) { ll i = 2; mll 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; } ll char_to_idx(char c) { ll idx = 0; Forchar(cc, 'a', 'z') { if (c == cc) return idx; else idx++; } } ll next_combination(ll sub) { /* ll n, k; ll bit = (1 << k) - 1; for (; bit < (1 << n); bit = next_combination(bit)){ bool ith = bit & (1 << i); procedures... } */ ll x = sub & -sub, y = sub + x; return (((sub & ~y) / x) >> 1) | y; } vl z_algorithm(str s) { ll n = s.length(); vl res(n); res[0] = n; ll i1 = 1, i2 = 0; while (i1 < n) { while (i1 + i2 < n && s[i2] == s[i1 + i2]) ++i2; res[i1] = i2; if (i2 == 0) { ++i1; continue; } ll i3 = 1; while (i1 + i3 < n && i3 + res[i3] < i2) { res[i1 + i3] = res[i3]; ++i3; } i1 += i3, i2 -= i3; } return res; } ll big_number_mod(str s, ll mod) { ll l = s.length(); ll idx = 0; ll val = 0; ll tenth = 1; while (idx < l) { ll m = ctol(s[l - 1 - idx]); val += (m * tenth) % mod; val %= mod; tenth *= 10; tenth %= mod; idx++; } return val; } str bin_expression(ll n, ll dig) { str s = ""; rep(i, dig) { s += ltos(n % 2); n /= 2; } reverse(all(s)); return s; } ll string_to_ll(str s) { ll l = s.length(); ll idx = 0; ll val = 0; ll tenth = 1; while (idx < l) { ll m = ctol(s[l - 1 - idx]); val += (m * tenth); tenth *= 10; idx++; } return val; } str reflected_string(str s) { str 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; } 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> center_2det_of_3points(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 c2x_2 = x2 + x1, c2y_2 = y2 + y1; ll c3x_2 = x3 + x1, c3y_2 = y3 + y1; ll b2y = x2 - x1, b2x = -y2 + y1; ll b3y = x3 - x1, b3x = -y3 + y1; ll x1_2 = c3x_2 * b3y - c3y_2 * b3x, y1_2 = c2x_2 * b2y - c2y_2 * b2x; ll det = -b3y * b2x + b3x * b2y; if (det == 0) return {{INF, INF}, det}; ll cx_2det = -b2x * x1_2 + b3x * y1_2, cy_2det = -b2y * x1_2 + b3y * y1_2; return {{cx_2det, cy_2det}, det}; } 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. */ BIT 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; } 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; } /* const ll N_VERTEX = 310; ll a, b, t; ll dist[N_VERTEX][N_VERTEX]; void warshall_floyd(ll n){ // 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]); } int main(void){ in2(n, m); rep(i, n) rep(j, n) dist[i][j] = INF * (i != j); rep(i, m){ in3(a, b, t); a--; b--; dist[a][b] = t; dist[b][a] = t; } warshall_floyd(n); } ll dist[1005], vertex_pre[1005]; void dijkstra(ll start, ll n) { priority_queue<pl, vector<pl>, greater<pl>> edge_costs; fill(dist, dist + n, INF); // fill(vertex_pre, vertex_pre + n, -1); dist[start] = 0; edge_costs.push(pl(0, start)); while (!edge_costs.empty()) { pl edge_cost = edge_costs.top(); edge_costs.pop(); ll idx = edge_cost.second; ll cost = edge_cost.first; if (dist[idx] < cost) continue; rep(i, graph[idx].size()){ edge e = graph[idx][i]; if (dist[e.to] > dist[idx] + e.cost){ dist[e.to] = dist[idx] + e.cost; // vertex_pre[e.to] = idx; edge_costs.push(pl(dist[e.to], e.to)); } } } } vl get_predecessor(ll g){ vl path; for (; g != -1; g = vertex_pre[g]) path.pb(g); reverse(all(path)); return path; } int main(void){ in2(n, m); rep(i, m){ in3(a, b, t); a--; b--; G[a].pb({b, t}); G[b].pb({a, t}); } dijkstra(0, n); } # ABC061D bool find_negative_cycle(ll goal){ 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 n, ll goal){ // 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) fill(dist, dist + 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(goal)) return false; return true; } */ /* # 1. The usage of map pair map<pl, ll> cnt; cnt[{i, j}] = 0; items(kv, cnt){ prl2(kv.first, kv.second); } # 2. The usage of next_permutation and combination (factorial search) ll a[8]; rep(i, 8) a[i] = i; sort(a, a + 8); do{ }while(next_permutation(a, a+n)); // here, combination ll n, r; ll i1, i2, ..., ir; For(i1, n - r, n) For(i2, n - r - 1, i1) ... For(ir, n - 2 * r + 1, irr){ process;} # 3. bit search ll n; in1(n); const ll base = 3; ll upper = power_normal(base, n); rep(i, upper){ ll tmp = i; rep(j, n){ rep(k, base) if (tmp % base == k) prl(k); tmp /= base; } } # 4. imos method // used when we would like to count the number which // shows how many times the numbers between l and r belongs to smt. // This method is composed of three process. ll n, m, s[MAX_M], l, r; in2(n, m); rep(i, m) s[i] = 0; // 1st step rep(i, n){ in3(l, r, c); l--; r--; // if l starts from 1. s[l] += c; s[r + 1] -= c; } // 2nd step rep(i, m - 1) s[i + 1] += s[i]; // 3rd step: judgement... #5. shakutori method (syakutori) // 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){ r = max(r, l); if (l == r) r++; while(r < n && cond) r++; answer += r - l; l++; } prl(answer); #6. priority queue pq q; ll answer = 0; ll v; rep(i, n) q.push(a[i]); rep(i, m){ v = q.top(); q.pop(); // get the top value and dump the value from queue v /= 2; q.push(v); // add the new value } while(!q.empty()){ answer += q.top(); q.pop(); } #7. The shortest path (distance) between the k-th edge and another edge (Tree) ll depth[MAX_N]; gr tree[MAX_N]; void dfs(ll source, ll parent, ll all_cost){ depth[source] = all_cost; items(e, tree[source]){ if (e.to == parent) continue; dfs(e.to, source, all_cost + e.cost); } } ll n, k, a, b, c; in2(n, k); rep(i, n - 1){ in3(a, b, c); a--; b--; tree[a].pb({b, c}); tree[b].pb({a, c}); } k--; dfs(k, -1, 0); #10. Visiting Subtree using recurrent function (ABC138D) gr tree[MAX_N]; ll c[MAX_N]; bool visited[MAX_N]; void dfs(ll source, ll parent, ll val){ visited[source] = true; c[source] += val; rep(i, tree[source].size()){ rep(i, m){ll res = n % match[i].e1;} ll vertex = tree[source][i].to; if (vertex == parent) continue; dfs(vertex, source, c[source]); } } #11. bfs ABC146D, ABC007C 1. first create a tree. 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. queue<pl> q; void bfs(ll source, ll parents){ ll n_edge = G[source].size(); if (parents != -1) dist[source] = min(dist[source], dist[parents] + 1); if (visited[source]) return; visited[source] = true; rep(idx, n_edge){ ll target = G[source][idx].to; if (target == parents) continue; q.push(mp(target, source)); } } q.push(mp(sg.e1, -1)); while(!q.empty()){ pl source = q.front(); q.pop(); bfs(source.e1, source.e2); } #12. grid to distance matrix (dx, dy) ll w, h; ll pos_to_idx(ll x, ll y){ return y * w + x; } pl idx_to_pos(ll idx){ return mp(idx % w, idx / w); } int main(void){ in2(h, w); rep(y, h){ in1(s); rep(x, w){ if (s[x] == '#') wall[x][y] = true; else wall[x][y] = false; } } rep(i1, h * w)rep(i2, h * w) dist[i1][i2] = INF * (i1 != i2); rep(x, w)rep(y, h){ ll idx1 = pos_to_idx(x, y); ll idx2; if (wall[x][y]) continue; if (x != 0 && !wall[x - 1][y]){ idx2 = pos_to_idx(x - 1, y); // if warshall floyd dist[idx1][idx2] = 1; // if dijkstra // graph[idx1].pb({idx2, 1}); } if (x != w - 1 && !wall[x + 1][y]){ idx2 = pos_to_idx(x + 1, y); dist[idx1][idx2] = 1; // graph[idx1].pb({idx2, 1}); } if (y != 0 && !wall[x][y - 1]){ idx2 = pos_to_idx(x, y - 1); dist[idx1][idx2] = 1; // graph[idx1].pb({idx2, 1}); } if (y != h - 1 && !wall[x][y + 1]){ idx2 = pos_to_idx(x, y + 1); dist[idx1][idx2] = 1; // graph[idx1].pb({idx2, 1}); } } } */ /* # the operators regarding bit & (AND), | (OR), ^ (XOR) - (REVERSE), >> (SMALLER SHIFT) << (BIGGER SHIFT) x1: 0000 0001 0010 0101 0110 0111 0111 x2: xxxx 0001 0011 0100 0101 1000 0110 x1 & x2: 0000 0001 0010 0100 0100 0000 0110 x: 1001 1010 1100 1011 1101 1111 x & - x: 0001 0010 0100 0001 0001 0001 sum: 1010 1100 10000 1100 1100 10000 x << y is x * 2 ** y x >> y is rep(i, y) x = x // 2 Let S be a bit sequence and i be a non-negative integer S & (1 << i) -> if true, i in S S | (1 << i) -> S union {i} S & -(1 << i) -> S - {i} __builtin_popcount(S) -> the number of elements in S S = 0 -> S is an empty set S = (1 << n) - 1 -> S includes all the elements up to the n-th #Conditional Operator condition ? true : false; #iterator type declaration: auto value reference: *itr increment: itr++ decrement: itr-- substitution of value: *itr = smt */ const ll MAX_N = 200005; bool okay = false; ll answer = 0; using cedge = struct { ll to; ll cost; ll color; }; vector<cedge> tree[MAX_N]; class LCA { public: ll N, logN; vl depth, len; vector<vl> parents; LCA(ll n) { N = n; logN = 0; while (N > (1LL << logN)) logN++; depth = vl(N); len = vl(N); parents = vector<vl>(logN, vl(N)); 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]; } }; ll n, q, a, b, c, d; ll x, y, u, v, w; using queries = struct { ll col; ll cost; ll from; ll to; ll anc; }; vector<queries> qs; vl ans[MAX_N]; set<ll> required_colors[MAX_N]; map<pl, ll> cmp, dmp; map<ll, ll> cmp_test, dmp_test; const ll K = 1e5 + 5; ll dists[MAX_N]; void dfs(ll source, ll parent, ll dist) { dists[source] = dist; for (ll c : required_colors[source]) { cmp[{source, c}] = cmp_test[c]; dmp[{source, c}] = dmp_test[c]; } ll target, c, d; for (cedge e : tree[source]) { target = e.to, c = e.color, d = e.cost; if (target == parent) continue; dist += d; cmp_test[c]++; dmp_test[c] += d; dfs(target, source, dist); dist -= d; cmp_test[c]--; dmp_test[c] -= d; } } void solve() { rep(i, n - 1) { in4(a, b, c, d); a--; b--; c--; tree[a].pb({b, d, c}); tree[b].pb({a, d, c}); } LCA lca(n); rep(i, q) { in4(x, y, u, v); x--; u--; v--; w = lca.query(u, v); qs[i] = {x, y, u, v, w}; required_colors[u].insert(x); required_colors[v].insert(x); required_colors[w].insert(x); } dfs(0, -1, 0); rep(i, q) { x = qs[i].col, y = qs[i].cost, u = qs[i].from, v = qs[i].to, w = qs[i].anc; answer = lca.distance(u, v); answer -= dmp[{u, x}] + dmp[{v, x}] - 2 * dmp[{w, x}]; answer += y * (cmp[{u, x}] + cmp[{v, x}] - 2 * cmp[{w, x}]); prl(answer); } // check negative MOD // check index flow // check overwrite of the input variables } int main(void) { in2(n, q); // assert(n <= 400); solve(); return 0; }
#include <algorithm> #include <bits/stdc++.h> #include <cassert> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <string> #include <vector> using namespace std; typedef long long ll; typedef long double ld; using edge = struct { ll to; ll cost; }; using point = struct { ll x; ll y; }; typedef string str; typedef std::pair<ll, ll> pl; typedef std::pair<ll, pl> pl3; typedef std::map<string, ll> msl; typedef std::map<char, ll> mcl; typedef std::map<ll, ll> mll; typedef std::vector<ll> vl; typedef std::vector<pl> vpl; typedef std::vector<point> points; typedef std::vector<pl3> vpl3; typedef std::priority_queue<ll> pq; typedef std::priority_queue<ll, vl, greater<ll>> pql; // priority queue taking from the lower value. typedef std::vector<edge> gr; const ll MOD = 1e9 + 7; // const ll MOD = 998244353; const ll INF = MOD * MOD; const long double EPS = 1e-9; const long double PI = 3.14159265358979323846; vpl dirs = { {1, 0}, {0, 1}, {-1, 0}, {0, -1}, // four directions {1, 1}, {-1, 1}, {-1, -1}, {1, -1}, // diagonal {0, 0} // self }; #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 pr(a) cout << (a) #define prl(a) cout << (a) << endl #define prl2(a, b) cout << (a) << " " << (b) << endl #define prl3(a, b, c) cout << (a) << " " << (b) << " " << (c) << endl #define prl4(a, b, c, d) \ cout << (a) << " " << (b) << " " << (c) << " " << (d) << endl #define prs(a) cout << (a) << " " #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 e1 first #define e2 second #define ctol(c) ll((c)) - ll('0') #define ltos(n) to_string((n)) #define items(kv, v) for (auto &(kv) : (v)) #define ndig(N, n) ctol(ll(ltos((N))[ll(ltos((N)).length()) - (n)])) #define rsort(a, n) sort(a, a + n, greater<>()) #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) \ cout << fixed << setprecision(digits) << (num) << endl; #define XOR(a, b) (a) ^ (b) #define prl_time(s) \ prl3("Elapsed Time:", 1000.0 * (clock() - s) / CLOCKS_PER_SEC, "[ms]"); #define char_to_str(c) string(1, (c)) #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) #define DEBUG(x) cerr << #x << ": " << (x) << endl #define DEBUG_VEC(v) \ cerr << #v << ": "; \ rep(__i, (v).size()) cerr << ((v)[__i]) << ", "; \ cerr << endl 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) { 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; } } }; class UnionFind { vl parents, set_size; public: UnionFind() {} UnionFind(ll n) { parents = set_size = vl(n); rep(i, n) { parents[i] = i; set_size[i] = 1LL; } } ll root_find(ll x) { if (parents[x] == x) return x; return parents[x] = root_find(parents[x]); } void unite(ll x, ll y) { x = root_find(x); y = root_find(y); if (x == y) return; if (set_size[x] < set_size[y]) { parents[y] = x; set_size[x] += set_size[y]; } else { parents[x] = y; set_size[y] += set_size[x]; } } bool is_same(ll x, ll y) { // connected or not return root_find(x) == root_find(y); } ll size(ll x) { return set_size[root_find(x)]; } }; /* class LCA{ public: ll N, logN; vl depth, len; gr tree[200005]; // global declaration later. vector<vl> parents; LCA(ll n){ N = n; logN = 0; while (N > (1LL << logN)) logN++; depth = vl(N); len = vl(N); parents = vector<vl>(logN, vl(N)); 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 BIT { ll n; vl tree_dat; BIT(ll n) : tree_dat(n + 1, 0), n(n){}; // 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 dat) { // if maximum -> max(val, dat) // return max(val, dat); // if cumulative sum return val + dat; } ll query(ll i) { /* e.g.) i = 10101 itr1. 10101 -> 10100 itr2. 10100 -> 10000 itr3. 10000 -> 00000 (break) */ ll ret = 0; for (ll j = i; j >= 0; j = (j & (j + 1)) - 1) { ret = update_func(ret, tree_dat[j]); } return ret; } ll lower_bound(ll key) { 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 && tree_dat[left + i - 1] < key) { key -= tree_dat[left + i - 1]; left += i; } } return left; } void update(ll i, ll val) { /* e.g.) i = 10101, n = 11111 itr1. 10101 -> 10110 itr2. 10110 -> 11000 itr3. 11000 -> 100000 (break) */ if (i < 0) return; for (ll j = i; j < n; j |= j + 1) { tree_dat[j] = update_func(val, tree_dat[j]); } } }; 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_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 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 comb_memo[55][55]; ll pascal_triangle(ll n) { comb_memo[0][0] = 1; For(i, 1, n + 1) rep(j, i + 1) { comb_memo[i][j] += comb_memo[i - 1][j]; if (j > 0) comb_memo[i][j] += comb_memo[i - 1][j - 1]; } } 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; } ll combination_memo(ll n, ll r, ll pre, ll mod) { // pre = nCr-1 // return nCr if (n == r && n == 0) return 1; else if (n <= 0 || r < 0 || r > n) return 0; ll numerator = ll(n - r + 1) % mod; ll denomenator = modinv(r, mod) % mod; ll val = (numerator * denomenator) % mod; val *= pre; return val % mod; } ll combination_no_mod(ll n, ll r) { 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++) { numerator *= n - i; denomenator *= i + 1; ll g = gcd(numerator, denomenator); numerator /= g; denomenator /= g; } return numerator; } 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; } ll bin_search(ll key, ll A[], ll left, ll right) { // return the index idx where A[idx] = key. // A[left] is start and A[right] is end.. // In other words, A[right], not A[right - 1], must be defined. while (right >= left) { ll mid = left + (right - left) / 2; if (A[mid] == key) return mid; else if (A[mid] > key) right = mid - 1; else if (A[mid] < key) left = mid + 1; } return -1; } /* ll bin_search_temp(ll left, ll right, callable judge){ while(right > left){ // when seeking lower bound ll mid = (right + left) / 2; if (judge(mid)) right = mid; else left = mid + 1; // when seeking upper bound ll mid = (right + left + 1) / 2; if (judge(mid)) left = mid; else right = mid - 1; } return right; } trinary_search // Care the value EPS!!! Compare to the condition ld left = 0; ld right = p; while(abs(right - left) > EPS){ ld left2 = (2 * left + right) / 3.0; ld right2 = (left + 2 * right) / 3.0; ld f1 = tak_func(left2); ld f2 = tak_func(right2); if (f1 <= f2) right = right2; else if (f2 <= f1) left = left2; } */ ll lower_bound_bin_search_temp(ll key, ll A[], ll left, ll right) { // Pay attention to the return value -1. // For example, sometimes you may want the value right instead of -1. if (A[left] >= key) return left; ll mid = left + (right - left) / 2; while (right >= left) { mid = left + (right - left) / 2; if (mid == left) { if (A[left] < key && key <= A[left + 1]) return left + 1; else return -1; } if (A[mid - 1] < key && key <= A[mid]) return mid; else if (A[mid - 1] >= key) right = mid - 1; else if (A[mid] < key) left = mid; } return -1; // all the elements < key } ll inf_bin_search_temp(ll key, ll A[], ll left, ll right) { // Pay attention to the return value -1. // For example, sometimes you may want the value right instead of -1. if (A[left] > key) return left; ll mid = left + (right - left) / 2; while (right >= left) { mid = left + (right - left) / 2; if (mid == left) { if (A[left] <= key && key < A[left + 1]) return left + 1; else return -1; } if (A[mid - 1] <= key && key < A[mid]) return mid; else if (A[mid - 1] > key) right = mid - 1; else if (A[mid] <= key) left = mid; } return -1; // all the elements <= key } ll upper_bound_bin_search_temp(ll key, ll A[], ll left, ll right) { // Pay attention to the return value -1. // For example, sometimes you may want the value left instead of -1. if (A[right] <= key) return right; ll mid = left + (right - left) / 2; while (right >= left) { mid = left + (right - left) / 2; if (mid == right) { if (A[right - 1] <= key && key < A[right]) return right - 1; else return -1; } if (A[mid] <= key && key < A[mid + 1]) return mid; else if (A[mid] > key) right = mid; else if (A[mid + 1] <= key) left = mid + 1; } return -1; // all the elements > key } ll sup_bin_search_temp(ll key, ll A[], ll left, ll right) { // Pay attention to the return value -1. // For example, sometimes you may want the value left instead of -1. if (A[right] < key) return right; ll mid = left + (right - left) / 2; while (right >= left) { mid = left + (right - left) / 2; if (mid == right) { if (A[right - 1] < key && key <= A[right]) return right - 1; else return -1; } if (A[mid] < key && key <= A[mid + 1]) return mid; else if (A[mid] >= key) right = mid; else if (A[mid + 1] < key) left = mid + 1; } return -1; // all the elements >= key } ll bin_search_vector(ll key, vl v, ll left, ll right) { // return the index idx where v[idx] = key. // v[left] is start and v[right] is end.. // In other words, v[right], not v[right - 1], must be defined. while (right >= left) { ll mid = left + (right - left) / 2; if (v[mid] == key) return mid; else if (v[mid] > key) right = mid - 1; else if (v[mid] < key) left = mid + 1; } return -1; } ll lower_bound_bin_search_vector(ll key, vl v) { // the return value N satisfies // v[N - 1] < key <= v[N] // N == -1 if all the elements < key return lower_bound(all(v), key) - v.begin(); } ll inf_bin_search_vector(ll key, vl v) { // the return value N satisfies // v[N - 1] <= key < v[N] <= key + 1 // N == -1 if all the elements <= key return lower_bound(all(v), key + 1) - v.begin(); } ll upper_bound_bin_search_vector(ll key, vl v) { // the return value N satisfies // v[N] <= key < v[N + 1] // N == -1 if all the elements > key return upper_bound(all(v), key) - v.begin(); // (- 1) } ll sup_bin_search_vector(ll key, vl v) { // the return value N satisfies // v[N] <= key - 1 < key <= v[N + 1] // N == -1 if all the elements >= key return upper_bound(all(v), key - 1) - v.begin() - 1; } ll fact(ll n) { if (n == 0) return 1; return n * fact(n - 1); } ll fact_mod(ll n, ll mod) { if (n == 0) return 1; return n * fact_mod(n - 1, mod); } 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; } ll bool_sum(ll a1, ll a2) { if (a1 == 1 || a2 == 1) return 1; return 0; } mll prime_factorization(ll n) { ll i = 2; mll 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; } ll char_to_idx(char c) { ll idx = 0; Forchar(cc, 'a', 'z') { if (c == cc) return idx; else idx++; } } ll next_combination(ll sub) { /* ll n, k; ll bit = (1 << k) - 1; for (; bit < (1 << n); bit = next_combination(bit)){ bool ith = bit & (1 << i); procedures... } */ ll x = sub & -sub, y = sub + x; return (((sub & ~y) / x) >> 1) | y; } vl z_algorithm(str s) { ll n = s.length(); vl res(n); res[0] = n; ll i1 = 1, i2 = 0; while (i1 < n) { while (i1 + i2 < n && s[i2] == s[i1 + i2]) ++i2; res[i1] = i2; if (i2 == 0) { ++i1; continue; } ll i3 = 1; while (i1 + i3 < n && i3 + res[i3] < i2) { res[i1 + i3] = res[i3]; ++i3; } i1 += i3, i2 -= i3; } return res; } ll big_number_mod(str s, ll mod) { ll l = s.length(); ll idx = 0; ll val = 0; ll tenth = 1; while (idx < l) { ll m = ctol(s[l - 1 - idx]); val += (m * tenth) % mod; val %= mod; tenth *= 10; tenth %= mod; idx++; } return val; } str bin_expression(ll n, ll dig) { str s = ""; rep(i, dig) { s += ltos(n % 2); n /= 2; } reverse(all(s)); return s; } ll string_to_ll(str s) { ll l = s.length(); ll idx = 0; ll val = 0; ll tenth = 1; while (idx < l) { ll m = ctol(s[l - 1 - idx]); val += (m * tenth); tenth *= 10; idx++; } return val; } str reflected_string(str s) { str 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; } 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> center_2det_of_3points(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 c2x_2 = x2 + x1, c2y_2 = y2 + y1; ll c3x_2 = x3 + x1, c3y_2 = y3 + y1; ll b2y = x2 - x1, b2x = -y2 + y1; ll b3y = x3 - x1, b3x = -y3 + y1; ll x1_2 = c3x_2 * b3y - c3y_2 * b3x, y1_2 = c2x_2 * b2y - c2y_2 * b2x; ll det = -b3y * b2x + b3x * b2y; if (det == 0) return {{INF, INF}, det}; ll cx_2det = -b2x * x1_2 + b3x * y1_2, cy_2det = -b2y * x1_2 + b3y * y1_2; return {{cx_2det, cy_2det}, det}; } 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. */ BIT 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; } 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; } /* const ll N_VERTEX = 310; ll a, b, t; ll dist[N_VERTEX][N_VERTEX]; void warshall_floyd(ll n){ // 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]); } int main(void){ in2(n, m); rep(i, n) rep(j, n) dist[i][j] = INF * (i != j); rep(i, m){ in3(a, b, t); a--; b--; dist[a][b] = t; dist[b][a] = t; } warshall_floyd(n); } ll dist[1005], vertex_pre[1005]; void dijkstra(ll start, ll n) { priority_queue<pl, vector<pl>, greater<pl>> edge_costs; fill(dist, dist + n, INF); // fill(vertex_pre, vertex_pre + n, -1); dist[start] = 0; edge_costs.push(pl(0, start)); while (!edge_costs.empty()) { pl edge_cost = edge_costs.top(); edge_costs.pop(); ll idx = edge_cost.second; ll cost = edge_cost.first; if (dist[idx] < cost) continue; rep(i, graph[idx].size()){ edge e = graph[idx][i]; if (dist[e.to] > dist[idx] + e.cost){ dist[e.to] = dist[idx] + e.cost; // vertex_pre[e.to] = idx; edge_costs.push(pl(dist[e.to], e.to)); } } } } vl get_predecessor(ll g){ vl path; for (; g != -1; g = vertex_pre[g]) path.pb(g); reverse(all(path)); return path; } int main(void){ in2(n, m); rep(i, m){ in3(a, b, t); a--; b--; G[a].pb({b, t}); G[b].pb({a, t}); } dijkstra(0, n); } # ABC061D bool find_negative_cycle(ll goal){ 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 n, ll goal){ // 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) fill(dist, dist + 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(goal)) return false; return true; } */ /* # 1. The usage of map pair map<pl, ll> cnt; cnt[{i, j}] = 0; items(kv, cnt){ prl2(kv.first, kv.second); } # 2. The usage of next_permutation and combination (factorial search) ll a[8]; rep(i, 8) a[i] = i; sort(a, a + 8); do{ }while(next_permutation(a, a+n)); // here, combination ll n, r; ll i1, i2, ..., ir; For(i1, n - r, n) For(i2, n - r - 1, i1) ... For(ir, n - 2 * r + 1, irr){ process;} # 3. bit search ll n; in1(n); const ll base = 3; ll upper = power_normal(base, n); rep(i, upper){ ll tmp = i; rep(j, n){ rep(k, base) if (tmp % base == k) prl(k); tmp /= base; } } # 4. imos method // used when we would like to count the number which // shows how many times the numbers between l and r belongs to smt. // This method is composed of three process. ll n, m, s[MAX_M], l, r; in2(n, m); rep(i, m) s[i] = 0; // 1st step rep(i, n){ in3(l, r, c); l--; r--; // if l starts from 1. s[l] += c; s[r + 1] -= c; } // 2nd step rep(i, m - 1) s[i + 1] += s[i]; // 3rd step: judgement... #5. shakutori method (syakutori) // 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){ r = max(r, l); if (l == r) r++; while(r < n && cond) r++; answer += r - l; l++; } prl(answer); #6. priority queue pq q; ll answer = 0; ll v; rep(i, n) q.push(a[i]); rep(i, m){ v = q.top(); q.pop(); // get the top value and dump the value from queue v /= 2; q.push(v); // add the new value } while(!q.empty()){ answer += q.top(); q.pop(); } #7. The shortest path (distance) between the k-th edge and another edge (Tree) ll depth[MAX_N]; gr tree[MAX_N]; void dfs(ll source, ll parent, ll all_cost){ depth[source] = all_cost; items(e, tree[source]){ if (e.to == parent) continue; dfs(e.to, source, all_cost + e.cost); } } ll n, k, a, b, c; in2(n, k); rep(i, n - 1){ in3(a, b, c); a--; b--; tree[a].pb({b, c}); tree[b].pb({a, c}); } k--; dfs(k, -1, 0); #10. Visiting Subtree using recurrent function (ABC138D) gr tree[MAX_N]; ll c[MAX_N]; bool visited[MAX_N]; void dfs(ll source, ll parent, ll val){ visited[source] = true; c[source] += val; rep(i, tree[source].size()){ rep(i, m){ll res = n % match[i].e1;} ll vertex = tree[source][i].to; if (vertex == parent) continue; dfs(vertex, source, c[source]); } } #11. bfs ABC146D, ABC007C 1. first create a tree. 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. queue<pl> q; void bfs(ll source, ll parents){ ll n_edge = G[source].size(); if (parents != -1) dist[source] = min(dist[source], dist[parents] + 1); if (visited[source]) return; visited[source] = true; rep(idx, n_edge){ ll target = G[source][idx].to; if (target == parents) continue; q.push(mp(target, source)); } } q.push(mp(sg.e1, -1)); while(!q.empty()){ pl source = q.front(); q.pop(); bfs(source.e1, source.e2); } #12. grid to distance matrix (dx, dy) ll w, h; ll pos_to_idx(ll x, ll y){ return y * w + x; } pl idx_to_pos(ll idx){ return mp(idx % w, idx / w); } int main(void){ in2(h, w); rep(y, h){ in1(s); rep(x, w){ if (s[x] == '#') wall[x][y] = true; else wall[x][y] = false; } } rep(i1, h * w)rep(i2, h * w) dist[i1][i2] = INF * (i1 != i2); rep(x, w)rep(y, h){ ll idx1 = pos_to_idx(x, y); ll idx2; if (wall[x][y]) continue; if (x != 0 && !wall[x - 1][y]){ idx2 = pos_to_idx(x - 1, y); // if warshall floyd dist[idx1][idx2] = 1; // if dijkstra // graph[idx1].pb({idx2, 1}); } if (x != w - 1 && !wall[x + 1][y]){ idx2 = pos_to_idx(x + 1, y); dist[idx1][idx2] = 1; // graph[idx1].pb({idx2, 1}); } if (y != 0 && !wall[x][y - 1]){ idx2 = pos_to_idx(x, y - 1); dist[idx1][idx2] = 1; // graph[idx1].pb({idx2, 1}); } if (y != h - 1 && !wall[x][y + 1]){ idx2 = pos_to_idx(x, y + 1); dist[idx1][idx2] = 1; // graph[idx1].pb({idx2, 1}); } } } */ /* # the operators regarding bit & (AND), | (OR), ^ (XOR) - (REVERSE), >> (SMALLER SHIFT) << (BIGGER SHIFT) x1: 0000 0001 0010 0101 0110 0111 0111 x2: xxxx 0001 0011 0100 0101 1000 0110 x1 & x2: 0000 0001 0010 0100 0100 0000 0110 x: 1001 1010 1100 1011 1101 1111 x & - x: 0001 0010 0100 0001 0001 0001 sum: 1010 1100 10000 1100 1100 10000 x << y is x * 2 ** y x >> y is rep(i, y) x = x // 2 Let S be a bit sequence and i be a non-negative integer S & (1 << i) -> if true, i in S S | (1 << i) -> S union {i} S & -(1 << i) -> S - {i} __builtin_popcount(S) -> the number of elements in S S = 0 -> S is an empty set S = (1 << n) - 1 -> S includes all the elements up to the n-th #Conditional Operator condition ? true : false; #iterator type declaration: auto value reference: *itr increment: itr++ decrement: itr-- substitution of value: *itr = smt */ const ll MAX_N = 200005; bool okay = false; ll answer = 0; using cedge = struct { ll to; ll cost; ll color; }; vector<cedge> tree[MAX_N]; class LCA { public: ll N, logN; vl depth, len; vector<vl> parents; LCA(ll n) { N = n; logN = 0; while (N > (1LL << logN)) logN++; depth = vl(N); len = vl(N); parents = vector<vl>(logN, vl(N)); 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]; } }; ll n, q, a, b, c, d; ll x, y, u, v, w; using queries = struct { ll col; ll cost; ll from; ll to; ll anc; }; vector<queries> qs; vl ans[MAX_N]; set<ll> required_colors[MAX_N]; map<pl, ll> cmp, dmp; map<ll, ll> cmp_test, dmp_test; const ll K = 1e5 + 5; ll dists[MAX_N]; void dfs(ll source, ll parent, ll dist) { dists[source] = dist; for (ll c : required_colors[source]) { cmp[{source, c}] = cmp_test[c]; dmp[{source, c}] = dmp_test[c]; } ll target, c, d; for (cedge e : tree[source]) { target = e.to, c = e.color, d = e.cost; if (target == parent) continue; dist += d; cmp_test[c]++; dmp_test[c] += d; dfs(target, source, dist); dist -= d; cmp_test[c]--; dmp_test[c] -= d; } } void solve() { rep(i, n - 1) { in4(a, b, c, d); a--; b--; c--; tree[a].pb({b, d, c}); tree[b].pb({a, d, c}); } LCA lca(n); qs = vector<queries>(q); rep(i, q) { in4(x, y, u, v); x--; u--; v--; w = lca.query(u, v); qs[i] = {x, y, u, v, w}; required_colors[u].insert(x); required_colors[v].insert(x); required_colors[w].insert(x); } dfs(0, -1, 0); rep(i, q) { x = qs[i].col, y = qs[i].cost, u = qs[i].from, v = qs[i].to, w = qs[i].anc; answer = lca.distance(u, v); answer -= dmp[{u, x}] + dmp[{v, x}] - 2 * dmp[{w, x}]; answer += y * (cmp[{u, x}] + cmp[{v, x}] - 2 * cmp[{w, x}]); prl(answer); } // check negative MOD // check index flow // check overwrite of the input variables } int main(void) { in2(n, q); // assert(n <= 400); solve(); return 0; }
insert
1,367
1,367
1,367
1,368
-11
p02986
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define y1 y11 #define fi first #define se second #define pi acos(-1.0) #define LL long long // #define mp make_pair #define pb emplace_back #define ls rt << 1, l, m #define rs rt << 1 | 1, m + 1, r #define ULL unsigned LL #define pll pair<LL, LL> #define pli pair<LL, int> #define pii pair<int, int> #define piii pair<pii, int> #define pdd pair<double, double> #define mem(a, b) memset(a, b, sizeof(a)) #define debug(x) cerr << #x << " = " << x << "\n"; #define add(x) (x > MOD ? x - MOD : (x < 0 ? x + MOD : x)) #define fio \ ios::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); // head const int N = 1 + 5; vector<tuple<int, int, int>> g[N]; vector<tuple<int, int, int, int>> vc[N]; int n, m, a, b, c, d, x, y, u, v; int anc[N][18], deep[N], len[N], ans[N]; int cur_cnt[N], cur_sum[N]; void dfs(int u, int o) { deep[u] = deep[o] + 1; anc[u][0] = o; for (int i = 1; i < 18; ++i) anc[u][i] = anc[anc[u][i - 1]][i - 1]; for (auto t : g[u]) { int v = get<0>(t), w = get<2>(t); if (v != o) { len[v] = len[u] + w; dfs(v, u); } } } void DFS(int u, int o) { for (int i = 0; i < vc[u].size(); ++i) { auto t = vc[u][i]; int id = get<0>(t), c = get<1>(t), w = get<2>(t), f = get<3>(t); ans[id] -= f * cur_sum[c]; ans[id] += f * cur_cnt[c] * w; } for (auto t : g[u]) { int v = get<0>(t), c = get<1>(t), w = get<2>(t); if (v != o) { cur_cnt[c]++; cur_sum[c] += w; DFS(v, u); cur_cnt[c]--; cur_sum[c] -= w; } } } inline int lca(int u, int v) { if (deep[u] < deep[v]) swap(u, v); for (int i = 17; i >= 0; --i) if (deep[anc[u][i]] >= deep[v]) u = anc[u][i]; if (u == v) return u; for (int i = 17; i >= 0; --i) if (anc[u][i] != anc[v][i]) u = anc[u][i], v = anc[v][i]; return anc[u][0]; } int main() { // freopen("input.in", "rt", stdin); scanf("%d %d", &n, &m); for (int i = 1; i < n; ++i) scanf("%d %d %d %d", &a, &b, &c, &d), g[a].pb(b, c, d), g[b].pb(a, c, d); dfs(1, 1); for (int i = 1; i <= m; ++i) { scanf("%d %d %d %d", &x, &y, &u, &v); int a = lca(u, v); ans[i] = len[u] + len[v] - 2 * len[a]; vc[u].pb(i, x, y, 1); vc[v].pb(i, x, y, 1); vc[a].pb(i, x, y, -2); } DFS(1, 1); for (int i = 1; i <= m; ++i) printf("%d\n", ans[i]); return 0; }
#include <bits/stdc++.h> using namespace std; #define y1 y11 #define fi first #define se second #define pi acos(-1.0) #define LL long long // #define mp make_pair #define pb emplace_back #define ls rt << 1, l, m #define rs rt << 1 | 1, m + 1, r #define ULL unsigned LL #define pll pair<LL, LL> #define pli pair<LL, int> #define pii pair<int, int> #define piii pair<pii, int> #define pdd pair<double, double> #define mem(a, b) memset(a, b, sizeof(a)) #define debug(x) cerr << #x << " = " << x << "\n"; #define add(x) (x > MOD ? x - MOD : (x < 0 ? x + MOD : x)) #define fio \ ios::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); // head const int N = 1e5 + 5; vector<tuple<int, int, int>> g[N]; vector<tuple<int, int, int, int>> vc[N]; int n, m, a, b, c, d, x, y, u, v; int anc[N][18], deep[N], len[N], ans[N]; int cur_cnt[N], cur_sum[N]; void dfs(int u, int o) { deep[u] = deep[o] + 1; anc[u][0] = o; for (int i = 1; i < 18; ++i) anc[u][i] = anc[anc[u][i - 1]][i - 1]; for (auto t : g[u]) { int v = get<0>(t), w = get<2>(t); if (v != o) { len[v] = len[u] + w; dfs(v, u); } } } void DFS(int u, int o) { for (int i = 0; i < vc[u].size(); ++i) { auto t = vc[u][i]; int id = get<0>(t), c = get<1>(t), w = get<2>(t), f = get<3>(t); ans[id] -= f * cur_sum[c]; ans[id] += f * cur_cnt[c] * w; } for (auto t : g[u]) { int v = get<0>(t), c = get<1>(t), w = get<2>(t); if (v != o) { cur_cnt[c]++; cur_sum[c] += w; DFS(v, u); cur_cnt[c]--; cur_sum[c] -= w; } } } inline int lca(int u, int v) { if (deep[u] < deep[v]) swap(u, v); for (int i = 17; i >= 0; --i) if (deep[anc[u][i]] >= deep[v]) u = anc[u][i]; if (u == v) return u; for (int i = 17; i >= 0; --i) if (anc[u][i] != anc[v][i]) u = anc[u][i], v = anc[v][i]; return anc[u][0]; } int main() { // freopen("input.in", "rt", stdin); scanf("%d %d", &n, &m); for (int i = 1; i < n; ++i) scanf("%d %d %d %d", &a, &b, &c, &d), g[a].pb(b, c, d), g[b].pb(a, c, d); dfs(1, 1); for (int i = 1; i <= m; ++i) { scanf("%d %d %d %d", &x, &y, &u, &v); int a = lca(u, v); ans[i] = len[u] + len[v] - 2 * len[a]; vc[u].pb(i, x, y, 1); vc[v].pb(i, x, y, 1); vc[a].pb(i, x, y, -2); } DFS(1, 1); for (int i = 1; i <= m; ++i) printf("%d\n", ans[i]); return 0; }
replace
26
27
26
27
0
p02986
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define reint register int #define coint const int using namespace std; typedef long long ll; coint SIZE = 100000 + 5; struct Edge { int v, w, head; }; int ecnt, head[SIZE]; Edge edge[SIZE << 1]; inline void addEdge(coint u, coint v, coint w) { edge[++ecnt] = (Edge){v, w, head[u]}, head[u] = ecnt; return; } int tick, dfn[SIZE], post[SIZE], bel[SIZE]; int hfa[SIZE], hson[SIZE], sz[SIZE], dep[SIZE], fa[SIZE]; ll dis[SIZE]; struct BinaryIndexedTree { ll li[SIZE]; inline int lowbit(coint num) { return num & (-num); } BinaryIndexedTree(void) { memset(li, 0, sizeof li); return; } inline void modify(coint ind, coint k) { for (reint i = ind; i < SIZE; i += lowbit(i)) li[i] += k; return; } inline ll query(coint ind) { register ll res = 0; for (reint i = ind; i; i -= lowbit(i)) res += li[i]; return res; } }; BinaryIndexedTree sgtr1, sgtr2; void dfs(coint u) { dfn[u] = ++tick, bel[tick] = u; for (int i = head[u]; ~i; i = edge[i].head) { coint v = edge[i].v; if (v == fa[u]) continue; dis[v] = dis[u] + edge[i].w, fa[v] = u, dep[v] = dep[u] + 1; dfs(v), sz[u] += sz[v]; if (sz[hson[u]] < sz[v]) hson[u] = v; } hfa[hson[u]] = u, post[u] = tick; return; } void dfs2(coint u) { hfa[u] = (hfa[u] ? hfa[fa[u]] : u); for (int i = head[u]; ~i; i = edge[i].head) { coint v = edge[i].v; if (v == fa[u]) continue; dfs2(v); } return; } inline int lca(reint a, reint b) { reint t; for (; hfa[a] != hfa[b]; b = fa[hfa[b]]) if (dep[hfa[a]] > dep[hfa[b]]) t = a, a = b, b = t; return dep[a] > dep[b] ? b : a; } inline int getint(void) { reint x = 0, ch = getchar(); for (; ch < '0' || ch > '9'; ch = getchar()) ; for (; ch >= '0' && ch <= '9'; x = (x << 1) + (x << 3) + (ch - '0'), ch = getchar()) ; return x; } struct Query { int b, c, d, ind; }; vector<int> vec1[SIZE], vec2[SIZE], vec3[SIZE]; vector<Query> query[SIZE]; ll ans[SIZE]; int n, q; inline ll queryxy(coint x, coint y, BinaryIndexedTree &bit) { coint xlca = lca(x, y); return bit.query(dfn[x]) + bit.query(dfn[y]) - (bit.query(dfn[xlca]) << 1); } int main(void) { ecnt = 0, memset(head, -1, sizeof head); n = getint(), q = getint(); for (reint i = 1; i < n; ++i) { coint u = getint(), v = getint(), a = getint(), b = getint(); vec1[a].push_back(u), vec2[a].push_back(v), vec3[a].push_back(b); addEdge(u, v, b), addEdge(v, u, b); } dfs(1), dfs2(1); for (reint i = 1; i <= q; ++i) { coint x = getint(), y = getint(), u = getint(), v = getint(); query[x].push_back((Query){y, u, v, i}); } for (reint i = 1; i < n; ++i) { coint szq = query[i].size(), sze = vec1[i].size(); for (reint j = 0; j < sze; ++j) { int u = vec1[i][j], v = vec2[i][j], w = vec3[i][j]; if (dep[u] > dep[v]) swap(u, v); sgtr1.modify(dfn[v], -w), sgtr1.modify(post[v] + 1, w); sgtr2.modify(dfn[v], 1), sgtr2.modify(post[v] + 1, -1); } for (reint j = 0; j < szq; ++j) { coint y = query[i][j].b, u = query[i][j].c, v = query[i][j].d; coint ind = query[i][j].ind; ans[ind] = queryxy(u, v, sgtr1) + queryxy(u, v, sgtr2) * y + dis[u] + dis[v] - (dis[lca(u, v)] << 1); } for (reint j = 0; j < sze; ++j) { int u = vec1[i][j], v = vec2[i][j], w = vec3[i][j]; if (dep[u] > dep[v]) swap(u, v); sgtr1.modify(dfn[v], w), sgtr1.modify(post[v] + 1, -w); sgtr2.modify(dfn[v], -1), sgtr2.modify(post[v] + 1, 1); } } for (reint i = 1; i <= q; ++i) printf("%lld\n", ans[i]); return 0; }
#include <bits/stdc++.h> #define reint register int #define coint const int using namespace std; typedef long long ll; coint SIZE = 100000 + 5; struct Edge { int v, w, head; }; int ecnt, head[SIZE]; Edge edge[SIZE << 1]; inline void addEdge(coint u, coint v, coint w) { edge[++ecnt] = (Edge){v, w, head[u]}, head[u] = ecnt; return; } int tick, dfn[SIZE], post[SIZE], bel[SIZE]; int hfa[SIZE], hson[SIZE], sz[SIZE], dep[SIZE], fa[SIZE]; ll dis[SIZE]; struct BinaryIndexedTree { ll li[SIZE]; inline int lowbit(coint num) { return num & (-num); } BinaryIndexedTree(void) { memset(li, 0, sizeof li); return; } inline void modify(coint ind, coint k) { for (reint i = ind; i < SIZE; i += lowbit(i)) li[i] += k; return; } inline ll query(coint ind) { register ll res = 0; for (reint i = ind; i; i -= lowbit(i)) res += li[i]; return res; } }; BinaryIndexedTree sgtr1, sgtr2; void dfs(coint u) { dfn[u] = ++tick, bel[tick] = u, sz[u] = 1; for (int i = head[u]; ~i; i = edge[i].head) { coint v = edge[i].v; if (v == fa[u]) continue; dis[v] = dis[u] + edge[i].w, fa[v] = u, dep[v] = dep[u] + 1; dfs(v), sz[u] += sz[v]; if (sz[hson[u]] < sz[v]) hson[u] = v; } hfa[hson[u]] = u, post[u] = tick; return; } void dfs2(coint u) { hfa[u] = (hfa[u] ? hfa[fa[u]] : u); for (int i = head[u]; ~i; i = edge[i].head) { coint v = edge[i].v; if (v == fa[u]) continue; dfs2(v); } return; } inline int lca(reint a, reint b) { reint t; for (; hfa[a] != hfa[b]; b = fa[hfa[b]]) if (dep[hfa[a]] > dep[hfa[b]]) t = a, a = b, b = t; return dep[a] > dep[b] ? b : a; } inline int getint(void) { reint x = 0, ch = getchar(); for (; ch < '0' || ch > '9'; ch = getchar()) ; for (; ch >= '0' && ch <= '9'; x = (x << 1) + (x << 3) + (ch - '0'), ch = getchar()) ; return x; } struct Query { int b, c, d, ind; }; vector<int> vec1[SIZE], vec2[SIZE], vec3[SIZE]; vector<Query> query[SIZE]; ll ans[SIZE]; int n, q; inline ll queryxy(coint x, coint y, BinaryIndexedTree &bit) { coint xlca = lca(x, y); return bit.query(dfn[x]) + bit.query(dfn[y]) - (bit.query(dfn[xlca]) << 1); } int main(void) { ecnt = 0, memset(head, -1, sizeof head); n = getint(), q = getint(); for (reint i = 1; i < n; ++i) { coint u = getint(), v = getint(), a = getint(), b = getint(); vec1[a].push_back(u), vec2[a].push_back(v), vec3[a].push_back(b); addEdge(u, v, b), addEdge(v, u, b); } dfs(1), dfs2(1); for (reint i = 1; i <= q; ++i) { coint x = getint(), y = getint(), u = getint(), v = getint(); query[x].push_back((Query){y, u, v, i}); } for (reint i = 1; i < n; ++i) { coint szq = query[i].size(), sze = vec1[i].size(); for (reint j = 0; j < sze; ++j) { int u = vec1[i][j], v = vec2[i][j], w = vec3[i][j]; if (dep[u] > dep[v]) swap(u, v); sgtr1.modify(dfn[v], -w), sgtr1.modify(post[v] + 1, w); sgtr2.modify(dfn[v], 1), sgtr2.modify(post[v] + 1, -1); } for (reint j = 0; j < szq; ++j) { coint y = query[i][j].b, u = query[i][j].c, v = query[i][j].d; coint ind = query[i][j].ind; ans[ind] = queryxy(u, v, sgtr1) + queryxy(u, v, sgtr2) * y + dis[u] + dis[v] - (dis[lca(u, v)] << 1); } for (reint j = 0; j < sze; ++j) { int u = vec1[i][j], v = vec2[i][j], w = vec3[i][j]; if (dep[u] > dep[v]) swap(u, v); sgtr1.modify(dfn[v], w), sgtr1.modify(post[v] + 1, -w); sgtr2.modify(dfn[v], -1), sgtr2.modify(post[v] + 1, 1); } } for (reint i = 1; i <= q; ++i) printf("%lld\n", ans[i]); return 0; }
replace
54
55
54
55
TLE
p02986
C++
Runtime Error
#pragma optimization_level 3 #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math") #include <bits/stdc++.h> #define F first #define S second #define vec vector #define ms multiset #define pb push_back #define pll pair<ll, ll> #define pdd pair<ld, ld> #define pq priority_queue #define umap unordered_map #define uset unordered_set #define pii pair<int, int> #define pnn pair<Node *, Node *> #define uid uniform_int_distribution #define FILE \ ifstream in("board.in"); \ ofstream out("board.out"); #define fast \ cin.tie(0); \ cout.tie(0); \ cin.sync_with_stdio(0); \ cout.sync_with_stdio(0); using namespace std; typedef string str; typedef long long ll; typedef long double ld; mt19937 rnd(chrono::steady_clock::now() .time_since_epoch() .count()); // uid<int> u1(5, 10); u1(rnd); struct edg { int y, c, d; edg(int a, int b, int cc) { y = a; c = b; d = cc; } }; struct Node { Node *l, *r; pii p; Node(Node *a, Node *b) { l = a; r = b; p = {a->p.F + b->p.F, a->p.S + b->p.S}; } Node(Node *a, Node *b, pii pp) { l = a; r = b; p = pp; } Node(Node *n) { l = n->l; r = n->r; p = n->p; } }; Node *add(int l, int r, int pos, Node *n, int val) { Node *cp = n ? new Node(n) : new Node(0, 0, {0, 0}); if (l == r) { cp->p.F++; cp->p.S += val; return cp; } int md = (l + r) / 2; if (pos <= md) cp->l = add(l, md, pos, cp->l, val); else cp->r = add(md + 1, r, pos, cp->r, val); return cp; } pii get(int l, int r, int pos, Node *n) { if (!n) return {0, 0}; if (l == r) return n->p; int md = (l + r) / 2; if (pos <= md) return get(l, md, pos, n->l); else return get(md + 1, r, pos, n->r); } int a, z, SIZE = 1e5 + 5; vec<vec<edg>> l; vec<vec<int>> n; vec<int> cl, dl, prs, dep, mx; vec<Node *> m; void init(int v, int pr, int c, int d, int depp) { cl[v] = c; dl[v] = d; dep[v] = depp; n[0][v] = pr; if (pr != -1) { m[v] = add(0, SIZE, c, m[pr], d); prs[v] = prs[pr] + d; } for (edg h : l[v]) { if (h.y == pr) continue; init(h.y, v, h.c, h.d, depp + 1); } } int lca(int x, int y) { if (dep[x] > dep[y]) swap(x, y); for (int d = dep[y] - dep[x]; d; d -= 1 << mx[d]) y = n[mx[d]][y]; for (int q = 16; q >= 0; q--) { int px = n[q][x], py = n[q][y]; if (px != py || q == 0) x = px, y = py; } return x == y ? x : n[0][x]; } int main() { fast; mx = vec<int>(SIZE); for (int q = 2; q < SIZE; q++) mx[q] = 2 << mx[q - 1] > q ? mx[q - 1] : mx[q - 1] + 1; cin >> a >> z; m = vec<Node *>(a); dep = vec<int>(a); cl = vec<int>(a); dl = vec<int>(a); prs = vec<int>(a); n = vec<vec<int>>(17, vec<int>(a)); l = vec<vec<edg>>(a, vec<edg>()); for (int q = 0; q < a - 1; q++) { int x, y, c, d; cin >> x >> y >> c >> d; x--; y--; l[x].pb(edg(y, c, d)); l[y].pb(edg(x, c, d)); } m[0] = new Node(0, 0, {0, 0}); init(0, -1, 0, 0, 0); for (int q = 1; q < 17; q++) { for (int w = 0; w < a; w++) { n[q][w] = n[q - 1][w] == -1 ? -1 : n[q - 1][n[q - 1][w]]; } } for (; z; z--) { int x, y, c, d; cin >> c >> d >> x >> y; x--; y--; int lc = lca(x, y); int sum = prs[x] + prs[y] - 2 * prs[lc]; pii px = get(0, SIZE, c, m[x]); pii py = get(0, SIZE, c, m[y]); pii pl = get(0, SIZE, c, m[lc]); pii po = {px.F + py.F - 2 * pl.F, px.S + py.S - 2 * pl.S}; cout << sum - po.S + po.F * d << "\n"; } }
#pragma optimization_level 3 #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math") #include <bits/stdc++.h> #define F first #define S second #define vec vector #define ms multiset #define pb push_back #define pll pair<ll, ll> #define pdd pair<ld, ld> #define pq priority_queue #define umap unordered_map #define uset unordered_set #define pii pair<int, int> #define pnn pair<Node *, Node *> #define uid uniform_int_distribution #define FILE \ ifstream in("board.in"); \ ofstream out("board.out"); #define fast \ cin.tie(0); \ cout.tie(0); \ cin.sync_with_stdio(0); \ cout.sync_with_stdio(0); using namespace std; typedef string str; typedef long long ll; typedef long double ld; mt19937 rnd(chrono::steady_clock::now() .time_since_epoch() .count()); // uid<int> u1(5, 10); u1(rnd); struct edg { int y, c, d; edg(int a, int b, int cc) { y = a; c = b; d = cc; } }; struct Node { Node *l, *r; pii p; Node(Node *a, Node *b) { l = a; r = b; p = {a->p.F + b->p.F, a->p.S + b->p.S}; } Node(Node *a, Node *b, pii pp) { l = a; r = b; p = pp; } Node(Node *n) { l = n->l; r = n->r; p = n->p; } }; Node *add(int l, int r, int pos, Node *n, int val) { Node *cp = n ? new Node(n) : new Node(0, 0, {0, 0}); if (l == r) { cp->p.F++; cp->p.S += val; return cp; } int md = (l + r) / 2; if (pos <= md) cp->l = add(l, md, pos, cp->l, val); else cp->r = add(md + 1, r, pos, cp->r, val); return cp; } pii get(int l, int r, int pos, Node *n) { if (!n) return {0, 0}; if (l == r) return n->p; int md = (l + r) / 2; if (pos <= md) return get(l, md, pos, n->l); else return get(md + 1, r, pos, n->r); } int a, z, SIZE = 1e5 + 5; vec<vec<edg>> l; vec<vec<int>> n; vec<int> cl, dl, prs, dep, mx; vec<Node *> m; void init(int v, int pr, int c, int d, int depp) { cl[v] = c; dl[v] = d; dep[v] = depp; n[0][v] = pr; if (pr != -1) { m[v] = add(0, SIZE, c, m[pr], d); prs[v] = prs[pr] + d; } for (edg h : l[v]) { if (h.y == pr) continue; init(h.y, v, h.c, h.d, depp + 1); } } int lca(int x, int y) { if (dep[x] > dep[y]) swap(x, y); for (int d = dep[y] - dep[x]; d; d -= 1 << mx[d]) y = n[mx[d]][y]; for (int q = 16; x != y && q >= 0; q--) { int px = n[q][x], py = n[q][y]; if (px != py || q == 0) x = px, y = py; } return x == y ? x : n[0][x]; } int main() { fast; mx = vec<int>(SIZE); for (int q = 2; q < SIZE; q++) mx[q] = 2 << mx[q - 1] > q ? mx[q - 1] : mx[q - 1] + 1; cin >> a >> z; m = vec<Node *>(a); dep = vec<int>(a); cl = vec<int>(a); dl = vec<int>(a); prs = vec<int>(a); n = vec<vec<int>>(17, vec<int>(a)); l = vec<vec<edg>>(a, vec<edg>()); for (int q = 0; q < a - 1; q++) { int x, y, c, d; cin >> x >> y >> c >> d; x--; y--; l[x].pb(edg(y, c, d)); l[y].pb(edg(x, c, d)); } m[0] = new Node(0, 0, {0, 0}); init(0, -1, 0, 0, 0); for (int q = 1; q < 17; q++) { for (int w = 0; w < a; w++) { n[q][w] = n[q - 1][w] == -1 ? -1 : n[q - 1][n[q - 1][w]]; } } for (; z; z--) { int x, y, c, d; cin >> c >> d >> x >> y; x--; y--; int lc = lca(x, y); int sum = prs[x] + prs[y] - 2 * prs[lc]; pii px = get(0, SIZE, c, m[x]); pii py = get(0, SIZE, c, m[y]); pii pl = get(0, SIZE, c, m[lc]); pii po = {px.F + py.F - 2 * pl.F, px.S + py.S - 2 * pl.S}; cout << sum - po.S + po.F * d << "\n"; } }
replace
118
119
118
119
-11
p02986
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double const int nax = 1e5 + 5; pair<int, int> arr[nax]; vector<vector<pair<int, pair<int, ll>>>> v(nax); int vis[nax] = {0}; int level[nax]; int parent[nax]; int dp[nax][20]; struct s { int a, b, c, d; }; vector<vector<s>> pre(nax); int ans[nax] = {0}; int find(int x, int d) { for (int i = 19; i >= 0; i--) { if (d < (1 << i)) continue; x = dp[x][i]; d -= (1 << i); } return x; } void dfs(int ci, int l) { vis[ci] = 1; level[ci] = l; if (l == 0) parent[ci] = ci; for (auto x : v[ci]) { if (vis[x.first] == 1) continue; parent[x.first] = ci; dfs(x.first, l + 1); } } int lca(int a, int b) { int p = a; int q = b; if (level[a] > level[b]) { p = b; q = a; } q = find(q, abs(level[a] - level[b])); if (p == q) return p; for (int i = 19; i >= 0; i--) { if (dp[p][i] == dp[q][i]) continue; p = dp[p][i]; q = dp[q][i]; } return dp[p][0]; } void dfs1(int ci, int sum, int col, int val) { vis[ci] = 1; for (auto x : pre[ci]) { if (x.b == 0) ans[x.a] += sum - arr[x.c].second + arr[x.c].first * x.d; else ans[x.a] -= 2 * (sum - arr[x.c].second + arr[x.c].first * x.d); } for (auto x : v[ci]) { if (vis[x.first] == 1) continue; arr[x.second.first].first++; arr[x.second.first].second += x.second.second; dfs1(x.first, sum + x.second.second, x.second.first, x.second.second); } if (ci != 0) { arr[col].first--; arr[col].second -= val; } } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, q; cin >> n >> q; for (int i = 0; i < n - 1; i++) { int a, b, c, d; cin >> a >> b >> c >> d; a--; b--; c--; v[a].push_back({b, {c, d}}); v[b].push_back({a, {c, d}}); arr[i] = {0, 0}; } dfs(0, 0); for (int i = 0; i < n; i++) { vis[i] = 0; dp[i][0] = parent[i]; } for (int j = 1; j < 20; j++) { for (int i = 0; i < n; i++) dp[i][j] = dp[dp[i][j - 1]][j - 1]; } vector<s> query; for (int i = 0; i < q; i++) { int a, b, c, d; cin >> a >> b >> c >> d; a--; c--; d--; pre[c].push_back({i, 0, a, b}); pre[d].push_back({i, 0, a, b}); pre[lca(c, d)].push_back({i, 1, a, b}); } dfs1(0, 0, -1, -1); for (int i = 0; i < q; i++) cout << ans[i] << "\n"; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double const int nax = 1e5 + 5; pair<int, int> arr[nax]; vector<vector<pair<int, pair<int, ll>>>> v(nax); int vis[nax] = {0}; int level[nax]; int parent[nax]; int dp[nax][20]; struct s { int a, b, c, d; }; vector<vector<s>> pre(nax); int ans[nax] = {0}; int find(int x, int d) { for (int i = 19; i >= 0; i--) { if (d < (1 << i)) continue; x = dp[x][i]; d -= (1 << i); } return x; } void dfs(int ci, int l) { vis[ci] = 1; level[ci] = l; if (l == 0) parent[ci] = ci; for (auto x : v[ci]) { if (vis[x.first] == 1) continue; parent[x.first] = ci; dfs(x.first, l + 1); } } int lca(int a, int b) { int p = a; int q = b; if (level[a] > level[b]) { p = b; q = a; } q = find(q, abs(level[a] - level[b])); if (p == q) return p; for (int i = 19; i >= 0; i--) { if (dp[p][i] == dp[q][i]) continue; p = dp[p][i]; q = dp[q][i]; } return dp[p][0]; } void dfs1(int ci, int sum, int col, int val) { vis[ci] = 1; for (auto x : pre[ci]) { if (x.b == 0) ans[x.a] += sum - arr[x.c].second + arr[x.c].first * x.d; else ans[x.a] -= 2 * (sum - arr[x.c].second + arr[x.c].first * x.d); } for (auto x : v[ci]) { if (vis[x.first] == 1) continue; arr[x.second.first].first++; arr[x.second.first].second += x.second.second; dfs1(x.first, sum + x.second.second, x.second.first, x.second.second); } if (ci != 0) { arr[col].first--; arr[col].second -= val; } } int main() { /* #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif */ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, q; cin >> n >> q; for (int i = 0; i < n - 1; i++) { int a, b, c, d; cin >> a >> b >> c >> d; a--; b--; c--; v[a].push_back({b, {c, d}}); v[b].push_back({a, {c, d}}); arr[i] = {0, 0}; } dfs(0, 0); for (int i = 0; i < n; i++) { vis[i] = 0; dp[i][0] = parent[i]; } for (int j = 1; j < 20; j++) { for (int i = 0; i < n; i++) dp[i][j] = dp[dp[i][j - 1]][j - 1]; } vector<s> query; for (int i = 0; i < q; i++) { int a, b, c, d; cin >> a >> b >> c >> d; a--; c--; d--; pre[c].push_back({i, 0, a, b}); pre[d].push_back({i, 0, a, b}); pre[lca(c, d)].push_back({i, 1, a, b}); } dfs1(0, 0, -1, -1); for (int i = 0; i < q; i++) cout << ans[i] << "\n"; }
replace
76
80
76
82
-11
p02986
C++
Runtime Error
#include <bits/stdc++.h> using i64 = long long; struct Edge { int to, c, d; Edge(int to, int c, int d) : to(to), c(c), d(d) {} }; constexpr int size = 1 << 17; i64 dat[size * 2 - 1]; auto query(int a, int b, int k, int l, int r) { if (r <= a || b <= l) return 0ll; if (a <= l && r <= b) return dat[k]; return query(a, b, k * 2 + 1, l, (l + r) / 2) + query(a, b, k * 2 + 2, (l + r) / 2, r); } void update(int i, i64 x) { i += size - 1; dat[i] = x; while (i) { i = (i - 1) / 2; dat[i] = dat[i * 2 + 1] + dat[i * 2 + 2]; } } auto dig(int k) { if (k >= size - 1) return dat[k]; return dat[k] = dig(k * 2 + 1) + dig(k * 2 + 2); } void build(const std::deque<i64> &v) { memset(dat, 0, sizeof(dat)); for (int i = 0; i < v.size(); i++) { dat[i + size - 1] = v[i]; } dig(0); } std::vector<std::deque<Edge>> g; std::vector<std::deque<std::pair<int, int>>> ce; std::deque<i64> euler_tour; std::vector<int> estart, depth; int par[100010][17]; void dfs(int v, int p, int c, int j) { estart[v] = euler_tour.size() - 1; for (auto &e : g[v]) { if (e.to == p) continue; ce[e.c].emplace_back(euler_tour.size(), e.d); euler_tour.push_back(e.d); depth[e.to] = depth[v] + 1; par[e.to][0] = v; dfs(e.to, v, e.c, e.d); } ce[c].emplace_back(euler_tour.size(), -j); euler_tour.push_back(-j); } int main() { int n, q; std::cin >> n >> q; g.resize(n); ce.resize(n); estart.resize(n, -1); depth.resize(n, -1); for (int i = 1; i < n; i++) { int u, v, c, d; std::cin >> u >> v >> c >> d; --u; --v; g[u].emplace_back(v, c, d); g[v].emplace_back(u, c, d); } for (int i = 0; i < n; i++) for (int j = 0; j < 17; j++) par[i][j] = -1; euler_tour.emplace_back(0); dfs(0, -1, 0, 0); build(euler_tour); for (int i = 0; i + 1 < 17; i++) for (int j = 0; j < n; j++) { if (par[j][i] < 0) continue; par[j][i + 1] = par[par[j][i]][i]; } std::vector<std::deque<std::tuple<int, int, int, int>>> cq(n); for (int i = 0; i < q; i++) { int x, y, u, v; std::cin >> x >> y >> u >> v; cq[x].emplace_back(y, u - 1, v - 1, i); } constexpr i64 pp = 10000000000; auto sgn = [](auto t) { return t > 0 ? 1 : -1; }; std::vector<int> ret(q); for (int i = 1; i < n; i++) { for (auto &che : ce[i]) { update(che.first, sgn(che.second) * pp); } for (auto &qq : cq[i]) { auto u = std::get<1>(qq), v = std::get<2>(qq); if (depth[u] > depth[v]) std::swap(u, v); auto diff = depth[v] - depth[u]; for (int i = 16; i >= 0; i--) { if (diff & (1 << i)) v = par[v][i]; } int l = -1, r = n; while (r - l > 1) { auto k = (l + r) / 2; int a = u, b = v; bool flg = a == b; for (int i = 16; !flg && i >= 0; i--) { if ((k & (1 << i)) == 0) continue; a = par[a][i]; b = par[b][i]; if (a == b) flg = true; } if (flg) r = k; else l = k; } int lca = v; for (int i = 16; i >= 0; i--) { if (r & (1 << i)) lca = par[lca][i]; } auto d1 = query(estart[lca] + 1, estart[std::get<1>(qq)] + 1, 0, 0, size), d2 = query(estart[lca] + 1, estart[std::get<2>(qq)] + 1, 0, 0, size); auto dd = d1 % pp + d2 % pp + (d1 / pp + d2 / pp) * std::get<0>(qq); ret[std::get<3>(qq)] = dd; } for (auto &che : ce[i]) { update(che.first, che.second); } } for (auto r : ret) std::cout << r << std::endl; return 0; }
#include <bits/stdc++.h> using i64 = long long; struct Edge { int to, c, d; Edge(int to, int c, int d) : to(to), c(c), d(d) {} }; constexpr int size = 1 << 18; i64 dat[size * 2 - 1]; auto query(int a, int b, int k, int l, int r) { if (r <= a || b <= l) return 0ll; if (a <= l && r <= b) return dat[k]; return query(a, b, k * 2 + 1, l, (l + r) / 2) + query(a, b, k * 2 + 2, (l + r) / 2, r); } void update(int i, i64 x) { i += size - 1; dat[i] = x; while (i) { i = (i - 1) / 2; dat[i] = dat[i * 2 + 1] + dat[i * 2 + 2]; } } auto dig(int k) { if (k >= size - 1) return dat[k]; return dat[k] = dig(k * 2 + 1) + dig(k * 2 + 2); } void build(const std::deque<i64> &v) { memset(dat, 0, sizeof(dat)); for (int i = 0; i < v.size(); i++) { dat[i + size - 1] = v[i]; } dig(0); } std::vector<std::deque<Edge>> g; std::vector<std::deque<std::pair<int, int>>> ce; std::deque<i64> euler_tour; std::vector<int> estart, depth; int par[100010][17]; void dfs(int v, int p, int c, int j) { estart[v] = euler_tour.size() - 1; for (auto &e : g[v]) { if (e.to == p) continue; ce[e.c].emplace_back(euler_tour.size(), e.d); euler_tour.push_back(e.d); depth[e.to] = depth[v] + 1; par[e.to][0] = v; dfs(e.to, v, e.c, e.d); } ce[c].emplace_back(euler_tour.size(), -j); euler_tour.push_back(-j); } int main() { int n, q; std::cin >> n >> q; g.resize(n); ce.resize(n); estart.resize(n, -1); depth.resize(n, -1); for (int i = 1; i < n; i++) { int u, v, c, d; std::cin >> u >> v >> c >> d; --u; --v; g[u].emplace_back(v, c, d); g[v].emplace_back(u, c, d); } for (int i = 0; i < n; i++) for (int j = 0; j < 17; j++) par[i][j] = -1; euler_tour.emplace_back(0); dfs(0, -1, 0, 0); build(euler_tour); for (int i = 0; i + 1 < 17; i++) for (int j = 0; j < n; j++) { if (par[j][i] < 0) continue; par[j][i + 1] = par[par[j][i]][i]; } std::vector<std::deque<std::tuple<int, int, int, int>>> cq(n); for (int i = 0; i < q; i++) { int x, y, u, v; std::cin >> x >> y >> u >> v; cq[x].emplace_back(y, u - 1, v - 1, i); } constexpr i64 pp = 10000000000; auto sgn = [](auto t) { return t > 0 ? 1 : -1; }; std::vector<int> ret(q); for (int i = 1; i < n; i++) { for (auto &che : ce[i]) { update(che.first, sgn(che.second) * pp); } for (auto &qq : cq[i]) { auto u = std::get<1>(qq), v = std::get<2>(qq); if (depth[u] > depth[v]) std::swap(u, v); auto diff = depth[v] - depth[u]; for (int i = 16; i >= 0; i--) { if (diff & (1 << i)) v = par[v][i]; } int l = -1, r = n; while (r - l > 1) { auto k = (l + r) / 2; int a = u, b = v; bool flg = a == b; for (int i = 16; !flg && i >= 0; i--) { if ((k & (1 << i)) == 0) continue; a = par[a][i]; b = par[b][i]; if (a == b) flg = true; } if (flg) r = k; else l = k; } int lca = v; for (int i = 16; i >= 0; i--) { if (r & (1 << i)) lca = par[lca][i]; } auto d1 = query(estart[lca] + 1, estart[std::get<1>(qq)] + 1, 0, 0, size), d2 = query(estart[lca] + 1, estart[std::get<2>(qq)] + 1, 0, 0, size); auto dd = d1 % pp + d2 % pp + (d1 / pp + d2 / pp) * std::get<0>(qq); ret[std::get<3>(qq)] = dd; } for (auto &che : ce[i]) { update(che.first, che.second); } } for (auto r : ret) std::cout << r << std::endl; return 0; }
replace
8
9
8
9
0
p02986
C++
Runtime Error
/* Warn - Don't change next line else you will get WA verdict. Online Judge is configured to give WA if next line is not present. "An ideal problem has no test data." Author - Aryan Choudhary (@aryanc403) */ #pragma warning(disable : 4996) #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma GCC optimize("-ffloat-store") #include <bits/stdc++.h> #include <iostream> #include <stdio.h> using namespace std; #define fo(i, n) for (i = 0; i < (n); ++i) #define repA(i, j, n) for (i = (j); i <= (n); ++i) #define repD(i, j, n) for (i = (j); i >= (n); --i) #define pb push_back #define mp make_pair #define X first #define Y second #define endl "\n" typedef long long int lli; typedef long double mytype; typedef pair<lli, lli> ii; typedef vector<ii> vii; typedef vector<lli> vi; clock_t time_p = clock(); void aryanc403() { time_p = clock() - time_p; cerr << "Time Taken : " << (float)(time_p) / CLOCKS_PER_SEC << "\n"; } #ifdef ARYANC403 #define dbg(...) \ { \ cerr << "[ "; \ __aryanc403__(#__VA_ARGS__, __VA_ARGS__); \ } #undef endl template <typename Arg1, typename Arg2> ostream &operator<<(ostream &out, const pair<Arg1, Arg2> &x) { return out << "(" << x.X << "," << x.Y << ")"; } template <typename Arg1> ostream &operator<<(ostream &out, const vector<Arg1> &a) { out << "["; for (const auto &x : a) out << x << ","; return out << "]"; } template <typename Arg1> ostream &operator<<(ostream &out, const set<Arg1> &a) { out << "["; for (const auto &x : a) out << x << ","; return out << "]"; } template <typename Arg1, typename Arg2> ostream &operator<<(ostream &out, const map<Arg1, Arg2> &a) { out << "["; for (const auto &x : a) out << x << ","; return out << "]"; } template <typename Arg1> void __aryanc403__(const string name, Arg1 &&arg1) { cerr << name << " : " << arg1 << " ] " << endl; } template <typename Arg1, typename... Args> void __aryanc403__(const string names, Arg1 &&arg1, Args &&...args) { const string name = names.substr(0, names.find(',')); cerr << name << " : " << arg1 << " | "; __aryanc403__(names.substr(1 + (int)name.size()), args...); } #else #define dbg(args...) #endif const lli INF = 0xFFFFFFFFFFFFFFFL; lli seed; mt19937 rng(seed = chrono::steady_clock::now().time_since_epoch().count()); inline lli rnd(lli l = 0, lli r = INF) { return uniform_int_distribution<lli>(l, r)(rng); } class CMP { public: bool operator()(ii a, ii b) // For min priority_queue . { return !(a.X < b.X || a.X == b.X && a.Y <= b.Y); } }; void add(map<lli, lli> &m, lli x, lli cnt = 1) { auto jt = m.find(x); if (jt == m.end()) m.insert({x, cnt}); else jt->Y += cnt; } void del(map<lli, lli> &m, lli x, lli cnt = 1) { auto jt = m.find(x); if (jt->Y <= cnt) m.erase(jt); else jt->Y -= cnt; } bool cmp(const ii &a, const ii &b) { return a.X < b.X || (a.X == b.X && a.Y < b.Y); } const lli mod = 1000000007L; const lli maxN = 100000L; const lli logN = 20; lli T, n, i, j, k, in, cnt, l, r, u, v, x, y; lli m, q, c, w; string s; vector<pair<lli, ii>> e[maxN + 5]; lli h[maxN + 5], p[logN + 5][maxN + 5]; lli tin[maxN + 5], tout[maxN + 5]; map<lli, lli> flen[4 * maxN + 5], fcnt[4 * maxN + 5]; vii elr; // priority_queue < ii , vector < ii > , CMP > pq;// min priority_queue . bool isAnc(lli l, lli u) { return tin[l] <= tin[u] && tout[u] <= tout[l]; } lli lca(lli u, lli v) { if (isAnc(v, u)) return v; swap(u, v); if (isAnc(v, u)) return v; lli i; repD(i, logN, 0) if (!isAnc(p[i][u], v)) u = p[i][u]; u = p[0][u]; return u; } void dfs(lli u, lli pa, lli s) { h[u] = s; p[0][u] = pa; tin[u] = ++T; lli i; repA(i, 1, logN) p[i][u] = p[i - 1][p[i - 1][u]]; for (auto x : e[u]) { if (x.X == pa) continue; elr.pb(x.Y); dfs(x.X, u, s + x.Y.Y); elr.pb({x.Y.X, -x.Y.Y}); } tout[u] = ++T; } ii query(lli id, lli l, lli r, lli L, lli R, lli c) { if (R < l || r < L) return {0, 0}; if (L <= l && r <= R) { ii ans = {0, 0}; ans = {flen[id][c], fcnt[id][c]}; return ans; } lli m = (l + r) / 2; ii a, b; a = query(2 * id, l, m, L, R, c); b = query(2 * id + 1, m + 1, r, L, R, c); return {a.X + b.X, a.Y + b.Y}; } lli solve(lli l, lli u, lli c, lli len) { if (l == u) return 0; lli ans = h[u] - h[l]; ii x = query(1, 1, T, tin[l], tin[u] - 1, c); // sum,cnt; dbg(x, l, u, c); ans -= x.X; ans += len * x.Y; return ans; } void build(lli id, lli l, lli r) { lli i; repA(i, l, r) { flen[id][elr[i].X] += elr[i].Y; fcnt[id][elr[i].X] += elr[i].Y / abs(elr[i].Y); } if (l == r) return; lli m = (l + r) / 2; build(2 * id, l, m); build(2 * id + 1, m + 1, r); } int main(void) { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); // freopen("txt.in", "r", stdin); // freopen("txt.out", "w", stdout); // cout<<std::fixed<<std::setprecision(35); // cin>>T;while(T--) { cin >> n >> q; elr.pb({0, 0}); // col,len; fo(i, n - 1) { cin >> u >> v >> c >> w; e[u].pb({v, {c, w}}); e[v].pb({u, {c, w}}); } dfs(1, 1, 0); T -= 2; dbg(T, (lli)elr.size()); build(1, 1, T); dbg(elr); dbg(tin[1], tin[4], tin[5]); while (q--) { cin >> x >> y >> u >> v; l = lca(u, v); cout << solve(l, u, x, y) + solve(l, v, x, y) << endl; } } aryanc403(); return 0; }
/* Warn - Don't change next line else you will get WA verdict. Online Judge is configured to give WA if next line is not present. "An ideal problem has no test data." Author - Aryan Choudhary (@aryanc403) */ #pragma warning(disable : 4996) #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma GCC optimize("-ffloat-store") #include <bits/stdc++.h> #include <iostream> #include <stdio.h> using namespace std; #define fo(i, n) for (i = 0; i < (n); ++i) #define repA(i, j, n) for (i = (j); i <= (n); ++i) #define repD(i, j, n) for (i = (j); i >= (n); --i) #define pb push_back #define mp make_pair #define X first #define Y second #define endl "\n" typedef long long int lli; typedef long double mytype; typedef pair<lli, lli> ii; typedef vector<ii> vii; typedef vector<lli> vi; clock_t time_p = clock(); void aryanc403() { time_p = clock() - time_p; cerr << "Time Taken : " << (float)(time_p) / CLOCKS_PER_SEC << "\n"; } #ifdef ARYANC403 #define dbg(...) \ { \ cerr << "[ "; \ __aryanc403__(#__VA_ARGS__, __VA_ARGS__); \ } #undef endl template <typename Arg1, typename Arg2> ostream &operator<<(ostream &out, const pair<Arg1, Arg2> &x) { return out << "(" << x.X << "," << x.Y << ")"; } template <typename Arg1> ostream &operator<<(ostream &out, const vector<Arg1> &a) { out << "["; for (const auto &x : a) out << x << ","; return out << "]"; } template <typename Arg1> ostream &operator<<(ostream &out, const set<Arg1> &a) { out << "["; for (const auto &x : a) out << x << ","; return out << "]"; } template <typename Arg1, typename Arg2> ostream &operator<<(ostream &out, const map<Arg1, Arg2> &a) { out << "["; for (const auto &x : a) out << x << ","; return out << "]"; } template <typename Arg1> void __aryanc403__(const string name, Arg1 &&arg1) { cerr << name << " : " << arg1 << " ] " << endl; } template <typename Arg1, typename... Args> void __aryanc403__(const string names, Arg1 &&arg1, Args &&...args) { const string name = names.substr(0, names.find(',')); cerr << name << " : " << arg1 << " | "; __aryanc403__(names.substr(1 + (int)name.size()), args...); } #else #define dbg(args...) #endif const lli INF = 0xFFFFFFFFFFFFFFFL; lli seed; mt19937 rng(seed = chrono::steady_clock::now().time_since_epoch().count()); inline lli rnd(lli l = 0, lli r = INF) { return uniform_int_distribution<lli>(l, r)(rng); } class CMP { public: bool operator()(ii a, ii b) // For min priority_queue . { return !(a.X < b.X || a.X == b.X && a.Y <= b.Y); } }; void add(map<lli, lli> &m, lli x, lli cnt = 1) { auto jt = m.find(x); if (jt == m.end()) m.insert({x, cnt}); else jt->Y += cnt; } void del(map<lli, lli> &m, lli x, lli cnt = 1) { auto jt = m.find(x); if (jt->Y <= cnt) m.erase(jt); else jt->Y -= cnt; } bool cmp(const ii &a, const ii &b) { return a.X < b.X || (a.X == b.X && a.Y < b.Y); } const lli mod = 1000000007L; const lli maxN = 2 * 100000L; const lli logN = 20; lli T, n, i, j, k, in, cnt, l, r, u, v, x, y; lli m, q, c, w; string s; vector<pair<lli, ii>> e[maxN + 5]; lli h[maxN + 5], p[logN + 5][maxN + 5]; lli tin[maxN + 5], tout[maxN + 5]; map<lli, lli> flen[4 * maxN + 5], fcnt[4 * maxN + 5]; vii elr; // priority_queue < ii , vector < ii > , CMP > pq;// min priority_queue . bool isAnc(lli l, lli u) { return tin[l] <= tin[u] && tout[u] <= tout[l]; } lli lca(lli u, lli v) { if (isAnc(v, u)) return v; swap(u, v); if (isAnc(v, u)) return v; lli i; repD(i, logN, 0) if (!isAnc(p[i][u], v)) u = p[i][u]; u = p[0][u]; return u; } void dfs(lli u, lli pa, lli s) { h[u] = s; p[0][u] = pa; tin[u] = ++T; lli i; repA(i, 1, logN) p[i][u] = p[i - 1][p[i - 1][u]]; for (auto x : e[u]) { if (x.X == pa) continue; elr.pb(x.Y); dfs(x.X, u, s + x.Y.Y); elr.pb({x.Y.X, -x.Y.Y}); } tout[u] = ++T; } ii query(lli id, lli l, lli r, lli L, lli R, lli c) { if (R < l || r < L) return {0, 0}; if (L <= l && r <= R) { ii ans = {0, 0}; ans = {flen[id][c], fcnt[id][c]}; return ans; } lli m = (l + r) / 2; ii a, b; a = query(2 * id, l, m, L, R, c); b = query(2 * id + 1, m + 1, r, L, R, c); return {a.X + b.X, a.Y + b.Y}; } lli solve(lli l, lli u, lli c, lli len) { if (l == u) return 0; lli ans = h[u] - h[l]; ii x = query(1, 1, T, tin[l], tin[u] - 1, c); // sum,cnt; dbg(x, l, u, c); ans -= x.X; ans += len * x.Y; return ans; } void build(lli id, lli l, lli r) { lli i; repA(i, l, r) { flen[id][elr[i].X] += elr[i].Y; fcnt[id][elr[i].X] += elr[i].Y / abs(elr[i].Y); } if (l == r) return; lli m = (l + r) / 2; build(2 * id, l, m); build(2 * id + 1, m + 1, r); } int main(void) { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); // freopen("txt.in", "r", stdin); // freopen("txt.out", "w", stdout); // cout<<std::fixed<<std::setprecision(35); // cin>>T;while(T--) { cin >> n >> q; elr.pb({0, 0}); // col,len; fo(i, n - 1) { cin >> u >> v >> c >> w; e[u].pb({v, {c, w}}); e[v].pb({u, {c, w}}); } dfs(1, 1, 0); T -= 2; dbg(T, (lli)elr.size()); build(1, 1, T); dbg(elr); dbg(tin[1], tin[4], tin[5]); while (q--) { cin >> x >> y >> u >> v; l = lca(u, v); cout << solve(l, u, x, y) + solve(l, v, x, y) << endl; } } aryanc403(); return 0; }
replace
123
124
123
124
0
Time Taken : 0.03817
p02986
C++
Time Limit Exceeded
#pragma optimization_level 3 #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math") #include <bits/stdc++.h> #define F first #define S second #define vec vector #define ms multiset #define pb push_back #define pll pair<ll, ll> #define pdd pair<ld, ld> #define pq priority_queue #define umap unordered_map #define uset unordered_set #define pii pair<int, int> #define pnn pair<Node *, Node *> #define uid uniform_int_distribution #define FILE \ ifstream in("board.in"); \ ofstream out("board.out"); #define fast \ cin.tie(0); \ cout.tie(0); \ cin.sync_with_stdio(0); \ cout.sync_with_stdio(0); using namespace std; typedef string str; typedef long long ll; typedef long double ld; mt19937 rnd(chrono::steady_clock::now() .time_since_epoch() .count()); // uid<int> u1(5, 10); u1(rnd); struct edg { ll y, c, d; edg(int a, int b, int cc) { y = a; c = b; d = cc; } }; struct Node { Node *l = 0, *r = 0; pii p = {0, 0}; Node(Node *a, Node *b, pii c) { l = a; r = b; p = c; } Node(Node *a, Node *b) { l = a; r = b; p = {a->p.F + b->p.F, a->p.S + b->p.S}; } Node(Node *n) { l = n->l; r = n->r; p = n->p; } }; Node *add(ll l, ll r, ll pos, Node *n, ll val) { Node *cp = n ? new Node(n) : new Node(0, 0, {0, 0}); if (l == r) { cp->p.F++; cp->p.S += val; return cp; } ll md = (l + r) / 2; if (pos <= md) cp->l = add(l, md, pos, cp->l, val); else cp->r = add(md + 1, r, pos, cp->r, val); return cp; } pll get(ll l, ll r, ll pos, Node *n) { if (!n) return {0, 0}; if (l == r) return n->p; int md = (l + r) / 2; if (pos <= md) return get(l, md, pos, n->l); else return get(md + 1, r, pos, n->r); } ll a, tim = 0, ndo = -1, SZ = 1e5 + 5; vec<vec<edg>> l; vec<ll> sz, dep, tin, tout, numdo, posindo, eul, p, lf, rg, cl, dl, prs, firindo; vec<Node *> m; ll init1(ll v, ll pr, ll d, ll color, ll dln) { sz[v] = 1; if (pr != -1) cl[v] = color, dl[v] = dln, prs[v] = prs[pr] + dl[v]; p[v] = pr; dep[v] = d; for (edg h : l[v]) { if (h.y != pr) sz[v] += init1(h.y, v, d + 1, h.c, h.d); } sort(l[v].begin(), l[v].end(), [](edg i1, edg i2) { return sz[i1.y] != sz[i2.y] ? sz[i1.y] > sz[i2.y] : i1.y < i2.y; }); return sz[v]; } void init2(ll v, int pr, int pdo) { eul.pb(v); if (pdo == 0) ndo++, lf.pb(eul.size() - 1); numdo[v] = ndo; tin[v] = tim++; posindo[v] = pdo; int cnt = 0; for (edg h : l[v]) { if (h.y == pr) continue; init2(h.y, v, (cnt == 0 ? pdo + 1 : 0)); cnt++; } if (rg.size() < lf.size()) rg.pb(eul.size() - 1); tout[v] = tim - 1; } ll check(ll v, ll pt) { return tin[v] <= tin[pt] && tout[pt] <= tout[v]; } ll sm = 0; pll go(ll x, ll y, ll c) { ll cnt = 0, d = 0, sx = x, sy = y; for (; !check(firindo[x], y);) { pll pp = get(0, SZ, c, m[lf[numdo[x]] + posindo[x]]); cnt += pp.F, d += pp.S; x = p[firindo[x]]; } for (; numdo[x] != numdo[y];) { pll pp = get(0, SZ, c, m[lf[numdo[y]] + posindo[y]]); cnt += pp.F, d += pp.S; y = p[firindo[y]]; } if (dep[x] > dep[y]) swap(x, y); sm = prs[sx] + prs[sy] - 2 * prs[x]; ll DO = numdo[x]; pll p1 = get(0, SZ, c, m[lf[DO] + posindo[y]]); pll p2 = get(0, SZ, c, m[lf[DO] + posindo[x]]); return {cnt + p1.F - p2.F, d + p1.S - p2.S}; } ll ga(ll x, ll y, ll c, ll dd) { ll summ = 0, cnt = 0, ss = 0; for (; !check(x, y);) { summ += dl[x]; if (cl[x] == c) cnt++, ss += dl[x]; x = p[x]; } for (; y != x;) { summ += dl[y]; if (cl[y] == c) cnt++, ss += dl[y]; y = p[y]; } return summ - ss + cnt * dd; } int main() { fast; ll z; cin >> a >> z; p = vec<ll>(a); sz = vec<ll>(a); cl = vec<ll>(a); dl = vec<ll>(a); dep = vec<ll>(a); tin = vec<ll>(a); prs = vec<ll>(a); tout = vec<ll>(a); numdo = vec<ll>(a); posindo = vec<ll>(a); firindo = vec<ll>(a); l = vec<vec<edg>>(a, vec<edg>()); for (ll q = 0; q < a - 1; q++) { ll x, y, c, d; cin >> x >> y >> c >> d; x--; y--; if (x > y) swap(x, y); l[x].pb(edg(y, c, d)); l[y].pb(edg(x, c, d)); } init1(0, -1, 0, 0, 0); init2(0, -1, 0); ndo++; m = vec<Node *>(a); for (ll q = 0; q < ndo; q++) { m[lf[q]] = new Node(0, 0, {0, 0}); for (ll w = lf[q]; w <= rg[q]; w++) m[w] = add(0, SZ, cl[eul[w]], (w == lf[q] ? m[w] : m[w - 1]), dl[eul[w]]), firindo[eul[w]] = eul[lf[q]]; } // for(int q=0; q<ndo; q++) cout<<lf[q]<<" "<<rg[q]<<endl; for (; z; z--) { ll x, y, c, d; cin >> c >> d >> x >> y; x--; y--; sm = 0; // pll p = go(x,y,c); cout << ga(x, y, c, d) << "\n"; // cout<<sm-p.S+p.F*d<<endl; } }
#pragma optimization_level 3 #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math") #include <bits/stdc++.h> #define F first #define S second #define vec vector #define ms multiset #define pb push_back #define pll pair<ll, ll> #define pdd pair<ld, ld> #define pq priority_queue #define umap unordered_map #define uset unordered_set #define pii pair<int, int> #define pnn pair<Node *, Node *> #define uid uniform_int_distribution #define FILE \ ifstream in("board.in"); \ ofstream out("board.out"); #define fast \ cin.tie(0); \ cout.tie(0); \ cin.sync_with_stdio(0); \ cout.sync_with_stdio(0); using namespace std; typedef string str; typedef long long ll; typedef long double ld; mt19937 rnd(chrono::steady_clock::now() .time_since_epoch() .count()); // uid<int> u1(5, 10); u1(rnd); struct edg { ll y, c, d; edg(int a, int b, int cc) { y = a; c = b; d = cc; } }; struct Node { Node *l = 0, *r = 0; pii p = {0, 0}; Node(Node *a, Node *b, pii c) { l = a; r = b; p = c; } Node(Node *a, Node *b) { l = a; r = b; p = {a->p.F + b->p.F, a->p.S + b->p.S}; } Node(Node *n) { l = n->l; r = n->r; p = n->p; } }; Node *add(ll l, ll r, ll pos, Node *n, ll val) { Node *cp = n ? new Node(n) : new Node(0, 0, {0, 0}); if (l == r) { cp->p.F++; cp->p.S += val; return cp; } ll md = (l + r) / 2; if (pos <= md) cp->l = add(l, md, pos, cp->l, val); else cp->r = add(md + 1, r, pos, cp->r, val); return cp; } pll get(ll l, ll r, ll pos, Node *n) { if (!n) return {0, 0}; if (l == r) return n->p; int md = (l + r) / 2; if (pos <= md) return get(l, md, pos, n->l); else return get(md + 1, r, pos, n->r); } ll a, tim = 0, ndo = -1, SZ = 1e5 + 5; vec<vec<edg>> l; vec<ll> sz, dep, tin, tout, numdo, posindo, eul, p, lf, rg, cl, dl, prs, firindo; vec<Node *> m; ll init1(ll v, ll pr, ll d, ll color, ll dln) { sz[v] = 1; if (pr != -1) cl[v] = color, dl[v] = dln, prs[v] = prs[pr] + dl[v]; p[v] = pr; dep[v] = d; for (edg h : l[v]) { if (h.y != pr) sz[v] += init1(h.y, v, d + 1, h.c, h.d); } sort(l[v].begin(), l[v].end(), [](edg i1, edg i2) { return sz[i1.y] != sz[i2.y] ? sz[i1.y] > sz[i2.y] : i1.y < i2.y; }); return sz[v]; } void init2(ll v, int pr, int pdo) { eul.pb(v); if (pdo == 0) ndo++, lf.pb(eul.size() - 1); numdo[v] = ndo; tin[v] = tim++; posindo[v] = pdo; int cnt = 0; for (edg h : l[v]) { if (h.y == pr) continue; init2(h.y, v, (cnt == 0 ? pdo + 1 : 0)); cnt++; } if (rg.size() < lf.size()) rg.pb(eul.size() - 1); tout[v] = tim - 1; } ll check(ll v, ll pt) { return tin[v] <= tin[pt] && tout[pt] <= tout[v]; } ll sm = 0; pll go(ll x, ll y, ll c) { ll cnt = 0, d = 0, sx = x, sy = y; for (; !check(firindo[x], y);) { pll pp = get(0, SZ, c, m[lf[numdo[x]] + posindo[x]]); cnt += pp.F, d += pp.S; x = p[firindo[x]]; } for (; numdo[x] != numdo[y];) { pll pp = get(0, SZ, c, m[lf[numdo[y]] + posindo[y]]); cnt += pp.F, d += pp.S; y = p[firindo[y]]; } if (dep[x] > dep[y]) swap(x, y); sm = prs[sx] + prs[sy] - 2 * prs[x]; ll DO = numdo[x]; pll p1 = get(0, SZ, c, m[lf[DO] + posindo[y]]); pll p2 = get(0, SZ, c, m[lf[DO] + posindo[x]]); return {cnt + p1.F - p2.F, d + p1.S - p2.S}; } ll ga(ll x, ll y, ll c, ll dd) { ll summ = 0, cnt = 0, ss = 0; for (; !check(x, y);) { summ += dl[x]; if (cl[x] == c) cnt++, ss += dl[x]; x = p[x]; } for (; y != x;) { summ += dl[y]; if (cl[y] == c) cnt++, ss += dl[y]; y = p[y]; } return summ - ss + cnt * dd; } int main() { fast; ll z; cin >> a >> z; p = vec<ll>(a); sz = vec<ll>(a); cl = vec<ll>(a); dl = vec<ll>(a); dep = vec<ll>(a); tin = vec<ll>(a); prs = vec<ll>(a); tout = vec<ll>(a); numdo = vec<ll>(a); posindo = vec<ll>(a); firindo = vec<ll>(a); l = vec<vec<edg>>(a, vec<edg>()); for (ll q = 0; q < a - 1; q++) { ll x, y, c, d; cin >> x >> y >> c >> d; x--; y--; if (x > y) swap(x, y); l[x].pb(edg(y, c, d)); l[y].pb(edg(x, c, d)); } init1(0, -1, 0, 0, 0); init2(0, -1, 0); ndo++; m = vec<Node *>(a); for (ll q = 0; q < ndo; q++) { m[lf[q]] = new Node(0, 0, {0, 0}); for (ll w = lf[q]; w <= rg[q]; w++) m[w] = add(0, SZ, cl[eul[w]], (w == lf[q] ? m[w] : m[w - 1]), dl[eul[w]]), firindo[eul[w]] = eul[lf[q]]; } // for(int q=0; q<ndo; q++) cout<<lf[q]<<" "<<rg[q]<<endl; for (; z; z--) { ll x, y, c, d; cin >> c >> d >> x >> y; x--; y--; sm = 0; pll p = go(x, y, c); // cout<<ga(x,y,c,d)<<"\n"; cout << sm - p.S + p.F * d << "\n"; } }
replace
217
222
217
222
TLE
p02986
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long #define pii pair<int, int> #define mp make_pair int n, q; struct edge { int v, c, w; edge(int vv, int cc, int ww) { v = vv; c = cc; w = ww; } }; vector<edge> G[123456]; int cnt[12345], in[123456], out[123456], tim = 0; int pa[18][123456]; bool anc(int x, int y) { return in[x] <= in[y] && out[x] >= out[y]; } void dfs(int cur, int pre) { in[cur] = ++tim; pa[0][cur] = pre; for (edge tmp : G[cur]) { int nxt = tmp.v, col = tmp.c, w = tmp.w; if (nxt == pre) continue; dfs(nxt, cur); } out[cur] = ++tim; } void build() { for (int i = 1; i < 18; i++) for (int j = 1; j <= n; j++) pa[i][j] = pa[i - 1][pa[i - 1][j]]; } int lca(int x, int y) { if (anc(x, y)) return x; if (anc(y, x)) return y; for (int i = 17; i >= 0; i--) if (!anc(pa[i][x], y)) x = pa[i][x]; return pa[0][x]; } int ans[123456], q_col[123456], q_val[123456], dist[123456]; vector<int> query[123456]; void solve(int cur, int pre, int cur_dis) { for (int id : query[cur]) { int mul = 1; if (id < 0) mul = -2, id = -id; ans[id] += mul * (cur_dis + cnt[q_col[id]] * q_val[id] - dist[q_col[id]]); } for (edge tmp : G[cur]) { int nxt = tmp.v, col = tmp.c, w = tmp.w; if (nxt == pre) continue; cnt[col]++; dist[col] += w; solve(nxt, cur, cur_dis + w); dist[col] -= w; cnt[col]--; } } int32_t main() { ios::sync_with_stdio(); cin.tie(0); cin >> n >> q; for (int i = 0; i < n - 1; i++) { int u, v, c, w; cin >> u >> v >> c >> w; G[u].push_back(edge(v, c, w)); G[v].push_back(edge(u, c, w)); } dfs(1, 1); build(); for (int i = 1; i <= q; i++) { int u, v; cin >> q_col[i] >> q_val[i] >> u >> v; query[u].push_back(i); query[v].push_back(i); query[lca(u, v)].push_back(-i); } solve(1, 1, 0); for (int i = 1; i <= q; i++) cout << ans[i] << endl; }
#include <bits/stdc++.h> using namespace std; #define int long long #define pii pair<int, int> #define mp make_pair int n, q; struct edge { int v, c, w; edge(int vv, int cc, int ww) { v = vv; c = cc; w = ww; } }; vector<edge> G[123456]; int cnt[123456], in[123456], out[123456], tim = 0; int pa[18][123456]; bool anc(int x, int y) { return in[x] <= in[y] && out[x] >= out[y]; } void dfs(int cur, int pre) { in[cur] = ++tim; pa[0][cur] = pre; for (edge tmp : G[cur]) { int nxt = tmp.v, col = tmp.c, w = tmp.w; if (nxt == pre) continue; dfs(nxt, cur); } out[cur] = ++tim; } void build() { for (int i = 1; i < 18; i++) for (int j = 1; j <= n; j++) pa[i][j] = pa[i - 1][pa[i - 1][j]]; } int lca(int x, int y) { if (anc(x, y)) return x; if (anc(y, x)) return y; for (int i = 17; i >= 0; i--) if (!anc(pa[i][x], y)) x = pa[i][x]; return pa[0][x]; } int ans[123456], q_col[123456], q_val[123456], dist[123456]; vector<int> query[123456]; void solve(int cur, int pre, int cur_dis) { for (int id : query[cur]) { int mul = 1; if (id < 0) mul = -2, id = -id; ans[id] += mul * (cur_dis + cnt[q_col[id]] * q_val[id] - dist[q_col[id]]); } for (edge tmp : G[cur]) { int nxt = tmp.v, col = tmp.c, w = tmp.w; if (nxt == pre) continue; cnt[col]++; dist[col] += w; solve(nxt, cur, cur_dis + w); dist[col] -= w; cnt[col]--; } } int32_t main() { ios::sync_with_stdio(); cin.tie(0); cin >> n >> q; for (int i = 0; i < n - 1; i++) { int u, v, c, w; cin >> u >> v >> c >> w; G[u].push_back(edge(v, c, w)); G[v].push_back(edge(u, c, w)); } dfs(1, 1); build(); for (int i = 1; i <= q; i++) { int u, v; cin >> q_col[i] >> q_val[i] >> u >> v; query[u].push_back(i); query[v].push_back(i); query[lca(u, v)].push_back(-i); } solve(1, 1, 0); for (int i = 1; i <= q; i++) cout << ans[i] << endl; }
replace
15
16
15
16
0
p02986
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < int(n); i++) #define rrep(i, n) for (int i = int(n) - 1; i >= 0; i--) #define reps(i, n) for (int i = 1; i <= int(n); i++) #define rreps(i, n) for (int i = int(n); i >= 1; i--) #define repc(i, n) for (int i = 0; i <= int(n); i++) #define rrepc(i, n) for (int i = int(n); i >= 0; i--) #define repi(i, a, b) for (int i = int(a); i < int(b); i++) #define repic(i, a, b) for (int i = int(a); i <= int(b); i++) #define each(x, y) for (auto &x : y) #define all(a) (a).begin(), (a).end() #define bit32(x) (1 << (x)) #define bit64(x) (1ll << (x)) using namespace std; using i32 = int; using i64 = long long; using u64 = unsigned long long; using f80 = long double; using vi32 = vector<i32>; using vi64 = vector<i64>; using vu64 = vector<u64>; using vf80 = vector<f80>; using vstr = vector<string>; inline void yes() { cout << "Yes" << '\n'; exit(0); } inline void no() { cout << "No" << '\n'; exit(0); } inline i64 gcd(i64 a, i64 b) { if (min(a, b) == 0) return max(a, b); if (a % b == 0) return b; return gcd(b, a % b); } inline i64 lcm(i64 a, i64 b) { if (min(a, b) == 0) return max(a, b); return a / gcd(a, b) * b; } inline u64 xorshift() { static u64 x = 88172645463325252ull; x = x ^ (x << 7); return x = x ^ (x >> 9); } template <typename T> class pqasc : public priority_queue<T, vector<T>, greater<T>> {}; template <typename T> class pqdesc : public priority_queue<T, vector<T>, less<T>> {}; template <typename T> inline void amax(T &x, T y) { if (x < y) x = y; } template <typename T> inline void amin(T &x, T y) { if (x > y) x = y; } template <typename T> inline T exp(T x, i64 n, T e = 1) { T r = e; while (n > 0) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; } template <typename T> inline T bis(T ok, T ng, function<bool(T)> f, T eps = 1) { while (abs(ok - ng) > eps) { T mi = (ok + ng) / 2; (f(mi) ? ok : ng) = mi; } return ok; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { each(x, v) is >> x; return is; } template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { rep(i, v.size()) { if (i) os << ' '; os << v[i]; } return os; } void solve(); int main() { ios::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(16); solve(); return 0; } template <typename T> struct WeightedGraph { struct Edge { int to; T cost; int col; }; vector<vector<Edge>> edges; int n; WeightedGraph(int n) : n(n) { edges = vector<vector<Edge>>(n); } void add_edge(int from, int to, T cost, int col) { edges[from].push_back((Edge){to, cost, col}); } vector<Edge> &operator[](int x) { return edges[x]; } }; struct LCA { WeightedGraph<int> g; vector<vi32> par; vi32 dep; LCA(WeightedGraph<int> &g) : g(g), par(30, vi32(g.n + 1, g.n)), dep(g.n) { dfs(0, -1, 0); rep(i, 29) rep(j, g.n) { par[i + 1][j] = par[i][par[i][j]]; } } void dfs(int u, int p, int d) { dep[u] = d; each(e, g[u]) if (e.to != p) { par[0][e.to] = u; dfs(e.to, u, d + 1); } } int operator()(int u, int v) { if (dep[u] < dep[v]) swap(u, v); rep(i, 30) { if ((dep[u] - dep[v]) & bit32(i)) u = par[i][u]; } if (u == v) return u; rrep(i, 30) { if (par[i][u] != par[i][v]) { u = par[i][u]; v = par[i][v]; } } return par[0][u]; } }; void solve() { int N, Q; cin >> N >> Q; WeightedGraph<int> g(N); rep(i, N - 1) { int a, b, c, d; cin >> a >> b >> c >> d; a--, b--; g.add_edge(a, b, d, c); g.add_edge(b, a, d, c); } auto lca = LCA(g); struct Query { int a, c, d, i; }; i64 sum = 0; vi64 col_sum(N); vi64 col_cnt(N); vi64 ans(N); vector<vector<Query>> query(N); rep(i, Q) { int x, y, u, v; cin >> x >> y >> u >> v; u--, v--; int p = lca(u, v); query[u].push_back((Query){i, x, y, 1}); query[v].push_back((Query){i, x, y, 1}); query[p].push_back((Query){i, x, y, -2}); } auto dfs = [&](auto dfs, int u, int p) -> void { each(q, query[u]) { i64 x = sum - col_sum[q.c]; x += col_cnt[q.c] * q.d; ans[q.a] += x * q.i; } each(e, g[u]) if (e.to != p) { sum += e.cost; col_sum[e.col] += e.cost; col_cnt[e.col] += 1; dfs(dfs, e.to, u); sum -= e.cost; col_sum[e.col] -= e.cost; col_cnt[e.col] -= 1; } }; dfs(dfs, 0, -1); rep(i, Q) cout << ans[i] << endl; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < int(n); i++) #define rrep(i, n) for (int i = int(n) - 1; i >= 0; i--) #define reps(i, n) for (int i = 1; i <= int(n); i++) #define rreps(i, n) for (int i = int(n); i >= 1; i--) #define repc(i, n) for (int i = 0; i <= int(n); i++) #define rrepc(i, n) for (int i = int(n); i >= 0; i--) #define repi(i, a, b) for (int i = int(a); i < int(b); i++) #define repic(i, a, b) for (int i = int(a); i <= int(b); i++) #define each(x, y) for (auto &x : y) #define all(a) (a).begin(), (a).end() #define bit32(x) (1 << (x)) #define bit64(x) (1ll << (x)) using namespace std; using i32 = int; using i64 = long long; using u64 = unsigned long long; using f80 = long double; using vi32 = vector<i32>; using vi64 = vector<i64>; using vu64 = vector<u64>; using vf80 = vector<f80>; using vstr = vector<string>; inline void yes() { cout << "Yes" << '\n'; exit(0); } inline void no() { cout << "No" << '\n'; exit(0); } inline i64 gcd(i64 a, i64 b) { if (min(a, b) == 0) return max(a, b); if (a % b == 0) return b; return gcd(b, a % b); } inline i64 lcm(i64 a, i64 b) { if (min(a, b) == 0) return max(a, b); return a / gcd(a, b) * b; } inline u64 xorshift() { static u64 x = 88172645463325252ull; x = x ^ (x << 7); return x = x ^ (x >> 9); } template <typename T> class pqasc : public priority_queue<T, vector<T>, greater<T>> {}; template <typename T> class pqdesc : public priority_queue<T, vector<T>, less<T>> {}; template <typename T> inline void amax(T &x, T y) { if (x < y) x = y; } template <typename T> inline void amin(T &x, T y) { if (x > y) x = y; } template <typename T> inline T exp(T x, i64 n, T e = 1) { T r = e; while (n > 0) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; } template <typename T> inline T bis(T ok, T ng, function<bool(T)> f, T eps = 1) { while (abs(ok - ng) > eps) { T mi = (ok + ng) / 2; (f(mi) ? ok : ng) = mi; } return ok; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { each(x, v) is >> x; return is; } template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { rep(i, v.size()) { if (i) os << ' '; os << v[i]; } return os; } void solve(); int main() { ios::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(16); solve(); return 0; } template <typename T> struct WeightedGraph { struct Edge { int to; T cost; int col; }; vector<vector<Edge>> edges; int n; WeightedGraph(int n) : n(n) { edges = vector<vector<Edge>>(n); } void add_edge(int from, int to, T cost, int col) { edges[from].push_back((Edge){to, cost, col}); } vector<Edge> &operator[](int x) { return edges[x]; } }; struct LCA { WeightedGraph<int> g; vector<vi32> par; vi32 dep; LCA(WeightedGraph<int> &g) : g(g), par(30, vi32(g.n + 1, g.n)), dep(g.n) { dfs(0, -1, 0); rep(i, 29) rep(j, g.n) { par[i + 1][j] = par[i][par[i][j]]; } } void dfs(int u, int p, int d) { dep[u] = d; each(e, g[u]) if (e.to != p) { par[0][e.to] = u; dfs(e.to, u, d + 1); } } int operator()(int u, int v) { if (dep[u] < dep[v]) swap(u, v); rep(i, 30) { if ((dep[u] - dep[v]) & bit32(i)) u = par[i][u]; } if (u == v) return u; rrep(i, 30) { if (par[i][u] != par[i][v]) { u = par[i][u]; v = par[i][v]; } } return par[0][u]; } }; void solve() { int N, Q; cin >> N >> Q; WeightedGraph<int> g(N); rep(i, N - 1) { int a, b, c, d; cin >> a >> b >> c >> d; a--, b--; g.add_edge(a, b, d, c); g.add_edge(b, a, d, c); } auto lca = LCA(g); struct Query { int a, c, d, i; }; i64 sum = 0; vi64 col_sum(N); vi64 col_cnt(N); vi64 ans(Q); vector<vector<Query>> query(N); rep(i, Q) { int x, y, u, v; cin >> x >> y >> u >> v; u--, v--; int p = lca(u, v); query[u].push_back((Query){i, x, y, 1}); query[v].push_back((Query){i, x, y, 1}); query[p].push_back((Query){i, x, y, -2}); } auto dfs = [&](auto dfs, int u, int p) -> void { each(q, query[u]) { i64 x = sum - col_sum[q.c]; x += col_cnt[q.c] * q.d; ans[q.a] += x * q.i; } each(e, g[u]) if (e.to != p) { sum += e.cost; col_sum[e.col] += e.cost; col_cnt[e.col] += 1; dfs(dfs, e.to, u); sum -= e.cost; col_sum[e.col] -= e.cost; col_cnt[e.col] -= 1; } }; dfs(dfs, 0, -1); rep(i, Q) cout << ans[i] << endl; }
replace
172
173
172
173
0
p02986
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int maxm = 1e6 + 10; const int maxn = 133334; typedef long long ll; struct node { int s, e, c, w, next; } edge[maxn]; int head[maxn], len; int n, q; void init() { memset(head, -1, sizeof(head)); len = 0; } void add(int s, int e, int c, int w) { edge[len].s = s; edge[len].e = e; edge[len].w = w; edge[len].c = c; edge[len].next = head[s]; head[s] = len++; } int son[maxn], top[maxn], tid[maxn], fat[maxn], siz[maxn], dep[maxn], rak[maxn]; int dfx; void dfs1(int x, int fa, int d) { siz[x] = 1, son[x] = -1, fat[x] = fa, dep[x] = d; for (int i = head[x]; i != -1; i = edge[i].next) { int y = edge[i].e; if (y == fa) continue; dfs1(y, x, d + 1); siz[x] += siz[y]; if (son[x] == -1 || siz[y] > siz[son[x]]) son[x] = y; } } void dfs2(int x, int c) { top[x] = c; tid[x] = ++dfx; rak[dfx] = x; if (son[x] == -1) return; dfs2(son[x], c); for (int i = head[x]; i != -1; i = edge[i].next) { int y = edge[i].e; if (y == fat[x] || y == son[x]) continue; dfs2(y, y); } } int cnt, root[maxn], ls[maxn * 40], rs[maxn * 40], num[maxn * 40], val[maxn * 40]; struct C { int color, dis; } a[maxn]; void build(C k, int l, int r, int &i) { num[++cnt] = num[i] + 1, val[cnt] = val[i] + k.dis, ls[cnt] = ls[i], rs[cnt] = rs[i]; i = cnt; if (l == r) return; int mid = l + r >> 1; if (k.color <= mid) build(k, l, mid, ls[i]); else build(k, mid + 1, r, rs[i]); } C query(int u, int v, int k, int l, int r) { if (l == r) { return {num[v] - num[u], val[v] - val[u]}; } int mid = l + r >> 1; if (k <= mid) return query(ls[u], ls[v], k, l, mid); else return query(rs[u], rs[v], k, mid + 1, r); } int solve(int cr, int dis, int x, int y) { C ans = {0, 0}; int sum = 0; while (top[x] != top[y]) { if (dep[top[x]] < dep[top[y]]) swap(x, y); C t = query(root[tid[top[x]] - 1], root[tid[x]], cr, 1, n); sum += val[root[tid[x]]] - val[root[tid[top[x]] - 1]]; ans.color += t.color, ans.dis += t.dis; x = fat[top[x]]; } if (x != y) { if (dep[x] < dep[y]) swap(x, y); C t = query(root[tid[son[y]] - 1], root[tid[x]], cr, 1, n); sum += val[root[tid[x]]] - val[root[tid[son[y]] - 1]]; ans.color += t.color, ans.dis += t.dis; } // cout << sum << " " << ans.color << " " << ans.dis << endl; return sum - ans.dis + ans.color * dis; } void dfs(int l, int r, int i) { if (l == r) { printf("%d#%d ", num[i], val[i]); return; } int mid = l + r >> 1; dfs(l, mid, ls[i]); dfs(mid + 1, r, rs[i]); } int main() { scanf("%d%d", &n, &q); init(); for (int i = 1, x, y, c, d; i < n; i++) { scanf("%d%d%d%d", &x, &y, &c, &d); add(x, y, c, d); add(y, x, c, d); } dfs1(1, 0, 1); dfs2(1, 1); for (int i = 0; i < len; i += 2) { int x = edge[i].e; int y = edge[i].s; if (dep[x] < dep[y]) swap(x, y); a[tid[x]] = {edge[i].c, edge[i].w}; } for (int i = 2; i <= dfx; i++) { root[i] = root[i - 1]; build(a[i], 1, n, root[i]); // dfs(1, n, root[i]); } while (q--) { int x, y, u, v; scanf("%d%d%d%d", &x, &y, &u, &v); printf("%d\n", solve(x, y, u, v)); } }
#include <bits/stdc++.h> using namespace std; const int maxm = 1e6 + 10; const int maxn = 3e5 + 10; typedef long long ll; struct node { int s, e, c, w, next; } edge[maxn]; int head[maxn], len; int n, q; void init() { memset(head, -1, sizeof(head)); len = 0; } void add(int s, int e, int c, int w) { edge[len].s = s; edge[len].e = e; edge[len].w = w; edge[len].c = c; edge[len].next = head[s]; head[s] = len++; } int son[maxn], top[maxn], tid[maxn], fat[maxn], siz[maxn], dep[maxn], rak[maxn]; int dfx; void dfs1(int x, int fa, int d) { siz[x] = 1, son[x] = -1, fat[x] = fa, dep[x] = d; for (int i = head[x]; i != -1; i = edge[i].next) { int y = edge[i].e; if (y == fa) continue; dfs1(y, x, d + 1); siz[x] += siz[y]; if (son[x] == -1 || siz[y] > siz[son[x]]) son[x] = y; } } void dfs2(int x, int c) { top[x] = c; tid[x] = ++dfx; rak[dfx] = x; if (son[x] == -1) return; dfs2(son[x], c); for (int i = head[x]; i != -1; i = edge[i].next) { int y = edge[i].e; if (y == fat[x] || y == son[x]) continue; dfs2(y, y); } } int cnt, root[maxn], ls[maxn * 40], rs[maxn * 40], num[maxn * 40], val[maxn * 40]; struct C { int color, dis; } a[maxn]; void build(C k, int l, int r, int &i) { num[++cnt] = num[i] + 1, val[cnt] = val[i] + k.dis, ls[cnt] = ls[i], rs[cnt] = rs[i]; i = cnt; if (l == r) return; int mid = l + r >> 1; if (k.color <= mid) build(k, l, mid, ls[i]); else build(k, mid + 1, r, rs[i]); } C query(int u, int v, int k, int l, int r) { if (l == r) { return {num[v] - num[u], val[v] - val[u]}; } int mid = l + r >> 1; if (k <= mid) return query(ls[u], ls[v], k, l, mid); else return query(rs[u], rs[v], k, mid + 1, r); } int solve(int cr, int dis, int x, int y) { C ans = {0, 0}; int sum = 0; while (top[x] != top[y]) { if (dep[top[x]] < dep[top[y]]) swap(x, y); C t = query(root[tid[top[x]] - 1], root[tid[x]], cr, 1, n); sum += val[root[tid[x]]] - val[root[tid[top[x]] - 1]]; ans.color += t.color, ans.dis += t.dis; x = fat[top[x]]; } if (x != y) { if (dep[x] < dep[y]) swap(x, y); C t = query(root[tid[son[y]] - 1], root[tid[x]], cr, 1, n); sum += val[root[tid[x]]] - val[root[tid[son[y]] - 1]]; ans.color += t.color, ans.dis += t.dis; } // cout << sum << " " << ans.color << " " << ans.dis << endl; return sum - ans.dis + ans.color * dis; } void dfs(int l, int r, int i) { if (l == r) { printf("%d#%d ", num[i], val[i]); return; } int mid = l + r >> 1; dfs(l, mid, ls[i]); dfs(mid + 1, r, rs[i]); } int main() { scanf("%d%d", &n, &q); init(); for (int i = 1, x, y, c, d; i < n; i++) { scanf("%d%d%d%d", &x, &y, &c, &d); add(x, y, c, d); add(y, x, c, d); } dfs1(1, 0, 1); dfs2(1, 1); for (int i = 0; i < len; i += 2) { int x = edge[i].e; int y = edge[i].s; if (dep[x] < dep[y]) swap(x, y); a[tid[x]] = {edge[i].c, edge[i].w}; } for (int i = 2; i <= dfx; i++) { root[i] = root[i - 1]; build(a[i], 1, n, root[i]); // dfs(1, n, root[i]); } while (q--) { int x, y, u, v; scanf("%d%d%d%d", &x, &y, &u, &v); printf("%d\n", solve(x, y, u, v)); } }
replace
3
4
3
4
0
p02986
C++
Runtime Error
#include <bits/stdc++.h> #define all(vec) vec.begin(), vec.end() using namespace std; using ll = long long; using P = pair<ll, ll>; constexpr ll INF = (1LL << 30) - 1LL; constexpr ll LINF = (1LL << 60) - 1LL; constexpr double eps = 1e-9; constexpr ll MOD = 1000000007LL; template <typename T> bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; }; template <typename T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; }; template <typename T> ostream &operator<<(ostream &os, vector<T> v) { for (int i = 0; i < v.size(); i++) { os << v[i] << (i + 1 == v.size() ? "\n" : " "); } return os; } template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); } template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); } template <typename T, typename V> typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) { t = v; } template <typename T, typename V> typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) { for (auto &e : t) { fill_v(e, v); } }; int n, q; struct edge { int to, c, d; }; vector<vector<edge>> G; vector<vector<int>> par; vector<int> dep; void ddfs(int i, int p) { for (auto &e : G[i]) { if (e.to == p) continue; par[e.to][0] = i; dep[e.to] = dep[i] + 1; ddfs(e.to, i); } } void build() { par[0][0] = -1; dep[0] = 0; ddfs(0, -1); for (int j = 1; j < 20; j++) { for (int i = 0; i < n; i++) { if (par[i][j - 1] == -1) { par[i][j] = -1; } else { par[i][j] = par[par[i][j - 1]][j - 1]; } } } } int lca(int u, int v) { if (dep[u] > dep[v]) swap(u, v); for (int i = 19; i >= 0; i--) { if (((dep[v] - dep[u]) >> i) & 1) v = par[v][i]; } if (u == v) return u; for (int i = 19; i >= 0; i--) { if (par[u][i] != par[v][i]) { u = par[u][i]; v = par[v][i]; } } return par[u][0]; } struct st { int x, y, t, id; }; vector<vector<st>> qs; vector<int> ans, co, sum; void dfs(int i, int p, int c, int d, int de) { for (auto &e : qs[i]) { int s = de - sum[e.x] + co[e.x] * e.y; if (e.t == 0) { ans[e.id] += s; } else { ans[e.id] -= 2 * s; } } for (auto &e : G[i]) { if (e.to == p) { continue; } co[e.c]++; sum[e.c] += e.d; dfs(e.to, i, e.c, e.d, de + e.d); } if (p >= 0) { co[c]--; sum[c] -= d; } } int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> n >> q; G.resize(n); qs.resize(n); par.resize(n); dep.resize(n); for (int i = 0; i < n; i++) { par[i].resize(20); } for (int i = 0; i < n - 1; i++) { int a, b, c, d; cin >> a >> b >> c >> d; --a; --b; --c; G[a].push_back({b, c, d}); G[b].push_back({a, c, d}); } build(); for (int i = 0; i < q; i++) { int x, y, u, v; cin >> x >> y >> u >> v; --x; --u; --v; if (u > 0) { qs[u].push_back({x, y, 0, i}); } if (v > 0) { qs[v].push_back({x, y, 0, i}); } if (lca(u, v) > 0) { qs[lca(u, v)].push_back({x, y, 1, i}); } } ans.resize(n); co.resize(n); sum.resize(n); dfs(0, -1, 0, 0, 0); for (int i = 0; i < q; i++) { cout << ans[i] << endl; } }
#include <bits/stdc++.h> #define all(vec) vec.begin(), vec.end() using namespace std; using ll = long long; using P = pair<ll, ll>; constexpr ll INF = (1LL << 30) - 1LL; constexpr ll LINF = (1LL << 60) - 1LL; constexpr double eps = 1e-9; constexpr ll MOD = 1000000007LL; template <typename T> bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; }; template <typename T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; }; template <typename T> ostream &operator<<(ostream &os, vector<T> v) { for (int i = 0; i < v.size(); i++) { os << v[i] << (i + 1 == v.size() ? "\n" : " "); } return os; } template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); } template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); } template <typename T, typename V> typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) { t = v; } template <typename T, typename V> typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) { for (auto &e : t) { fill_v(e, v); } }; int n, q; struct edge { int to, c, d; }; vector<vector<edge>> G; vector<vector<int>> par; vector<int> dep; void ddfs(int i, int p) { for (auto &e : G[i]) { if (e.to == p) continue; par[e.to][0] = i; dep[e.to] = dep[i] + 1; ddfs(e.to, i); } } void build() { par[0][0] = -1; dep[0] = 0; ddfs(0, -1); for (int j = 1; j < 20; j++) { for (int i = 0; i < n; i++) { if (par[i][j - 1] == -1) { par[i][j] = -1; } else { par[i][j] = par[par[i][j - 1]][j - 1]; } } } } int lca(int u, int v) { if (dep[u] > dep[v]) swap(u, v); for (int i = 19; i >= 0; i--) { if (((dep[v] - dep[u]) >> i) & 1) v = par[v][i]; } if (u == v) return u; for (int i = 19; i >= 0; i--) { if (par[u][i] != par[v][i]) { u = par[u][i]; v = par[v][i]; } } return par[u][0]; } struct st { int x, y, t, id; }; vector<vector<st>> qs; vector<int> ans, co, sum; void dfs(int i, int p, int c, int d, int de) { for (auto &e : qs[i]) { int s = de - sum[e.x] + co[e.x] * e.y; if (e.t == 0) { ans[e.id] += s; } else { ans[e.id] -= 2 * s; } } for (auto &e : G[i]) { if (e.to == p) { continue; } co[e.c]++; sum[e.c] += e.d; dfs(e.to, i, e.c, e.d, de + e.d); } if (p >= 0) { co[c]--; sum[c] -= d; } } int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> n >> q; G.resize(n); qs.resize(n); par.resize(n); dep.resize(n); for (int i = 0; i < n; i++) { par[i].resize(20); } for (int i = 0; i < n - 1; i++) { int a, b, c, d; cin >> a >> b >> c >> d; --a; --b; --c; G[a].push_back({b, c, d}); G[b].push_back({a, c, d}); } build(); for (int i = 0; i < q; i++) { int x, y, u, v; cin >> x >> y >> u >> v; --x; --u; --v; if (u > 0) { qs[u].push_back({x, y, 0, i}); } if (v > 0) { qs[v].push_back({x, y, 0, i}); } if (lca(u, v) > 0) { qs[lca(u, v)].push_back({x, y, 1, i}); } } ans.resize(q); co.resize(n); sum.resize(n); dfs(0, -1, 0, 0, 0); for (int i = 0; i < q; i++) { cout << ans[i] << endl; } }
replace
154
155
154
155
0
p02986
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int OO = 1e9; const double EPS = 1e-9; #define ndl cout << '\n' #define sz(v) int(v.size()) #define pb push_back #define mp make_pair #define fs first #define sc second #define present(a, x) (a.find(x) != a.end()) #ifdef LOCAL #define db(...) \ ({ \ cout << "> Line " << __LINE__ << ": "; \ _db(#__VA_ARGS__, __VA_ARGS__); \ }) #else #define db(...) true #endif template <class T> void _db(const char *dbStr, T e) { cout << dbStr << " = " << e << endl; } template <class T, class... L> void _db(const char *dbStr, T e, L... r) { while (*dbStr != ',') cout << *dbStr++; cout << " = " << e << ','; _db(dbStr + 1, r...); } template <class S, class T> ostream &operator<<(ostream &o, const map<S, T> &v) { o << "["; int i = 0; for (const pair<S, T> &pr : v) o << (!i++ ? "" : ", ") << "{" << pr.fs << " : " << pr.sc << "}"; return o << "]"; } template <template <class, class...> class S, class T, class... L> ostream &operator<<(ostream &o, const S<T, L...> &v) { o << "["; int i = 0; for (const auto &e : v) o << (!i++ ? "" : ", ") << e; return o << "]"; } template <class S, class T> ostream &operator<<(ostream &o, const pair<S, T> &pr) { return o << "(" << pr.fs << ", " << pr.sc << ")"; } ostream &operator<<(ostream &o, const string &s) { for (const char &c : s) o << c; return o; } template <class T> using V = vector<T>; template <class T> using VV = V<V<T>>; template <class T> using VVV = VV<V<T>>; using ll = long long; using pii = pair<int, int>; using vi = V<int>; using vii = V<pii>; using vvi = VV<int>; using mii = map<int, int>; using umii = unordered_map<int, int>; using si = set<int>; using usi = unordered_set<int>; int N, Q; V<V<pair<int, pii>>> adj; // (v, (c, d)) vi D, E, W; V<V<pair<int, pair<int, pii>>>> nodeQuery; // (ID, (c, (y, coef.))) vi ans; #define LOG_TWO_N 10 struct SparseTable { vi A; vvi SpT; vvi mnIdx; SparseTable(const vi &_A) { A = _A; int n = A.size(); SpT.assign(n, vi(LOG_TWO_N)); mnIdx.assign(n, vi(LOG_TWO_N)); for (int i = 0; i < n; i++) SpT[i][0] = i, mnIdx[i][0] = i; for (int j = 1; (1 << j) <= n; ++j) for (int i = 0; i + (1 << j) - 1 < n; ++i) if (A[SpT[i][j - 1]] < A[SpT[i + (1 << (j - 1))][j - 1]]) SpT[i][j] = SpT[i][j - 1], mnIdx[i][j] = mnIdx[i][j - 1]; else SpT[i][j] = SpT[i + (1 << (j - 1))][j - 1], mnIdx[i][j] = mnIdx[i + (1 << (j - 1))][j - 1]; } SparseTable() {} int query(int i, int j) { int k = floor(log2(j - i + 1)); if (A[SpT[i][k]] <= A[SpT[j - (1 << k) + 1][k]]) return SpT[i][k]; else return SpT[j - (1 << k) + 1][k]; } int queryIdx(int i, int j) { int k = floor(log2(j - i + 1)); if (A[SpT[i][k]] <= A[SpT[j - (1 << k) + 1][k]]) return mnIdx[i][k]; else return mnIdx[j - (1 << k) + 1][k]; } }; struct LCA { int n, idx; vi dep, seq, fsOcc; SparseTable RMQ; void dfs(int node, int depth, int parent) { fsOcc[node] = idx; seq[idx] = node; dep[idx++] = depth; for (auto &pr : adj[node]) if (pr.fs != parent) { dfs(pr.fs, depth + 1, node); seq[idx] = node; dep[idx++] = depth; } } LCA(int _n, int root) { n = _n + 1; dep.assign(2 * n, 0); seq.assign(2 * n, 0); fsOcc.assign(n, 0); idx = 0; dfs(root, 0, -1); RMQ = SparseTable(dep); } int query(int u, int v) { if (fsOcc[u] > fsOcc[v]) swap(u, v); return seq[RMQ.query(fsOcc[u], fsOcc[v])]; } }; void dfs(int node, int par, int dist) { db(node, E, W); D[node] = dist; for (auto &data : nodeQuery[node]) ans[data.fs] += data.sc.sc.sc * (D[node] + data.sc.sc.fs * E[data.sc.fs] - W[data.sc.fs]); for (auto &pr : adj[node]) if (pr.fs != par) { ++E[pr.sc.fs]; W[pr.sc.fs] += pr.sc.sc; dfs(pr.fs, node, dist + pr.sc.sc); --E[pr.sc.fs]; W[pr.sc.fs] -= pr.sc.sc; } } int main() { #ifdef LOCAL auto stTime = clock(); // freopen("in.txt", "r", stdin); #endif ios::sync_with_stdio(false); cout.precision(10); cin.tie(0); cin >> N >> Q; adj.assign(N + 1, V<pair<int, pii>>()); nodeQuery.assign(N + 1, V<pair<int, pair<int, pii>>>()); D.assign(N + 1, 0); E.assign(N, 0); W.assign(N, 0); int u, v, c, d; for (int i = 0; i < N - 1; ++i) { cin >> u >> v >> c >> d; adj[u].pb(mp(v, mp(c, d))); adj[v].pb(mp(u, mp(c, d))); } LCA lca(N, 1); ans.assign(Q, 0); int x, y; for (int i = 0; i < Q; ++i) { cin >> x >> y >> u >> v; nodeQuery[u].pb(mp(i, mp(x, mp(y, 1)))); nodeQuery[v].pb(mp(i, mp(x, mp(y, 1)))); nodeQuery[lca.query(u, v)].pb(mp(i, mp(x, mp(y, -2)))); } dfs(1, -1, 0); db(D); for (auto &lilAns : ans) cout << lilAns << '\n'; #ifdef LOCAL cout << "\n\n\nExecution time: " << (clock() - stTime) * 1e3 / CLOCKS_PER_SEC << " ms" << endl; #endif return 0; }
#include <bits/stdc++.h> using namespace std; const int OO = 1e9; const double EPS = 1e-9; #define ndl cout << '\n' #define sz(v) int(v.size()) #define pb push_back #define mp make_pair #define fs first #define sc second #define present(a, x) (a.find(x) != a.end()) #ifdef LOCAL #define db(...) \ ({ \ cout << "> Line " << __LINE__ << ": "; \ _db(#__VA_ARGS__, __VA_ARGS__); \ }) #else #define db(...) true #endif template <class T> void _db(const char *dbStr, T e) { cout << dbStr << " = " << e << endl; } template <class T, class... L> void _db(const char *dbStr, T e, L... r) { while (*dbStr != ',') cout << *dbStr++; cout << " = " << e << ','; _db(dbStr + 1, r...); } template <class S, class T> ostream &operator<<(ostream &o, const map<S, T> &v) { o << "["; int i = 0; for (const pair<S, T> &pr : v) o << (!i++ ? "" : ", ") << "{" << pr.fs << " : " << pr.sc << "}"; return o << "]"; } template <template <class, class...> class S, class T, class... L> ostream &operator<<(ostream &o, const S<T, L...> &v) { o << "["; int i = 0; for (const auto &e : v) o << (!i++ ? "" : ", ") << e; return o << "]"; } template <class S, class T> ostream &operator<<(ostream &o, const pair<S, T> &pr) { return o << "(" << pr.fs << ", " << pr.sc << ")"; } ostream &operator<<(ostream &o, const string &s) { for (const char &c : s) o << c; return o; } template <class T> using V = vector<T>; template <class T> using VV = V<V<T>>; template <class T> using VVV = VV<V<T>>; using ll = long long; using pii = pair<int, int>; using vi = V<int>; using vii = V<pii>; using vvi = VV<int>; using mii = map<int, int>; using umii = unordered_map<int, int>; using si = set<int>; using usi = unordered_set<int>; int N, Q; V<V<pair<int, pii>>> adj; // (v, (c, d)) vi D, E, W; V<V<pair<int, pair<int, pii>>>> nodeQuery; // (ID, (c, (y, coef.))) vi ans; #define LOG_TWO_N 25 struct SparseTable { vi A; vvi SpT; vvi mnIdx; SparseTable(const vi &_A) { A = _A; int n = A.size(); SpT.assign(n, vi(LOG_TWO_N)); mnIdx.assign(n, vi(LOG_TWO_N)); for (int i = 0; i < n; i++) SpT[i][0] = i, mnIdx[i][0] = i; for (int j = 1; (1 << j) <= n; ++j) for (int i = 0; i + (1 << j) - 1 < n; ++i) if (A[SpT[i][j - 1]] < A[SpT[i + (1 << (j - 1))][j - 1]]) SpT[i][j] = SpT[i][j - 1], mnIdx[i][j] = mnIdx[i][j - 1]; else SpT[i][j] = SpT[i + (1 << (j - 1))][j - 1], mnIdx[i][j] = mnIdx[i + (1 << (j - 1))][j - 1]; } SparseTable() {} int query(int i, int j) { int k = floor(log2(j - i + 1)); if (A[SpT[i][k]] <= A[SpT[j - (1 << k) + 1][k]]) return SpT[i][k]; else return SpT[j - (1 << k) + 1][k]; } int queryIdx(int i, int j) { int k = floor(log2(j - i + 1)); if (A[SpT[i][k]] <= A[SpT[j - (1 << k) + 1][k]]) return mnIdx[i][k]; else return mnIdx[j - (1 << k) + 1][k]; } }; struct LCA { int n, idx; vi dep, seq, fsOcc; SparseTable RMQ; void dfs(int node, int depth, int parent) { fsOcc[node] = idx; seq[idx] = node; dep[idx++] = depth; for (auto &pr : adj[node]) if (pr.fs != parent) { dfs(pr.fs, depth + 1, node); seq[idx] = node; dep[idx++] = depth; } } LCA(int _n, int root) { n = _n + 1; dep.assign(2 * n, 0); seq.assign(2 * n, 0); fsOcc.assign(n, 0); idx = 0; dfs(root, 0, -1); RMQ = SparseTable(dep); } int query(int u, int v) { if (fsOcc[u] > fsOcc[v]) swap(u, v); return seq[RMQ.query(fsOcc[u], fsOcc[v])]; } }; void dfs(int node, int par, int dist) { db(node, E, W); D[node] = dist; for (auto &data : nodeQuery[node]) ans[data.fs] += data.sc.sc.sc * (D[node] + data.sc.sc.fs * E[data.sc.fs] - W[data.sc.fs]); for (auto &pr : adj[node]) if (pr.fs != par) { ++E[pr.sc.fs]; W[pr.sc.fs] += pr.sc.sc; dfs(pr.fs, node, dist + pr.sc.sc); --E[pr.sc.fs]; W[pr.sc.fs] -= pr.sc.sc; } } int main() { #ifdef LOCAL auto stTime = clock(); // freopen("in.txt", "r", stdin); #endif ios::sync_with_stdio(false); cout.precision(10); cin.tie(0); cin >> N >> Q; adj.assign(N + 1, V<pair<int, pii>>()); nodeQuery.assign(N + 1, V<pair<int, pair<int, pii>>>()); D.assign(N + 1, 0); E.assign(N, 0); W.assign(N, 0); int u, v, c, d; for (int i = 0; i < N - 1; ++i) { cin >> u >> v >> c >> d; adj[u].pb(mp(v, mp(c, d))); adj[v].pb(mp(u, mp(c, d))); } LCA lca(N, 1); ans.assign(Q, 0); int x, y; for (int i = 0; i < Q; ++i) { cin >> x >> y >> u >> v; nodeQuery[u].pb(mp(i, mp(x, mp(y, 1)))); nodeQuery[v].pb(mp(i, mp(x, mp(y, 1)))); nodeQuery[lca.query(u, v)].pb(mp(i, mp(x, mp(y, -2)))); } dfs(1, -1, 0); db(D); for (auto &lilAns : ans) cout << lilAns << '\n'; #ifdef LOCAL cout << "\n\n\nExecution time: " << (clock() - stTime) * 1e3 / CLOCKS_PER_SEC << " ms" << endl; #endif return 0; }
replace
78
79
78
79
0
p02986
C++
Runtime Error
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; ///////////////////////////////////////////////////////////////////////// // LCA ///////////////////////////////////////////////////////////////////////// class LowestCommonAncestor { public: LowestCommonAncestor(); LowestCommonAncestor(const vector<vector<int>> &g, int r); LowestCommonAncestor(const vector<int> g[], int r, int siz); void build(const vector<vector<int>> &g, int r); void build(const vector<int> g[], int r, int siz); int get(int u, int v) const; int getParent(int cur, int u) const; int getDepth(int cur) const; private: void initialize(); void dfs(int cur, int par, int d); static constexpr int MAX_SIZE = 30; vector<vector<int>> graph; vector<vector<int>> parent; vector<int> depth; int bit_; int root; }; LowestCommonAncestor::LowestCommonAncestor(){}; LowestCommonAncestor::LowestCommonAncestor(const vector<vector<int>> &g, int r = -1) : graph(g), root(r) { if (root == -1) root = 0; initialize(); } LowestCommonAncestor::LowestCommonAncestor(const vector<int> g[], int r = -1, int siz = -1) : root(r) { assert(siz > 0); if (root == -1) root = 0; graph.resize(siz); for (int i = 0; i < siz; i++) for (auto &j : g[i]) graph[i].push_back(j); initialize(); } void LowestCommonAncestor::build(const vector<vector<int>> &g, int r = -1) { root = r; graph = g; if (root == -1) root = 0; initialize(); } void LowestCommonAncestor::build(const vector<int> g[], int r = -1, int siz = -1) { assert(siz > 0); root = r; graph.resize(siz); for (int i = 0; i < siz; i++) for (auto &j : g[i]) graph[i].push_back(j); initialize(); } int LowestCommonAncestor::get(int u, int v) const { assert(u >= 0 && u < (int)graph.size()); assert(v >= 0 && v < (int)graph.size()); if (depth[u] > depth[v]) swap(u, v); for (int i = 0; i < bit_; ++i) if (((depth[v] - depth[u]) >> i) & i) v = parent[v][i]; if (u == v) return u; for (int i = bit_ - 1; i >= 0; --i) if (parent[u][i] != parent[v][i]) u = parent[u][i], v = parent[v][i]; return parent[u][0]; } int LowestCommonAncestor::getParent(int cur, int u = 0) const { assert(cur >= 0 && cur < (int)graph.size()); assert(u >= 0 && (int)graph[cur].size() > u); return parent[cur][u]; } int LowestCommonAncestor::getDepth(int cur) const { assert(cur >= 0 && cur < (int)depth.size()); return depth[cur]; } void LowestCommonAncestor::initialize() { bit_ = 1; while ((1 << bit_) < (int)graph.size()) ++bit_; parent = vector<vector<int>>((int)graph.size(), vector<int>(bit_)); depth.assign((int)graph.size(), -1); dfs(root, -1, 0); for (int i = 0; i < bit_ - 1; ++i) { for (int v = 0; v < (int)graph.size(); ++v) { if (depth[v] == -1) continue; if (parent[v][i] < 0) parent[v][i + 1] = -1; else parent[v][i + 1] = parent[parent[v][i]][i]; } } } void LowestCommonAncestor::dfs(int cur, int par, int d) { parent[cur][0] = par; depth[cur] = d; for (auto &child : graph[cur]) if (child != par) dfs(child, cur, d + 1); } using LCA = LowestCommonAncestor; ///////////////////////////////////////////////////////////////////////// // END LCA ///////////////////////////////////////////////////////////////////////// int n; vector<vector<int>> g1[101010]; vector<int> g2[101010]; vector<int> need[101010]; map<int, int> res_cnt[101010], res_sum[101010]; int dist[101010]; int col_cnt[101010], col_sum[101010]; void dfs(int cur, int par) { for (int c : need[cur]) { res_cnt[cur][c] = col_cnt[c]; res_sum[cur][c] = col_sum[c]; } for (auto nxt : g1[cur]) { int child = nxt[0]; int c = nxt[1]; int d = nxt[2]; if (child == par) continue; dist[child] = dist[cur] + d; col_cnt[c]++; col_sum[c] += d; dfs(child, cur); col_cnt[c]--; col_sum[c] -= d; } } signed main() { ios::sync_with_stdio(false); cin.tie(0); int q; cin >> n >> q; for (int i = 0; i + 1 < n; i++) { int a, b, c, d; cin >> a >> b >> c >> d; a--; b--; c--; g1[a].push_back({b, c, d}); g1[b].push_back({a, c, d}); g2[a].push_back(b); g2[b].push_back(a); } LCA lca(g2, 0, 101010); vector<int> inx(q), iny(q), inu(q), inv(q), inw(q); for (int i = 0; i < q; i++) { cin >> inx[i] >> iny[i] >> inu[i] >> inv[i]; inx[i]--; inu[i]--; inv[i]--; inw[i] = lca.get(inu[i], inv[i]); need[inu[i]].push_back(inx[i]); need[inv[i]].push_back(inx[i]); need[inw[i]].push_back(inx[i]); } dfs(0, -1); for (int i = 0; i < q; i++) { int du = dist[inu[i]] - res_sum[inu[i]][inx[i]] + res_cnt[inu[i]][inx[i]] * iny[i]; int dv = dist[inv[i]] - res_sum[inv[i]][inx[i]] + res_cnt[inv[i]][inx[i]] * iny[i]; int dw = dist[inw[i]] - res_sum[inw[i]][inx[i]] + res_cnt[inw[i]][inx[i]] * iny[i]; cout << du + dv - 2 * dw << '\n'; } return 0; }
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; ///////////////////////////////////////////////////////////////////////// // LCA ///////////////////////////////////////////////////////////////////////// class LowestCommonAncestor { public: LowestCommonAncestor(); LowestCommonAncestor(const vector<vector<int>> &g, int r); LowestCommonAncestor(const vector<int> g[], int r, int siz); void build(const vector<vector<int>> &g, int r); void build(const vector<int> g[], int r, int siz); int get(int u, int v) const; int getParent(int cur, int u) const; int getDepth(int cur) const; private: void initialize(); void dfs(int cur, int par, int d); static constexpr int MAX_SIZE = 30; vector<vector<int>> graph; vector<vector<int>> parent; vector<int> depth; int bit_; int root; }; LowestCommonAncestor::LowestCommonAncestor(){}; LowestCommonAncestor::LowestCommonAncestor(const vector<vector<int>> &g, int r = -1) : graph(g), root(r) { if (root == -1) root = 0; initialize(); } LowestCommonAncestor::LowestCommonAncestor(const vector<int> g[], int r = -1, int siz = -1) : root(r) { assert(siz > 0); if (root == -1) root = 0; graph.resize(siz); for (int i = 0; i < siz; i++) for (auto &j : g[i]) graph[i].push_back(j); initialize(); } void LowestCommonAncestor::build(const vector<vector<int>> &g, int r = -1) { root = r; graph = g; if (root == -1) root = 0; initialize(); } void LowestCommonAncestor::build(const vector<int> g[], int r = -1, int siz = -1) { assert(siz > 0); root = r; graph.resize(siz); for (int i = 0; i < siz; i++) for (auto &j : g[i]) graph[i].push_back(j); initialize(); } int LowestCommonAncestor::get(int u, int v) const { assert(u >= 0 && u < (int)graph.size()); assert(v >= 0 && v < (int)graph.size()); if (depth[u] > depth[v]) swap(u, v); for (int i = 0; i < bit_; ++i) if (((depth[v] - depth[u]) >> i) & 1) v = parent[v][i]; if (u == v) return u; for (int i = bit_ - 1; i >= 0; --i) if (parent[u][i] != parent[v][i]) u = parent[u][i], v = parent[v][i]; return parent[u][0]; } int LowestCommonAncestor::getParent(int cur, int u = 0) const { assert(cur >= 0 && cur < (int)graph.size()); assert(u >= 0 && (int)graph[cur].size() > u); return parent[cur][u]; } int LowestCommonAncestor::getDepth(int cur) const { assert(cur >= 0 && cur < (int)depth.size()); return depth[cur]; } void LowestCommonAncestor::initialize() { bit_ = 1; while ((1 << bit_) < (int)graph.size()) ++bit_; parent = vector<vector<int>>((int)graph.size(), vector<int>(bit_)); depth.assign((int)graph.size(), -1); dfs(root, -1, 0); for (int i = 0; i < bit_ - 1; ++i) { for (int v = 0; v < (int)graph.size(); ++v) { if (depth[v] == -1) continue; if (parent[v][i] < 0) parent[v][i + 1] = -1; else parent[v][i + 1] = parent[parent[v][i]][i]; } } } void LowestCommonAncestor::dfs(int cur, int par, int d) { parent[cur][0] = par; depth[cur] = d; for (auto &child : graph[cur]) if (child != par) dfs(child, cur, d + 1); } using LCA = LowestCommonAncestor; ///////////////////////////////////////////////////////////////////////// // END LCA ///////////////////////////////////////////////////////////////////////// int n; vector<vector<int>> g1[101010]; vector<int> g2[101010]; vector<int> need[101010]; map<int, int> res_cnt[101010], res_sum[101010]; int dist[101010]; int col_cnt[101010], col_sum[101010]; void dfs(int cur, int par) { for (int c : need[cur]) { res_cnt[cur][c] = col_cnt[c]; res_sum[cur][c] = col_sum[c]; } for (auto nxt : g1[cur]) { int child = nxt[0]; int c = nxt[1]; int d = nxt[2]; if (child == par) continue; dist[child] = dist[cur] + d; col_cnt[c]++; col_sum[c] += d; dfs(child, cur); col_cnt[c]--; col_sum[c] -= d; } } signed main() { ios::sync_with_stdio(false); cin.tie(0); int q; cin >> n >> q; for (int i = 0; i + 1 < n; i++) { int a, b, c, d; cin >> a >> b >> c >> d; a--; b--; c--; g1[a].push_back({b, c, d}); g1[b].push_back({a, c, d}); g2[a].push_back(b); g2[b].push_back(a); } LCA lca(g2, 0, 101010); vector<int> inx(q), iny(q), inu(q), inv(q), inw(q); for (int i = 0; i < q; i++) { cin >> inx[i] >> iny[i] >> inu[i] >> inv[i]; inx[i]--; inu[i]--; inv[i]--; inw[i] = lca.get(inu[i], inv[i]); need[inu[i]].push_back(inx[i]); need[inv[i]].push_back(inx[i]); need[inw[i]].push_back(inx[i]); } dfs(0, -1); for (int i = 0; i < q; i++) { int du = dist[inu[i]] - res_sum[inu[i]][inx[i]] + res_cnt[inu[i]][inx[i]] * iny[i]; int dv = dist[inv[i]] - res_sum[inv[i]][inx[i]] + res_cnt[inv[i]][inx[i]] * iny[i]; int dw = dist[inw[i]] - res_sum[inw[i]][inx[i]] + res_cnt[inw[i]][inx[i]] * iny[i]; cout << du + dv - 2 * dw << '\n'; } return 0; }
replace
71
72
71
72
-11