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
p02888
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int INF = 1e9; const ll LINF = 1e18; const int MOD = 1e9 + 7; #define REP(i, n) for (int(i) = 0; (i) < (n); (i)++) #define REPR(i, n) for (int(i) = (n - 1); (i) >= 0; i--) #define FOR(i, m, n) for (int(i) = (m); (i) < (n); i++) #define FORR(i, m, n) for (int(i) = (m - 1); (i) >= (n); i--) #define PRINT(x) \ { cout << (x) << endl; } int main() { int n; cin >> n; vector<int> L; REP(i, n) { int a; cin >> a; L.push_back(a); } int ans = 0; REP(i, n - 2) { FOR(j, i + 1, n - 1) { FOR(k, j + 1, n) { int a = L[i], b = L[j], c = L[k]; if (a < b + c and b < a + c and c < a + b) ans++; } } } PRINT(ans) }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int INF = 1e9; const ll LINF = 1e18; const int MOD = 1e9 + 7; #define REP(i, n) for (int(i) = 0; (i) < (n); (i)++) #define REPR(i, n) for (int(i) = (n - 1); (i) >= 0; i--) #define FOR(i, m, n) for (int(i) = (m); (i) < (n); i++) #define FORR(i, m, n) for (int(i) = (m - 1); (i) >= (n); i--) #define PRINT(x) \ { cout << (x) << endl; } int main() { int n; cin >> n; vector<int> L; REP(i, n) { int a; cin >> a; L.push_back(a); } sort(L.begin(), L.end()); int ans = 0; REP(i, n - 2) { FOR(j, i + 1, n - 1) { FOR(k, j + 1, n) { int a = L[i], b = L[j], c = L[k]; if (a < b + c and b < a + c and c < a + b) ans++; } } } PRINT(ans) }
insert
24
24
24
25
TLE
p02888
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n]; int counter = 0; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n - 2; i++) { for (int j = i + 1; j < n - 1; j++) { for (int k = j + 1; k < n; k++) { if (a[i] + a[j] > a[k] && a[j] + a[k] > a[i] && a[k] + a[i] > a[j]) counter++; } } } cout << counter << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n]; int counter = 0; for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); // int o,p,mid; int i = 0; int j = 1; int k; while (i < n - 2) { k = j + 1; while (k < n && a[i] + a[j] > a[k]) { k++; } counter += k - j - 1; j++; if (j > n - 2) { i++; j = i + 1; } } cout << counter << endl; return 0; }
replace
10
16
10
25
TLE
p02888
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; #define out(a) cout << a << " " int main() { int N, cnt = 0; cin >> N; vector<int> ls(N); rep(i, N) cin >> ls[i]; for (int i = 0; i < N; i++) for (int j = i + 1; j < N; j++) for (int k = j + 1; k < N; k++) { if (ls[i] < ls[j] + ls[k] && ls[j] < ls[k] + ls[i] && ls[k] < ls[i] + ls[j]) cnt++; } cout << cnt << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; typedef long long ll; #define out(a) cout << a << " " int main() { int N, cnt = 0; cin >> N; vector<int> ls(N); rep(i, N) cin >> ls[i]; sort(ls.begin(), ls.end()); for (int i = 0; i < N; i++) for (int j = i + 1; j < N; j++) for (int k = j + 1; k < N; k++) { if (ls[i] < ls[j] + ls[k] && ls[j] < ls[k] + ls[i] && ls[k] < ls[i] + ls[j]) cnt++; } cout << cnt << endl; return 0; }
insert
11
11
11
12
TLE
p02888
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[1000]; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); int s = 0; for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n - 1; j++) { int x = lower_bound(a, a + n, a[i] + a[j]) - a - j - 1; s += x; // cout << x << endl; } } cout << s; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[2000]; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); int s = 0; for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n - 1; j++) { int x = lower_bound(a, a + n, a[i] + a[j]) - a - j - 1; s += x; // cout << x << endl; } } cout << s; }
replace
6
7
6
7
0
p02888
C++
Runtime Error
#include "bits/stdc++.h" using namespace std; int a[1000 + 5]; int binary(int l, int r, int x) { int ans = l - 1; while (l <= r) { int mid = (l + r) >> 1; if (a[mid] < x) { ans = mid, l = mid + 1; } else r = mid - 1; } return ans; } int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } int cont = 0; sort(a + 1, a + n + 1); for (int i = 1; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { int p = a[i] + a[j]; int q = binary(j + 1, n, p); cont += (q - j); } } cout << cont << endl; return 0; }
#include "bits/stdc++.h" using namespace std; int a[2000 + 5]; int binary(int l, int r, int x) { int ans = l - 1; while (l <= r) { int mid = (l + r) >> 1; if (a[mid] < x) { ans = mid, l = mid + 1; } else r = mid - 1; } return ans; } int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } int cont = 0; sort(a + 1, a + n + 1); for (int i = 1; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { int p = a[i] + a[j]; int q = binary(j + 1, n, p); cont += (q - j); } } cout << cont << endl; return 0; }
replace
3
4
3
4
0
p02888
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { int n, ans = 0; cin >> n; vector<int> l(n); for (int i = 0; i < n; i++) { cin >> l[i]; } for (int i = 0; i < n - 2; i++) { for (int j = i + 1; j < n - 1; j++) { for (int k = j + 1; k < n; k++) { if (l[i] < l[j] + l[k] && l[j] < l[k] + l[i] && l[k] < l[i] + l[j]) { ans += 1; } } } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int n, ans = 0; cin >> n; vector<int> l(n); for (int i = 0; i < n; i++) { cin >> l[i]; } sort(l.begin(), l.end()); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { int k = lower_bound(l.begin(), l.end(), l[i] + l[j]) - l.begin(); ans += max(k - (j + 1), 0); } } cout << ans << endl; }
replace
9
16
9
14
TLE
p02888
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> using namespace std; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; ++i) cin >> a[i]; int ans = 0; sort(a, a + n); for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { for (int k = j + 1; k < n; ++k) { int len = a[i] + a[j] + a[k]; int ma = max(a[i], max(a[j], a[k])); int rest = len - ma; if (ma < rest) ++ans; else break; } } } cout << ans; return 0; }
#include <algorithm> #include <iostream> using namespace std; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; ++i) cin >> a[i]; int ans = 0; sort(a, a + n); for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { for (int k = j + 1; k < n; ++k) { if (a[k] < a[i] + a[j]) ++ans; else break; } } } cout << ans; return 0; }
replace
14
18
14
15
TLE
p02888
C++
Runtime Error
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define all(a) a.begin(), a.end() #define F first #define S second #define pb push_back #define ll long long #define ld long double #define mkp make_pair #define sz(x) (int)x.size() #define pi pair<long long, long long> #define watch(x) \ cout << (#x) << " is " << (x) << endl; \ cout.flush() #define endl '\n' #define _ << ' ' #define FAST_LOG2(x) \ (sizeof(unsigned long) * 8 - 1 - __builtin_clzl((unsigned long)(x))) #define clog2(x) (((x) - (1 << FAST_LOG2(x))) ? FAST_LOG2(x) + 1 : FAST_LOG2(x)) using namespace __gnu_pbds; using namespace std; typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> idxset; // find_by_order(i) : iterator to the value at index i, order_of_key(x) : the // index of value x const ll mod = (ll)1e9 + 7; const ll INF = (ll)1e9 + 1; int n, a[2001], ans = 0; int f(int x) { int l = 0, r = n; int ret = -1; while (l < r) { int mid = (l + r) / 2; if (a[mid] < x) { ret = mid; l = mid + 1; } else { r = mid; } } return ret; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); #ifndef ONLINE_JUDGE freopen("B.txt", "r", stdin); #endif cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { int x = f(a[i] + a[j]); if (x > j) ans += x - j; } } cout << ans; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define all(a) a.begin(), a.end() #define F first #define S second #define pb push_back #define ll long long #define ld long double #define mkp make_pair #define sz(x) (int)x.size() #define pi pair<long long, long long> #define watch(x) \ cout << (#x) << " is " << (x) << endl; \ cout.flush() #define endl '\n' #define _ << ' ' #define FAST_LOG2(x) \ (sizeof(unsigned long) * 8 - 1 - __builtin_clzl((unsigned long)(x))) #define clog2(x) (((x) - (1 << FAST_LOG2(x))) ? FAST_LOG2(x) + 1 : FAST_LOG2(x)) using namespace __gnu_pbds; using namespace std; typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> idxset; // find_by_order(i) : iterator to the value at index i, order_of_key(x) : the // index of value x const ll mod = (ll)1e9 + 7; const ll INF = (ll)1e9 + 1; int n, a[2001], ans = 0; int f(int x) { int l = 0, r = n; int ret = -1; while (l < r) { int mid = (l + r) / 2; if (a[mid] < x) { ret = mid; l = mid + 1; } else { r = mid; } } return ret; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); #ifndef ONLINE_JUDGE // freopen("B.txt", "r", stdin); #endif cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { int x = f(a[i] + a[j]); if (x > j) ans += x - j; } } cout << ans; }
replace
51
52
51
52
0
p02888
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int> A(N); for (int i = 0; i < N; i++) cin >> A[i]; long long ans = 0; for (int i = 0; i < N; i++) for (int j = i + 1; j < N; j++) for (int k = j + 1; k < N; k++) if (A[i] + A[j] > A[k] && A[i] + A[k] > A[j] && A[k] + A[j] > A[i]) ans++; cout << ans << endl; }
#include <algorithm> #include <assert.h> #include <bitset> #include <cassert> #include <cctype> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdint> #include <cstdio> #include <ctime> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <limits> #include <list> #include <map> #include <memory> #include <queue> #include <random> #include <set> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; int main() { int N; cin >> N; vector<int> A(N); for (int i = 0; i < N; i++) cin >> A[i]; long long ans = 0; for (int i = 0; i < N; i++) for (int j = i + 1; j < N; j++) for (int k = j + 1; k < N; k++) if (A[i] + A[j] > A[k] && A[i] + A[k] > A[j] && A[k] + A[j] > A[i]) ans++; cout << ans << endl; }
replace
0
1
0
34
TLE
p02888
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) #define sz(x) int(x.size()) typedef long long ll; typedef pair<int, int> P; int main() { int n; cin >> n; vector<int> l(n); rep(i, n) cin >> l[i]; sort(l.begin(), l.end()); int res = 0; for (int i = 0; i < n; i) { for (int j = i + 1; j < n; j) { auto itr = lower_bound(l.begin(), l.end(), l[i] + l[j]) - l.begin(); int k = itr - (j + 1); res += max(k, 0); } } cout << res << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) #define sz(x) int(x.size()) typedef long long ll; typedef pair<int, int> P; int main() { int n; cin >> n; vector<int> l(n); rep(i, n) cin >> l[i]; sort(l.begin(), l.end()); int res = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { auto itr = lower_bound(l.begin(), l.end(), l[i] + l[j]) - l.begin(); int k = itr - (j + 1); res += max(k, 0); } } cout << res << endl; return 0; }
replace
14
16
14
16
TLE
p02888
C++
Time Limit Exceeded
#include <algorithm> #include <cstdlib> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <string> #include <vector> using namespace std; typedef long long ll; #define rep(i, n) for (ll i = 0; i < n; i++) const ll INF = 1LL << 60; signed main() { ll n; cin >> n; vector<ll> a(n); rep(i, n) cin >> a[i]; sort(a.rbegin(), a.rend()); ll ans = 0; rep(i, n - 2) { for (int j = i + 1; j < n - 1; j++) { for (int k = j + 1; k < n; k++) { if (a[i] < a[j] + a[k] && a[j] < a[i] + a[k] && a[k] < a[i] + a[j]) { ans++; } else { break; } } } } cout << ans; }
#include <algorithm> #include <cstdlib> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <string> #include <vector> using namespace std; typedef long long ll; #define rep(i, n) for (ll i = 0; i < n; i++) const ll INF = 1LL << 60; signed main() { ll n; cin >> n; vector<ll> a(n); rep(i, n) cin >> a[i]; sort(a.begin(), a.end()); ll ans = 0; rep(i, n - 2) { for (int j = i + 1; j < n - 1; j++) { for (int k = j + 1; k < n; k++) { if (a[i] < a[j] + a[k] && a[j] < a[i] + a[k] && a[k] < a[i] + a[j]) { ans++; } else { break; } } } } cout << ans; }
replace
26
27
26
27
TLE
p02888
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; int d[N]; vector<int> L; for (int i = 0; i < N; i++) { cin >> d[i]; L.push_back(d[i]); } sort(L.begin(), L.end()); long long int ans = 0; for (vector<int>::iterator e = L.end(); e - 1 != L.begin(); e--) { for (vector<int>::iterator f = L.end() - 1; *f * 2 > *e; f--) { for (vector<int>::iterator g = f - 1; *g + *f > *e; g--) { if (abs(*g - *f) < *e) ans++; } } } cout << ans; }
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; int d[N]; vector<int> L; for (int i = 0; i < N; i++) { cin >> d[i]; L.push_back(d[i]); } sort(L.begin(), L.end()); long long int ans = 0; for (vector<int>::iterator e = L.end() - 1; e - 1 != L.begin(); e--) { for (vector<int>::iterator f = e - 1; *f * 2 > *e; f--) { for (vector<int>::iterator g = f - 1; *g + *f > *e; g--) { if (abs(*g - *f) < *e) ans++; } } } cout << ans; }
replace
13
15
13
15
-11
p02888
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (ll i = 0; i < n; i++) #define eb(t) emplace_back(t) typedef long long ll; typedef long long unsigned int llu; ll MOD = 1000000007; ll INF = 1000000009; ll l[1010]; void solve() { ll n; cin >> n; rep(i, n) { cin >> l[i]; } l[n] = 2000; sort(l, l + n); ll ans = 0; for (int i = n - 1; i >= 0; i--) { for (int j = i - 1; j >= 0; j--) { // l[0]~l[j-1]までの間で, l[i]-l[j]より大きいもの auto itr = upper_bound(l, l + n, l[i] - l[j]); // cout << "i: " << i << ", j:" << j << endl; // cout << *itr << endl; // cout << max(int(0), int(distance(itr,l+j))) << endl; ans += max(int(0), int(distance(itr, l + j))); } } cout << ans << endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (ll i = 0; i < n; i++) #define eb(t) emplace_back(t) typedef long long ll; typedef long long unsigned int llu; ll MOD = 1000000007; ll INF = 1000000009; ll l[2010]; void solve() { ll n; cin >> n; rep(i, n) { cin >> l[i]; } l[n] = 2000; sort(l, l + n); ll ans = 0; for (int i = n - 1; i >= 0; i--) { for (int j = i - 1; j >= 0; j--) { // l[0]~l[j-1]までの間で, l[i]-l[j]より大きいもの auto itr = upper_bound(l, l + n, l[i] - l[j]); // cout << "i: " << i << ", j:" << j << endl; // cout << *itr << endl; // cout << max(int(0), int(distance(itr,l+j))) << endl; ans += max(int(0), int(distance(itr, l + j))); } } cout << ans << endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); solve(); return 0; }
replace
9
10
9
10
0
p02888
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define pie 3.141592653589793238462643383279 #define int long long #define mod 1000000007 #define INF LLONG_MAX / 5 #define all(v) v.begin(), v.end() #define P pair<int, int> #define S second #define F first #define f(i, n) for (int i = 0; i < n; i++) int x[1000]; signed main() { int n, ans = 0; cin >> n; f(i, n) cin >> x[i]; sort(x, x + n); f(i, n) { for (int j = i + 1; j < n; j++) { ans += lower_bound(x, x + n, x[i] + x[j]) - x - j - 1; } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define pie 3.141592653589793238462643383279 #define int long long #define mod 1000000007 #define INF LLONG_MAX / 5 #define all(v) v.begin(), v.end() #define P pair<int, int> #define S second #define F first #define f(i, n) for (int i = 0; i < n; i++) int x[2000]; signed main() { int n, ans = 0; cin >> n; f(i, n) cin >> x[i]; sort(x, x + n); f(i, n) { for (int j = i + 1; j < n; j++) { ans += lower_bound(x, x + n, x[i] + x[j]) - x - j - 1; } } cout << ans << endl; }
replace
11
12
11
12
0
p02888
C++
Runtime Error
#include <algorithm> #include <complex> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <string.h> #include <string> #include <time.h> #include <vector> using namespace std; #define rep(i, a, n) for (int i = (a); i < (n); i++) #define ll long long #define llint long long int #define reverse(v) reverse(v.begin(), v.end()); #define Yes(ans) \ if (ans) \ cout << "Yes" << endl; \ else \ cout << "No" << endl; #define YES(ans) \ if (ans) \ cout << "YES" << endl; \ else \ cout << "NO" << endl; #define hei(a) vector<a> #define whei(a) vector<vector<a>> #define UF UnionFind #define Pint pair<int, int> #define keta(a) fixed << setprecision(a) constexpr auto INF = 1000000000000000000; constexpr auto mod = 1000000007; // 辞書順はnext_permutation( begin( v ), end( v ) );やで! struct edge { int to, cost; }; ll gcd(ll a, ll b) { if (a < b) swap(a, b); if (b == 0) return a; return gcd(b, a % b); } long long modpow(long long a, long long n, long long mod) { long long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } // mod. m での a の逆元 a^{-1} を計算するよ! long long modinv(long long a, long long m) { long long b = m, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } // aCbをmod.mで割った余りを求める ll int c(ll int a, ll int b, ll int m) { ll int ans = 1; for (ll int i = 0; i < b; i++) { ans *= a - i; ans %= m; } for (ll int i = 1; i <= b; i++) { ans *= modinv(i, m); ans %= m; } return ans; } struct UnionFind { vector<int> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2 UnionFind(int N) : par(N) { // 最初は全てが根であるとして初期化 for (int i = 0; i < N; i++) par[i] = i; } int size(int a) { return par[root(a)]; } int root(int x) { // データxが属する木の根を再帰で得る:root(x) = {xの木の根} if (par[x] == x) return x; return par[x] = root(par[x]); } void unite(int x, int y) { // xとyの木を併合 int rx = root(x); // xの根をrx int ry = root(y); // yの根をry if (rx == ry) return; // xとyの根が同じ(=同じ木にある)時はそのまま par[rx] = ry; // xとyの根が同じでない(=同じ木にない)時:xの根rxをyの根ryにつける } bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す int rx = root(x); int ry = root(y); return rx == ry; } }; int main() { int n; cin >> n; int ans = 0; int a[301]; rep(i, 0, n) cin >> a[i]; sort(a, a + n); for (int i = n - 1; i >= 0; i--) { for (int j = i - 1; j >= 0; j--) { ans += j - (lower_bound(a, a + j, a[i] - a[j] + 1) - a); } } cout << ans << endl; return 0; }
#include <algorithm> #include <complex> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <string.h> #include <string> #include <time.h> #include <vector> using namespace std; #define rep(i, a, n) for (int i = (a); i < (n); i++) #define ll long long #define llint long long int #define reverse(v) reverse(v.begin(), v.end()); #define Yes(ans) \ if (ans) \ cout << "Yes" << endl; \ else \ cout << "No" << endl; #define YES(ans) \ if (ans) \ cout << "YES" << endl; \ else \ cout << "NO" << endl; #define hei(a) vector<a> #define whei(a) vector<vector<a>> #define UF UnionFind #define Pint pair<int, int> #define keta(a) fixed << setprecision(a) constexpr auto INF = 1000000000000000000; constexpr auto mod = 1000000007; // 辞書順はnext_permutation( begin( v ), end( v ) );やで! struct edge { int to, cost; }; ll gcd(ll a, ll b) { if (a < b) swap(a, b); if (b == 0) return a; return gcd(b, a % b); } long long modpow(long long a, long long n, long long mod) { long long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } // mod. m での a の逆元 a^{-1} を計算するよ! long long modinv(long long a, long long m) { long long b = m, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } // aCbをmod.mで割った余りを求める ll int c(ll int a, ll int b, ll int m) { ll int ans = 1; for (ll int i = 0; i < b; i++) { ans *= a - i; ans %= m; } for (ll int i = 1; i <= b; i++) { ans *= modinv(i, m); ans %= m; } return ans; } struct UnionFind { vector<int> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2 UnionFind(int N) : par(N) { // 最初は全てが根であるとして初期化 for (int i = 0; i < N; i++) par[i] = i; } int size(int a) { return par[root(a)]; } int root(int x) { // データxが属する木の根を再帰で得る:root(x) = {xの木の根} if (par[x] == x) return x; return par[x] = root(par[x]); } void unite(int x, int y) { // xとyの木を併合 int rx = root(x); // xの根をrx int ry = root(y); // yの根をry if (rx == ry) return; // xとyの根が同じ(=同じ木にある)時はそのまま par[rx] = ry; // xとyの根が同じでない(=同じ木にない)時:xの根rxをyの根ryにつける } bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す int rx = root(x); int ry = root(y); return rx == ry; } }; int main() { int n; cin >> n; int ans = 0; int a[2001]; rep(i, 0, n) cin >> a[i]; sort(a, a + n); for (int i = n - 1; i >= 0; i--) { for (int j = i - 1; j >= 0; j--) { ans += j - (lower_bound(a, a + j, a[i] - a[j] + 1) - a); } } cout << ans << endl; return 0; }
replace
125
126
125
126
0
p02888
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int l[1111]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> l[i]; sort(l, l + n); int r = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { int p = lower_bound(l, l + n, l[i] + l[j]) - l; r += max(0, p - j - 1); } } cout << r << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int l[2222]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> l[i]; sort(l, l + n); int r = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { int p = lower_bound(l, l + n, l[i] + l[j]) - l; r += max(0, p - j - 1); } } cout << r << endl; return 0; }
replace
4
5
4
5
0
p02888
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, n) for (int i = 0; i < (int)(n); i++) int n; vector<int> l; bool isok(int a, int b, int c) { if (l[a] + l[b] > l[c]) return true; else return false; } int nibutan(int a, int b) { int ok = b, ng = n; while (ng - ok > 1) { int naka = (ng + ok) / 2; if (isok(a, b, naka)) ok = naka; else { ng = naka; } } return ok - b; } int main() { cin >> n; rep(i, n) { cin >> l[i]; } ll ans = 0; sort(l.begin(), l.end()); for (int a = 0; a < n; a++) { for (int b = a + 1; b < n; b++) { ans += nibutan(a, b); } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, n) for (int i = 0; i < (int)(n); i++) int n; vector<int> l; bool isok(int a, int b, int c) { if (l[a] + l[b] > l[c]) return true; else return false; } int nibutan(int a, int b) { int ok = b, ng = n; while (ng - ok > 1) { int naka = (ng + ok) / 2; if (isok(a, b, naka)) ok = naka; else { ng = naka; } } return ok - b; } int main() { cin >> n; l.resize(n); rep(i, n) { cin >> l[i]; } ll ans = 0; sort(l.begin(), l.end()); for (int a = 0; a < n; a++) { for (int b = a + 1; b < n; b++) { ans += nibutan(a, b); } } cout << ans << endl; }
insert
30
30
30
31
-11
p02888
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <iostream> #include <string> #include <vector> using namespace std; int main() { int n, count = 0; cin >> n; vector<int> l(n); for (int i = 0; i < n; i++) { cin >> l[i]; } for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { for (int k = j + 1; k < n; k++) { if (l[i] < l[j] + l[k] && l[j] < l[k] + l[i] && l[k] < l[j] + l[i]) { count++; } } } } cout << count << endl; }
#include <algorithm> #include <cmath> #include <iostream> #include <string> #include <vector> using namespace std; int main() { int n, count = 0; cin >> n; vector<int> l(n); for (int i = 0; i < n; i++) { cin >> l[i]; } sort(l.begin(), l.end()); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { for (int k = j + 1; k < n; k++) { if (l[i] < l[j] + l[k] && l[j] < l[k] + l[i] && l[k] < l[j] + l[i]) { count++; } } } } cout << count << endl; }
insert
13
13
13
14
TLE
p02888
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define REP(type, i, a, b) for (type i = a; i < b; i++) typedef long long ll; using namespace std; int main(void) { ll N, ans; scanf("%lld", &N); vector<ll> L(N); REP(int, i, 0, N) scanf("%lld", &L.at(i)); // sort(L.begin(),L.end()); ans = 0; REP(int, i, 0, N - 2) { REP(int, j, i + 1, N - 1) { REP(int, k, j + 1, N) { // cout << i << j << k << endl; if (L[i] < L[j] + L[k] && L[j] < L[i] + L[k] && L[k] < L[i] + L[j]) { ans++; } } } } printf("%lld\n", ans); return 0; }
#include <bits/stdc++.h> #define REP(type, i, a, b) for (type i = a; i < b; i++) typedef long long ll; using namespace std; int main(void) { ll N, ans; scanf("%lld", &N); vector<ll> L(N); REP(int, i, 0, N) scanf("%lld", &L.at(i)); sort(L.begin(), L.end()); ans = 0; REP(int, i, 0, N - 2) { REP(int, j, i + 1, N - 1) { REP(int, k, j + 1, N) { // cout << i << j << k << endl; if (L[i] < L[j] + L[k] && L[j] < L[i] + L[k] && L[k] < L[i] + L[j]) { ans++; } } } } printf("%lld\n", ans); return 0; }
replace
12
13
12
13
TLE
p02888
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <cstdio> #include <functional> #include <iostream> #include <map> #include <string> #include <vector> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, l[3000]; int cnt = 0; int i, j, k; cin >> n; for (i = 0; i < n; i++) cin >> l[i]; // sort(l, l + n); for (i = 0; i < n - 2; i++) { for (j = i + 1; j < n - 1; j++) { for (k = j + 1; k < n; k++) { if (l[i] < l[j] + l[k] && l[k] < l[j] + l[i] && l[j] < l[i] + l[k]) cnt++; } } } cout << cnt << endl; return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <functional> #include <iostream> #include <map> #include <string> #include <vector> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, l[3000]; int cnt = 0; int i, j, k; cin >> n; for (i = 0; i < n; i++) cin >> l[i]; sort(l, l + n); for (i = 0; i < n - 2; i++) { for (j = i + 1; j < n - 1; j++) { for (k = j + 1; k < n; k++) { if (l[i] < l[j] + l[k] && l[k] < l[j] + l[i] && l[j] < l[i] + l[k]) cnt++; } } } cout << cnt << endl; return 0; }
replace
23
24
23
24
TLE
p02888
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, N) for (int i = 0; i < (int)N; i++) using namespace std; int main() { int64_t N, b, e, m, count = 0; vector<int64_t> L(1024); cin >> N; rep(i, N) { cin >> L.at(i); } sort(L.begin(), L.end(), greater<int>()); rep(i, N - 2) { for (int64_t j = i + 1; j < N - 1; j++) { b = j; e = N; while (e - b > 1) { m = (b + e) / 2; if (L.at(i) < L.at(j) + L.at(m)) b = m; else e = m; } count += b - j; } } cout << count << endl; }
#include <bits/stdc++.h> #define rep(i, N) for (int i = 0; i < (int)N; i++) using namespace std; int main() { int64_t N, b, e, m, count = 0; vector<int64_t> L(pow(2, 16)); cin >> N; rep(i, N) { cin >> L.at(i); } sort(L.begin(), L.end(), greater<int>()); rep(i, N - 2) { for (int64_t j = i + 1; j < N - 1; j++) { b = j; e = N; while (e - b > 1) { m = (b + e) / 2; if (L.at(i) < L.at(j) + L.at(m)) b = m; else e = m; } count += b - j; } } cout << count << endl; }
replace
5
6
5
6
0
p02888
C++
Runtime Error
#pragma GCC optimize(3) #include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/hash_policy.hpp> #include <ext/pb_ds/priority_queue.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/trie_policy.hpp> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <vector> #define PII pair<int, int> #define ll long long #define ull unsigned long long #define INFI 2147483647 #define INFL 9223372036854775807 #define INFU 18446744073709551615 #define maxn 1010 using namespace std; using namespace __gnu_pbds; const double PI = acos(-1.0); const double eps = 1e-6; inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } int l[maxn]; int main() { // ios_base::sync_with_stdio(false); // cin.tie(0); // cout.tie(0); int n; while (cin >> n) { memset(l, 0, sizeof(l)); for (int i = 0; i < n; i++) scanf("%d", &l[i]); sort(l, l + n); // for(int i=0;i<n;i++) printf("%d ",l[i]); // cout<<endl; int ans = 0; for (int i = 0; i <= n - 3; i++) { for (int j = i + 1; j <= n - 2; j++) { int sum = l[i] + l[j]; int pos = lower_bound(l + j + 1, l + n, sum) - l; // printf("sum=%d,pos=%d\n",sum,pos); if (pos == 0) ; else ans += (pos - j - 1); } } cout << ans << endl; } return 0; }
#pragma GCC optimize(3) #include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/hash_policy.hpp> #include <ext/pb_ds/priority_queue.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/trie_policy.hpp> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <vector> #define PII pair<int, int> #define ll long long #define ull unsigned long long #define INFI 2147483647 #define INFL 9223372036854775807 #define INFU 18446744073709551615 #define maxn 3010 using namespace std; using namespace __gnu_pbds; const double PI = acos(-1.0); const double eps = 1e-6; inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } int l[maxn]; int main() { // ios_base::sync_with_stdio(false); // cin.tie(0); // cout.tie(0); int n; while (cin >> n) { memset(l, 0, sizeof(l)); for (int i = 0; i < n; i++) scanf("%d", &l[i]); sort(l, l + n); // for(int i=0;i<n;i++) printf("%d ",l[i]); // cout<<endl; int ans = 0; for (int i = 0; i <= n - 3; i++) { for (int j = i + 1; j <= n - 2; j++) { int sum = l[i] + l[j]; int pos = lower_bound(l + j + 1, l + n, sum) - l; // printf("sum=%d,pos=%d\n",sum,pos); if (pos == 0) ; else ans += (pos - j - 1); } } cout << ans << endl; } return 0; }
replace
26
27
26
27
0
p02888
C++
Runtime Error
#include <algorithm> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <unordered_map> #include <vector> #include <math.h> #include <stdio.h> #include <stdlib.h> #include <string.h> using namespace std; #define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i) #define repx(i, s, d, e) for (int i = s, i##_end = (e); i < i##_end; i += d) #define repxr(i, s, d, e) for (int i = s, i##_end = (e); i > i##_end; i += d) #define rept(n) \ for (int ___i___ = 0, i##_len = (n); ___i___ < i##_len; ++___i___) #define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++) #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define ZERO(a) memset(a, 0, sizeof(a)) #define BIT(n) (1LL << (n)) #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()); #define CEIL(x, y) (((x) + (y)-1) / (y)) // vector template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (T &x : vec) is >> x; return is; } // pair template <typename T, typename U> ostream &operator<<(ostream &os, pair<T, U> &pair_var) { os << "(" << pair_var.first << ", " << pair_var.second << ")"; return os; } // vector template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << "{"; for (int i = 0; i < vec.size(); i++) { os << vec[i] << (i + 1 == vec.size() ? "" : ", "); } os << "}"; return os; } // map template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &map_var) { os << "{"; repi(itr, map_var) { os << *itr; itr++; if (itr != map_var.end()) os << ", "; itr--; } os << "}"; return os; } // set template <typename T> ostream &operator<<(ostream &os, set<T> &set_var) { os << "{"; repi(itr, set_var) { os << *itr; itr++; if (itr != set_var.end()) os << ", "; itr--; } os << "}"; return os; } #define DUMPOUT cout void dump_func() { DUMPOUT << endl; } template <class Head, class... Tail> void dump_func(Head &&head, Tail &&...tail) { DUMPOUT << head; if (sizeof...(Tail) > 0) { DUMPOUT << ", "; } dump_func(std::move(tail)...); } #ifdef LOCAL_ #define dump(...) \ DUMPOUT << " " << string(#__VA_ARGS__) << ": " \ << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" << endl \ << " ", \ dump_func(__VA_ARGS__) #else #define dump(...) #endif typedef long long int lli; typedef pair<int, int> ii; typedef priority_queue<int, vector<int>, greater<int>> heapq; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; struct aaa { aaa() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(20); }; } aaaaaaa; // #define int long long int // struct S{ // bool operator<(const S& x) const { // return ; // } // }; signed main() { int N; cin >> N; int L[1001]; rep(i, N) { cin >> L[i]; } sort(L, L + N); int ans = 0; rep(i, N) { repx(j, i + 1, 1, N) { int lK = lower_bound(L, L + N, L[j] - L[i]) - L; lK = max(lK, j); int uK = upper_bound(L, L + N, L[i] + L[j] - 1) - L; if (uK <= lK) continue; ans += uK - lK - 1; dump(i, j, L[i], L[j], uK, lK, ans); } } cout << ans << endl; return 0; }
#include <algorithm> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <unordered_map> #include <vector> #include <math.h> #include <stdio.h> #include <stdlib.h> #include <string.h> using namespace std; #define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i) #define repx(i, s, d, e) for (int i = s, i##_end = (e); i < i##_end; i += d) #define repxr(i, s, d, e) for (int i = s, i##_end = (e); i > i##_end; i += d) #define rept(n) \ for (int ___i___ = 0, i##_len = (n); ___i___ < i##_len; ++___i___) #define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++) #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define ZERO(a) memset(a, 0, sizeof(a)) #define BIT(n) (1LL << (n)) #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()); #define CEIL(x, y) (((x) + (y)-1) / (y)) // vector template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (T &x : vec) is >> x; return is; } // pair template <typename T, typename U> ostream &operator<<(ostream &os, pair<T, U> &pair_var) { os << "(" << pair_var.first << ", " << pair_var.second << ")"; return os; } // vector template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << "{"; for (int i = 0; i < vec.size(); i++) { os << vec[i] << (i + 1 == vec.size() ? "" : ", "); } os << "}"; return os; } // map template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &map_var) { os << "{"; repi(itr, map_var) { os << *itr; itr++; if (itr != map_var.end()) os << ", "; itr--; } os << "}"; return os; } // set template <typename T> ostream &operator<<(ostream &os, set<T> &set_var) { os << "{"; repi(itr, set_var) { os << *itr; itr++; if (itr != set_var.end()) os << ", "; itr--; } os << "}"; return os; } #define DUMPOUT cout void dump_func() { DUMPOUT << endl; } template <class Head, class... Tail> void dump_func(Head &&head, Tail &&...tail) { DUMPOUT << head; if (sizeof...(Tail) > 0) { DUMPOUT << ", "; } dump_func(std::move(tail)...); } #ifdef LOCAL_ #define dump(...) \ DUMPOUT << " " << string(#__VA_ARGS__) << ": " \ << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" << endl \ << " ", \ dump_func(__VA_ARGS__) #else #define dump(...) #endif typedef long long int lli; typedef pair<int, int> ii; typedef priority_queue<int, vector<int>, greater<int>> heapq; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; struct aaa { aaa() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(20); }; } aaaaaaa; // #define int long long int // struct S{ // bool operator<(const S& x) const { // return ; // } // }; signed main() { int N; cin >> N; int L[2001]; rep(i, N) { cin >> L[i]; } sort(L, L + N); int ans = 0; rep(i, N) { repx(j, i + 1, 1, N) { int lK = lower_bound(L, L + N, L[j] - L[i]) - L; lK = max(lK, j); int uK = upper_bound(L, L + N, L[i] + L[j] - 1) - L; if (uK <= lK) continue; ans += uK - lK - 1; dump(i, j, L[i], L[j], uK, lK, ans); } } cout << ans << endl; return 0; }
replace
120
121
120
121
0
p02888
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { int N, sum = 0; cin >> N; vector<int> L(N); for (int n = 0; n < N; n++) { cin >> L.at(n); } sort(L.begin(), L.end()); for (int x = 0; x < N; x++) { for (int y = x + 1; y < N; y++) { for (int z = y + 1; z < N; z++) { if (L.at(x) < L.at(y) + L.at(z) && L.at(y) < L.at(x) + L.at(z) && L.at(z) < L.at(y) + L.at(x)) sum++; else break; } } } cout << sum << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int N, sum = 0; cin >> N; vector<int> L(N); for (int n = 0; n < N; n++) { cin >> L.at(n); } sort(L.begin(), L.end()); for (int x = 0; x < N; x++) { for (int y = x + 1; y < N; y++) { for (int z = y + 1; z < N; z++) { if (L.at(z) < L.at(x) + L.at(y)) sum++; else break; } } } cout << sum << endl; }
replace
16
18
16
17
TLE
p02888
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <string> #include <vector> #define rip(i, n) for (int i = 0; i < n; i++) #define ll long long #define lb long double #define vec vector<int> #define vec2 vector<vector<int>> #define set setprecision #define mod 1000000007 #define pi 3.14159265 #include <numeric> using namespace std; int gcd(int a, int b) { if (a % b == 0) return (b); else return (gcd(b, a % b)); } int lcm(int a, int b) { return a * b / gcd(a, b); } ll pow(ll a, ll b) { ll k = b % 30000, h = b / 30000, s = 1; rip(i, 30000) s = (s * a) % mod; ll j = 1; if (h != 0) rip(i, h) j = j * s % mod; if (k != 0) rip(i, k) j = j * a % mod; return (j); } int main() { int n; cin >> n; vec a(n); rip(i, n) cin >> a[i]; sort(a.begin(), a.end()); int count = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { for (int k = j + 1; j < n; k++) if (a[i] + a[j] > a[k]) count++; } } cout << count << endl; }
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <string> #include <vector> #define rip(i, n) for (int i = 0; i < n; i++) #define ll long long #define lb long double #define vec vector<int> #define vec2 vector<vector<int>> #define set setprecision #define mod 1000000007 #define pi 3.14159265 #include <numeric> using namespace std; int gcd(int a, int b) { if (a % b == 0) return (b); else return (gcd(b, a % b)); } int lcm(int a, int b) { return a * b / gcd(a, b); } ll pow(ll a, ll b) { ll k = b % 30000, h = b / 30000, s = 1; rip(i, 30000) s = (s * a) % mod; ll j = 1; if (h != 0) rip(i, h) j = j * s % mod; if (k != 0) rip(i, k) j = j * a % mod; return (j); } int main() { int n; cin >> n; vec a(n); rip(i, n) cin >> a[i]; sort(a.begin(), a.end()); int count = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { for (int k = j + 1; k < n; k++) if (a[i] + a[j] > a[k]) count++; } } cout << count << endl; }
replace
43
44
43
44
TLE
p02888
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef tuple<ll, int, int> tlii; typedef pair<ll, ll> pll; typedef pair<int, int> pii; typedef vector<vector<ll>> vvl; typedef vector<vector<int>> vvi; #define rep(i, a, b) for (ll i = (a); i < (b); i++) #define rrep(i, a, b) for (ll i = (a); i > (b); i--) #define all(obj) (obj).begin(), (obj).end() #define pb push_back #define str to_string #define mkp make_pair #define mkt make_tuple // #define print(out) cout << (out) << endl const ll INF = 1e18; const ll MOD = 1e9 + 7; void chmax(ll &a, ll b) { a = max(a, b); } void chmin(ll &a, ll b) { a = min(a, b); } template <typename Any> void print(Any out) { cout << out << '\n'; } void print(vector<ll> A) { rep(i, 0, A.size()) { cout << A[i]; cout << (i == A.size() - 1 ? '\n' : ' '); } } ll max(vector<ll> A) { ll res = -INF; for (ll a : A) chmax(res, a); return res; } ll min(vector<ll> A) { ll res = INF; for (ll a : A) chmin(res, a); return res; } ll max(ll A[], int len) { ll res = -INF; rep(i, 0, len) chmax(res, A[i]); return res; } ll min(ll A[], int len) { ll res = INF; rep(i, 0, len) chmin(res, A[i]); return res; } int N, a, b, c; ll cnt; vector<int> A; int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> N; A.resize(N); rep(i, 0, N) cin >> A[i]; cnt = 0; sort(all(A)); rep(i, 0, N) rep(j, i + 1, N) rep(k, j + 1, N) { a = A[i], b = A[j], c = A[k]; if (c >= a + b) break; if (a < b + c && b < c + a && c < a + b) { cnt++; } } print(cnt); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef tuple<ll, int, int> tlii; typedef pair<ll, ll> pll; typedef pair<int, int> pii; typedef vector<vector<ll>> vvl; typedef vector<vector<int>> vvi; #define rep(i, a, b) for (ll i = (a); i < (b); i++) #define rrep(i, a, b) for (ll i = (a); i > (b); i--) #define all(obj) (obj).begin(), (obj).end() #define pb push_back #define str to_string #define mkp make_pair #define mkt make_tuple // #define print(out) cout << (out) << endl const ll INF = 1e18; const ll MOD = 1e9 + 7; void chmax(ll &a, ll b) { a = max(a, b); } void chmin(ll &a, ll b) { a = min(a, b); } template <typename Any> void print(Any out) { cout << out << '\n'; } void print(vector<ll> A) { rep(i, 0, A.size()) { cout << A[i]; cout << (i == A.size() - 1 ? '\n' : ' '); } } ll max(vector<ll> A) { ll res = -INF; for (ll a : A) chmax(res, a); return res; } ll min(vector<ll> A) { ll res = INF; for (ll a : A) chmin(res, a); return res; } ll max(ll A[], int len) { ll res = -INF; rep(i, 0, len) chmax(res, A[i]); return res; } ll min(ll A[], int len) { ll res = INF; rep(i, 0, len) chmin(res, A[i]); return res; } int N, a, b, c; ll cnt; vector<int> A; int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> N; A.resize(N); rep(i, 0, N) cin >> A[i]; cnt = 0; sort(all(A)); rep(i, 0, N) { a = A[i]; rep(j, i + 1, N) { b = A[j]; rep(k, j + 1, N) { c = A[k]; // 枝刈り if (c >= a + b) break; if (a < b + c && b < c + a && c < a + b) { cnt++; } } } } print(cnt); return 0; }
replace
71
77
71
84
TLE
p02888
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++) int main() { int n; cin >> n; vector<int> L(n); REP(i, n) cin >> L[i]; // int n = 2000; // vector<int> L(n); // random_device rnd; // REP(i,n)L[i] = rnd() % 100; sort(L.begin(), L.end()); int ans = 0; for (int a = 0; a < n - 2; a++) { for (int b = a + 1; b < n - 1; b++) { int sum_ab = L[a] + L[b]; int left = lower_bound(L.begin(), L.end(), sum_ab) - L.begin(); int right = b + 1; ans += left - right; } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < (int)(n); i++) int main() { int n; cin >> n; vector<int> L(n); REP(i, n) cin >> L[i]; // int n = 2000; // vector<int> L(n); // random_device rnd; // REP(i,n)L[i] = rnd() % 100; sort(L.begin(), L.end()); int ans = 0; for (int a = 0; a < n - 2; a++) { for (int b = a + 1; b < n - 1; b++) { int sum_ab = L[a] + L[b]; int left = lower_bound(L.begin(), L.end(), sum_ab) - L.begin(); int right = b + 1; ans += left - right; } } cout << ans << endl; }
delete
0
1
0
0
TLE
p02888
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } #define all(x) (x).begin(), (x).end() #define pll pair<ll, ll> #define pii pair<int, int> #define rep(i, n) for (int i = 0; i < n; i++) #define sz(x) ((ll)(x).size()) #define pb push_back #define mp make_pair #define bit(n) (1LL << (n)) #define F first #define S second int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; const ll INF = 1LL << 60; const ll mod = (int)1e9 + 7; int main() { int n; cin >> n; vector<int> l(n); rep(i, n) cin >> l[i]; sort(all(l)); ll ans = 0; rep(i, n) { rep(j, n) { if (i == j) continue; int tem; auto itr1 = upper_bound(all(l), abs(l[i] - l[j])); auto itr2 = lower_bound(all(l), l[i] + l[j]); tem = itr2 - itr1; int it1 = itr1 - l.begin(); int it2 = itr2 - l.begin(); if (it1 <= i && i < it2) tem--; if (it1 <= j && j < it2) tem--; if (tem > 0) { ans += tem; cerr << i << ' ' << j << ' ' << tem << endl; } } } cout << ans / 6; } /* */
#include <bits/stdc++.h> using namespace std; typedef long long ll; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } #define all(x) (x).begin(), (x).end() #define pll pair<ll, ll> #define pii pair<int, int> #define rep(i, n) for (int i = 0; i < n; i++) #define sz(x) ((ll)(x).size()) #define pb push_back #define mp make_pair #define bit(n) (1LL << (n)) #define F first #define S second int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; const ll INF = 1LL << 60; const ll mod = (int)1e9 + 7; int main() { int n; cin >> n; vector<int> l(n); rep(i, n) cin >> l[i]; sort(all(l)); ll ans = 0; rep(i, n) { rep(j, n) { if (i == j) continue; int tem; auto itr1 = upper_bound(all(l), abs(l[i] - l[j])); auto itr2 = lower_bound(all(l), l[i] + l[j]); tem = itr2 - itr1; int it1 = itr1 - l.begin(); int it2 = itr2 - l.begin(); if (it1 <= i && i < it2) tem--; if (it1 <= j && j < it2) tem--; if (tem > 0) ans += (ll)tem; } } cout << ans / 6; } /* */
replace
53
57
53
55
TLE
p02888
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define all(x) (x).begin(), (x).end() #define sz(x) (int)x.size() #define pb push_back #define fi first #define se second #define mp make_pair #define debug(x) cerr << " - " << #x << ": " << x << endl; #define debugs(x, y) \ cerr << " - " << #x << ": " << x << "\t\t" << #y << ": " << y << endl; #define debugv(v, n) \ cerr << #v << " : " \ << " "; \ for (int i = 0; i < n; i++) \ cerr << "\t" << #v << "[" << i << "]" \ << " : " << v[i] << "\n"; \ cerr << endl; #define sep() cerr << "--------------------" << endl; typedef long long ll; const ll mod = 1e9 + 7; const int N = 1234; int a[N]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); ll sum = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n - 1; j++) { int c = a[i] + a[j]; int idx = lower_bound(a + j + 1, a + n, c) - a; idx--; sum += idx - j; } } cout << sum << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define all(x) (x).begin(), (x).end() #define sz(x) (int)x.size() #define pb push_back #define fi first #define se second #define mp make_pair #define debug(x) cerr << " - " << #x << ": " << x << endl; #define debugs(x, y) \ cerr << " - " << #x << ": " << x << "\t\t" << #y << ": " << y << endl; #define debugv(v, n) \ cerr << #v << " : " \ << " "; \ for (int i = 0; i < n; i++) \ cerr << "\t" << #v << "[" << i << "]" \ << " : " << v[i] << "\n"; \ cerr << endl; #define sep() cerr << "--------------------" << endl; typedef long long ll; const ll mod = 1e9 + 7; const int N = 2234; int a[N]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); ll sum = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n - 1; j++) { int c = a[i] + a[j]; int idx = lower_bound(a + j + 1, a + n, c) - a; idx--; sum += idx - j; } } cout << sum << endl; return 0; }
replace
24
25
24
25
0
p02888
C++
Runtime Error
#include <algorithm> #include <iostream> using namespace std; int v[1004]; int caut_bin(int st, int fin, int sum) { int p = st; while (st <= fin) { int mij = (st + fin) / 2; if (v[mij] >= sum) { fin = mij - 1; } else { st = mij + 1; } } return st - p; } int main() { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> v[i]; } int sum = 0; sort(v, v + n); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { sum += caut_bin(j + 1, n - 1, v[i] + v[j]); } } cout << sum; return 0; }
#include <algorithm> #include <iostream> using namespace std; int v[2004]; int caut_bin(int st, int fin, int sum) { int p = st; while (st <= fin) { int mij = (st + fin) / 2; if (v[mij] >= sum) { fin = mij - 1; } else { st = mij + 1; } } return st - p; } int main() { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> v[i]; } int sum = 0; sort(v, v + n); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { sum += caut_bin(j + 1, n - 1, v[i] + v[j]); } } cout << sum; return 0; }
replace
3
4
3
4
0
p02888
C++
Time Limit Exceeded
/** * author: yuya1234 * created: 17.07.2020 09:17:47 **/ #include <bits/stdc++.h> using namespace std; typedef long long ll; long double eps = 1.0E-14; #define REP(i, n) for (ll i = 0; i < (ll)(n); i++) #define REPD(i, n) for (ll i = n - 1; i >= 0; i--) #define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++) #define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--) #define FORV(i, v) for (auto i = v.begin(); i != v.end(); i++) #define SORT(s) sort((s).begin(), (s).end()) #define SORTD(s) sort((s).rbegin(), (s).rend()) #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()); #define SZ(x) ((int)(x).size()) #define MEMSET(v, h) memset((v), h, sizeof(v)) 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; } template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } template <class T> T lcm(T a, T b) { return gcd(a, b) * (a / gcd(a, b)) * (b / gcd(a, b)); } int main() { cin.tie(0); ios_base::sync_with_stdio(false); // std::cout << std::fixed << std::setprecision(15); int n; cin >> n; int l[n]; REP(i, n) cin >> l[i]; ll ans = 0; REP(i, n - 2) FOR(j, i + 1, n - 2) FOR(k, j + 1, n - 1) { if (l[i] < l[j] + l[k] && l[j] < l[i] + l[k] && l[k] < l[j] + l[i]) ans++; } cout << ans << endl; return 0; }
/** * author: yuya1234 * created: 17.07.2020 09:17:47 **/ #include <bits/stdc++.h> using namespace std; typedef long long ll; long double eps = 1.0E-14; #define REP(i, n) for (ll i = 0; i < (ll)(n); i++) #define REPD(i, n) for (ll i = n - 1; i >= 0; i--) #define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++) #define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--) #define FORV(i, v) for (auto i = v.begin(); i != v.end(); i++) #define SORT(s) sort((s).begin(), (s).end()) #define SORTD(s) sort((s).rbegin(), (s).rend()) #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()); #define SZ(x) ((int)(x).size()) #define MEMSET(v, h) memset((v), h, sizeof(v)) 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; } template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } template <class T> T lcm(T a, T b) { return gcd(a, b) * (a / gcd(a, b)) * (b / gcd(a, b)); } int main() { cin.tie(0); ios_base::sync_with_stdio(false); // std::cout << std::fixed << std::setprecision(15); int n; cin >> n; int l[n]; REP(i, n) cin >> l[i]; ll ans = 0; sort(l, l + n, greater<int>()); REP(i, n - 2) { FOR(j, i + 1, n - 2) { FOR(k, j + 1, n - 1) { if (l[i] < l[j] + l[k] && l[j] < l[i] + l[k] && l[k] < l[j] + l[i]) ans++; else break; } } } cout << ans << endl; return 0; }
replace
55
58
55
66
TLE
p02888
C++
Runtime Error
#include <algorithm> #include <iostream> using namespace std; long long l[2010], cnt[1010][2010] = {}, ans = 0; int main() { int i, j, n; cin >> n; for (i = 0; i < n; i++) { cin >> l[i]; } sort(l, l + n); for (i = 1; i <= n; i++) { for (j = 0; j <= 1000; j++) { cnt[i][j] = cnt[i - 1][j]; } for (j = 0; j <= l[i - 1]; j++) { cnt[i][j]++; } } for (i = n - 1; i >= 0; i--) { for (j = i - 1; j >= 1; j--) { int x = l[i] - l[j]; ans += cnt[j][x + 1]; } } cout << ans << endl; }
#include <algorithm> #include <iostream> using namespace std; long long l[2010], cnt[2010][2010] = {}, ans = 0; int main() { int i, j, n; cin >> n; for (i = 0; i < n; i++) { cin >> l[i]; } sort(l, l + n); for (i = 1; i <= n; i++) { for (j = 0; j <= 1000; j++) { cnt[i][j] = cnt[i - 1][j]; } for (j = 0; j <= l[i - 1]; j++) { cnt[i][j]++; } } for (i = n - 1; i >= 0; i--) { for (j = i - 1; j >= 1; j--) { int x = l[i] - l[j]; ans += cnt[j][x + 1]; } } cout << ans << endl; }
replace
3
4
3
4
0
p02888
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <string> #include <unordered_map> #include <vector> using namespace std; typedef long long ll; const ll INF = 1e9 + 7; template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } #define rep(i, n) for (int i = 0; i < (n); i++) int main() { int N, a, b, c; cin >> N; int L[N], count = 0; rep(i, N) cin >> L[i]; for (int i = 0; i < N - 2; i++) { for (int j = i + 1; j < N - 1; j++) { for (int k = j + 1; k < N; k++) { a = L[i]; b = L[j]; c = L[k]; if (a < b + c && b < a + c && c < a + b) { count++; } } } } cout << count << endl; return 0; } // a=97,z=122 // 1,2,3,4,5 1,2,3 1,2,4 1,2,5, 1,3,4 1,3,5, 1,4,5, 2,3,4, 2,3,5 2,4,5 3,4,5
#include <algorithm> #include <cmath> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <string> #include <unordered_map> #include <vector> using namespace std; typedef long long ll; const ll INF = 1e9 + 7; template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } #define rep(i, n) for (int i = 0; i < (n); i++) int main() { int N, a, b, c; cin >> N; int L[N], count = 0; rep(i, N) cin >> L[i]; sort(L, L + N); for (int i = 0; i < N - 2; i++) { for (int j = i + 1; j < N - 1; j++) { for (int k = j + 1; k < N; k++) { a = L[i]; b = L[j]; c = L[k]; if (a < b + c && b < a + c && c < a + b) { count++; } } } } cout << count << endl; return 0; } // a=97,z=122 // 1,2,3,4,5 1,2,3 1,2,4 1,2,5, 1,3,4 1,3,5, 1,4,5, 2,3,4, 2,3,5 2,4,5 3,4,5
insert
42
42
42
43
TLE
p02888
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; bool flag(int a, int b, int c) { return ((a < b + c) && (b < a + c) && (c < b + a)); } int main() { ios_base ::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; int v[n]; for (int i = 0; i < n; ++i) { cin >> v[i]; } int count = 0; for (int i = 0; i < n - 2; ++i) { for (int j = i + 1; j < n - 1; ++j) { for (int k = j + 1; k < n; ++k) { if (flag(v[i], v[k], v[j])) count++; } } } cout << count << endl; }
#include <bits/stdc++.h> using namespace std; bool flag(int a, int b, int c) { return ((a < b + c) && (b < a + c) && (c < b + a)); } int main() { ios_base ::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; int v[n]; for (int i = 0; i < n; ++i) { cin >> v[i]; } sort(v, v + n); int count = 0; for (int i = 0; i < n - 2; ++i) { for (int j = i + 1; j < n - 1; ++j) { for (int k = j + 1; k < n; ++k) { if (flag(v[i], v[k], v[j])) count++; } } } cout << count << endl; }
insert
16
16
16
17
TLE
p02888
C++
Time Limit Exceeded
#include <bits/stdc++.h> #include <numeric> #include <vector> #define PI 3.14159265358979323846 #define MAXINF (1e18L) #define INF (1e9L) #define EPS (1e-9) #define MOD ((ll)(1e9 + 7)) #define REP(i, n) for (int i = 0; i < int(n); ++i) #define Rep(i, sta, n) for (int i = sta; i < n; i++) #define RREP(i, n) for (int i = int(n) - 1; i >= 0; --i) #define ALL(v) v.begin(), v.end() #define FIND(v, x) (binary_search(ALL(v), (x))) #define SORT(v) sort(ALL(v)) #define RSORT(v) \ sort(ALL(v)); \ reverse(ALL(v)) #define DEBUG(x) cerr << #x << ": " << x << endl; #define DEBUG_VEC(v) \ cerr << #v << ":"; \ for (int i = 0; i < v.size(); i++) \ cerr << " " << v[i]; \ cerr << endl #define Yes(n) cout << ((n) ? "Yes" : "No") << endl #define YES(n) cout << ((n) ? "YES" : "NO") << endl #define pb push_back #define fi first #define se second using namespace std; template <class A> void pr(A a) { cout << (a) << endl; } template <class A, class B> void pr(A a, B b) { cout << a << " "; pr(b); } template <class A, class B, class C> void pr(A a, B b, C c) { cout << a << " "; pr(b, c); } template <class A, class B, class C, class D> void pr(A a, B b, C c, D d) { cout << a << " "; pr(b, c, d); } template <class T> inline bool chmin(T &a, T b) { return a > b ? a = b, true : false; } template <class T> inline bool chmax(T &a, T b) { return a < b ? a = b, true : false; } typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; int main(void) { int N; cin >> N; vector<int> L(N); for (int i = 0; i < N; i++) { cin >> L[i]; } SORT(L); int ans = 0; REP(i, N) { Rep(j, i + 1, N) { Rep(k, j + 1, N) { if (L[i] < L[j] + L[k] && L[j] < L[i] + L[k] && L[k] < L[i] + L[j]) ans++; if (L[k] >= L[i] + L[j]) break; } } } pr(ans); }
#include <bits/stdc++.h> #include <numeric> #include <vector> #define PI 3.14159265358979323846 #define MAXINF (1e18L) #define INF (1e9L) #define EPS (1e-9) #define MOD ((ll)(1e9 + 7)) #define REP(i, n) for (int i = 0; i < int(n); ++i) #define Rep(i, sta, n) for (int i = sta; i < n; i++) #define RREP(i, n) for (int i = int(n) - 1; i >= 0; --i) #define ALL(v) v.begin(), v.end() #define FIND(v, x) (binary_search(ALL(v), (x))) #define SORT(v) sort(ALL(v)) #define RSORT(v) \ sort(ALL(v)); \ reverse(ALL(v)) #define DEBUG(x) cerr << #x << ": " << x << endl; #define DEBUG_VEC(v) \ cerr << #v << ":"; \ for (int i = 0; i < v.size(); i++) \ cerr << " " << v[i]; \ cerr << endl #define Yes(n) cout << ((n) ? "Yes" : "No") << endl #define YES(n) cout << ((n) ? "YES" : "NO") << endl #define pb push_back #define fi first #define se second using namespace std; template <class A> void pr(A a) { cout << (a) << endl; } template <class A, class B> void pr(A a, B b) { cout << a << " "; pr(b); } template <class A, class B, class C> void pr(A a, B b, C c) { cout << a << " "; pr(b, c); } template <class A, class B, class C, class D> void pr(A a, B b, C c, D d) { cout << a << " "; pr(b, c, d); } template <class T> inline bool chmin(T &a, T b) { return a > b ? a = b, true : false; } template <class T> inline bool chmax(T &a, T b) { return a < b ? a = b, true : false; } typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; int main(void) { int N; cin >> N; vector<int> L(N); for (int i = 0; i < N; i++) { cin >> L[i]; } SORT(L); int ans = 0; REP(i, N) { Rep(j, i + 1, N) { int l = max(L[i] - L[j], L[j] - L[i]); int r = L[i] + L[j]; int l_id = upper_bound(ALL(L), l) - L.begin(); int r_id = lower_bound(ALL(L), r) - L.begin(); if (l_id < j) l_id = j; ans += r_id - l_id - 1; if (l_id < i && i < r_id) ans--; if (l_id < j && j < r_id) ans--; // Rep(k, j+1, N){ // if(L[i]<L[j]+L[k] && L[j]<L[i]+L[k] && L[k]<L[i]+L[j]) ans++; // if(L[k]>=L[i]+L[j])break; // } } } pr(ans); }
replace
63
69
63
79
TLE
p02888
C++
Time Limit Exceeded
// #include <bits/stdc++.h> using namespace std; using ll = long long; using vl = vector<ll>; using vvl = vector<vl>; using vi = vector<int>; using vvi = vector<vi>; #define ote(x) cout << (x) << endl #define all(x) (x).begin(), (x).end() #define rp(i, s, e) for (int i = (s); i < (e); ++i) using PII = pair<int, int>; void vprn(vector<int> &v) { for (int i = 0; i < v.size(); i++) cout << v[i] << (i < v.size() - 1 ? ",\t" : "\n"); } // ll solve(int N, vi &L) { ll ans = 0; rp(ia, 0, N - 2) { ll a = L[ia]; rp(ib, ia + 1, N - 1) { ll b = L[ib]; rp(ic, ib + 1, N) { ll c = L[ic]; // printf("a %d b %d c %d\n",a,b,c);// if (a < b + c && b < c + a && c < a + b) { ans++; } } } } return ans; } int main() { int N; cin >> N; vi L(N); rp(i, 0, N) cin >> L[i]; ote(solve(N, L)); }
// #include <bits/stdc++.h> using namespace std; using ll = long long; using vl = vector<ll>; using vvl = vector<vl>; using vi = vector<int>; using vvi = vector<vi>; #define ote(x) cout << (x) << endl #define all(x) (x).begin(), (x).end() #define rp(i, s, e) for (int i = (s); i < (e); ++i) using PII = pair<int, int>; void vprn(vector<int> &v) { for (int i = 0; i < v.size(); i++) cout << v[i] << (i < v.size() - 1 ? ",\t" : "\n"); } // ll solve(int N, vi &L) { ll ans = 0; rp(ia, 0, N - 2) { ll a = L[ia]; rp(ib, ia + 1, N - 1) { ll b = L[ib]; rp(ic, ib + 1, N) { ll c = L[ic]; int minL = min({a, b, c}); int maxL = max({a, b, c}); int midL = a + b + c - minL - maxL; // printf("min %d mid %d max %d\n",minL,midL,maxL);// if (maxL < minL + midL) { ans++; } } } } return ans; } int main() { int N; cin >> N; vi L(N); rp(i, 0, N) cin >> L[i]; ote(solve(N, L)); }
replace
26
28
26
31
TLE
p02888
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long LL; void go(); int main() { #ifdef tokitsukaze freopen("TEST.txt", "r", stdin); #endif go(); return 0; } /********************************* head *********************************/ int n, a[10]; int cmp(int a, int b) { return a > b; } LL ans; void go() { cin >> n; for (int i = 0; i < n; ++i) { cin >> a[i]; } sort(a, a + n, cmp); for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { int t = lower_bound(a + j, a + n, a[i] - a[j], greater<int>()) - a; t -= 1; if (t > j) ans += t - j; } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long LL; void go(); int main() { #ifdef tokitsukaze freopen("TEST.txt", "r", stdin); #endif go(); return 0; } /********************************* head *********************************/ int n, a[100100]; int cmp(int a, int b) { return a > b; } LL ans; void go() { cin >> n; for (int i = 0; i < n; ++i) { cin >> a[i]; } sort(a, a + n, cmp); for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { int t = lower_bound(a + j, a + n, a[i] - a[j], greater<int>()) - a; t -= 1; if (t > j) ans += t - j; } } cout << ans << endl; }
replace
14
15
14
15
0
p02888
C++
Runtime Error
#include <algorithm> #include <cctype> #include <cstdio> #include <iostream> #include <map> #include <math.h> #include <set> #include <string> #include <tuple> #include <utility> #include <vector> using i8 = int8_t; using u8 = uint8_t; using i16 = int16_t; using u16 = uint16_t; using i32 = int32_t; using u32 = uint32_t; using i64 = int64_t; using u64 = uint64_t; using f32 = float; using f64 = double; constexpr i64 INF = 1'010'000'000'000'000'017LL; constexpr i64 MOD = 1'000'000'007LL; constexpr f64 EPS = 1e-12; constexpr f64 PI = 3.14159265358979323846; const int AMAX = 100000; using namespace std; #define rep(i, n) for (i64 i = 0; i < (int)(n); i++) #define FOR(i, m, n) for (i64 i = m; i < n; i++) #define SORT(x) sort(x.begin(), x.end()) #define REVE(x) reverse(x.begin(), x.end()) #define all(x) (x).begin(), (x).end() #define fst first #define mp make_pair #define pb push_back #define eb emplace_back #define pob pop_back #define sw swap #define UP(x) transform(x.begin(), x.end(), x.begin(), ::toupper); #define down(x) transform(x.begin(), x.end(), x.begin(), ::tolower); using LL = long long; using ULL = unsigned long long; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } int main() { LL n, a; cin >> n; vector<LL> l; rep(i, n) { cin >> a; l.pb(a); } a = 0; sort(l.end(), l.begin()); rep(i, n) { for (int j = i + 1; j < n; j++) { for (int k = j + 1; k < n; k++) { if (l[i] < l[j] + l[k]) { a++; } } } } cout << a; }
#include <algorithm> #include <cctype> #include <cstdio> #include <iostream> #include <map> #include <math.h> #include <set> #include <string> #include <tuple> #include <utility> #include <vector> using i8 = int8_t; using u8 = uint8_t; using i16 = int16_t; using u16 = uint16_t; using i32 = int32_t; using u32 = uint32_t; using i64 = int64_t; using u64 = uint64_t; using f32 = float; using f64 = double; constexpr i64 INF = 1'010'000'000'000'000'017LL; constexpr i64 MOD = 1'000'000'007LL; constexpr f64 EPS = 1e-12; constexpr f64 PI = 3.14159265358979323846; const int AMAX = 100000; using namespace std; #define rep(i, n) for (i64 i = 0; i < (int)(n); i++) #define FOR(i, m, n) for (i64 i = m; i < n; i++) #define SORT(x) sort(x.begin(), x.end()) #define REVE(x) reverse(x.begin(), x.end()) #define all(x) (x).begin(), (x).end() #define fst first #define mp make_pair #define pb push_back #define eb emplace_back #define pob pop_back #define sw swap #define UP(x) transform(x.begin(), x.end(), x.begin(), ::toupper); #define down(x) transform(x.begin(), x.end(), x.begin(), ::tolower); using LL = long long; using ULL = unsigned long long; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } int main() { LL n, a; cin >> n; vector<LL> l; rep(i, n) { cin >> a; l.pb(a); } a = 0; SORT(l); REVE(l); rep(i, n) { for (int j = i + 1; j < n; j++) { for (int k = j + 1; k < n; k++) { if (l[i] < l[j] + l[k]) { a++; } } } } cout << a; }
replace
66
67
66
68
-11
p02888
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> l(n); vector<int> a(1001); for (int i = 0; i < n; i++) { cin >> l.at(i); a.at(l.at(i))++; } int64_t ans = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (i != j) { for (int k = abs(l.at(i) - l.at(j)) + 1; k < min(1001, (l.at(i) + l.at(j))); k++) { ans += a.at(k); if (k == l.at(i)) ans--; if (k == l.at(j)) ans--; } } } } cout << ans / 3 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> l(n); vector<int> a(1001); for (int i = 0; i < n; i++) { cin >> l.at(i); a.at(l.at(i))++; } int64_t ans = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (i != j) { for (int k = abs(l.at(i) - l.at(j)) + 1, x = min(1001, (l.at(i) + l.at(j))); k < x; k++) { ans += a.at(k); if (k == l.at(i)) ans--; if (k == l.at(j)) ans--; } } } } cout << ans / 3 << endl; return 0; }
replace
16
18
16
19
TLE
p02888
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) typedef pair<int, int> P; int n, l[2020]; int main() { cin >> n; rep(i, n) cin >> l[i]; sort(l, l + n); int sum = 0; for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { int a = min(l[i], l[j]), b = l[i] + l[j] - a; auto sentou = l + j + 1; auto ub = upper_bound(sentou, l + n, a + b - 1); int num = ub - sentou; cerr << a << " " << b << " " << *ub << " " << num << endl; sum += num; } } cout << sum << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) typedef pair<int, int> P; int n, l[2020]; int main() { cin >> n; rep(i, n) cin >> l[i]; sort(l, l + n); int sum = 0; for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { int a = min(l[i], l[j]), b = l[i] + l[j] - a; auto sentou = l + j + 1; auto ub = upper_bound(sentou, l + n, a + b - 1); int num = ub - sentou; // cerr << a << " " << b << " " << *ub << " " << num << endl; sum += num; } } cout << sum << endl; return 0; }
replace
18
19
18
19
TLE
p02888
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) typedef long long ll; #define REP(i, n) for (ll i = 0; i < (ll)(n); i++) #define REPD(i, n) for (ll i = n - 1; i >= 0; i--) #define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++) #define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--) // 定数 #define INF 1000000000005 // 10^9:極めて大きい値,∞ // 略記 #define PB push_back // vectorヘの挿入 #define MP make_pair // pairのコンストラクタ #define F first // pairの一つ目の要素 #define S second // pairの二つ目の要素 int main() { ll N; cin >> N; vector<int> L(N); rep(i, N) cin >> L[i]; ll ans = 0; rep(i, N - 2) { for (int j = i + 1; j < N - 1; j++) { for (int k = j + 1; k < N; k++) { if (L[i] + L[j] > L[k] && L[k] + L[i] > L[j] && L[j] + L[k] > L[i]) ans++; } } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) typedef long long ll; #define REP(i, n) for (ll i = 0; i < (ll)(n); i++) #define REPD(i, n) for (ll i = n - 1; i >= 0; i--) #define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++) #define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--) // 定数 #define INF 1000000000005 // 10^9:極めて大きい値,∞ // 略記 #define PB push_back // vectorヘの挿入 #define MP make_pair // pairのコンストラクタ #define F first // pairの一つ目の要素 #define S second // pairの二つ目の要素 int main() { ll N; cin >> N; vector<int> L(N); rep(i, N) cin >> L[i]; ll ans = 0; sort(L.begin(), L.end()); for (int a = 2; a < N; a++) { for (int b = 1; b < a; b++) { int c = upper_bound(L.begin(), L.begin() + b, L[a] - L[b]) - L.begin(); ans += max(b - c, 0); } } cout << ans << endl; return 0; }
replace
25
31
25
30
TLE
p02888
C++
Time Limit Exceeded
#define _GLIBCXX_DEBUG #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; cin >> n; vll l(n); rep(i, n) { cin >> l[i]; } sort(all(l)); ll ans = 0; rep(i, n - 2) { for (int j = i + 1; j < n - 1; j++) { auto iter = lower_bound(all(l), l[i] + l[j]); // auto ite2 = lower_bound(all(l), max(l[i],l[j])); // ans += (iter-l.begin()) - (l.end()-ite2) - 1; ans += (iter - l.begin()) - (j + 1); // if() } } 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; cin >> n; vll l(n); rep(i, n) { cin >> l[i]; } sort(all(l)); ll ans = 0; rep(i, n - 2) { for (int j = i + 1; j < n - 1; j++) { auto iter = lower_bound(all(l), l[i] + l[j]); // auto ite2 = lower_bound(all(l), max(l[i],l[j])); // ans += (iter-l.begin()) - (l.end()-ite2) - 1; ans += (iter - l.begin()) - (j + 1); // if() } } cout << ans << endl; return 0; }
delete
0
1
0
0
TLE
p02888
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; int main(void) { int n; int cnt = 0; cin >> n; vector<int> L(n, 0); rep(i, n) cin >> L[i]; sort(L.begin(), L.end()); // rep(i, n) cout << L[i]; rep(i, n) { rep(j, n) { rep(k, n) { if (i != j and i != k and j != k) { if (i < j and j < k) { if (L[i] + L[j] > L[k]) cnt += 1; } } } } } cout << cnt; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; int main(void) { int n; int cnt = 0; cin >> n; vector<int> L(n, 0); rep(i, n) cin >> L[i]; sort(L.begin(), L.end()); // rep(i, n) cout << L[i]; rep(i, n) { rep(j, n) { if (i != j) { if (i < j) { for (int k = j + 1; k < n; k++) { if (L[i] + L[j] > L[k]) cnt += 1; } } } } } cout << cnt; }
replace
13
16
13
16
TLE
p02888
C++
Runtime Error
#include <algorithm> #include <iostream> #include <utility> #include <vector> typedef long long ll; #define PB push_back #define FOR(n) for (int i = 0; i < n; ++i) #define RFOR(n) for (int i = n; i >= 0; --i) #define FORR(start, end) for (int i = start; i < end; ++i) #define COUT(n) cout << n << " " << flush #define vi(v) vector<int> v #define vii(v) vector<pair<int, int>> v #define mkp(a, b) make_pair(a, b) #define ALL(obj) obj.begin(), obj.end() using namespace std; int main() { int n; vector<ll> L(1000); ll cou = 0; cin >> n; FOR(n) cin >> L[i]; sort(ALL(L), greater<ll>()); FOR(n - 2) { ll longest = L[i]; for (int j = i + 1; j < n - 1; ++j) { for (int k = j + 1; k < n; ++k) if (longest < L[j] + L[k]) { ++cou; } else { break; } } } cout << cou << endl; return 0; }
#include <algorithm> #include <iostream> #include <utility> #include <vector> typedef long long ll; #define PB push_back #define FOR(n) for (int i = 0; i < n; ++i) #define RFOR(n) for (int i = n; i >= 0; --i) #define FORR(start, end) for (int i = start; i < end; ++i) #define COUT(n) cout << n << " " << flush #define vi(v) vector<int> v #define vii(v) vector<pair<int, int>> v #define mkp(a, b) make_pair(a, b) #define ALL(obj) obj.begin(), obj.end() using namespace std; int main() { int n; vector<ll> L(2000); ll cou = 0; cin >> n; FOR(n) cin >> L[i]; sort(ALL(L), greater<ll>()); FOR(n - 2) { ll longest = L[i]; for (int j = i + 1; j < n - 1; ++j) { for (int k = j + 1; k < n; ++k) if (longest < L[j] + L[k]) { ++cou; } else { break; } } } cout << cou << endl; return 0; }
replace
18
19
18
19
0
p02888
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <queue> #include <stdio.h> #include <string> #include <tuple> #include <vector> using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; vector<int> L(N); for (int i = 0; i < N; i++) { cin >> L[i]; } sort(L.begin(), L.end()); int count = 0; for (int i = 0; i < N - 2; i++) { int a = L[i]; for (int j = i + 1; j < N - 1; j++) { int b = L[j]; int left = b - a > a - b ? b - a + 1 : a - b + 1; int right = a + b; int l = lower_bound(L.begin() + j + 1, L.end(), left) - L.begin(); /* if(l == 0) { l = j + 1; }*/ int r = lower_bound(L.begin() + j + 1, L.end(), right) - L.begin(); /* if(r == 0) { r = N + 1; }*/ cerr << left << " " << right << endl; cerr << l << " " << r << endl; // if(r > l) { count += r - l; // } } } cout << count << endl; }
#include <algorithm> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <queue> #include <stdio.h> #include <string> #include <tuple> #include <vector> using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; vector<int> L(N); for (int i = 0; i < N; i++) { cin >> L[i]; } sort(L.begin(), L.end()); int count = 0; for (int i = 0; i < N - 2; i++) { int a = L[i]; for (int j = i + 1; j < N - 1; j++) { int b = L[j]; int left = b - a > a - b ? b - a + 1 : a - b + 1; int right = a + b; int l = lower_bound(L.begin() + j + 1, L.end(), left) - L.begin(); /* if(l == 0) { l = j + 1; }*/ int r = lower_bound(L.begin() + j + 1, L.end(), right) - L.begin(); /* if(r == 0) { r = N + 1; }*/ // cerr << left << " " << right << endl; // cerr << l << " " << r << endl; // if(r > l) { count += r - l; // } } } cout << count << endl; }
replace
49
51
49
51
TLE
p02888
C++
Time Limit Exceeded
#include <algorithm> #include <array> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <memory> #include <numeric> #include <queue> #include <random> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <vector> int main() { int n; std::cin >> n; std::vector<int> sticks(n); for (auto &s : sticks) std::cin >> s; std::sort(sticks.begin(), sticks.end()); int count = 0; for (auto i = 0; i < n; ++i) for (auto j = 0; j < n; ++j) for (auto k = 0; k < n; ++k) { if (i < j && j < k && sticks[i] + sticks[j] > sticks[k]) { ++count; } } std::cout << count << std::endl; }
#include <algorithm> #include <array> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <memory> #include <numeric> #include <queue> #include <random> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <vector> int main() { int n; std::cin >> n; std::vector<int> sticks(n); for (auto &s : sticks) std::cin >> s; std::sort(sticks.begin(), sticks.end()); int count = 0; for (auto i = 0; i < n; ++i) for (auto j = i + 1; j < n; ++j) for (auto k = j + 1; k < n; ++k) { if (i < j && j < k && sticks[i] + sticks[j] > sticks[k]) { ++count; } } std::cout << count << std::endl; }
replace
30
32
30
32
TLE
p02888
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = unsigned long long; int main() { int N; cin >> N; int L[1001]; for (int i = 0; i < N; ++i) cin >> L[i]; sort(L, L + N); int ans = 0; for (int i = 0; i < N - 2; ++i) { for (int j = i + 1; j < N - 1; ++j) { for (int k = j + 1; k < N; ++k) { int a = L[i]; int b = L[j]; int c = L[k]; if ((a < (b + c)) && (b < (c + a)) && (c < (a + b))) { ans++; } } } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; using ll = unsigned long long; int main() { int N; cin >> N; int L[2001]; for (int i = 0; i < N; ++i) cin >> L[i]; sort(L, L + N); int ans = 0; for (int i = 0; i < N - 2; ++i) { for (int j = i + 1; j < N - 1; ++j) { for (int k = j + 1; k < N; ++k) { int a = L[i]; int b = L[j]; int c = L[k]; if ((a < (b + c)) && (b < (c + a)) && (c < (a + b))) { ans++; } } } } cout << ans << endl; }
replace
7
8
7
8
0
p02888
C++
Runtime Error
#include <algorithm> #include <assert.h> #include <bitset> #include <chrono> #include <cmath> #include <complex> #include <deque> #include <functional> #include <iostream> #include <limits.h> #include <map> #include <math.h> #include <queue> #include <random> #include <set> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <string> #include <time.h> #include <unordered_map> #include <unordered_set> #include <vector> #define ll long long #define rep2(i, a, b) for (ll i = a; i <= b; ++i) #define rep(i, n) for (ll i = 0; i < n; i++) #define rep3(i, a, b) for (ll i = a; i >= b; i--) #define REP(e, v) for (auto e : v) #define pii pair<int, int> #define pll pair<ll, ll> #define mp make_pair #define mt make_tuple #define tii tuple<int, int, int> #define pq priority_queue<ll> #define pqg priority_queue<int, vector<int>, greater<int>> #define pb push_back #define edge(v, a, b) \ v[a].pb(b); \ v[b].pb(a); #define vec vector<int> #define vecll vector<ll> #define vecpii vector<pii> #define vecbl vector<bool> #define endl "\n" #define ALL(c) (c).begin(), (c).end() using namespace std; int in() { int x; scanf("%d", &x); return x; } ll lin() { ll x; scanf("%lld", &x); return x; } string stin() { string s; cin >> s; return s; } int main() { int n = in(); vec l(n); rep(i, n) l[i] = in(); ll ans = 0; sort(ALL(l)); vec cnt(2010); cnt[0] = 0; int tmp = 0; for (int i = 1; i <= 2010; i++) { cnt[i] = cnt[i - 1]; while (l[tmp] <= i) { if (tmp < n - 1) { tmp++; cnt[i]++; } else break; } if (i >= l[n - 1]) cnt[i] = n; } for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { ans += cnt[l[i] + l[j] - 1] - j - 1; } } cout << ans << endl; }
#include <algorithm> #include <assert.h> #include <bitset> #include <chrono> #include <cmath> #include <complex> #include <deque> #include <functional> #include <iostream> #include <limits.h> #include <map> #include <math.h> #include <queue> #include <random> #include <set> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <string> #include <time.h> #include <unordered_map> #include <unordered_set> #include <vector> #define ll long long #define rep2(i, a, b) for (ll i = a; i <= b; ++i) #define rep(i, n) for (ll i = 0; i < n; i++) #define rep3(i, a, b) for (ll i = a; i >= b; i--) #define REP(e, v) for (auto e : v) #define pii pair<int, int> #define pll pair<ll, ll> #define mp make_pair #define mt make_tuple #define tii tuple<int, int, int> #define pq priority_queue<ll> #define pqg priority_queue<int, vector<int>, greater<int>> #define pb push_back #define edge(v, a, b) \ v[a].pb(b); \ v[b].pb(a); #define vec vector<int> #define vecll vector<ll> #define vecpii vector<pii> #define vecbl vector<bool> #define endl "\n" #define ALL(c) (c).begin(), (c).end() using namespace std; int in() { int x; scanf("%d", &x); return x; } ll lin() { ll x; scanf("%lld", &x); return x; } string stin() { string s; cin >> s; return s; } int main() { int n = in(); vec l(n); rep(i, n) l[i] = in(); ll ans = 0; sort(ALL(l)); vec cnt(2010); cnt[0] = 0; int tmp = 0; for (int i = 1; i < 2010; i++) { cnt[i] = cnt[i - 1]; while (l[tmp] <= i) { if (tmp < n - 1) { tmp++; cnt[i]++; } else break; } if (i >= l[n - 1]) cnt[i] = n; } for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { ans += cnt[l[i] + l[j] - 1] - j - 1; } } cout << ans << endl; }
replace
71
72
71
72
-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)
p02888
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int N, R = 0, flg = 0; cin >> N; vector<int> L(N); for (int i = 0; i < N; i++) { cin >> L.at(i); } sort(L.begin(), L.end()); for (int i = 0; i < N - 2; i++) { for (int j = i + 1; j < N - 1; j++) { flg = 0; for (int k = j + 1; k < N; k++) { if (L.at(k) < L.at(i) + L.at(j)) { R++; } else { flg = 1; } if ((flg == 1) && (L.at(k) != L.at(k + 1))) { break; } } } } cout << R << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int N, R = 0, flg = 0; cin >> N; vector<int> L(N); for (int i = 0; i < N; i++) { cin >> L.at(i); } sort(L.begin(), L.end()); for (int i = 0; i < N - 2; i++) { for (int j = i + 1; j < N - 1; j++) { flg = 0; for (int k = j + 1; k < N; k++) { if (L.at(k) < L.at(i) + L.at(j)) { R++; } else { flg = 1; } if ((flg == 1) && (k != N - 1)) { if (L.at(k) != L.at(k + 1)) { break; } } } } } cout << R << endl; }
replace
21
23
21
25
-6
terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check: __n (which is 4) >= this->size() (which is 4)
p02888
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define ROOP(i, N) for (int i = 0; i++; i < N) #define RVROOP(i, N) for (int i = N; i--; i >= 0) #define INF 1e9 #define ALL(v) v.begin(), v.end() using namespace std; typedef long long ll; int main() { int N; cin >> N; vector<int> L(N); for (int i = 0; i < N; i++) { cin >> L.at(i); } sort(L.begin(), L.end()); int ans = 0; for (int i = N - 1; i > 0; i--) { for (int j = i - 1; j > 0; j--) { for (int k = j - 1; k >= 0; k--) { if (L.at(k) + L.at(j) > L.at(i)) { ans++; } else { break; } } } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define ROOP(i, N) for (int i = 0; i++; i < N) #define RVROOP(i, N) for (int i = N; i--; i >= 0) #define INF 1e9 #define ALL(v) v.begin(), v.end() using namespace std; typedef long long ll; int main() { int N; cin >> N; vector<int> L(N); for (int i = 0; i < N; i++) { cin >> L.at(i); } sort(L.begin(), L.end()); int ans = 0; for (int i = N - 1; i >= 2; i--) { if (L.at(0) + L.at(1) > L.at(i)) { ans += i * (i - 1) / 2; } else { for (int j = i - 1; j > 0; j--) { for (int k = j - 1; k >= 0; k--) { if (L.at(k) + L.at(j) > L.at(i)) { ans++; } else { break; } } } } } cout << ans << endl; return 0; }
replace
21
28
21
32
TLE
p02888
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vl = vector<ll>; using vvl = vector<vl>; using P = pair<int, int>; using PL = pair<ll, ll>; using vp = vector<P>; using vpl = vector<PL>; template <typename T> constexpr auto inf = numeric_limits<T>::max() / 2; constexpr int INF = inf<int>, MOD = 1000000007; constexpr ll LINF = inf<ll>; #define _ol3(_1, _2, _3, name, ...) name #define _rep(i, n) _repi(i, 0, n) #define _repi(i, a, b) for (int i = a, i##_l = (b); i < i##_l; ++i) #define REP(...) _ol3(__VA_ARGS__, _repi, _rep, )(__VA_ARGS__) #define REPR(i, n) for (int i = n - 1; i >= 0; --i) #define REPA(i, v) REP(i, (v).size()) #define all(v) (v).begin(), (v).end() #define rall(v) (v).rbegin(), (v).rend() #define bit(n) (1ll << (n)) #define F first #define S second #define endl '\n' #define cho(n, a, b) cout << ((n) ? a : b) << endl void YES(int n) { cho(n, "YES", "NO"); } void Yes(int n) { cho(n, "Yes", "No"); } void Poss(int n) { cho(n, "Possible", "Impossible"); } void _print(ostream &) {} template <class T, class... U> void _print(ostream &s, const T &t, const U &...u) { s << t << (sizeof...(u) ? ' ' : '\n'); _print(s, u...); } template <class... T> void print(const T &...t) { _print(cout, t...); } template <class... T> void dprint(const T &...t) { _print(cerr, t...); } #ifndef LOCAL struct osd { template <class T> osd &operator<<(const T &t) { return *this; } }; osd cer_; #define dprint(...) #define cerr cer_ #endif template <class T> bool chmax(T &a, const T &b) { bool x = a < b; x ? a = b : b; return x; } template <class T> bool chmin(T &a, const T &b) { bool x = a > b; x ? a = b : b; return x; } template <class T, class U> ostream &operator<<(ostream &o, const pair<T, U> &p) { return o << p.F << ' ' << p.S; } template <class T, class U> istream &operator>>(istream &i, pair<T, U> &p) { return i >> p.F >> p.S; } template <class T> class iterable { static false_type c(string v); template <class U> static auto c(U v) -> decltype(all(v), true_type()); static false_type c(...); public: const static bool value = decltype(c(declval<T>()))::value; }; template <class T, typename = enable_if_t<iterable<T>::value>> ostream &operator<<(ostream &o, const T &v) { for (auto &&i : v) o << i << ' '; return o; } template <class T> istream &operator>>(istream &i, vector<T> &v) { for (T &j : v) i >> j; return i; } template <class T> vector<T> &operator<<(vector<T> &v, const T &t) { v.push_back(t); return v; } template <class T> set<T> &operator<<(set<T> &v, const T &t) { v.insert(t); return v; } template <class T> multiset<T> &operator<<(multiset<T> &v, const T &t) { v.insert(t); return v; } int main() { cin.tie(0); ios::sync_with_stdio(0); cout << setprecision(10); int n; cin >> n; vi l(n); cin >> l; int s = 0; sort(all(l)); REP(i, n) { REP(j, n) { if (i == j) continue; dprint(string(10, '-')); const int a = l[i]; const int b = l[j]; dprint(a, b); const int ma = a + b; const int mi = abs(a - b); dprint(mi, '<', ma); const auto itl = upper_bound(all(l), mi); const auto itr = lower_bound(all(l), ma); int k = itr - itl; dprint(k); if (k > 0) { if (mi < a && a < ma) --k; if (mi < b && b < ma) --k; dprint(k); s += k; } } } dprint(s); assert(s % 6 == 0); cout << s / 6 << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vl = vector<ll>; using vvl = vector<vl>; using P = pair<int, int>; using PL = pair<ll, ll>; using vp = vector<P>; using vpl = vector<PL>; template <typename T> constexpr auto inf = numeric_limits<T>::max() / 2; constexpr int INF = inf<int>, MOD = 1000000007; constexpr ll LINF = inf<ll>; #define _ol3(_1, _2, _3, name, ...) name #define _rep(i, n) _repi(i, 0, n) #define _repi(i, a, b) for (int i = a, i##_l = (b); i < i##_l; ++i) #define REP(...) _ol3(__VA_ARGS__, _repi, _rep, )(__VA_ARGS__) #define REPR(i, n) for (int i = n - 1; i >= 0; --i) #define REPA(i, v) REP(i, (v).size()) #define all(v) (v).begin(), (v).end() #define rall(v) (v).rbegin(), (v).rend() #define bit(n) (1ll << (n)) #define F first #define S second #define endl '\n' #define cho(n, a, b) cout << ((n) ? a : b) << endl void YES(int n) { cho(n, "YES", "NO"); } void Yes(int n) { cho(n, "Yes", "No"); } void Poss(int n) { cho(n, "Possible", "Impossible"); } void _print(ostream &) {} template <class T, class... U> void _print(ostream &s, const T &t, const U &...u) { s << t << (sizeof...(u) ? ' ' : '\n'); _print(s, u...); } template <class... T> void print(const T &...t) { _print(cout, t...); } template <class... T> void dprint(const T &...t) { _print(cerr, t...); } #ifndef LOCAL struct osd { template <class T> osd &operator<<(const T &t) { return *this; } }; osd cer_; #define dprint(...) #define cerr cer_ #endif template <class T> bool chmax(T &a, const T &b) { bool x = a < b; x ? a = b : b; return x; } template <class T> bool chmin(T &a, const T &b) { bool x = a > b; x ? a = b : b; return x; } template <class T, class U> ostream &operator<<(ostream &o, const pair<T, U> &p) { return o << p.F << ' ' << p.S; } template <class T, class U> istream &operator>>(istream &i, pair<T, U> &p) { return i >> p.F >> p.S; } template <class T> class iterable { static false_type c(string v); template <class U> static auto c(U v) -> decltype(all(v), true_type()); static false_type c(...); public: const static bool value = decltype(c(declval<T>()))::value; }; template <class T, typename = enable_if_t<iterable<T>::value>> ostream &operator<<(ostream &o, const T &v) { for (auto &&i : v) o << i << ' '; return o; } template <class T> istream &operator>>(istream &i, vector<T> &v) { for (T &j : v) i >> j; return i; } template <class T> vector<T> &operator<<(vector<T> &v, const T &t) { v.push_back(t); return v; } template <class T> set<T> &operator<<(set<T> &v, const T &t) { v.insert(t); return v; } template <class T> multiset<T> &operator<<(multiset<T> &v, const T &t) { v.insert(t); return v; } int main() { cin.tie(0); ios::sync_with_stdio(0); cout << setprecision(10); int n; cin >> n; vi l(n); cin >> l; ll s = 0; sort(all(l)); REP(i, n) { REP(j, n) { if (i == j) continue; dprint(string(10, '-')); const int a = l[i]; const int b = l[j]; dprint(a, b); const int ma = a + b; const int mi = abs(a - b); dprint(mi, '<', ma); const auto itl = upper_bound(all(l), mi); const auto itr = lower_bound(all(l), ma); int k = itr - itl; dprint(k); if (k > 0) { if (mi < a && a < ma) --k; if (mi < b && b < ma) --k; dprint(k); s += k; } } } dprint(s); assert(s % 6 == 0); cout << s / 6 << endl; }
replace
103
104
103
104
0
p02888
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int> L(N); for (int i = 0; i < N; i++) { cin >> L[i]; } int ans = 0; for (int i = 0; i < N - 2; i++) { for (int j = i + 1; j < N - 1; j++) { for (int k = j + 1; k < N; k++) { if (L[i] < L[j] + L[k] && L[j] < L[i] + L[k] && L[k] < L[i] + L[j]) { ans++; } } } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int> L(N); for (int i = 0; i < N; i++) { cin >> L[i]; } int ans = 0; for (int i = 0; i < N - 2; i++) { for (int j = i + 1; j < N - 1; j++) { for (int k = j + 1; k < N; k++) { if (max({L[i], L[j], L[k]}) * 2 < L[i] + L[j] + L[k]) { ans++; } } } } cout << ans << endl; }
replace
14
15
14
15
TLE
p02888
C++
Time Limit Exceeded
#include <algorithm> /*sort()を使用するのに必要*/ #include <iostream> #include <vector> using namespace std; int main() { int N; vector<int> L; cin >> N; for (int i = 0; i < N; i++) { int j; cin >> j; L.push_back(j); } // sort(L.begin(), L.end()); int count = 0; for (int i = 0; i < N - 2; i++) { for (int j = i + 1; j < N - 1; j++) { // 二分探索で3つめを絞る /*nt start = j + 1; int end = N - 1; int middle = (start + end) / 2; int k; while(start != end){ if(L[middle] >= L[i] + L[j]){ end = middle - 1; }else{ continue; } } */ for (int k = j + 1; k < N; k++) { if (L[i] >= L[j] + L[k]) continue; if (L[j] >= L[i] + L[k]) continue; if (L[k] >= L[i] + L[j]) continue; // すでにやっている count++; } } } cout << count; return 0; }
#include <algorithm> /*sort()を使用するのに必要*/ #include <iostream> #include <vector> using namespace std; int main() { int N; vector<int> L; cin >> N; for (int i = 0; i < N; i++) { int j; cin >> j; L.push_back(j); } sort(L.begin(), L.end()); int count = 0; for (int i = 0; i < N - 2; i++) { for (int j = i + 1; j < N - 1; j++) { // 二分探索で3つめを絞る /*nt start = j + 1; int end = N - 1; int middle = (start + end) / 2; int k; while(start != end){ if(L[middle] >= L[i] + L[j]){ end = middle - 1; }else{ continue; } } */ for (int k = j + 1; k < N; k++) { if (L[i] >= L[j] + L[k]) continue; if (L[j] >= L[i] + L[k]) continue; if (L[k] >= L[i] + L[j]) continue; // すでにやっている count++; } } } cout << count; return 0; }
replace
17
18
17
18
TLE
p02888
C++
Time Limit Exceeded
#include <bits/stdc++.h> //----***やべーやつら***---- using namespace std; #define int long long //----***型定義***---- using ll = long long; using pll = pair<ll, ll>; using pdd = pair<double, double>; typedef long double lb; typedef vector<int, vector<int>> vvec; typedef long double ld; //----***Like a Pythonista***---- #define REP(i, j, n) for (ll i = j; i < (n); i++) #define RREP(i, n, j) for (ll i = n; j < i; i--) #define each(i, ...) for (auto &&i : __VA_ARGS__) #define ALL(vec) (vec).begin(), (vec).end() #define sum(...) accumulate(all(__VA_ARGS__), 0LL) #define dsum(...) accumulate(all(__VA_ARGS__), 0.0L) #define vec(type, name, ...) vector<type> name(__VA_ARGS__) template <class T> inline auto max(const T &a) { return *max_element(all(a)); } template <class T> inline auto min(const T &a) { return *min_element(all(a)); } inline ll gcd(ll a, ll b) { while (b) { ll c = b; b = a % b; a = c; } return a; } inline ll lcm(ll a, ll b) { if (!a || !b) return 0; return a * b / gcd(a, b); } //----***定数***---- const int MOD = 1000000007; #define INF 1e9; #define EPS 1e-9; //----***入出力***---- #define print(out) cout << out << "\n"; #define debug(var) \ do { \ std::cerr << #var << " ↓ " \ << "\n"; \ view(var); \ } while (0) template <typename T> void view(T e) { std::cout << e << std::endl; } template <typename T> void view(const std::vector<T> &v) { for (const auto &e : v) { std::cout << e << " "; } std::cout << std::endl; } template <typename T> void view(const std::vector<std::vector<T>> &vv) { for (const auto &v : vv) { view(v); } } #define HERE cout << "Here" << endl; //----***初期時読み込み***---- struct initial { initial() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(20); }; } initial_; signed main() { int N, ans = 0; cin >> N; vector<int> L(N); REP(i, 0, N) { cin >> L[i]; } REP(i, 0, N) REP(j, i + 1, N) REP(k, j + 1, N) { if ((L[i] < L[j] + L[k]) && (L[j] < L[k] + L[i]) && (L[k] < L[j] + L[i])) { ans++; } } print(ans) }
#include <bits/stdc++.h> //----***やべーやつら***---- using namespace std; #define int long long //----***型定義***---- using ll = long long; using pll = pair<ll, ll>; using pdd = pair<double, double>; typedef long double lb; typedef vector<int, vector<int>> vvec; typedef long double ld; //----***Like a Pythonista***---- #define REP(i, j, n) for (ll i = j; i < (n); i++) #define RREP(i, n, j) for (ll i = n; j < i; i--) #define each(i, ...) for (auto &&i : __VA_ARGS__) #define ALL(vec) (vec).begin(), (vec).end() #define sum(...) accumulate(all(__VA_ARGS__), 0LL) #define dsum(...) accumulate(all(__VA_ARGS__), 0.0L) #define vec(type, name, ...) vector<type> name(__VA_ARGS__) template <class T> inline auto max(const T &a) { return *max_element(all(a)); } template <class T> inline auto min(const T &a) { return *min_element(all(a)); } inline ll gcd(ll a, ll b) { while (b) { ll c = b; b = a % b; a = c; } return a; } inline ll lcm(ll a, ll b) { if (!a || !b) return 0; return a * b / gcd(a, b); } //----***定数***---- const int MOD = 1000000007; #define INF 1e9; #define EPS 1e-9; //----***入出力***---- #define print(out) cout << out << "\n"; #define debug(var) \ do { \ std::cerr << #var << " ↓ " \ << "\n"; \ view(var); \ } while (0) template <typename T> void view(T e) { std::cout << e << std::endl; } template <typename T> void view(const std::vector<T> &v) { for (const auto &e : v) { std::cout << e << " "; } std::cout << std::endl; } template <typename T> void view(const std::vector<std::vector<T>> &vv) { for (const auto &v : vv) { view(v); } } #define HERE cout << "Here" << endl; //----***初期時読み込み***---- struct initial { initial() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(20); }; } initial_; signed main() { int N, ans = 0; cin >> N; vector<int> L(N); REP(i, 0, N) { cin >> L[i]; } sort(ALL(L)); RREP(i, N - 1, 1) RREP(j, i - 1, 0) { int left = L[i] - L[j]; auto iter2 = upper_bound(ALL(L), left); left = min(j, max(0LL, (int)distance(L.begin(), iter2))); // debug(left); // print(i<<" "<<j) // debug(j-left); ans += j - left; } print(ans) }
replace
78
82
78
87
TLE
p02888
C++
Runtime Error
#include <bits/stdc++.h> #define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; i++) #define FORN(i, b, a) for (int i = (b); _a = (a); i >= _a; i--) #define REP(i, n) for (int i = 0, _n = n; i < n; i++) #define ll long long #define pii pair<int, int> #define re return #define vi vector<int> #define pb push_back #define si set<int> #define in insert #define fl float #define db double #define ld long double #define X first #define Y second #define st string #define ull unsigned long long #define mod 1000000007 #define INF 1000000007 #define x1 XZVJDFADSPFOE #define y1 GASDIJSLDAEJF #define x2 DFDAJKVOHKWIW #define y2 PSFSAODSXVNMQ using namespace std; inline void read(int &x) { short negative = 1; x = 0; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') negative = -1; c = getchar(); } while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); x *= negative; } int main() { int n; int a[1111]; cin >> n; REP(i, n) cin >> a[i]; sort(a, a + n); ll ans = 0; REP(i, n) for (int j = i + 1; j < n; j++) { int l = j + 1; int r = n - 1; while (l <= r) { int mid = (l + r) / 2; if (a[i] + a[j] > a[mid]) l = mid + 1; else r = mid - 1; } ans += l - j - 1; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; i++) #define FORN(i, b, a) for (int i = (b); _a = (a); i >= _a; i--) #define REP(i, n) for (int i = 0, _n = n; i < n; i++) #define ll long long #define pii pair<int, int> #define re return #define vi vector<int> #define pb push_back #define si set<int> #define in insert #define fl float #define db double #define ld long double #define X first #define Y second #define st string #define ull unsigned long long #define mod 1000000007 #define INF 1000000007 #define x1 XZVJDFADSPFOE #define y1 GASDIJSLDAEJF #define x2 DFDAJKVOHKWIW #define y2 PSFSAODSXVNMQ using namespace std; inline void read(int &x) { short negative = 1; x = 0; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') negative = -1; c = getchar(); } while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); x *= negative; } int main() { int n; int a[3111]; cin >> n; REP(i, n) cin >> a[i]; sort(a, a + n); ll ans = 0; REP(i, n) for (int j = i + 1; j < n; j++) { int l = j + 1; int r = n - 1; while (l <= r) { int mid = (l + r) / 2; if (a[i] + a[j] > a[mid]) l = mid + 1; else r = mid - 1; } ans += l - j - 1; } cout << ans << endl; return 0; }
replace
40
41
40
41
0
p02888
C++
Runtime Error
#include <algorithm> #include <iostream> using namespace std; const int L = 1000; int main() { ios::sync_with_stdio(false); int n; cin >> n; int cnt[L + 1] = {0}; for (int i = 0; i < n; i++) { int l; cin >> l; cnt[l]++; } int ps[L + 1]; for (int i = 0; i <= L; i++) { ps[i] = i == 0 ? 0 : ps[i - 1] + cnt[i]; } int ans = 0; for (int i = 1; i <= L; i++) { ans += 1ll * cnt[i] * (cnt[i] - 1) * (cnt[i] - 2) / 6; } for (int i = 1; i <= L; i++) { ans += cnt[i] * (cnt[i] - 1) / 2 * (ps[2 * i - 1] - cnt[i]); } for (int i = 1; i <= L; i++) { for (int j = i + 1; j <= L; j++) { ans += cnt[i] * cnt[j] * (ps[min(i + j - 1, L)] - ps[j]); } } cout << ans << endl; return 0; }
#include <algorithm> #include <iostream> using namespace std; const int L = 1000; int main() { ios::sync_with_stdio(false); int n; cin >> n; int cnt[L + 1] = {0}; for (int i = 0; i < n; i++) { int l; cin >> l; cnt[l]++; } int ps[L + 1]; for (int i = 0; i <= L; i++) { ps[i] = i == 0 ? 0 : ps[i - 1] + cnt[i]; } int ans = 0; for (int i = 1; i <= L; i++) { ans += 1ll * cnt[i] * (cnt[i] - 1) * (cnt[i] - 2) / 6; } for (int i = 1; i <= L; i++) { ans += cnt[i] * (cnt[i] - 1) / 2 * (ps[min(2 * i - 1, L)] - cnt[i]); } for (int i = 1; i <= L; i++) { for (int j = i + 1; j <= L; j++) { ans += cnt[i] * cnt[j] * (ps[min(i + j - 1, L)] - ps[j]); } } cout << ans << endl; return 0; }
replace
30
31
30
31
0
p02888
C++
Runtime Error
/* Author: Dhruv Rastogi */ #include <bits/stdc++.h> using namespace std; #define ll long long #define int long long #define all(sdk) sdk.begin(), sdk.end() #define mll map<int, int> #define pb push_back #define rep(i, a, b) for (int i = a; i < b; ++i) #define repd(i, a, b) for (int i = a; i >= b; --i) #define mp make_pair #define hell 1000000007 #define endl '\n' #define vvll vector<vector<int>> #define vll vector<int> #define mint map<int, int> #define sz(x) (int)x.size() #define sll set<int> #define pll pair<int, int> #define F first #define S second const long double PI = acos(-1.0); int a[1005]; void solve() { int n; cin >> n; rep(i, 0, n) cin >> a[i]; sort(a, a + n); int ans = 0; rep(i, 0, n) { rep(j, i + 1, n) { int idx = lower_bound(a, a + n, a[i] + a[j]) - a - 1; ans += idx - j; } } cout << ans << endl; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; // cin>>t; while (t--) { solve(); } return 0; }
/* Author: Dhruv Rastogi */ #include <bits/stdc++.h> using namespace std; #define ll long long #define int long long #define all(sdk) sdk.begin(), sdk.end() #define mll map<int, int> #define pb push_back #define rep(i, a, b) for (int i = a; i < b; ++i) #define repd(i, a, b) for (int i = a; i >= b; --i) #define mp make_pair #define hell 1000000007 #define endl '\n' #define vvll vector<vector<int>> #define vll vector<int> #define mint map<int, int> #define sz(x) (int)x.size() #define sll set<int> #define pll pair<int, int> #define F first #define S second const long double PI = acos(-1.0); int a[2005]; void solve() { int n; cin >> n; rep(i, 0, n) cin >> a[i]; sort(a, a + n); int ans = 0; rep(i, 0, n) { rep(j, i + 1, n) { int idx = lower_bound(a, a + n, a[i] + a[j]) - a - 1; ans += idx - j; } } cout << ans << endl; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; // cin>>t; while (t--) { solve(); } return 0; }
replace
23
24
23
24
0
p02888
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; // define #define int long long #define vint vector<int> #define rep(x, y) for (int i = x; i < y; i++) #define REP(x, y) for (int j = x; j < y; j++) #define all(x) x.begin(), x.end() #define pb() push_back() #define mod 100000007 #define elif else if #define str string // main signed main() { int N, M, O, Q, count = 0, count2 = 0, count3 = 0; cin >> N; vint L(N); rep(0, N) cin >> L[i]; sort(all(L)); rep(0, N - 2) { REP(i + 1, N - 1) { for (int k = j + 1; k < N; k++) { if (i != j || i != k || j != k) { if (L[i] < L[j] + L[k] && L[j] < L[i] + L[k] && L[k] < L[i] + L[j]) count += 1; } } } } cout << count << endl; }
#include <bits/stdc++.h> using namespace std; // define #define int long long #define vint vector<int> #define rep(x, y) for (int i = x; i < y; i++) #define REP(x, y) for (int j = x; j < y; j++) #define all(x) x.begin(), x.end() #define pb() push_back() #define mod 100000007 #define elif else if #define str string // main signed main() { int N, M, O, Q, count = 0, count2 = 0, count3 = 0; cin >> N; vint L(N); rep(0, N) cin >> L[i]; sort(all(L)); rep(0, N - 2) { REP(i + 1, N - 1) { for (int k = j + 1; k < N; k++) { if (i != j || i != k || j != k) { if (L[k] < L[i] + L[j]) count += 1; } } } } cout << count << endl; }
replace
23
24
23
24
TLE
p02888
C++
Time Limit Exceeded
#include <stdio.h> #include <stdlib.h> int ascSort(const void *v1, const void *v2); int ascSort(const void *v1, const void *v2) { const int _v1 = *((const int *)v1); const int _v2 = *((const int *)v2); if (_v1 > _v2) { return 1; } else if (_v1 < _v2) { return -1; } else { return 0; } } int main(void) { int N, L[2 * 1000]; int a, b, c, i; int ans = 0; scanf("%d", &N); for (i = 0; i < N; i++) { scanf("%d", &L[i]); } qsort(L, N, sizeof(L[0]), ascSort); for (a = 0; a < N; a++) { for (b = a + 1; b < N; b++) { for (c = b + 1; (c < N) || (L[a] + L[b] < L[c]); c++) { if ((L[a] < L[b] + L[c]) && (L[b] < L[a] + L[c]) && (L[c] < L[a] + L[b])) { ans++; } } } } printf("%d", ans); return 0; }
#include <stdio.h> #include <stdlib.h> int ascSort(const void *v1, const void *v2); int ascSort(const void *v1, const void *v2) { const int _v1 = *((const int *)v1); const int _v2 = *((const int *)v2); if (_v1 > _v2) { return 1; } else if (_v1 < _v2) { return -1; } else { return 0; } } int main(void) { int N, L[2 * 1000]; int a, b, c, i; int ans = 0; scanf("%d", &N); for (i = 0; i < N; i++) { scanf("%d", &L[i]); } qsort(L, N, sizeof(L[0]), ascSort); for (a = 0; a < N; a++) { for (b = a + 1; b < N; b++) { for (c = b + 1; (c < N) && (L[c] < L[a] + L[b]); c++) { if ((L[a] < L[b] + L[c]) && (L[b] < L[a] + L[c]) && (L[c] < L[a] + L[b])) { ans++; } } } } printf("%d", ans); return 0; }
replace
32
33
32
33
TLE
p02888
C++
Runtime Error
// #pragma GCC optimize("Ofast") #pragma GCC optimize("O3") #pragma GCC target("avx,avx2,fma") #include <algorithm> #include <cmath> #include <cstdlib> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> constexpr auto INF = 9223372036854775807; typedef long long int ll; typedef unsigned long long int ull; typedef unsigned long int ul; #define f(i, a, b) for (ll i = (ll)a; i < (ll)b; i += 1) #define rf(i, a, b) for (ll i = (ll)a; i >= (ll)b; i -= 1) #define endl '\n' #define N 1000000007 // prime modulo value #define M 998244353 #define all(x) x.begin(), x.end() using namespace std; // From Geeksforgeeks inline ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } // Power function took from geeksforgeeks ll power(ll x, ll y) { ll res = 1; x = x % N; if (x == 0) return 0; while (y > 0) { if (y & 1) res = (res * x) % N; y = y >> 1; x = (x * x) % N; } return res; } double dist(ll x1, ll y1, ll x2, ll y2) { double ans = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); return ans; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); ll n; cin >> n; vector<ll> arr(n, 0); f(i, 0, n) { cin >> arr[i]; } sort(all(arr)); ll ans = 0; f(i, 0, n) { f(j, i + 1, n) { f(k, j + 1, n) { if (arr[k] < arr[i] + arr[j]) { ans += 1; } } } } cout << ans; return 0; }
// #pragma GCC optimize("Ofast") // #pragma GCC optimize("O3") #pragma GCC target("avx,avx2,fma") #include <algorithm> #include <cmath> #include <cstdlib> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> constexpr auto INF = 9223372036854775807; typedef long long int ll; typedef unsigned long long int ull; typedef unsigned long int ul; #define f(i, a, b) for (ll i = (ll)a; i < (ll)b; i += 1) #define rf(i, a, b) for (ll i = (ll)a; i >= (ll)b; i -= 1) #define endl '\n' #define N 1000000007 // prime modulo value #define M 998244353 #define all(x) x.begin(), x.end() using namespace std; // From Geeksforgeeks inline ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } // Power function took from geeksforgeeks ll power(ll x, ll y) { ll res = 1; x = x % N; if (x == 0) return 0; while (y > 0) { if (y & 1) res = (res * x) % N; y = y >> 1; x = (x * x) % N; } return res; } double dist(ll x1, ll y1, ll x2, ll y2) { double ans = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); return ans; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); ll n; cin >> n; vector<ll> arr(n, 0); f(i, 0, n) { cin >> arr[i]; } sort(all(arr)); ll ans = 0; f(i, 0, n) { f(j, i + 1, n) { f(k, j + 1, n) { if (arr[k] < arr[i] + arr[j]) { ans += 1; } } } } cout << ans; return 0; }
replace
2
3
2
3
0
p02889
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <queue> #include <utility> #include <vector> using namespace std; typedef long long ll; typedef pair<ll, ll> P; typedef pair<P, ll> Pl; const ll INF = 1000000000000000; const int MAX = 310; vector<pair<ll, ll>> G[MAX]; // (行き先,cost) priority_queue<Pl, vector<Pl>, greater<Pl>> que; //(count,energy,位置) ll ans[310][310]; void dijkstra(int s, int l) { que.push({{0, -l}, s}); while (!que.empty()) { Pl p = que.top(); que.pop(); ll x = p.second; ll cnt = p.first.first, en = -(p.first.second); if (ans[s][x] != INF) continue; ans[s][x] = cnt; for (P e : G[x]) { if (en >= e.second) { que.push({{cnt, -(en - e.second)}, e.first}); } else { que.push({{cnt + 1, -(l - e.second)}, e.first}); } } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int i, j, n, m, q; ll l; cin >> n >> m >> l; for (i = 0; i < m; i++) { int a, b, c; cin >> a >> b >> c; a--; b--; if (c > l) continue; G[a].push_back({b, c}); G[b].push_back({a, c}); } for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { ans[i][j] = INF; } } for (i = 0; i < n; i++) { dijkstra(i, l); } cin >> q; for (i = 0; i < q; i++) { int s, t; cin >> s >> t; s--; t--; if (ans[s][t] < INF) cout << ans[s][t] << endl; else cout << -1 << endl; } }
#include <algorithm> #include <iostream> #include <queue> #include <utility> #include <vector> using namespace std; typedef long long ll; typedef pair<ll, ll> P; typedef pair<P, ll> Pl; const ll INF = 1000000000000000; const int MAX = 310; vector<pair<ll, ll>> G[MAX]; // (行き先,cost) priority_queue<Pl, vector<Pl>, greater<Pl>> que; //(count,energy,位置) ll ans[310][310]; void dijkstra(int s, int l) { que.push({{0, -l}, s}); while (!que.empty()) { Pl p = que.top(); que.pop(); ll x = p.second; ll cnt = p.first.first, en = -(p.first.second); if (ans[s][x] != INF) continue; ans[s][x] = cnt; for (P e : G[x]) { if (ans[s][e.first] != INF) continue; if (en >= e.second) { que.push({{cnt, -(en - e.second)}, e.first}); } else { que.push({{cnt + 1, -(l - e.second)}, e.first}); } } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int i, j, n, m, q; ll l; cin >> n >> m >> l; for (i = 0; i < m; i++) { int a, b, c; cin >> a >> b >> c; a--; b--; if (c > l) continue; G[a].push_back({b, c}); G[b].push_back({a, c}); } for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { ans[i][j] = INF; } } for (i = 0; i < n; i++) { dijkstra(i, l); } cin >> q; for (i = 0; i < q; i++) { int s, t; cin >> s >> t; s--; t--; if (ans[s][t] < INF) cout << ans[s][t] << endl; else cout << -1 << endl; } }
insert
26
26
26
28
TLE
p02889
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define ll int #define ld long double #define pii pair<int, int> #define pld pair<ld, ld> #define all(v) v.begin(), v.end() #define F first #define S second #define mod 1000000007 #define pb push_back #define MP make_pair #define mset(x) memset(x, 0, sizeof(x)); #define ios \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define inf 10000000000000007 const int N = 304; ll ans[N][N]; ll vis[N]; vector<pair<ll, ll>> adj[N]; priority_queue<pair<pair<ll, ll>, ll>, vector<pair<pair<ll, ll>, ll>>, greater<pair<pair<ll, ll>, ll>>> pq; void run() { ll n, m, k, u, v, w, i, q; cin >> n >> m >> k; while (m--) { cin >> u >> v >> w; adj[u].pb(MP(v, w)); adj[v].pb(MP(u, w)); } memset(ans, -1, sizeof ans); for (i = 1; i <= n; i++) { memset(vis, 0, sizeof vis); pq.push(MP(MP(0, -k), i)); while (!pq.empty()) { pair<pair<ll, ll>, ll> p = pq.top(); u = p.S; pq.pop(); if (vis[u]) continue; vis[u] = 1; ans[i][u] = p.F.F; for (auto it : adj[u]) { v = it.F; w = it.S; if (w <= -p.F.S) pq.push(MP(MP(p.F.F, -(-p.F.S - w)), v)); else if (w <= k) pq.push(MP(MP(p.F.F + 1, -(k - w)), v)); } } } cin >> q; while (q--) { cin >> u >> v; cout << ans[u][v] << "\n"; } } void setup() { ios; // cout << setprecision(20); // #ifndef ONLINE_JUDGE // freopen("input", "r", stdin); // freopen("output", "w", stdout); // #endif } signed main() { setup(); int tests = 1; // cin >> tests; for (int i = 1; i <= tests; i++) run(); return 0; }
#include <bits/stdc++.h> using namespace std; #define ll int #define ld long double #define pii pair<int, int> #define pld pair<ld, ld> #define all(v) v.begin(), v.end() #define F first #define S second #define mod 1000000007 #define pb push_back #define MP make_pair #define mset(x) memset(x, 0, sizeof(x)); #define ios \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define inf 10000000000000007 const int N = 304; ll ans[N][N]; ll vis[N]; vector<pair<ll, ll>> adj[N]; priority_queue<pair<pair<ll, ll>, ll>, vector<pair<pair<ll, ll>, ll>>, greater<pair<pair<ll, ll>, ll>>> pq; void run() { ll n, m, k, u, v, w, i, q; cin >> n >> m >> k; while (m--) { cin >> u >> v >> w; adj[u].pb(MP(v, w)); adj[v].pb(MP(u, w)); } memset(ans, -1, sizeof ans); for (i = 1; i <= n; i++) { memset(vis, 0, sizeof vis); pq.push(MP(MP(0, -k), i)); while (!pq.empty()) { pair<pair<ll, ll>, ll> p = pq.top(); u = p.S; pq.pop(); if (vis[u]) continue; vis[u] = 1; ans[i][u] = p.F.F; for (auto it : adj[u]) { v = it.F; w = it.S; if (vis[v]) continue; if (w <= -p.F.S) pq.push(MP(MP(p.F.F, -(-p.F.S - w)), v)); else if (w <= k) pq.push(MP(MP(p.F.F + 1, -(k - w)), v)); } } } cin >> q; while (q--) { cin >> u >> v; cout << ans[u][v] << "\n"; } } void setup() { ios; // cout << setprecision(20); // #ifndef ONLINE_JUDGE // freopen("input", "r", stdin); // freopen("output", "w", stdout); // #endif } signed main() { setup(); int tests = 1; // cin >> tests; for (int i = 1; i <= tests; i++) run(); return 0; }
insert
49
49
49
51
TLE
p02889
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <forward_list> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <utility> #include <vector> #define rep(i, s, g) for ((i) = (s); (i) < (g); ++(i)) using namespace std; using ll = long long; using P = pair<ll, ll>; const ll MOD = 1e9 + 7; const ll INF = (1ll << 62) - 1; int main(void) { int n, m, l; int a, b, c; int q; vector<int> s(10010), t(10010); ll d[500][500], count[500][500]; int i, j, k; cin >> n >> m >> l; rep(i, 0, n) { rep(j, 0, n) { if (i == j) { d[i][j] = 0; } else { d[i][j] = INF; } } } rep(i, 0, m) { cin >> a >> b >> c; d[a - 1][b - 1] = c; d[b - 1][a - 1] = c; } rep(i, 0, n) { rep(j, 0, n) { rep(k, 0, n) { d[j][k] = min(d[j][k], d[j][i] + d[i][k]); if (j == k) { count[j][k] = 0; } else if (d[j][k] <= l) { count[j][k] = 1; } else { count[j][k] = INF; } } } } rep(i, 0, n) { rep(j, 0, n) { rep(k, 0, n) { count[j][k] = min(count[j][k], count[j][i] + count[i][k]); } } } cin >> q; rep(i, 0, q) { cin >> s[i] >> t[i]; } rep(i, 0, q) { if (count[s[i] - 1][t[i] - 1] == INF) { cout << -1 << endl; } else { cout << count[s[i] - 1][t[i] - 1] - 1 << endl; } } }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <forward_list> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <utility> #include <vector> #define rep(i, s, g) for ((i) = (s); (i) < (g); ++(i)) using namespace std; using ll = long long; using P = pair<ll, ll>; const ll MOD = 1e9 + 7; const ll INF = (1ll << 62) - 1; int main(void) { int n, m, l; int a, b, c; int q; vector<int> s(100010), t(100010); ll d[500][500], count[500][500]; int i, j, k; cin >> n >> m >> l; rep(i, 0, n) { rep(j, 0, n) { if (i == j) { d[i][j] = 0; } else { d[i][j] = INF; } } } rep(i, 0, m) { cin >> a >> b >> c; d[a - 1][b - 1] = c; d[b - 1][a - 1] = c; } rep(i, 0, n) { rep(j, 0, n) { rep(k, 0, n) { d[j][k] = min(d[j][k], d[j][i] + d[i][k]); if (j == k) { count[j][k] = 0; } else if (d[j][k] <= l) { count[j][k] = 1; } else { count[j][k] = INF; } } } } rep(i, 0, n) { rep(j, 0, n) { rep(k, 0, n) { count[j][k] = min(count[j][k], count[j][i] + count[i][k]); } } } cin >> q; rep(i, 0, q) { cin >> s[i] >> t[i]; } rep(i, 0, q) { if (count[s[i] - 1][t[i] - 1] == INF) { cout << -1 << endl; } else { cout << count[s[i] - 1][t[i] - 1] - 1 << endl; } } }
replace
23
24
23
24
0
p02889
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for (long long i = 0; i < n; i++) #define repr(i, n, m) for (long long i = m; i < n; i++) #define mod 1000000007 #define sorti(x) sort(x.begin(), x.end()) #define sortd(x) sort(x.begin(), x.end(), std::greater<long long>()) #define debug(x) std::cerr << (x) << std::endl; #define roll(x) \ for (auto &&itr : x) { \ cerr << (itr) << " "; \ } ll inf = 10000000000; template <class T> inline void chmax(T &ans, T t) { if (t > ans) ans = t; } template <class T> inline void chmin(T &ans, T t) { if (t < ans) ans = t; } template <class T> void warshall_floyd(vector<vector<T>> &cost, int N) { for (ll k = 0; k < N; ++k) { for (ll i = 0; i < N; ++i) { for (ll j = 0; j < N; ++j) { cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]); } } } } /* END */ /* int main() { int h,w; cin >> h >> w; vector< vector<long long> > cost(10, vector<long long>(10)); for (int i = 0; i < 10; ++i) for (int j = 0; j < 10; ++j) cin >> cost[i][j]; int wall[h][w]; for (int i = 0; i < h; ++i) for (int j = 0; j < w; ++j) cin >> wall[i][j]; floyd_wardhall(cost, 10); ll ans = 0; for (int i = 0; i < h; ++i) for (int j = 0; j < w; ++j) { if (wall[i][j] == -1 or wall[i][j] == 1) continue; ans += cost[wall[i][j]][1]; } cout << ans << endl; }*/ int main() { cin.tie(0); ios::sync_with_stdio(false); int N, M, L; cin >> N >> M >> L; vector<ll> a(N), b(N), c(N); vector<vector<ll>> fuel(N + 1, vector<ll>(N + 1, inf)); vector<vector<ll>> dist(N + 1, vector<ll>(N + 1, inf)); for (int i = 0; i < N + 1; ++i) { fuel[i][i] = 0; dist[i][i] = 0; } repr(i, M, 0) { cin >> a[i] >> b[i] >> c[i]; fuel[a[i]][b[i]] = c[i]; fuel[b[i]][a[i]] = c[i]; } warshall_floyd(fuel, N + 1); for (int i = 0; i < N + 1; ++i) { for (int j = 0; j < N + 1; ++j) { if (fuel[i][j] <= L) { dist[i][j] = 1; } } } warshall_floyd(dist, N + 1); int Q; cin >> Q; repr(i, Q, 0) { int s, t; cin >> s >> t; if (dist[s][t] < inf) { cout << dist[s][t] - 1 << endl; // 最初から持っている燃料 } else { cout << -1 << endl; } } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for (long long i = 0; i < n; i++) #define repr(i, n, m) for (long long i = m; i < n; i++) #define mod 1000000007 #define sorti(x) sort(x.begin(), x.end()) #define sortd(x) sort(x.begin(), x.end(), std::greater<long long>()) #define debug(x) std::cerr << (x) << std::endl; #define roll(x) \ for (auto &&itr : x) { \ cerr << (itr) << " "; \ } ll inf = 10000000000; template <class T> inline void chmax(T &ans, T t) { if (t > ans) ans = t; } template <class T> inline void chmin(T &ans, T t) { if (t < ans) ans = t; } template <class T> void warshall_floyd(vector<vector<T>> &cost, int N) { for (ll k = 0; k < N; ++k) { for (ll i = 0; i < N; ++i) { for (ll j = 0; j < N; ++j) { cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]); } } } } /* END */ /* int main() { int h,w; cin >> h >> w; vector< vector<long long> > cost(10, vector<long long>(10)); for (int i = 0; i < 10; ++i) for (int j = 0; j < 10; ++j) cin >> cost[i][j]; int wall[h][w]; for (int i = 0; i < h; ++i) for (int j = 0; j < w; ++j) cin >> wall[i][j]; floyd_wardhall(cost, 10); ll ans = 0; for (int i = 0; i < h; ++i) for (int j = 0; j < w; ++j) { if (wall[i][j] == -1 or wall[i][j] == 1) continue; ans += cost[wall[i][j]][1]; } cout << ans << endl; }*/ int main() { cin.tie(0); ios::sync_with_stdio(false); int N, M, L; cin >> N >> M >> L; vector<ll> a(M), b(M), c(M); vector<vector<ll>> fuel(N + 1, vector<ll>(N + 1, inf)); vector<vector<ll>> dist(N + 1, vector<ll>(N + 1, inf)); for (int i = 0; i < N + 1; ++i) { fuel[i][i] = 0; dist[i][i] = 0; } repr(i, M, 0) { cin >> a[i] >> b[i] >> c[i]; fuel[a[i]][b[i]] = c[i]; fuel[b[i]][a[i]] = c[i]; } warshall_floyd(fuel, N + 1); for (int i = 0; i < N + 1; ++i) { for (int j = 0; j < N + 1; ++j) { if (fuel[i][j] <= L) { dist[i][j] = 1; } } } warshall_floyd(dist, N + 1); int Q; cin >> Q; repr(i, Q, 0) { int s, t; cin >> s >> t; if (dist[s][t] < inf) { cout << dist[s][t] - 1 << endl; // 最初から持っている燃料 } else { cout << -1 << endl; } } }
replace
71
72
71
72
0
p02889
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <cstring> #include <iostream> #include <numeric> #include <queue> #include <string> #include <utility> #include <vector> using namespace std; int N, L; int ans[300][300]; void func(vector<vector<pair<int, int>>> &E, int start) { vector<vector<int>> maxF(N, vector<int>(N, -1)); vector<int> minCnt(N, -1); priority_queue<pair<pair<int, int>, int>> pq; maxF[start][0] = L; pq.push(make_pair(make_pair(0, L), start)); while (!pq.empty()) { auto temp = pq.top(); pq.pop(); int tempF = temp.first.second; int node = temp.second; int tempCnt = -temp.first.first; if (minCnt[node] != -1) { continue; } minCnt[node] = tempCnt; if (tempCnt == N - 1) { continue; } for (auto e : E[node]) { int nextNode = e.first; int cost = e.second; if (cost > L) { continue; } if (cost > tempF) { if (minCnt[nextNode] == -1 && maxF[nextNode][tempCnt + 1] < L) { maxF[nextNode][tempCnt + 1] = L - cost; pq.push(make_pair(make_pair(-(tempCnt + 1), L - cost), nextNode)); } } else { int nextF = tempF - cost; if (minCnt[nextNode] == -1 && maxF[nextNode][tempCnt] < nextF) { maxF[nextNode][tempCnt] = nextF; pq.push(make_pair(make_pair(-tempCnt, nextF), nextNode)); } } } } for (int i = 0; i < N; i++) { ans[start][i] = minCnt[i]; } } int main() { int M; cin >> N >> M >> L; vector<vector<pair<int, int>>> E(N); int a, b, c; for (int i = 0; i < M; i++) { cin >> a >> b >> c; E[a - 1].emplace_back(make_pair(b - 1, c)); E[b - 1].emplace_back(make_pair(a - 1, c)); } for (int i = 0; i < N; i++) { func(E, i); } int Q; cin >> Q; vector<pair<int, int>> st(Q); int s, t; for (int i = 0; i < Q; i++) { cin >> s >> t; st[i] = make_pair(s, t); } for (int i = 0; i < Q; i++) { auto temp = st[i]; cout << ans[temp.first - 1][temp.second - 1] << endl; } return 0; }
#include <algorithm> #include <cmath> #include <cstring> #include <iostream> #include <numeric> #include <queue> #include <string> #include <utility> #include <vector> using namespace std; int N, L; int ans[300][300]; void func(vector<vector<pair<int, int>>> &E, int start) { vector<vector<int>> maxF(N, vector<int>(N, -1)); vector<int> minCnt(N, -1); priority_queue<pair<pair<int, int>, int>> pq; maxF[start][0] = L; pq.push(make_pair(make_pair(0, L), start)); while (!pq.empty()) { auto temp = pq.top(); pq.pop(); int tempF = temp.first.second; int node = temp.second; int tempCnt = -temp.first.first; if (minCnt[node] != -1) { continue; } minCnt[node] = tempCnt; if (tempCnt == N - 1) { continue; } for (auto e : E[node]) { int nextNode = e.first; int cost = e.second; if (cost > L) { continue; } if (cost > tempF) { if (minCnt[nextNode] == -1 && maxF[nextNode][tempCnt + 1] < L - cost) { maxF[nextNode][tempCnt + 1] = L - cost; pq.push(make_pair(make_pair(-(tempCnt + 1), L - cost), nextNode)); } } else { int nextF = tempF - cost; if (minCnt[nextNode] == -1 && maxF[nextNode][tempCnt] < nextF) { maxF[nextNode][tempCnt] = nextF; pq.push(make_pair(make_pair(-tempCnt, nextF), nextNode)); } } } } for (int i = 0; i < N; i++) { ans[start][i] = minCnt[i]; } } int main() { int M; cin >> N >> M >> L; vector<vector<pair<int, int>>> E(N); int a, b, c; for (int i = 0; i < M; i++) { cin >> a >> b >> c; E[a - 1].emplace_back(make_pair(b - 1, c)); E[b - 1].emplace_back(make_pair(a - 1, c)); } for (int i = 0; i < N; i++) { func(E, i); } int Q; cin >> Q; vector<pair<int, int>> st(Q); int s, t; for (int i = 0; i < Q; i++) { cin >> s >> t; st[i] = make_pair(s, t); } for (int i = 0; i < Q; i++) { auto temp = st[i]; cout << ans[temp.first - 1][temp.second - 1] << endl; } return 0; }
replace
48
49
48
49
TLE
p02889
C++
Time Limit Exceeded
#include <bits/stdc++.h> int ri() { int n; scanf("%d", &n); return n; } int main() { int n = ri(); int m = ri(); int l = ri(); std::vector<std::pair<int, int>> hen[n]; for (int i = 0; i < m; i++) { int a = ri() - 1, b = ri() - 1, c = ri(); hen[a].push_back({b, c}); hen[b].push_back({a, c}); } int res[n][n]; for (int i = 0; i < n; i++) { std::vector<std::pair<int, int>> dist(n, {1000000001, 0}); dist[i] = {0, 0}; using T = std::pair<std::pair<int, int>, int>; std::priority_queue<T, std::vector<T>, std::greater<T>> que; que.push({{0, 0}, i}); while (que.size()) { auto cur = que.top(); que.pop(); int i = cur.second; int cnt = cur.first.first; int left = l - cur.first.second; for (auto j : hen[i]) { if (j.second > l) continue; std::pair<int, int> new_cost; if (j.second > left) { new_cost = {cnt + 1, j.second}; } else new_cost = {cnt, cur.first.second + j.second}; if (new_cost < dist[j.first]) dist[j.first] = new_cost, que.push({new_cost, j.first}); } } for (int j = 0; j < n; j++) res[i][j] = dist[j].first == 1000000001 ? -1 : dist[j].first; } int q = ri(); for (int i = 0; i < q; i++) { int a = ri() - 1, b = ri() - 1; std::cout << res[a][b] << std::endl; } return 0; }
#include <bits/stdc++.h> int ri() { int n; scanf("%d", &n); return n; } int main() { int n = ri(); int m = ri(); int l = ri(); std::vector<std::pair<int, int>> hen[n]; for (int i = 0; i < m; i++) { int a = ri() - 1, b = ri() - 1, c = ri(); hen[a].push_back({b, c}); hen[b].push_back({a, c}); } int res[n][n]; for (int i = 0; i < n; i++) { std::vector<std::pair<int, int>> dist(n, {1000000001, 0}); dist[i] = {0, 0}; using T = std::pair<std::pair<int, int>, int>; std::priority_queue<T, std::vector<T>, std::greater<T>> que; que.push({{0, 0}, i}); while (que.size()) { auto cur = que.top(); que.pop(); if (cur.first != dist[cur.second]) continue; int i = cur.second; int cnt = cur.first.first; int left = l - cur.first.second; for (auto j : hen[i]) { if (j.second > l) continue; std::pair<int, int> new_cost; if (j.second > left) { new_cost = {cnt + 1, j.second}; } else new_cost = {cnt, cur.first.second + j.second}; if (new_cost < dist[j.first]) dist[j.first] = new_cost, que.push({new_cost, j.first}); } } for (int j = 0; j < n; j++) res[i][j] = dist[j].first == 1000000001 ? -1 : dist[j].first; } int q = ri(); for (int i = 0; i < q; i++) { int a = ri() - 1, b = ri() - 1; std::cout << res[a][b] << std::endl; } return 0; }
insert
28
28
28
30
TLE
p02889
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long using namespace std; const ll INF = 1e11; ll n, m, l, q; ll graph[311][311]; ll graph2[311][311]; ll st[300 * 300 / 2][2]; ll abc[300 * 300 / 2][3]; void input() { cin >> n >> m >> l; for (int i = 0; i < m; i++) { cin >> abc[i][0] >> abc[i][1] >> abc[i][2]; } cin >> q; for (int i = 0; i < q; i++) { cin >> st[i][0] >> st[i][1]; } } int main() { input(); for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) { graph[i][j] = INF; graph2[i][j] = INF; } graph[i][i] = 0; graph2[i][i] = 0; } for (int i = 0; i < m; i++) { graph[abc[i][0]][abc[i][1]] = abc[i][2]; graph[abc[i][1]][abc[i][0]] = abc[i][2]; } for (int k = 0; k <= n; k++) { for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) { if (graph[i][j] > graph[i][k] + graph[k][j]) { graph[i][j] = graph[i][k] + graph[k][j]; } } } } for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) { if (graph[i][j] <= l) { graph2[i][j] = 1; } } } for (int k = 0; k <= n; k++) { for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) { if (graph2[i][j] > graph2[i][k] + graph2[k][j]) { graph2[i][j] = graph2[i][k] + graph2[k][j]; } } } } for (int i = 0; i < q; i++) { ll result = graph2[st[i][0]][st[i][1]] == (INF) ? -1 : graph2[st[i][0]][st[i][1]] - 1; cout << result << endl; } return 0; }
#include <bits/stdc++.h> #define ll long long using namespace std; const ll INF = 1e11; ll n, m, l, q; ll graph[311][311]; ll graph2[311][311]; ll st[300 * 300 + 10][2]; ll abc[300 * 300 + 10][3]; void input() { cin >> n >> m >> l; for (int i = 0; i < m; i++) { cin >> abc[i][0] >> abc[i][1] >> abc[i][2]; } cin >> q; for (int i = 0; i < q; i++) { cin >> st[i][0] >> st[i][1]; } } int main() { input(); for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) { graph[i][j] = INF; graph2[i][j] = INF; } graph[i][i] = 0; graph2[i][i] = 0; } for (int i = 0; i < m; i++) { graph[abc[i][0]][abc[i][1]] = abc[i][2]; graph[abc[i][1]][abc[i][0]] = abc[i][2]; } for (int k = 0; k <= n; k++) { for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) { if (graph[i][j] > graph[i][k] + graph[k][j]) { graph[i][j] = graph[i][k] + graph[k][j]; } } } } for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) { if (graph[i][j] <= l) { graph2[i][j] = 1; } } } for (int k = 0; k <= n; k++) { for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) { if (graph2[i][j] > graph2[i][k] + graph2[k][j]) { graph2[i][j] = graph2[i][k] + graph2[k][j]; } } } } for (int i = 0; i < q; i++) { ll result = graph2[st[i][0]][st[i][1]] == (INF) ? -1 : graph2[st[i][0]][st[i][1]] - 1; cout << result << endl; } return 0; }
replace
9
11
9
11
0
p02889
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> vi; typedef pair<int, int> Pii; #define pb push_back #define mp make_pair #define MOD 1000000007 #define INF 1e17 #define rep(i, a, b) for (int i = (a); i < (b); i++) #define fs first #define ss second inline int Scan() { int x = 0; int f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } template <typename T, typename U> static inline void amin(T &x, U y) { if (y < x) x = y; } // call by reference is used in x template <typename T, typename U> static inline void amax(T &x, U y) { if (x < y) x = y; } //////////////////////////////////////end of ///template////////////////////////////////////// ll a[100000], b[100000], c[100000]; ll mat[305][305]; ll dist[305][305]; ll s[105000], t[105000]; int main() { ios::sync_with_stdio(false); cin.tie(NULL); ll n, m, l; cin >> n >> m >> l; for (int i = 1; i <= m; i++) cin >> a[i] >> b[i] >> c[i]; ll q; cin >> q; for (int i = 1; i <= q; i++) cin >> s[i] >> t[i]; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { mat[i][j] = INF; dist[i][j] = INF; } mat[i][i] = 0; dist[i][i] = 0; } for (int i = 1; i <= m; i++) { for (int j = 1; j <= m; j++) { amin(mat[a[i]][b[i]], c[i]); amin(mat[b[i]][a[i]], c[i]); } } // cout <<"INPUT"<<endl; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { for (int k = 1; k <= n; k++) { amin(mat[j][k], mat[j][i] + mat[i][k]); } } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (i == j) continue; if (mat[i][j] <= l) amin(dist[i][j], 1ll); } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { for (int k = 1; k <= n; k++) { amin(dist[j][k], dist[j][i] + dist[i][k]); } } } for (int i = 1; i <= q; i++) { if (dist[s[i]][t[i]] < INF) cout << dist[s[i]][t[i]] - 1 << endl; else cout << -1 << endl; } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> vi; typedef pair<int, int> Pii; #define pb push_back #define mp make_pair #define MOD 1000000007 #define INF 1e17 #define rep(i, a, b) for (int i = (a); i < (b); i++) #define fs first #define ss second inline int Scan() { int x = 0; int f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } template <typename T, typename U> static inline void amin(T &x, U y) { if (y < x) x = y; } // call by reference is used in x template <typename T, typename U> static inline void amax(T &x, U y) { if (x < y) x = y; } //////////////////////////////////////end of ///template////////////////////////////////////// ll a[100000], b[100000], c[100000]; ll mat[305][305]; ll dist[305][305]; ll s[105000], t[105000]; int main() { ios::sync_with_stdio(false); cin.tie(NULL); ll n, m, l; cin >> n >> m >> l; for (int i = 1; i <= m; i++) cin >> a[i] >> b[i] >> c[i]; ll q; cin >> q; for (int i = 1; i <= q; i++) cin >> s[i] >> t[i]; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { mat[i][j] = INF; dist[i][j] = INF; } mat[i][i] = 0; dist[i][i] = 0; } for (int i = 1; i <= m; i++) { for (int j = 1; j <= m; j++) { mat[a[i]][b[i]] = c[i]; mat[b[i]][a[i]] = c[i]; } } // cout <<"INPUT"<<endl; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { for (int k = 1; k <= n; k++) { amin(mat[j][k], mat[j][i] + mat[i][k]); } } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (i == j) continue; if (mat[i][j] <= l) amin(dist[i][j], 1ll); } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { for (int k = 1; k <= n; k++) { amin(dist[j][k], dist[j][i] + dist[i][k]); } } } for (int i = 1; i <= q; i++) { if (dist[s[i]][t[i]] < INF) cout << dist[s[i]][t[i]] - 1 << endl; else cout << -1 << endl; } }
replace
76
78
76
78
TLE
p02889
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <fstream> #include <iomanip> #include <iostream> #include <limits> #include <map> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; typedef long long ll; #define REP(i, n) for (int i = 0; i < n; i++) template <class T> using reversed_priority_queue = priority_queue<T, vector<T>, greater<T>>; struct Edge { ll to; ll cost; Edge(ll t, ll c) { to = t; cost = c; } }; struct Node { int cost; int gas; int from; Node(int c, int g, int f) { cost = c; gas = g; from = f; } bool operator<(const Node &a) const { return cost == a.cost ? gas < a.gas : cost > a.cost; } }; ll N, M, L; ll INF = 1e18; ll dist[1010][1010]; ll lg[1010][1010]; int s[100010]; int t[100010]; vector<Edge> G[310]; void dijkstra(int start) { // cost, left, idx priority_queue<Node> que; que.emplace(Node(0, L, start)); while (!que.empty()) { ll cost = que.top().cost; ll gas = que.top().gas; ll from = que.top().from; que.pop(); if (dist[start][from] > cost) { dist[start][from] = cost; } else if (lg[start][from] < gas && dist[start][from] == cost) { lg[start][from] = gas; } else { continue; } REP(i, G[from].size()) { ll to = G[from][i].to; ll ct = G[from][i].cost; if (gas >= ct) { ll ng = gas - ct; ll nc = cost; ll ni = to; if (dist[start][ni] < nc) continue; if (dist[start][ni] == nc && lg[start][ni] >= ng) continue; que.emplace(Node(nc, ng, ni)); } else { ll ng = L - ct; ll nc = cost + 1; ll ni = to; if (dist[start][ni] < nc) continue; if (dist[start][ni] == nc && lg[start][ni] >= ng) continue; que.emplace(Node(nc, ng, ni)); } } } } int main() { cin >> N >> M >> L; REP(i, 1010) REP(j, 1010) { dist[i][j] = INF; lg[i][j] = 0; } REP(i, M) { int A, B, C; scanf("%d%d%d", &A, &B, &C); if (C > L) continue; G[A].push_back(Edge(B, C)); G[B].push_back(Edge(A, C)); } REP(i, N) { dijkstra(i + 1); } int Q; cin >> Q; REP(i, Q) { scanf("%d%d", s + i, t + i); } REP(i, Q) { ll dt = dist[s[i]][t[i]]; if (dt == INF) { cout << -1 << endl; } else { cout << dt << endl; } } return 0; }
#include <algorithm> #include <bitset> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <fstream> #include <iomanip> #include <iostream> #include <limits> #include <map> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; typedef long long ll; #define REP(i, n) for (int i = 0; i < n; i++) template <class T> using reversed_priority_queue = priority_queue<T, vector<T>, greater<T>>; struct Edge { ll to; ll cost; Edge(ll t, ll c) { to = t; cost = c; } }; struct Node { int cost; int gas; int from; Node(int c, int g, int f) { cost = c; gas = g; from = f; } bool operator<(const Node &a) const { return cost == a.cost ? gas < a.gas : cost > a.cost; } }; ll N, M, L; ll INF = 1e18; ll dist[1010][1010]; ll lg[1010][1010]; int s[100010]; int t[100010]; vector<Edge> G[310]; void dijkstra(int start) { // cost, left, idx priority_queue<Node> que; que.emplace(Node(0, L, start)); while (!que.empty()) { ll cost = que.top().cost; ll gas = que.top().gas; ll from = que.top().from; que.pop(); if (dist[start][from] > cost) { dist[start][from] = cost; lg[start][from] = gas; } else if (lg[start][from] < gas && dist[start][from] == cost) { lg[start][from] = gas; } else { continue; } REP(i, G[from].size()) { ll to = G[from][i].to; ll ct = G[from][i].cost; if (gas >= ct) { ll ng = gas - ct; ll nc = cost; ll ni = to; if (dist[start][ni] < nc) continue; if (dist[start][ni] == nc && lg[start][ni] >= ng) continue; que.emplace(Node(nc, ng, ni)); } else { ll ng = L - ct; ll nc = cost + 1; ll ni = to; if (dist[start][ni] < nc) continue; if (dist[start][ni] == nc && lg[start][ni] >= ng) continue; que.emplace(Node(nc, ng, ni)); } } } } int main() { cin >> N >> M >> L; REP(i, 1010) REP(j, 1010) { dist[i][j] = INF; lg[i][j] = 0; } REP(i, M) { int A, B, C; scanf("%d%d%d", &A, &B, &C); if (C > L) continue; G[A].push_back(Edge(B, C)); G[B].push_back(Edge(A, C)); } REP(i, N) { dijkstra(i + 1); } int Q; cin >> Q; REP(i, Q) { scanf("%d%d", s + i, t + i); } REP(i, Q) { ll dt = dist[s[i]][t[i]]; if (dt == INF) { cout << -1 << endl; } else { cout << dt << endl; } } return 0; }
insert
69
69
69
70
TLE
p02889
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long int; const int MAX = (int)(1e5 + 5); const ll INF = (ll)(1e13 + 5); using ql = pair<ll, ll>; const int MAX_N = (int)(3e2 + 5); int n, m; ll l; ll cost[MAX_N][MAX_N]; int q; ql jump[MAX_N][MAX_N]; int main(void) { // Here your code ! scanf("%d %d", &n, &m); scanf("%lld", &l); for (int i = 1; i <= n; ++i) { for (int j = i + 1; j <= n; ++j) { cost[i][j] = INF; cost[j][i] = INF; jump[i][j] = ql(INF, INF); jump[j][i] = ql(INF, INF); } cost[i][i] = 0L; jump[i][i] = ql(INF, INF); } for (int i = 0; i < m; ++i) { int a, b; ll c; scanf("%d %d %lld", &a, &b, &c); cost[a][b] = c; cost[b][a] = c; } for (int s = 1; s <= n; ++s) { priority_queue<pair<ql, int>, vector<pair<ql, int>>, greater<pair<ql, int>>> pq; pq.push(make_pair(ql(0L, 0L), s)); jump[s][s] = ql(0L, 0L); while (!pq.empty()) { auto cur = pq.top(); pq.pop(); ll times = cur.first.first; ll running = cur.first.second; int node = cur.second; // if (cur.first > jump[s][node]) continue; for (int i = 1; i <= n; ++i) { if (s == i) continue; if (cost[node][i] > l) continue; ll streched_running = running + cost[node][i]; ll next_times = (streched_running > l) ? times + 1 : times; ll next_running = (streched_running > l) ? cost[node][i] : streched_running; auto candidate = ql(next_times, next_running); if (jump[s][i] <= candidate) continue; jump[s][i] = candidate; pq.push(make_pair(candidate, i)); } } } scanf("%d", &q); for (int i = 0; i < q; ++i) { int s, t; scanf("%d %d", &s, &t); ll ans = (jump[s][t].first == INF) ? -1L : jump[s][t].first; printf("%lld\n", ans); } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long int; const int MAX = (int)(1e5 + 5); const ll INF = (ll)(1e13 + 5); using ql = pair<ll, ll>; const int MAX_N = (int)(3e2 + 5); int n, m; ll l; ll cost[MAX_N][MAX_N]; int q; ql jump[MAX_N][MAX_N]; int main(void) { // Here your code ! scanf("%d %d", &n, &m); scanf("%lld", &l); for (int i = 1; i <= n; ++i) { for (int j = i + 1; j <= n; ++j) { cost[i][j] = INF; cost[j][i] = INF; jump[i][j] = ql(INF, INF); jump[j][i] = ql(INF, INF); } cost[i][i] = 0L; jump[i][i] = ql(INF, INF); } for (int i = 0; i < m; ++i) { int a, b; ll c; scanf("%d %d %lld", &a, &b, &c); cost[a][b] = c; cost[b][a] = c; } for (int s = 1; s <= n; ++s) { priority_queue<pair<ql, int>, vector<pair<ql, int>>, greater<pair<ql, int>>> pq; pq.push(make_pair(ql(0L, 0L), s)); jump[s][s] = ql(0L, 0L); while (!pq.empty()) { auto cur = pq.top(); pq.pop(); ll times = cur.first.first; ll running = cur.first.second; int node = cur.second; if (cur.first > jump[s][node]) continue; for (int i = 1; i <= n; ++i) { if (s == i) continue; if (cost[node][i] > l) continue; ll streched_running = running + cost[node][i]; ll next_times = (streched_running > l) ? times + 1 : times; ll next_running = (streched_running > l) ? cost[node][i] : streched_running; auto candidate = ql(next_times, next_running); if (jump[s][i] <= candidate) continue; jump[s][i] = candidate; pq.push(make_pair(candidate, i)); } } } scanf("%d", &q); for (int i = 0; i < q; ++i) { int s, t; scanf("%d %d", &s, &t); ll ans = (jump[s][t].first == INF) ? -1L : jump[s][t].first; printf("%lld\n", ans); } return 0; }
replace
65
66
65
67
TLE
p02889
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define endl '\n' typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; 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; } const ll inf = 1e17; ll N, M, L, Q; ll fuel[305][305]; ll dist[305][305]; ll s[10500], t[10500]; ll A[100000], B[100000], C[100000]; void input() { cin >> N >> M >> L; for (int i = 1; i <= M; i++) cin >> A[i] >> B[i] >> C[i]; cin >> Q; for (int i = 1; i <= Q; i++) cin >> s[i] >> t[i]; } int main() { input(); for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) { fuel[i][j] = inf; dist[i][j] = inf; } fuel[i][i] = 0; dist[i][i] = 0; } for (int i = 1; i <= M; i++) { chmin(fuel[A[i]][B[i]], C[i]); chmin(fuel[B[i]][A[i]], C[i]); } for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) { for (int k = 1; k <= N; k++) { chmin(fuel[j][k], fuel[j][i] + fuel[i][k]); } } } for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) { if (i == j) continue; if (fuel[i][j] <= L) chmin(dist[i][j], 1ll); } } for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) { for (int k = 1; k <= N; k++) { chmin(dist[j][k], dist[j][i] + dist[i][k]); } } } for (int i = 1; i <= Q; i++) { if (dist[t[i]][s[i]] < inf) cout << dist[t[i]][s[i]] - 1 << endl; else cout << -1 << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define endl '\n' typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; 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; } const ll inf = 1e17; ll N, M, L, Q; ll fuel[305][305]; ll dist[305][305]; ll s[105000], t[105000]; ll A[100000], B[100000], C[100000]; void input() { cin >> N >> M >> L; for (int i = 1; i <= M; i++) cin >> A[i] >> B[i] >> C[i]; cin >> Q; for (int i = 1; i <= Q; i++) cin >> s[i] >> t[i]; } int main() { input(); for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) { fuel[i][j] = inf; dist[i][j] = inf; } fuel[i][i] = 0; dist[i][i] = 0; } for (int i = 1; i <= M; i++) { chmin(fuel[A[i]][B[i]], C[i]); chmin(fuel[B[i]][A[i]], C[i]); } for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) { for (int k = 1; k <= N; k++) { chmin(fuel[j][k], fuel[j][i] + fuel[i][k]); } } } for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) { if (i == j) continue; if (fuel[i][j] <= L) chmin(dist[i][j], 1ll); } } for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) { for (int k = 1; k <= N; k++) { chmin(dist[j][k], dist[j][i] + dist[i][k]); } } } for (int i = 1; i <= Q; i++) { if (dist[t[i]][s[i]] < inf) cout << dist[t[i]][s[i]] - 1 << endl; else cout << -1 << endl; } return 0; }
replace
25
26
25
26
0
p02889
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(x) (x).begin(), (x).end() #define pb push_back #define lb lower_bound #define ub upper_bound typedef long long ll; const ll INF = 100000000000ll; const long INF64 = 1000000000000000ll; const ll MOD = 1000000007ll; int main() { ll n, m, l; std::cin >> n >> m >> l; vector<ll> a(m), b(m), c(m); rep(i, m) std::cin >> a[i] >> b[i] >> c[i]; ll q; std::cin >> q; vector<ll> s(q), t(q); rep(i, q) std::cin >> s[i] >> t[i]; std::vector<std::vector<ll>> d(n, vector<ll>(n, INF)); std::vector<std::vector<ll>> dd(n, vector<ll>(n, INF)); rep(i, n) d[i][i] = 0ll; rep(i, m) { if (c[i] <= l) { d[a[i] - 1][b[i] - 1] = 1ll; d[b[i] - 1][a[i] - 1] = 1ll; } } rep(i, n) dd[i][i] = 0ll; rep(i, m) { if (c[i] <= l) { dd[a[i] - 1][b[i] - 1] = c[i]; dd[b[i] - 1][a[i] - 1] = c[i]; } } for (int i = 0; i < n; i++) // 経由す頂点 for (int j = 0; j < n; j++) // 開始頂点 for (int k = 0; k < n; k++) // 終端 dd[j][k] = min(dd[j][k], dd[j][i] + dd[i][k]); rep(ii, 300) { for (int i = 0; i < n; i++) // 経由す頂点 for (int j = 0; j < n; j++) // 開始頂点 for (int k = 0; k < n; k++) // 終端 d[j][k] = min(d[j][k], d[j][i] + d[i][k]); rep(i, n) rep(j, n) { if (d[i][j] < INF && d[i][j] >= 1 && dd[i][j] <= l) d[i][j] = 1; } } rep(i, q) { if (d[s[i] - 1][t[i] - 1] == INF) std::cout << -1 << std::endl; else std::cout << max(d[s[i] - 1][t[i] - 1] - 1, 0ll) << std::endl; } }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(x) (x).begin(), (x).end() #define pb push_back #define lb lower_bound #define ub upper_bound typedef long long ll; const ll INF = 100000000000ll; const long INF64 = 1000000000000000ll; const ll MOD = 1000000007ll; int main() { ll n, m, l; std::cin >> n >> m >> l; vector<ll> a(m), b(m), c(m); rep(i, m) std::cin >> a[i] >> b[i] >> c[i]; ll q; std::cin >> q; vector<ll> s(q), t(q); rep(i, q) std::cin >> s[i] >> t[i]; std::vector<std::vector<ll>> d(n, vector<ll>(n, INF)); std::vector<std::vector<ll>> dd(n, vector<ll>(n, INF)); rep(i, n) d[i][i] = 0ll; rep(i, m) { if (c[i] <= l) { d[a[i] - 1][b[i] - 1] = 1ll; d[b[i] - 1][a[i] - 1] = 1ll; } } rep(i, n) dd[i][i] = 0ll; rep(i, m) { if (c[i] <= l) { dd[a[i] - 1][b[i] - 1] = c[i]; dd[b[i] - 1][a[i] - 1] = c[i]; } } for (int i = 0; i < n; i++) // 経由す頂点 for (int j = 0; j < n; j++) // 開始頂点 for (int k = 0; k < n; k++) // 終端 dd[j][k] = min(dd[j][k], dd[j][i] + dd[i][k]); rep(ii, 2) { for (int i = 0; i < n; i++) // 経由す頂点 for (int j = 0; j < n; j++) // 開始頂点 for (int k = 0; k < n; k++) // 終端 d[j][k] = min(d[j][k], d[j][i] + d[i][k]); rep(i, n) rep(j, n) { if (d[i][j] < INF && d[i][j] >= 1 && dd[i][j] <= l) d[i][j] = 1; } } rep(i, q) { if (d[s[i] - 1][t[i] - 1] == INF) std::cout << -1 << std::endl; else std::cout << max(d[s[i] - 1][t[i] - 1] - 1, 0ll) << std::endl; } }
replace
43
44
43
44
TLE
p02889
C++
Runtime Error
#include <bits/stdc++.h> // #include <boost/multiprecision/cpp_int.hpp> // #define int long long #define inf 1000000007 #define pa pair<int, int> #define ll long long #define pal pair<double, double> #define ppap pair<pa, int> #define PI 3.14159265358979323846 #define paa pair<int, char> #define mp make_pair #define pb push_back #define EPS (1e-8) int dx[8] = {0, 1, 0, -1, 1, 1, -1, -1}; int dy[8] = {1, 0, -1, 0, -1, 1, 1, -1}; using namespace std; class pa3 { public: int x; int y, z; pa3(int x = 0, int y = 0, int z = 0) : x(x), y(y), z(z) {} bool operator<(const pa3 &p) const { if (x != p.x) return x < p.x; if (y != p.y) return y < p.y; return z < p.z; // return x != p.x ? x<p.x: y<p.y; } bool operator>(const pa3 &p) const { if (x != p.x) return x > p.x; if (y != p.y) return y > p.y; return z > p.z; // return x != p.x ? x<p.x: y<p.y; } bool operator==(const pa3 &p) const { return x == p.x && y == p.y && z == p.z; } bool operator!=(const pa3 &p) const { return !(x == p.x && y == p.y && z == p.z); } }; class pa4 { public: int x; int y, z, w; pa4(int x = 0, int y = 0, int z = 0, int w = 0) : x(x), y(y), z(z), w(w) {} bool operator<(const pa4 &p) const { if (x != p.x) return x < p.x; if (y != p.y) return y < p.y; if (z != p.z) return z < p.z; return w < p.w; // return x != p.x ? x<p.x: y<p.y; } bool operator>(const pa4 &p) const { if (x != p.x) return x > p.x; if (y != p.y) return y > p.y; if (z != p.z) return z > p.z; return w > p.w; // return x != p.x ? x<p.x: y<p.y; } bool operator==(const pa4 &p) const { return x == p.x && y == p.y && z == p.z && w == p.w; } }; class pa2 { public: int x, y; pa2(int x = 0, int y = 0) : x(x), y(y) {} pa2 operator+(pa2 p) { return pa2(x + p.x, y + p.y); } pa2 operator-(pa2 p) { return pa2(x - p.x, y - p.y); } bool operator<(const pa2 &p) const { return y != p.y ? y < p.y : x < p.x; } bool operator>(const pa2 &p) const { return x != p.x ? x < p.x : y < p.y; } bool operator==(const pa2 &p) const { return abs(x - p.x) == 0 && abs(y - p.y) == 0; } bool operator!=(const pa2 &p) const { return !(abs(x - p.x) == 0 && abs(y - p.y) == 0); } }; string itos(int i) { ostringstream s; s << i; return s.str(); } int gcd(int v, int b) { if (v > b) return gcd(b, v); if (v == b) return b; if (b % v == 0) return v; return gcd(v, b % v); } int mod; int extgcd(int a, int b, int &x, int &y) { if (b == 0) { x = 1; y = 0; return a; } int d = extgcd(b, a % b, y, x); y -= a / b * x; return d; } pa operator+(const pa &l, const pa &r) { return {l.first + r.first, l.second + r.second}; } pa operator-(const pa &l, const pa &r) { return {l.first - r.first, l.second - r.second}; } int pr[1000010]; int inv[1000010]; int beki(int wa, int rr, int warukazu) { if (rr == 0) return 1 % warukazu; if (rr == 1) return wa % warukazu; wa %= warukazu; if (rr % 2 == 1) return ((ll)beki(wa, rr - 1, warukazu) * (ll)wa) % warukazu; ll zx = beki(wa, rr / 2, warukazu); return (zx * zx) % warukazu; } int comb(int nn, int rr) { if (rr < 0 || rr > nn || nn < 0) return 0; int r = pr[nn] * inv[rr]; r %= mod; r *= inv[nn - rr]; r %= mod; return r; } void gya(int ert) { pr[0] = 1; for (int i = 1; i <= ert; i++) { pr[i] = (pr[i - 1] * i) % mod; } inv[ert] = beki(pr[ert], mod - 2, mod); for (int i = ert - 1; i >= 0; i--) { inv[i] = inv[i + 1] * (i + 1) % mod; } } // cin.tie(0); // ios::sync_with_stdio(false); // priority_queue<pa3,vector<pa3>,greater<pa3>> pq; // sort(ve.begin(),ve.end(),greater<int>()); // mt19937(clock_per_sec); // mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()) ; //----------------kokomade tenpure------------ vector<pa> G[320]; int ans[320][320]; int n, m, l; int solve(int r) { for (int i = 1; i <= n; i++) ans[r][i] = -1; priority_queue<pa3, vector<pa3>, greater<pa3>> pq; pq.push((pa3){0, -l, r}); while (pq.size()) { pa3 z = pq.top(); pq.pop(); if (ans[r][z.z] >= 0) continue; // cout<<z.x<<" "<<-z.y<<" "<<" "<<r<<" "<<z.z<<endl; ans[r][z.z] = z.x; for (auto v : G[z.z]) { // cout<<ans[r][v.s]<<" "<<v.second<<endl; if (ans[r][v.first] >= 0) continue; // cout<<v.second<<endl; int nokori = -z.y; if (nokori >= v.second) pq.push((pa3){z.x, -(nokori - v.second), v.first}); else pq.push((pa3){z.x + 1, -(l - v.second), v.first}); } } } signed main() { cin.tie(0); ios::sync_with_stdio(false); cin >> n >> m >> l; for (int i = 0; i < m; i++) { int y, yy, yyy; cin >> y >> yy >> yyy; if (yyy > l) continue; G[y].pb(mp(yy, yyy)); G[yy].pb(mp(y, yyy)); } // cout<<G[3][0].first<<endl; for (int i = 1; i <= n; i++) solve(i); int q; cin >> q; for (int i = 0; i < q; i++) { int a, b; cin >> a >> b; cout << ans[a][b] << endl; } return 0; }
#include <bits/stdc++.h> // #include <boost/multiprecision/cpp_int.hpp> // #define int long long #define inf 1000000007 #define pa pair<int, int> #define ll long long #define pal pair<double, double> #define ppap pair<pa, int> #define PI 3.14159265358979323846 #define paa pair<int, char> #define mp make_pair #define pb push_back #define EPS (1e-8) int dx[8] = {0, 1, 0, -1, 1, 1, -1, -1}; int dy[8] = {1, 0, -1, 0, -1, 1, 1, -1}; using namespace std; class pa3 { public: int x; int y, z; pa3(int x = 0, int y = 0, int z = 0) : x(x), y(y), z(z) {} bool operator<(const pa3 &p) const { if (x != p.x) return x < p.x; if (y != p.y) return y < p.y; return z < p.z; // return x != p.x ? x<p.x: y<p.y; } bool operator>(const pa3 &p) const { if (x != p.x) return x > p.x; if (y != p.y) return y > p.y; return z > p.z; // return x != p.x ? x<p.x: y<p.y; } bool operator==(const pa3 &p) const { return x == p.x && y == p.y && z == p.z; } bool operator!=(const pa3 &p) const { return !(x == p.x && y == p.y && z == p.z); } }; class pa4 { public: int x; int y, z, w; pa4(int x = 0, int y = 0, int z = 0, int w = 0) : x(x), y(y), z(z), w(w) {} bool operator<(const pa4 &p) const { if (x != p.x) return x < p.x; if (y != p.y) return y < p.y; if (z != p.z) return z < p.z; return w < p.w; // return x != p.x ? x<p.x: y<p.y; } bool operator>(const pa4 &p) const { if (x != p.x) return x > p.x; if (y != p.y) return y > p.y; if (z != p.z) return z > p.z; return w > p.w; // return x != p.x ? x<p.x: y<p.y; } bool operator==(const pa4 &p) const { return x == p.x && y == p.y && z == p.z && w == p.w; } }; class pa2 { public: int x, y; pa2(int x = 0, int y = 0) : x(x), y(y) {} pa2 operator+(pa2 p) { return pa2(x + p.x, y + p.y); } pa2 operator-(pa2 p) { return pa2(x - p.x, y - p.y); } bool operator<(const pa2 &p) const { return y != p.y ? y < p.y : x < p.x; } bool operator>(const pa2 &p) const { return x != p.x ? x < p.x : y < p.y; } bool operator==(const pa2 &p) const { return abs(x - p.x) == 0 && abs(y - p.y) == 0; } bool operator!=(const pa2 &p) const { return !(abs(x - p.x) == 0 && abs(y - p.y) == 0); } }; string itos(int i) { ostringstream s; s << i; return s.str(); } int gcd(int v, int b) { if (v > b) return gcd(b, v); if (v == b) return b; if (b % v == 0) return v; return gcd(v, b % v); } int mod; int extgcd(int a, int b, int &x, int &y) { if (b == 0) { x = 1; y = 0; return a; } int d = extgcd(b, a % b, y, x); y -= a / b * x; return d; } pa operator+(const pa &l, const pa &r) { return {l.first + r.first, l.second + r.second}; } pa operator-(const pa &l, const pa &r) { return {l.first - r.first, l.second - r.second}; } int pr[1000010]; int inv[1000010]; int beki(int wa, int rr, int warukazu) { if (rr == 0) return 1 % warukazu; if (rr == 1) return wa % warukazu; wa %= warukazu; if (rr % 2 == 1) return ((ll)beki(wa, rr - 1, warukazu) * (ll)wa) % warukazu; ll zx = beki(wa, rr / 2, warukazu); return (zx * zx) % warukazu; } int comb(int nn, int rr) { if (rr < 0 || rr > nn || nn < 0) return 0; int r = pr[nn] * inv[rr]; r %= mod; r *= inv[nn - rr]; r %= mod; return r; } void gya(int ert) { pr[0] = 1; for (int i = 1; i <= ert; i++) { pr[i] = (pr[i - 1] * i) % mod; } inv[ert] = beki(pr[ert], mod - 2, mod); for (int i = ert - 1; i >= 0; i--) { inv[i] = inv[i + 1] * (i + 1) % mod; } } // cin.tie(0); // ios::sync_with_stdio(false); // priority_queue<pa3,vector<pa3>,greater<pa3>> pq; // sort(ve.begin(),ve.end(),greater<int>()); // mt19937(clock_per_sec); // mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()) ; //----------------kokomade tenpure------------ vector<pa> G[320]; int ans[320][320]; int n, m, l; int solve(int r) { for (int i = 1; i <= n; i++) ans[r][i] = -1; priority_queue<pa3, vector<pa3>, greater<pa3>> pq; pq.push((pa3){0, -l, r}); while (pq.size()) { pa3 z = pq.top(); pq.pop(); if (ans[r][z.z] >= 0) continue; // cout<<z.x<<" "<<-z.y<<" "<<" "<<r<<" "<<z.z<<endl; ans[r][z.z] = z.x; for (auto v : G[z.z]) { // cout<<ans[r][v.s]<<" "<<v.second<<endl; if (ans[r][v.first] >= 0) continue; // cout<<v.second<<endl; int nokori = -z.y; if (nokori >= v.second) pq.push((pa3){z.x, -(nokori - v.second), v.first}); else pq.push((pa3){z.x + 1, -(l - v.second), v.first}); } } return 0; } signed main() { cin.tie(0); ios::sync_with_stdio(false); cin >> n >> m >> l; for (int i = 0; i < m; i++) { int y, yy, yyy; cin >> y >> yy >> yyy; if (yyy > l) continue; G[y].pb(mp(yy, yyy)); G[yy].pb(mp(y, yyy)); } // cout<<G[3][0].first<<endl; for (int i = 1; i <= n; i++) solve(i); int q; cin >> q; for (int i = 0; i < q; i++) { int a, b; cin >> a >> b; cout << ans[a][b] << endl; } return 0; }
insert
204
204
204
206
0
p02889
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using Weight = int; using Vertex = int; struct Edge { Vertex from, to; Weight weight, charge; }; using Graph = vector<vector<Edge>>; int main() { int N, M, L; cin >> N >> M >> L; Graph G(N); for (auto i = 0; i < M; ++i) { int A, B, C; cin >> A >> B >> C; --A; --B; if (L < C) continue; G[A].push_back({A, B, C}); G[B].push_back({B, A, C}); } vector<vector<Weight>> memo(G.size(), vector<Weight>(G.size(), -1)); priority_queue<Edge, vector<Edge>, function<bool(Edge, Edge)>> q( [](Edge lhs, Edge rhs) { if (lhs.charge != rhs.charge) return lhs.charge > rhs.charge; return lhs.weight < rhs.weight; }); for (auto s = 0; s < N; ++s) { vector<vector<Weight>> d(G.size() + 1, vector<Weight>(G.size(), -1)); auto &m = memo[s]; q.push({s, s, L, 0}); while (!q.empty()) { auto cur = q.top(); q.pop(); if (m[cur.to] != -1) continue; m[cur.to] = cur.charge; for (const auto &nex : G[cur.to]) if (m[nex.to] == -1) { auto w = cur.weight - nex.weight; auto c = cur.charge; if (cur.weight < nex.weight) w = L - nex.weight, ++c; if (w <= d[c][nex.to]) continue; d[c][nex.to] = w; q.push({cur.to, nex.to, w, c}); } } } int Q; cin >> Q; for (auto i = 0; i < Q; ++i) { int s, t; cin >> s >> t; --s, --t; cout << memo[s][t] << endl; } }
#include <bits/stdc++.h> using namespace std; using Weight = int; using Vertex = int; struct Edge { Vertex from, to; Weight weight, charge; }; using Graph = vector<vector<Edge>>; int main() { int N, M, L; cin >> N >> M >> L; Graph G(N); for (auto i = 0; i < M; ++i) { int A, B, C; cin >> A >> B >> C; --A; --B; if (L < C) continue; G[A].push_back({A, B, C}); G[B].push_back({B, A, C}); } vector<vector<Weight>> memo(G.size(), vector<Weight>(G.size(), -1)); priority_queue<Edge, vector<Edge>, function<bool(const Edge &, const Edge &)>> q([](const Edge &lhs, const Edge &rhs) { if (lhs.charge != rhs.charge) return lhs.charge > rhs.charge; return lhs.weight < rhs.weight; }); for (auto s = 0; s < N; ++s) { vector<vector<Weight>> d(G.size() + 1, vector<Weight>(G.size(), -1)); auto &m = memo[s]; q.push({s, s, L, 0}); while (!q.empty()) { auto cur = q.top(); q.pop(); if (m[cur.to] != -1) continue; m[cur.to] = cur.charge; for (const auto &nex : G[cur.to]) if (m[nex.to] == -1) { auto w = cur.weight - nex.weight; auto c = cur.charge; if (cur.weight < nex.weight) w = L - nex.weight, ++c; if (w <= d[c][nex.to]) continue; d[c][nex.to] = w; q.push({cur.to, nex.to, w, c}); } } } int Q; cin >> Q; for (auto i = 0; i < Q; ++i) { int s, t; cin >> s >> t; --s, --t; cout << memo[s][t] << endl; } }
replace
28
30
28
30
TLE
p02889
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; /// Welcome to Nasif's Code #define bug printf("bug\n"); #define bug2(var) cout << #var << " " << var << endl; #define co(q) cout << q << endl; #define all(q) (q).begin(), (q).end() #define pi acos(-1) #define FastRead \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define MODADD(ADD_X, ADD_Y) (ADD_X + ADD_Y) % MOD; #define MODSUB(SUB_X, SUB_Y) (SUB_X-SUB_Y)+MOD)%MOD; #define MODMUL(MUL_X, MUL_Y) (MUL_X * MUL_Y) % MOD; #define LCM(LCM_X, LCM_Y) (LCM_X * LCM_Y) / __gcd(LCM_X, LCM_Y); typedef long long int ll; typedef unsigned long long int ull; const int MOD = (int)1e9 + 7; const int MAX = 1e6; const ll INF = 1e17; int dx[] = {1, 0, -1, 0, 1, -1, 1, -1}; int dy[] = {0, 1, 0, -1, 1, -1, -1, 1}; ll dist[500][500], gas[500][500]; ll a[500], b[500], c[500]; int main() { FastRead // freopen("output.txt", "w", stdout); ll n, m, l; cin >> n >> m >> l; for (int i = 1; i <= m; i++) cin >> a[i] >> b[i] >> c[i]; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) dist[i][j] = INF, gas[i][j] = INF; gas[i][i] = 0; dist[i][i] = 0; } for (int i = 1; i <= m; i++) { gas[a[i]][b[i]] = min(gas[a[i]][b[i]], c[i]); gas[b[i]][a[i]] = min(gas[b[i]][a[i]], c[i]); } for (int k = 1; k <= n; k++) for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) gas[i][j] = min(gas[i][j], gas[i][k] + gas[k][j]); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) { if (i == j) continue; if (gas[i][j] <= l) dist[i][j] = min(dist[i][j], 1ll); } for (int k = 1; k <= n; k++) for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); int q; cin >> q; while (q--) { int x, y; cin >> x >> y; if (dist[x][y] == INF) cout << "-1" << endl; else cout << dist[x][y] - 1 << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; /// Welcome to Nasif's Code #define bug printf("bug\n"); #define bug2(var) cout << #var << " " << var << endl; #define co(q) cout << q << endl; #define all(q) (q).begin(), (q).end() #define pi acos(-1) #define FastRead \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define MODADD(ADD_X, ADD_Y) (ADD_X + ADD_Y) % MOD; #define MODSUB(SUB_X, SUB_Y) (SUB_X-SUB_Y)+MOD)%MOD; #define MODMUL(MUL_X, MUL_Y) (MUL_X * MUL_Y) % MOD; #define LCM(LCM_X, LCM_Y) (LCM_X * LCM_Y) / __gcd(LCM_X, LCM_Y); typedef long long int ll; typedef unsigned long long int ull; const int MOD = (int)1e9 + 7; const int MAX = 1e6; const ll INF = 1e17; int dx[] = {1, 0, -1, 0, 1, -1, 1, -1}; int dy[] = {0, 1, 0, -1, 1, -1, -1, 1}; ll dist[500][500], gas[500][500]; ll a[MAX], b[MAX], c[MAX]; int main() { FastRead // freopen("output.txt", "w", stdout); ll n, m, l; cin >> n >> m >> l; for (int i = 1; i <= m; i++) cin >> a[i] >> b[i] >> c[i]; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) dist[i][j] = INF, gas[i][j] = INF; gas[i][i] = 0; dist[i][i] = 0; } for (int i = 1; i <= m; i++) { gas[a[i]][b[i]] = min(gas[a[i]][b[i]], c[i]); gas[b[i]][a[i]] = min(gas[b[i]][a[i]], c[i]); } for (int k = 1; k <= n; k++) for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) gas[i][j] = min(gas[i][j], gas[i][k] + gas[k][j]); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) { if (i == j) continue; if (gas[i][j] <= l) dist[i][j] = min(dist[i][j], 1ll); } for (int k = 1; k <= n; k++) for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); int q; cin >> q; while (q--) { int x, y; cin >> x >> y; if (dist[x][y] == INF) cout << "-1" << endl; else cout << dist[x][y] - 1 << endl; } return 0; }
replace
24
25
24
25
0
p02889
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll n, m, l; int main() { ios_base::sync_with_stdio(false); cin >> n >> m >> l; ll adj[n][n]; for (int i = 0; i <= n; i++) for (int j = 0; j <= n; j++) adj[i][j] = (i == j) ? 0 : 1e13; for (int i = 0; i < m; i++) { ll a, b, c; cin >> a >> b >> c; a--, b--; adj[a][b] = adj[b][a] = c; } for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { adj[i][j] = min(adj[i][j], adj[i][k] + adj[k][j]); } } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { adj[i][j] = adj[i][j] <= l ? 1 : 1e13; if (i == j) adj[i][j] = 0; } } for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { adj[i][j] = min(adj[i][j], adj[i][k] + adj[k][j]); } } } int q; cin >> q; while (q--) { int s, t; cin >> s >> t; s--, t--; ll ans = adj[s][t]; if (ans >= 1e13) cout << -1 << "\n"; else cout << (ans - 1) << "\n"; } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll n, m, l; int main() { ios_base::sync_with_stdio(false); cin >> n >> m >> l; ll adj[n][n]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) adj[i][j] = (i == j) ? 0 : 1e13; for (int i = 0; i < m; i++) { ll a, b, c; cin >> a >> b >> c; a--, b--; adj[a][b] = adj[b][a] = c; } for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { adj[i][j] = min(adj[i][j], adj[i][k] + adj[k][j]); } } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { adj[i][j] = adj[i][j] <= l ? 1 : 1e13; if (i == j) adj[i][j] = 0; } } for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { adj[i][j] = min(adj[i][j], adj[i][k] + adj[k][j]); } } } int q; cin >> q; while (q--) { int s, t; cin >> s >> t; s--, t--; ll ans = adj[s][t]; if (ans >= 1e13) cout << -1 << "\n"; else cout << (ans - 1) << "\n"; } }
replace
12
14
12
14
0
p02889
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define fi first #define se second #define all(v) v.begin(), v.end() #define eb emplace_back #define uqv(v) v.erase(unique(all(v)), v.end()); #define INF 1e9 #define LINF 1e18 #define gt(tp, x) get<x>(tp) using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef tuple<int, int, int> tp3; int ans[303][303]; bool chk[303][303]; vector<pii> g[303]; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m, l; cin >> n >> m >> l; for (int i = 1; i <= m; i++) { int u, v, w; cin >> u >> v >> w; if (w > l) continue; g[u].eb(v, w), g[v].eb(u, w); } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { ans[i][j] = -1; } } for (int i = 1; i <= n; i++) { priority_queue<tp3> pq; pq.push(make_tuple(0, l, i)); while (!pq.empty()) { int x = gt(pq.top(), 2), f = gt(pq.top(), 1), dg = -gt(pq.top(), 0); pq.pop(); if (chk[i][x]) continue; chk[i][x] = true; ans[i][x] = dg; for (auto j : g[x]) { if (j.se > f) pq.push(make_tuple(-dg - 1, l - j.se, j.fi)); else pq.push(make_tuple(-dg, f - j.se, j.fi)); } } } int q; cin >> q; while (q--) { int s, t; cin >> s >> t; cout << ans[s][t] << '\n'; } }
#include <bits/stdc++.h> #define fi first #define se second #define all(v) v.begin(), v.end() #define eb emplace_back #define uqv(v) v.erase(unique(all(v)), v.end()); #define INF 1e9 #define LINF 1e18 #define gt(tp, x) get<x>(tp) using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef tuple<int, int, int> tp3; int ans[303][303]; bool chk[303][303]; vector<pii> g[303]; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m, l; cin >> n >> m >> l; for (int i = 1; i <= m; i++) { int u, v, w; cin >> u >> v >> w; if (w > l) continue; g[u].eb(v, w), g[v].eb(u, w); } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { ans[i][j] = -1; } } for (int i = 1; i <= n; i++) { priority_queue<tp3> pq; pq.push(make_tuple(0, l, i)); while (!pq.empty()) { int x = gt(pq.top(), 2), f = gt(pq.top(), 1), dg = -gt(pq.top(), 0); pq.pop(); if (chk[i][x]) continue; chk[i][x] = true; ans[i][x] = dg; for (auto j : g[x]) { if (chk[i][j.fi]) continue; if (j.se > f) pq.push(make_tuple(-dg - 1, l - j.se, j.fi)); else pq.push(make_tuple(-dg, f - j.se, j.fi)); } } } int q; cin >> q; while (q--) { int s, t; cin >> s >> t; cout << ans[s][t] << '\n'; } }
insert
51
51
51
53
TLE
p02889
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define arep(i, x, n) for (int i = int(x); i < (int)(n); i++) #define rep(i, n) for (long long i = 0; i < n; ++i) #define rrep(i, n) for (int i = int(n - 1); i >= 0; i--) #define fs first #define sc second #define all(x) (x).begin(), (x).end() #define pi 3.141592653589793 #define eps 0.00000001 #define INF 1e9 + 7 using ll = long long; using P = pair<int, int>; using lP = pair<ll, ll>; using fP = pair<double, double>; using PPI = pair<P, int>; ll const mod = 998244353; // ll const mod=1e9+7; const ll MAX = 300000; using vi = vector<int>; using vl = vector<ll>; using vc = vector<char>; using vd = vector<double>; using vs = vector<string>; using vp = vector<P>; using vb = vector<bool>; using vvi = vector<vector<int>>; using vvd = vector<vector<double>>; using vvc = vector<vector<char>>; using vvp = vector<vector<P>>; using vvb = vector<vector<bool>>; 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; } ////////////////////////////////////// int main() { int n, m, l; cin >> n >> m >> l; vvi dp(n, vi(n, INF)); rep(i, n) dp[i][i] = 0; rep(i, m) { int a, b, c; a--, b--; dp[a][b] = c; dp[b][a] = c; } rep(k, n) rep(i, n) rep(j, n) chmin(dp[i][j], dp[i][k] + dp[k][j]); vvi dp2(n, vi(n, INF)); rep(i, n) rep(j, n) { if (dp[i][j] <= l) dp2[i][j] = 1; } rep(k, n) rep(i, n) rep(j, n) chmin(dp2[i][j], dp2[i][k] + dp2[k][j]); int q; cin >> q; rep(i, q) { int s, t; cin >> s >> t; --s; --t; int ans = dp2[s][t]; if (ans == INF) cout << -1 << endl; else cout << ans - 1 << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define arep(i, x, n) for (int i = int(x); i < (int)(n); i++) #define rep(i, n) for (long long i = 0; i < n; ++i) #define rrep(i, n) for (int i = int(n - 1); i >= 0; i--) #define fs first #define sc second #define all(x) (x).begin(), (x).end() #define pi 3.141592653589793 #define eps 0.00000001 #define INF 1e9 + 7 using ll = long long; using P = pair<int, int>; using lP = pair<ll, ll>; using fP = pair<double, double>; using PPI = pair<P, int>; ll const mod = 998244353; // ll const mod=1e9+7; const ll MAX = 300000; using vi = vector<int>; using vl = vector<ll>; using vc = vector<char>; using vd = vector<double>; using vs = vector<string>; using vp = vector<P>; using vb = vector<bool>; using vvi = vector<vector<int>>; using vvd = vector<vector<double>>; using vvc = vector<vector<char>>; using vvp = vector<vector<P>>; using vvb = vector<vector<bool>>; 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; } ////////////////////////////////////// int main() { int n, m, l; cin >> n >> m >> l; vvi dp(n, vi(n, INF)); rep(i, n) dp[i][i] = 0; rep(i, m) { int a, b, c; cin >> a >> b >> c; a--, b--; dp[a][b] = c; dp[b][a] = c; } rep(k, n) rep(i, n) rep(j, n) chmin(dp[i][j], dp[i][k] + dp[k][j]); vvi dp2(n, vi(n, INF)); rep(i, n) rep(j, n) { if (dp[i][j] <= l) dp2[i][j] = 1; } rep(k, n) rep(i, n) rep(j, n) chmin(dp2[i][j], dp2[i][k] + dp2[k][j]); int q; cin >> q; rep(i, q) { int s, t; cin >> s >> t; --s; --t; int ans = dp2[s][t]; if (ans == INF) cout << -1 << endl; else cout << ans - 1 << endl; } return 0; }
insert
55
55
55
57
-11
p02889
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; i++) #define RFOR(i, a, n) for (ll i = (ll)n - 1; i >= (ll)a; i--) #define rep(i, n) FOR(i, 0, n) #define rrep(i, n) RFOR(i, 0, n) #define ALL(v) v.begin(), v.end() #define bra(x, y) '(' << x << ',' << y << ')' ll MOD = 1000000007; ll INF = 214748364700000000; double EPS = 1e-12; double PI = 3.141592653589793238; template <typename T> void remove(std::vector<T> &vector, unsigned int index) { vector.erase(vector.begin() + index); } using Graph = vector<vector<pair<ll, ll>>>; ll N, M, L; Graph G(310); pair<ll, ll> fuel[310][310]; void dijkstra(ll s, ll e = L) { rep(i, N) { fuel[s][i].first = INF; fuel[s][i].second = -1; } ll cnt = 0; map<pair<ll, ll>, ll> mp; priority_queue<tuple<ll, ll, ll>> Q; bool done[310]; fill(done, done + 310, false); done[s] = true; Q.emplace(0, e, s); while (Q.size()) { // cnt++; if (cnt > N) { break; } ll C, E, X; tie(C, E, X) = Q.top(); Q.pop(); ll n = G[X].size(); done[X] = true; rep(i, n) { ll Y = G[X][i].first, D = G[X][i].second; if (done[Y]) continue; if (E >= D) { if (fuel[s][Y].first > -C) { Q.emplace(C, E - D, Y); fuel[s][Y].first = min(fuel[s][Y].first, -C); fuel[s][Y].second = E - D; } else if (fuel[s][Y].first == -C && fuel[s][Y].second < E - D) { Q.emplace(C, E - D, Y); fuel[s][Y].second = E - D; fuel[s][Y].first = min(fuel[s][Y].first, -C); } } else { if (fuel[s][Y].first > -C + 1) { Q.emplace(C - 1, L - D, Y); fuel[s][Y].first = min(fuel[s][Y].first, -C + 1); fuel[s][Y].second = L - D; } else if (fuel[s][Y].first == -C + 1 && fuel[s][Y].second < L - D) { Q.emplace(C - 1, L - D, Y); fuel[s][Y].second = L - D; fuel[s][Y].first = min(fuel[s][Y].first, -C + 1); } } } } return; } int main() { cin >> N >> M >> L; rep(i, M) { ll a, b, c; cin >> a >> b >> c; a--; b--; if (c > L) continue; G[a].emplace_back(b, c); G[b].emplace_back(a, c); } rep(i, N) { dijkstra(i); } ll q; cin >> q; vector<ll> ans(q); rep(i, q) { ll s, t; cin >> s >> t; s--; t--; ans[i] = fuel[s][t].first; if (fuel[s][t].first == INF) ans[i] = -1; } rep(i, q) { cout << ans[i] << endl; } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; i++) #define RFOR(i, a, n) for (ll i = (ll)n - 1; i >= (ll)a; i--) #define rep(i, n) FOR(i, 0, n) #define rrep(i, n) RFOR(i, 0, n) #define ALL(v) v.begin(), v.end() #define bra(x, y) '(' << x << ',' << y << ')' ll MOD = 1000000007; ll INF = 214748364700000000; double EPS = 1e-12; double PI = 3.141592653589793238; template <typename T> void remove(std::vector<T> &vector, unsigned int index) { vector.erase(vector.begin() + index); } using Graph = vector<vector<pair<ll, ll>>>; ll N, M, L; Graph G(310); pair<ll, ll> fuel[310][310]; void dijkstra(ll s, ll e = L) { rep(i, N) { fuel[s][i].first = INF; fuel[s][i].second = -1; } ll cnt = 0; map<pair<ll, ll>, ll> mp; priority_queue<tuple<ll, ll, ll>> Q; bool done[310]; fill(done, done + 310, false); done[s] = true; Q.emplace(0, e, s); while (Q.size()) { cnt++; if (cnt > 3 * N) { break; } ll C, E, X; tie(C, E, X) = Q.top(); Q.pop(); ll n = G[X].size(); done[X] = true; rep(i, n) { ll Y = G[X][i].first, D = G[X][i].second; if (done[Y]) continue; if (E >= D) { if (fuel[s][Y].first > -C) { Q.emplace(C, E - D, Y); fuel[s][Y].first = min(fuel[s][Y].first, -C); fuel[s][Y].second = E - D; } else if (fuel[s][Y].first == -C && fuel[s][Y].second < E - D) { Q.emplace(C, E - D, Y); fuel[s][Y].second = E - D; fuel[s][Y].first = min(fuel[s][Y].first, -C); } } else { if (fuel[s][Y].first > -C + 1) { Q.emplace(C - 1, L - D, Y); fuel[s][Y].first = min(fuel[s][Y].first, -C + 1); fuel[s][Y].second = L - D; } else if (fuel[s][Y].first == -C + 1 && fuel[s][Y].second < L - D) { Q.emplace(C - 1, L - D, Y); fuel[s][Y].second = L - D; fuel[s][Y].first = min(fuel[s][Y].first, -C + 1); } } } } return; } int main() { cin >> N >> M >> L; rep(i, M) { ll a, b, c; cin >> a >> b >> c; a--; b--; if (c > L) continue; G[a].emplace_back(b, c); G[b].emplace_back(a, c); } rep(i, N) { dijkstra(i); } ll q; cin >> q; vector<ll> ans(q); rep(i, q) { ll s, t; cin >> s >> t; s--; t--; ans[i] = fuel[s][t].first; if (fuel[s][t].first == INF) ans[i] = -1; } rep(i, q) { cout << ans[i] << endl; } }
replace
36
38
36
38
TLE
p02889
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, j, n) for (int i = (int)(j); i < (int)(n); i++) #define REP(i, j, n) for (int i = (int)(j); i <= (int)(n); i++) #define MOD 1000000007 #define int long long #define ALL(a) (a).begin(), (a).end() #define vi vector<int> #define vii vector<vi> #define pii pair<int, int> #define priq priority_queue<int> #define disup(A, key) distance(A.begin(), upper_bound(ALL(A), (int)(key))) #define dislow(A, key) distance(A.begin(), lower_bound(ALL(A), (int)(key))) #define tii tuple<int, int, int> #define Priq priority_queue<int, vi, greater<int>> #define pb push_back #define mp make_pair #define INF (1ll << 60) signed main() { int N, M, L; cin >> N >> M >> L; vector<vector<pii>> A(N), F(N, vector<pii>(N, mp(INF, L))); rep(i, 0, M) { int X, Y, Z; cin >> X >> Y >> Z; if (L >= Z) { A[X - 1].pb(mp(Y - 1, Z)); A[Y - 1].pb(mp(X - 1, Z)); } } rep(i, 0, N) { priority_queue<pair<pii, int>, vector<pair<pii, int>>, greater<pair<pii, int>>> Q; Q.push(mp(mp(0, 0), i)); F[i][i] = mp(0, 0); while (Q.size() > 0) { int X = Q.top().second, Y = Q.top().first.first, Z = Q.top().first.second; Q.pop(); rep(j, 0, A[X].size()) { int P = A[X][j].first, R = A[X][j].second; if (P != X) { int B = Z + R, C = Y; if (B > L) { B = R; C++; } if (F[i][P] > mp(C, B)) { F[i][P] = mp(C, B); Q.push(mp(mp(C, B), P)); } } } } } int Q; cin >> Q; rep(i, 0, Q) { int X, Y; cin >> X >> Y; int ans = F[X - 1][Y - 1].first; if (ans == INF) ans = -1; cout << ans << endl; } }
#include <bits/stdc++.h> using namespace std; #define rep(i, j, n) for (int i = (int)(j); i < (int)(n); i++) #define REP(i, j, n) for (int i = (int)(j); i <= (int)(n); i++) #define MOD 1000000007 #define int long long #define ALL(a) (a).begin(), (a).end() #define vi vector<int> #define vii vector<vi> #define pii pair<int, int> #define priq priority_queue<int> #define disup(A, key) distance(A.begin(), upper_bound(ALL(A), (int)(key))) #define dislow(A, key) distance(A.begin(), lower_bound(ALL(A), (int)(key))) #define tii tuple<int, int, int> #define Priq priority_queue<int, vi, greater<int>> #define pb push_back #define mp make_pair #define INF (1ll << 60) signed main() { int N, M, L; cin >> N >> M >> L; vector<vector<pii>> A(N), F(N, vector<pii>(N, mp(INF, L))); rep(i, 0, M) { int X, Y, Z; cin >> X >> Y >> Z; if (L >= Z) { A[X - 1].pb(mp(Y - 1, Z)); A[Y - 1].pb(mp(X - 1, Z)); } } rep(i, 0, N) { priority_queue<pair<pii, int>, vector<pair<pii, int>>, greater<pair<pii, int>>> Q; Q.push(mp(mp(0, 0), i)); F[i][i] = mp(0, 0); while (Q.size() > 0) { int X = Q.top().second, Y = Q.top().first.first, Z = Q.top().first.second; Q.pop(); if (mp(Y, Z) > F[i][X]) continue; rep(j, 0, A[X].size()) { int P = A[X][j].first, R = A[X][j].second; if (P != X) { int B = Z + R, C = Y; if (B > L) { B = R; C++; } if (F[i][P] > mp(C, B)) { F[i][P] = mp(C, B); Q.push(mp(mp(C, B), P)); } } } } } int Q; cin >> Q; rep(i, 0, Q) { int X, Y; cin >> X >> Y; int ans = F[X - 1][Y - 1].first; if (ans == INF) ans = -1; cout << ans << endl; } }
insert
39
39
39
41
TLE
p02889
C++
Time Limit Exceeded
#include <algorithm> #include <deque> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unordered_map> #include <vector> long long mod = 1e9 + 7; class Mint { public: long long x; Mint(){}; // 引数なしでも定義できるように引数なしコンストラクタも用意しておく Mint(long long a) { x = a % mod; while (x < 0) { x += mod; } }; Mint &operator+=(const Mint &a) { x += a.x; x %= mod; return *this; } Mint &operator-=(const Mint &a) { x += (mod - a.x); x %= mod; return *this; } Mint &operator*=(const Mint &a) { x *= a.x; x %= mod; return *this; } // a^n mod を計算する // Useaeg: Mint z = y.pow(y, n); Mint pow(Mint a, long long n) const { Mint y(1); Mint b(a); while (n > 0) { if (n & 1) y *= b; b *= b; n >>= 1; } return y; } // a^{-1} mod を計算する Mint modinv(const Mint &a) const { return pow(a, mod - 2); } Mint &operator/=(const Mint &a) { x *= modinv(a).x; x %= mod; return *this; } Mint operator+(Mint &a) const { Mint y(*this); y += a; return y; } Mint operator-(Mint &a) const { Mint y(*this); y -= a; return y; } Mint operator*(Mint &a) const { Mint y(*this); y *= a; return y; } Mint operator/(Mint &a) const { Mint y(*this); y /= a; return y.x; } // nCk @mod を計算する Mint nCk(Mint &n, const long long k) const { Mint y = Mint(1); for (Mint i(0); (i.x) < k; i.x++) { y *= (n - i); y /= i; } return y; } // nPk @mod を計算する Mint nPk(Mint &n, long long k) const { Mint y(1); for (Mint i(0); (i.x) < k; i.x++) { y *= (n - i); } return y; } }; struct node { int v; int supply; int amari; }; class MyComparison { public: bool operator()(node n1, node n2) { return (n1.supply != n2.supply) ? (n1.supply > n2.supply) : (n1.amari < n2.amari); } }; std::pair<int, int> dist[304][304]; int main() { int N, M, L; std::cin >> N >> M >> L; std::vector<std::pair<int, int>> edges[304]; int A, B, C; for (int i = 0; i < M; i++) { std::cin >> A >> B >> C; if (C > L) continue; A--; B--; edges[A].push_back(std::make_pair(B, C)); edges[B].push_back(std::make_pair(A, C)); } int Q; std::cin >> Q; std::vector<int> s(Q), t(Q); for (int i = 0; i < Q; i++) { std::cin >> s[i] >> t[i]; s[i]--; t[i]--; } for (int i = 0; i < 304; i++) { for (int j = 0; j < 304; j++) { dist[i][j] = std::make_pair(1001001001, 0); } } for (int i = 0; i < N; i++) { dist[i][i] = std::make_pair(0, L); std::priority_queue<node, std::vector<node>, MyComparison> queue; queue.push((node){i, 0, L}); while (queue.size() > 0) { auto entry = queue.top(); queue.pop(); int v = entry.v; int amari = entry.amari; int supply = entry.supply; // if ((dist[i][v].first<supply) || (dist[i][v].first==supply && // dist[i][v].second>amari)) continue; for (auto cand : edges[v]) { int next_amari = amari - cand.second; int next_supply = supply; int vv = cand.first; if (next_amari < 0) { next_amari = L - cand.second; next_supply++; } if ((next_supply < dist[i][vv].first) || (next_supply == dist[i][vv].first && next_amari > dist[i][vv].second)) { dist[i][vv].first = next_supply; dist[i][vv].second = next_amari; queue.push((node){vv, next_supply, next_amari}); // printf("dist[%d][%d]=(%d, %d)\n", i, vv, dist[i][vv].first, // dist[i][vv].second); } } } } for (int i = 0; i < Q; i++) { if (dist[s[i]][t[i]].first == 1001001001) { printf("-1\n"); } else { printf("%d\n", dist[s[i]][t[i]].first); } } return 0; }
#include <algorithm> #include <deque> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unordered_map> #include <vector> long long mod = 1e9 + 7; class Mint { public: long long x; Mint(){}; // 引数なしでも定義できるように引数なしコンストラクタも用意しておく Mint(long long a) { x = a % mod; while (x < 0) { x += mod; } }; Mint &operator+=(const Mint &a) { x += a.x; x %= mod; return *this; } Mint &operator-=(const Mint &a) { x += (mod - a.x); x %= mod; return *this; } Mint &operator*=(const Mint &a) { x *= a.x; x %= mod; return *this; } // a^n mod を計算する // Useaeg: Mint z = y.pow(y, n); Mint pow(Mint a, long long n) const { Mint y(1); Mint b(a); while (n > 0) { if (n & 1) y *= b; b *= b; n >>= 1; } return y; } // a^{-1} mod を計算する Mint modinv(const Mint &a) const { return pow(a, mod - 2); } Mint &operator/=(const Mint &a) { x *= modinv(a).x; x %= mod; return *this; } Mint operator+(Mint &a) const { Mint y(*this); y += a; return y; } Mint operator-(Mint &a) const { Mint y(*this); y -= a; return y; } Mint operator*(Mint &a) const { Mint y(*this); y *= a; return y; } Mint operator/(Mint &a) const { Mint y(*this); y /= a; return y.x; } // nCk @mod を計算する Mint nCk(Mint &n, const long long k) const { Mint y = Mint(1); for (Mint i(0); (i.x) < k; i.x++) { y *= (n - i); y /= i; } return y; } // nPk @mod を計算する Mint nPk(Mint &n, long long k) const { Mint y(1); for (Mint i(0); (i.x) < k; i.x++) { y *= (n - i); } return y; } }; struct node { int v; int supply; int amari; }; class MyComparison { public: bool operator()(node n1, node n2) { return (n1.supply != n2.supply) ? (n1.supply > n2.supply) : (n1.amari < n2.amari); } }; std::pair<int, int> dist[304][304]; int main() { int N, M, L; std::cin >> N >> M >> L; std::vector<std::pair<int, int>> edges[304]; int A, B, C; for (int i = 0; i < M; i++) { std::cin >> A >> B >> C; if (C > L) continue; A--; B--; edges[A].push_back(std::make_pair(B, C)); edges[B].push_back(std::make_pair(A, C)); } int Q; std::cin >> Q; std::vector<int> s(Q), t(Q); for (int i = 0; i < Q; i++) { std::cin >> s[i] >> t[i]; s[i]--; t[i]--; } for (int i = 0; i < 304; i++) { for (int j = 0; j < 304; j++) { dist[i][j] = std::make_pair(1001001001, 0); } } for (int i = 0; i < N; i++) { dist[i][i] = std::make_pair(0, L); std::priority_queue<node, std::vector<node>, MyComparison> queue; queue.push((node){i, 0, L}); while (queue.size() > 0) { auto entry = queue.top(); queue.pop(); int v = entry.v; int amari = entry.amari; int supply = entry.supply; if ((dist[i][v].first < supply) || (dist[i][v].first == supply && dist[i][v].second > amari)) continue; for (auto cand : edges[v]) { int next_amari = amari - cand.second; int next_supply = supply; int vv = cand.first; if (next_amari < 0) { next_amari = L - cand.second; next_supply++; } if ((next_supply < dist[i][vv].first) || (next_supply == dist[i][vv].first && next_amari > dist[i][vv].second)) { dist[i][vv].first = next_supply; dist[i][vv].second = next_amari; queue.push((node){vv, next_supply, next_amari}); // printf("dist[%d][%d]=(%d, %d)\n", i, vv, dist[i][vv].first, // dist[i][vv].second); } } } } for (int i = 0; i < Q; i++) { if (dist[s[i]][t[i]].first == 1001001001) { printf("-1\n"); } else { printf("%d\n", dist[s[i]][t[i]].first); } } return 0; }
replace
161
163
161
164
TLE
p02889
C++
Runtime Error
/** * code generated by JHelper * More info: https://github.com/AlexeyDmitriev/JHelper * @author alireza_kaviani */ #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; typedef long long ll; typedef long double ld; typedef pair<ll, ll> pll; typedef pair<double, double> pdd; #define all(x) (x).begin(), (x).end() #define Sort(x) sort(all((x))) #define X first #define Y second #define Mp make_pair #define sep ' ' #define endl '\n' #define debug(x) cerr << #x << " = " << x << endl #define SZ(x) ll(x.size()) #define fast_io \ ios::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define set_random \ mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); ll poww(ll a, ll b, ll md) { return (!b ? 1 : (b & 1 ? a * poww(a * a % md, b / 2, md) % md : poww(a * a % md, b / 2, md) % md)); } const ll MAXN = 1e2 + 10; const ll LOG = 22; const ll INF = 1e18; const ll MOD = 1e9 + 7; // 998244353; // 1e9 + 9; ll n, m, L, dist1[MAXN][MAXN], dist2[MAXN][MAXN], q; int main() { fast_io; for (ll i = 0; i < MAXN; i++) { for (ll j = 0; j < MAXN; j++) { dist1[i][j] = dist2[i][j] = INF; } dist1[i][i] = dist2[i][i] = 0; } cin >> n >> m >> L; for (ll i = 1; i <= m; i++) { ll v, u, w; cin >> v >> u >> w; dist1[v][u] = dist1[u][v] = w; } for (ll k = 1; k <= n; k++) { for (ll i = 1; i <= n; i++) { for (ll j = 1; j <= n; j++) { dist1[i][j] = min(dist1[i][j], dist1[i][k] + dist1[k][j]); } } } for (ll i = 1; i <= n; i++) { for (ll j = 1; j <= n; j++) { if (dist1[i][j] <= L) dist2[i][j] = 1; } } for (ll k = 1; k <= n; k++) { for (ll i = 1; i <= n; i++) { for (ll j = 1; j <= n; j++) { dist2[i][j] = min(dist2[i][j], dist2[i][k] + dist2[k][j]); } } } cin >> q; while (q--) { ll v, u; cin >> v >> u; cout << (dist2[v][u] == INF ? -1 : dist2[v][u] - 1) << endl; } return 0; } /* */
/** * code generated by JHelper * More info: https://github.com/AlexeyDmitriev/JHelper * @author alireza_kaviani */ #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; typedef long long ll; typedef long double ld; typedef pair<ll, ll> pll; typedef pair<double, double> pdd; #define all(x) (x).begin(), (x).end() #define Sort(x) sort(all((x))) #define X first #define Y second #define Mp make_pair #define sep ' ' #define endl '\n' #define debug(x) cerr << #x << " = " << x << endl #define SZ(x) ll(x.size()) #define fast_io \ ios::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define set_random \ mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); ll poww(ll a, ll b, ll md) { return (!b ? 1 : (b & 1 ? a * poww(a * a % md, b / 2, md) % md : poww(a * a % md, b / 2, md) % md)); } const ll MAXN = 3e2 + 10; const ll LOG = 22; const ll INF = 1e18; const ll MOD = 1e9 + 7; // 998244353; // 1e9 + 9; ll n, m, L, dist1[MAXN][MAXN], dist2[MAXN][MAXN], q; int main() { fast_io; for (ll i = 0; i < MAXN; i++) { for (ll j = 0; j < MAXN; j++) { dist1[i][j] = dist2[i][j] = INF; } dist1[i][i] = dist2[i][i] = 0; } cin >> n >> m >> L; for (ll i = 1; i <= m; i++) { ll v, u, w; cin >> v >> u >> w; dist1[v][u] = dist1[u][v] = w; } for (ll k = 1; k <= n; k++) { for (ll i = 1; i <= n; i++) { for (ll j = 1; j <= n; j++) { dist1[i][j] = min(dist1[i][j], dist1[i][k] + dist1[k][j]); } } } for (ll i = 1; i <= n; i++) { for (ll j = 1; j <= n; j++) { if (dist1[i][j] <= L) dist2[i][j] = 1; } } for (ll k = 1; k <= n; k++) { for (ll i = 1; i <= n; i++) { for (ll j = 1; j <= n; j++) { dist2[i][j] = min(dist2[i][j], dist2[i][k] + dist2[k][j]); } } } cin >> q; while (q--) { ll v, u; cin >> v >> u; cout << (dist2[v][u] == INF ? -1 : dist2[v][u] - 1) << endl; } return 0; } /* */
replace
44
45
44
45
0
p02889
C++
Runtime Error
#pragma GCC optimize(3, "Ofast", "inline") #include <bits/stdc++.h> #if __cplusplus >= 201103L #include <chrono> using namespace std::chrono; #endif using namespace std; #define ll long long #define ull unsigned long long #define pii pair<int, int> #define pb push_back #define fi first #define se second #ifdef LOCAL #define prln(x) (cout << #x << ' ' << x << endl) #define pr(x) (cout << #x << ' ' << x << ' ') #define prv(x) (cout << x << ' ') #else #define prln(x) #define pr(x) #define prv(x) #endif #define clr(x) memset((x), 0, sizeof((x))) #define clr1(x) memset((x), -1, sizeof((x))) #define endl "\n" #define pi acos(-1) #define rep(i, st, ed) for (int i = (st); i <= (ed); ++i) #define rep0(i, st, ed) for (int i = (st); i < (ed); ++i) #define per0(i, st, ed) for (int i = (st); i > ed; --i) #define per(i, st, ed) for (int i = (st); i >= ed; --i) #define chmin(a, b) (a = a > b ? b : a) #define chmax(a, b) (a = a < b ? b : a) template <class T> void _sf(T &x) { cin >> x; } void _sf(int &x) { scanf("%d", &x); } void _sf(ll &x) { scanf("%lld", &x); } void _sf(double &x) { scanf("%lf", &x); } void _sf(char &x) { scanf(" %c", &x); } void _sf(char *x) { scanf("%s", x); } void sf() {} template <class T, class... U> void sf(T &head, U &...tail) { _sf(head); sf(tail...); } const int mod = 1e9 + 7; const int INF = 0x3f3f3f3f; int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } int dis[35][35]; int a[35][35]; int main() { #ifdef LOCAL freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #if __cplusplus >= 201103L auto start = steady_clock::now(); #endif #endif // ios::sync_with_stdio(false); // cin.tie(0); memset(dis, INF, sizeof(dis)); memset(a, INF, sizeof(a)); int n, m, L; sf(n, m, L); // cin >> n >> m >> L; pr(n); pr(m); prln(L); getchar(); for (int i = 1; i <= m; ++i) { int u, v, w; cin >> u >> v >> w; pr(u); pr(v); prln(w); a[u][v] = a[v][u] = w; } for (int k = 1; k <= n; ++k) { for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { // pr(i);pr(j);prln(k); if (a[i][j] > a[i][k] + a[k][j]) { a[i][j] = a[i][k] + a[k][j]; } // pr(a[i][j]);pr(a[i][k]);prln(a[k][j]); } } } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { if (a[i][j] <= L) dis[i][j] = 0; } } for (int k = 1; k <= n; ++k) { for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { chmin(dis[i][j], dis[i][k] + dis[k][j] + 1); } } } int q; cin >> q; prln(q); while (q--) { int s, t; cin >> s >> t; if (dis[s][t] == INF) { puts("-1"); } else { cout << dis[s][t] << endl; } } #ifdef LOCAL #if __cplusplus >= 201103L auto end = steady_clock::now(); duration<double> time_span = duration_cast<duration<double>>(end - start); ; printf("Total time: %.6fs\n", time_span.count()); #endif #endif return 0; }
#pragma GCC optimize(3, "Ofast", "inline") #include <bits/stdc++.h> #if __cplusplus >= 201103L #include <chrono> using namespace std::chrono; #endif using namespace std; #define ll long long #define ull unsigned long long #define pii pair<int, int> #define pb push_back #define fi first #define se second #ifdef LOCAL #define prln(x) (cout << #x << ' ' << x << endl) #define pr(x) (cout << #x << ' ' << x << ' ') #define prv(x) (cout << x << ' ') #else #define prln(x) #define pr(x) #define prv(x) #endif #define clr(x) memset((x), 0, sizeof((x))) #define clr1(x) memset((x), -1, sizeof((x))) #define endl "\n" #define pi acos(-1) #define rep(i, st, ed) for (int i = (st); i <= (ed); ++i) #define rep0(i, st, ed) for (int i = (st); i < (ed); ++i) #define per0(i, st, ed) for (int i = (st); i > ed; --i) #define per(i, st, ed) for (int i = (st); i >= ed; --i) #define chmin(a, b) (a = a > b ? b : a) #define chmax(a, b) (a = a < b ? b : a) template <class T> void _sf(T &x) { cin >> x; } void _sf(int &x) { scanf("%d", &x); } void _sf(ll &x) { scanf("%lld", &x); } void _sf(double &x) { scanf("%lf", &x); } void _sf(char &x) { scanf(" %c", &x); } void _sf(char *x) { scanf("%s", x); } void sf() {} template <class T, class... U> void sf(T &head, U &...tail) { _sf(head); sf(tail...); } const int mod = 1e9 + 7; const int INF = 0x3f3f3f3f; int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } int dis[606][606]; int a[606][606]; int main() { #ifdef LOCAL freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #if __cplusplus >= 201103L auto start = steady_clock::now(); #endif #endif // ios::sync_with_stdio(false); // cin.tie(0); memset(dis, INF, sizeof(dis)); memset(a, INF, sizeof(a)); int n, m, L; sf(n, m, L); // cin >> n >> m >> L; pr(n); pr(m); prln(L); getchar(); for (int i = 1; i <= m; ++i) { int u, v, w; cin >> u >> v >> w; pr(u); pr(v); prln(w); a[u][v] = a[v][u] = w; } for (int k = 1; k <= n; ++k) { for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { // pr(i);pr(j);prln(k); if (a[i][j] > a[i][k] + a[k][j]) { a[i][j] = a[i][k] + a[k][j]; } // pr(a[i][j]);pr(a[i][k]);prln(a[k][j]); } } } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { if (a[i][j] <= L) dis[i][j] = 0; } } for (int k = 1; k <= n; ++k) { for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { chmin(dis[i][j], dis[i][k] + dis[k][j] + 1); } } } int q; cin >> q; prln(q); while (q--) { int s, t; cin >> s >> t; if (dis[s][t] == INF) { puts("-1"); } else { cout << dis[s][t] << endl; } } #ifdef LOCAL #if __cplusplus >= 201103L auto end = steady_clock::now(); duration<double> time_span = duration_cast<duration<double>>(end - start); ; printf("Total time: %.6fs\n", time_span.count()); #endif #endif return 0; }
replace
60
62
60
62
0
p02889
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 <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef long long ll; typedef pair<int, int> pii; struct Edge { int v, d; Edge(int v, int d) : v(v), d(d) {} }; struct Node { int u, t, r; Node(int u, int t, int r) : u(u), t(t), r(r) {} bool operator<(Node o) const { if (t == o.t) { return r < o.r; } else { return t > o.t; } } }; int N, M, L, Q; vector<vector<Edge>> adj; int maxR[305][305]; bool vis[305]; int memo[305][305]; int find(int s, int t) { memset(vis, false, sizeof(vis)); memset(maxR, 0, sizeof(maxR)); priority_queue<Node> q; q.push(Node(s, 0, L)); while (!q.empty()) { Node node = q.top(); q.pop(); int u = node.u; if (u == t) { return node.t; } if (vis[u]) { continue; } memo[s][u] = node.t; vis[u] = true; int m = adj[u].size(); for (int k = 0; k < m; ++k) { int v = adj[u][k].v; int d = adj[u][k].d; if (vis[v]) { continue; } if (node.r >= d) { if (node.r - d >= maxR[node.t][v]) { maxR[node.t][v] = node.r - d; q.push(Node(v, node.t, node.r - d)); } } else { if (L - d >= maxR[node.t + 1][v]) { maxR[node.t + 1][v] = L - d; q.push(Node(v, node.t + 1, L - d)); } } } } return -1; } int main() { scanf("%d %d %d", &N, &M, &L); for (int i = 0; i <= N; ++i) { adj.push_back(vector<Edge>()); } for (int i = 0; i < M; ++i) { int a, b, c; scanf("%d %d %d", &a, &b, &c); adj[a].push_back(Edge(b, c)); adj[b].push_back(Edge(a, c)); } scanf("%d", &Q); memset(memo, -1, sizeof(memo)); for (int q = 0; q < Q; ++q) { int s, t; scanf("%d %d", &s, &t); int ans = memo[s][t]; if (ans == -1) { ans = find(s, t); } printf("%d\n", ans); } return 0; }
#include <algorithm> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef long long ll; typedef pair<int, int> pii; struct Edge { int v, d; Edge(int v, int d) : v(v), d(d) {} }; struct Node { int u, t, r; Node(int u, int t, int r) : u(u), t(t), r(r) {} bool operator<(Node o) const { if (t == o.t) { return r < o.r; } else { return t > o.t; } } }; int N, M, L, Q; vector<vector<Edge>> adj; int maxR[305][305]; bool vis[305]; int memo[305][305]; int find(int s, int t) { memset(vis, false, sizeof(vis)); memset(maxR, 0, sizeof(maxR)); priority_queue<Node> q; q.push(Node(s, 0, L)); while (!q.empty()) { Node node = q.top(); q.pop(); int u = node.u; if (u == t) { return node.t; } if (vis[u]) { continue; } memo[s][u] = node.t; memo[u][s] = node.t; vis[u] = true; int m = adj[u].size(); for (int k = 0; k < m; ++k) { int v = adj[u][k].v; int d = adj[u][k].d; if (vis[v]) { continue; } if (node.r >= d) { if (node.r - d >= maxR[node.t][v]) { maxR[node.t][v] = node.r - d; q.push(Node(v, node.t, node.r - d)); } } else { if (L - d >= maxR[node.t + 1][v]) { maxR[node.t + 1][v] = L - d; q.push(Node(v, node.t + 1, L - d)); } } } } return -1; } int main() { scanf("%d %d %d", &N, &M, &L); for (int i = 0; i <= N; ++i) { adj.push_back(vector<Edge>()); } for (int i = 0; i < M; ++i) { int a, b, c; scanf("%d %d %d", &a, &b, &c); adj[a].push_back(Edge(b, c)); adj[b].push_back(Edge(a, c)); } scanf("%d", &Q); memset(memo, -1, sizeof(memo)); for (int q = 0; q < Q; ++q) { int s, t; scanf("%d %d", &s, &t); int ans = memo[s][t]; if (ans == -1) { ans = find(s, t); } printf("%d\n", ans); } return 0; }
insert
62
62
62
63
TLE
p02889
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define PI 3.1415926535897932384626433832795 #define MOD (1000000007) #define EPS (1e-15) #define rep(i, n) for (int i = 0; i < (n); i++) #define repc(i, s, n) for (int i = (s); i <= (n); i++) #define rrep(i, n) for (int i = (n)-1; i >= 0; i--) #define rrepc(i, s, n) for (int i = (s); i >= (n); i--) #define bet(x, a, b) (a) <= (x) && (x) < (b) typedef long long ll; typedef unsigned long long ull; #define NMAX (300) #define p(a, b) make_pair(a, b) typedef pair<int, int> pi; struct pc { int chg; int rem; int idx; }; int N, M, L, Q; int s[NMAX * (NMAX - 1)]; int t[NMAX * (NMAX - 1)]; vector<pi> G[NMAX + 1]; bool done[NMAX + 1] = {false}; int ans[NMAX + 1][NMAX + 1]; void setAns(int root) { auto comp = [](pc &a, pc &b) { return (a.chg != b.chg ? a.chg > b.chg : a.rem < b.rem); }; priority_queue<pc, vector<pc>, decltype(comp)> pq{comp}; pc item; repc(i, 1, N) done[i] = false; item.chg = 0, item.rem = L, item.idx = root; pq.push(item); while (!pq.empty()) { item = pq.top(); pq.pop(); int chg = item.chg; int rem = item.rem; int current = item.idx; int size = G[current].size(); if (done[current]) continue; done[current] = true; ans[root][current] = chg; rep(i, size) { pi info = G[current][i]; int next = info.first; int len = info.second; if (rem >= len) { item.chg = chg; item.rem = rem - len; } else { item.chg = chg + 1; item.rem = L - len; } item.idx = next; pq.push(item); } } } int main() { int A, B, C; cin >> N >> M >> L; rep(i, M) { cin >> A >> B >> C; if (C > L) continue; G[A].push_back(p(B, C)); G[B].push_back(p(A, C)); } cin >> Q; rep(i, Q) cin >> s[i] >> t[i]; repc(i, 1, N) repc(j, 1, N) ans[i][j] = -1; repc(i, 1, N) setAns(i); rep(i, Q) cout << ans[s[i]][t[i]] << endl; return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define PI 3.1415926535897932384626433832795 #define MOD (1000000007) #define EPS (1e-15) #define rep(i, n) for (int i = 0; i < (n); i++) #define repc(i, s, n) for (int i = (s); i <= (n); i++) #define rrep(i, n) for (int i = (n)-1; i >= 0; i--) #define rrepc(i, s, n) for (int i = (s); i >= (n); i--) #define bet(x, a, b) (a) <= (x) && (x) < (b) typedef long long ll; typedef unsigned long long ull; #define NMAX (300) #define p(a, b) make_pair(a, b) typedef pair<int, int> pi; struct pc { int chg; int rem; int idx; }; int N, M, L, Q; int s[NMAX * (NMAX - 1)]; int t[NMAX * (NMAX - 1)]; vector<pi> G[NMAX + 1]; bool done[NMAX + 1] = {false}; int ans[NMAX + 1][NMAX + 1]; void setAns(int root) { auto comp = [](pc &a, pc &b) { return (a.chg != b.chg ? a.chg > b.chg : a.rem < b.rem); }; priority_queue<pc, vector<pc>, decltype(comp)> pq{comp}; pc item; repc(i, 1, N) done[i] = false; item.chg = 0, item.rem = L, item.idx = root; pq.push(item); while (!pq.empty()) { item = pq.top(); pq.pop(); int chg = item.chg; int rem = item.rem; int current = item.idx; int size = G[current].size(); if (done[current]) continue; done[current] = true; ans[root][current] = chg; rep(i, size) { pi info = G[current][i]; int next = info.first; int len = info.second; if (done[next]) continue; if (rem >= len) { item.chg = chg; item.rem = rem - len; } else { item.chg = chg + 1; item.rem = L - len; } item.idx = next; pq.push(item); } } } int main() { int A, B, C; cin >> N >> M >> L; rep(i, M) { cin >> A >> B >> C; if (C > L) continue; G[A].push_back(p(B, C)); G[B].push_back(p(A, C)); } cin >> Q; rep(i, Q) cin >> s[i] >> t[i]; repc(i, 1, N) repc(j, 1, N) ans[i][j] = -1; repc(i, 1, N) setAns(i); rep(i, Q) cout << ans[s[i]][t[i]] << endl; return 0; }
insert
73
73
73
76
TLE
p02889
C++
Time Limit Exceeded
#include <bits/stdc++.h> // #include <boost/multiprecision/cpp_int.hpp> #define int long long #define inf 1000000007 #define pa pair<int, int> #define ll long long #define pal pair<double, double> #define ppap pair<pa, int> #define PI 3.14159265358979323846 #define paa pair<int, char> #define mp make_pair #define pb push_back #define EPS (1e-8) int dx[8] = {0, 1, 0, -1, 1, 1, -1, -1}; int dy[8] = {1, 0, -1, 0, -1, 1, 1, -1}; using namespace std; class pa3 { public: int x; int y, z; pa3(int x = 0, int y = 0, int z = 0) : x(x), y(y), z(z) {} bool operator<(const pa3 &p) const { if (x != p.x) return x < p.x; if (y != p.y) return y < p.y; return z < p.z; // return x != p.x ? x<p.x: y<p.y; } bool operator>(const pa3 &p) const { if (x != p.x) return x > p.x; if (y != p.y) return y > p.y; return z > p.z; // return x != p.x ? x<p.x: y<p.y; } bool operator==(const pa3 &p) const { return x == p.x && y == p.y && z == p.z; } bool operator!=(const pa3 &p) const { return !(x == p.x && y == p.y && z == p.z); } }; class pa4 { public: int x; int y, z, w; pa4(int x = 0, int y = 0, int z = 0, int w = 0) : x(x), y(y), z(z), w(w) {} bool operator<(const pa4 &p) const { if (x != p.x) return x < p.x; if (y != p.y) return y < p.y; if (z != p.z) return z < p.z; return w < p.w; // return x != p.x ? x<p.x: y<p.y; } bool operator>(const pa4 &p) const { if (x != p.x) return x > p.x; if (y != p.y) return y > p.y; if (z != p.z) return z > p.z; return w > p.w; // return x != p.x ? x<p.x: y<p.y; } bool operator==(const pa4 &p) const { return x == p.x && y == p.y && z == p.z && w == p.w; } }; class pa2 { public: int x, y; pa2(int x = 0, int y = 0) : x(x), y(y) {} pa2 operator+(pa2 p) { return pa2(x + p.x, y + p.y); } pa2 operator-(pa2 p) { return pa2(x - p.x, y - p.y); } bool operator<(const pa2 &p) const { return y != p.y ? y < p.y : x < p.x; } bool operator>(const pa2 &p) const { return x != p.x ? x < p.x : y < p.y; } bool operator==(const pa2 &p) const { return abs(x - p.x) == 0 && abs(y - p.y) == 0; } bool operator!=(const pa2 &p) const { return !(abs(x - p.x) == 0 && abs(y - p.y) == 0); } }; string itos(int i) { ostringstream s; s << i; return s.str(); } int gcd(int v, int b) { if (v > b) return gcd(b, v); if (v == b) return b; if (b % v == 0) return v; return gcd(v, b % v); } int mod; int extgcd(int a, int b, int &x, int &y) { if (b == 0) { x = 1; y = 0; return a; } int d = extgcd(b, a % b, y, x); y -= a / b * x; return d; } pa operator+(const pa &l, const pa &r) { return {l.first + r.first, l.second + r.second}; } pa operator-(const pa &l, const pa &r) { return {l.first - r.first, l.second - r.second}; } int pr[1000010]; int inv[1000010]; int beki(int wa, int rr, int warukazu) { if (rr == 0) return 1 % warukazu; if (rr == 1) return wa % warukazu; wa %= warukazu; if (rr % 2 == 1) return ((ll)beki(wa, rr - 1, warukazu) * (ll)wa) % warukazu; ll zx = beki(wa, rr / 2, warukazu); return (zx * zx) % warukazu; } int comb(int nn, int rr) { if (rr < 0 || rr > nn || nn < 0) return 0; int r = pr[nn] * inv[rr]; r %= mod; r *= inv[nn - rr]; r %= mod; return r; } void gya(int ert) { pr[0] = 1; for (int i = 1; i <= ert; i++) { pr[i] = (pr[i - 1] * i) % mod; } inv[ert] = beki(pr[ert], mod - 2, mod); for (int i = ert - 1; i >= 0; i--) { inv[i] = inv[i + 1] * (i + 1) % mod; } } // cin.tie(0); // ios::sync_with_stdio(false); // priority_queue<pa3,vector<pa3>,greater<pa3>> pq; // sort(ve.begin(),ve.end(),greater<int>()); // mt19937(clock_per_sec); // mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()) ; //----------------kokomade tenpure------------ vector<pa> G[320]; int ans[320][320]; int n, m, l; int solve(int r) { for (int i = 1; i <= n; i++) ans[r][i] = -1; priority_queue<pa3, vector<pa3>, greater<pa3>> pq; pq.push((pa3){0, -l, r}); while (pq.size()) { pa3 z = pq.top(); pq.pop(); if (ans[r][z.z] >= 0) continue; // cout<<z.x<<" "<<-z.y<<" "<<" "<<r<<" "<<z.z<<endl; ans[r][z.z] = z.x; for (auto v : G[z.z]) { // cout<<ans[r][v.s]<<" "<<v.second<<endl; if (ans[r][v.first] >= 0) continue; // cout<<v.second<<endl; int nokori = -z.y; if (nokori >= v.second) pq.push((pa3){z.x, -(nokori - v.second), v.first}); else pq.push((pa3){z.x + 1, -(l - v.second), v.first}); } } } signed main() { cin.tie(0); ios::sync_with_stdio(false); cin >> n >> m >> l; for (int i = 0; i < m; i++) { int y, yy, yyy; cin >> y >> yy >> yyy; if (yyy > l) continue; G[y].pb(mp(yy, yyy)); G[yy].pb(mp(y, yyy)); } // cout<<G[3][0].first<<endl; for (int i = 1; i <= n; i++) solve(i); int q; cin >> q; for (int i = 0; i < q; i++) { int a, b; cin >> a >> b; cout << ans[a][b] << endl; } return 0; }
#include <bits/stdc++.h> // #include <boost/multiprecision/cpp_int.hpp> // #define int long long #define inf 1000000007 #define pa pair<int, int> #define ll long long #define pal pair<double, double> #define ppap pair<pa, int> #define PI 3.14159265358979323846 #define paa pair<int, char> #define mp make_pair #define pb push_back #define EPS (1e-8) int dx[8] = {0, 1, 0, -1, 1, 1, -1, -1}; int dy[8] = {1, 0, -1, 0, -1, 1, 1, -1}; using namespace std; class pa3 { public: int x; int y, z; pa3(int x = 0, int y = 0, int z = 0) : x(x), y(y), z(z) {} bool operator<(const pa3 &p) const { if (x != p.x) return x < p.x; if (y != p.y) return y < p.y; return z < p.z; // return x != p.x ? x<p.x: y<p.y; } bool operator>(const pa3 &p) const { if (x != p.x) return x > p.x; if (y != p.y) return y > p.y; return z > p.z; // return x != p.x ? x<p.x: y<p.y; } bool operator==(const pa3 &p) const { return x == p.x && y == p.y && z == p.z; } bool operator!=(const pa3 &p) const { return !(x == p.x && y == p.y && z == p.z); } }; class pa4 { public: int x; int y, z, w; pa4(int x = 0, int y = 0, int z = 0, int w = 0) : x(x), y(y), z(z), w(w) {} bool operator<(const pa4 &p) const { if (x != p.x) return x < p.x; if (y != p.y) return y < p.y; if (z != p.z) return z < p.z; return w < p.w; // return x != p.x ? x<p.x: y<p.y; } bool operator>(const pa4 &p) const { if (x != p.x) return x > p.x; if (y != p.y) return y > p.y; if (z != p.z) return z > p.z; return w > p.w; // return x != p.x ? x<p.x: y<p.y; } bool operator==(const pa4 &p) const { return x == p.x && y == p.y && z == p.z && w == p.w; } }; class pa2 { public: int x, y; pa2(int x = 0, int y = 0) : x(x), y(y) {} pa2 operator+(pa2 p) { return pa2(x + p.x, y + p.y); } pa2 operator-(pa2 p) { return pa2(x - p.x, y - p.y); } bool operator<(const pa2 &p) const { return y != p.y ? y < p.y : x < p.x; } bool operator>(const pa2 &p) const { return x != p.x ? x < p.x : y < p.y; } bool operator==(const pa2 &p) const { return abs(x - p.x) == 0 && abs(y - p.y) == 0; } bool operator!=(const pa2 &p) const { return !(abs(x - p.x) == 0 && abs(y - p.y) == 0); } }; string itos(int i) { ostringstream s; s << i; return s.str(); } int gcd(int v, int b) { if (v > b) return gcd(b, v); if (v == b) return b; if (b % v == 0) return v; return gcd(v, b % v); } int mod; int extgcd(int a, int b, int &x, int &y) { if (b == 0) { x = 1; y = 0; return a; } int d = extgcd(b, a % b, y, x); y -= a / b * x; return d; } pa operator+(const pa &l, const pa &r) { return {l.first + r.first, l.second + r.second}; } pa operator-(const pa &l, const pa &r) { return {l.first - r.first, l.second - r.second}; } int pr[1000010]; int inv[1000010]; int beki(int wa, int rr, int warukazu) { if (rr == 0) return 1 % warukazu; if (rr == 1) return wa % warukazu; wa %= warukazu; if (rr % 2 == 1) return ((ll)beki(wa, rr - 1, warukazu) * (ll)wa) % warukazu; ll zx = beki(wa, rr / 2, warukazu); return (zx * zx) % warukazu; } int comb(int nn, int rr) { if (rr < 0 || rr > nn || nn < 0) return 0; int r = pr[nn] * inv[rr]; r %= mod; r *= inv[nn - rr]; r %= mod; return r; } void gya(int ert) { pr[0] = 1; for (int i = 1; i <= ert; i++) { pr[i] = (pr[i - 1] * i) % mod; } inv[ert] = beki(pr[ert], mod - 2, mod); for (int i = ert - 1; i >= 0; i--) { inv[i] = inv[i + 1] * (i + 1) % mod; } } // cin.tie(0); // ios::sync_with_stdio(false); // priority_queue<pa3,vector<pa3>,greater<pa3>> pq; // sort(ve.begin(),ve.end(),greater<int>()); // mt19937(clock_per_sec); // mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()) ; //----------------kokomade tenpure------------ vector<pa> G[320]; int ans[320][320]; int n, m, l; int solve(int r) { for (int i = 1; i <= n; i++) ans[r][i] = -1; priority_queue<pa3, vector<pa3>, greater<pa3>> pq; pq.push((pa3){0, -l, r}); while (pq.size()) { pa3 z = pq.top(); pq.pop(); if (ans[r][z.z] >= 0) continue; // cout<<z.x<<" "<<-z.y<<" "<<" "<<r<<" "<<z.z<<endl; ans[r][z.z] = z.x; for (auto v : G[z.z]) { // cout<<ans[r][v.s]<<" "<<v.second<<endl; if (ans[r][v.first] >= 0) continue; // cout<<v.second<<endl; int nokori = -z.y; if (nokori >= v.second) pq.push((pa3){z.x, -(nokori - v.second), v.first}); else pq.push((pa3){z.x + 1, -(l - v.second), v.first}); } } } signed main() { cin.tie(0); ios::sync_with_stdio(false); cin >> n >> m >> l; for (int i = 0; i < m; i++) { int y, yy, yyy; cin >> y >> yy >> yyy; if (yyy > l) continue; G[y].pb(mp(yy, yyy)); G[yy].pb(mp(y, yyy)); } // cout<<G[3][0].first<<endl; for (int i = 1; i <= n; i++) solve(i); int q; cin >> q; for (int i = 0; i < q; i++) { int a, b; cin >> a >> b; cout << ans[a][b] << endl; } return 0; }
replace
3
4
3
4
TLE
p02889
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define reg register #define ll long long #define maxn 305 inline ll read() { ll x = 0, w = 0; char ch = getchar(); while (!isdigit(ch)) w |= ch == '-', ch = getchar(); while (isdigit(ch)) x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar(); return w ? -x : x; } int n, m, l, Q; int headd[maxn], nextt[maxn << 1], to[maxn << 1], val[maxn << 1], cntt; int dp[maxn][maxn], ss[maxn], vis[maxn]; inline void add(int u, int v, int w) { nextt[++cntt] = headd[u]; headd[u] = cntt; to[cntt] = v, val[cntt] = w; } struct node { int x, y, z; bool operator<(const node &b) const { if (y == b.y) return z < b.z; return y > b.y; } }; priority_queue<node> q; inline void dij(int x) { memset(vis, 0, sizeof vis); memset(dp[x], 63, sizeof dp[x]); memset(ss, 63, sizeof ss); dp[x][x] = 0; ss[x] = l; node u; reg int i, k, o; q.push((node){x, 0, l}); while (!q.empty()) { u = q.top(); q.pop(); if (vis[u.x]) continue; vis[u.x] = 1; for (i = headd[u.x]; i; i = nextt[i]) { k = -1; if (u.z < val[i] && l >= val[i]) k = 1, o = l - val[i]; if (u.z >= val[i]) k = 0, o = u.z - val[i]; if (k == -1) continue; if (dp[x][to[i]] > dp[x][u.x] + k) { dp[x][to[i]] = dp[x][u.x] + k; ss[to[i]] = o; q.push((node){to[i], dp[x][to[i]], ss[to[i]]}); } else { if (dp[x][to[i]] == dp[x][u.x] + k && ss[to[i]] < o) ss[to[i]] = o, q.push((node){to[i], dp[x][to[i]], ss[to[i]]}); } } } } int main() { n = read(), m = read(), l = read(); reg int i, u, v, w; for (i = 1; i <= m; ++i) { u = read(), v = read(), w = read(); add(u, v, w); add(v, u, w); } for (i = 1; i <= n; ++i) dij(i); Q = read(); while (Q--) { u = read(), v = read(); if (dp[u][v] > n) puts("-1"); else printf("%d\n", dp[u][v]); } return 0; }
#include <bits/stdc++.h> using namespace std; #define reg register #define ll long long #define maxn 305 inline ll read() { ll x = 0, w = 0; char ch = getchar(); while (!isdigit(ch)) w |= ch == '-', ch = getchar(); while (isdigit(ch)) x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar(); return w ? -x : x; } int n, m, l, Q; int headd[maxn * maxn], nextt[maxn * maxn], to[maxn * maxn], val[maxn * maxn], cntt; int dp[maxn][maxn], ss[maxn], vis[maxn]; inline void add(int u, int v, int w) { nextt[++cntt] = headd[u]; headd[u] = cntt; to[cntt] = v, val[cntt] = w; } struct node { int x, y, z; bool operator<(const node &b) const { if (y == b.y) return z < b.z; return y > b.y; } }; priority_queue<node> q; inline void dij(int x) { memset(vis, 0, sizeof vis); memset(dp[x], 63, sizeof dp[x]); memset(ss, 63, sizeof ss); dp[x][x] = 0; ss[x] = l; node u; reg int i, k, o; q.push((node){x, 0, l}); while (!q.empty()) { u = q.top(); q.pop(); if (vis[u.x]) continue; vis[u.x] = 1; for (i = headd[u.x]; i; i = nextt[i]) { k = -1; if (u.z < val[i] && l >= val[i]) k = 1, o = l - val[i]; if (u.z >= val[i]) k = 0, o = u.z - val[i]; if (k == -1) continue; if (dp[x][to[i]] > dp[x][u.x] + k) { dp[x][to[i]] = dp[x][u.x] + k; ss[to[i]] = o; q.push((node){to[i], dp[x][to[i]], ss[to[i]]}); } else { if (dp[x][to[i]] == dp[x][u.x] + k && ss[to[i]] < o) ss[to[i]] = o, q.push((node){to[i], dp[x][to[i]], ss[to[i]]}); } } } } int main() { n = read(), m = read(), l = read(); reg int i, u, v, w; for (i = 1; i <= m; ++i) { u = read(), v = read(), w = read(); add(u, v, w); add(v, u, w); } for (i = 1; i <= n; ++i) dij(i); Q = read(); while (Q--) { u = read(), v = read(); if (dp[u][v] > n) puts("-1"); else printf("%d\n", dp[u][v]); } return 0; }
replace
15
16
15
17
0
p02889
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <unordered_map> #include <vector> using namespace std; #define MOD 1000000007 #define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++) #define FOR(i, c) \ for (decltype((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define ll long long #define ull unsigned long long #define all(hoge) (hoge).begin(), (hoge).end() typedef pair<ll, ll> P; const long long INF = 1LL << 60; typedef vector<ll> Array; typedef vector<Array> Matrix; template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } // グラフ関連 struct Edge { // グラフ ll to, cap, rev; Edge(ll _to, ll _cap, ll _rev) { to = _to; cap = _cap; rev = _rev; } }; typedef vector<Edge> Edges; typedef vector<Edges> Graph; void add_edge(Graph &G, ll from, ll to, ll cap, bool revFlag, ll revCap) { G[from].push_back(Edge(to, cap, (ll)G[to].size())); if (revFlag) G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1)); } void Dijkstra(Graph &G, ll s, Array &d, Array &u, ll l) { // O(|E|log|V|) d.resize(G.size()); u.resize(G.size()); REP(i, d.size()) d[i] = INF; REP(i, u.size()) u[i] = INF; d[s] = 0; u[s] = 0; vector<ll> q(G.size()); REP(i, G.size()) q.push_back(i); while (q.size() > 0) { ll uu = INF; ll dd = INF; ll v = -1; ll k = -1; REP(i, q.size()) { if (uu > u[q[i]] || (uu == u[q[i]] && dd > d[q[i]])) { v = q[i]; uu = u[q[i]]; dd = d[q[i]]; k = i; } } q.erase(q.begin() + k); for (auto e : G[v]) { ll nu = uu; ll nd = dd + e.cap; if (nd > l) { nd = e.cap; nu++; } if (u[e.to] > nu || (u[e.to] == nu && d[e.to] > nd)) { u[e.to] = nu; d[e.to] = nd; } } } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); ll n, m, l; cin >> n >> m >> l; Graph g(n); REP(i, m) { ll a, b, c; cin >> a >> b >> c; a--; b--; if (c <= l) add_edge(g, a, b, c, true, c); } Matrix ans(n); Matrix d(n); REP(i, n) { Dijkstra(g, i, d[i], ans[i], l); } /* for (auto i : ans) { for (auto j : i) { cout << j << " "; } cout << endl; } */ ll q; cin >> q; REP(i, q) { ll s, t; cin >> s >> t; s--; t--; if (ans[s][t] != INF) { cout << ans[s][t] << endl; } else { cout << -1 << endl; } } return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <unordered_map> #include <vector> using namespace std; #define MOD 1000000007 #define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++) #define FOR(i, c) \ for (decltype((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define ll long long #define ull unsigned long long #define all(hoge) (hoge).begin(), (hoge).end() typedef pair<ll, ll> P; const long long INF = 1LL << 60; typedef vector<ll> Array; typedef vector<Array> Matrix; template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } // グラフ関連 struct Edge { // グラフ ll to, cap, rev; Edge(ll _to, ll _cap, ll _rev) { to = _to; cap = _cap; rev = _rev; } }; typedef vector<Edge> Edges; typedef vector<Edges> Graph; void add_edge(Graph &G, ll from, ll to, ll cap, bool revFlag, ll revCap) { G[from].push_back(Edge(to, cap, (ll)G[to].size())); if (revFlag) G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1)); } void Dijkstra(Graph &G, ll s, Array &d, Array &u, ll l) { // O(|E|log|V|) d.resize(G.size()); u.resize(G.size()); REP(i, d.size()) d[i] = INF; REP(i, u.size()) u[i] = INF; d[s] = 0; u[s] = 0; vector<ll> q(G.size()); REP(i, G.size()) q.push_back(i); while (q.size() > 0) { ll uu = INF; ll dd = INF; ll v = -1; ll k = -1; REP(i, q.size()) { if (uu > u[q[i]] || (uu == u[q[i]] && dd > d[q[i]])) { v = q[i]; uu = u[q[i]]; dd = d[q[i]]; k = i; } } if (k == -1) break; q.erase(q.begin() + k); for (auto e : G[v]) { ll nu = uu; ll nd = dd + e.cap; if (nd > l) { nd = e.cap; nu++; } if (u[e.to] > nu || (u[e.to] == nu && d[e.to] > nd)) { u[e.to] = nu; d[e.to] = nd; } } } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); ll n, m, l; cin >> n >> m >> l; Graph g(n); REP(i, m) { ll a, b, c; cin >> a >> b >> c; a--; b--; if (c <= l) add_edge(g, a, b, c, true, c); } Matrix ans(n); Matrix d(n); REP(i, n) { Dijkstra(g, i, d[i], ans[i], l); } /* for (auto i : ans) { for (auto j : i) { cout << j << " "; } cout << endl; } */ ll q; cin >> q; REP(i, q) { ll s, t; cin >> s >> t; s--; t--; if (ans[s][t] != INF) { cout << ans[s][t] << endl; } else { cout << -1 << endl; } } return 0; }
insert
84
84
84
86
0
p02889
C++
Time Limit Exceeded
#pragma GCC optimize(3) #include <bits/stdc++.h> #define ll long long #define F first #define S second #define P pair #define FOR(i, a, b) for (int i = a; i <= b; i++) #define V vector #define RE return #define ALL(a) a.begin(), a.end() #define MP make_pair #define PB push_back #define PF push_front #define FILL(a, b) memset(a, b, sizeof(a)) using namespace std; P<int, int> dis[305][305]; V<P<int, int>> v[305]; int n; #define PP pair<pair<int, int>, int> priority_queue<PP, vector<PP>, greater<PP>> q; int main() { ios::sync_with_stdio(0); cin.tie(0); int m, l; cin >> n >> m >> l; int x, y, z; FOR(i, 1, m) { cin >> x >> y >> z; if (z > l) continue; v[x].PB(MP(y, z)); v[y].PB(MP(x, z)); } FOR(i, 1, n) { FOR(j, 1, n) dis[i][j] = MP(-1, 0); } FOR(i, 1, n) { q.push(MP(MP(0, 0), i)); while (!q.empty()) { PP t = q.top(); q.pop(); if (dis[i][t.S].F != -1) continue; dis[i][t.S] = t.F; for (int j = 0; j < v[t.S].size(); j++) { if (v[t.S][j].S + t.F.S > l) { q.push(MP(MP(t.F.F + 1, v[t.S][j].S), v[t.S][j].F)); } else { q.push(MP(MP(t.F.F, t.F.S + v[t.S][j].S), v[t.S][j].F)); } } } } int q, s1, s2; cin >> q; FOR(i, 1, q) { cin >> s1 >> s2; cout << dis[s1][s2].F << '\n'; } return 0; }
#pragma GCC optimize(3) #include <bits/stdc++.h> #define ll long long #define F first #define S second #define P pair #define FOR(i, a, b) for (int i = a; i <= b; i++) #define V vector #define RE return #define ALL(a) a.begin(), a.end() #define MP make_pair #define PB push_back #define PF push_front #define FILL(a, b) memset(a, b, sizeof(a)) using namespace std; P<int, int> dis[305][305]; V<P<int, int>> v[305]; int n; #define PP pair<pair<int, int>, int> priority_queue<PP, vector<PP>, greater<PP>> q; int main() { ios::sync_with_stdio(0); cin.tie(0); int m, l; cin >> n >> m >> l; int x, y, z; FOR(i, 1, m) { cin >> x >> y >> z; if (z > l) continue; v[x].PB(MP(y, z)); v[y].PB(MP(x, z)); } FOR(i, 1, n) { FOR(j, 1, n) dis[i][j] = MP(-1, 0); } FOR(i, 1, n) { q.push(MP(MP(0, 0), i)); while (!q.empty()) { PP t = q.top(); q.pop(); if (dis[i][t.S].F != -1) continue; dis[i][t.S] = t.F; for (int j = 0; j < v[t.S].size(); j++) { if (dis[i][v[t.S][j].F].F != -1) continue; if (v[t.S][j].S + t.F.S > l) { q.push(MP(MP(t.F.F + 1, v[t.S][j].S), v[t.S][j].F)); } else { q.push(MP(MP(t.F.F, t.F.S + v[t.S][j].S), v[t.S][j].F)); } } } } int q, s1, s2; cin >> q; FOR(i, 1, q) { cin >> s1 >> s2; cout << dis[s1][s2].F << '\n'; } return 0; }
insert
43
43
43
45
TLE
p02889
C++
Time Limit Exceeded
#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; }; struct point { ll x; ll y; bool operator<(const point &p) const { if (x == p.x) return y < p.y; return x < p.x; } }; struct undirected_edge { ll from; ll to; ll cost; bool operator<(const undirected_edge &ue) const { return cost < ue.cost; } }; typedef string str; typedef std::pair<ll, ll> pl; typedef std::tuple<ll, ll, ll> tp3; typedef std::tuple<ll, ll, ll, ll> tp4; 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<vl> vl2; typedef std::vector<vl2> vl3; typedef std::vector<vl3> vl4; typedef std::vector<bool> vb; typedef std::vector<vb> vb2; typedef std::vector<vb2> vb3; typedef std::vector<vb3> vb4; typedef std::vector<pl> vpl; typedef std::vector<tp3> vtp3; typedef std::vector<tp4> vtp4; typedef std::vector<point> points; // priority queue. Taking from the higher value. Don't forget calling !q.empty() typedef std::priority_queue<ll> pq; // priority queue. Taking from the lower value typedef std::priority_queue<ll, vl, greater<ll>> pql; typedef std::vector<vector<edge>> Graph; const ll N_DIGITS = 60; const 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; points dirs = { {-1, 0}, {1, 0}, {0, 1}, {0, -1}, // four directions {1, 1}, {-1, 1}, {-1, -1}, {1, -1}, // diagonal {0, 0} // self }; template <typename A, typename B> string to_string(pair<A, B> p); template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p); template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p); string to_string(const string &s) { return '"' + s + '"'; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } string to_string(vector<bool> v) { bool first = true; string res = "{"; for (int i = 0; i < static_cast<int>(v.size()); i++) { if (!first) { res += ", "; } first = false; res += to_string(v[i]); } res += "}"; return res; } template <size_t N> string to_string(bitset<N> v) { string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; } template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #define rep(i, n) for (ll(i) = 0; (i) < (n); (i)++) #define revrep(i, n) for (ll(i) = n - 1; (i) >= 0; (i)--) #define For(i, a, b) for (ll(i) = (a); (i) < (b); (i)++) #define revFor(i, b, a) for (ll(i) = (b)-1; (i) >= (a); (i)--) #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define isUpper(c) ('a' - c > 0) #define isNum(c) (0 <= (c) - '0' && (c) - '0' <= 9) #define toLower(c) char((c) + 0x20) #define toUpper(c) char((c)-0x20) #define pb push_back #define mp make_pair #define mt make_tuple #define pr(a) 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 prs2(a, b) cout << (a) << " " << (b) << " " #define prs3(a, b, c) cout << (a) << " " << (b) << " " << (c) << " " #define prs4(a, b, c, d) \ cout << (a) << " " << (b) << " " << (c) << " " << (d) << " " #define yn(condition) \ if ((condition)) \ prl("Yes"); \ else \ prl("No"); #define YN(condition) \ if ((condition)) \ prl("YES"); \ else \ prl("NO"); #define in1(a) cin >> (a) #define in2(a, b) cin >> (a) >> (b) #define in3(a, b, c) cin >> (a) >> (b) >> (c) #define in4(a, b, c, d) cin >> (a) >> (b) >> (c) >> (d) #define in5(a, b, c, d, e) cin >> (a) >> (b) >> (c) >> (d) >> (e) #define in6(a, b, c, d, e, f) cin >> (a) >> (b) >> (c) >> (d) >> (e) >> (f) #define in7(a, b, c, d, e, f, g) \ cin >> (a) >> (b) >> (c) >> (d) >> (e) >> (f) >> (g) #define e1 first #define e2 second #define 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 DEBUG(x) cerr << #x << ": " << (x) << endl #define DEBUG_VEC(v) \ cerr << #v << ": "; \ rep(__i, (v).size()) cerr << ((v)[__i]) << ", "; \ cerr << endl #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } struct MaxFlow { struct F_edge { ll to, rev, capacity; F_edge(ll to, ll rev, ll capacity) : to(to), rev(rev), capacity(capacity) {} }; typedef vector<F_edge> F_edges; vector<F_edges> graph; ll n_vertex; // level is the shortest path to get a given node from the source node. vl level, iter; MaxFlow(ll n_vertex) : n_vertex(n_vertex) { graph.resize(n_vertex); } void add_edge(ll from, ll to, ll capacity) { graph[from].pb({to, ll(graph[to].size()), capacity}); graph[to].pb({from, ll(graph[from].size()) - 1, 0}); } void bfs(ll source) { level = vl(n_vertex, -1); level[source] = 0; queue<ll> q; q.push(source); while (!q.empty()) { ll vertex = q.front(); q.pop(); rep(i, graph[vertex].size()) { ll target = graph[vertex][i].to; ll cap_target = graph[vertex][i].capacity; // if the flow can be into the target node, implement below. if (cap_target > 0 && level[target] < 0) { level[target] = level[vertex] + 1; q.push(target); } } } } ll dfs(ll vertex, ll sink, ll flow) { if (vertex == sink) return flow; for (ll &i = iter[vertex]; i < graph[vertex].size(); i++) { ll target = graph[vertex][i].to; ll cap_target = graph[vertex][i].capacity; ll rev_target = graph[vertex][i].rev; // if capasitiy is not full yet and target is farther, // then assign current flow if (cap_target > 0 && level[vertex] < level[target]) { ll d = dfs(target, sink, min(cap_target, flow)); if (d > 0) { // if the flow successfully reaches the sink, reduce the // flow from the capacity graph[vertex][i].capacity -= d; graph[target][rev_target].capacity += d; return d; } } } return 0; } ll dinic(ll source, ll sink) { // complexity O(EV^2) ll flow = 0; while (true) { bfs(source); // if there is no path leading to the sink, the maximum flow is 0. if (level[sink] < 0) return flow; iter = vl(n_vertex, 0); ll f; while ((f = dfs(source, sink, INF)) > 0) flow += f; } } }; class UnionFind { vl parents, set_size; ll n_groups; public: UnionFind() {} UnionFind(ll n) { parents = set_size = vl(n); n_groups = 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) { // priority for x is larger than that of y x = root_find(x); y = root_find(y); if (x == y) return; parents[y] = x, set_size[x] += set_size[y]; n_groups--; } 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)]; } ll union_count() const { return n_groups; } }; struct Doubling { // ABC167D ll n; ll sz; vl2 next; /* next[k + 1][i] := next[k][next[k][i]] next[0][i] := edge[i] e.g. a0, a1, ..., an-1 / 0 <= ai <= n - 1 a0 -> a[a0] -> a[a[a0]] -> ... -> a[a[...[a[0]]]] (m times) Let the function repeatedly input a[i] m times be f[m](a[i]) - get(i, x) returns f[x](a[i]) - lower_bound(i, j) returns minimum x which satisfies f[x](a[i]) >= j. if not possible returns n. */ // edge[i]: the step size for one iteration Doubling(vl &edge) : n(edge.size()), sz(62) { next.resize(sz, vl(n, -1)); rep(i, n) next[0][i] = edge[i]; rep(k, sz - 1) rep(i, n) next[k + 1][i] = next[k][next[k][i]]; } ll get(ll i, ll x) { ll ret = i; rep(bit, sz) { if (!(x >> bit & 1)) continue; ret = next[bit][ret]; } return ret; } ll lower_bound(ll i, ll j) { ll cur = i, acc = 0; revrep(wid, sz) { if (next[wid][cur] < j) { acc += 1LL << wid; cur = next[wid][cur]; } } return min(n, acc + 1); } }; class LowestCommonAncestor { public: ll N, logN; vl depth, len; Graph tree; vl2 parents; LowestCommonAncestor(ll n, ll offset = -1, bool is_weighted = true) { N = n; logN = 0; while (N > (1LL << logN)) logN++; depth = vl(N); len = vl(N); parents = vl2(logN, vl(N)); tree = Graph(N); input(N, offset, is_weighted); init(0, -1, 0, 0); build(); } void add_edge(ll from, ll to, ll dist) { tree[from].pb({to, dist}); tree[to].pb({from, dist}); } void input(ll n, ll offset = -1, bool is_weighted = true) { rep(i, n - 1) { ll a, b, d = 1; in2(a, b); a += offset, b += offset; if (is_weighted) in1(d); add_edge(a, b, d); } } void init(ll source, ll parent, ll d, ll l) { depth[source] = d; parents[0][source] = parent; len[source] = l; rep(i, tree[source].size()) { ll target = tree[source][i].to; ll cost = tree[source][i].cost; if (target == parent) continue; init(target, source, d + 1, cost + l); } } void build() { rep(k, logN - 1) rep(n, N) { // if there is no parent, -1. // otherwise, the parent of the parent is the parent. if (parents[k][n] < 0) parents[k + 1][n] = -1; else parents[k + 1][n] = parents[k][parents[k][n]]; } } ll query(ll u, ll v) { if (depth[u] > depth[v]) swap(u, v); rep(k, logN) if ((depth[v] - depth[u]) >> k & 1) v = parents[k][v]; if (u == v) return u; revrep(k, logN) { if (parents[k][u] != parents[k][v]) { u = parents[k][u]; v = parents[k][v]; } } return parents[0][u]; } ll distance(ll u, ll v) { ll w = query(u, v); return len[u] + len[v] - 2 * len[w]; } }; struct BinaryIndexedTree { ll n, ini; vl dat; BinaryIndexedTree(ll n, ll ini = 0) : dat(n + 1, ini), n(n), ini(ini){}; // x: 1001 1010 1100 1011 1101 1111 // x & - x: 0001 0010 0100 0001 0001 0001 // ->: 1010 1100 10000 1100 1100 10000 ll update_func(ll val, ll d) { // if maximum -> max(val, dat) // return max(val, dat); // if cumulative sum return val + d; } ll query(ll i) { /* v[0] + v[1] + ... + v[i] e.g.) i = 10101 itr1. 10101 -> 10100 itr2. 10100 -> 10000 itr3. 10000 -> 00000 (break) */ if (i < 0) return ini; ll ret = 0; for (ll j = i; j >= 0; j = (j & (j + 1)) - 1) { ret = update_func(ret, dat[j]); } return ret; } ll query(ll l, ll r) { // a[l] + a[l + 1] + ... + a[r - 1] + a[r] return query(r) - query(l - 1); } ll lower_bound(ll key) { // v[0] + v[1] + ... + v[left - 1] < key <= v[0] + v[1] + ... + v[left] if (key <= 0) return 0; ll left = 0, right = 1; while (right <= n) right *= 2; for (ll i = right; i > 0; i /= 2) { if (left + i <= n && dat[left + i - 1] < key) { key -= dat[left + i - 1]; left += i; } } return left; } void update(ll i, ll val) { /* e.g.) i = 10101, n = 11111 itr1. i: 10101, i+1: 10110 -> 10111 itr2. i: 10111, i+1: 11000 -> 11111 (break) */ if (i < 0) return; for (ll j = i; j < n; j |= j + 1) { dat[j] = update_func(val, dat[j]); } } }; struct SegmentTree { ll n, ini, minimize; vl dat; // when seeking minimum // ini = INF // when seeking maximum // ini = -INF SegmentTree(ll n_, bool minimize_ = true) { n = 1; minimize = minimize_; if (minimize) ini = INF; else ini = -INF; while (n < n_) n *= 2; dat.resize(2 * n - 1); rep(i, 2 * n - 1) dat[i] = ini; }; void update(ll idx, ll val) { idx += n - 1; if (minimize && dat[idx] <= val) return; if (!minimize && dat[idx] >= val) return; dat[idx] = val; while (idx > 0) { idx = (idx - 1) / 2; // when seeking minimum if (minimize) dat[idx] = min(dat[idx * 2 + 1], dat[idx * 2 + 2]); // when seeking maximum else dat[idx] = max(dat[idx * 2 + 1], dat[idx * 2 + 2]); } } ll query(ll l, ll r) { // ### NOTE ### // the range is [l, r] // l, l + 1, ..., r r++; // to adjust to this method return query_segment(l, r, 0, 0, n); } ll query_segment(ll a, ll b, ll idx, ll l, ll r) { assert(a < b); if (r <= a || b <= l) return ini; if (a <= l && r <= b) return dat[idx]; else { ll seg1 = query_segment(a, b, idx * 2 + 1, l, (l + r) / 2); ll seg2 = query_segment(a, b, idx * 2 + 2, (l + r) / 2, r); // when seeking minimum if (minimize) return min(seg1, seg2); // when seeking maximum else return max(seg1, seg2); } } }; template <class Target> class RerootingTreeDP { public: using T = typename Target::type; struct DP_edge { ll to, rev; // rev is the index to trace the source node. T value; // objective value }; private: ll n; void dfs_fwd(ll source, ll parent) { ll par_idx = -1; vector<T> values; rep(i, tree[source].size()) { const DP_edge &e = tree[source][i]; if (e.to == parent) { par_idx = i; continue; } dfs_fwd(e.to, source); values.pb(e.value); } // If the parent != -1, update the value on edge from parent to source if (par_idx != -1) { ll src_idx = tree[source][par_idx].rev; // update values on the edge from parent to source tree[parent][src_idx].value = Target::merge(values); } } void dfs_bwd(ll source, ll parent) { vector<T> values; for (auto &&e : tree[source]) values.pb(e.value); values = Target::evaluate(values); rep(i, tree[source].size()) { const DP_edge &e = tree[source][i]; if (e.to == parent) continue; // tree[e.to][e.rev]: e.to -> source tree[e.to][e.rev].value = values[i]; dfs_bwd(e.to, source); } } public: UnionFind uf; vector<vector<DP_edge>> tree; RerootingTreeDP(ll n) : n(n), uf(n), tree(n) {} void add_edge(ll u, ll v, T val) { assert(!uf.is_same(u, v)); tree[u].pb({v, ll(tree[v].size()), val}); tree[v].pb({u, ll(tree[u].size()) - 1, val}); uf.unite(u, v); } void dp() { vb visited(n, false); rep(i, n) { if (visited[uf.root_find(i)]) continue; dfs_fwd(i, -1); visited[uf.root_find(i)] = true; } visited.assign(n, false); rep(i, n) { if (visited[uf.root_find(i)]) continue; dfs_bwd(i, -1); visited[uf.root_find(i)] = true; } } ll size() const { return tree.size(); } }; // ABC160F is one example // Modify the functions evaluate and merge based on given problems struct Merger { using type = ll; static type merge(const vector<type> &value) { // merge the result below the source node // each value is from each child node of the source node // value[i] := f(child i) // Here, we would like to obtain f(source) using f(child i) (i = 0, 1, ..., // n_children) ll ret = 1; for (auto &&v : value) ret += v; return ret; } static vector<type> evaluate(const vector<type> &value) { // value[i] := f(source -> child i) // we would like to obtain f(child i -> source) // child j (j != i) is the grandchildren of child i // represent f(child i -> source) using f(source -> j) (j != i) // L[i + 1] := the result using f(source -> k) (k = 0, 1, ..., i) // R[i] := the result using f(source -> k) (k = i, i + 1, ..., n_children) const ll n_children = value.size(); vl L(n_children + 1, 0), R(n_children + 1, 0); rep(i, n_children) L[i + 1] = L[i] + value[i]; revrep(i, n_children) R[i] = R[i + 1] + value[i]; vl ret(n_children); rep(i, n_children) ret[i] = L[i] + R[i + 1] + 1; return ret; } }; struct StronglyConnectedComponents { ll n, n_cmp; vl2 graph, graph_rev, dag, cmp; vl order, visited, cmp_idx; StronglyConnectedComponents() {} StronglyConnectedComponents(ll sz) : n(sz), graph(sz), graph_rev(sz), visited(sz), cmp_idx(sz) {} void add_edge(ll from, ll to) { graph[from].pb(to); graph_rev[to].pb(from); } void input(ll m, ll offset = -1) { ll a, b; rep(i, m) { in2(a, b); add_edge(a + offset, b + offset); } } ll operator[](ll k) { return cmp_idx[k]; } void dfs_fwd(ll source) { visited[source] = 1; rep(i, graph[source].size()) { ll target = graph[source][i]; if (!visited[target]) dfs_fwd(target); } order.pb(source); } void dfs_bwd(ll source, ll num) { visited[source] = 1, cmp_idx[source] = num; cmp[num].pb(source); rep(i, graph_rev[source].size()) { ll target = graph_rev[source][i]; if (!visited[target]) dfs_bwd(target, num); } } ll build() { fill(all(visited), 0); order.clear(); rep(i, n) if (!visited[i]) dfs_fwd(i); fill(all(visited), 0); ll num = 0; revrep(i, order.size()) { if (!visited[order[i]]) { dag.pb(vl()); cmp.pb(vl()); dfs_bwd(order[i], num++); } } rep(i, n) for (ll to : graph[i]) if (cmp_idx[i] != cmp_idx[to]) dag[cmp_idx[i]] .pb(cmp_idx[to]); rep(i, num) { sort(all(dag[i])); dag[i].erase(unique(all(dag[i])), dag[i].end()); } return n_cmp = num; } }; struct CombinationMemo { ll sz, mod; vl facts, facts_inv, minv; CombinationMemo(ll sz, ll _mod) : sz(sz), mod(_mod) { facts.resize(sz + 5); facts_inv.resize(sz + 5); minv.resize(sz + 5); init(); } void init() { facts[0] = facts[1] = 1; minv[1] = 1; facts_inv[0] = facts_inv[1] = 1; For(i, 2, sz + 3) { facts[i] = (i * facts[i - 1]) % mod; minv[i] = mod - minv[mod % i] * (mod / i) % mod; facts_inv[i] = facts_inv[i - 1] * minv[i] % mod; } } ll nCk(ll n, ll r) { if (n == r && n == 0) return 1; else if (n <= 0 || r < 0 || r > n) return 0; ll val = (facts[n] * facts_inv[n - r]) % mod; val *= facts_inv[r]; return val % mod; } }; struct Grid2D { Graph graph; ll Width, Height; Grid2D(ll w, ll h) : Width(w), Height(h) { graph.resize(w * h); } ll pos_to_idx(point p) { return p.y * Width + p.x; } point idx_to_pos(ll idx) { return {idx % Width, idx / Width}; } bool undefined_region(point p, vb2 &block) { if (p.x < 0 || p.x > Width - 1) return true; if (p.y < 0 || p.y > Height - 1) return true; if (block[p.x][p.y]) return true; return false; } void build(vb2 &block, ll val = 1) { rep(x, Width) rep(y, Height) { point p = {x, y}; ll idx1 = pos_to_idx(p); ll idx2; if (block[x][y]) continue; rep(i, 4) { point d = dirs[i], nxt = {x + d.x, y + d.y}; idx2 = pos_to_idx(nxt); if (!undefined_region(nxt, block)) graph[idx1].pb( {idx2, val}); // dist[idx1][idx2] = val; (warshall-floyd) } } } }; 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; } vl2 pascal_triangle(ll n) { /* Complexity: O(n^2) The upper bound of n is nearly 50. Parameters ---------- n; the size of returned vector Returns ------- comb[i][j]: combination(i, j). 0 <= i <= n, 0 <= j <= i */ vl2 comb(n, vl(n)); comb[0][0] = 1; For(i, 1, n + 1) rep(j, i + 1) { comb[i][j] += comb[i - 1][j]; if (j > 0) comb[i][j] += comb[i - 1][j - 1]; } return comb; } 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; } 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; } /* // vs[lb - 1] < key <= vs[lb] lb = lower_bound(all(vs), key); // vs[ub - 1] <= key < vs[lb] ub = upper_bound(all(vs), key); 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; } */ str bin_expression(ll n, ll dig) { str s = ""; rep(i, dig) { s += ltos(n % 2); n /= 2; } reverse(all(s)); return s; } 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; } 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 next_combination(ll sub) { /* ### Attention ### if k is 0 or 1 and n is 0, it does not work well. ll n, k; ll bit = (1 << k) - 1; for (; bit < (1 << n); bit = next_combination(bit)){ bool ith = bit & (1 << i); procedures... } sub & -sub: the binary which shares the last digit whose value is 1 in sub sub + x : carry up the last digit ~y : the binary whose digits are 1 if y's digit is 0. (sub & ~y) / x: reduce the same number of 0s after first 1 in x from (sub & ~y). */ ll x = sub & -sub, y = sub + x; return (((sub & ~y) / x) >> 1) | y; } // just change the input if you want to change the target. // If you want to check the common sequences in two strings, // combine them. e.g. ABC150F vl z_algorithm(str s) { /* Paramters --------- str: the string of interest Returns ------- res[i] is the maximum number of K which satisfies s[:K] == s[i:i + K] for each i = 0, 1, 2, ..., n - 1. */ ll n = s.length(); vl res(n); res[0] = n; ll i1 = 1, i2 = 0; while (i1 < n) { /* i1: the starting point i2: the length of substring */ while (i1 + i2 < n && s[i2] == s[i1 + i2]) ++i2; res[i1] = i2; if (i2 == 0) { ++i1; continue; } ll i3 = 1; // update the already seen points while (i1 + i3 < n && i3 + res[i3] < i2) { res[i1 + i3] = res[i3]; ++i3; } // update up to i1 + i3 and the next possible minimum length is i2 - i3 (= // res[i3]) i1 += i3, i2 -= i3; } return res; } ll 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; } ll big_number_compare(str s1, str s2) { if (s1.length() > s2.length()) return 1; else if (s1.length() < s2.length()) return -1; else if (s1 == s2) return 0; return 2 * (s1 > s2) - 1; } 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) { // the center of circle on the given three points // return the determinant value and the product of center points and 2 * // determinant value ll x1 = p1.x, y1 = p1.y; ll x2 = p2.x, y2 = p2.y; ll x3 = p3.x, y3 = p3.y; // center of 2 points ll c2x_2 = x2 + x1, c2y_2 = y2 + y1; ll c3x_2 = x3 + x1, c3y_2 = y3 + y1; // vertical vector of 2 lines 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. */ BinaryIndexedTree bit(a_max + 1); ll val = 0; rep(i, a.size()) { // i is the number of elements that have lower index than a[i]. // call the number of elements that have lower value than a[i] // by subtracting these two, the residual number is the number of elements // that have larger value val += i - bit.query(a[i] - 1); // cumulative sum from 0 to a[i] - 1 bit.update(a[i], 1); } return val; } template <typename T> vector<T> compress(vector<T> v) { // sort and remove all the duplicated values sort(all(v)); v.erase(unique(all(v)), v.end()); return v; } template <typename T> map<T, ll> dict(const vector<T> &v) { map<T, ll> d; rep(i, v.size()) d[v[i]] = i; return d; } points compress2D(vl xs, vl ys) { /* NOTE ---- Add the corner points if required */ ll n = xs.size(); vl xcs = compress(xs), ycs = compress(ys); map<ll, ll> xd = dict(xcs), yd = dict(ycs); points ps(n); rep(i, n) xs[i] = xd[xs[i]], ys[i] = yd[ys[i]]; rep(i, n) ps[i] = {xs[i], ys[i]}; sort(all(ps)); return ps; } void GaussJordanBitVector(vl &bs) { ll n = bs.size(); ll rank = 0; ll j = 0; revrep(i, N_DIGITS) { for (j = rank; j < n; j++) if (bs[j] & (1LL << i)) break; if (j == n) continue; if (j > rank) bs[rank] ^= bs[j]; for (j = rank + 1; j < n; j++) bs[j] = min(bs[j], bs[j] ^ bs[rank]); rank++; } } ll kruskal(vector<undirected_edge> &es, ll n_vertex) { sort(all(es)); UnionFind uf(n_vertex); ll min_cost = 0; rep(i, es.size()) { undirected_edge &e = es[i]; if (!uf.is_same(e.from, e.to)) { min_cost += e.cost; uf.unite(e.from, e.to); } } return min_cost; } ll LongestIncreasedSequence(vl &v, ll n) { vl dp(n, INF); rep(i, n) * lower_bound(all(dp), v[i]) = v[i]; return lower_bound(all(dp), INF) - dp.begin(); } void dijkstra(ll start, Graph &graph, vl &dist, vl &vertex_pre, bool trace = false) { priority_queue<pl, vpl, greater<pl>> edge_costs; ll n = graph.size(); dist = vl(n, INF); if (trace) vertex_pre = vl(n, -1); dist[start] = 0; edge_costs.push(pl(0, start)); while (!edge_costs.empty()) { ll idx, cost; tie(cost, idx) = edge_costs.top(); edge_costs.pop(); if (dist[idx] < cost) continue; for (auto e : graph[idx]) { if (dist[e.to] > dist[idx] + e.cost) { dist[e.to] = dist[idx] + e.cost; if (trace) vertex_pre[e.to] = idx; edge_costs.push(pl(dist[e.to], e.to)); } } } } vl get_predecessor(ll g, vl &vertex_pre) { vl path; for (; g != -1; g = vertex_pre[g]) path.pb(g); reverse(all(path)); return path; } void warshall_floyd(vl2 &dist) { ll n = dist.size(); // Dont forget the initialization // rep(i, n) rep(j, n) dist[i][j] = INF * (i != j); rep(k, n) rep(i, n) rep(j, n) dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); } // ABC061D bool find_negative_cycle(ll n, ll goal, Graph &graph, vl &dist) { rep(i, n) rep(v, n) rep(k, graph[v].size()) { edge e = graph[v][k]; if (dist[e.to] != INF && dist[e.to] > dist[v] + e.cost) { dist[e.to] = -INF; if (goal == -1) return true; else if (goal == e.to) return true; } } return false; } bool bellman_ford(ll start, ll goal, Graph &graph, vl &dist) { // if there is a closed circuit, it returns false. (when goal == -1) // if the distance to goal cannot be obtained, it returns false (when goal != // -1) ll n = graph.size(); dist = vl(n, INF); dist[start] = 0; rep(i, n) rep(v, n) rep(k, graph[v].size()) { edge e = graph[v][k]; if (dist[v] != INF && dist[e.to] > dist[v] + e.cost) dist[e.to] = dist[v] + e.cost; } if (find_negative_cycle(n, goal, graph, dist)) return false; return true; } void read_graph(ll n_vertex, ll n_edges, Graph &graph, ll cost = -1, ll directed = false, ll offset = -1) { graph.resize(n_vertex); rep(i, n_edges) { ll from, to, c = cost; if (cost == -1) in3(from, to, c); else in2(from, to); from += offset, to += offset; graph[from].pb({to, cost}); if (!directed) graph[to].pb({from, cost}); } } void read_vector(ll n, vl &v, ll offset = 0) { v.resize(n); rep(i, n) { in1(v[i]); v[i] += offset; } } /* diameter of tree Graph tree; ll dM = 0, vM = 0, v2 = 0; void dfs1(ll source, ll parent, ll d){ if (d > dM) dM = d, vM = source; rep(i, tree[source].size()){ ll target = tree[source][i].to; if (target == parent) continue; dfs1(target, source, d + 1); } } void dfs2(ll source, ll parent, ll d){ if (dM <= d) dM = d, v2 = source; rep(i, tree[source].size()){ ll target = tree[source][i].to; if (target == parent) continue; dfs2(target, source, d + 1); } } dfs(0, -1, 0); dfs2(vM, -1, 0); prl2(vM + 1, v2 + 1); // the two edges of tree */ /* # 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)); # 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, two pointers technique) // 1. strech right side while the condition is met. // 2. renew the answer // 3. increments left side // 4. Back to 1. (l <= r must be satisfied all the time.) ll l = 0; ll r = 0; while (l < n){ r = max(r, l); if (l == r) r++; while(r < n && cond) r++; answer += r - l; l++; } prl(answer); #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 = graph[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 = graph[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); } # Cumulative sum (2 dimension) ll func(ll x, ll y, ll dx, ll dy){ if (x + dx > w || y + dy > h) return - INF; ll val = cum[x + dx][y + dy]; val += cum[x][y]; val -= cum[x][y + dy]; val -= cum[x + dx][y]; return val; } rep(x, w + 1) cum[x][0] = 0; rep(y, h + 1) cum[0][y] = 0; rep(y, h) rep(x, w) cum[x + 1][y + 1] = cum[x][y + 1] + vs[x][y]; rep(x, w + 1) rep(y, h) cum[x][y + 1] += cum[x][y]; */ /* # 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 ####### Attention ####### 1 << i and 1LL << i is different. If programs show WA, try switch to this one. 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 __builtin_popcountl(i) -> the number of 1 in binary 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 # inclusion-exclusion principle |U[i = 1 to n] Ai| = sum[i = 1 to n] |Ai| - sum[i < j]|Ai ^ Aj| + ... + (-1)^(n - 1) |^[i = 1 to n]Ai| */ const ll MAX_N = 200005; bool okay = false; ll answer = 0; str s; ll n, m, l, q; void dijkstra2(ll start, Graph &graph, vl &dist) { priority_queue<tp3, vtp3, greater<tp3>> edge_costs; ll n = graph.size(); dist = vl(n, INF); dist[start] = 0; edge_costs.push(mt(0, -l, start)); vl oils(n, 0); while (!edge_costs.empty()) { ll idx, cost, oil; tie(cost, oil, idx) = edge_costs.top(); oil *= -1; edge_costs.pop(); if (dist[idx] < cost) continue; for (auto e : graph[idx]) { if (dist[e.to] > dist[idx] && oil >= e.cost) { dist[e.to] = dist[idx]; oils[e.to] = oil - e.cost; edge_costs.push(mt(dist[e.to], -oils[e.to], e.to)); } else if (dist[e.to] == dist[idx] && oil >= e.cost && oils[e.to] < oil - e.cost) { oils[e.to] = oil - e.cost; edge_costs.push(mt(dist[e.to], -oils[e.to], e.to)); } else if (dist[e.to] >= dist[idx] + 1) { dist[e.to] = dist[idx] + 1; oils[e.to] = l - e.cost; edge_costs.push(mt(dist[e.to], -oils[e.to], e.to)); } } } } void solve() { Graph graph(n); rep(i, m) { ll a, b, c; in3(a, b, c); a--, b--; if (c > l) continue; graph[a].pb({b, c}); graph[b].pb({a, c}); } vl2 dist(n); rep(i, n) dijkstra2(i, graph, dist[i]); in1(q); rep(i, q) { ll s1, s2; in2(s1, s2); s1--, s2--; if (dist[s1][s2] == INF) prl(-1); else prl(dist[s1][s2]); } } int main(void) { in3(n, m, l); solve(); // assert(n <= 3000); // 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; }; struct point { ll x; ll y; bool operator<(const point &p) const { if (x == p.x) return y < p.y; return x < p.x; } }; struct undirected_edge { ll from; ll to; ll cost; bool operator<(const undirected_edge &ue) const { return cost < ue.cost; } }; typedef string str; typedef std::pair<ll, ll> pl; typedef std::tuple<ll, ll, ll> tp3; typedef std::tuple<ll, ll, ll, ll> tp4; 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<vl> vl2; typedef std::vector<vl2> vl3; typedef std::vector<vl3> vl4; typedef std::vector<bool> vb; typedef std::vector<vb> vb2; typedef std::vector<vb2> vb3; typedef std::vector<vb3> vb4; typedef std::vector<pl> vpl; typedef std::vector<tp3> vtp3; typedef std::vector<tp4> vtp4; typedef std::vector<point> points; // priority queue. Taking from the higher value. Don't forget calling !q.empty() typedef std::priority_queue<ll> pq; // priority queue. Taking from the lower value typedef std::priority_queue<ll, vl, greater<ll>> pql; typedef std::vector<vector<edge>> Graph; const ll N_DIGITS = 60; const 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; points dirs = { {-1, 0}, {1, 0}, {0, 1}, {0, -1}, // four directions {1, 1}, {-1, 1}, {-1, -1}, {1, -1}, // diagonal {0, 0} // self }; template <typename A, typename B> string to_string(pair<A, B> p); template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p); template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p); string to_string(const string &s) { return '"' + s + '"'; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } string to_string(vector<bool> v) { bool first = true; string res = "{"; for (int i = 0; i < static_cast<int>(v.size()); i++) { if (!first) { res += ", "; } first = false; res += to_string(v[i]); } res += "}"; return res; } template <size_t N> string to_string(bitset<N> v) { string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; } template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #define rep(i, n) for (ll(i) = 0; (i) < (n); (i)++) #define revrep(i, n) for (ll(i) = n - 1; (i) >= 0; (i)--) #define For(i, a, b) for (ll(i) = (a); (i) < (b); (i)++) #define revFor(i, b, a) for (ll(i) = (b)-1; (i) >= (a); (i)--) #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define isUpper(c) ('a' - c > 0) #define isNum(c) (0 <= (c) - '0' && (c) - '0' <= 9) #define toLower(c) char((c) + 0x20) #define toUpper(c) char((c)-0x20) #define pb push_back #define mp make_pair #define mt make_tuple #define pr(a) 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 prs2(a, b) cout << (a) << " " << (b) << " " #define prs3(a, b, c) cout << (a) << " " << (b) << " " << (c) << " " #define prs4(a, b, c, d) \ cout << (a) << " " << (b) << " " << (c) << " " << (d) << " " #define yn(condition) \ if ((condition)) \ prl("Yes"); \ else \ prl("No"); #define YN(condition) \ if ((condition)) \ prl("YES"); \ else \ prl("NO"); #define in1(a) cin >> (a) #define in2(a, b) cin >> (a) >> (b) #define in3(a, b, c) cin >> (a) >> (b) >> (c) #define in4(a, b, c, d) cin >> (a) >> (b) >> (c) >> (d) #define in5(a, b, c, d, e) cin >> (a) >> (b) >> (c) >> (d) >> (e) #define in6(a, b, c, d, e, f) cin >> (a) >> (b) >> (c) >> (d) >> (e) >> (f) #define in7(a, b, c, d, e, f, g) \ cin >> (a) >> (b) >> (c) >> (d) >> (e) >> (f) >> (g) #define e1 first #define e2 second #define 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 DEBUG(x) cerr << #x << ": " << (x) << endl #define DEBUG_VEC(v) \ cerr << #v << ": "; \ rep(__i, (v).size()) cerr << ((v)[__i]) << ", "; \ cerr << endl #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } struct MaxFlow { struct F_edge { ll to, rev, capacity; F_edge(ll to, ll rev, ll capacity) : to(to), rev(rev), capacity(capacity) {} }; typedef vector<F_edge> F_edges; vector<F_edges> graph; ll n_vertex; // level is the shortest path to get a given node from the source node. vl level, iter; MaxFlow(ll n_vertex) : n_vertex(n_vertex) { graph.resize(n_vertex); } void add_edge(ll from, ll to, ll capacity) { graph[from].pb({to, ll(graph[to].size()), capacity}); graph[to].pb({from, ll(graph[from].size()) - 1, 0}); } void bfs(ll source) { level = vl(n_vertex, -1); level[source] = 0; queue<ll> q; q.push(source); while (!q.empty()) { ll vertex = q.front(); q.pop(); rep(i, graph[vertex].size()) { ll target = graph[vertex][i].to; ll cap_target = graph[vertex][i].capacity; // if the flow can be into the target node, implement below. if (cap_target > 0 && level[target] < 0) { level[target] = level[vertex] + 1; q.push(target); } } } } ll dfs(ll vertex, ll sink, ll flow) { if (vertex == sink) return flow; for (ll &i = iter[vertex]; i < graph[vertex].size(); i++) { ll target = graph[vertex][i].to; ll cap_target = graph[vertex][i].capacity; ll rev_target = graph[vertex][i].rev; // if capasitiy is not full yet and target is farther, // then assign current flow if (cap_target > 0 && level[vertex] < level[target]) { ll d = dfs(target, sink, min(cap_target, flow)); if (d > 0) { // if the flow successfully reaches the sink, reduce the // flow from the capacity graph[vertex][i].capacity -= d; graph[target][rev_target].capacity += d; return d; } } } return 0; } ll dinic(ll source, ll sink) { // complexity O(EV^2) ll flow = 0; while (true) { bfs(source); // if there is no path leading to the sink, the maximum flow is 0. if (level[sink] < 0) return flow; iter = vl(n_vertex, 0); ll f; while ((f = dfs(source, sink, INF)) > 0) flow += f; } } }; class UnionFind { vl parents, set_size; ll n_groups; public: UnionFind() {} UnionFind(ll n) { parents = set_size = vl(n); n_groups = 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) { // priority for x is larger than that of y x = root_find(x); y = root_find(y); if (x == y) return; parents[y] = x, set_size[x] += set_size[y]; n_groups--; } 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)]; } ll union_count() const { return n_groups; } }; struct Doubling { // ABC167D ll n; ll sz; vl2 next; /* next[k + 1][i] := next[k][next[k][i]] next[0][i] := edge[i] e.g. a0, a1, ..., an-1 / 0 <= ai <= n - 1 a0 -> a[a0] -> a[a[a0]] -> ... -> a[a[...[a[0]]]] (m times) Let the function repeatedly input a[i] m times be f[m](a[i]) - get(i, x) returns f[x](a[i]) - lower_bound(i, j) returns minimum x which satisfies f[x](a[i]) >= j. if not possible returns n. */ // edge[i]: the step size for one iteration Doubling(vl &edge) : n(edge.size()), sz(62) { next.resize(sz, vl(n, -1)); rep(i, n) next[0][i] = edge[i]; rep(k, sz - 1) rep(i, n) next[k + 1][i] = next[k][next[k][i]]; } ll get(ll i, ll x) { ll ret = i; rep(bit, sz) { if (!(x >> bit & 1)) continue; ret = next[bit][ret]; } return ret; } ll lower_bound(ll i, ll j) { ll cur = i, acc = 0; revrep(wid, sz) { if (next[wid][cur] < j) { acc += 1LL << wid; cur = next[wid][cur]; } } return min(n, acc + 1); } }; class LowestCommonAncestor { public: ll N, logN; vl depth, len; Graph tree; vl2 parents; LowestCommonAncestor(ll n, ll offset = -1, bool is_weighted = true) { N = n; logN = 0; while (N > (1LL << logN)) logN++; depth = vl(N); len = vl(N); parents = vl2(logN, vl(N)); tree = Graph(N); input(N, offset, is_weighted); init(0, -1, 0, 0); build(); } void add_edge(ll from, ll to, ll dist) { tree[from].pb({to, dist}); tree[to].pb({from, dist}); } void input(ll n, ll offset = -1, bool is_weighted = true) { rep(i, n - 1) { ll a, b, d = 1; in2(a, b); a += offset, b += offset; if (is_weighted) in1(d); add_edge(a, b, d); } } void init(ll source, ll parent, ll d, ll l) { depth[source] = d; parents[0][source] = parent; len[source] = l; rep(i, tree[source].size()) { ll target = tree[source][i].to; ll cost = tree[source][i].cost; if (target == parent) continue; init(target, source, d + 1, cost + l); } } void build() { rep(k, logN - 1) rep(n, N) { // if there is no parent, -1. // otherwise, the parent of the parent is the parent. if (parents[k][n] < 0) parents[k + 1][n] = -1; else parents[k + 1][n] = parents[k][parents[k][n]]; } } ll query(ll u, ll v) { if (depth[u] > depth[v]) swap(u, v); rep(k, logN) if ((depth[v] - depth[u]) >> k & 1) v = parents[k][v]; if (u == v) return u; revrep(k, logN) { if (parents[k][u] != parents[k][v]) { u = parents[k][u]; v = parents[k][v]; } } return parents[0][u]; } ll distance(ll u, ll v) { ll w = query(u, v); return len[u] + len[v] - 2 * len[w]; } }; struct BinaryIndexedTree { ll n, ini; vl dat; BinaryIndexedTree(ll n, ll ini = 0) : dat(n + 1, ini), n(n), ini(ini){}; // x: 1001 1010 1100 1011 1101 1111 // x & - x: 0001 0010 0100 0001 0001 0001 // ->: 1010 1100 10000 1100 1100 10000 ll update_func(ll val, ll d) { // if maximum -> max(val, dat) // return max(val, dat); // if cumulative sum return val + d; } ll query(ll i) { /* v[0] + v[1] + ... + v[i] e.g.) i = 10101 itr1. 10101 -> 10100 itr2. 10100 -> 10000 itr3. 10000 -> 00000 (break) */ if (i < 0) return ini; ll ret = 0; for (ll j = i; j >= 0; j = (j & (j + 1)) - 1) { ret = update_func(ret, dat[j]); } return ret; } ll query(ll l, ll r) { // a[l] + a[l + 1] + ... + a[r - 1] + a[r] return query(r) - query(l - 1); } ll lower_bound(ll key) { // v[0] + v[1] + ... + v[left - 1] < key <= v[0] + v[1] + ... + v[left] if (key <= 0) return 0; ll left = 0, right = 1; while (right <= n) right *= 2; for (ll i = right; i > 0; i /= 2) { if (left + i <= n && dat[left + i - 1] < key) { key -= dat[left + i - 1]; left += i; } } return left; } void update(ll i, ll val) { /* e.g.) i = 10101, n = 11111 itr1. i: 10101, i+1: 10110 -> 10111 itr2. i: 10111, i+1: 11000 -> 11111 (break) */ if (i < 0) return; for (ll j = i; j < n; j |= j + 1) { dat[j] = update_func(val, dat[j]); } } }; struct SegmentTree { ll n, ini, minimize; vl dat; // when seeking minimum // ini = INF // when seeking maximum // ini = -INF SegmentTree(ll n_, bool minimize_ = true) { n = 1; minimize = minimize_; if (minimize) ini = INF; else ini = -INF; while (n < n_) n *= 2; dat.resize(2 * n - 1); rep(i, 2 * n - 1) dat[i] = ini; }; void update(ll idx, ll val) { idx += n - 1; if (minimize && dat[idx] <= val) return; if (!minimize && dat[idx] >= val) return; dat[idx] = val; while (idx > 0) { idx = (idx - 1) / 2; // when seeking minimum if (minimize) dat[idx] = min(dat[idx * 2 + 1], dat[idx * 2 + 2]); // when seeking maximum else dat[idx] = max(dat[idx * 2 + 1], dat[idx * 2 + 2]); } } ll query(ll l, ll r) { // ### NOTE ### // the range is [l, r] // l, l + 1, ..., r r++; // to adjust to this method return query_segment(l, r, 0, 0, n); } ll query_segment(ll a, ll b, ll idx, ll l, ll r) { assert(a < b); if (r <= a || b <= l) return ini; if (a <= l && r <= b) return dat[idx]; else { ll seg1 = query_segment(a, b, idx * 2 + 1, l, (l + r) / 2); ll seg2 = query_segment(a, b, idx * 2 + 2, (l + r) / 2, r); // when seeking minimum if (minimize) return min(seg1, seg2); // when seeking maximum else return max(seg1, seg2); } } }; template <class Target> class RerootingTreeDP { public: using T = typename Target::type; struct DP_edge { ll to, rev; // rev is the index to trace the source node. T value; // objective value }; private: ll n; void dfs_fwd(ll source, ll parent) { ll par_idx = -1; vector<T> values; rep(i, tree[source].size()) { const DP_edge &e = tree[source][i]; if (e.to == parent) { par_idx = i; continue; } dfs_fwd(e.to, source); values.pb(e.value); } // If the parent != -1, update the value on edge from parent to source if (par_idx != -1) { ll src_idx = tree[source][par_idx].rev; // update values on the edge from parent to source tree[parent][src_idx].value = Target::merge(values); } } void dfs_bwd(ll source, ll parent) { vector<T> values; for (auto &&e : tree[source]) values.pb(e.value); values = Target::evaluate(values); rep(i, tree[source].size()) { const DP_edge &e = tree[source][i]; if (e.to == parent) continue; // tree[e.to][e.rev]: e.to -> source tree[e.to][e.rev].value = values[i]; dfs_bwd(e.to, source); } } public: UnionFind uf; vector<vector<DP_edge>> tree; RerootingTreeDP(ll n) : n(n), uf(n), tree(n) {} void add_edge(ll u, ll v, T val) { assert(!uf.is_same(u, v)); tree[u].pb({v, ll(tree[v].size()), val}); tree[v].pb({u, ll(tree[u].size()) - 1, val}); uf.unite(u, v); } void dp() { vb visited(n, false); rep(i, n) { if (visited[uf.root_find(i)]) continue; dfs_fwd(i, -1); visited[uf.root_find(i)] = true; } visited.assign(n, false); rep(i, n) { if (visited[uf.root_find(i)]) continue; dfs_bwd(i, -1); visited[uf.root_find(i)] = true; } } ll size() const { return tree.size(); } }; // ABC160F is one example // Modify the functions evaluate and merge based on given problems struct Merger { using type = ll; static type merge(const vector<type> &value) { // merge the result below the source node // each value is from each child node of the source node // value[i] := f(child i) // Here, we would like to obtain f(source) using f(child i) (i = 0, 1, ..., // n_children) ll ret = 1; for (auto &&v : value) ret += v; return ret; } static vector<type> evaluate(const vector<type> &value) { // value[i] := f(source -> child i) // we would like to obtain f(child i -> source) // child j (j != i) is the grandchildren of child i // represent f(child i -> source) using f(source -> j) (j != i) // L[i + 1] := the result using f(source -> k) (k = 0, 1, ..., i) // R[i] := the result using f(source -> k) (k = i, i + 1, ..., n_children) const ll n_children = value.size(); vl L(n_children + 1, 0), R(n_children + 1, 0); rep(i, n_children) L[i + 1] = L[i] + value[i]; revrep(i, n_children) R[i] = R[i + 1] + value[i]; vl ret(n_children); rep(i, n_children) ret[i] = L[i] + R[i + 1] + 1; return ret; } }; struct StronglyConnectedComponents { ll n, n_cmp; vl2 graph, graph_rev, dag, cmp; vl order, visited, cmp_idx; StronglyConnectedComponents() {} StronglyConnectedComponents(ll sz) : n(sz), graph(sz), graph_rev(sz), visited(sz), cmp_idx(sz) {} void add_edge(ll from, ll to) { graph[from].pb(to); graph_rev[to].pb(from); } void input(ll m, ll offset = -1) { ll a, b; rep(i, m) { in2(a, b); add_edge(a + offset, b + offset); } } ll operator[](ll k) { return cmp_idx[k]; } void dfs_fwd(ll source) { visited[source] = 1; rep(i, graph[source].size()) { ll target = graph[source][i]; if (!visited[target]) dfs_fwd(target); } order.pb(source); } void dfs_bwd(ll source, ll num) { visited[source] = 1, cmp_idx[source] = num; cmp[num].pb(source); rep(i, graph_rev[source].size()) { ll target = graph_rev[source][i]; if (!visited[target]) dfs_bwd(target, num); } } ll build() { fill(all(visited), 0); order.clear(); rep(i, n) if (!visited[i]) dfs_fwd(i); fill(all(visited), 0); ll num = 0; revrep(i, order.size()) { if (!visited[order[i]]) { dag.pb(vl()); cmp.pb(vl()); dfs_bwd(order[i], num++); } } rep(i, n) for (ll to : graph[i]) if (cmp_idx[i] != cmp_idx[to]) dag[cmp_idx[i]] .pb(cmp_idx[to]); rep(i, num) { sort(all(dag[i])); dag[i].erase(unique(all(dag[i])), dag[i].end()); } return n_cmp = num; } }; struct CombinationMemo { ll sz, mod; vl facts, facts_inv, minv; CombinationMemo(ll sz, ll _mod) : sz(sz), mod(_mod) { facts.resize(sz + 5); facts_inv.resize(sz + 5); minv.resize(sz + 5); init(); } void init() { facts[0] = facts[1] = 1; minv[1] = 1; facts_inv[0] = facts_inv[1] = 1; For(i, 2, sz + 3) { facts[i] = (i * facts[i - 1]) % mod; minv[i] = mod - minv[mod % i] * (mod / i) % mod; facts_inv[i] = facts_inv[i - 1] * minv[i] % mod; } } ll nCk(ll n, ll r) { if (n == r && n == 0) return 1; else if (n <= 0 || r < 0 || r > n) return 0; ll val = (facts[n] * facts_inv[n - r]) % mod; val *= facts_inv[r]; return val % mod; } }; struct Grid2D { Graph graph; ll Width, Height; Grid2D(ll w, ll h) : Width(w), Height(h) { graph.resize(w * h); } ll pos_to_idx(point p) { return p.y * Width + p.x; } point idx_to_pos(ll idx) { return {idx % Width, idx / Width}; } bool undefined_region(point p, vb2 &block) { if (p.x < 0 || p.x > Width - 1) return true; if (p.y < 0 || p.y > Height - 1) return true; if (block[p.x][p.y]) return true; return false; } void build(vb2 &block, ll val = 1) { rep(x, Width) rep(y, Height) { point p = {x, y}; ll idx1 = pos_to_idx(p); ll idx2; if (block[x][y]) continue; rep(i, 4) { point d = dirs[i], nxt = {x + d.x, y + d.y}; idx2 = pos_to_idx(nxt); if (!undefined_region(nxt, block)) graph[idx1].pb( {idx2, val}); // dist[idx1][idx2] = val; (warshall-floyd) } } } }; 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; } vl2 pascal_triangle(ll n) { /* Complexity: O(n^2) The upper bound of n is nearly 50. Parameters ---------- n; the size of returned vector Returns ------- comb[i][j]: combination(i, j). 0 <= i <= n, 0 <= j <= i */ vl2 comb(n, vl(n)); comb[0][0] = 1; For(i, 1, n + 1) rep(j, i + 1) { comb[i][j] += comb[i - 1][j]; if (j > 0) comb[i][j] += comb[i - 1][j - 1]; } return comb; } 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; } 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; } /* // vs[lb - 1] < key <= vs[lb] lb = lower_bound(all(vs), key); // vs[ub - 1] <= key < vs[lb] ub = upper_bound(all(vs), key); 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; } */ str bin_expression(ll n, ll dig) { str s = ""; rep(i, dig) { s += ltos(n % 2); n /= 2; } reverse(all(s)); return s; } 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; } 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 next_combination(ll sub) { /* ### Attention ### if k is 0 or 1 and n is 0, it does not work well. ll n, k; ll bit = (1 << k) - 1; for (; bit < (1 << n); bit = next_combination(bit)){ bool ith = bit & (1 << i); procedures... } sub & -sub: the binary which shares the last digit whose value is 1 in sub sub + x : carry up the last digit ~y : the binary whose digits are 1 if y's digit is 0. (sub & ~y) / x: reduce the same number of 0s after first 1 in x from (sub & ~y). */ ll x = sub & -sub, y = sub + x; return (((sub & ~y) / x) >> 1) | y; } // just change the input if you want to change the target. // If you want to check the common sequences in two strings, // combine them. e.g. ABC150F vl z_algorithm(str s) { /* Paramters --------- str: the string of interest Returns ------- res[i] is the maximum number of K which satisfies s[:K] == s[i:i + K] for each i = 0, 1, 2, ..., n - 1. */ ll n = s.length(); vl res(n); res[0] = n; ll i1 = 1, i2 = 0; while (i1 < n) { /* i1: the starting point i2: the length of substring */ while (i1 + i2 < n && s[i2] == s[i1 + i2]) ++i2; res[i1] = i2; if (i2 == 0) { ++i1; continue; } ll i3 = 1; // update the already seen points while (i1 + i3 < n && i3 + res[i3] < i2) { res[i1 + i3] = res[i3]; ++i3; } // update up to i1 + i3 and the next possible minimum length is i2 - i3 (= // res[i3]) i1 += i3, i2 -= i3; } return res; } ll 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; } ll big_number_compare(str s1, str s2) { if (s1.length() > s2.length()) return 1; else if (s1.length() < s2.length()) return -1; else if (s1 == s2) return 0; return 2 * (s1 > s2) - 1; } 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) { // the center of circle on the given three points // return the determinant value and the product of center points and 2 * // determinant value ll x1 = p1.x, y1 = p1.y; ll x2 = p2.x, y2 = p2.y; ll x3 = p3.x, y3 = p3.y; // center of 2 points ll c2x_2 = x2 + x1, c2y_2 = y2 + y1; ll c3x_2 = x3 + x1, c3y_2 = y3 + y1; // vertical vector of 2 lines 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. */ BinaryIndexedTree bit(a_max + 1); ll val = 0; rep(i, a.size()) { // i is the number of elements that have lower index than a[i]. // call the number of elements that have lower value than a[i] // by subtracting these two, the residual number is the number of elements // that have larger value val += i - bit.query(a[i] - 1); // cumulative sum from 0 to a[i] - 1 bit.update(a[i], 1); } return val; } template <typename T> vector<T> compress(vector<T> v) { // sort and remove all the duplicated values sort(all(v)); v.erase(unique(all(v)), v.end()); return v; } template <typename T> map<T, ll> dict(const vector<T> &v) { map<T, ll> d; rep(i, v.size()) d[v[i]] = i; return d; } points compress2D(vl xs, vl ys) { /* NOTE ---- Add the corner points if required */ ll n = xs.size(); vl xcs = compress(xs), ycs = compress(ys); map<ll, ll> xd = dict(xcs), yd = dict(ycs); points ps(n); rep(i, n) xs[i] = xd[xs[i]], ys[i] = yd[ys[i]]; rep(i, n) ps[i] = {xs[i], ys[i]}; sort(all(ps)); return ps; } void GaussJordanBitVector(vl &bs) { ll n = bs.size(); ll rank = 0; ll j = 0; revrep(i, N_DIGITS) { for (j = rank; j < n; j++) if (bs[j] & (1LL << i)) break; if (j == n) continue; if (j > rank) bs[rank] ^= bs[j]; for (j = rank + 1; j < n; j++) bs[j] = min(bs[j], bs[j] ^ bs[rank]); rank++; } } ll kruskal(vector<undirected_edge> &es, ll n_vertex) { sort(all(es)); UnionFind uf(n_vertex); ll min_cost = 0; rep(i, es.size()) { undirected_edge &e = es[i]; if (!uf.is_same(e.from, e.to)) { min_cost += e.cost; uf.unite(e.from, e.to); } } return min_cost; } ll LongestIncreasedSequence(vl &v, ll n) { vl dp(n, INF); rep(i, n) * lower_bound(all(dp), v[i]) = v[i]; return lower_bound(all(dp), INF) - dp.begin(); } void dijkstra(ll start, Graph &graph, vl &dist, vl &vertex_pre, bool trace = false) { priority_queue<pl, vpl, greater<pl>> edge_costs; ll n = graph.size(); dist = vl(n, INF); if (trace) vertex_pre = vl(n, -1); dist[start] = 0; edge_costs.push(pl(0, start)); while (!edge_costs.empty()) { ll idx, cost; tie(cost, idx) = edge_costs.top(); edge_costs.pop(); if (dist[idx] < cost) continue; for (auto e : graph[idx]) { if (dist[e.to] > dist[idx] + e.cost) { dist[e.to] = dist[idx] + e.cost; if (trace) vertex_pre[e.to] = idx; edge_costs.push(pl(dist[e.to], e.to)); } } } } vl get_predecessor(ll g, vl &vertex_pre) { vl path; for (; g != -1; g = vertex_pre[g]) path.pb(g); reverse(all(path)); return path; } void warshall_floyd(vl2 &dist) { ll n = dist.size(); // Dont forget the initialization // rep(i, n) rep(j, n) dist[i][j] = INF * (i != j); rep(k, n) rep(i, n) rep(j, n) dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); } // ABC061D bool find_negative_cycle(ll n, ll goal, Graph &graph, vl &dist) { rep(i, n) rep(v, n) rep(k, graph[v].size()) { edge e = graph[v][k]; if (dist[e.to] != INF && dist[e.to] > dist[v] + e.cost) { dist[e.to] = -INF; if (goal == -1) return true; else if (goal == e.to) return true; } } return false; } bool bellman_ford(ll start, ll goal, Graph &graph, vl &dist) { // if there is a closed circuit, it returns false. (when goal == -1) // if the distance to goal cannot be obtained, it returns false (when goal != // -1) ll n = graph.size(); dist = vl(n, INF); dist[start] = 0; rep(i, n) rep(v, n) rep(k, graph[v].size()) { edge e = graph[v][k]; if (dist[v] != INF && dist[e.to] > dist[v] + e.cost) dist[e.to] = dist[v] + e.cost; } if (find_negative_cycle(n, goal, graph, dist)) return false; return true; } void read_graph(ll n_vertex, ll n_edges, Graph &graph, ll cost = -1, ll directed = false, ll offset = -1) { graph.resize(n_vertex); rep(i, n_edges) { ll from, to, c = cost; if (cost == -1) in3(from, to, c); else in2(from, to); from += offset, to += offset; graph[from].pb({to, cost}); if (!directed) graph[to].pb({from, cost}); } } void read_vector(ll n, vl &v, ll offset = 0) { v.resize(n); rep(i, n) { in1(v[i]); v[i] += offset; } } /* diameter of tree Graph tree; ll dM = 0, vM = 0, v2 = 0; void dfs1(ll source, ll parent, ll d){ if (d > dM) dM = d, vM = source; rep(i, tree[source].size()){ ll target = tree[source][i].to; if (target == parent) continue; dfs1(target, source, d + 1); } } void dfs2(ll source, ll parent, ll d){ if (dM <= d) dM = d, v2 = source; rep(i, tree[source].size()){ ll target = tree[source][i].to; if (target == parent) continue; dfs2(target, source, d + 1); } } dfs(0, -1, 0); dfs2(vM, -1, 0); prl2(vM + 1, v2 + 1); // the two edges of tree */ /* # 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)); # 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, two pointers technique) // 1. strech right side while the condition is met. // 2. renew the answer // 3. increments left side // 4. Back to 1. (l <= r must be satisfied all the time.) ll l = 0; ll r = 0; while (l < n){ r = max(r, l); if (l == r) r++; while(r < n && cond) r++; answer += r - l; l++; } prl(answer); #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 = graph[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 = graph[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); } # Cumulative sum (2 dimension) ll func(ll x, ll y, ll dx, ll dy){ if (x + dx > w || y + dy > h) return - INF; ll val = cum[x + dx][y + dy]; val += cum[x][y]; val -= cum[x][y + dy]; val -= cum[x + dx][y]; return val; } rep(x, w + 1) cum[x][0] = 0; rep(y, h + 1) cum[0][y] = 0; rep(y, h) rep(x, w) cum[x + 1][y + 1] = cum[x][y + 1] + vs[x][y]; rep(x, w + 1) rep(y, h) cum[x][y + 1] += cum[x][y]; */ /* # 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 ####### Attention ####### 1 << i and 1LL << i is different. If programs show WA, try switch to this one. 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 __builtin_popcountl(i) -> the number of 1 in binary 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 # inclusion-exclusion principle |U[i = 1 to n] Ai| = sum[i = 1 to n] |Ai| - sum[i < j]|Ai ^ Aj| + ... + (-1)^(n - 1) |^[i = 1 to n]Ai| */ const ll MAX_N = 200005; bool okay = false; ll answer = 0; str s; ll n, m, l, q; void dijkstra2(ll start, Graph &graph, vl &dist) { priority_queue<tp3, vtp3, greater<tp3>> edge_costs; ll n = graph.size(); dist = vl(n, INF); dist[start] = 0; edge_costs.push(mt(0, -l, start)); vl oils(n, 0); while (!edge_costs.empty()) { ll idx, cost, oil; tie(cost, oil, idx) = edge_costs.top(); oil *= -1; edge_costs.pop(); if (pl(dist[idx], -oils[idx]) < pl(cost, -oil)) continue; for (auto e : graph[idx]) { if (dist[e.to] > dist[idx] && oil >= e.cost) { dist[e.to] = dist[idx]; oils[e.to] = oil - e.cost; edge_costs.push(mt(dist[e.to], -oils[e.to], e.to)); } else if (dist[e.to] == dist[idx] && oil >= e.cost && oils[e.to] < oil - e.cost) { oils[e.to] = oil - e.cost; edge_costs.push(mt(dist[e.to], -oils[e.to], e.to)); } else if (dist[e.to] >= dist[idx] + 1) { dist[e.to] = dist[idx] + 1; oils[e.to] = l - e.cost; edge_costs.push(mt(dist[e.to], -oils[e.to], e.to)); } } } } void solve() { Graph graph(n); rep(i, m) { ll a, b, c; in3(a, b, c); a--, b--; if (c > l) continue; graph[a].pb({b, c}); graph[b].pb({a, c}); } vl2 dist(n); rep(i, n) dijkstra2(i, graph, dist[i]); in1(q); rep(i, q) { ll s1, s2; in2(s1, s2); s1--, s2--; if (dist[s1][s2] == INF) prl(-1); else prl(dist[s1][s2]); } } int main(void) { in3(n, m, l); solve(); // assert(n <= 3000); // solve(); return 0; }
replace
1,651
1,652
1,651
1,652
TLE
p02889
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) using ULL = unsigned long long; void solve() { int AZ = 1000000001; int n, m; cin >> n >> m; int L; cin >> L; queue<pair<int, int>> st; int l[300][300]; rep(i, n) rep(j, n) l[i][j] = AZ; rep(i, m) { int a, b; cin >> a >> b; a--; b--; int c; cin >> c; if (c <= L) l[a][b] = l[b][a] = c; st.push({a, b}); rep(i, n) st.push({b, a}); } rep(i, n) l[i][i] = 0; while (!st.empty()) { auto p = st.front(); st.pop(); int d = l[p.first][p.second]; rep(i, n) { int dd = l[p.second][i] + d; if (dd > L) continue; if (l[p.first][i] <= dd) continue; l[p.first][i] = dd; st.push({p.first, i}); } } int dp[300][300]; rep(i, n) rep(j, n) { if (l[i][j] <= L) { st.push({i, j}); dp[i][j] = 0; } else dp[i][j] = AZ; } rep(i, n) dp[i][i] = 0; while (!st.empty()) { auto p = st.front(); st.pop(); int d = dp[p.first][p.second]; rep(i, n) { if (dp[p.second][i] != 0) continue; if (dp[p.first][i] <= d + 1) continue; dp[p.first][i] = d + 1; st.push({p.first, i}); } } int q; cin >> q; vector<int> ans; rep(i, q) { int s, t; cin >> s >> t; s--; t--; if (dp[s][t] == AZ) ans.push_back(-1); else ans.push_back(dp[s][t]); } rep(i, ans.size()) { cout << ans[i] << endl; } } int main() { solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) using ULL = unsigned long long; void solve() { int AZ = 1000000001; int n, m; cin >> n >> m; int L; cin >> L; queue<pair<int, int>> st; int l[300][300]; rep(i, n) rep(j, n) l[i][j] = AZ; rep(i, m) { int a, b; cin >> a >> b; a--; b--; int c; cin >> c; if (c <= L) l[a][b] = l[b][a] = c; st.push({a, b}); st.push({b, a}); } rep(i, n) l[i][i] = 0; while (!st.empty()) { auto p = st.front(); st.pop(); int d = l[p.first][p.second]; rep(i, n) { int dd = l[p.second][i] + d; if (dd > L) continue; if (l[p.first][i] <= dd) continue; l[p.first][i] = dd; st.push({p.first, i}); } } int dp[300][300]; rep(i, n) rep(j, n) { if (l[i][j] <= L) { st.push({i, j}); dp[i][j] = 0; } else dp[i][j] = AZ; } rep(i, n) dp[i][i] = 0; while (!st.empty()) { auto p = st.front(); st.pop(); int d = dp[p.first][p.second]; rep(i, n) { if (dp[p.second][i] != 0) continue; if (dp[p.first][i] <= d + 1) continue; dp[p.first][i] = d + 1; st.push({p.first, i}); } } int q; cin >> q; vector<int> ans; rep(i, q) { int s, t; cin >> s >> t; s--; t--; if (dp[s][t] == AZ) ans.push_back(-1); else ans.push_back(dp[s][t]); } rep(i, ans.size()) { cout << ans[i] << endl; } } int main() { solve(); return 0; }
replace
26
27
26
27
TLE
p02889
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using vvll = vector<vll>; using vb = vector<bool>; using vs = vector<string>; using vld = vector<ld>; using vvld = vector<vld>; typedef pair<ll, ll> P; #define bit(n) (1LL << (n)) // #define int long long #define all(v) v.begin(), v.end() #define rep(i, n) for (ll i = 0; i < n; i++) #define REP(i, n) for (ll i = 1; i < n; i++) #define FOR(i, a, b) for (ll i = (a); i < (b); i++) #define FORm(i, m) for (auto i = m.begin(); i != m.end(); i++) template <class T> inline void chmax(T &a, T b) { a = std::max(a, b); } template <class T> inline void chmin(T &a, T b) { a = std::min(a, b); } #define mod (ll)(1e9 + 7) const long long INF = 1LL << 60; // const long long INF = 1LL << 60; ll n, m, l; template <typename T> struct Edge { int to; T cost; }; // Dijkstra template <typename T> class Dijkstra { private: vector<vector<Edge<T>>> G; int num_v; public: Dijkstra(int num_v) : G(num_v), num_v(num_v) {} void add_edge(int from, int to, T cost) { G[from].push_back((Edge<T>){to, cost}); } // start: Start point // result: result void solve(int start, vector<T> &result) { vector<int> tmp; solve(start, result, tmp); } void solve(int start, vector<T> &result, vector<int> &prev) { result.resize(num_v, make_pair(INF, INF)); prev.resize(num_v, -1); // get<0>(T): cost // get<1>(T): index // get<2>(T): ... ( for extent) using Tup = tuple<T, int>; priority_queue<Tup, vector<Tup>, greater<Tup>> que; que.emplace(make_pair(0ll, 0ll), start); result[start] = make_pair(0ll, 0ll); while (!que.empty()) { Tup p = que.top(); que.pop(); T t = get<0>(p); int v = get<1>(p); // vの各辺に対しよりコストの低い経路があるかを確認 for (auto &e : G[v]) { auto pr = result[v]; pr.second += e.cost.second; if (pr.second > l) { pr.first++; pr.second = e.cost.second; } if (result[e.to] > pr) { prev[e.to] = v; result[e.to] = pr; que.emplace(pr, e.to); } } } } // t: 目的地 vector<int> get_path(const vector<int> &prev, int t) { vector<int> path; for (int v = t; v != -1; v = prev[v]) { path.push_back(v); } reverse(path.begin(), path.end()); return path; } }; signed main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); cin >> n >> m >> l; Dijkstra<P> g(n + 1); rep(i, m) { ll a, b, c; cin >> a >> b >> c; if (c <= l) { auto p = make_pair(0ll, c); g.add_edge(a, b, p); g.add_edge(b, a, p); } } vector<vector<P>> ans(n + 1); REP(i, n + 1) { g.solve(i, ans[i]); } ll q; cin >> q; rep(i, q) { ll s, t; cin >> s >> t; ll v = ans[s][t].first; if (v == INF) { v = -1; } cout << v << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using vvll = vector<vll>; using vb = vector<bool>; using vs = vector<string>; using vld = vector<ld>; using vvld = vector<vld>; typedef pair<ll, ll> P; #define bit(n) (1LL << (n)) // #define int long long #define all(v) v.begin(), v.end() #define rep(i, n) for (ll i = 0; i < n; i++) #define REP(i, n) for (ll i = 1; i < n; i++) #define FOR(i, a, b) for (ll i = (a); i < (b); i++) #define FORm(i, m) for (auto i = m.begin(); i != m.end(); i++) template <class T> inline void chmax(T &a, T b) { a = std::max(a, b); } template <class T> inline void chmin(T &a, T b) { a = std::min(a, b); } #define mod (ll)(1e9 + 7) const long long INF = 1LL << 60; // const long long INF = 1LL << 60; ll n, m, l; template <typename T> struct Edge { int to; T cost; }; // Dijkstra template <typename T> class Dijkstra { private: vector<vector<Edge<T>>> G; int num_v; public: Dijkstra(int num_v) : G(num_v), num_v(num_v) {} void add_edge(int from, int to, T cost) { G[from].push_back((Edge<T>){to, cost}); } // start: Start point // result: result void solve(int start, vector<T> &result) { vector<int> tmp; solve(start, result, tmp); } void solve(int start, vector<T> &result, vector<int> &prev) { result.resize(num_v, make_pair(INF, INF)); prev.resize(num_v, -1); // get<0>(T): cost // get<1>(T): index // get<2>(T): ... ( for extent) using Tup = tuple<T, int>; priority_queue<Tup, vector<Tup>, greater<Tup>> que; que.emplace(make_pair(0ll, 0ll), start); result[start] = make_pair(0ll, 0ll); while (!que.empty()) { Tup p = que.top(); que.pop(); T t = get<0>(p); int v = get<1>(p); if (result[v] != t) { continue; } // vの各辺に対しよりコストの低い経路があるかを確認 for (auto &e : G[v]) { auto pr = result[v]; pr.second += e.cost.second; if (pr.second > l) { pr.first++; pr.second = e.cost.second; } if (result[e.to] > pr) { prev[e.to] = v; result[e.to] = pr; que.emplace(pr, e.to); } } } } // t: 目的地 vector<int> get_path(const vector<int> &prev, int t) { vector<int> path; for (int v = t; v != -1; v = prev[v]) { path.push_back(v); } reverse(path.begin(), path.end()); return path; } }; signed main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); cin >> n >> m >> l; Dijkstra<P> g(n + 1); rep(i, m) { ll a, b, c; cin >> a >> b >> c; if (c <= l) { auto p = make_pair(0ll, c); g.add_edge(a, b, p); g.add_edge(b, a, p); } } vector<vector<P>> ans(n + 1); REP(i, n + 1) { g.solve(i, ans[i]); } ll q; cin >> q; rep(i, q) { ll s, t; cin >> s >> t; ll v = ans[s][t].first; if (v == INF) { v = -1; } cout << v << endl; } return 0; }
insert
85
85
85
89
TLE
p02889
C++
Time Limit Exceeded
#include <algorithm> #include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < (n); i++) template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } ll inf = 1001001001; ll n, m, l, q; class CompareDist { public: bool operator()(pair<pair<ll, ll>, ll> n1, pair<pair<ll, ll>, ll> n2) { if (n1.first.first == n2.first.first) return n1.first.second > n2.first.second; return n1.first.first < n2.first.first; } }; int main() { cin >> n >> m >> l; vector<vector<pair<ll, ll>>> route(n + 1); vector<vector<ll>> is(n + 1, vector<ll>(n + 1, 0)); rep(i, m) { ll a, b, c; cin >> a >> b >> c; if (c > l) continue; route[a].push_back({b, c}); route[b].push_back({a, c}); is[a][b] = c; is[b][a] = c; } cin >> q; vector<vector<ll>> memo(n + 1, vector<ll>(n + 1, -10)); rep(i, q) { ll s, t; cin >> s >> t; if (memo[s][t] != -10) { cout << memo[s][t] << endl; continue; } if (is[s][t] != 0) { cout << 0 << endl; memo[s][t] = 0; continue; } vector<pair<ll, ll>> cost(n + 1, {inf, 0}); cost[s] = {0, l}; priority_queue<pair<pair<ll, ll>, ll>, vector<pair<pair<ll, ll>, ll>>, CompareDist> que; que.push({{0, l}, s}); while (!que.empty()) { pair<pair<ll, ll>, ll> tmp = que.top(); que.pop(); int x = tmp.first.first, y = tmp.first.second, z = tmp.second; if (cost[z].first < x || (cost[z].first == x && cost[z].second > y)) continue; for (pair<ll, ll> j : route[z]) { if (y >= j.second) { if (cost[j.first].first > x || (cost[j.first].first == x && cost[j.first].second < y - j.second)) { cost[j.first] = {x, y - j.second}; que.push({{x, y - j.second}, j.first}); } } else { if (cost[j.first].first > x + 1 || (cost[j.first].first == x + 1 && cost[j.first].second < l - j.second)) { cost[j.first] = {x + 1, l - j.second}; que.push({{x + 1, l - j.second}, j.first}); } } } } rep(j, n + 1) { if (cost[j].first == inf) cost[j].first = -1; memo[s][j] = cost[j].first; memo[j][s] = cost[j].first; } cout << cost[t].first << endl; } }
#include <algorithm> #include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < (n); i++) template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } ll inf = 1001001001; ll n, m, l, q; class CompareDist { public: bool operator()(pair<pair<ll, ll>, ll> n1, pair<pair<ll, ll>, ll> n2) { if (n1.first.first == n2.first.first) return n1.first.second < n2.first.second; return n1.first.first > n2.first.first; } }; int main() { cin >> n >> m >> l; vector<vector<pair<ll, ll>>> route(n + 1); vector<vector<ll>> is(n + 1, vector<ll>(n + 1, 0)); rep(i, m) { ll a, b, c; cin >> a >> b >> c; if (c > l) continue; route[a].push_back({b, c}); route[b].push_back({a, c}); is[a][b] = c; is[b][a] = c; } cin >> q; vector<vector<ll>> memo(n + 1, vector<ll>(n + 1, -10)); rep(i, q) { ll s, t; cin >> s >> t; if (memo[s][t] != -10) { cout << memo[s][t] << endl; continue; } if (is[s][t] != 0) { cout << 0 << endl; memo[s][t] = 0; continue; } vector<pair<ll, ll>> cost(n + 1, {inf, 0}); cost[s] = {0, l}; priority_queue<pair<pair<ll, ll>, ll>, vector<pair<pair<ll, ll>, ll>>, CompareDist> que; que.push({{0, l}, s}); while (!que.empty()) { pair<pair<ll, ll>, ll> tmp = que.top(); que.pop(); int x = tmp.first.first, y = tmp.first.second, z = tmp.second; if (cost[z].first < x || (cost[z].first == x && cost[z].second > y)) continue; for (pair<ll, ll> j : route[z]) { if (y >= j.second) { if (cost[j.first].first > x || (cost[j.first].first == x && cost[j.first].second < y - j.second)) { cost[j.first] = {x, y - j.second}; que.push({{x, y - j.second}, j.first}); } } else { if (cost[j.first].first > x + 1 || (cost[j.first].first == x + 1 && cost[j.first].second < l - j.second)) { cost[j.first] = {x + 1, l - j.second}; que.push({{x + 1, l - j.second}, j.first}); } } } } rep(j, n + 1) { if (cost[j].first == inf) cost[j].first = -1; memo[s][j] = cost[j].first; memo[j][s] = cost[j].first; } cout << cost[t].first << endl; } }
replace
27
29
27
29
TLE
p02889
C++
Time Limit Exceeded
/*~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~= *$* WRITER:kakitamasziru/OxOmisosiru *$* ~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=*/ #ifdef LOCAL_JUDGE #define _GLIBCXX_DEBUG #endif #include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound #include <cstdint> // int64_t, int*_t #include <iomanip> #include <iostream> // cout, endl, cin #include <limits> //setprecision #include <string> // string, to_string, stoi #include <tuple> // tuple, make_tuple #include <utility> // pair, make_pair #include <vector> // vector // #include <cstdio> // printf #include <map> // map #include <queue> // queue, priority_queue #include <set> // set // #include <unordered_set> //unordered_set #include <bitset> // bitset #include <cmath> //abs,,, #include <deque> // deque #include <math.h> //pow,,, #include <stack> // stack #define endl "\n"; using namespace std; const long long INF = 100100100100100100; const int MOD = 1000000007; const int inf = 1001001001; typedef pair<long long, long long> P; // Solve N^M. This, mod_pow use Iterative Square Method. long long mod_pow(long long N, long long M) { if (M == 0) return 1; long long res = mod_pow((N * N) % MOD, M / 2); // 最下位ビット(*N)が1の時は単独でNをかける if (M & 1) res = (res * N) % MOD; return res %= MOD; } long long gcd(long long a, long long b) { if (b == 0) return a; else return gcd(b, a % b); } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } vector<vector<P>> Edge(305); // mini_supply.at(i).at(j).first:iからjへの最小補給回数 //.second:iからjへの最小補給回数で行った時の最小消費燃料 vector<vector<P>> mini_supply(305, vector<P>(305)); int main() { ios::sync_with_stdio(false); std::cin.tie(nullptr); long long N, M, L; cin >> N >> M >> L; for (int i = 0; i < M; i++) { long long from, to, cost; cin >> from >> to >> cost; from--, to--; Edge.at(from).push_back(make_pair(to, cost)); Edge.at(to).push_back(make_pair(from, cost)); } for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) { mini_supply.at(i).at(j).first = inf; mini_supply.at(i).at(j).second = inf; } for (long long start = 0; start < N; start++) { mini_supply.at(start).at(start).first = 0; mini_supply.at(start).at(start).second = 0; priority_queue<pair<P, long long>, vector<pair<P, long long>>, greater<pair<P, long long>>> PQ; // PQ.top().first.first:mini_supply //.first.second:mini_fuel when mini_supply //.second:from PQ.push(make_pair(make_pair(0, 0), start)); while (!PQ.empty()) { pair<P, long long> p = PQ.top(); PQ.pop(); long long mini_sup = p.first.first; long long mini_fuel = p.first.second; long long from = p.second; if (mini_sup > mini_supply.at(start).at(from).first) continue; for (P V : Edge.at(from)) { long long to = V.first, cost = V.second; // 満タンでも行けないところは飛ばす if (cost > L) continue; long long next_fuel = mini_fuel + cost; long long next_sup = mini_sup; if (next_fuel > L) { next_sup++; next_fuel = cost; } if (mini_supply.at(start).at(to).first > next_sup || (mini_supply.at(start).at(to).first == next_sup && mini_supply.at(start).at(to).second > next_fuel)) { mini_supply.at(start).at(to).first = next_sup; mini_supply.at(start).at(to).second = next_fuel; PQ.push(make_pair(make_pair(next_sup, next_fuel), to)); } } } } long long Q; cin >> Q; for (int i = 0; i < Q; i++) { long long from, to; cin >> from >> to; from--, to--; if (mini_supply.at(from).at(to).first == inf) { cout << -1 << endl; } else { cout << mini_supply.at(from).at(to).first << endl; } } }
/*~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~= *$* WRITER:kakitamasziru/OxOmisosiru *$* ~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=*/ #ifdef LOCAL_JUDGE #define _GLIBCXX_DEBUG #endif #include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound #include <cstdint> // int64_t, int*_t #include <iomanip> #include <iostream> // cout, endl, cin #include <limits> //setprecision #include <string> // string, to_string, stoi #include <tuple> // tuple, make_tuple #include <utility> // pair, make_pair #include <vector> // vector // #include <cstdio> // printf #include <map> // map #include <queue> // queue, priority_queue #include <set> // set // #include <unordered_set> //unordered_set #include <bitset> // bitset #include <cmath> //abs,,, #include <deque> // deque #include <math.h> //pow,,, #include <stack> // stack #define endl "\n"; using namespace std; const long long INF = 100100100100100100; const int MOD = 1000000007; const int inf = 1001001001; typedef pair<long long, long long> P; // Solve N^M. This, mod_pow use Iterative Square Method. long long mod_pow(long long N, long long M) { if (M == 0) return 1; long long res = mod_pow((N * N) % MOD, M / 2); // 最下位ビット(*N)が1の時は単独でNをかける if (M & 1) res = (res * N) % MOD; return res %= MOD; } long long gcd(long long a, long long b) { if (b == 0) return a; else return gcd(b, a % b); } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } vector<vector<P>> Edge(305); // mini_supply.at(i).at(j).first:iからjへの最小補給回数 //.second:iからjへの最小補給回数で行った時の最小消費燃料 vector<vector<P>> mini_supply(305, vector<P>(305)); int main() { ios::sync_with_stdio(false); std::cin.tie(nullptr); long long N, M, L; cin >> N >> M >> L; for (int i = 0; i < M; i++) { long long from, to, cost; cin >> from >> to >> cost; from--, to--; Edge.at(from).push_back(make_pair(to, cost)); Edge.at(to).push_back(make_pair(from, cost)); } for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) { mini_supply.at(i).at(j).first = inf; mini_supply.at(i).at(j).second = inf; } for (long long start = 0; start < N; start++) { mini_supply.at(start).at(start).first = 0; mini_supply.at(start).at(start).second = 0; priority_queue<pair<P, long long>, vector<pair<P, long long>>, greater<pair<P, long long>>> PQ; // PQ.top().first.first:mini_supply //.first.second:mini_fuel when mini_supply //.second:from PQ.push(make_pair(make_pair(0, 0), start)); while (!PQ.empty()) { pair<P, long long> p = PQ.top(); PQ.pop(); long long mini_sup = p.first.first; long long mini_fuel = p.first.second; long long from = p.second; if (mini_sup > mini_supply.at(start).at(from).first || mini_sup == mini_supply.at(start).at(from).first && mini_fuel > mini_supply.at(start).at(from).second) continue; for (P V : Edge.at(from)) { long long to = V.first, cost = V.second; // 満タンでも行けないところは飛ばす if (cost > L) continue; long long next_fuel = mini_fuel + cost; long long next_sup = mini_sup; if (next_fuel > L) { next_sup++; next_fuel = cost; } if (mini_supply.at(start).at(to).first > next_sup || (mini_supply.at(start).at(to).first == next_sup && mini_supply.at(start).at(to).second > next_fuel)) { mini_supply.at(start).at(to).first = next_sup; mini_supply.at(start).at(to).second = next_fuel; PQ.push(make_pair(make_pair(next_sup, next_fuel), to)); } } } } long long Q; cin >> Q; for (int i = 0; i < Q; i++) { long long from, to; cin >> from >> to; from--, to--; if (mini_supply.at(from).at(to).first == inf) { cout << -1 << endl; } else { cout << mini_supply.at(from).at(to).first << endl; } } }
replace
93
94
93
96
TLE
p02889
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); ++i) #define dup(x, y) (((x) + (y)-1) / (y)) #define ALL(x) (x).begin(), (x).end() typedef long long ll; typedef pair<int, int> pii; const double EPS = 1e-10; const int INF = 1e9; const ll LINF = 1e15; const int MOD = 1000000007; const double PI = acos(-1); int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; int main() { int n, m, l; cin >> n >> m >> l; vector<vector<ll>> dist1(n, vector<ll>(n, LINF)); rep(i, n) dist1[i][i] = 0; rep(i, m) { int a, b, c; cin >> a >> b >> c; a--, b--; dist1[a][b] = c; dist1[b][a] = c; } int q; cin >> q; vector<pii> v(q); rep(i, q) { cin >> v[i].first >> v[i].second; v[i].first--, v[i].second--; } rep(i, m) rep(k, n) { rep(i, n) { rep(j, n) { dist1[i][j] = min(dist1[i][j], dist1[i][k] + dist1[k][j]); } } } vector<vector<ll>> dist2(n, vector<ll>(n, LINF)); rep(i, n) { rep(j, n) { dist2[i][j] = (dist1[i][j] <= l ? 1 : LINF); } } rep(k, n) { rep(i, n) { rep(j, n) { dist2[i][j] = min(dist2[i][j], dist2[i][k] + dist2[k][j]); } } } rep(i, q) { if (dist2[v[i].first][v[i].second] == LINF) cout << -1 << endl; else cout << dist2[v[i].first][v[i].second] - 1 << endl; } }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); ++i) #define dup(x, y) (((x) + (y)-1) / (y)) #define ALL(x) (x).begin(), (x).end() typedef long long ll; typedef pair<int, int> pii; const double EPS = 1e-10; const int INF = 1e9; const ll LINF = 1e15; const int MOD = 1000000007; const double PI = acos(-1); int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; int main() { int n, m, l; cin >> n >> m >> l; vector<vector<ll>> dist1(n, vector<ll>(n, LINF)); rep(i, n) dist1[i][i] = 0; rep(i, m) { int a, b, c; cin >> a >> b >> c; a--, b--; dist1[a][b] = c; dist1[b][a] = c; } int q; cin >> q; vector<pii> v(q); rep(i, q) { cin >> v[i].first >> v[i].second; v[i].first--, v[i].second--; } rep(k, n) { rep(i, n) { rep(j, n) { dist1[i][j] = min(dist1[i][j], dist1[i][k] + dist1[k][j]); } } } vector<vector<ll>> dist2(n, vector<ll>(n, LINF)); rep(i, n) { rep(j, n) { dist2[i][j] = (dist1[i][j] <= l ? 1 : LINF); } } rep(k, n) { rep(i, n) { rep(j, n) { dist2[i][j] = min(dist2[i][j], dist2[i][k] + dist2[k][j]); } } } rep(i, q) { if (dist2[v[i].first][v[i].second] == LINF) cout << -1 << endl; else cout << dist2[v[i].first][v[i].second] - 1 << endl; } }
replace
35
36
35
36
TLE
p02889
C++
Runtime Error
#include <bits/stdc++.h> #define all(x) x.begin(), x.end() using namespace std; using ll = long long; using pii = pair<int, int>; const int MN = 300 + 5, LN = 17, MOD = 1e9 + 7, INF = 0x3f3f3f3f, BSZ = 320; const ll INFLL = 1e17; ll N, M, L, Q; ll dis[MN][MN], f[MN][MN]; int main() { ios_base::sync_with_stdio(0), cin.tie(0); freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); cin >> N >> M >> L; for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) { dis[i][j] = f[i][j] = INFLL; } dis[i][i] = f[i][i] = 0; } for (int i = 0; i < M; i++) { int u, v; ll w; cin >> u >> v >> w; dis[u][v] = min(dis[u][v], w); dis[v][u] = min(dis[v][u], w); } for (int k = 1; k <= N; k++) { for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) { dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]); } } } for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) { if (i == j) continue; if (dis[i][j] <= L) f[i][j] = min(f[i][j], 1LL); } } for (int k = 1; k <= N; k++) { for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) { f[i][j] = min(f[i][j], f[i][k] + f[k][j]); } } } cin >> Q; for (int i = 0; i < Q; i++) { int s, t; cin >> s >> t; if (f[s][t] < INFLL) { cout << f[s][t] - 1 << '\n'; } else cout << -1 << '\n'; } return 0; }
#include <bits/stdc++.h> #define all(x) x.begin(), x.end() using namespace std; using ll = long long; using pii = pair<int, int>; const int MN = 300 + 5, LN = 17, MOD = 1e9 + 7, INF = 0x3f3f3f3f, BSZ = 320; const ll INFLL = 1e17; ll N, M, L, Q; ll dis[MN][MN], f[MN][MN]; int main() { ios_base::sync_with_stdio(0), cin.tie(0); // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); cin >> N >> M >> L; for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) { dis[i][j] = f[i][j] = INFLL; } dis[i][i] = f[i][i] = 0; } for (int i = 0; i < M; i++) { int u, v; ll w; cin >> u >> v >> w; dis[u][v] = min(dis[u][v], w); dis[v][u] = min(dis[v][u], w); } for (int k = 1; k <= N; k++) { for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) { dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]); } } } for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) { if (i == j) continue; if (dis[i][j] <= L) f[i][j] = min(f[i][j], 1LL); } } for (int k = 1; k <= N; k++) { for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) { f[i][j] = min(f[i][j], f[i][k] + f[k][j]); } } } cin >> Q; for (int i = 0; i < Q; i++) { int s, t; cin >> s >> t; if (f[s][t] < INFLL) { cout << f[s][t] - 1 << '\n'; } else cout << -1 << '\n'; } return 0; }
replace
13
15
13
15
0
p02889
C++
Runtime Error
/* _ _ _ | | | | ___ ___ | | | || . || _| |_____||_ ||_| _____ |_| |_____| */ // Time : 19/10/20 #include <bits/stdc++.h> #define inf 0x3f3f3f3f using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vll; typedef pair<int, int> pii; const int nmax = 300 + 30; int dis1[nmax][nmax], dis2[nmax][nmax]; int main() { #ifdef Wqr_ freopen("in.txt", "r", stdin); #endif std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); int n, m, l; cin >> n >> m >> l; int a, b, c; memset(dis1, inf, sizeof(dis1)); memset(dis2, inf, sizeof(dis2)); for (int i = 0; i < m; i++) { cin >> a >> b >> c; --a, --b; dis1[a][b] = c; dis1[b][a] = c; dis1[i][i] = 0; dis2[i][i] = 0; } for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { dis1[i][j] = min(dis1[i][j], dis1[i][k] + dis1[k][j]); } } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (dis1[i][j] <= l) { dis2[i][j] = 1; dis2[j][i] = 1; } } } for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { dis2[i][j] = min(dis2[i][j], dis2[i][k] + dis2[k][j]); } } } int q; cin >> q; for (int i = 0; i < q; i++) { cin >> a >> b; --a, --b; if (dis2[a][b] == inf) dis2[a][b] = 0; cout << (dis2[a][b] - 1) << endl; } return 0; }
/* _ _ _ | | | | ___ ___ | | | || . || _| |_____||_ ||_| _____ |_| |_____| */ // Time : 19/10/20 #include <bits/stdc++.h> #define inf 0x3f3f3f3f using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vll; typedef pair<int, int> pii; const int nmax = 300 + 30; int dis1[nmax][nmax], dis2[nmax][nmax]; int main() { #ifdef Wqr_ freopen("in.txt", "r", stdin); #endif std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); int n, m, l; cin >> n >> m >> l; int a, b, c; memset(dis1, inf, sizeof(dis1)); memset(dis2, inf, sizeof(dis2)); for (int i = 0; i < m; i++) { cin >> a >> b >> c; --a, --b; dis1[a][b] = c; dis1[b][a] = c; } for (int i = 0; i < n; i++) { dis1[i][i] = dis2[i][i] = 0; } for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { dis1[i][j] = min(dis1[i][j], dis1[i][k] + dis1[k][j]); } } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (dis1[i][j] <= l) { dis2[i][j] = 1; dis2[j][i] = 1; } } } for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { dis2[i][j] = min(dis2[i][j], dis2[i][k] + dis2[k][j]); } } } int q; cin >> q; for (int i = 0; i < q; i++) { cin >> a >> b; --a, --b; if (dis2[a][b] == inf) dis2[a][b] = 0; cout << (dis2[a][b] - 1) << endl; } return 0; }
replace
30
32
30
33
0
p02889
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; // ll MOD=1000000007; ll INF = 300000000000; int main() { ll N, M, L; cin >> N >> M >> L; vector<ll> A(N), B(N), C(N); for (ll i = 0; i < M; i++) { ll a, b, c; cin >> a >> b >> c; a--; b--; A[i] = a; B[i] = b; C[i] = c; } ll Dist[N][N]; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { Dist[i][j] = INF; } } for (int i = 0; i < N; i++) { Dist[i][i] = 0; } for (int i = 0; i < M; i++) { Dist[A[i]][B[i]] = C[i]; Dist[B[i]][A[i]] = C[i]; } for (int k = 0; k < N; k++) { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { Dist[i][j] = min(Dist[i][j], Dist[i][k] + Dist[k][j]); } } } ll Way[N][N]; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { Way[i][j] = 301; } } for (int i = 0; i < N; i++) { Way[i][i] = 0; } for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (i != j) { if (Dist[i][j] <= L) { Way[i][j] = 1; } } } } for (int k = 0; k < N; k++) { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { Way[i][j] = min(Way[i][j], Way[i][k] + Way[k][j]); } } } int Q; cin >> Q; vector<ll> S(Q), T(Q); for (int i = 0; i < Q; i++) { ll s, t; cin >> s >> t; s--; t--; // ここを変えた S[i] = s; T[i] = t; if (Way[S[i]][T[i]] == 301) { cout << -1 << endl; } else { cout << Way[S[i]][T[i]] - 1 << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; // ll MOD=1000000007; ll INF = 300000000000; int main() { ll N, M, L; cin >> N >> M >> L; vector<ll> A(M), B(M), C(M); for (ll i = 0; i < M; i++) { ll a, b, c; cin >> a >> b >> c; a--; b--; A[i] = a; B[i] = b; C[i] = c; } ll Dist[N][N]; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { Dist[i][j] = INF; } } for (int i = 0; i < N; i++) { Dist[i][i] = 0; } for (int i = 0; i < M; i++) { Dist[A[i]][B[i]] = C[i]; Dist[B[i]][A[i]] = C[i]; } for (int k = 0; k < N; k++) { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { Dist[i][j] = min(Dist[i][j], Dist[i][k] + Dist[k][j]); } } } ll Way[N][N]; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { Way[i][j] = 301; } } for (int i = 0; i < N; i++) { Way[i][i] = 0; } for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (i != j) { if (Dist[i][j] <= L) { Way[i][j] = 1; } } } } for (int k = 0; k < N; k++) { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { Way[i][j] = min(Way[i][j], Way[i][k] + Way[k][j]); } } } int Q; cin >> Q; vector<ll> S(Q), T(Q); for (int i = 0; i < Q; i++) { ll s, t; cin >> s >> t; s--; t--; // ここを変えた S[i] = s; T[i] = t; if (Way[S[i]][T[i]] == 301) { cout << -1 << endl; } else { cout << Way[S[i]][T[i]] - 1 << endl; } } return 0; }
replace
8
9
8
9
0
p02889
C++
Time Limit Exceeded
// 2020-07-24 07:52:55 #include <bits/stdc++.h> #ifdef LOCAL #include "lib/debug.hpp" #else #define debug(...) 1 #endif #define ALL(a) (a).begin(), (a).end() #define rep(i, n) REP(i, 0, (n)) #define repc(i, n) REPC(i, 0, (n)) #define REP(i, n, m) for (int i = (int)(n); i < (int)(m); i++) #define REPC(i, n, m) for (int i = (int)(n); i <= (int)(m); i++) #define REPCM(i, n, m) for (int i = (int)(n); i >= (int)(m); i--) using namespace std; using ll = long long; using ld = long double; using pr = pair<ll, ll>; using vll = vector<ll>; using vpr = vector<pr>; template <class T> inline bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } else return false; } template <class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } else return false; } using P = pair<int, int>; const int INF = 1e9 + 5; int G[300][300]; // cost P dp[300][300]; // supply, used int n, m, l; void bfs(int s) { using T = tuple<int, int, int>; // supply, used, to priority_queue<T, vector<T>, greater<T>> que; que.emplace(T(0, 0, s)); while (!que.empty()) { int supply, used, to; tie(supply, used, to) = que.top(); que.pop(); if (chmin(dp[s][to], P(supply, used))) { rep(v, n) if (G[to][v] <= l) { if (used + G[to][v] <= l) { que.emplace(T(supply, used + G[to][v], v)); } else { que.emplace(T(supply + 1, G[to][v], v)); } } } } } void answer() { cin >> n >> m >> l; rep(i, n) rep(j, n) { G[i][j] = INF; dp[i][j] = P(INF, 0); if (i == j) G[i][j] = 0; } rep(i, m) { int a, b, c; cin >> a >> b >> c; a--; b--; G[a][b] = c; G[b][a] = c; } rep(i, n) bfs(i); int q; cin >> q; rep(qi, q) { int s, t; cin >> s >> t; s--; t--; if (dp[s][t].first == INF) cout << -1 << '\n'; else cout << dp[s][t].first << '\n'; } } int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); answer(); return 0; }
// 2020-07-24 07:52:55 #include <bits/stdc++.h> #ifdef LOCAL #include "lib/debug.hpp" #else #define debug(...) 1 #endif #define ALL(a) (a).begin(), (a).end() #define rep(i, n) REP(i, 0, (n)) #define repc(i, n) REPC(i, 0, (n)) #define REP(i, n, m) for (int i = (int)(n); i < (int)(m); i++) #define REPC(i, n, m) for (int i = (int)(n); i <= (int)(m); i++) #define REPCM(i, n, m) for (int i = (int)(n); i >= (int)(m); i--) using namespace std; using ll = long long; using ld = long double; using pr = pair<ll, ll>; using vll = vector<ll>; using vpr = vector<pr>; template <class T> inline bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } else return false; } template <class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } else return false; } using P = pair<int, int>; const int INF = 1e9 + 5; int G[300][300]; // cost P dp[300][300]; // supply, used int n, m, l; void bfs(int s) { using T = tuple<int, int, int>; // supply, used, to priority_queue<T, vector<T>, greater<T>> que; que.emplace(T(0, 0, s)); while (!que.empty()) { int supply, used, to; tie(supply, used, to) = que.top(); que.pop(); if (chmin(dp[s][to], P(supply, used))) { rep(v, n) if (G[to][v] <= l) { int nsupply = supply, nused = used + G[to][v]; if (nused > l) { nsupply++; nused = G[to][v]; } if (dp[s][v] > P(nsupply, nused)) { que.emplace(T(nsupply, nused, v)); } } } } } void answer() { cin >> n >> m >> l; rep(i, n) rep(j, n) { G[i][j] = INF; dp[i][j] = P(INF, 0); if (i == j) G[i][j] = 0; } rep(i, m) { int a, b, c; cin >> a >> b >> c; a--; b--; G[a][b] = c; G[b][a] = c; } rep(i, n) bfs(i); int q; cin >> q; rep(qi, q) { int s, t; cin >> s >> t; s--; t--; if (dp[s][t].first == INF) cout << -1 << '\n'; else cout << dp[s][t].first << '\n'; } } int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); answer(); return 0; }
replace
49
53
49
56
TLE
p02889
C++
Runtime Error
// #include<bits/stdc++.h> #include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define MAX_N 100000000 #define INF 100000000000000 int main() { int n, m; long long l; cin >> n >> m >> l; vector<pair<int, long long>> adj[m]; int a, b; long long c; for (int i = 0; i < m; i++) { cin >> a >> b >> c; adj[a - 1].push_back(make_pair(b - 1, c)); adj[b - 1].push_back(make_pair(a - 1, c)); } long long d[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == j) d[i][j] = 0; else d[i][j] = INF; } } for (int i = 0; i < n; i++) { for (int j = 0; j < adj[i].size(); j++) { b = adj[i][j].first; d[i][b] = adj[i][j].second; } } for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } } } long long rd[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == j) rd[i][j] = 0; else if (d[i][j] <= l) rd[i][j] = 1; else rd[i][j] = INF; } } for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { rd[i][j] = min(rd[i][j], rd[i][k] + rd[k][j]); } } } int q; cin >> q; int s[q]; int t[q]; for (int i = 0; i < q; i++) { cin >> s[i] >> t[i]; } for (int i = 0; i < q; i++) { if (rd[s[i] - 1][t[i] - 1] == INF) cout << -1 << endl; else cout << rd[s[i] - 1][t[i] - 1] - 1 << endl; } return 0; }
// #include<bits/stdc++.h> #include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define MAX_N 100000000 #define INF 100000000000000 int main() { int n, m; long long l; cin >> n >> m >> l; vector<pair<int, long long>> adj[n]; int a, b; long long c; for (int i = 0; i < m; i++) { cin >> a >> b >> c; adj[a - 1].push_back(make_pair(b - 1, c)); adj[b - 1].push_back(make_pair(a - 1, c)); } long long d[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == j) d[i][j] = 0; else d[i][j] = INF; } } for (int i = 0; i < n; i++) { for (int j = 0; j < adj[i].size(); j++) { b = adj[i][j].first; d[i][b] = adj[i][j].second; } } for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } } } long long rd[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == j) rd[i][j] = 0; else if (d[i][j] <= l) rd[i][j] = 1; else rd[i][j] = INF; } } for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { rd[i][j] = min(rd[i][j], rd[i][k] + rd[k][j]); } } } int q; cin >> q; int s[q]; int t[q]; for (int i = 0; i < q; i++) { cin >> s[i] >> t[i]; } for (int i = 0; i < q; i++) { if (rd[s[i] - 1][t[i] - 1] == INF) cout << -1 << endl; else cout << rd[s[i] - 1][t[i] - 1] - 1 << endl; } return 0; }
replace
21
22
21
22
-11