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
p02918
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; string s; cin >> s; int score = 0; for (int i = 0; i < n; i++) { if (s.at(i) == s.at(i + 1)) score++; } int ans = min(score + 2 * k, n - 1); cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; string s; cin >> s; int score = 0; for (int i = 0; i < n - 1; i++) { if (s.at(i) == s.at(i + 1)) score++; } int ans = min(score + 2 * k, n - 1); cout << ans << endl; }
replace
9
10
9
10
-6
terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::at: __n (which is 6) >= this->size() (which is 6)
p02918
C++
Runtime Error
#include <algorithm> #include <iostream> #include <map> #include <math.h> #include <string> #include <vector> using namespace std; int main(void) { int n, k; cin >> n >> k; string s; int ans = 0; for (int i = 0; i < n - 1; i++) { if (s[i] == s[i + 1]) { ans++; } } cout << min(ans + 2 * k, n - 1) << endl; }
#include <algorithm> #include <iostream> #include <map> #include <math.h> #include <string> #include <vector> using namespace std; int main(void) { int n, k; cin >> n >> k; string s; cin >> s; int ans = 0; for (int i = 0; i < n - 1; i++) { if (s[i] == s[i + 1]) { ans++; } } cout << min(ans + 2 * k, n - 1) << endl; }
insert
13
13
13
14
0
p02918
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long #define PI pair<int, int> const int maxm = 1e6 + 5; char s[maxm]; int n, k; signed main() { cin >> n >> k; scanf("%s", s + 1); int ans = 0; vector<int> rl, lr; for (int i = 2; i <= n; i++) { if (s[i] == s[i - 1]) { ans++; } else { if (s[i] == 'L') { rl.push_back(i); } else { lr.push_back(i); } } } int len1 = rl.size(); int len2 = lr.size(); int c = 0, cc = 0; vector<int> temp; while (c < len1 && cc < len2 && k) { int x = rl[c], y = lr[cc]; if (x < y) { // R|LLL|R ans += 2; k--; cc++; while (rl[c] < y) c++; } else { temp.push_back(y); cc++; } } while (c < len1) temp.push_back(rl[c++]); while (cc < len2) temp.push_back(lr[cc++]); ans += min((int)temp.size(), k); cout << ans << endl; return 0; } /* LLL这种全部一样的翻转之后权值不变, 只有R|LLL这种,翻转一遍之后会增加一个权值, 遇到R|LLL|R这种能增加两个权值. 其他情况下不增加了. 发现一次操作最多增加两个权值. 那么贪心一下就行了: 记录每个L|R和R|L的位置,然后贪心的翻转. */
#include <bits/stdc++.h> using namespace std; #define int long long #define PI pair<int, int> const int maxm = 1e6 + 5; char s[maxm]; int n, k; signed main() { cin >> n >> k; scanf("%s", s + 1); int ans = 0; vector<int> rl, lr; for (int i = 2; i <= n; i++) { if (s[i] == s[i - 1]) { ans++; } else { if (s[i] == 'L') { rl.push_back(i); } else { lr.push_back(i); } } } int len1 = rl.size(); int len2 = lr.size(); int c = 0, cc = 0; vector<int> temp; while (c < len1 && cc < len2 && k) { int x = rl[c], y = lr[cc]; if (x < y) { // R|LLL|R ans += 2; k--; c++, cc++; } else { // L|RRR|L ans += 2; k--; c++, cc++; } } while (c < len1) temp.push_back(rl[c++]); while (cc < len2) temp.push_back(lr[cc++]); ans += min((int)temp.size(), k); cout << ans << endl; return 0; } /* LLL这种全部一样的翻转之后权值不变, 只有R|LLL这种,翻转一遍之后会增加一个权值, 遇到R|LLL|R这种能增加两个权值. 其他情况下不增加了. 发现一次操作最多增加两个权值. 那么贪心一下就行了: 记录每个L|R和R|L的位置,然后贪心的翻转. */
replace
32
38
32
37
0
p02918
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long #define rep(i, l, r) for (int i = (int)(l); i < (int)(r); i++) #define all(x) (x).begin(), (x).end() #define sz(x) ((int)x.size()) template <class T> bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<vi> vvi; const int inf = 1LL << 60; const int mod = 1e9 + 7; const double eps = 1e-9; /*{ 11121 114 1211121121 41121121 621121 9121 55 10 3111112 51112 514 }*/ signed main() { int n, k; string s; cin >> n >> k >> s; vi a; int cnt = 1; char cur = s[0]; rep(i, 1, n) { if (cur == s[i]) { cnt++; } else { a.emplace_back(cnt); cnt = 1; } cur = s[i]; } if (cnt) a.emplace_back(cnt); int len = 2 * k + 1, ans; if (sz(a) <= len) { int sum = 0; for (int i : a) sum += i; ans = sum - 1; } else { vi s(n + 1), s2(sz(a) + 1); rep(i, 0, n) s[i + 1] = s[i] + a[i]; rep(i, 0, sz(a)) s2[i + 1] = s2[i] + a[i] - 1; ans = 0; rep(i, 0, sz(a) - len + 1) { int tmp = s[i + len] - s[i] - 1 + s2[sz(a)] - s2[i + len] + s2[i]; // cout << i << ":" << tmp << endl; chmax(ans, tmp); } } cout << ans << endl; // rep(i, 0, sz(a)) cout << a[i] << " "; // cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long #define rep(i, l, r) for (int i = (int)(l); i < (int)(r); i++) #define all(x) (x).begin(), (x).end() #define sz(x) ((int)x.size()) template <class T> bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<vi> vvi; const int inf = 1LL << 60; const int mod = 1e9 + 7; const double eps = 1e-9; /*{ 11121 114 1211121121 41121121 621121 9121 55 10 3111112 51112 514 }*/ signed main() { int n, k; string s; cin >> n >> k >> s; vi a; int cnt = 1; char cur = s[0]; rep(i, 1, n) { if (cur == s[i]) { cnt++; } else { a.emplace_back(cnt); cnt = 1; } cur = s[i]; } if (cnt) a.emplace_back(cnt); int len = 2 * k + 1, ans; if (sz(a) <= len) { int sum = 0; for (int i : a) sum += i; ans = sum - 1; } else { vi s(sz(a) + 1), s2(sz(a) + 1); rep(i, 0, sz(a)) s[i + 1] = s[i] + a[i]; rep(i, 0, sz(a)) s2[i + 1] = s2[i] + a[i] - 1; ans = 0; rep(i, 0, sz(a) - len + 1) { int tmp = s[i + len] - s[i] - 1 + s2[sz(a)] - s2[i + len] + s2[i]; // cout << i << ":" << tmp << endl; chmax(ans, tmp); } } cout << ans << endl; // rep(i, 0, sz(a)) cout << a[i] << " "; // cout << endl; return 0; }
replace
74
76
74
76
0
p02918
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { int n, k; string s; cin >> n >> k; cin >> s; char x = s.at(0); int st = 1; int en = 0; for (int i = 0; i < k; i++) { en = s.length(); for (int j = st - 1; j < s.length(); j++) { if (s.at(j) == x) continue; else { st = j; for (int k = j; k < s.length(); k++) { if (s.at(st) != s.at(k)) { en = k; break; } } break; } } for (int j = st; j < en; j++) s.at(j) = x; } ll ans = 0; for (int i = 0; i < s.length(); i++) { if (s.at(i) == 'L') { if (i == 0) continue; else { if (s.at(i - 1) == 'L') ans++; } } else { if (i == s.length() - 1) continue; else { if (s.at(i + 1) == 'R') ans++; } } } // cout << s << endl; cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { int n, k; string s; cin >> n >> k; cin >> s; char x = s.at(0); int st = 1; int en = 0; for (int i = 0; i < k; i++) { en = s.length(); for (int j = st - 1; j < s.length(); j++) { if (s.at(j) == x) continue; else { st = j; for (int k = j; k < s.length(); k++) { if (s.at(st) != s.at(k)) { en = k; break; } } break; } } for (int j = st; j < en; j++) s.at(j) = x; st = en - 1; } ll ans = 0; for (int i = 0; i < s.length(); i++) { if (s.at(i) == 'L') { if (i == 0) continue; else { if (s.at(i - 1) == 'L') ans++; } } else { if (i == s.length() - 1) continue; else { if (s.at(i + 1) == 'R') ans++; } } } // cout << s << endl; cout << ans << endl; }
insert
30
30
30
31
TLE
p02918
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { ll N, K; string S; cin >> N >> K >> S; ll t = 0; for (ll i = 0; i < K; i++) { for (ll j = t; j < N; j++) { if (S[0] != S[j] && S[j] == S[j + 1]) { S[j] = S[0]; } else if (S[0] != S[j]) { S[j] = S[0]; t = j + 1; break; } } } ll ans = 0; for (ll i = 0; i < N; i++) { if (i != 0 && S[i] == 'L' && S[i - 1] == 'L') { ans++; } if (i != N - 1 && S[i] == 'R' && S[i + 1] == 'R') { ans++; } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { ll N, K; string S; cin >> N >> K >> S; ll t = 0; for (ll i = 0; i < K; i++) { for (ll j = t; j < N; j++) { if (S[0] != S[j] && S[j] == S[j + 1]) { S[j] = S[0]; } else if (S[0] != S[j]) { S[j] = S[0]; t = j + 1; break; } else { t = j + 1; } } } ll ans = 0; for (ll i = 0; i < N; i++) { if (i != 0 && S[i] == 'L' && S[i - 1] == 'L') { ans++; } if (i != N - 1 && S[i] == 'R' && S[i + 1] == 'R') { ans++; } } cout << ans << endl; }
insert
18
18
18
20
TLE
p02918
C++
Runtime Error
#include <bits/stdc++.h> #define f first #define s second #define inf 999999999999 #define N 500009 #define M (L + R) / 2 #define ll long long #define pb push_back using namespace std; ll n, k, ans, len, KL, KR; string A, L, R; ll check(string x) { ll pas = 0; ll nn = x.size(); for (int i = 0; i < nn - 1; i++) { if (x[i] == 'R' && x[i + 1] == 'R') pas++; } for (int i = nn; i > 0; i--) { if (x[i] == 'L' && x[i - 1] == 'L') pas++; } return pas; } vector<pair<ll, pair<ll, ll>>> vL, vR; int main() { ios::sync_with_stdio(0); cin >> n >> k; cin >> A; ans = check(A); L = A; L += '*'; len = 0; for (int i = 0; i < n; i++) { if (i > 0 && L[i] == 'L' && L[i - 1] == 'R' && L[i + 1] == 'R') { vL.pb({5, {i, i}}); len = 0; continue; } if (L[i] == 'L') len++; else continue; if (L[i] == 'L' && L[i + 1] != 'L') { vL.pb({len * 2, {i - len + 1, i}}); len = 0; } } R = A; R += '*'; len = 0; for (int i = 0; i < n; i++) { if (i > 0 && R[i] == 'R' && R[i - 1] == 'L' && R[i + 1] == 'L') { vR.pb({5, {i, i}}); len = 0; continue; } if (R[i] == 'R') len++; else continue; if (R[i] == 'R' && R[i + 1] != 'R') { vR.pb({len * 2, {i - len + 1, i}}); len = 0; } } sort(vL.begin(), vL.end()); sort(vR.begin(), vR.end()); /* for(int i=0;i<vL.size();i++){ cout<<vL[i].f<<" "<<vL[i].s.f<<" - "<<vL[i].s.s<<endl; } */ L = A; KL = k; int TRY = vL.size(); while (KL--) { TRY--; for (int i = vL[TRY].s.f; i <= vL[TRY].s.s; i++) { L[i] = 'R'; } if (KL < 100) ans = max(ans, check(L)); // cout<<L<<endl; } R = A; KR = k; TRY = vR.size(); while (KR--) { TRY--; for (int i = vR[TRY].s.f; i <= vR[TRY].s.s; i++) { R[i] = 'L'; } if (KR < 100) ans = max(ans, check(R)); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define f first #define s second #define inf 999999999999 #define N 500009 #define M (L + R) / 2 #define ll long long #define pb push_back using namespace std; ll n, k, ans, len, KL, KR; string A, L, R; ll check(string x) { ll pas = 0; ll nn = x.size(); for (int i = 0; i < nn - 1; i++) { if (x[i] == 'R' && x[i + 1] == 'R') pas++; } for (int i = nn; i > 0; i--) { if (x[i] == 'L' && x[i - 1] == 'L') pas++; } return pas; } vector<pair<ll, pair<ll, ll>>> vL, vR; int main() { ios::sync_with_stdio(0); cin >> n >> k; cin >> A; ans = check(A); ans = min(n - 1, ans + 2 * k); cout << ans << endl; return 0; }
replace
37
109
37
38
0
p02918
C++
Runtime Error
#include <cstdio> #include <cstring> using namespace std; int min(int a, int b); int main() { char queue[10000]; int happy_num = 0; int len, time; scanf("%d%d", &len, &time); scanf("%s", queue); for (int i = 0; i < strlen(queue); i++) { if ((i != strlen(queue) - 1) && (queue[i + 1] == 'R') && (queue[i] == 'R')) happy_num++; else if ((i != 0) && (queue[i - 1] == 'L') && (queue[i] == 'L')) happy_num++; } printf("%d", min((happy_num + 2 * time), len - 1)); return 0; } int min(int a, int b) { return (a >= b) ? b : a; }
#include <cstdio> #include <cstring> using namespace std; int min(int a, int b); int main() { char queue[100000]; int happy_num = 0; int len, time; scanf("%d%d", &len, &time); scanf("%s", queue); for (int i = 0; i < strlen(queue); i++) { if ((i != strlen(queue) - 1) && (queue[i + 1] == 'R') && (queue[i] == 'R')) happy_num++; else if ((i != 0) && (queue[i - 1] == 'L') && (queue[i] == 'L')) happy_num++; } printf("%d", min((happy_num + 2 * time), len - 1)); return 0; } int min(int a, int b) { return (a >= b) ? b : a; }
replace
5
6
5
6
0
p02918
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int N, K; cin >> N >> K; string S; cin >> S; vector<int> pos; char cur = 'a'; for (int i = 0; i < N; i++) { if (cur != S[i]) { cur = S[i]; pos.emplace_back(i); } } pos.emplace_back(N); cur = S[0]; for (int i = 1; i < (int)pos.size() && K > 0; i += 2) { for (int j = pos[i]; j < pos[i + 1]; j++) { S[j] = cur; } K--; } int res = 0; for (int i = 0; i < N - 1; i++) { if (S[i] == S[i + 1]) res++; } cout << res << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int N, K; cin >> N >> K; string S; cin >> S; vector<int> pos; char cur = 'a'; for (int i = 0; i < N; i++) { if (cur != S[i]) { cur = S[i]; pos.emplace_back(i); } } pos.emplace_back(N); cur = S[0]; for (int i = 1; i < (int)pos.size() - 1 && K > 0; i += 2) { for (int j = pos[i]; j < pos[i + 1]; j++) { S[j] = cur; } K--; } int res = 0; for (int i = 0; i < N - 1; i++) { if (S[i] == S[i + 1]) res++; } cout << res << '\n'; return 0; }
replace
21
22
21
22
0
p02918
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #define ll long long using namespace std; const int N = 110; const int mod = 10007; inline int read() { int jg = 0, jk = getchar() - '0', f = 1; while (jk < 0 || jk > 9) { if (jk == '-' - '0') f = -1; jk = getchar() - '0'; } while (jk >= 0 && jk <= 9) jg *= 10, jg += jk, jk = getchar() - '0'; return jg * f; } int n, ans, k; char s[N]; int main() { // freopen("a.txt", "r", stdin); n = read(), k = read(); scanf("%s", s); for (int i = 1; i < n; ++i) if (s[i] == s[i - 1]) ans++; ans += 2 * k; ans = min(ans, n - 1); printf("%d", ans); return 0; }
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #define ll long long using namespace std; const int N = 100010; const int mod = 10007; inline int read() { int jg = 0, jk = getchar() - '0', f = 1; while (jk < 0 || jk > 9) { if (jk == '-' - '0') f = -1; jk = getchar() - '0'; } while (jk >= 0 && jk <= 9) jg *= 10, jg += jk, jk = getchar() - '0'; return jg * f; } int n, ans, k; char s[N]; int main() { // freopen("a.txt", "r", stdin); n = read(), k = read(); scanf("%s", s); for (int i = 1; i < n; ++i) if (s[i] == s[i - 1]) ans++; ans += 2 * k; ans = min(ans, n - 1); printf("%d", ans); return 0; }
replace
9
10
9
10
0
p02918
C++
Runtime Error
// From Naruto to Hokage // #include <RJ> #include <bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define endl '\n' #define mii map<ll, ll> #define pii pair<ll, ll> #define vi vector<ll> #define all(a) (a).begin(), (a).end() #define F first #define S second #define sz(x) (ll) x.size() #define hell 1000000007 #define INF (1ll << 60) #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 ios \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define time \ cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n"; ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); } ll lcm(ll a, ll b) { return a * (b / gcd(a, b)); } const int N = 100005; void solve() { ll n, k; cin >> n >> k; string s; cin >> s; // t=s; ll i = 0, c = 0; vi temp; while (i < n) { while (i < n && s[i] == 'R') c++, i++; if (c) temp.pb(c); c = 0; while (i < n && s[i] == 'L') c++, i++; if (c) temp.pb(c); c = 0; } // rep(i,0,sz(temp)-1) cout<<temp[i]<<" \n"[i==sz(temp)-1]; vector<pii> odd; vector<pii> even; rep(i, 0, sz(temp) - 1) { if (i % 2 == 0) even.pb({temp[i], i}); else odd.pb({temp[i], i}); } sort(all(even), greater<pii>()); sort(all(odd), greater<pii>()); bool vis[sz(temp)]; memset(vis, 0, sizeof(vis)); rep(i, 0, k - 1) vis[even[i].S] = 1; rep(i, 0, k - 1) vis[odd[i].S] = 1; ll ans = 0, curr = 0; ll kk = 0; // rep(i,0,sz(temp)-1) cout<<vis[i]<<" \n"[i==sz(temp)-1]; rep(i, 0, sz(temp) - 1) { if (i % 2 == 0 && vis[i]) { curr += temp[i]; } else if (i % 2 == 1) curr += temp[i]; else { if (curr) ans += curr - 1; curr = 0; } } if (curr) ans += curr - 1; kk = max(ans, kk); // cout<<ans<<endl; ans = 0; curr = 0; rep(i, 0, sz(temp) - 1) { if (i % 2 == 1 && vis[i]) { curr += temp[i]; } else if (i % 2 == 0) curr += temp[i]; else { ans += curr - 1; curr = 0; } } if (curr) ans += curr - 1; kk = max(ans, kk); cout << kk; } signed main() { ios int TESTS = 1; // cin>>TESTS; while (TESTS--) { solve(); } time return 0; }
// From Naruto to Hokage // #include <RJ> #include <bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define endl '\n' #define mii map<ll, ll> #define pii pair<ll, ll> #define vi vector<ll> #define all(a) (a).begin(), (a).end() #define F first #define S second #define sz(x) (ll) x.size() #define hell 1000000007 #define INF (1ll << 60) #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 ios \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define time \ cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n"; ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); } ll lcm(ll a, ll b) { return a * (b / gcd(a, b)); } const int N = 100005; void solve() { ll n, k; cin >> n >> k; string s; cin >> s; // t=s; ll i = 0, c = 0; vi temp; while (i < n) { while (i < n && s[i] == 'R') c++, i++; if (c) temp.pb(c); c = 0; while (i < n && s[i] == 'L') c++, i++; if (c) temp.pb(c); c = 0; } // rep(i,0,sz(temp)-1) cout<<temp[i]<<" \n"[i==sz(temp)-1]; ll ans = 0; for (auto i : temp) ans += i - 1; ans += min(sz(temp) - 1, k) * 2; cout << min(ans, n - 1); } signed main() { ios int TESTS = 1; // cin>>TESTS; while (TESTS--) { solve(); } time return 0; }
replace
49
97
49
54
0
Time elapsed: 31ms
p02919
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <set> #include <utility> #include <vector> using namespace std; int main() { int n; cin >> n; vector<pair<int, int>> p(n); for (int i = 0; i < n; ++i) cin >> p[i].first, p[i].second = i; sort(p.begin(), p.end(), greater<>()); set<int> se; se.insert(-1); se.insert(n); se.insert(p[0].second); long long ans = 0; for (int i = 1; i < n; ++i) { int now = p[i].second; auto r = lower_bound(se.begin(), se.end(), now); auto l = r; l--; long long lcnt1 = now - *l, rcnt1 = *r - now; long long lcnt2 = 0, rcnt2 = 0; if (l != se.begin()) l--, lcnt2 = now - *l - lcnt1; r++; if (r != se.end()) rcnt2 = *r - now - rcnt1; ans += p[i].first * (lcnt1 * rcnt2 + rcnt1 * lcnt2); se.insert(now); } cout << ans << endl; return 0; }
#include <algorithm> #include <iostream> #include <set> #include <utility> #include <vector> using namespace std; int main() { int n; cin >> n; vector<pair<int, int>> p(n); for (int i = 0; i < n; ++i) cin >> p[i].first, p[i].second = i; sort(p.begin(), p.end(), greater<>()); set<int> se; se.insert(-1); se.insert(n); se.insert(p[0].second); long long ans = 0; for (int i = 1; i < n; ++i) { int now = p[i].second; auto r = se.lower_bound(now); auto l = r; l--; long long lcnt1 = now - *l, rcnt1 = *r - now; long long lcnt2 = 0, rcnt2 = 0; if (l != se.begin()) l--, lcnt2 = now - *l - lcnt1; r++; if (r != se.end()) rcnt2 = *r - now - rcnt1; ans += p[i].first * (lcnt1 * rcnt2 + rcnt1 * lcnt2); se.insert(now); } cout << ans << endl; return 0; }
replace
22
23
22
23
TLE
p02919
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define INF 1e9 #define llINF 1e18 #define MOD 1000000007 #define pb push_back #define mp make_pair #define F first #define S second #define ll long long #define ull unsigned long long #define vi vector<ll> #define vvi vector<vi> #define DBG_N(hoge) cerr << " " << (hoge) << endl; #define DBG cerr << "!" << endl; #define BITLE(n) (1LL << ((ll)n)) #define BITCNT(n) (__builtin_popcountll(n)) #define SUBS(s, f, t) ((s).substr((f)-1, (t) - (f) + 1)) #define ALL(a) (a).begin(), (a).end() using namespace std; ll po[222222]; int main() { cin.tie(0); ios::sync_with_stdio(false); ll n; cin >> n; vi A(n); for (int i = 0; i < n; i++) { cin >> A[i]; po[A[i]] = i + 1; } set<ll> se; se.insert(po[n]); se.insert(0); se.insert(n + 1); // for(auto a:se)cout<<a <<" "; // cout<<endl; ll ans = 0; for (ll i = n - 1; i >= 1; i--) { auto lb = lower_bound(ALL(se), po[i]); ll num = *lb; --lb; ll num2 = *lb; if (num == n + 1) { --lb; ll num3 = *lb; ans += (num - po[i]) * (num2 - num3) * i; } else if (num2 == 0) { // auto lb3=lower_bound(ALL(se),num+1); ++lb; ++lb; ll num3 = *lb; ans += po[i] * (num3 - num) * i; } else { --lb; ll num4 = *lb; ++lb; ++lb; ++lb; // auto lb3=lower_bound(ALL(se),num+1); ll num3 = *lb; ans += (po[i] - num2) * (num3 - num) * i; ans += (num - po[i]) * (num2 - num4) * i; } se.insert(po[i]); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define INF 1e9 #define llINF 1e18 #define MOD 1000000007 #define pb push_back #define mp make_pair #define F first #define S second #define ll long long #define ull unsigned long long #define vi vector<ll> #define vvi vector<vi> #define DBG_N(hoge) cerr << " " << (hoge) << endl; #define DBG cerr << "!" << endl; #define BITLE(n) (1LL << ((ll)n)) #define BITCNT(n) (__builtin_popcountll(n)) #define SUBS(s, f, t) ((s).substr((f)-1, (t) - (f) + 1)) #define ALL(a) (a).begin(), (a).end() using namespace std; ll po[222222]; int main() { cin.tie(0); ios::sync_with_stdio(false); ll n; cin >> n; vi A(n); for (int i = 0; i < n; i++) { cin >> A[i]; po[A[i]] = i + 1; } set<ll> se; se.insert(po[n]); se.insert(0); se.insert(n + 1); // for(auto a:se)cout<<a <<" "; // cout<<endl; ll ans = 0; for (ll i = n - 1; i >= 1; i--) { auto lb = se.lower_bound(po[i]); ll num = *lb; --lb; ll num2 = *lb; if (num == n + 1) { --lb; ll num3 = *lb; ans += (num - po[i]) * (num2 - num3) * i; } else if (num2 == 0) { // auto lb3=lower_bound(ALL(se),num+1); ++lb; ++lb; ll num3 = *lb; ans += po[i] * (num3 - num) * i; } else { --lb; ll num4 = *lb; ++lb; ++lb; ++lb; // auto lb3=lower_bound(ALL(se),num+1); ll num3 = *lb; ans += (po[i] - num2) * (num3 - num) * i; ans += (num - po[i]) * (num2 - num4) * i; } se.insert(po[i]); } cout << ans << endl; return 0; }
replace
38
39
38
39
TLE
p02919
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)n; i++) #define rep2(i, s, n) for (int i = s; i < (int)n; i++) #define all(obj) obj.begin(), obj.end() #define debug(x) cerr << #x << ":" << x << "\n" #define vdebug(vec) \ cerr << #vec << ":"; \ for (auto e : vec) \ cerr << e << " "; \ cout << "\n" #define YN(f) cout << (f ? "YES" : "NO") << endl #define Yn(f) cout << (f ? "Yes" : "No") << endl #define yn(f) cout << (f ? "yes" : "no") << endl using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using pii = pair<int, int>; int main() { int N; cin >> N; vi P(N); // index -> value map<int, int> m; // value -> index rep(i, N) { // 0-indexed cin >> P.at(i); // index -> value m[P.at(i)] = i; // value -> index } // vdebug(P); // for (auto mi : m) cout << mi.first << " " << mi.second << endl; set<int> s; s.insert(-2); s.insert(-1); s.insert(N); s.insert(N + 1); // 番兵 ※重複不可のため ll ans = 0; for (int k = N; k > 0; k--) { // k:value s.insert(m.at(k)); // indexを入れる auto it = lower_bound(all(s), m.at(k)); // 値kに対応する添字xに対応するiterator ll l2 = max(*prev(prev(it)), -1); ll l1 = *prev(it); ll x = *it; ll r1 = *next(it); ll r2 = min(*next(next(it)), N); // debug(k); // for (auto si : s) cout << si << " "; cout << endl; // debug (l2); debug(l1); debug(x); debug(r1); debug(r2); ll temp = k * ((l1 - l2) * (r1 - x) + (x - l1) * (r2 - r1)); // debug(temp); ans += temp; } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)n; i++) #define rep2(i, s, n) for (int i = s; i < (int)n; i++) #define all(obj) obj.begin(), obj.end() #define debug(x) cerr << #x << ":" << x << "\n" #define vdebug(vec) \ cerr << #vec << ":"; \ for (auto e : vec) \ cerr << e << " "; \ cout << "\n" #define YN(f) cout << (f ? "YES" : "NO") << endl #define Yn(f) cout << (f ? "Yes" : "No") << endl #define yn(f) cout << (f ? "yes" : "no") << endl using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using pii = pair<int, int>; int main() { int N; cin >> N; vi P(N); // index -> value map<int, int> m; // value -> index rep(i, N) { // 0-indexed cin >> P.at(i); // index -> value m[P.at(i)] = i; // value -> index } // vdebug(P); // for (auto mi : m) cout << mi.first << " " << mi.second << endl; set<int> s; s.insert(-2); s.insert(-1); s.insert(N); s.insert(N + 1); // 番兵 ※重複不可のため ll ans = 0; for (int k = N; k > 0; k--) { // k:value s.insert(m.at(k)); // indexを入れる auto it = s.lower_bound(m.at(k)); // 値kに対応する添字xに対応するiterator // auto it = lower_bound(all(s), m.at(k)); // ダメな例 ll l2 = max(*prev(prev(it)), -1); ll l1 = *prev(it); ll x = *it; ll r1 = *next(it); ll r2 = min(*next(next(it)), N); // debug(k); // for (auto si : s) cout << si << " "; cout << endl; // debug (l2); debug(l1); debug(x); debug(r1); debug(r2); ll temp = k * ((l1 - l2) * (r1 - x) + (x - l1) * (r2 - r1)); // debug(temp); ans += temp; } cout << ans << endl; }
replace
38
40
38
40
TLE
p02919
C++
Time Limit Exceeded
#include "bits/stdc++.h" using namespace std; class Timer { std::chrono::high_resolution_clock::time_point m_start; public: Timer() : m_start(std::chrono::high_resolution_clock::now()) {} ~Timer() { auto end = std::chrono::high_resolution_clock::now(); auto duration_s = std::chrono::duration<double>(end - m_start).count(); cerr << "Completed in " << duration_s << endl; } }; template <class c> struct rge { c b, e; }; template <class c> rge<c> range(c i, c j) { return rge<c>{i, j}; } template <class c> auto dud(c *x) -> decltype(cerr << *x, 0); template <class c> char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } template <class c> typename enable_if<sizeof dud<c>(0) != 1, debug &>::type operator<<(c i) { cerr << boolalpha << i; return *this; } template <class c> typename enable_if<sizeof dud<c>(0) == 1, debug &>::type operator<<(c i) { return *this << range(begin(i), end(i)); } template <class c, class b> debug &operator<<(pair<b, c> d) { return *this << "(" << d.first << ", " << d.second << ")"; } template <class c> debug &operator<<(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; return *this << "]"; } #else template <class c> debug &operator<<(const c &) { return *this; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " using ll = long long; const int INF = 1e9 + 5; #define FAST \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #include <iostream> void solve() { ll n; cin >> n; vector<int> p(n); unordered_map<int, int> mp; for (int i = 0; i < n; ++i) { cin >> p[i]; } ll sum = 0; if (/*is_sorted(p.begin(), p.end()) || */ is_sorted(p.rbegin(), p.rend())) { cout << n * (n - 1) * (2 * n - 1) / 6 << endl; return; } Timer timer; for (int i = 0; i < n; ++i) { ll a = 0, b = 0, c = 0, d = 0; bool ar = false, dr = false; int counter = i; while (--counter >= 0) { if (p[counter] > p[i]) { ar = true; break; } ++a; } while (--counter >= 0) { if (p[counter] > p[i]) { break; } ++b; } counter = i; while (++counter < n) { if (p[counter] > p[i]) { dr = true; break; } ++c; } while (++counter < n) { if (p[counter] > p[i]) { break; } ++d; } if (ar) { sum += (b + 1) * (c + 1) * ll(p[i]); } if (dr) { sum += (a + 1) * (d + 1) * ll(p[i]); } } cout << sum << endl; } int main() { FAST int t = 1; // cin >> t; for (int i = 0; i < t; ++i) { solve(); } }
#include "bits/stdc++.h" using namespace std; class Timer { std::chrono::high_resolution_clock::time_point m_start; public: Timer() : m_start(std::chrono::high_resolution_clock::now()) {} ~Timer() { auto end = std::chrono::high_resolution_clock::now(); auto duration_s = std::chrono::duration<double>(end - m_start).count(); cerr << "Completed in " << duration_s << endl; } }; template <class c> struct rge { c b, e; }; template <class c> rge<c> range(c i, c j) { return rge<c>{i, j}; } template <class c> auto dud(c *x) -> decltype(cerr << *x, 0); template <class c> char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } template <class c> typename enable_if<sizeof dud<c>(0) != 1, debug &>::type operator<<(c i) { cerr << boolalpha << i; return *this; } template <class c> typename enable_if<sizeof dud<c>(0) == 1, debug &>::type operator<<(c i) { return *this << range(begin(i), end(i)); } template <class c, class b> debug &operator<<(pair<b, c> d) { return *this << "(" << d.first << ", " << d.second << ")"; } template <class c> debug &operator<<(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; return *this << "]"; } #else template <class c> debug &operator<<(const c &) { return *this; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " using ll = long long; const int INF = 1e9 + 5; #define FAST \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #include <iostream> void solve() { ll n; cin >> n; vector<int> p(n); unordered_map<int, int> mp; for (int i = 0; i < n; ++i) { cin >> p[i]; } ll sum = 0; if (is_sorted(p.begin(), p.end()) || is_sorted(p.rbegin(), p.rend())) { cout << n * (n - 1) * (2 * n - 1) / 6 << endl; return; } Timer timer; for (int i = 0; i < n; ++i) { ll a = 0, b = 0, c = 0, d = 0; bool ar = false, dr = false; int counter = i; while (--counter >= 0) { if (p[counter] > p[i]) { ar = true; break; } ++a; } while (--counter >= 0) { if (p[counter] > p[i]) { break; } ++b; } counter = i; while (++counter < n) { if (p[counter] > p[i]) { dr = true; break; } ++c; } while (++counter < n) { if (p[counter] > p[i]) { break; } ++d; } if (ar) { sum += (b + 1) * (c + 1) * ll(p[i]); } if (dr) { sum += (a + 1) * (d + 1) * ll(p[i]); } } cout << sum << endl; } int main() { FAST int t = 1; // cin >> t; for (int i = 0; i < t; ++i) { solve(); } }
replace
72
73
72
73
TLE
p02919
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> #define rep(i, a, b) for (int i = (a); i < (b); i++) #define per(i, a, b) for (int i = (b)-1; i >= (a); i--) #define pb push_back #define mp make_pair #define bg begin() #define en end() #define all(v) (v).begin(), (v).end() #define rall(v) (v).rbegin(), (v).rend() #define sz(v) (int)(v).size() using namespace std; typedef long long ll; typedef long double ld; using P = pair<int, int>; static const long long MOD = 1000000007; static const long long LINF = (ll)(1e18 + 99); static const int INF = 1e9 + 99; long long dat[200200], dat2[200200]; int n = 1; int p[100005]; ll LL[100005], LR[100005], RL[100005], RR[100005]; void init(int _n) { while (n < _n) n *= 2; for (int i = 0; i < 2 * n - 1; i++) dat[i] = LINF; for (int i = 0; i < 2 * n - 1; i++) dat2[i] = -LINF; return; } void built() { for (int i = n - 2; i >= 0; i--) { dat[i] = min(dat[2 * i + 1], dat[2 * i + 2]); dat2[i] = max(dat2[2 * i + 1], dat2[2 * i + 2]); } return; } void update(int k, long long a) { k += n - 1; dat[k] = a; while (k > 0) { k = (k - 1) / 2; dat[k] = min(dat[2 * k + 1], dat[2 * k + 2]); } return; } long long query(int a, int b, int l, int r, int k) { if (b <= l || a >= r) return LINF; if (a <= l && b >= r) return dat[k]; long long vl = query(a, b, l, (r + l) / 2, 2 * k + 1); long long vr = query(a, b, (r + l) / 2, r, 2 * k + 2); return min(vl, vr); } void update2(int k, long long a) { k += n - 1; dat2[k] = a; while (k > 0) { k = (k - 1) / 2; dat2[k] = max(dat2[2 * k + 1], dat2[2 * k + 2]); } return; } long long query2(int a, int b, int l, int r, int k) { if (b <= l || a >= r) return -LINF; if (a <= l && b >= r) return dat2[k]; long long vl = query2(a, b, l, (r + l) / 2, 2 * k + 1); long long vr = query2(a, b, (r + l) / 2, r, 2 * k + 2); return max(vl, vr); } int main(void) { int N; cin >> N; rep(i, 0, N) cin >> p[i]; init(N + 5); rep(i, 0, N) { dat[p[i] + n - 1] = (ll)i; dat2[p[i] + n - 1] = (ll)i; } built(); rep(i, 0, N) { ll l = LINF, r = LINF; l = query(p[i] + 1, N + 1, 0, n, 0); if (l != LINF) { update(p[l], LINF); r = query(p[i] + 1, N + 1, 0, n, 0); update(p[l], l); } update(p[i], LINF); if (l == LINF) l = N; if (r == LINF) r = N; RL[p[i]] = l; RR[p[i]] = r; } for (int i = N - 1; i >= 0; i--) { ll l = -LINF, r = -LINF; r = query2(p[i] + 1, N + 1, 0, n, 0); if (r != -LINF) { update2(p[r], -LINF); l = query2(p[i] + 1, N + 1, 0, n, 0); update2(p[r], r); } update2(p[i], -LINF); if (l == -LINF) l = -1; if (r == -LINF) r = -1; LL[p[i]] = l; LR[p[i]] = r; } ll ans = 0; rep(i, 0, N) { int x = p[i]; // printf("x:%d, LL[x]:%lld, LR[x]:%lld, RL[x]:%lld, RR[x]:%lld\n", x, // LL[x], LR[x], RL[x], RR[x]); ans += (RR[x] - RL[x]) * (i - LR[x]) * x; ans += (LR[x] - LL[x]) * (RL[x] - i) * x; // printf("%lld\n", ans); } cout << ans << endl; return 0; }
#include <algorithm> #include <cmath> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> #define rep(i, a, b) for (int i = (a); i < (b); i++) #define per(i, a, b) for (int i = (b)-1; i >= (a); i--) #define pb push_back #define mp make_pair #define bg begin() #define en end() #define all(v) (v).begin(), (v).end() #define rall(v) (v).rbegin(), (v).rend() #define sz(v) (int)(v).size() using namespace std; typedef long long ll; typedef long double ld; using P = pair<int, int>; static const long long MOD = 1000000007; static const long long LINF = (ll)(1e18 + 99); static const int INF = 1e9 + 99; long long dat[500200], dat2[500200]; int n = 1; int p[100005]; ll LL[100005], LR[100005], RL[100005], RR[100005]; void init(int _n) { while (n < _n) n *= 2; for (int i = 0; i < 2 * n - 1; i++) dat[i] = LINF; for (int i = 0; i < 2 * n - 1; i++) dat2[i] = -LINF; return; } void built() { for (int i = n - 2; i >= 0; i--) { dat[i] = min(dat[2 * i + 1], dat[2 * i + 2]); dat2[i] = max(dat2[2 * i + 1], dat2[2 * i + 2]); } return; } void update(int k, long long a) { k += n - 1; dat[k] = a; while (k > 0) { k = (k - 1) / 2; dat[k] = min(dat[2 * k + 1], dat[2 * k + 2]); } return; } long long query(int a, int b, int l, int r, int k) { if (b <= l || a >= r) return LINF; if (a <= l && b >= r) return dat[k]; long long vl = query(a, b, l, (r + l) / 2, 2 * k + 1); long long vr = query(a, b, (r + l) / 2, r, 2 * k + 2); return min(vl, vr); } void update2(int k, long long a) { k += n - 1; dat2[k] = a; while (k > 0) { k = (k - 1) / 2; dat2[k] = max(dat2[2 * k + 1], dat2[2 * k + 2]); } return; } long long query2(int a, int b, int l, int r, int k) { if (b <= l || a >= r) return -LINF; if (a <= l && b >= r) return dat2[k]; long long vl = query2(a, b, l, (r + l) / 2, 2 * k + 1); long long vr = query2(a, b, (r + l) / 2, r, 2 * k + 2); return max(vl, vr); } int main(void) { int N; cin >> N; rep(i, 0, N) cin >> p[i]; init(N + 5); rep(i, 0, N) { dat[p[i] + n - 1] = (ll)i; dat2[p[i] + n - 1] = (ll)i; } built(); rep(i, 0, N) { ll l = LINF, r = LINF; l = query(p[i] + 1, N + 1, 0, n, 0); if (l != LINF) { update(p[l], LINF); r = query(p[i] + 1, N + 1, 0, n, 0); update(p[l], l); } update(p[i], LINF); if (l == LINF) l = N; if (r == LINF) r = N; RL[p[i]] = l; RR[p[i]] = r; } for (int i = N - 1; i >= 0; i--) { ll l = -LINF, r = -LINF; r = query2(p[i] + 1, N + 1, 0, n, 0); if (r != -LINF) { update2(p[r], -LINF); l = query2(p[i] + 1, N + 1, 0, n, 0); update2(p[r], r); } update2(p[i], -LINF); if (l == -LINF) l = -1; if (r == -LINF) r = -1; LL[p[i]] = l; LR[p[i]] = r; } ll ans = 0; rep(i, 0, N) { int x = p[i]; // printf("x:%d, LL[x]:%lld, LR[x]:%lld, RL[x]:%lld, RR[x]:%lld\n", x, // LL[x], LR[x], RL[x], RR[x]); ans += (RR[x] - RL[x]) * (i - LR[x]) * x; ans += (LR[x] - LL[x]) * (RL[x] - i) * x; // printf("%lld\n", ans); } cout << ans << endl; return 0; }
replace
31
32
31
32
0
p02919
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <cstdint> #include <cstdio> #include <ctime> #include <deque> #include <float.h> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <map> #include <math.h> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <tuple> #include <type_traits> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; using ll = long long; unsigned euclidean_gcd(unsigned a, unsigned b) { if (a < b) return euclidean_gcd(b, a); unsigned r; while ((r = a % b)) { a = b; b = r; } return b; } ll ll_gcd(ll a, ll b) { if (a < b) return ll_gcd(b, a); ll r; while ((r = a % b)) { a = b; b = r; } return b; } struct UnionFind { vector<ll> par; vector<ll> siz; UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) { for (ll i = 0; i < sz_; ++i) par[i] = i; } void init(ll sz_) { par.resize(sz_); siz.assign(sz_, 1LL); for (ll i = 0; i < sz_; ++i) par[i] = i; } ll root(ll x) { while (par[x] != x) { x = par[x] = par[par[x]]; } return x; } bool merge(ll x, ll y) { x = root(x); y = root(y); if (x == y) return false; if (siz[x] < siz[y]) swap(x, y); siz[x] += siz[y]; par[y] = x; return true; } bool issame(ll x, ll y) { return root(x) == root(y); } ll size(ll x) { return siz[root(x)]; } }; 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; } long long modinv(long long a, long long mod) { return modpow(a, mod - 2, mod); } vector<int> tpsort(vector<vector<int>> &G) { int V = G.size(); vector<int> sorted_vertices; queue<int> que; vector<int> indegree(V); for (int i = 0; i < V; i++) { for (int j = 0; j < G[i].size(); j++) { indegree[G[i][j]]++; } } for (int i = 0; i < V; i++) { if (indegree[i] == 0) { que.push(i); } } while (que.empty() == false) { int v = que.front(); que.pop(); for (int i = 0; i < G[v].size(); i++) { int u = G[v][i]; indegree[u] -= 1; if (indegree[u] == 0) que.push(u); } sorted_vertices.push_back(v); } return sorted_vertices; } struct Point { double x; double y; }; struct LineSegment { Point start; Point end; }; double tenkyori(const LineSegment &line, const Point &point) { double x0 = point.x, y0 = point.y; double x1 = line.start.x, y1 = line.start.y; double x2 = line.end.x, y2 = line.end.y; double a = x2 - x1; double b = y2 - y1; double a2 = a * a; double b2 = b * b; double r2 = a2 + b2; double tt = -(a * (x1 - x0) + b * (y1 - y0)); if (tt < 0) return sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0)); else if (tt > r2) return sqrt((x2 - x0) * (x2 - x0) + (y2 - y0) * (y2 - y0)); double f1 = a * (y1 - y0) - b * (x1 - x0); return sqrt((f1 * f1) / r2); } void dfs1(vector<vector<ll>> &z, ll k, ll oya, ll &ans, vector<ll> &b) { for (auto m : z[k]) { if (m != oya) dfs1(z, m, k, ans, b); } vector<ll> s; for (auto m : z[k]) { if (m != oya) s.push_back(b[m]); } ll m = b.size() - 1; for (auto d : s) { m -= d; } b[k] = b.size() - m; if (m != 0) s.push_back(m); ll a = modinv(2, 1000000007); for (auto d : s) { a += 1000000007 - modinv(modpow(2, b.size() - d, 1000000007), 1000000007); } a += modinv(modpow(2, b.size(), 1000000007), 1000000007) * (z[k].size() - 1); ans += a; ans %= 1000000007; return; } ll merge_cnt(vector<int> &a) { int n = a.size(); if (n <= 1) { return 0; } ll cnt = 0; vector<int> b(a.begin(), a.begin() + n / 2); vector<int> c(a.begin() + n / 2, a.end()); cnt += merge_cnt(b); cnt += merge_cnt(c); int ai = 0, bi = 0, ci = 0; while (ai < n) { if (bi < b.size() && (ci == c.size() || b[bi] <= c[ci])) { a[ai++] = b[bi++]; } else { cnt += n / 2 - bi; a[ai++] = c[ci++]; } } return cnt; } int main() { ll n; cin >> n; vector<ll> z(n); vector<ll> x(n); for (int i = 0; i < n; i++) { cin >> z[i]; x[(n - z[i])] = i; } multiset<ll> s; s.insert(-1); s.insert(n); s.insert(-1); s.insert(n); ll ans = 0; for (int i = 0; i < n; i++) { auto p = lower_bound(s.begin(), s.end(), x[i]); auto q = p; q++; auto r = p; r--; auto v = r; v--; ans += ((*q - *p) * (x[i] - *r) + (*r - *v) * (*p - x[i])) * (n - i); s.insert(x[i]); } cout << ans << endl; }
#include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <cstdint> #include <cstdio> #include <ctime> #include <deque> #include <float.h> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <map> #include <math.h> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <tuple> #include <type_traits> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; using ll = long long; unsigned euclidean_gcd(unsigned a, unsigned b) { if (a < b) return euclidean_gcd(b, a); unsigned r; while ((r = a % b)) { a = b; b = r; } return b; } ll ll_gcd(ll a, ll b) { if (a < b) return ll_gcd(b, a); ll r; while ((r = a % b)) { a = b; b = r; } return b; } struct UnionFind { vector<ll> par; vector<ll> siz; UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) { for (ll i = 0; i < sz_; ++i) par[i] = i; } void init(ll sz_) { par.resize(sz_); siz.assign(sz_, 1LL); for (ll i = 0; i < sz_; ++i) par[i] = i; } ll root(ll x) { while (par[x] != x) { x = par[x] = par[par[x]]; } return x; } bool merge(ll x, ll y) { x = root(x); y = root(y); if (x == y) return false; if (siz[x] < siz[y]) swap(x, y); siz[x] += siz[y]; par[y] = x; return true; } bool issame(ll x, ll y) { return root(x) == root(y); } ll size(ll x) { return siz[root(x)]; } }; 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; } long long modinv(long long a, long long mod) { return modpow(a, mod - 2, mod); } vector<int> tpsort(vector<vector<int>> &G) { int V = G.size(); vector<int> sorted_vertices; queue<int> que; vector<int> indegree(V); for (int i = 0; i < V; i++) { for (int j = 0; j < G[i].size(); j++) { indegree[G[i][j]]++; } } for (int i = 0; i < V; i++) { if (indegree[i] == 0) { que.push(i); } } while (que.empty() == false) { int v = que.front(); que.pop(); for (int i = 0; i < G[v].size(); i++) { int u = G[v][i]; indegree[u] -= 1; if (indegree[u] == 0) que.push(u); } sorted_vertices.push_back(v); } return sorted_vertices; } struct Point { double x; double y; }; struct LineSegment { Point start; Point end; }; double tenkyori(const LineSegment &line, const Point &point) { double x0 = point.x, y0 = point.y; double x1 = line.start.x, y1 = line.start.y; double x2 = line.end.x, y2 = line.end.y; double a = x2 - x1; double b = y2 - y1; double a2 = a * a; double b2 = b * b; double r2 = a2 + b2; double tt = -(a * (x1 - x0) + b * (y1 - y0)); if (tt < 0) return sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0)); else if (tt > r2) return sqrt((x2 - x0) * (x2 - x0) + (y2 - y0) * (y2 - y0)); double f1 = a * (y1 - y0) - b * (x1 - x0); return sqrt((f1 * f1) / r2); } void dfs1(vector<vector<ll>> &z, ll k, ll oya, ll &ans, vector<ll> &b) { for (auto m : z[k]) { if (m != oya) dfs1(z, m, k, ans, b); } vector<ll> s; for (auto m : z[k]) { if (m != oya) s.push_back(b[m]); } ll m = b.size() - 1; for (auto d : s) { m -= d; } b[k] = b.size() - m; if (m != 0) s.push_back(m); ll a = modinv(2, 1000000007); for (auto d : s) { a += 1000000007 - modinv(modpow(2, b.size() - d, 1000000007), 1000000007); } a += modinv(modpow(2, b.size(), 1000000007), 1000000007) * (z[k].size() - 1); ans += a; ans %= 1000000007; return; } ll merge_cnt(vector<int> &a) { int n = a.size(); if (n <= 1) { return 0; } ll cnt = 0; vector<int> b(a.begin(), a.begin() + n / 2); vector<int> c(a.begin() + n / 2, a.end()); cnt += merge_cnt(b); cnt += merge_cnt(c); int ai = 0, bi = 0, ci = 0; while (ai < n) { if (bi < b.size() && (ci == c.size() || b[bi] <= c[ci])) { a[ai++] = b[bi++]; } else { cnt += n / 2 - bi; a[ai++] = c[ci++]; } } return cnt; } int main() { ll n; cin >> n; vector<ll> z(n); vector<ll> x(n); for (int i = 0; i < n; i++) { cin >> z[i]; x[(n - z[i])] = i; } multiset<ll> s; s.insert(-1); s.insert(n); s.insert(-1); s.insert(n); ll ans = 0; for (int i = 0; i < n; i++) { auto p = s.lower_bound(x[i]); auto q = p; q++; auto r = p; r--; auto v = r; v--; ans += ((*q - *p) * (x[i] - *r) + (*r - *v) * (*p - x[i])) * (n - i); s.insert(x[i]); } cout << ans << endl; }
replace
227
228
227
228
TLE
p02919
C++
Runtime Error
#include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <cstdio> #include <cstring> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; typedef unsigned int uint; typedef long long ll; typedef unsigned long long ull; #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i) #define ALL(c) (c).begin(), (c).end() #define SIZE(v) ((int)v.size()) #define pb push_back #define mp make_pair #define mt make_tuple int main(void) { cin.sync_with_stdio(false); int N; cin >> N; vector<int> Ps(N); vector<int> locs(N); REP(n, N) { int p; cin >> p; locs[p] = n + 1; } // 番兵を入れる multiset<int> seen; seen.insert(0); seen.insert(0); seen.insert(N + 1); seen.insert(N + 1); // 以下のように並んでいるとき、2番めに大きい値が4となる範囲を考える // inf inf 1 5 2 4 3 7 6 inf inf // w x y z // ll ans = 0; for (int n = N; n >= 1; --n) { auto loc = locs[n]; auto it = seen.lower_bound(loc); auto y = *it; ++it; auto z = *it; --it; --it; auto x = *it; --it; auto w = *it; // cout << "loc of " << n << ": " << loc << endl; // cout << "y = " << y << endl; // cout << "z = " << z << endl; // cout << "x = " << x << endl; // cout << "w = " << w << endl; ll combination = (x - w) * (y - loc); combination += (z - y) * (loc - x); ans += combination * n; // cout << "added: " << (x - w) * (y - loc) << endl; // cout << "added: " << (z - y) * (loc - x) << endl; seen.insert(loc); } cout << ans << endl; return 0; }
#include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <cstdio> #include <cstring> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; typedef unsigned int uint; typedef long long ll; typedef unsigned long long ull; #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i) #define ALL(c) (c).begin(), (c).end() #define SIZE(v) ((int)v.size()) #define pb push_back #define mp make_pair #define mt make_tuple int main(void) { cin.sync_with_stdio(false); int N; cin >> N; vector<int> locs(N + 1); REP(n, N) { int p; cin >> p; locs[p] = n + 1; } // 番兵を入れる multiset<int> seen; seen.insert(0); seen.insert(0); seen.insert(N + 1); seen.insert(N + 1); // 以下のように並んでいるとき、2番めに大きい値が4となる範囲を考える // inf inf 1 5 2 4 3 7 6 inf inf // w x y z // ll ans = 0; for (int n = N; n >= 1; --n) { auto loc = locs[n]; auto it = seen.lower_bound(loc); auto y = *it; ++it; auto z = *it; --it; --it; auto x = *it; --it; auto w = *it; // cout << "loc of " << n << ": " << loc << endl; // cout << "y = " << y << endl; // cout << "z = " << z << endl; // cout << "x = " << x << endl; // cout << "w = " << w << endl; ll combination = (x - w) * (y - loc); combination += (z - y) * (loc - x); ans += combination * n; // cout << "added: " << (x - w) * (y - loc) << endl; // cout << "added: " << (z - y) * (loc - x) << endl; seen.insert(loc); } cout << ans << endl; return 0; }
replace
38
40
38
39
0
p02919
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using Cache = pair<long long, pair<long long, long long>>; const bool DEBUG = false; Cache cache = make_pair(-1, make_pair(-1, 1)); tuple<long long, long long, long long, long long> get_anchors(long long i, const vector<long long> &P) { long long outer_left_anchor, inner_left_anchor, inner_right_anchor, outer_right_anchor; long long anchor; anchor = (P[i] > cache.first) ? min(cache.second.first, i - 1) : i - 1; while ((anchor >= 0) && P[anchor] < P[i]) anchor--; inner_left_anchor = anchor; if (anchor == -1) { outer_left_anchor = -1; } else { anchor--; while ((anchor >= 0) && P[anchor] < P[i]) anchor--; outer_left_anchor = anchor; } anchor = (P[i] > cache.first) ? max(cache.second.second, i + 1) : i + 1; while ((anchor < P.size()) && P[anchor] < P[i]) anchor++; inner_right_anchor = anchor; if (anchor == P.size()) { outer_right_anchor = P.size(); } else { anchor++; while ((anchor < P.size()) && P[anchor] < P[i]) anchor++; outer_right_anchor = anchor; } if (P[i] > cache.first) { if (DEBUG) cout << "cache hit at: (" << inner_left_anchor << ", " << i << ", " << inner_right_anchor << ")" << endl; cache.first = P[i]; cache.second.first = inner_left_anchor; cache.second.second = inner_right_anchor; } return forward_as_tuple(outer_left_anchor, inner_left_anchor, inner_right_anchor, outer_right_anchor); } int main(int argc, char const *argv[]) { long long N; cin >> N; vector<long long> P(N); for (size_t i = 0; i < N; i++) cin >> P[i]; long long total = 0; long long outer_left_anchor, inner_left_anchor, inner_right_anchor, outer_right_anchor; for (size_t i = 0; i < N; i++) { tie(outer_left_anchor, inner_left_anchor, inner_right_anchor, outer_right_anchor) = get_anchors(i, P); total += P[i] * (((i - inner_left_anchor) * (outer_right_anchor - inner_right_anchor)) + ((inner_left_anchor - outer_left_anchor) * (inner_right_anchor - i))); if (DEBUG) cout << outer_left_anchor << ", " << inner_left_anchor << ", " << i << ", " << inner_right_anchor << ", " << outer_right_anchor << endl; } cout << total << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using Cache = pair<long long, pair<long long, long long>>; const bool DEBUG = false; Cache cache = make_pair(-1, make_pair(-1, 1)); tuple<long long, long long, long long, long long> get_anchors(long long i, const vector<long long> &P) { long long outer_left_anchor, inner_left_anchor, inner_right_anchor, outer_right_anchor; long long anchor; anchor = (P[i] > cache.first) ? min(cache.second.first, i - 1) : i - 1; while ((anchor >= 0) && P[anchor] < P[i]) anchor--; inner_left_anchor = anchor; if (anchor == -1) { outer_left_anchor = -1; } else { anchor--; while ((anchor >= 0) && P[anchor] < P[i]) anchor--; outer_left_anchor = anchor; } anchor = (P[i] > cache.first) ? max(cache.second.second, i + 1) : i + 1; while ((anchor < P.size()) && P[anchor] < P[i]) anchor++; inner_right_anchor = anchor; if (anchor == P.size()) { outer_right_anchor = P.size(); } else { anchor++; while ((anchor < P.size()) && P[anchor] < P[i]) anchor++; outer_right_anchor = anchor; } if (P[i] > cache.first) { if (DEBUG) cout << "cache hit at: (" << inner_left_anchor << ", " << i << ", " << inner_right_anchor << ")" << endl; cache.first = P[i]; cache.second.first = inner_left_anchor; cache.second.second = inner_right_anchor; } return forward_as_tuple(outer_left_anchor, inner_left_anchor, inner_right_anchor, outer_right_anchor); } int main(int argc, char const *argv[]) { long long N; cin >> N; vector<long long> P(N); for (size_t i = 0; i < N; i++) cin >> P[i]; size_t max_index = distance(P.begin(), max_element(P.begin(), P.end())); if (max_index <= P.size() / 2) reverse(P.begin(), P.end()); long long total = 0; long long outer_left_anchor, inner_left_anchor, inner_right_anchor, outer_right_anchor; for (size_t i = 0; i < N; i++) { tie(outer_left_anchor, inner_left_anchor, inner_right_anchor, outer_right_anchor) = get_anchors(i, P); total += P[i] * (((i - inner_left_anchor) * (outer_right_anchor - inner_right_anchor)) + ((inner_left_anchor - outer_left_anchor) * (inner_right_anchor - i))); if (DEBUG) cout << outer_left_anchor << ", " << inner_left_anchor << ", " << i << ", " << inner_right_anchor << ", " << outer_right_anchor << endl; } cout << total << endl; return 0; }
insert
60
60
60
64
TLE
p02919
C++
Time Limit Exceeded
#include <algorithm> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <deque> #include <iostream> #include <iterator> #include <limits> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> #define FOR(i, k, n) for (int(i) = (k); (i) < (n); ++(i)) #define rep(i, n) FOR(i, 0, n) #define all(v) begin(v), end(v) #define debug(x) std::cerr << #x << ": " << x << "\n" #define debug2(x, y) \ std::cerr << #x << ": " << x << ", " << #y << ": " << y << "\n" #define debug3(x, y, z) \ std::cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z \ << ": " << z << "\n" using ll = long long; using vi = std::vector<int>; using vvi = std::vector<vi>; using vll = std::vector<ll>; using vvll = std::vector<vll>; template <typename T> using vvec = std::vector<std::vector<T>>; template <typename T> auto make_v(size_t sz) { return std::vector<T>(sz); } template <typename T, typename... Ts> auto make_v(size_t sz, Ts... ts) { return std::vector<decltype(make_v<T>(ts...))>(sz, make_v<T>(ts...)); } template <typename T> void fill_v(T &var, const T &x) { var = x; } template <typename V, typename T> void fill_v(V &v, const T &x) { for (auto &&w : v) { fill_v(w, x); } } template <typename T> std::ostream &operator<<(std::ostream &s, const std::vector<T> &v) { int sz = v.size(); s << "\n"; rep(i, sz) { s << v[i]; if (i < sz - 1) { s << "\t"; } } s << "\n"; return s; } template <typename T> std::ostream &operator<<(std::ostream &s, const std::vector<std::vector<T>> &v) { for (auto &&w : v) { s << w; } return s; } template <typename T> std::ostream &operator<<(std::ostream &s, const std::deque<T> &v) { int sz = v.size(); s << "\n"; rep(i, sz) { s << v[i]; if (i < sz - 1) { s << "\t"; } } s << "\n"; return s; } template <typename T> std::ostream &operator<<(std::ostream &s, const std::deque<std::deque<T>> &v) { for (auto &&w : v) { s << w; } return s; } template <typename T> std::ostream &operator<<(std::ostream &s, const std::set<T> &v) { s << "\n"; for (auto &&elm : v) { s << elm << "\t"; } s << "\n"; return s; } inline void scan(int &a) { scanf("%d", &a); } inline void scan(ll &a) { scanf("%lld", &a); } inline void scan(char &a) { scanf(" %c", &a); } inline void scan(double &a) { scanf("%lf", &a); } inline void scan(std::string &s) { char BUF[3000000]; scanf(" %s", BUF); s = std::string(BUF); } template <typename T> inline void scan(std::vector<T> &v) { for (auto &&sv : v) { scan(sv); } } template <typename First, typename... Args> inline void scan(First &f, Args &...args) { scan(f); scan(args...); } inline void print(int a) { printf("%d\n", a); } inline void print(ll a) { printf("%lld\n", a); } inline void print(double a) { printf("%.12f\n", a); } inline void print(std::string s) { std::cout << s << "\n"; } using namespace std; int main() { int n; scan(n); vi p(n); scan(p); vi idx(n, 0); rep(i, n) { p[i] -= 1; idx[p[i]] = i; } multiset<int> si = {-1, -1, n, n}; ll ans = 0; for (int i = n - 1; i >= 0; --i) { int index = idx[i]; auto lb = lower_bound(all(si), index); auto x3 = lb; auto x4 = ++lb; --lb; auto x2 = --lb; auto x1 = --lb; ll d1 = (*x2) - (*x1); ll d2 = (*x4) - (*x3); ans += d1 * ((*x3) - index) * (i + 1); ans += d2 * (index - (*x2)) * (i + 1); si.insert(index); } print(ans); return 0; }
#include <algorithm> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <deque> #include <iostream> #include <iterator> #include <limits> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> #define FOR(i, k, n) for (int(i) = (k); (i) < (n); ++(i)) #define rep(i, n) FOR(i, 0, n) #define all(v) begin(v), end(v) #define debug(x) std::cerr << #x << ": " << x << "\n" #define debug2(x, y) \ std::cerr << #x << ": " << x << ", " << #y << ": " << y << "\n" #define debug3(x, y, z) \ std::cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z \ << ": " << z << "\n" using ll = long long; using vi = std::vector<int>; using vvi = std::vector<vi>; using vll = std::vector<ll>; using vvll = std::vector<vll>; template <typename T> using vvec = std::vector<std::vector<T>>; template <typename T> auto make_v(size_t sz) { return std::vector<T>(sz); } template <typename T, typename... Ts> auto make_v(size_t sz, Ts... ts) { return std::vector<decltype(make_v<T>(ts...))>(sz, make_v<T>(ts...)); } template <typename T> void fill_v(T &var, const T &x) { var = x; } template <typename V, typename T> void fill_v(V &v, const T &x) { for (auto &&w : v) { fill_v(w, x); } } template <typename T> std::ostream &operator<<(std::ostream &s, const std::vector<T> &v) { int sz = v.size(); s << "\n"; rep(i, sz) { s << v[i]; if (i < sz - 1) { s << "\t"; } } s << "\n"; return s; } template <typename T> std::ostream &operator<<(std::ostream &s, const std::vector<std::vector<T>> &v) { for (auto &&w : v) { s << w; } return s; } template <typename T> std::ostream &operator<<(std::ostream &s, const std::deque<T> &v) { int sz = v.size(); s << "\n"; rep(i, sz) { s << v[i]; if (i < sz - 1) { s << "\t"; } } s << "\n"; return s; } template <typename T> std::ostream &operator<<(std::ostream &s, const std::deque<std::deque<T>> &v) { for (auto &&w : v) { s << w; } return s; } template <typename T> std::ostream &operator<<(std::ostream &s, const std::set<T> &v) { s << "\n"; for (auto &&elm : v) { s << elm << "\t"; } s << "\n"; return s; } inline void scan(int &a) { scanf("%d", &a); } inline void scan(ll &a) { scanf("%lld", &a); } inline void scan(char &a) { scanf(" %c", &a); } inline void scan(double &a) { scanf("%lf", &a); } inline void scan(std::string &s) { char BUF[3000000]; scanf(" %s", BUF); s = std::string(BUF); } template <typename T> inline void scan(std::vector<T> &v) { for (auto &&sv : v) { scan(sv); } } template <typename First, typename... Args> inline void scan(First &f, Args &...args) { scan(f); scan(args...); } inline void print(int a) { printf("%d\n", a); } inline void print(ll a) { printf("%lld\n", a); } inline void print(double a) { printf("%.12f\n", a); } inline void print(std::string s) { std::cout << s << "\n"; } using namespace std; int main() { int n; scan(n); vi p(n); scan(p); vi idx(n, 0); rep(i, n) { p[i] -= 1; idx[p[i]] = i; } multiset<int> si = {-1, -1, n, n}; ll ans = 0; for (int i = n - 1; i >= 0; --i) { int index = idx[i]; auto lb = si.lower_bound(index); auto x3 = lb; auto x4 = ++lb; --lb; auto x2 = --lb; auto x1 = --lb; ll d1 = (*x2) - (*x1); ll d2 = (*x4) - (*x3); ans += d1 * ((*x3) - index) * (i + 1); ans += d2 * (index - (*x2)) * (i + 1); si.insert(index); } print(ans); return 0; }
replace
142
143
142
143
TLE
p02919
C++
Time Limit Exceeded
/** * code generated by JHelper * More info: https://github.com/AlexeyDmitriev/JHelper * @author */ #include <fstream> #include <iostream> #include "bits/stdc++.h" #define REP(i, x, y) for (int i = (x); i < (y); i++) #define RREP(i, x, y) for (int i = (y)-1; i >= (x); i--) #define all(x) (x).begin(), (x).end() // #define int long long using namespace std; // conversion inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } #define dump(x) cout << #x << " = " << (x) << endl #define debug(x) \ cout << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl typedef long long ll; const ll MOD = 1e9 + 7; class ESecondSum { public: void solve(std::istream &in, std::ostream &out) { int N; in >> N; vector<int> P(N); REP(i, 0, N) in >> P[i]; vector<pair<int, int>> pIndex(N); REP(i, 0, N) pIndex[i] = {P[i], i}; sort(all(pIndex), greater<pair<int, int>>()); ll answer = 0; set<int> processed; REP(i, 0, N) { const int target = pIndex[i].first; const int targetIndex = pIndex[i].second; const auto itrRight = upper_bound(all(processed), targetIndex); const auto itrLeft = itrRight != processed.begin() && processed.size() != 0 ? prev(itrRight) : processed.end(); if (itrRight != processed.end()) { const auto itrRight2 = next(itrRight); ll left = itrLeft != processed.end() ? targetIndex - *itrLeft : targetIndex + 1; ll right = itrRight2 != processed.end() ? *itrRight2 - *itrRight : N - *itrRight; answer += left * right * target; } if (itrLeft != processed.end()) { const auto itrLeft2 = itrLeft != processed.begin() && processed.size() != 0 ? prev(itrLeft) : processed.end(); ll left = itrLeft2 != processed.end() ? *itrLeft - *itrLeft2 : (*itrLeft) + 1; ll right = itrRight != processed.end() ? *itrRight - targetIndex : N - targetIndex; answer += left * right * target; } processed.insert(targetIndex); } out << answer << "\n"; } }; int main() { ESecondSum solver; std::istream &in(std::cin); std::ostream &out(std::cout); solver.solve(in, out); return 0; }
/** * code generated by JHelper * More info: https://github.com/AlexeyDmitriev/JHelper * @author */ #include <fstream> #include <iostream> #include "bits/stdc++.h" #define REP(i, x, y) for (int i = (x); i < (y); i++) #define RREP(i, x, y) for (int i = (y)-1; i >= (x); i--) #define all(x) (x).begin(), (x).end() // #define int long long using namespace std; // conversion inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } #define dump(x) cout << #x << " = " << (x) << endl #define debug(x) \ cout << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl typedef long long ll; const ll MOD = 1e9 + 7; class ESecondSum { public: void solve(std::istream &in, std::ostream &out) { int N; in >> N; vector<int> P(N); REP(i, 0, N) in >> P[i]; vector<pair<int, int>> pIndex(N); REP(i, 0, N) pIndex[i] = {P[i], i}; sort(all(pIndex), greater<pair<int, int>>()); ll answer = 0; set<int> processed; REP(i, 0, N) { const int target = pIndex[i].first; const int targetIndex = pIndex[i].second; const auto itrRight = processed.upper_bound(targetIndex); const auto itrLeft = itrRight != processed.begin() && processed.size() != 0 ? prev(itrRight) : processed.end(); if (itrRight != processed.end()) { const auto itrRight2 = next(itrRight); ll left = itrLeft != processed.end() ? targetIndex - *itrLeft : targetIndex + 1; ll right = itrRight2 != processed.end() ? *itrRight2 - *itrRight : N - *itrRight; answer += left * right * target; } if (itrLeft != processed.end()) { const auto itrLeft2 = itrLeft != processed.begin() && processed.size() != 0 ? prev(itrLeft) : processed.end(); ll left = itrLeft2 != processed.end() ? *itrLeft - *itrLeft2 : (*itrLeft) + 1; ll right = itrRight != processed.end() ? *itrRight - targetIndex : N - targetIndex; answer += left * right * target; } processed.insert(targetIndex); } out << answer << "\n"; } }; int main() { ESecondSum solver; std::istream &in(std::cin); std::ostream &out(std::cout); solver.solve(in, out); return 0; }
replace
57
58
57
58
TLE
p02919
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <climits> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <vector> #define MOD 1000000007 #define MOD2 998244353 #define int long long // #define PI 3.14159265358979 #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; template <typename T> ostream &operator<<(ostream &os, const vector<T> &A) { for (int i = 0; i < A.size(); i++) os << A[i] << " "; os << endl; return os; } template <> ostream &operator<<(ostream &os, const vector<vector<int>> &A) { int N = A.size(); int M; if (N > 0) M = A[0].size(); for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) os << A[i][j] << " "; os << endl; } return os; } typedef pair<int, int> pii; typedef long long ll; struct edge { int from, to, d, c; edge(int _from = 0, int _to = 0, int _d = 0, int _c = 0) { from = _from; to = _to; d = _d; c = _c; } bool operator<(const edge &rhs) const { return (d == rhs.d) ? (c < rhs.c) : (d < rhs.d); } }; struct aabb { int x1, y1, x2, y2; aabb(int x1, int y1, int x2, int y2) : x1(x1), y1(y1), x2(x2), y2(y2) {} }; typedef vector<edge> edges; typedef vector<edges> graph; struct flow { int to, cap, rev, cost; flow(int to = 0, int cap = 0, int rev = 0, int cost = 0) : to(to), cap(cap), rev(rev), cost(cost) {} }; typedef vector<vector<flow>> flows; const int di[4] = {0, -1, 0, 1}; const int dj[4] = {-1, 0, 1, 0}; const int ci[5] = {0, 0, -1, 0, 1}; const int cj[5] = {0, -1, 0, 1, 0}; const ll LINF = LLONG_MAX / 2; const int INF = INT_MAX / 2; const double PI = acos(-1); int pow2(int n) { return 1 << n; } template <typename T, typename U> bool chmin(T &x, const U &y) { if (x > y) { x = y; return true; } return false; } template <typename T, typename U> bool chmax(T &x, const U &y) { if (x < y) { x = y; return true; } return false; } struct initializer { initializer() { cout << fixed << setprecision(11); } }; initializer _____; int N, M, K, T, Q; signed main() { cin >> N; vector<pii> P(N); rep(i, N) { cin >> P[i].first; P[i].second = i; } sort(P.rbegin(), P.rend()); multiset<int> s; s.insert(N); s.insert(N); s.insert(-1); s.insert(-1); int ans = 0; rep(k, N) { int i = P[k].second; int p = P[k].first; auto it = upper_bound(s.begin(), s.end(), i); it++; int z = *it; it--; int y = *it; it--; int x = *it; it--; int w = *it; s.insert(i); ans += (i - x) * (z - y) * p; ans += (y - i) * (x - w) * p; /*cout << p << endl << w << " " << x << " " << i << " " << y << " " << z << endl << ans << endl; */ } cout << ans << endl; return 0; }
#include <algorithm> #include <bitset> #include <climits> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <vector> #define MOD 1000000007 #define MOD2 998244353 #define int long long // #define PI 3.14159265358979 #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; template <typename T> ostream &operator<<(ostream &os, const vector<T> &A) { for (int i = 0; i < A.size(); i++) os << A[i] << " "; os << endl; return os; } template <> ostream &operator<<(ostream &os, const vector<vector<int>> &A) { int N = A.size(); int M; if (N > 0) M = A[0].size(); for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) os << A[i][j] << " "; os << endl; } return os; } typedef pair<int, int> pii; typedef long long ll; struct edge { int from, to, d, c; edge(int _from = 0, int _to = 0, int _d = 0, int _c = 0) { from = _from; to = _to; d = _d; c = _c; } bool operator<(const edge &rhs) const { return (d == rhs.d) ? (c < rhs.c) : (d < rhs.d); } }; struct aabb { int x1, y1, x2, y2; aabb(int x1, int y1, int x2, int y2) : x1(x1), y1(y1), x2(x2), y2(y2) {} }; typedef vector<edge> edges; typedef vector<edges> graph; struct flow { int to, cap, rev, cost; flow(int to = 0, int cap = 0, int rev = 0, int cost = 0) : to(to), cap(cap), rev(rev), cost(cost) {} }; typedef vector<vector<flow>> flows; const int di[4] = {0, -1, 0, 1}; const int dj[4] = {-1, 0, 1, 0}; const int ci[5] = {0, 0, -1, 0, 1}; const int cj[5] = {0, -1, 0, 1, 0}; const ll LINF = LLONG_MAX / 2; const int INF = INT_MAX / 2; const double PI = acos(-1); int pow2(int n) { return 1 << n; } template <typename T, typename U> bool chmin(T &x, const U &y) { if (x > y) { x = y; return true; } return false; } template <typename T, typename U> bool chmax(T &x, const U &y) { if (x < y) { x = y; return true; } return false; } struct initializer { initializer() { cout << fixed << setprecision(11); } }; initializer _____; int N, M, K, T, Q; signed main() { cin >> N; vector<pii> P(N); rep(i, N) { cin >> P[i].first; P[i].second = i; } sort(P.rbegin(), P.rend()); multiset<int> s; s.insert(N); s.insert(N); s.insert(-1); s.insert(-1); int ans = 0; rep(k, N) { int i = P[k].second; int p = P[k].first; auto it = s.lower_bound(i); it++; int z = *it; it--; int y = *it; it--; int x = *it; it--; int w = *it; s.insert(i); ans += (i - x) * (z - y) * p; ans += (y - i) * (x - w) * p; /*cout << p << endl << w << " " << x << " " << i << " " << y << " " << z << endl << ans << endl; */ } cout << ans << endl; return 0; }
replace
121
122
121
122
TLE
p02919
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define PI 3.14159265358979 #define rep(i, n) for (int i = 0; i < (n); ++i) #define repp(i, s, e) for (int i = (s); i <= (e); ++i) #define sz(x) ((int)x.size()) #define all(x) x.begin(), x.end() #define FAST_IO() \ ios::sync_with_stdio(0); \ cin.tie(0) template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { for (auto x : v) os << x << " "; return os << "\n"; } template <class U, class V> ostream &operator<<(ostream &os, const pair<U, V> &p) { return os << "(" << p.first << "," << p.second << ")"; } int main() { FAST_IO(); int N; cin >> N; vector<int> P(N + 1); repp(i, 1, N) { int x; cin >> x; P[x] = i; } set<int> bigger; bigger.insert(0); bigger.insert(N + 1); ll ans = 0; for (int x = N; x >= 1; --x) { auto pos = lower_bound(all(bigger), P[x]); int R = *(pos); int RR; if (R == N + 1) RR = R; else { RR = *(++pos); --pos; } int L = *(--pos); int LL; if (L == 0) LL = L; else { LL = *(--pos); } ans += (ll)x * ((L - LL) * (R - P[x]) + (RR - R) * (P[x] - L)); bigger.insert(P[x]); } cout << ans << "\n"; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define PI 3.14159265358979 #define rep(i, n) for (int i = 0; i < (n); ++i) #define repp(i, s, e) for (int i = (s); i <= (e); ++i) #define sz(x) ((int)x.size()) #define all(x) x.begin(), x.end() #define FAST_IO() \ ios::sync_with_stdio(0); \ cin.tie(0) template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { for (auto x : v) os << x << " "; return os << "\n"; } template <class U, class V> ostream &operator<<(ostream &os, const pair<U, V> &p) { return os << "(" << p.first << "," << p.second << ")"; } int main() { FAST_IO(); int N; cin >> N; vector<int> P(N + 1); repp(i, 1, N) { int x; cin >> x; P[x] = i; } set<int> bigger; bigger.insert(0); bigger.insert(N + 1); ll ans = 0; for (int x = N; x >= 1; --x) { auto pos = bigger.lower_bound(P[x]); int R = *(pos); int RR; if (R == N + 1) RR = R; else { RR = *(++pos); --pos; } int L = *(--pos); int LL; if (L == 0) LL = L; else { LL = *(--pos); } ans += (ll)x * ((L - LL) * (R - P[x]) + (RR - R) * (P[x] - L)); bigger.insert(P[x]); } cout << ans << "\n"; }
replace
38
39
38
39
TLE
p02919
C++
Time Limit Exceeded
#include <algorithm> #include <cassert> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <string> #include <utility> #include <vector> using namespace std; using ll = long long; constexpr ll infl = 10000000000000007LL; constexpr int inf = 1000000007; int main() { int n; cin >> n; vector<int> idx(n + 1); for (int i = 1; i <= n; ++i) { int p; cin >> p; idx[p] = i; } multiset<int> mset{0, 0, n + 1, n + 1}; ll ans = 0; for (int i = n; i >= 1; --i) { int c = idx[i]; auto it_d = lower_bound(mset.begin(), mset.end(), c); ll d = *it_d; ll e = *(++it_d); ll b = *(-- --it_d); ll a = *(--it_d); ans += ((e - d) * (c - b) + (b - a) * (d - c)) * (ll)i; mset.insert(c); } cout << ans << endl; return 0; }
#include <algorithm> #include <cassert> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <string> #include <utility> #include <vector> using namespace std; using ll = long long; constexpr ll infl = 10000000000000007LL; constexpr int inf = 1000000007; int main() { int n; cin >> n; vector<int> idx(n + 1); for (int i = 1; i <= n; ++i) { int p; cin >> p; idx[p] = i; } multiset<int> mset{0, 0, n + 1, n + 1}; ll ans = 0; for (int i = n; i >= 1; --i) { int c = idx[i]; auto it_d = mset.lower_bound(c); ll d = *it_d; ll e = *(++it_d); ll b = *(-- --it_d); ll a = *(--it_d); ans += ((e - d) * (c - b) + (b - a) * (d - c)) * (ll)i; mset.insert(c); } cout << ans << endl; return 0; }
replace
33
34
33
34
TLE
p02919
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <set> #include <string> #include <vector> using namespace std; #define rip(i, n, s) for (int i = (s); i < (int)(n); i++) #define all(a) (a).begin(), (a).end() typedef long long ll; int main() { int n; cin >> n; vector<int> da(n), li(n); rip(i, n, 0) { cin >> da[i]; li[da[i] - 1] = i; } multiset<int> se; se.insert(-1); se.insert(-1); se.insert(n); se.insert(n); long long ans = 0; rip(i, n, 0) { int no = n - 1 - i; auto itr = lower_bound(se.begin(), se.end(), li[no]); int w = *++itr; itr--; int z = *itr; int y = *--itr; int x = *--itr; // printf("%d %d %d %d\n",x,y,z,w); ans += (ll)(no + 1) * ((y - x) * (z - li[no]) + (w - z) * (li[no] - y)); se.insert(li[no]); } cout << ans << endl; }
#include <algorithm> #include <iostream> #include <set> #include <string> #include <vector> using namespace std; #define rip(i, n, s) for (int i = (s); i < (int)(n); i++) #define all(a) (a).begin(), (a).end() typedef long long ll; int main() { int n; cin >> n; vector<int> da(n), li(n); rip(i, n, 0) { cin >> da[i]; li[da[i] - 1] = i; } multiset<int> se; se.insert(-1); se.insert(-1); se.insert(n); se.insert(n); long long ans = 0; rip(i, n, 0) { int no = n - 1 - i; auto itr = se.lower_bound(li[no]); int w = *++itr; itr--; int z = *itr; int y = *--itr; int x = *--itr; // printf("%d %d %d %d\n",x,y,z,w); ans += (ll)(no + 1) * ((y - x) * (z - li[no]) + (w - z) * (li[no] - y)); se.insert(li[no]); } cout << ans << endl; }
replace
28
29
28
29
TLE
p02919
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; using vb = vector<bool>; using vvb = vector<vb>; using vd = vector<double>; using vvd = vector<vd>; using vi = vector<int>; using vvi = vector<vi>; using vl = vector<ll>; using vvl = vector<vl>; using pii = pair<int, int>; using pll = pair<ll, ll>; using tll = tuple<ll, ll>; using tlll = tuple<ll, ll, ll>; using vs = vector<string>; #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define rep(i, n) range(i, 0, n) #define rrep(i, n) for (int i = (n)-1; i >= 0; i--) #define range(i, a, n) for (int i = (a); i < (n); i++) #define LINF ((ll)1ll << 60) #define INF ((int)1 << 30) #define EPS (1e-9) #define MOD (1000000007ll) #define fcout(a) cout << setprecision(a) << fixed #define fs first #define sc second #define PI 3.1415926535897932384 int dx[] = {1, 0, -1, 0, 1, -1, -1, 1}, dy[] = {0, 1, 0, -1, 1, 1, -1, -1}; template <class T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class S> S acm(vector<S> &a) { return accumulate(all(a), S()); } template <class S> S max(vector<S> &a) { return *max_element(all(a)); } template <class S> S min(vector<S> &a) { return *min_element(all(a)); } void YN(bool b) { cout << (b ? "YES" : "NO") << "\n"; } void Yn(bool b) { cout << (b ? "Yes" : "No") << "\n"; } void yn(bool b) { cout << (b ? "yes" : "no") << "\n"; } int sgn(const double &r) { return (r > EPS) - (r < -EPS); } // a>0 : sgn(a)>0 int sgn(const double &a, const double &b) { return sgn(a - b); } // b<=c : sgn(b,c)<=0 ll max(int a, ll b) { return max((ll)a, b); } ll max(ll a, int b) { return max(a, (ll)b); } template <class T> void puta(T &&t) { cout << t << "\n"; } template <class H, class... T> void puta(H &&h, T &&...t) { cout << h << ' '; puta(t...); } template <class S, class T> ostream &operator<<(ostream &os, pair<S, T> p) { os << "[" << p.first << ", " << p.second << "]"; return os; }; template <class S> auto &operator<<(ostream &os, vector<S> t) { bool a = 1; for (auto s : t) { os << (a ? "" : " ") << s; a = 0; } return os; } int main() { cin.tie(0); ios::sync_with_stdio(false); multiset<ll> s; ll n, ans = 0; cin >> n; vl v(n), ind(n); rep(i, n) cin >> v[i], ind[v[i] - 1] = i + 1; s.insert(0); s.insert(0); s.insert(n + 1); s.insert(n + 1); rrep(i, n) { ll num = ind[i]; auto rl = lower_bound(all(s), num); auto rr = rl; auto ll = rl; auto lr = rl; rr++; lr--; ll--; ll--; ans += (i + 1) * (*lr - *ll) * (*rl - num); ans += (i + 1) * (num - *lr) * (*rr - *rl); s.insert(num); } puta(ans); }
#include <bits/stdc++.h> using namespace std; using ll = long long; using vb = vector<bool>; using vvb = vector<vb>; using vd = vector<double>; using vvd = vector<vd>; using vi = vector<int>; using vvi = vector<vi>; using vl = vector<ll>; using vvl = vector<vl>; using pii = pair<int, int>; using pll = pair<ll, ll>; using tll = tuple<ll, ll>; using tlll = tuple<ll, ll, ll>; using vs = vector<string>; #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define rep(i, n) range(i, 0, n) #define rrep(i, n) for (int i = (n)-1; i >= 0; i--) #define range(i, a, n) for (int i = (a); i < (n); i++) #define LINF ((ll)1ll << 60) #define INF ((int)1 << 30) #define EPS (1e-9) #define MOD (1000000007ll) #define fcout(a) cout << setprecision(a) << fixed #define fs first #define sc second #define PI 3.1415926535897932384 int dx[] = {1, 0, -1, 0, 1, -1, -1, 1}, dy[] = {0, 1, 0, -1, 1, 1, -1, -1}; template <class T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class S> S acm(vector<S> &a) { return accumulate(all(a), S()); } template <class S> S max(vector<S> &a) { return *max_element(all(a)); } template <class S> S min(vector<S> &a) { return *min_element(all(a)); } void YN(bool b) { cout << (b ? "YES" : "NO") << "\n"; } void Yn(bool b) { cout << (b ? "Yes" : "No") << "\n"; } void yn(bool b) { cout << (b ? "yes" : "no") << "\n"; } int sgn(const double &r) { return (r > EPS) - (r < -EPS); } // a>0 : sgn(a)>0 int sgn(const double &a, const double &b) { return sgn(a - b); } // b<=c : sgn(b,c)<=0 ll max(int a, ll b) { return max((ll)a, b); } ll max(ll a, int b) { return max(a, (ll)b); } template <class T> void puta(T &&t) { cout << t << "\n"; } template <class H, class... T> void puta(H &&h, T &&...t) { cout << h << ' '; puta(t...); } template <class S, class T> ostream &operator<<(ostream &os, pair<S, T> p) { os << "[" << p.first << ", " << p.second << "]"; return os; }; template <class S> auto &operator<<(ostream &os, vector<S> t) { bool a = 1; for (auto s : t) { os << (a ? "" : " ") << s; a = 0; } return os; } int main() { cin.tie(0); ios::sync_with_stdio(false); multiset<ll> s; ll n, ans = 0; cin >> n; vl v(n), ind(n); rep(i, n) cin >> v[i], ind[v[i] - 1] = i + 1; s.insert(0); s.insert(0); s.insert(n + 1); s.insert(n + 1); rrep(i, n) { ll num = ind[i]; auto rl = s.lower_bound(num); auto rr = rl; auto ll = rl; auto lr = rl; rr++; lr--; ll--; ll--; ans += (i + 1) * (*lr - *ll) * (*rl - num); ans += (i + 1) * (num - *lr) * (*rr - *rl); s.insert(num); } puta(ans); }
replace
93
94
93
94
TLE
p02919
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; #define INF 2100000000 #define MOD 1000000007 int main() { ll N; cin >> N; vector<pair<ll, ll>> P(N); for (ll i = 0; i < N; i++) { cin >> P.at(i).first; P.at(i).second = i; } sort(P.begin(), P.end()); reverse(P.begin(), P.end()); multiset<ll> S; S.insert(-1); S.insert(-1); S.insert(N); S.insert(N); ll ans = 0; for (ll i = 0; i < N; i++) { auto iter = upper_bound(S.begin(), S.end(), P.at(i).second); iter--; iter--; ll a1 = *iter; iter++; ll a2 = *iter; iter++; ll a3 = *iter; iter++; ll a4 = *iter; ans += (ll)P.at(i).first * (((ll)a2 - (ll)a1) * ((ll)a3 - (ll)P.at(i).second) + ((ll)a4 - (ll)a3) * ((ll)P.at(i).second - (ll)a2)); S.insert(P.at(i).second); } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define INF 2100000000 #define MOD 1000000007 int main() { ll N; cin >> N; vector<pair<ll, ll>> P(N); for (ll i = 0; i < N; i++) { cin >> P.at(i).first; P.at(i).second = i; } sort(P.begin(), P.end()); reverse(P.begin(), P.end()); multiset<ll> S; S.insert(-1); S.insert(-1); S.insert(N); S.insert(N); ll ans = 0; for (ll i = 0; i < N; i++) { auto iter = S.upper_bound(P.at(i).second); iter--; iter--; ll a1 = *iter; iter++; ll a2 = *iter; iter++; ll a3 = *iter; iter++; ll a4 = *iter; ans += (ll)P.at(i).first * (((ll)a2 - (ll)a1) * ((ll)a3 - (ll)P.at(i).second) + ((ll)a4 - (ll)a3) * ((ll)P.at(i).second - (ll)a2)); S.insert(P.at(i).second); } cout << ans << endl; }
replace
24
25
24
25
TLE
p02919
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; using vll = vector<ll>; using vi = vector<int>; using vb = vector<bool>; using pll = pair<ll, ll>; using pii = pair<int, int>; using vpii = vector<pii>; using vpll = vector<pll>; const ll LINF = 1ll << 55; const ll INF = 0x3f3f3f3f; const ll MOD = 1e9 + 7; const int MAXN = 1e5 + 10; const ll dx[] = {1, 0, -1, 0}; const ll dy[] = {0, 1, 0, -1}; /// cin/cout overloading template <typename T> ostream &operator<<(ostream &out, vector<T> &vec) { for (auto it = vec.begin(); it != vec.end(); ++it) { out << *it << " "; } return out; } template <typename T> ostream &operator<<(ostream &out, pair<T, T> &P) { out << P.first << " " << P.second; return out; } template <typename T> istream &operator>>(istream &in, vector<T> &vec) { for (auto it = vec.begin(); it != vec.end(); ++it) { in >> *it; } return in; } template <typename T> istream &operator>>(istream &in, pair<T, T> &P) { in >> P.first >> P.second; return in; } #define coutp(x, y) cout << (x) << " " << (y) << endl #define coutn(x) cout << (x) << endl #define coutd(x) cout << setprecision(10) << (x) << endl /// 图相关 // ll N, M; struct Edge { int to; ll weight; int next; bool operator<(const Edge &rhs) { return weight < rhs.weight; } }; template <typename T> struct Edge2 { T from, to, weight; Edge2(T from_, T to_, T weight_) : from(from_), to(to_), weight(weight_) {} Edge2() : from(T(0)), to(T(0)), weight(T(0)) {} bool operator<(const Edge2 &rhs) { return weight < rhs.weight; } }; // vector<Edge> edges(MAXN << 1); // vi head(MAXN, -1); // vector<int> matchingx(MAXN, -1); // vector<int> matchingy(MAXN, -1); // vector<bool> check(MAXN); // vector<ll> dis(MAXN); // vector<bool> vis(MAXN, false); // int cnt = 1; // void addEdge(int from, int to, ll weight) { // edges[cnt].to = to; // edges[cnt].weight = weight; // edges[cnt].next = head[from]; // head[from] = cnt++; // } // // bool dfs(int u) { // for (int i = head[u]; i != -1; i = edges[i].next) { // int v = edges[i].to; // if (!check[v]) { // check[v] = true; // if (matchingy[v] == -1 || dfs(matchingy[v])) { // matchingy[v] = u; // matchingx[u] = v; // return true; // } // } // } // return false; //} // // int hungarian() { // int ans = 0; // fill(matchingx.begin(), matchingx.end(), -1); // fill(matchingy.begin(), matchingy.end(), -1); // for (int u = 1; u <= N; ++u) { //// if (matchingx[u] == -1) { // { // fill(check.begin(), check.end(), false); // if (dfs(u)) { // ++ans; // } // } // } // return ans; //} // void dijkstra(const ll s) { // priority_queue<P, vector<P>, greater<P>> que; // fill(dis.begin(), dis.end(), INF); // dis[s] = 0; // que.push(P(0, s)); // // multiple sources // for (auto& x : shops) { // dis[x] = 0; // que.push(P(0, x)); // } // while (!que.empty()) { // P p = que.top(); // que.pop(); // cout << "pop " << p.second << endl; // int u = p.second; // if (dis[u] < p.first) continue; // for (int i = head[u]; i != -1; i = edges[i].next) { // int v = edges[i].to; // if (dis[v] > dis[u] + edges[i].weight) { // dis[v] = dis[u] + edges[i].weight; // cout << "push " << v << endl; // que.push(P(dis[v], v)); // } // } // } // } // void zeroOneBFS(const int s) { // deque<P> que; // fill(dis.begin(), dis.end(), INF); // dis[s] = 0; // que.push_front(P(0, s)); // while (!que.empty()) { // P p = que.front(); // que.pop_front(); // int u = p.second; // if (dis[u] < p.first) continue; // for (int i = head[u]; i != -1; i = edges[i].next) { // int v = edges[i].to; // if (dis[v] > dis[u] + edges[i].weight) { // dis[v] = dis[u] + edges[i].weight; // if (edges[i].weight) { // que.push_back(P(dis[v], v)); // } else { // que.push_front(P(dis[v], v)); // } // } // } // } // } // Union-Find 并查集 class UnionFind { vector<ll> par; public: explicit UnionFind(ll n) : par(n, -1) {} ll root(ll a) { if (par[a] < 0) { return a; } return par[a] = root(par[a]); } ll size(ll a) { return -par[root(a)]; } void unite(ll a, ll b) { a = root(a); b = root(b); if (a != b) { if (size(a) < size(b)) { swap(a, b); } par[a] += par[b]; par[b] = a; } } }; // template<typename T> // ll kruskal(vector<Edge2>& edges2, const ll V) { // sort(edges2.begin(), edges2.end()); // UnionFind uf(V + 10); // ll res = 0; // Edge2 e; // for (int i = 0; i < edges2.size(); ++i) { // e = edges2[i]; // if (uf.root(e.u) != uf.root(e.v)) { // uf.unite(e.u, e.v); // res += e.w; // } // } // return res; // } // 线段树 struct SegmentTreeNode { ll maxVal; ll minVal; ll sum; ll len; ll lazy; ll left, right; SegmentTreeNode() {} }; class SegmentTree { public: explicit SegmentTree(size_t size, const vll &nums) : tree(size << 2), nums(nums) {} void build(ll root, ll left, ll right) { tree[root].lazy = 0; tree[root].left = left; tree[root].right = right; tree[root].len = right - left + 1; if (left == right) { tree[root].maxVal = nums[left]; tree[root].minVal = nums[left]; tree[root].sum = nums[left]; } else { ll mid = (right - left) / 2 + left; build(root * 2, left, mid); build(root * 2 + 1, mid + 1, right); tree[root].minVal = min(tree[root * 2].minVal, tree[root * 2 + 1].minVal); tree[root].maxVal = max(tree[root * 2].maxVal, tree[root * 2 + 1].maxVal); tree[root].sum = tree[root * 2].sum + tree[root * 2 + 1].sum; } } void pushup(ll root) { tree[root].minVal = min(tree[root * 2].minVal, tree[root * 2 + 1].minVal); tree[root].maxVal = max(tree[root * 2].maxVal, tree[root * 2 + 1].maxVal); tree[root].sum = tree[root * 2].sum + tree[root * 2 + 1].sum; } //// add single node val void add(ll root, ll id, ll addVal) { if (tree[root].left == tree[root].right) { tree[root].maxVal += addVal; tree[root].minVal += addVal; tree[root].sum += addVal; return; } ll mid = (tree[root].right - tree[root].left) / 2 + tree[root].left; if (id <= mid) { add(root * 2, id, addVal); } else { add(root * 2 + 1, id, addVal); } pushup(root); } void pushdown(ll root) { if (tree[root].lazy) { tree[root * 2].lazy += tree[root].lazy; tree[root * 2 + 1].lazy += tree[root].lazy; tree[root * 2].sum += tree[root * 2].len * tree[root].lazy; tree[root * 2 + 1].sum += tree[root * 2 + 1].len * tree[root].lazy; tree[root * 2].maxVal += tree[root].lazy; tree[root * 2 + 1].maxVal += tree[root].lazy; tree[root * 2].minVal += tree[root].lazy; tree[root * 2 + 1].minVal += tree[root].lazy; tree[root].lazy = 0; } } //// query range sum ll querySum(ll root, ll left, ll right) { if (tree[root].left >= left && tree[root].right <= right) { return tree[root].sum; } if (tree[root].left > right || tree[root].right < left) { return 0; } if (tree[root].lazy) { pushdown(root); } return querySum(root * 2, left, right) + querySum(root * 2 + 1, left, right); } //// query range max/min ll queryMax(ll root, ll left, ll right) { if (tree[root].left >= left && tree[root].right <= right) { return tree[root].maxVal; } if (tree[root].left > right || tree[root].right < left) { return -INF; } if (tree[root].lazy) { pushdown(root); } return max(queryMax(root * 2, left, right), queryMax(root * 2 + 1, left, right)); } ll queryMin(ll root, ll left, ll right) { if (tree[root].left >= left && tree[root].right <= right) { return tree[root].minVal; } if (tree[root].left > right || tree[root].right < left) { return INF; } if (tree[root].lazy) { pushdown(root); } return min(queryMin(root * 2, left, right), queryMin(root * 2 + 1, left, right)); } //// add range val void update(ll root, ll left, ll right, ll addVal) { if (tree[root].left >= left && tree[root].right <= right) { tree[root].lazy += addVal; tree[root].sum += tree[root].len * addVal; tree[root].maxVal += addVal; tree[root].minVal += addVal; return; } if (tree[root].left > right || tree[root].right < left) { return; } if (tree[root].lazy) { pushdown(root); } update(root * 2, left, right, addVal); update(root * 2 + 1, left, right, addVal); pushup(root); } private: vector<SegmentTreeNode> tree; const vll &nums; }; /// 组合数 ////約数求める //約数 // void divisor(ll n, vector<ll> &ret) { // for (ll i = 1; i * i <= n; i++) { // if (n % i == 0) { // ret.push_back(i); // if (i * i != n) ret.push_back(n / i); // } // } // sort(ret.begin(), ret.end()); // } ////階乗 ll factorial(ll n) { ll ret = 1; for (ll i = 1; i <= n; ++i) { ret = (ret * i) % MOD; } return ret; } ////モジュラ逆数 ll inv_mod(ll n) { ll a = n % MOD, b = MOD - 2, ret = 1; while (b > 0) { if (b & 1) ret = (ret * a) % MOD; a = (a * a) % MOD; b >>= 1; } return ret; } ll nPr(ll n, ll r) { ll ret = 1; for (ll i = n; i >= n - r + 1; --i) { ret = ret * (i % MOD) % MOD; } return ret; } ll nCr(ll n, ll r) { return nPr(n, r) * inv_mod(factorial(r)) % MOD; } // // vll F(MAXN), Finv(MAXN), inv(MAXN); // // ll pow_mod(ll a, ll b, ll p) { // ll ret = 1; // while (b) { // if (b & 1) ret = (ret * a) % p; // a = (a * a) % p; // b >>= 1; // } // return ret; //} // // void comb_init() { // inv[1] = 1; // for (int i = 2; i < MAXN; ++i) { // inv[i] = (MOD - MOD / i) * 1ll * inv[MOD % i] % MOD; // } // F[0] = Finv[0] = 1; // for (int i = 1; i < MAXN; ++i) { // F[i] = F[i-1] * 1ll * i % MOD; // Finv[i] = Finv[i-1] * 1ll * inv[i] % MOD; // } //} // // inline ll comb(ll n, ll m) { // if (m < 0 || m > n) return 0; // return F[n] * 1ll * Finv[n - m] % MOD * Finv[m] % MOD; // } // inline ll ADD(ll a, ll b) { return (a + b) % MOD; } inline ll MUL(ll a, ll b) { return (a % MOD) * (b % MOD) % MOD; } inline ll SUB(ll a, ll b) { return (a + MOD - b) % MOD; } /// main函数 int main() { int N; cin >> N; vi P(N); cin >> P; int first, second; ll sum = 0; for (int L = 0; L < N - 1; ++L) { first = P[L]; second = -1; for (int R = L + 1; R < N; ++R) { if (P[R] > first) { second = first; first = P[R]; } else if (P[R] > second) { second = P[R]; } sum += second; } } coutn(sum); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using vll = vector<ll>; using vi = vector<int>; using vb = vector<bool>; using pll = pair<ll, ll>; using pii = pair<int, int>; using vpii = vector<pii>; using vpll = vector<pll>; const ll LINF = 1ll << 55; const ll INF = 0x3f3f3f3f; const ll MOD = 1e9 + 7; const int MAXN = 1e5 + 10; const ll dx[] = {1, 0, -1, 0}; const ll dy[] = {0, 1, 0, -1}; /// cin/cout overloading template <typename T> ostream &operator<<(ostream &out, vector<T> &vec) { for (auto it = vec.begin(); it != vec.end(); ++it) { out << *it << " "; } return out; } template <typename T> ostream &operator<<(ostream &out, pair<T, T> &P) { out << P.first << " " << P.second; return out; } template <typename T> istream &operator>>(istream &in, vector<T> &vec) { for (auto it = vec.begin(); it != vec.end(); ++it) { in >> *it; } return in; } template <typename T> istream &operator>>(istream &in, pair<T, T> &P) { in >> P.first >> P.second; return in; } #define coutp(x, y) cout << (x) << " " << (y) << endl #define coutn(x) cout << (x) << endl #define coutd(x) cout << setprecision(10) << (x) << endl /// 图相关 // ll N, M; struct Edge { int to; ll weight; int next; bool operator<(const Edge &rhs) { return weight < rhs.weight; } }; template <typename T> struct Edge2 { T from, to, weight; Edge2(T from_, T to_, T weight_) : from(from_), to(to_), weight(weight_) {} Edge2() : from(T(0)), to(T(0)), weight(T(0)) {} bool operator<(const Edge2 &rhs) { return weight < rhs.weight; } }; // vector<Edge> edges(MAXN << 1); // vi head(MAXN, -1); // vector<int> matchingx(MAXN, -1); // vector<int> matchingy(MAXN, -1); // vector<bool> check(MAXN); // vector<ll> dis(MAXN); // vector<bool> vis(MAXN, false); // int cnt = 1; // void addEdge(int from, int to, ll weight) { // edges[cnt].to = to; // edges[cnt].weight = weight; // edges[cnt].next = head[from]; // head[from] = cnt++; // } // // bool dfs(int u) { // for (int i = head[u]; i != -1; i = edges[i].next) { // int v = edges[i].to; // if (!check[v]) { // check[v] = true; // if (matchingy[v] == -1 || dfs(matchingy[v])) { // matchingy[v] = u; // matchingx[u] = v; // return true; // } // } // } // return false; //} // // int hungarian() { // int ans = 0; // fill(matchingx.begin(), matchingx.end(), -1); // fill(matchingy.begin(), matchingy.end(), -1); // for (int u = 1; u <= N; ++u) { //// if (matchingx[u] == -1) { // { // fill(check.begin(), check.end(), false); // if (dfs(u)) { // ++ans; // } // } // } // return ans; //} // void dijkstra(const ll s) { // priority_queue<P, vector<P>, greater<P>> que; // fill(dis.begin(), dis.end(), INF); // dis[s] = 0; // que.push(P(0, s)); // // multiple sources // for (auto& x : shops) { // dis[x] = 0; // que.push(P(0, x)); // } // while (!que.empty()) { // P p = que.top(); // que.pop(); // cout << "pop " << p.second << endl; // int u = p.second; // if (dis[u] < p.first) continue; // for (int i = head[u]; i != -1; i = edges[i].next) { // int v = edges[i].to; // if (dis[v] > dis[u] + edges[i].weight) { // dis[v] = dis[u] + edges[i].weight; // cout << "push " << v << endl; // que.push(P(dis[v], v)); // } // } // } // } // void zeroOneBFS(const int s) { // deque<P> que; // fill(dis.begin(), dis.end(), INF); // dis[s] = 0; // que.push_front(P(0, s)); // while (!que.empty()) { // P p = que.front(); // que.pop_front(); // int u = p.second; // if (dis[u] < p.first) continue; // for (int i = head[u]; i != -1; i = edges[i].next) { // int v = edges[i].to; // if (dis[v] > dis[u] + edges[i].weight) { // dis[v] = dis[u] + edges[i].weight; // if (edges[i].weight) { // que.push_back(P(dis[v], v)); // } else { // que.push_front(P(dis[v], v)); // } // } // } // } // } // Union-Find 并查集 class UnionFind { vector<ll> par; public: explicit UnionFind(ll n) : par(n, -1) {} ll root(ll a) { if (par[a] < 0) { return a; } return par[a] = root(par[a]); } ll size(ll a) { return -par[root(a)]; } void unite(ll a, ll b) { a = root(a); b = root(b); if (a != b) { if (size(a) < size(b)) { swap(a, b); } par[a] += par[b]; par[b] = a; } } }; // template<typename T> // ll kruskal(vector<Edge2>& edges2, const ll V) { // sort(edges2.begin(), edges2.end()); // UnionFind uf(V + 10); // ll res = 0; // Edge2 e; // for (int i = 0; i < edges2.size(); ++i) { // e = edges2[i]; // if (uf.root(e.u) != uf.root(e.v)) { // uf.unite(e.u, e.v); // res += e.w; // } // } // return res; // } // 线段树 struct SegmentTreeNode { ll maxVal; ll minVal; ll sum; ll len; ll lazy; ll left, right; SegmentTreeNode() {} }; class SegmentTree { public: explicit SegmentTree(size_t size, const vll &nums) : tree(size << 2), nums(nums) {} void build(ll root, ll left, ll right) { tree[root].lazy = 0; tree[root].left = left; tree[root].right = right; tree[root].len = right - left + 1; if (left == right) { tree[root].maxVal = nums[left]; tree[root].minVal = nums[left]; tree[root].sum = nums[left]; } else { ll mid = (right - left) / 2 + left; build(root * 2, left, mid); build(root * 2 + 1, mid + 1, right); tree[root].minVal = min(tree[root * 2].minVal, tree[root * 2 + 1].minVal); tree[root].maxVal = max(tree[root * 2].maxVal, tree[root * 2 + 1].maxVal); tree[root].sum = tree[root * 2].sum + tree[root * 2 + 1].sum; } } void pushup(ll root) { tree[root].minVal = min(tree[root * 2].minVal, tree[root * 2 + 1].minVal); tree[root].maxVal = max(tree[root * 2].maxVal, tree[root * 2 + 1].maxVal); tree[root].sum = tree[root * 2].sum + tree[root * 2 + 1].sum; } //// add single node val void add(ll root, ll id, ll addVal) { if (tree[root].left == tree[root].right) { tree[root].maxVal += addVal; tree[root].minVal += addVal; tree[root].sum += addVal; return; } ll mid = (tree[root].right - tree[root].left) / 2 + tree[root].left; if (id <= mid) { add(root * 2, id, addVal); } else { add(root * 2 + 1, id, addVal); } pushup(root); } void pushdown(ll root) { if (tree[root].lazy) { tree[root * 2].lazy += tree[root].lazy; tree[root * 2 + 1].lazy += tree[root].lazy; tree[root * 2].sum += tree[root * 2].len * tree[root].lazy; tree[root * 2 + 1].sum += tree[root * 2 + 1].len * tree[root].lazy; tree[root * 2].maxVal += tree[root].lazy; tree[root * 2 + 1].maxVal += tree[root].lazy; tree[root * 2].minVal += tree[root].lazy; tree[root * 2 + 1].minVal += tree[root].lazy; tree[root].lazy = 0; } } //// query range sum ll querySum(ll root, ll left, ll right) { if (tree[root].left >= left && tree[root].right <= right) { return tree[root].sum; } if (tree[root].left > right || tree[root].right < left) { return 0; } if (tree[root].lazy) { pushdown(root); } return querySum(root * 2, left, right) + querySum(root * 2 + 1, left, right); } //// query range max/min ll queryMax(ll root, ll left, ll right) { if (tree[root].left >= left && tree[root].right <= right) { return tree[root].maxVal; } if (tree[root].left > right || tree[root].right < left) { return -INF; } if (tree[root].lazy) { pushdown(root); } return max(queryMax(root * 2, left, right), queryMax(root * 2 + 1, left, right)); } ll queryMin(ll root, ll left, ll right) { if (tree[root].left >= left && tree[root].right <= right) { return tree[root].minVal; } if (tree[root].left > right || tree[root].right < left) { return INF; } if (tree[root].lazy) { pushdown(root); } return min(queryMin(root * 2, left, right), queryMin(root * 2 + 1, left, right)); } //// add range val void update(ll root, ll left, ll right, ll addVal) { if (tree[root].left >= left && tree[root].right <= right) { tree[root].lazy += addVal; tree[root].sum += tree[root].len * addVal; tree[root].maxVal += addVal; tree[root].minVal += addVal; return; } if (tree[root].left > right || tree[root].right < left) { return; } if (tree[root].lazy) { pushdown(root); } update(root * 2, left, right, addVal); update(root * 2 + 1, left, right, addVal); pushup(root); } private: vector<SegmentTreeNode> tree; const vll &nums; }; /// 组合数 ////約数求める //約数 // void divisor(ll n, vector<ll> &ret) { // for (ll i = 1; i * i <= n; i++) { // if (n % i == 0) { // ret.push_back(i); // if (i * i != n) ret.push_back(n / i); // } // } // sort(ret.begin(), ret.end()); // } ////階乗 ll factorial(ll n) { ll ret = 1; for (ll i = 1; i <= n; ++i) { ret = (ret * i) % MOD; } return ret; } ////モジュラ逆数 ll inv_mod(ll n) { ll a = n % MOD, b = MOD - 2, ret = 1; while (b > 0) { if (b & 1) ret = (ret * a) % MOD; a = (a * a) % MOD; b >>= 1; } return ret; } ll nPr(ll n, ll r) { ll ret = 1; for (ll i = n; i >= n - r + 1; --i) { ret = ret * (i % MOD) % MOD; } return ret; } ll nCr(ll n, ll r) { return nPr(n, r) * inv_mod(factorial(r)) % MOD; } // // vll F(MAXN), Finv(MAXN), inv(MAXN); // // ll pow_mod(ll a, ll b, ll p) { // ll ret = 1; // while (b) { // if (b & 1) ret = (ret * a) % p; // a = (a * a) % p; // b >>= 1; // } // return ret; //} // // void comb_init() { // inv[1] = 1; // for (int i = 2; i < MAXN; ++i) { // inv[i] = (MOD - MOD / i) * 1ll * inv[MOD % i] % MOD; // } // F[0] = Finv[0] = 1; // for (int i = 1; i < MAXN; ++i) { // F[i] = F[i-1] * 1ll * i % MOD; // Finv[i] = Finv[i-1] * 1ll * inv[i] % MOD; // } //} // // inline ll comb(ll n, ll m) { // if (m < 0 || m > n) return 0; // return F[n] * 1ll * Finv[n - m] % MOD * Finv[m] % MOD; // } // inline ll ADD(ll a, ll b) { return (a + b) % MOD; } inline ll MUL(ll a, ll b) { return (a % MOD) * (b % MOD) % MOD; } inline ll SUB(ll a, ll b) { return (a + MOD - b) % MOD; } /// main函数 int main() { int N; cin >> N; vi pos(N + 1); int cur = 0; for (int i = 1; i <= N; ++i) { cin >> cur; pos[cur] = i; } multiset<ll> seen; seen.insert(0); seen.insert(0); seen.insert(N + 1); seen.insert(N + 1); ll ans = 0; for (int i = N; i > 0; --i) { seen.insert(pos[i]); auto it = seen.find(pos[i]); --it; auto b = *it; --it; auto a = *it; ++it; ++it; ++it; auto y = *it; ++it; auto z = *it; ans += i * ((b - a) * (y - pos[i]) + (pos[i] - b) * (z - y)); } coutn(ans); return 0; }
replace
426
444
426
454
TLE
p02919
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, N) for (int i = 0; i < (int)N; i++) const ll MOD = pow(10, 9) + 7; const ll LLINF = pow(2, 61) - 1; const int INF = pow(2, 30) - 1; int main() { int N; cin >> N; int P[N]; rep(i, N) { cin >> P[i]; P[i]--; } int pos[N]; rep(i, N) pos[(N - 1) - P[i]] = i; multiset<int> gr; gr.insert(-1); gr.insert(-1); gr.insert(N); gr.insert(N); ll result = 0; rep(i, N) { auto r1 = upper_bound(gr.begin(), gr.end(), pos[i]); auto r2 = r1; r2++; auto l1 = r1; l1--; auto l2 = l1; l2--; ll tmp = (pos[i] - *l1) * (*r2 - *r1) + (*r1 - pos[i]) * (*l1 - *l2); result += tmp * (N - i); // cout << *l2 << " " << *l1 << " " << *r1 << " " << *r2 << endl; // cout << N-i << ":" << tmp << endl; gr.insert(pos[i]); } cout << result << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, N) for (int i = 0; i < (int)N; i++) const ll MOD = pow(10, 9) + 7; const ll LLINF = pow(2, 61) - 1; const int INF = pow(2, 30) - 1; int main() { int N; cin >> N; int P[N]; rep(i, N) { cin >> P[i]; P[i]--; } int pos[N]; rep(i, N) pos[(N - 1) - P[i]] = i; multiset<int> gr; gr.insert(-1); gr.insert(-1); gr.insert(N); gr.insert(N); ll result = 0; rep(i, N) { auto r1 = gr.upper_bound(pos[i]); auto r2 = r1; r2++; auto l1 = r1; l1--; auto l2 = l1; l2--; ll tmp = (pos[i] - *l1) * (*r2 - *r1) + (*r1 - pos[i]) * (*l1 - *l2); result += tmp * (N - i); // cout << *l2 << " " << *l1 << " " << *r1 << " " << *r2 << endl; // cout << N-i << ":" << tmp << endl; gr.insert(pos[i]); } cout << result << endl; return 0; }
replace
27
28
27
28
TLE
p02919
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++) #define RITR(x, c) for (__typeof(c.rbegin()) x = c.rbegin(); x != c.rend(); x++) #define setp(n) fixed << setprecision(n) #define lf double #define ll long long #define vll vector<ll> #define vi vector<int> #define pll pair<ll, ll> #define pi pair<int, int> #define all(a) (a.begin()), (a.end()) #define rall(a) (a.rbegin()), (a.rend()) #define fi first #define se second #define pb push_back #define mp make_pair #define ins insert using namespace std; int main(void) { cin.tie(0); ios::sync_with_stdio(false); ll n; cin >> n; map<ll, ll> p; rep(i, n) { ll temp; cin >> temp; p.ins(mp(temp, i)); } multiset<ll> q; q.ins(-1); q.ins(-1); q.ins(n); q.ins(n); ll ans = 0; for (int i = n; i >= 1; i--) { auto bs = lower_bound(q.begin(), q.end(), p[i]); ll r1 = *bs; bs++; ll r2 = *bs; bs--; bs--; ll l2 = *bs; bs--; ll l1 = *bs; ans += i * (l2 - l1) * (r1 - p[i]); ans += i * (p[i] - l2) * (r2 - r1); q.ins(p[i]); } cout << ans << "\n"; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++) #define RITR(x, c) for (__typeof(c.rbegin()) x = c.rbegin(); x != c.rend(); x++) #define setp(n) fixed << setprecision(n) #define lf double #define ll long long #define vll vector<ll> #define vi vector<int> #define pll pair<ll, ll> #define pi pair<int, int> #define all(a) (a.begin()), (a.end()) #define rall(a) (a.rbegin()), (a.rend()) #define fi first #define se second #define pb push_back #define mp make_pair #define ins insert using namespace std; int main(void) { cin.tie(0); ios::sync_with_stdio(false); ll n; cin >> n; map<ll, ll> p; rep(i, n) { ll temp; cin >> temp; p.ins(mp(temp, i)); } multiset<ll> q; q.ins(-1); q.ins(-1); q.ins(n); q.ins(n); ll ans = 0; for (int i = n; i >= 1; i--) { auto bs = q.lower_bound(p[i]); ll r1 = *bs; bs++; ll r2 = *bs; bs--; bs--; ll l2 = *bs; bs--; ll l1 = *bs; ans += i * (l2 - l1) * (r1 - p[i]); ans += i * (p[i] - l2) * (r2 - r1); q.ins(p[i]); } cout << ans << "\n"; return 0; }
replace
44
45
44
45
TLE
p02919
C++
Runtime Error
#include <bits/stdc++.h> #define rep2(x, fr, to) for (int(x) = (fr); (x) < (to); (x)++) #define rep(x, to) for (int(x) = 0; (x) < (to); (x)++) #define repr(x, fr, to) for (int(x) = (fr); (x) >= (to); (x)--) #define all(c) (c).begin(), (c).end() #define sz(v) (int)(v).size() using namespace std; typedef long long ll; typedef vector<int> VI; typedef pair<int, int> pii; const int MD = (int)1e9 + 7; typedef vector<ll> VL; void dbg() { cerr << "\n"; } template <class T, class... T2> void dbg(const T &fst, const T2 &...rst) { cerr << fst << ": "; dbg(rst...); } int main() { const int MXZ = 1024; cin.tie(0); ios_base::sync_with_stdio(false); int n; ll ans = 0; cin >> n; vector<pii> p(n); rep(i, n) { cin >> p[i].first; p[i].second = i + 1; } sort(p.rbegin(), p.rend()); bitset<MXZ> bs1, bs2; bs1[0] = bs2[0] = 1; bs1[n + 1] = bs2[n + 1] = 1; rep(i, n) { int cid = p[i].second; bs1[cid] = bs2[n - cid] = 1; int l1 = 0, l2 = 0; int r1 = n + 1, r2 = n + 1; auto it = bs1._Find_next(cid); if (it <= n) { r1 = it; it = bs1._Find_next(r1); if (it <= n) r2 = it; } it = bs2._Find_next(n - cid); if (it <= n) { l2 = it; it = bs2._Find_next(l2); if (it <= n) l1 = n - it; l2 = n - l2; } ll a = cid - l2, b = l2 - l1; ll c = r1 - cid, d = r2 - r1; ans += (a * d + b * c) * p[i].first; // dbg(i, p[i].first, l1,l2,r1,r2, a*d+b*c, ans); } cout << ans << "\n"; return 0; }
#include <bits/stdc++.h> #define rep2(x, fr, to) for (int(x) = (fr); (x) < (to); (x)++) #define rep(x, to) for (int(x) = 0; (x) < (to); (x)++) #define repr(x, fr, to) for (int(x) = (fr); (x) >= (to); (x)--) #define all(c) (c).begin(), (c).end() #define sz(v) (int)(v).size() using namespace std; typedef long long ll; typedef vector<int> VI; typedef pair<int, int> pii; const int MD = (int)1e9 + 7; typedef vector<ll> VL; void dbg() { cerr << "\n"; } template <class T, class... T2> void dbg(const T &fst, const T2 &...rst) { cerr << fst << ": "; dbg(rst...); } int main() { const int MXZ = 1 << 17; cin.tie(0); ios_base::sync_with_stdio(false); int n; ll ans = 0; cin >> n; vector<pii> p(n); rep(i, n) { cin >> p[i].first; p[i].second = i + 1; } sort(p.rbegin(), p.rend()); bitset<MXZ> bs1, bs2; bs1[0] = bs2[0] = 1; bs1[n + 1] = bs2[n + 1] = 1; rep(i, n) { int cid = p[i].second; bs1[cid] = bs2[n - cid] = 1; int l1 = 0, l2 = 0; int r1 = n + 1, r2 = n + 1; auto it = bs1._Find_next(cid); if (it <= n) { r1 = it; it = bs1._Find_next(r1); if (it <= n) r2 = it; } it = bs2._Find_next(n - cid); if (it <= n) { l2 = it; it = bs2._Find_next(l2); if (it <= n) l1 = n - it; l2 = n - l2; } ll a = cid - l2, b = l2 - l1; ll c = r1 - cid, d = r2 - r1; ans += (a * d + b * c) * p[i].first; // dbg(i, p[i].first, l1,l2,r1,r2, a*d+b*c, ans); } cout << ans << "\n"; return 0; }
replace
21
22
21
22
0
p02919
C++
Time Limit Exceeded
/* while(!success_achieved()) work_hard(); code by:aman_ayush */ #include <bits/stdc++.h> using namespace std; #define ll long long int main() { ll n; cin >> n; ll arr[n], pos[n + 1]; for (ll i = 0; i < n; i++) { cin >> arr[i]; pos[arr[i]] = i; } multiset<ll> ms; ms.insert(-1); ms.insert(-1); ms.insert(n); ms.insert(n); ms.insert(pos[n]); ll cnt = 0; for (ll i = n - 1; i >= 1; i--) { auto itr = upper_bound(ms.begin(), ms.end(), pos[i]); ll r1 = *itr; itr++; ll r2 = *itr; for (int j = 0; j < 2; j++) itr--; ll l1 = *itr; itr--; ll l2 = *itr; cnt += i * ((r2 - r1) * (pos[i] - l1) + (l1 - l2) * (r1 - pos[i])); ms.insert(pos[i]); } cout << cnt << "\n"; return 0; }
/* while(!success_achieved()) work_hard(); code by:aman_ayush */ #include <bits/stdc++.h> using namespace std; #define ll long long int main() { ll n; cin >> n; ll arr[n], pos[n + 1]; for (ll i = 0; i < n; i++) { cin >> arr[i]; pos[arr[i]] = i; } multiset<ll> ms; ms.insert(-1); ms.insert(-1); ms.insert(n); ms.insert(n); ms.insert(pos[n]); ll cnt = 0; for (ll i = n - 1; i >= 1; i--) { auto itr = ms.upper_bound(pos[i]); ll r1 = *itr; itr++; ll r2 = *itr; for (int j = 0; j < 2; j++) itr--; ll l1 = *itr; itr--; ll l2 = *itr; cnt += i * ((r2 - r1) * (pos[i] - l1) + (l1 - l2) * (r1 - pos[i])); ms.insert(pos[i]); } cout << cnt << "\n"; return 0; }
replace
26
27
26
27
TLE
p02919
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define ALL(obj) (obj).begin(), (obj).end() #define REP(i, n) for (int i = 0; i < (n); ++i) #define RREP(i, n) for (int i = (n)-1; i >= 0; --i) #define ln '\n' #define pb push_back #define eb emplace_back #define prique priority_queue #define umap unordered_map #define BIG 2000000000 #define VERYBIG 1000000000000000ll #define PI 3.1415926 #define coutdb cout << fixed << setprecision(15) const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1}, dy[] = {0, -1, 0, 1, 1, -1, 1, -1}; const long long MOD = 1e9 + 7; // typedef int_fast64_t ll; #define int int_fast64_t using Graph = vector<vector<int>>; typedef pair<int, int> P; template <typename T> inline T GCD(T a, T b) { T c; while (b != 0) { c = a % b; a = b; b = c; } return a; } template <typename T> inline T LCM(T a, T b) { T c = GCD(a, b); a /= c; return a * b; } template <typename T> inline T nCr(T a, T b) { T i, r = 1; for (i = 1; i <= b; i++) { r *= (a + 1 - i); r /= i; } return r; } template <typename T> inline T nHr(T a, T b) { return nCr(a + b - 1, b); } signed main(void) { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; vector<int> P(N), idx(N + 1); REP(i, N) { cin >> P[i]; idx[P[i]] = i; } multiset<int> S; S.insert(-1); S.insert(-1); S.insert(N); S.insert(N); int ans = 0, index = idx[N]; for (int i = N; i > 0; i--) { auto itr = lower_bound(ALL(S), index); int tmin = *itr++; int tsec = *itr--; itr--; int smax = *itr--; int ssec = *itr; ans += i * (smax - ssec) * (tmin - index); ans += i * (index - smax) * (tsec - tmin); S.insert(index); if (i == 1) break; index = idx[i - 1]; } cout << ans << ln; }
#include <bits/stdc++.h> using namespace std; #define ALL(obj) (obj).begin(), (obj).end() #define REP(i, n) for (int i = 0; i < (n); ++i) #define RREP(i, n) for (int i = (n)-1; i >= 0; --i) #define ln '\n' #define pb push_back #define eb emplace_back #define prique priority_queue #define umap unordered_map #define BIG 2000000000 #define VERYBIG 1000000000000000ll #define PI 3.1415926 #define coutdb cout << fixed << setprecision(15) const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1}, dy[] = {0, -1, 0, 1, 1, -1, 1, -1}; const long long MOD = 1e9 + 7; // typedef int_fast64_t ll; #define int int_fast64_t using Graph = vector<vector<int>>; typedef pair<int, int> P; template <typename T> inline T GCD(T a, T b) { T c; while (b != 0) { c = a % b; a = b; b = c; } return a; } template <typename T> inline T LCM(T a, T b) { T c = GCD(a, b); a /= c; return a * b; } template <typename T> inline T nCr(T a, T b) { T i, r = 1; for (i = 1; i <= b; i++) { r *= (a + 1 - i); r /= i; } return r; } template <typename T> inline T nHr(T a, T b) { return nCr(a + b - 1, b); } signed main(void) { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; vector<int> P(N), idx(N + 1); REP(i, N) { cin >> P[i]; idx[P[i]] = i; } multiset<int> S; S.insert(-1); S.insert(-1); S.insert(N); S.insert(N); int ans = 0, index = idx[N]; for (int i = N; i > 0; i--) { auto itr = S.lower_bound(index); int tmin = *itr++; int tsec = *itr--; itr--; int smax = *itr--; int ssec = *itr; ans += i * (smax - ssec) * (tmin - index); ans += i * (index - smax) * (tsec - tmin); S.insert(index); if (i == 1) break; index = idx[i - 1]; } cout << ans << ln; }
replace
68
69
68
69
TLE
p02919
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cmath> #include <complex> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string.h> #include <string> #include <vector> // output #define SPBR(w, n) std::cout << (w + 1 == n ? '\n' : ' '); #define YES cout << "YES" << endl #define Yes cout << "Yes" << endl #define NO cout << "NO" << endl #define No cout << "No" << endl // utility #define ALL(i) (i).begin(), (i).end() #define FOR(i, a, n) for (int i = (a); i < (n); ++i) #define RFOR(i, a, n) for (int i = (n)-1; i >= (a); --i) #define REP(i, n) for (int i = 0; i < int(n); ++i) #define RREP(i, n) for (int i = int(n) - 1; i >= 0; --i) #define IN(a, x, b) (a <= x && x < b) #define OUT(a, x, b) (x < a || b <= x) template <class T> inline T chmax(T &a, const T b) { return a = (a < b) ? b : a; } template <class T> inline T chmin(T &a, const T b) { return a = (a > b) ? b : a; } // type/const #define int ll using ll = long long; using ull = unsigned long long; using ld = long double; const int MOD = 1000000007; /* const int MOD = 998244353; */ const int INF = 1e18; const double PI = acos(-1); using namespace std; struct INIT { INIT() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(10); } } INIT; signed main() { int N; cin >> N; vector<int> A(N); REP(i, N) cin >> A[i]; vector<int> o(N); iota(ALL(o), 0); sort(ALL(o), [A](int l, int r) { return A[l] > A[r]; }); multiset<int> s; s.insert(-1); s.insert(N); s.insert(-1); s.insert(N); int ans = 0; REP(i, N) { int I = o[i]; auto it = s.upper_bound(I); int r1 = *it; it++; int r2 = *it; it--; it--; int l2 = *it; it--; int l1 = *it; ans += ((l2 - l1) * (r1 - I) + (I - l2) * (r2 - r1)) * A[I]; s.insert(I); } cout << ans << "\n"; return 0; }
#include <algorithm> #include <bitset> #include <cmath> #include <complex> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string.h> #include <string> #include <vector> // output #define SPBR(w, n) std::cout << (w + 1 == n ? '\n' : ' '); #define YES cout << "YES" << endl #define Yes cout << "Yes" << endl #define NO cout << "NO" << endl #define No cout << "No" << endl // utility #define ALL(i) (i).begin(), (i).end() #define FOR(i, a, n) for (int i = (a); i < (n); ++i) #define RFOR(i, a, n) for (int i = (n)-1; i >= (a); --i) #define REP(i, n) for (int i = 0; i < int(n); ++i) #define RREP(i, n) for (int i = int(n) - 1; i >= 0; --i) #define IN(a, x, b) (a <= x && x < b) #define OUT(a, x, b) (x < a || b <= x) template <class T> inline T chmax(T &a, const T b) { return a = (a < b) ? b : a; } template <class T> inline T chmin(T &a, const T b) { return a = (a > b) ? b : a; } // type/const #define int ll using ll = long long; using ull = unsigned long long; using ld = long double; const int MOD = 1000000007; /* const int MOD = 998244353; */ const int INF = 1e18; const double PI = acos(-1); using namespace std; struct INIT { INIT() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(10); } } INIT; signed main() { int N; cin >> N; vector<int> A(N); REP(i, N) cin >> A[i]; vector<int> o(N); iota(ALL(o), 0); sort(ALL(o), [&](int const &l, int const &r) { return A[l] > A[r]; }); multiset<int> s; s.insert(-1); s.insert(N); s.insert(-1); s.insert(N); int ans = 0; REP(i, N) { int I = o[i]; auto it = s.upper_bound(I); int r1 = *it; it++; int r2 = *it; it--; it--; int l2 = *it; it--; int l1 = *it; ans += ((l2 - l1) * (r1 - I) + (I - l2) * (r2 - r1)) * A[I]; s.insert(I); } cout << ans << "\n"; return 0; }
replace
66
67
66
67
TLE
p02919
C++
Runtime Error
#include <algorithm> #include <cassert> #include <chrono> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define rep(i, a, b) for (auto i = (a); i < (b); i++) #define rrep(i, a, b) for (auto i = (a); i > (b); i--) #define all(v) (v).begin(), (v).end() #define print(v) \ { \ for (auto x : v) \ cout << x << ' '; \ cout << endl; \ } #define printn(v, n) \ { \ for (int _i = 0; _i < n; _i++) \ cout << *(v + _i) << ' '; \ cout << endl; \ } typedef unsigned long long ull; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<double, double> pdd; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<vvi> vvvi; typedef vector<pii> vpii; const int MAXN = 1e3 + 20; long long n; long long a[MAXN], pos[MAXN]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; pos[a[i]] = i; } long long l = min(pos[n], pos[n - 1]), r = max(pos[n], pos[n - 1]); set<long long> st({0, l, r, n + 1}); long long res = (n - 1) * (n - r + 1) * l; // cout << res << endl; for (int i = n - 2; i >= 1; i--) { st.insert(pos[i]); auto it1 = st.find(pos[i]); it1--; auto it2 = it1; if (it2 != st.begin()) it2--; auto it3 = st.find(pos[i]); it3++; auto it4 = it3; it4++; if (it4 == st.end()) it4--; res += (*it3 - pos[i]) * (*it1 - *it2) * i; res += (pos[i] - *it1) * (*it4 - *it3) * i; } cout << res << endl; return 0; }
#include <algorithm> #include <cassert> #include <chrono> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define rep(i, a, b) for (auto i = (a); i < (b); i++) #define rrep(i, a, b) for (auto i = (a); i > (b); i--) #define all(v) (v).begin(), (v).end() #define print(v) \ { \ for (auto x : v) \ cout << x << ' '; \ cout << endl; \ } #define printn(v, n) \ { \ for (int _i = 0; _i < n; _i++) \ cout << *(v + _i) << ' '; \ cout << endl; \ } typedef unsigned long long ull; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<double, double> pdd; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<vvi> vvvi; typedef vector<pii> vpii; const int MAXN = 1e5 + 20; long long n; long long a[MAXN], pos[MAXN]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; pos[a[i]] = i; } long long l = min(pos[n], pos[n - 1]), r = max(pos[n], pos[n - 1]); set<long long> st({0, l, r, n + 1}); long long res = (n - 1) * (n - r + 1) * l; // cout << res << endl; for (int i = n - 2; i >= 1; i--) { st.insert(pos[i]); auto it1 = st.find(pos[i]); it1--; auto it2 = it1; if (it2 != st.begin()) it2--; auto it3 = st.find(pos[i]); it3++; auto it4 = it3; it4++; if (it4 == st.end()) it4--; res += (*it3 - pos[i]) * (*it1 - *it2) * i; res += (pos[i] - *it1) * (*it4 - *it3) * i; } cout << res << endl; return 0; }
replace
50
51
50
51
0
p02919
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> VI; typedef vector<ll> VL; typedef vector<VI> VVI; typedef vector<VL> VVL; typedef pair<int, int> P; typedef pair<ll, ll> PL; #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) for (int i = 0; i < (n); ++i) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define debug(x) cout << #x << ": " << x << endl #define out(x) cout << x << endl // #define int long long int const int MOD = 1000000007; const ll LINF = (ll)1e18 - 1; const int INF = 1e9 - 1; const double EPS = 0.000000001; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; void print(const vector<int> &v) { for (auto x : v) { cout << x << " "; } cout << endl; } signed main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; VI p(n), pos(n); multiset<int> s; FOR(i, 1, n + 1) { cin >> p[i]; pos[p[i]] = i; } s.insert(0); s.insert(0); s.insert(n + 1); s.insert(n + 1); ll ans = 0; for (int i = n; i >= 1; --i) { auto it = s.lower_bound(pos[i]); int a = *(++it); int b = *(--it); int c = *(--it); int d = *(--it); ans += 1LL * ((a - b) * (pos[i] - c) + (c - d) * (b - pos[i])) * i; s.insert(pos[i]); } out(ans); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> VI; typedef vector<ll> VL; typedef vector<VI> VVI; typedef vector<VL> VVL; typedef pair<int, int> P; typedef pair<ll, ll> PL; #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) for (int i = 0; i < (n); ++i) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define debug(x) cout << #x << ": " << x << endl #define out(x) cout << x << endl // #define int long long int const int MOD = 1000000007; const ll LINF = (ll)1e18 - 1; const int INF = 1e9 - 1; const double EPS = 0.000000001; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; void print(const vector<int> &v) { for (auto x : v) { cout << x << " "; } cout << endl; } signed main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; VI p(n + 3), pos(n + 3); multiset<int> s; FOR(i, 1, n + 1) { cin >> p[i]; pos[p[i]] = i; } s.insert(0); s.insert(0); s.insert(n + 1); s.insert(n + 1); ll ans = 0; for (int i = n; i >= 1; --i) { auto it = s.lower_bound(pos[i]); int a = *(++it); int b = *(--it); int c = *(--it); int d = *(--it); ans += 1LL * ((a - b) * (pos[i] - c) + (c - d) * (b - pos[i])) * i; s.insert(pos[i]); } out(ans); return 0; }
replace
36
37
36
37
0
p02919
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> o(n); for (int i = 1; i <= n; i++) { int p; cin >> p; o.at(p - 1) = i; } set<int> s; s.insert(0); s.insert(n + 1); int64_t a = 0; for (int i = n - 1; i >= 0; i--) { auto l = prev(lower_bound(s.begin(), s.end(), o.at(i))); auto r = lower_bound(s.begin(), s.end(), o.at(i)); int64_t xl = (*l == 0 ? 0 : (int64_t)(*l - *prev(l)) * (*r - o.at(i))); int64_t xr = (*r == n + 1 ? 0 : (int64_t)(o.at(i) - *l) * (*next(r) - *r)); a += (xl + xr) * (i + 1); s.insert(o.at(i)); } cout << a << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> o(n); for (int i = 1; i <= n; i++) { int p; cin >> p; o.at(p - 1) = i; } set<int> s; s.insert(0); s.insert(n + 1); int64_t a = 0; for (int i = n - 1; i >= 0; i--) { auto l = prev(s.lower_bound(o.at(i))); auto r = s.lower_bound(o.at(i)); int64_t xl = (*l == 0 ? 0 : (int64_t)(*l - *prev(l)) * (*r - o.at(i))); int64_t xr = (*r == n + 1 ? 0 : (int64_t)(o.at(i) - *l) * (*next(r) - *r)); a += (xl + xr) * (i + 1); s.insert(o.at(i)); } cout << a << endl; }
replace
16
18
16
18
TLE
p02919
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <vector> #define initdp(a, b) \ for (int i = 0; i <= a; i++) \ for (int j = 0; j <= b; j++) \ dp[i][j] = -1; #define fi first #define se second #define pb push_back #define pii pair<int, int> #define ppi pair<pii, int> #define pip pair<int, pii> #define ll long long #define pll pair<ll, ll> #define rep(i, n) for (int i = 0; i < n; i++) #define repd(i, n) for (int i = n - 1; i >= 0; i--) #define inf 1000000001 #define inf1 1000000000000000001 #define mod 1000000007 #define pie 3.14159265358979323846 #define N 100005 #define mid(l, r) l + (r - l) / 2 using namespace std; int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; int ddx[8] = {1, 1, 0, -1, -1, -1, 0, 1}, ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1}; void mad(ll &a, ll b) { a = (a + b) % mod; if (a < 0) a += mod; } ll gcd(ll a, ll b) { if (!a) return b; return gcd(b % a, a); } pii st[2 * N + 1][2]; pii l[N], r[N]; int n; pii merge(pii a, pii b, int c) { vector<int> v; v.pb(a.fi); v.pb(a.se); v.pb(b.fi); v.pb(b.se); sort(v.begin(), v.end()); if (c) return pii(v[0], v[1]); else return pii(v[3], v[2]); } void update(int i, int l, int r, int x, int v, int c) { if (l == r) { st[i][c].fi = v; if (c) st[i][c].se = n + 1; else st[i][c].se = 0; return; } if (mid(l, r) >= x) update(2 * i + 1, l, mid(l, r), x, v, c); else update(2 * i + 2, mid(l, r) + 1, r, x, v, c); // if(c)st[i][c]=min(st[2*i+1][c],st[2*i+2][c]); // else st[i][c]=max(st[2*i+1][c],st[2*i+2][c]); st[i][c] = merge(st[2 * i + 1][c], st[2 * i + 2][c], c); } pii qry(int i, int l, int r, int l1, int r1, int c) { if (r < l1 || r1 < l) { if (c) return pii(n + 1, n + 1); else return pii(0, 0); } if (l1 <= l && r <= r1) return st[i][c]; pii q1 = qry(2 * i + 1, l, mid(l, r), l1, r1, c); pii q2 = qry(2 * i + 2, mid(l, r) + 1, r, l1, r1, c); // cout<<q1<<" "<<q2<<" "<<l<<" "<<r<<"\n"; return merge(q1, q2, c); } void solve() { cin >> n; ll int a[n + 2]; rep(i, n) { cin >> a[i + 1]; } for (int i = n + 1; i; i--) update(0, 0, n + 1, i, n + 1, 1); for (int i = n; i; i--) { r[i] = qry(0, 0, n + 1, a[i] + 1, n + 1, 1); update(0, 0, n + 1, a[i], i, 1); // cout<<r[i]<<" "<<i<<"\n"; } for (int i = 1; i <= n; i++) { l[i] = qry(0, 0, n + 1, a[i] + 1, n + 1, 0); update(0, 0, n + 1, a[i], i, 0); // cout<<l[i]<<" "<<i<<"\n"; } r[n + 1] = pii(n + 1, n + 1); l[0] = pii(0, 0); ll ans = 0; for (ll int i = 1; i <= n; i++) { ll int l1 = l[i].fi; ll int ll1 = l[i].se; ll int r1 = r[i].fi; ll int rr1 = r[i].se; ll cnt = (l1 - ll1) * (r1 - i) + (i - l1) * (rr1 - r1); ans += cnt * a[i]; // cout<<cnt<<" "<<ll1<<" "<<l1<<" "<<i<<" "<<r1<<" "<<rr1<<"\n"; } cout << ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; // cin>>t; t = 1; while (t--) { solve(); } }
#include <algorithm> #include <cmath> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <vector> #define initdp(a, b) \ for (int i = 0; i <= a; i++) \ for (int j = 0; j <= b; j++) \ dp[i][j] = -1; #define fi first #define se second #define pb push_back #define pii pair<int, int> #define ppi pair<pii, int> #define pip pair<int, pii> #define ll long long #define pll pair<ll, ll> #define rep(i, n) for (int i = 0; i < n; i++) #define repd(i, n) for (int i = n - 1; i >= 0; i--) #define inf 1000000001 #define inf1 1000000000000000001 #define mod 1000000007 #define pie 3.14159265358979323846 #define N 100005 #define mid(l, r) l + (r - l) / 2 using namespace std; int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; int ddx[8] = {1, 1, 0, -1, -1, -1, 0, 1}, ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1}; void mad(ll &a, ll b) { a = (a + b) % mod; if (a < 0) a += mod; } ll gcd(ll a, ll b) { if (!a) return b; return gcd(b % a, a); } pii st[4 * N + 1][2]; pii l[N], r[N]; int n; pii merge(pii a, pii b, int c) { vector<int> v; v.pb(a.fi); v.pb(a.se); v.pb(b.fi); v.pb(b.se); sort(v.begin(), v.end()); if (c) return pii(v[0], v[1]); else return pii(v[3], v[2]); } void update(int i, int l, int r, int x, int v, int c) { if (l == r) { st[i][c].fi = v; if (c) st[i][c].se = n + 1; else st[i][c].se = 0; return; } if (mid(l, r) >= x) update(2 * i + 1, l, mid(l, r), x, v, c); else update(2 * i + 2, mid(l, r) + 1, r, x, v, c); // if(c)st[i][c]=min(st[2*i+1][c],st[2*i+2][c]); // else st[i][c]=max(st[2*i+1][c],st[2*i+2][c]); st[i][c] = merge(st[2 * i + 1][c], st[2 * i + 2][c], c); } pii qry(int i, int l, int r, int l1, int r1, int c) { if (r < l1 || r1 < l) { if (c) return pii(n + 1, n + 1); else return pii(0, 0); } if (l1 <= l && r <= r1) return st[i][c]; pii q1 = qry(2 * i + 1, l, mid(l, r), l1, r1, c); pii q2 = qry(2 * i + 2, mid(l, r) + 1, r, l1, r1, c); // cout<<q1<<" "<<q2<<" "<<l<<" "<<r<<"\n"; return merge(q1, q2, c); } void solve() { cin >> n; ll int a[n + 2]; rep(i, n) { cin >> a[i + 1]; } for (int i = n + 1; i; i--) update(0, 0, n + 1, i, n + 1, 1); for (int i = n; i; i--) { r[i] = qry(0, 0, n + 1, a[i] + 1, n + 1, 1); update(0, 0, n + 1, a[i], i, 1); // cout<<r[i]<<" "<<i<<"\n"; } for (int i = 1; i <= n; i++) { l[i] = qry(0, 0, n + 1, a[i] + 1, n + 1, 0); update(0, 0, n + 1, a[i], i, 0); // cout<<l[i]<<" "<<i<<"\n"; } r[n + 1] = pii(n + 1, n + 1); l[0] = pii(0, 0); ll ans = 0; for (ll int i = 1; i <= n; i++) { ll int l1 = l[i].fi; ll int ll1 = l[i].se; ll int r1 = r[i].fi; ll int rr1 = r[i].se; ll cnt = (l1 - ll1) * (r1 - i) + (i - l1) * (rr1 - r1); ans += cnt * a[i]; // cout<<cnt<<" "<<ll1<<" "<<l1<<" "<<i<<" "<<r1<<" "<<rr1<<"\n"; } cout << ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; // cin>>t; t = 1; while (t--) { solve(); } }
replace
42
43
42
43
0
p02919
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define all(x) (x).begin(), (x).end() #define exists(s, x) (s).find(x) != (s).end() #define REP(i, n) for (int i = 0; i < (n); ++i) #define FOR(i, k, n) for (int i = (k); i < (n); ++i) #define fi first #define se second typedef long long ll; const ll INF = 1LL << 60; const ll MOD = 1000000007; inline ll add(ll x, ll y) { return (x + y) % MOD; } inline ll mul(ll x, ll y) { return (x * y) % MOD; } inline ll sub(ll x, ll y) { ll res = x - y; if (res < 0) res += MOD; return res; } inline ll modpow(ll x, ll y) { ll res = 1; while (y) { if (y & 1) res = mul(res, x); y >>= 1; x = mul(x, x); } return res; } int main() { int N; cin >> N; vector<int> P(N); vector<int> IP(N); REP(i, N) { cin >> P[i]; P[i]--; IP[P[i]] = i; } multiset<int> S; S.insert(-1); S.insert(-1); S.insert(N); S.insert(N); ll ans = 0; for (int i = N - 1; i >= 0; --i) { auto it = lower_bound(all(S), IP[i]); int r1 = *it; it++; int r2 = *it; it--; it--; int l1 = *it; it--; int l2 = *it; // cout << l2 << " " << l1 << " " << IP[i] << " " << r1 << " " << r2 << // endl; ll tmp = 0; tmp += (ll)(r1 - IP[i]) * (l1 - l2); tmp += (ll)(r2 - r1) * (IP[i] - l1); ans += tmp * (i + 1); S.insert(IP[i]); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define all(x) (x).begin(), (x).end() #define exists(s, x) (s).find(x) != (s).end() #define REP(i, n) for (int i = 0; i < (n); ++i) #define FOR(i, k, n) for (int i = (k); i < (n); ++i) #define fi first #define se second typedef long long ll; const ll INF = 1LL << 60; const ll MOD = 1000000007; inline ll add(ll x, ll y) { return (x + y) % MOD; } inline ll mul(ll x, ll y) { return (x * y) % MOD; } inline ll sub(ll x, ll y) { ll res = x - y; if (res < 0) res += MOD; return res; } inline ll modpow(ll x, ll y) { ll res = 1; while (y) { if (y & 1) res = mul(res, x); y >>= 1; x = mul(x, x); } return res; } int main() { int N; cin >> N; vector<int> P(N); vector<int> IP(N); REP(i, N) { cin >> P[i]; P[i]--; IP[P[i]] = i; } multiset<int> S; S.insert(-1); S.insert(-1); S.insert(N); S.insert(N); ll ans = 0; for (int i = N - 1; i >= 0; --i) { auto it = S.lower_bound(IP[i]); int r1 = *it; it++; int r2 = *it; it--; it--; int l1 = *it; it--; int l2 = *it; // cout << l2 << " " << l1 << " " << IP[i] << " " << r1 << " " << r2 << // endl; ll tmp = 0; tmp += (ll)(r1 - IP[i]) * (l1 - l2); tmp += (ll)(r2 - r1) * (IP[i] - l1); ans += tmp * (i + 1); S.insert(IP[i]); } cout << ans << endl; return 0; }
replace
48
49
48
49
TLE
p02919
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define int long long #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; const int MOD = 1000000007; const int INF = numeric_limits<int>::max() / 2; typedef pair<int, int> P; signed main() { cin.tie(nullptr); ios::sync_with_stdio(false); int N; cin >> N; P p[N]; rep(i, N) { int a; cin >> a; p[i] = P(a, i + 1); } sort(p, p + N); reverse(p, p + N); multiset<int> vec; vec.insert(0); vec.insert(0); vec.insert(N + 1); vec.insert(N + 1); int ans = 0; for (int i = 0; i < N; i++) { int id = p[i].second; auto itr = lower_bound(vec.begin(), vec.end(), id); int R1 = *itr++; int R2 = *itr--; itr--; int L2 = *itr--; int L1 = *itr; ans += p[i].first * (id - L2) * (R2 - R1); ans += p[i].first * (L2 - L1) * (R1 - id); vec.insert(id); } cout << ans << endl; }
#include <bits/stdc++.h> #define int long long #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; const int MOD = 1000000007; const int INF = numeric_limits<int>::max() / 2; typedef pair<int, int> P; signed main() { cin.tie(nullptr); ios::sync_with_stdio(false); int N; cin >> N; P p[N]; rep(i, N) { int a; cin >> a; p[i] = P(a, i + 1); } sort(p, p + N); reverse(p, p + N); multiset<int> vec; vec.insert(0); vec.insert(0); vec.insert(N + 1); vec.insert(N + 1); int ans = 0; for (int i = 0; i < N; i++) { int id = p[i].second; auto itr = vec.lower_bound(id); int R1 = *itr++; int R2 = *itr--; itr--; int L2 = *itr--; int L1 = *itr; ans += p[i].first * (id - L2) * (R2 - R1); ans += p[i].first * (L2 - L1) * (R1 - id); vec.insert(id); } cout << ans << endl; }
replace
30
31
30
31
TLE
p02919
C++
Runtime Error
#include "bits/stdc++.h" using namespace std; #define ll long long int #define endl '\n' static const int maxn = 2e5 + 5; struct Seg { int fmax, smax; Seg(int fmax = 0, int smax = 0) : fmax(fmax), smax(smax) {} void assignLeaf(int num) { fmax = num; smax = 0; } void marge(const Seg &lchild, const Seg &rchild) { fmax = max(lchild.fmax, rchild.fmax); smax = max({min(lchild.fmax, rchild.fmax), lchild.smax, rchild.smax}); } } Tree[maxn * 4]; void build(int node, int a, int b) { if (a == b) return void(Tree[node] = Seg()); int lchild = node * 2; int rchild = lchild + 1; int mid = (a + b) / 2; build(lchild, a, mid); build(rchild, mid + 1, b); Tree[node].marge(Tree[lchild], Tree[rchild]); } void update(int node, int a, int b, int pos, int val) { if (a > pos or b < pos) return; if (a >= pos and b <= pos) return void(Tree[node].assignLeaf(val)); int lchild = node * 2; int rchild = lchild + 1; int mid = (a + b) / 2; update(lchild, a, mid, pos, val); update(rchild, mid + 1, b, pos, val); Tree[node].marge(Tree[lchild], Tree[rchild]); } Seg query(int node, int a, int b, int i, int j) { if (a > j or b < i) return Seg(); if (a >= i and b <= j) return Tree[node]; int lchild = node * 2; int rchild = lchild + 1; int mid = (a + b) / 2; Seg p = query(lchild, a, mid, i, j); Seg q = query(rchild, mid + 1, b, i, j); Seg res; res.marge(p, q); return res; } ll ans; void solve(vector<int> &vec) { int n = vec.size() - 1; vector<Seg> LEFT(n + 1); build(1, 1, n); for (int i = 1; i <= n; i++) { Seg pos = query(1, 1, n, vec[i] + 1, n); LEFT[i] = pos; ans += 1LL * vec[i] * (LEFT[i].fmax - LEFT[i].smax); // cerr << "LEFT " << i << " " << LEFT[i].fmax << " " << // LEFT[i].smax << " " << 1LL * vec[i] * (LEFT[i].fmax - // LEFT[i].smax) << endl; update(1, 1, n, vec[i], i); } reverse(vec.begin() + 1, vec.end()); vector<Seg> RIGHT(n + 1); build(1, 1, n); for (int i = 1; i <= n; i++) { Seg pos = query(1, 1, n, vec[i] + 1, n); RIGHT[i] = pos; ans += 1LL * vec[i] * (RIGHT[i].fmax - RIGHT[i].smax); // cerr << "RIGHT " << i << " " << RIGHT[i].fmax << " " << // RIGHT[i].smax << " " << 1LL * vec[i] * (RIGHT[i].fmax - // RIGHT[i].smax) << endl; int j = n - i + 1; if (j > LEFT[j].fmax) ans += 1LL * vec[i] * (j - LEFT[j].fmax - 1) * (RIGHT[i].fmax - RIGHT[i].smax); if (i > RIGHT[i].fmax) ans += 1LL * vec[i] * (i - RIGHT[i].fmax - 1) * (LEFT[j].fmax - LEFT[j].smax); update(1, 1, n, vec[i], i); } } signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); #endif // ONLINE_JUDGE int n; cin >> n; vector<int> vec(n + 1); for (int i = 1; i <= n; i++) cin >> vec[i]; solve(vec); cout << ans << endl; }
#include "bits/stdc++.h" using namespace std; #define ll long long int #define endl '\n' static const int maxn = 2e5 + 5; struct Seg { int fmax, smax; Seg(int fmax = 0, int smax = 0) : fmax(fmax), smax(smax) {} void assignLeaf(int num) { fmax = num; smax = 0; } void marge(const Seg &lchild, const Seg &rchild) { fmax = max(lchild.fmax, rchild.fmax); smax = max({min(lchild.fmax, rchild.fmax), lchild.smax, rchild.smax}); } } Tree[maxn * 4]; void build(int node, int a, int b) { if (a == b) return void(Tree[node] = Seg()); int lchild = node * 2; int rchild = lchild + 1; int mid = (a + b) / 2; build(lchild, a, mid); build(rchild, mid + 1, b); Tree[node].marge(Tree[lchild], Tree[rchild]); } void update(int node, int a, int b, int pos, int val) { if (a > pos or b < pos) return; if (a >= pos and b <= pos) return void(Tree[node].assignLeaf(val)); int lchild = node * 2; int rchild = lchild + 1; int mid = (a + b) / 2; update(lchild, a, mid, pos, val); update(rchild, mid + 1, b, pos, val); Tree[node].marge(Tree[lchild], Tree[rchild]); } Seg query(int node, int a, int b, int i, int j) { if (a > j or b < i) return Seg(); if (a >= i and b <= j) return Tree[node]; int lchild = node * 2; int rchild = lchild + 1; int mid = (a + b) / 2; Seg p = query(lchild, a, mid, i, j); Seg q = query(rchild, mid + 1, b, i, j); Seg res; res.marge(p, q); return res; } ll ans; void solve(vector<int> &vec) { int n = vec.size() - 1; vector<Seg> LEFT(n + 1); build(1, 1, n); for (int i = 1; i <= n; i++) { Seg pos = query(1, 1, n, vec[i] + 1, n); LEFT[i] = pos; ans += 1LL * vec[i] * (LEFT[i].fmax - LEFT[i].smax); // cerr << "LEFT " << i << " " << LEFT[i].fmax << " " << // LEFT[i].smax << " " << 1LL * vec[i] * (LEFT[i].fmax - // LEFT[i].smax) << endl; update(1, 1, n, vec[i], i); } reverse(vec.begin() + 1, vec.end()); vector<Seg> RIGHT(n + 1); build(1, 1, n); for (int i = 1; i <= n; i++) { Seg pos = query(1, 1, n, vec[i] + 1, n); RIGHT[i] = pos; ans += 1LL * vec[i] * (RIGHT[i].fmax - RIGHT[i].smax); // cerr << "RIGHT " << i << " " << RIGHT[i].fmax << " " << // RIGHT[i].smax << " " << 1LL * vec[i] * (RIGHT[i].fmax - // RIGHT[i].smax) << endl; int j = n - i + 1; if (j > LEFT[j].fmax) ans += 1LL * vec[i] * (j - LEFT[j].fmax - 1) * (RIGHT[i].fmax - RIGHT[i].smax); if (i > RIGHT[i].fmax) ans += 1LL * vec[i] * (i - RIGHT[i].fmax - 1) * (LEFT[j].fmax - LEFT[j].smax); update(1, 1, n, vec[i], i); } } signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<int> vec(n + 1); for (int i = 1; i <= n; i++) cin >> vec[i]; solve(vec); cout << ans << endl; }
delete
102
106
102
102
-6
terminate called after throwing an instance of 'std::length_error' what(): cannot create std::vector larger than max_size()
p02919
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using vi = vector<int>; using vl = vector<ll>; using _loop_int = int; #define REP(i, n) for (_loop_int i = 0; i < (_loop_int)(n); i++) #define FOR(i, a, b) for (_loop_int i = (_loop_int)(a); i < (_loop_int)(b); i++) #define FORR(i, a, b) \ for (_loop_int i = (_loop_int)(b)-1; i >= (_loop_int)(a); i--) #define CHMIN(a, b) (a) = min((a), (b)) #define CHMAX(a, b) (a) = max((a), (b)) #define ALL(v) (v).begin(), (v).end() #define DEBUG(x) cerr << #x << ": " << (x) << endl #define DEBUG_VEC(v) \ cerr << #v << ": "; \ REP(__i, (v).size()) cerr << ((v)[__i]) << ", "; \ cerr << endl const ll MOD = 1000000007ll; int n; int p[125252]; int rev[125252]; const int N = 1 << 17; pii dat[2 * N]; pii merge(pii a, pii b) { int x[4] = {a.first, a.second, b.first, b.second}; sort(x, x + 4); return pii(x[3], x[2]); } pii query(int l, int r, int a, int b, int k) { if (r <= a || b <= l) return pii(-1, -1); if (l <= a && b <= r) return dat[k]; int m = (a + b) / 2; pii x = query(l, r, a, m, 2 * k + 1); pii y = query(l, r, m, b, 2 * k + 2); return merge(x, y); } int main() { scanf("%d", &n); REP(i, n) scanf("%d", p + i); REP(i, n) rev[p[i]] = i; REP(i, n) dat[i + N - 1] = pii(p[i], -1); FORR(i, 0, N - 1) { dat[i] = merge(dat[2 * i + 1], dat[2 * i + 2]); } ll ans = 0; REP(i, n) { int val = p[i]; if (val == n) continue; if (query(0, i + 1, 0, N, 0).first == val) { // left has no max int ok = i + 2, ng = n + 1; while (abs(ok - ng) > 1) { int r = (ok + ng) / 2; pii P = query(i, r, 0, N, 0); (P.first == val || P.second == val ? ok : ng) = r; } int r2 = ok; ok = r2; ng = i + 1; while (abs(ok - ng) > 1) { int r = (ok + ng) / 2; (query(i, r, 0, N, 0).second == val ? ok : ng) = r; } int r1 = ok; r2--; r1--; ans += 1ll * val * (i + 1) * (r2 - r1 + 1); } else if (query(i, n, 0, N, 0).first == val) { // right has no max int ok = i - 1, ng = -1; while (abs(ok - ng) > 1) { int l = (ok + ng) / 2; pii P = query(l, i + 1, 0, N, 0); (P.first == val || P.second == val ? ok : ng) = l; } int l2 = ok; ok = l2; ng = i; while (abs(ok - ng) > 1) { int l = (ok + ng) / 2; (query(l, i + 1, 0, N, 0).second == val ? ok : ng) = l; } int l1 = ok; ans += 1ll * val * (n - i) * (l1 - l2 + 1); } else { int ok = i + 2, ng = n + 1; while (abs(ok - ng) > 1) { int r = (ok + ng) / 2; pii P = query(i, r, 0, N, 0); (P.first == val || P.second == val ? ok : ng) = r; } int r2 = ok; ok = r2; ng = i + 1; while (abs(ok - ng) > 1) { int r = (ok + ng) / 2; (query(i, r, 0, N, 0).second == val ? ok : ng) = r; } int r1 = ok; r2--; r1--; ok = i - 1, ng = -1; while (abs(ok - ng) > 1) { int l = (ok + ng) / 2; pii P = query(l, i + 1, 0, N, 0); (P.first == val || P.second == val ? ok : ng) = l; } int l2 = ok; ok = l2; ng = i; while (abs(ok - ng) > 1) { int l = (ok + ng) / 2; (query(l, i + 1, 0, N, 0).second == val ? ok : ng) = l; } int l1 = ok; ans += 1ll * val * (r2 - r1 + 1) * (i - l1); ans += 1ll * val * (l1 - l2 + 1) * (r1 - i); } } printf("%lld\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using vi = vector<int>; using vl = vector<ll>; using _loop_int = int; #define REP(i, n) for (_loop_int i = 0; i < (_loop_int)(n); i++) #define FOR(i, a, b) for (_loop_int i = (_loop_int)(a); i < (_loop_int)(b); i++) #define FORR(i, a, b) \ for (_loop_int i = (_loop_int)(b)-1; i >= (_loop_int)(a); i--) #define CHMIN(a, b) (a) = min((a), (b)) #define CHMAX(a, b) (a) = max((a), (b)) #define ALL(v) (v).begin(), (v).end() #define DEBUG(x) cerr << #x << ": " << (x) << endl #define DEBUG_VEC(v) \ cerr << #v << ": "; \ REP(__i, (v).size()) cerr << ((v)[__i]) << ", "; \ cerr << endl const ll MOD = 1000000007ll; int n; int p[125252]; int rev[125252]; const int N = 1 << 17; pii dat[2 * N]; pii merge(pii a, pii b) { if (a.first > b.first) { return pii(a.first, max(a.second, b.first)); } else { return pii(b.first, max(a.first, b.second)); } } pii query(int l, int r, int a, int b, int k) { if (r <= a || b <= l) return pii(-1, -1); if (l <= a && b <= r) return dat[k]; int m = (a + b) / 2; pii x = query(l, r, a, m, 2 * k + 1); pii y = query(l, r, m, b, 2 * k + 2); return merge(x, y); } int main() { scanf("%d", &n); REP(i, n) scanf("%d", p + i); REP(i, n) rev[p[i]] = i; REP(i, n) dat[i + N - 1] = pii(p[i], -1); FORR(i, 0, N - 1) { dat[i] = merge(dat[2 * i + 1], dat[2 * i + 2]); } ll ans = 0; REP(i, n) { int val = p[i]; if (val == n) continue; if (query(0, i + 1, 0, N, 0).first == val) { // left has no max int ok = i + 2, ng = n + 1; while (abs(ok - ng) > 1) { int r = (ok + ng) / 2; pii P = query(i, r, 0, N, 0); (P.first == val || P.second == val ? ok : ng) = r; } int r2 = ok; ok = r2; ng = i + 1; while (abs(ok - ng) > 1) { int r = (ok + ng) / 2; (query(i, r, 0, N, 0).second == val ? ok : ng) = r; } int r1 = ok; r2--; r1--; ans += 1ll * val * (i + 1) * (r2 - r1 + 1); } else if (query(i, n, 0, N, 0).first == val) { // right has no max int ok = i - 1, ng = -1; while (abs(ok - ng) > 1) { int l = (ok + ng) / 2; pii P = query(l, i + 1, 0, N, 0); (P.first == val || P.second == val ? ok : ng) = l; } int l2 = ok; ok = l2; ng = i; while (abs(ok - ng) > 1) { int l = (ok + ng) / 2; (query(l, i + 1, 0, N, 0).second == val ? ok : ng) = l; } int l1 = ok; ans += 1ll * val * (n - i) * (l1 - l2 + 1); } else { int ok = i + 2, ng = n + 1; while (abs(ok - ng) > 1) { int r = (ok + ng) / 2; pii P = query(i, r, 0, N, 0); (P.first == val || P.second == val ? ok : ng) = r; } int r2 = ok; ok = r2; ng = i + 1; while (abs(ok - ng) > 1) { int r = (ok + ng) / 2; (query(i, r, 0, N, 0).second == val ? ok : ng) = r; } int r1 = ok; r2--; r1--; ok = i - 1, ng = -1; while (abs(ok - ng) > 1) { int l = (ok + ng) / 2; pii P = query(l, i + 1, 0, N, 0); (P.first == val || P.second == val ? ok : ng) = l; } int l2 = ok; ok = l2; ng = i; while (abs(ok - ng) > 1) { int l = (ok + ng) / 2; (query(l, i + 1, 0, N, 0).second == val ? ok : ng) = l; } int l1 = ok; ans += 1ll * val * (r2 - r1 + 1) * (i - l1); ans += 1ll * val * (l1 - l2 + 1) * (r1 - i); } } printf("%lld\n", ans); return 0; }
replace
36
39
36
41
TLE
p02919
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define fi first #define se second #define rep(i, n) for (int i = 0; i < (n); ++i) #define drep(i, n) for (int i = (n)-1; i >= 0; --i) #define srep(i, s, t) for (int i = s; i < t; ++i) #define rng(a) a.begin(), a.end() #define sz(x) (int)(x).size() #define uni(x) x.erase(unique(rng(x)), x.end()) #define show(x) cout << #x << " = " << x << endl; #define PQ(T) priority_queue<T, v(T), greater<T>> #define newline puts("") #define v(T) vector<T> #define vv(T) v(v(T)) #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) #define EPS (1e-10) #define equals(a, b) (fabs((a) - (b)) < EPS) using namespace std; typedef long long int ll; typedef pair<int, int> P; typedef set<int> S; typedef queue<int> Q; typedef queue<P> QP; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<P> vp; typedef vector<double> vd; typedef pair<double, double> PD; typedef pair<int, P> ed; const ll LINF = 1001002003004005006ll; const int INF = 1001001001; const int MOD = 1000000007; int main() { int N; cin >> N; vi A(N), pos(N); rep(i, N) { cin >> A[i]; A[i]--; pos[A[i]] = i; } multiset<int> s; s.insert(-1); s.insert(-1); s.insert(pos[N - 1]); s.insert(N); s.insert(N); ll ans = 0; drep(k, N - 1) { auto r = lower_bound(rng(s), pos[k]); auto l = prev(r); auto nr = next(r); auto nl = prev(l); ll a = 1ll * (*l - *nl) * (*r - pos[k]); ll b = 1ll * (*nr - *r) * (pos[k] - *l); ans += (a + b) * (k + 1); s.insert(pos[k]); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define fi first #define se second #define rep(i, n) for (int i = 0; i < (n); ++i) #define drep(i, n) for (int i = (n)-1; i >= 0; --i) #define srep(i, s, t) for (int i = s; i < t; ++i) #define rng(a) a.begin(), a.end() #define sz(x) (int)(x).size() #define uni(x) x.erase(unique(rng(x)), x.end()) #define show(x) cout << #x << " = " << x << endl; #define PQ(T) priority_queue<T, v(T), greater<T>> #define newline puts("") #define v(T) vector<T> #define vv(T) v(v(T)) #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) #define EPS (1e-10) #define equals(a, b) (fabs((a) - (b)) < EPS) using namespace std; typedef long long int ll; typedef pair<int, int> P; typedef set<int> S; typedef queue<int> Q; typedef queue<P> QP; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<P> vp; typedef vector<double> vd; typedef pair<double, double> PD; typedef pair<int, P> ed; const ll LINF = 1001002003004005006ll; const int INF = 1001001001; const int MOD = 1000000007; int main() { int N; cin >> N; vi A(N), pos(N); rep(i, N) { cin >> A[i]; A[i]--; pos[A[i]] = i; } multiset<int> s; s.insert(-1); s.insert(-1); s.insert(pos[N - 1]); s.insert(N); s.insert(N); ll ans = 0; drep(k, N - 1) { auto r = s.lower_bound(pos[k]); auto l = prev(r); auto nr = next(r); auto nl = prev(l); ll a = 1ll * (*l - *nl) * (*r - pos[k]); ll b = 1ll * (*nr - *r) * (pos[k] - *l); ans += (a + b) * (k + 1); s.insert(pos[k]); } cout << ans << endl; return 0; }
replace
52
53
52
53
TLE
p02919
C++
Time Limit Exceeded
#include <algorithm> #include <bits/stdc++.h> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define rep(X, S, E) for (int(X) = (S); (X) < (E); ++(X)) #define rrep(X, S, E) for (int(X) = (E)-1; (X) >= (S); --(X)) #define itrep(X, Y) for (auto(X) = (Y).begin(); (X) != (Y).end(); (X)++) #define all(X) (X).begin(), (X).end() #define sortDecending(X) sort(all(X), greater<ll>()) // 降順 #define sortAscending(X) sort(all(X)) // 昇順 #define pb push_back #define mp make_pair #define fi first #define sc second #define print(x) cout << x << endl #define printDouble(x) cout << fixed << setprecision(13) << x << endl #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<bool> vb; typedef priority_queue<ll, vl> decendingQueue; // 降順 typedef priority_queue<ll, vl, greater<ll>> ascendingQueue; // 昇順 const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; const ll INF = 9 * 1e18; const ll MOD = 1e9 + 7; long long gcd(long long m, long long n) { if (m < n) return gcd(n, m); if (n == 0) return m; return gcd(n, m % n); } long long lcm(long long m, long long n) { // m * nでlong型のオーバフローを発生させないため、先に割り算から行う return m * (n / gcd(m, n)); } // a + b + cをprimeで割った値を返す long long addMod(long long a, long long b) { return (a + b) % MOD; } long long minusMod(long long a, long long b) { return (a + MOD - b) % MOD; } long long multipleMod(long long a, long long b) { return (a * b) % MOD; } vector<long long> SieveOfEratosthenes(int max) { vector<long long> sieve; vector<long long> primes; for (int i = 1; i < max + 1; ++i) { sieve.push_back(i); } sieve[0] = 0; for (int i = 2; i < max + 1; ++i) { if (sieve[i - 1] != 0) { primes.push_back(sieve[i - 1]); for (int j = 2 * sieve[i - 1]; j < max + 1; j += sieve[i - 1]) { sieve[j - 1] = 0; } } } return primes; } class Combination { private: vector<long long> fac_; vector<long long> finv_; vector<long long> inv_; long long prime_; public: Combination(long long n, long long prime) { fac_ = vector<long long>(n + 1); finv_ = vector<long long>(n + 1); inv_ = vector<long long>(n + 1); prime_ = prime; fac_[0] = fac_[1] = 1; finv_[0] = finv_[1] = 1; inv_[1] = 1; for (long long i = 2; i <= n; i++) { fac_[i] = fac_[i - 1] * i % prime_; inv_[i] = prime_ - inv_[prime_ % i] * (prime_ / i) % prime_; finv_[i] = finv_[i - 1] * inv_[i] % prime_; } } // nCk long long getCombination(long long n, long long k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac_[n] * (finv_[k] * finv_[n - k] % prime_) % prime_; } }; class UnionFindTree { private: vector<int> par; vector<int> rnk; vector<int> siz; public: UnionFindTree(int n) { par.assign(n, -1); rnk.assign(n, -1); siz.assign(n, -1); for (int i = 0; i < n; ++i) { par[i] = i; rnk[i] = 0; siz[i] = 1; } } int find(int x) { if (par[x] == x) return x; else return par[x] = find(par[x]); } bool same(int x, int y) { return find(x) == find(y); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (rnk[x] < rnk[y]) { par[x] = y; siz[y] += siz[x]; } else { par[y] = x; siz[x] += siz[y]; if (rnk[x] == rnk[y]) ++rnk[x]; } } int size(int x) { x = find(x); return siz[x]; } }; class Edge { public: ll from; ll to; ll cost; Edge() {} Edge(ll from, ll to, ll cost) { this->from = from; this->to = to; this->cost = cost; } bool operator<(const Edge &edge) const { return cost < edge.cost; // 昇順 } }; class Graph { public: ll nodes; // ノード数 vector<Edge> edges; Graph() {} Graph(ll nodes) { this->nodes = nodes; } void addEdge(ll from, ll to, ll cost) { this->edges.push_back(Edge(from, to, cost)); } }; // クラスカル法 // 連結グラフの最小全域木を求める class Kruskal { private: Graph graph; vector<Edge> MinimumSpanningTree; ll minimumCost; void searchMinimumSpanningTree() { UnionFindTree uf(graph.nodes); sort(all(graph.edges)); itrep(edge, graph.edges) { if (!uf.same(edge->from, edge->to)) { uf.unite(edge->from, edge->to); MinimumSpanningTree.push_back(*edge); } } } public: Kruskal(Graph graph) { this->graph = graph; } ll getMinimumSpanningTreeCost() { searchMinimumSpanningTree(); ll cost = 0; itrep(it, MinimumSpanningTree) { cost += it->cost; } return cost; } }; // ダイクストラ法 O((E+V)logV) // 最小経路問題を解くためのアルゴリズム。辺の重みに負数を含む場合は利用不可 class Dijkstra { private: Graph graph; map<ll, vector<Edge>> fromPaths; vl distances; vl srcs; public: Dijkstra(Graph graph) { this->graph = graph; itrep(edge, graph.edges) { fromPaths[edge->from].push_back(*edge); fromPaths[edge->to].push_back(Edge(edge->to, edge->from, edge->cost)); } } void searchMinimumPathFrom(ll src) { // 複数回呼ばれる度に計算する this->distances = vl(graph.nodes + 1, INF); this->srcs = vl(graph.nodes + 1, INF); priority_queue<ll> pq; distances[src] = 0; srcs[src] = -1; pq.push(src); while (!pq.empty()) { int u = pq.top(); pq.pop(); itrep(edge, fromPaths[u]) { int v = edge->to; int w = edge->cost; if (distances[v] > distances[u] + w) { distances[v] = distances[u] + w; srcs[v] = u; pq.push(v); } } } }; ll getDistance(ll n) { return this->distances[n]; } ll getFrom(ll n) { return this->srcs[n]; } }; // ベルマンフォード O(|V||E|) // 非負コストが含まれていても最短経路問題を解くためのアルゴリズム。閉路の検出も可能 class BellmanFord { private: Graph graph; // 閉路が含まれるかは個々のノードごとに管理する必要あり vector<bool> hasNegativeCycles; vector<ll> distances; public: BellmanFord(Graph graph) { this->graph = graph; this->distances = vector<ll>(this->graph.nodes + 1, INF); this->hasNegativeCycles = vector<bool>(this->graph.nodes + 1, false); } void searchMinimumPathFrom(ll src) { this->distances[src] = 0; for (ll i = 0; i < graph.nodes - 1; i++) { itrep(edge, graph.edges) { ll u = edge->from; ll v = edge->to; ll w = edge->cost; if (this->distances[u] + w < this->distances[v]) { this->distances[v] = this->distances[u] + w; } } } itrep(edge, graph.edges) { ll u = edge->from; ll v = edge->to; ll w = edge->cost; if (this->distances[u] + w < this->distances[v]) { this->hasNegativeCycles[v] = true; } if (this->hasNegativeCycles[u] == true) { this->hasNegativeCycles[v] = true; } } } ll getDistance(ll n) { return this->distances[n]; } bool hasNegativeCycle(ll n) { return this->hasNegativeCycles[n]; } }; void solve(long long N, std::vector<long long> P) { vl index(N + 1); rep(i, 0, N) { index[P[i]] = i; } multiset<ll> pos; pos.insert(-1); pos.insert(-1); pos.insert(N); pos.insert(N); ll ans = 0; rrep(i, 1, N + 1) { auto l3 = upper_bound(all(pos), index[i]); auto l4 = l3; l4++; auto l2 = l3; l2--; auto l1 = l2; l1--; ans += (*l4 - *l3) * (index[i] - *l2) * i; ans += (*l3 - index[i]) * (*l2 - *l1) * i; pos.insert(index[i]); } print(ans); } int main() { long long N; scanf("%lld", &N); std::vector<long long> P(N); for (int i = 0; i < N; i++) { scanf("%lld", &P[i]); } solve(N, std::move(P)); return 0; }
#include <algorithm> #include <bits/stdc++.h> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define rep(X, S, E) for (int(X) = (S); (X) < (E); ++(X)) #define rrep(X, S, E) for (int(X) = (E)-1; (X) >= (S); --(X)) #define itrep(X, Y) for (auto(X) = (Y).begin(); (X) != (Y).end(); (X)++) #define all(X) (X).begin(), (X).end() #define sortDecending(X) sort(all(X), greater<ll>()) // 降順 #define sortAscending(X) sort(all(X)) // 昇順 #define pb push_back #define mp make_pair #define fi first #define sc second #define print(x) cout << x << endl #define printDouble(x) cout << fixed << setprecision(13) << x << endl #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<bool> vb; typedef priority_queue<ll, vl> decendingQueue; // 降順 typedef priority_queue<ll, vl, greater<ll>> ascendingQueue; // 昇順 const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; const ll INF = 9 * 1e18; const ll MOD = 1e9 + 7; long long gcd(long long m, long long n) { if (m < n) return gcd(n, m); if (n == 0) return m; return gcd(n, m % n); } long long lcm(long long m, long long n) { // m * nでlong型のオーバフローを発生させないため、先に割り算から行う return m * (n / gcd(m, n)); } // a + b + cをprimeで割った値を返す long long addMod(long long a, long long b) { return (a + b) % MOD; } long long minusMod(long long a, long long b) { return (a + MOD - b) % MOD; } long long multipleMod(long long a, long long b) { return (a * b) % MOD; } vector<long long> SieveOfEratosthenes(int max) { vector<long long> sieve; vector<long long> primes; for (int i = 1; i < max + 1; ++i) { sieve.push_back(i); } sieve[0] = 0; for (int i = 2; i < max + 1; ++i) { if (sieve[i - 1] != 0) { primes.push_back(sieve[i - 1]); for (int j = 2 * sieve[i - 1]; j < max + 1; j += sieve[i - 1]) { sieve[j - 1] = 0; } } } return primes; } class Combination { private: vector<long long> fac_; vector<long long> finv_; vector<long long> inv_; long long prime_; public: Combination(long long n, long long prime) { fac_ = vector<long long>(n + 1); finv_ = vector<long long>(n + 1); inv_ = vector<long long>(n + 1); prime_ = prime; fac_[0] = fac_[1] = 1; finv_[0] = finv_[1] = 1; inv_[1] = 1; for (long long i = 2; i <= n; i++) { fac_[i] = fac_[i - 1] * i % prime_; inv_[i] = prime_ - inv_[prime_ % i] * (prime_ / i) % prime_; finv_[i] = finv_[i - 1] * inv_[i] % prime_; } } // nCk long long getCombination(long long n, long long k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac_[n] * (finv_[k] * finv_[n - k] % prime_) % prime_; } }; class UnionFindTree { private: vector<int> par; vector<int> rnk; vector<int> siz; public: UnionFindTree(int n) { par.assign(n, -1); rnk.assign(n, -1); siz.assign(n, -1); for (int i = 0; i < n; ++i) { par[i] = i; rnk[i] = 0; siz[i] = 1; } } int find(int x) { if (par[x] == x) return x; else return par[x] = find(par[x]); } bool same(int x, int y) { return find(x) == find(y); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (rnk[x] < rnk[y]) { par[x] = y; siz[y] += siz[x]; } else { par[y] = x; siz[x] += siz[y]; if (rnk[x] == rnk[y]) ++rnk[x]; } } int size(int x) { x = find(x); return siz[x]; } }; class Edge { public: ll from; ll to; ll cost; Edge() {} Edge(ll from, ll to, ll cost) { this->from = from; this->to = to; this->cost = cost; } bool operator<(const Edge &edge) const { return cost < edge.cost; // 昇順 } }; class Graph { public: ll nodes; // ノード数 vector<Edge> edges; Graph() {} Graph(ll nodes) { this->nodes = nodes; } void addEdge(ll from, ll to, ll cost) { this->edges.push_back(Edge(from, to, cost)); } }; // クラスカル法 // 連結グラフの最小全域木を求める class Kruskal { private: Graph graph; vector<Edge> MinimumSpanningTree; ll minimumCost; void searchMinimumSpanningTree() { UnionFindTree uf(graph.nodes); sort(all(graph.edges)); itrep(edge, graph.edges) { if (!uf.same(edge->from, edge->to)) { uf.unite(edge->from, edge->to); MinimumSpanningTree.push_back(*edge); } } } public: Kruskal(Graph graph) { this->graph = graph; } ll getMinimumSpanningTreeCost() { searchMinimumSpanningTree(); ll cost = 0; itrep(it, MinimumSpanningTree) { cost += it->cost; } return cost; } }; // ダイクストラ法 O((E+V)logV) // 最小経路問題を解くためのアルゴリズム。辺の重みに負数を含む場合は利用不可 class Dijkstra { private: Graph graph; map<ll, vector<Edge>> fromPaths; vl distances; vl srcs; public: Dijkstra(Graph graph) { this->graph = graph; itrep(edge, graph.edges) { fromPaths[edge->from].push_back(*edge); fromPaths[edge->to].push_back(Edge(edge->to, edge->from, edge->cost)); } } void searchMinimumPathFrom(ll src) { // 複数回呼ばれる度に計算する this->distances = vl(graph.nodes + 1, INF); this->srcs = vl(graph.nodes + 1, INF); priority_queue<ll> pq; distances[src] = 0; srcs[src] = -1; pq.push(src); while (!pq.empty()) { int u = pq.top(); pq.pop(); itrep(edge, fromPaths[u]) { int v = edge->to; int w = edge->cost; if (distances[v] > distances[u] + w) { distances[v] = distances[u] + w; srcs[v] = u; pq.push(v); } } } }; ll getDistance(ll n) { return this->distances[n]; } ll getFrom(ll n) { return this->srcs[n]; } }; // ベルマンフォード O(|V||E|) // 非負コストが含まれていても最短経路問題を解くためのアルゴリズム。閉路の検出も可能 class BellmanFord { private: Graph graph; // 閉路が含まれるかは個々のノードごとに管理する必要あり vector<bool> hasNegativeCycles; vector<ll> distances; public: BellmanFord(Graph graph) { this->graph = graph; this->distances = vector<ll>(this->graph.nodes + 1, INF); this->hasNegativeCycles = vector<bool>(this->graph.nodes + 1, false); } void searchMinimumPathFrom(ll src) { this->distances[src] = 0; for (ll i = 0; i < graph.nodes - 1; i++) { itrep(edge, graph.edges) { ll u = edge->from; ll v = edge->to; ll w = edge->cost; if (this->distances[u] + w < this->distances[v]) { this->distances[v] = this->distances[u] + w; } } } itrep(edge, graph.edges) { ll u = edge->from; ll v = edge->to; ll w = edge->cost; if (this->distances[u] + w < this->distances[v]) { this->hasNegativeCycles[v] = true; } if (this->hasNegativeCycles[u] == true) { this->hasNegativeCycles[v] = true; } } } ll getDistance(ll n) { return this->distances[n]; } bool hasNegativeCycle(ll n) { return this->hasNegativeCycles[n]; } }; void solve(long long N, std::vector<long long> P) { vl index(N + 1); rep(i, 0, N) { index[P[i]] = i; } multiset<ll> pos; pos.insert(-1); pos.insert(-1); pos.insert(N); pos.insert(N); ll ans = 0; rrep(i, 1, N + 1) { auto l3 = pos.upper_bound(index[i]); auto l4 = l3; l4++; auto l2 = l3; l2--; auto l1 = l2; l1--; ans += (*l4 - *l3) * (index[i] - *l2) * i; ans += (*l3 - index[i]) * (*l2 - *l1) * i; pos.insert(index[i]); } print(ans); } int main() { long long N; scanf("%lld", &N); std::vector<long long> P(N); for (int i = 0; i < N; i++) { scanf("%lld", &P[i]); } solve(N, std::move(P)); return 0; }
replace
333
334
333
334
TLE
p02919
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define rep(i, s, N) for (ll i{s}; i < (N); i++) #define rem(i, N, s) for (ll i{N}; i > (s); i--) #define debug(x) cerr << #x << ": " << x << '\n' #define debugp(i) cerr << "(" << i.first << ", " << i.second << ")\n" #define debugv(v) \ cerr << #v << ": "; \ for (auto &&i : v) \ cerr << i << " "; \ cerr << '\n' #define debugvp(v) \ cerr << #v << ": "; \ for (auto &&i : v) \ cerr << "(" << i.first << ", " << i.second << ") "; \ cerr << '\n' const int MOD = (int)1e9 + 7; const long double pi = 3.141592653589793238462643383279L; using namespace std; using ll = long long; using ld = long double; using str = string; using wstr = wstring; const string rt = "\n", sp = " "; const wstring wrt = L"\n", wsp = L" "; /* GCD */ template <typename M, typename N> constexpr common_type_t<M, N> gcd(M a, N b) { return b != 0 ? gcd(b, a % b) : a; } /* LCM */ template <typename M, typename N> constexpr common_type_t<M, N> lcm(M a, N b) { return a * b / gcd(a, b); } /* UNIONFIND */ template <typename T> struct UnionFind { vector<T> par; UnionFind(T n) : par(n, -1) {} void init(T n) { par.assign(n, -1); } T root(T x) { if (par[x] < 0) return x; else return par[x] = root(par[x]); } bool issame(T x, T y) { return root(x) == root(y); } bool merge(T x, T y) { x = root(x); y = root(y); if (x == y) return false; if (par[x] > par[y]) swap(x, y); par[x] += par[y]; par[y] = x; return true; } int size(int x) { return -par[root(x)]; } }; /* COMB */ auto comb(ll N) { vector<vector<ll>> v(N + 1, vector<ll>(N + 1)); for (unsigned int i = 0; i < v.size(); i++) { v[i][0] = 1; v[i][i] = 1; } for (unsigned int k = 1; k < v.size(); k++) { for (unsigned int j = 1; j < k; j++) { v[k][j] = (v[k - 1][j - 1] + v[k - 1][j]); } } return v; } auto comb(ll N, ll n) { vector<vector<ll>> v(max(N + 1, n + 1), vector<ll>(max(N + 1, n + 1))); for (unsigned int i = 0; i < v.size(); i++) { v[i][0] = 1; v[i][i] = 1; } for (unsigned int k = 1; k < v.size(); k++) { for (unsigned int j = 1; j < k; j++) { v[k][j] = (v[k - 1][j - 1] + v[k - 1][j]); } } return v[N][n]; } /* COMB % MOD */ template <typename T> ll combpm(T N_, T C_) { const int NUM_ = 400001; static ll fact[NUM_ + 1], factr[NUM_ + 1], inv[NUM_ + 1]; if (fact[0] == 0) { inv[1] = fact[0] = factr[0] = 1; for (int i = 2; i <= NUM_; ++i) inv[i] = inv[MOD % i] * (MOD - MOD / i) % MOD; for (int i = 1; i <= NUM_; ++i) fact[i] = fact[i - 1] * i % MOD, factr[i] = factr[i - 1] * inv[i] % MOD; } if (C_ < 0 || C_ > N_) return 0; return factr[C_] * fact[N_] % MOD * factr[N_ - C_] % MOD; } /* MAKE VECTOR */ template <class T> constexpr vector<T> make_vector(size_t a) { return vector<T>(a); } template <class T, class... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector<T>(ts...))>(a, make_vector<T>(ts...)); } /* MAKE DEQUE */ template <class T> constexpr deque<T> make_deque(size_t a) { return deque<T>(a); } template <class T, class... Ts> auto make_deque(size_t a, Ts... ts) { return deque<decltype(make_deque<T>(ts...))>(a, make_deque<T>(ts...)); } /* TEST */ void test(ll n) { cout << "test" << n << endl; } /* PRECISION */ void fixsp(ll n) { cout << fixed << setprecision(n); } void defsp(ll n) { cout << defaultfloat << setprecision(n); } /* WEIGHTENED UNIONFIND */ struct WUnionFind { vector<int> par; vector<int> rank; WUnionFind(int n = 1) { init(n); } void init(int n = 1) { par.resize(n); rank.resize(n); for (int i = 0; i < n; ++i) par[i] = i, rank[i] = 0; } int root(int x) { if (par[x] == x) { return x; } else { int r = root(par[x]); return par[x] = r; } } bool issame(int x, int y) { return root(x) == root(y); } bool merge(int x, int y) { x = root(x); y = root(y); if (x == y) return false; if (rank[x] < rank[y]) swap(x, y); if (rank[x] == rank[y]) ++rank[x]; par[y] = x; return true; } }; /* DIVISOR */ deque<ll> divisor(ll n) { deque<ll> ret; for (ll i = 1; i * i <= n; i++) { if (n % i == 0) { ret.push_back(i); if (i * i != n) ret.push_back(n / i); } } sort(begin(ret), end(ret)); return (ret); } /* MODE */ template <typename T> auto mode(T data) { if (data.size() == 1) return *begin(data); sort(begin(data), end(data)); typename decltype(data)::value_type mode{}; size_t n{}, count{1}; for (auto iter = adjacent_find(begin(data), end(data)), last = end(data), next = end(data); iter != last;) { next = upper_bound(iter, last, *iter); count = distance(iter, next); if (n < count) n = count, mode = *iter; iter = adjacent_find(next, last); } return mode; } /* MEDIAN */ template <typename T> auto median(T data) { sort(begin(data), end(data)); size_t median_index = data.size() / 2; return ( data.size() % 2 == 0 ? static_cast<double>(data[median_index] + data[median_index - 1]) / 2 : data[median_index]); } /* INT POW */ template <typename T> T multi(T a, ll b) { T ans{1}; for (int i{}; i < b; i++) ans *= a; return ans; } /* INF */ template <typename T> constexpr T inf() { return numeric_limits<T>::max(); } /* FASTER IO */ void fastio(ll a) { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cerr.tie(nullptr); if (a) fixsp(a); } /* MIN COST FLOW */ template <typename flow_t, typename cost_t> struct PrimalDual { const cost_t INF; struct edge { int to; flow_t cap; cost_t cost; int rev; bool isrev; }; vector<vector<edge>> graph; vector<cost_t> potential, min_cost; vector<int> prevv, preve; PrimalDual(int V) : graph(V), INF(numeric_limits<cost_t>::max()) {} void add_edge(int from, int to, flow_t cap, cost_t cost) { graph[from].emplace_back( (edge){to, cap, cost, (int)graph[to].size(), false}); graph[to].emplace_back( (edge){from, 0, -cost, (int)graph[from].size() - 1, true}); } cost_t min_cost_flow(int s, int t, flow_t f) { int V = (int)graph.size(); cost_t ret = 0; using Pi = pair<cost_t, int>; priority_queue<Pi, vector<Pi>, greater<Pi>> que; potential.assign(V, 0); preve.assign(V, -1); prevv.assign(V, -1); while (f > 0) { min_cost.assign(V, INF); que.emplace(0, s); min_cost[s] = 0; while (!que.empty()) { Pi p = que.top(); que.pop(); if (min_cost[p.second] < p.first) continue; for (int i = 0; i < graph[p.second].size(); i++) { edge &e = graph[p.second][i]; cost_t nextCost = min_cost[p.second] + e.cost + potential[p.second] - potential[e.to]; if (e.cap > 0 && min_cost[e.to] > nextCost) { min_cost[e.to] = nextCost; prevv[e.to] = p.second, preve[e.to] = i; que.emplace(min_cost[e.to], e.to); } } } if (min_cost[t] == INF) return -1; for (int v = 0; v < V; v++) potential[v] += min_cost[v]; flow_t addflow = f; for (int v = t; v != s; v = prevv[v]) { addflow = min(addflow, graph[prevv[v]][preve[v]].cap); } f -= addflow; ret += addflow * potential[t]; for (int v = t; v != s; v = prevv[v]) { edge &e = graph[prevv[v]][preve[v]]; e.cap -= addflow; graph[v][e.rev].cap += addflow; } } return ret; } void output() { for (int i = 0; i < graph.size(); i++) { for (auto &e : graph[i]) { if (e.isrev) continue; auto &rev_e = graph[e.to][e.rev]; cout << i << "->" << e.to << " (flow: " << rev_e.cap << "/" << rev_e.cap + e.cap << ")" << endl; } } } }; /* BELLMANFORD */ vector<long long> BellmanFord(int s, int V, vector<tuple<long long, long long, long long>> &G) { vector<long long> d(V, inf<long long>()); vector<bool> neg(V); d[s] = 0; for (int i = 0; i < V - 1; ++i) { for (auto &&e : G) { if (d[get<0>(e)] == inf<long long>()) continue; d[get<1>(e)] = min(d[get<1>(e)], d[get<0>(e)] + get<2>(e)); } } for (int i = 0; i < V; ++i) { for (auto &&e : G) { if (d[get<0>(e)] == inf<long long>()) continue; if (d[get<1>(e)] > d[get<0>(e)] + get<2>(e)) { d[get<1>(e)] = d[get<0>(e)] + get<2>(e); neg[get<1>(e)] = 1; } if (neg[get<0>(e)]) neg[get<1>(e)] = 1; } } for (int i = 0; i < V; i++) { if (neg[i]) d[i] = -inf<long long>(); } return d; } /* DIJKSTRA */ vector<long long> Dijkstra(int s, int V, vector<tuple<long long, long long, long long>> &G) { vector<vector<pair<long long, long long>>> uv(V); for (auto &&i : G) uv[get<0>(i)].emplace_back(get<1>(i), get<2>(i)); vector<long long> d(V, inf<long long>() / 2); vector<long long> prev(V, -1); d[s] = 0; priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, greater<pair<long long, long long>>> Q; rep(i, 0, V) Q.emplace(d[i], i); while (!Q.empty()) { long long u = Q.top().second; Q.pop(); for (auto &&v : uv[u]) { long long alt = d[u] + v.second; if (d[v.first] > alt) { d[v.first] = alt; if (d[v.first] > inf<long long>() / 4) d[v.first] = inf<long long>() / 2; else if (d[v.first] < -inf<long long>() / 4) d[v.first] = -inf<long long>() / 2; prev[v.first] = u; Q.emplace(alt, v.first); } } } for (auto &&i : d) { if (abs(i) > inf<long long>() / 4) (i *= 2)++; } return d; } /* BFS */ vector<long long> BFS(long long s, long long V, vector<pair<long long, long long>> &G) { queue<long long> Q; vector<vector<long long>> uv(V); for (auto &&i : G) uv[i.first].emplace_back(i.second); vector<long long> d(V, inf<long long>()); d[s] = 0; Q.emplace(s); while (!Q.empty()) { long long v = Q.front(); Q.pop(); for (auto &&i : uv[v]) { if (d[i] == inf<long long>()) { d[i] = d[v] + 1; Q.emplace(i); } } } return d; } /* WARSHALLFLOYD */ vector<vector<long long>> WarshallFloyd(int V, vector<tuple<long long, long long, long long>> &G) { vector<vector<long long>> d(V, vector<long long>(V, inf<long long>())); for (auto &&i : G) { d[get<0>(i)][get<1>(i)] = get<2>(i); } rep(k, 0, V) rep(i, 0, V) rep(j, 0, V) { if (d[i][k] != inf<ll>() && d[k][j] != inf<ll>()) d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } rep(i, 0, V) { d[i][i] = 0; } return d; } /* NUITA.INC & KYOTO UNIVERSITY */ /* LAST EDITED ON 9.5.2019 */ /* CODE STARTS FROM HERE */ int main() { fastio(0); ll N; cin >> N; auto P = make_deque<ll>(0); rep(i, 0, N) { ll p; cin >> p; P.emplace_back(p); } auto dir = make_deque<ll>(N); rep(i, 0, N) { dir[P[i] - 1] = i; } multiset<ll> se; rep(i, 0, 2) se.insert(-1); rep(i, 0, 2) se.insert(N); ll ans{}; rem(i, N, 0) { se.insert(dir[i - 1]); auto it = lower_bound(all(se), dir[i - 1]); it--; it--; ll l2 = *it; it++; ll l1 = *it; it++; ll c = *it; it++; ll r1 = *it; it++; ll r2 = *it; ans += i * ((l1 - l2) * (r1 - c) + (c - l1) * (r2 - r1)); } cout << ans; }
#include <bits/stdc++.h> #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define rep(i, s, N) for (ll i{s}; i < (N); i++) #define rem(i, N, s) for (ll i{N}; i > (s); i--) #define debug(x) cerr << #x << ": " << x << '\n' #define debugp(i) cerr << "(" << i.first << ", " << i.second << ")\n" #define debugv(v) \ cerr << #v << ": "; \ for (auto &&i : v) \ cerr << i << " "; \ cerr << '\n' #define debugvp(v) \ cerr << #v << ": "; \ for (auto &&i : v) \ cerr << "(" << i.first << ", " << i.second << ") "; \ cerr << '\n' const int MOD = (int)1e9 + 7; const long double pi = 3.141592653589793238462643383279L; using namespace std; using ll = long long; using ld = long double; using str = string; using wstr = wstring; const string rt = "\n", sp = " "; const wstring wrt = L"\n", wsp = L" "; /* GCD */ template <typename M, typename N> constexpr common_type_t<M, N> gcd(M a, N b) { return b != 0 ? gcd(b, a % b) : a; } /* LCM */ template <typename M, typename N> constexpr common_type_t<M, N> lcm(M a, N b) { return a * b / gcd(a, b); } /* UNIONFIND */ template <typename T> struct UnionFind { vector<T> par; UnionFind(T n) : par(n, -1) {} void init(T n) { par.assign(n, -1); } T root(T x) { if (par[x] < 0) return x; else return par[x] = root(par[x]); } bool issame(T x, T y) { return root(x) == root(y); } bool merge(T x, T y) { x = root(x); y = root(y); if (x == y) return false; if (par[x] > par[y]) swap(x, y); par[x] += par[y]; par[y] = x; return true; } int size(int x) { return -par[root(x)]; } }; /* COMB */ auto comb(ll N) { vector<vector<ll>> v(N + 1, vector<ll>(N + 1)); for (unsigned int i = 0; i < v.size(); i++) { v[i][0] = 1; v[i][i] = 1; } for (unsigned int k = 1; k < v.size(); k++) { for (unsigned int j = 1; j < k; j++) { v[k][j] = (v[k - 1][j - 1] + v[k - 1][j]); } } return v; } auto comb(ll N, ll n) { vector<vector<ll>> v(max(N + 1, n + 1), vector<ll>(max(N + 1, n + 1))); for (unsigned int i = 0; i < v.size(); i++) { v[i][0] = 1; v[i][i] = 1; } for (unsigned int k = 1; k < v.size(); k++) { for (unsigned int j = 1; j < k; j++) { v[k][j] = (v[k - 1][j - 1] + v[k - 1][j]); } } return v[N][n]; } /* COMB % MOD */ template <typename T> ll combpm(T N_, T C_) { const int NUM_ = 400001; static ll fact[NUM_ + 1], factr[NUM_ + 1], inv[NUM_ + 1]; if (fact[0] == 0) { inv[1] = fact[0] = factr[0] = 1; for (int i = 2; i <= NUM_; ++i) inv[i] = inv[MOD % i] * (MOD - MOD / i) % MOD; for (int i = 1; i <= NUM_; ++i) fact[i] = fact[i - 1] * i % MOD, factr[i] = factr[i - 1] * inv[i] % MOD; } if (C_ < 0 || C_ > N_) return 0; return factr[C_] * fact[N_] % MOD * factr[N_ - C_] % MOD; } /* MAKE VECTOR */ template <class T> constexpr vector<T> make_vector(size_t a) { return vector<T>(a); } template <class T, class... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector<T>(ts...))>(a, make_vector<T>(ts...)); } /* MAKE DEQUE */ template <class T> constexpr deque<T> make_deque(size_t a) { return deque<T>(a); } template <class T, class... Ts> auto make_deque(size_t a, Ts... ts) { return deque<decltype(make_deque<T>(ts...))>(a, make_deque<T>(ts...)); } /* TEST */ void test(ll n) { cout << "test" << n << endl; } /* PRECISION */ void fixsp(ll n) { cout << fixed << setprecision(n); } void defsp(ll n) { cout << defaultfloat << setprecision(n); } /* WEIGHTENED UNIONFIND */ struct WUnionFind { vector<int> par; vector<int> rank; WUnionFind(int n = 1) { init(n); } void init(int n = 1) { par.resize(n); rank.resize(n); for (int i = 0; i < n; ++i) par[i] = i, rank[i] = 0; } int root(int x) { if (par[x] == x) { return x; } else { int r = root(par[x]); return par[x] = r; } } bool issame(int x, int y) { return root(x) == root(y); } bool merge(int x, int y) { x = root(x); y = root(y); if (x == y) return false; if (rank[x] < rank[y]) swap(x, y); if (rank[x] == rank[y]) ++rank[x]; par[y] = x; return true; } }; /* DIVISOR */ deque<ll> divisor(ll n) { deque<ll> ret; for (ll i = 1; i * i <= n; i++) { if (n % i == 0) { ret.push_back(i); if (i * i != n) ret.push_back(n / i); } } sort(begin(ret), end(ret)); return (ret); } /* MODE */ template <typename T> auto mode(T data) { if (data.size() == 1) return *begin(data); sort(begin(data), end(data)); typename decltype(data)::value_type mode{}; size_t n{}, count{1}; for (auto iter = adjacent_find(begin(data), end(data)), last = end(data), next = end(data); iter != last;) { next = upper_bound(iter, last, *iter); count = distance(iter, next); if (n < count) n = count, mode = *iter; iter = adjacent_find(next, last); } return mode; } /* MEDIAN */ template <typename T> auto median(T data) { sort(begin(data), end(data)); size_t median_index = data.size() / 2; return ( data.size() % 2 == 0 ? static_cast<double>(data[median_index] + data[median_index - 1]) / 2 : data[median_index]); } /* INT POW */ template <typename T> T multi(T a, ll b) { T ans{1}; for (int i{}; i < b; i++) ans *= a; return ans; } /* INF */ template <typename T> constexpr T inf() { return numeric_limits<T>::max(); } /* FASTER IO */ void fastio(ll a) { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cerr.tie(nullptr); if (a) fixsp(a); } /* MIN COST FLOW */ template <typename flow_t, typename cost_t> struct PrimalDual { const cost_t INF; struct edge { int to; flow_t cap; cost_t cost; int rev; bool isrev; }; vector<vector<edge>> graph; vector<cost_t> potential, min_cost; vector<int> prevv, preve; PrimalDual(int V) : graph(V), INF(numeric_limits<cost_t>::max()) {} void add_edge(int from, int to, flow_t cap, cost_t cost) { graph[from].emplace_back( (edge){to, cap, cost, (int)graph[to].size(), false}); graph[to].emplace_back( (edge){from, 0, -cost, (int)graph[from].size() - 1, true}); } cost_t min_cost_flow(int s, int t, flow_t f) { int V = (int)graph.size(); cost_t ret = 0; using Pi = pair<cost_t, int>; priority_queue<Pi, vector<Pi>, greater<Pi>> que; potential.assign(V, 0); preve.assign(V, -1); prevv.assign(V, -1); while (f > 0) { min_cost.assign(V, INF); que.emplace(0, s); min_cost[s] = 0; while (!que.empty()) { Pi p = que.top(); que.pop(); if (min_cost[p.second] < p.first) continue; for (int i = 0; i < graph[p.second].size(); i++) { edge &e = graph[p.second][i]; cost_t nextCost = min_cost[p.second] + e.cost + potential[p.second] - potential[e.to]; if (e.cap > 0 && min_cost[e.to] > nextCost) { min_cost[e.to] = nextCost; prevv[e.to] = p.second, preve[e.to] = i; que.emplace(min_cost[e.to], e.to); } } } if (min_cost[t] == INF) return -1; for (int v = 0; v < V; v++) potential[v] += min_cost[v]; flow_t addflow = f; for (int v = t; v != s; v = prevv[v]) { addflow = min(addflow, graph[prevv[v]][preve[v]].cap); } f -= addflow; ret += addflow * potential[t]; for (int v = t; v != s; v = prevv[v]) { edge &e = graph[prevv[v]][preve[v]]; e.cap -= addflow; graph[v][e.rev].cap += addflow; } } return ret; } void output() { for (int i = 0; i < graph.size(); i++) { for (auto &e : graph[i]) { if (e.isrev) continue; auto &rev_e = graph[e.to][e.rev]; cout << i << "->" << e.to << " (flow: " << rev_e.cap << "/" << rev_e.cap + e.cap << ")" << endl; } } } }; /* BELLMANFORD */ vector<long long> BellmanFord(int s, int V, vector<tuple<long long, long long, long long>> &G) { vector<long long> d(V, inf<long long>()); vector<bool> neg(V); d[s] = 0; for (int i = 0; i < V - 1; ++i) { for (auto &&e : G) { if (d[get<0>(e)] == inf<long long>()) continue; d[get<1>(e)] = min(d[get<1>(e)], d[get<0>(e)] + get<2>(e)); } } for (int i = 0; i < V; ++i) { for (auto &&e : G) { if (d[get<0>(e)] == inf<long long>()) continue; if (d[get<1>(e)] > d[get<0>(e)] + get<2>(e)) { d[get<1>(e)] = d[get<0>(e)] + get<2>(e); neg[get<1>(e)] = 1; } if (neg[get<0>(e)]) neg[get<1>(e)] = 1; } } for (int i = 0; i < V; i++) { if (neg[i]) d[i] = -inf<long long>(); } return d; } /* DIJKSTRA */ vector<long long> Dijkstra(int s, int V, vector<tuple<long long, long long, long long>> &G) { vector<vector<pair<long long, long long>>> uv(V); for (auto &&i : G) uv[get<0>(i)].emplace_back(get<1>(i), get<2>(i)); vector<long long> d(V, inf<long long>() / 2); vector<long long> prev(V, -1); d[s] = 0; priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, greater<pair<long long, long long>>> Q; rep(i, 0, V) Q.emplace(d[i], i); while (!Q.empty()) { long long u = Q.top().second; Q.pop(); for (auto &&v : uv[u]) { long long alt = d[u] + v.second; if (d[v.first] > alt) { d[v.first] = alt; if (d[v.first] > inf<long long>() / 4) d[v.first] = inf<long long>() / 2; else if (d[v.first] < -inf<long long>() / 4) d[v.first] = -inf<long long>() / 2; prev[v.first] = u; Q.emplace(alt, v.first); } } } for (auto &&i : d) { if (abs(i) > inf<long long>() / 4) (i *= 2)++; } return d; } /* BFS */ vector<long long> BFS(long long s, long long V, vector<pair<long long, long long>> &G) { queue<long long> Q; vector<vector<long long>> uv(V); for (auto &&i : G) uv[i.first].emplace_back(i.second); vector<long long> d(V, inf<long long>()); d[s] = 0; Q.emplace(s); while (!Q.empty()) { long long v = Q.front(); Q.pop(); for (auto &&i : uv[v]) { if (d[i] == inf<long long>()) { d[i] = d[v] + 1; Q.emplace(i); } } } return d; } /* WARSHALLFLOYD */ vector<vector<long long>> WarshallFloyd(int V, vector<tuple<long long, long long, long long>> &G) { vector<vector<long long>> d(V, vector<long long>(V, inf<long long>())); for (auto &&i : G) { d[get<0>(i)][get<1>(i)] = get<2>(i); } rep(k, 0, V) rep(i, 0, V) rep(j, 0, V) { if (d[i][k] != inf<ll>() && d[k][j] != inf<ll>()) d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } rep(i, 0, V) { d[i][i] = 0; } return d; } /* NUITA.INC & KYOTO UNIVERSITY */ /* LAST EDITED ON 9.5.2019 */ /* CODE STARTS FROM HERE */ int main() { fastio(0); ll N; cin >> N; auto P = make_deque<ll>(0); rep(i, 0, N) { ll p; cin >> p; P.emplace_back(p); } auto dir = make_deque<ll>(N); rep(i, 0, N) { dir[P[i] - 1] = i; } multiset<ll> se; rep(i, 0, 2) se.insert(-1); rep(i, 0, 2) se.insert(N); ll ans{}; rem(i, N, 0) { se.insert(dir[i - 1]); auto it = se.lower_bound(dir[i - 1]); it--; it--; ll l2 = *it; it++; ll l1 = *it; it++; ll c = *it; it++; ll r1 = *it; it++; ll r2 = *it; ans += i * ((l1 - l2) * (r1 - c) + (c - l1) * (r2 - r1)); } cout << ans; }
replace
440
441
440
441
TLE
p02919
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <string.h> #include <unordered_map> #include <vector> using namespace std; #define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl #define rep(i, n) for (int i = 0; i < n; ++i) #define all(s) s.begin(), s.end() typedef long long ll; typedef unsigned long long ull; typedef pair<ll, ll> P; typedef tuple<ll, ll, ll> triple; typedef double D; const ll INF = 1e9; const ll MOD = 1000000007; // 1e9 + 7 int main(int argc, char **argv) { int N; cin >> N; vector<P> sorted; rep(i, N) { ll p; cin >> p; sorted.emplace_back(p, i); // first is value, second is index } sort(all(sorted)); reverse(all(sorted)); set<ll> st; // Contains the indices of larger values in each step. st.insert(-2); st.insert(-1); // left guard st.insert(N); st.insert(N + 1); // right guard ll ans = 0; for (auto e : sorted) { // Check value and index order by decreasing. ll v = e.first; ll i = e.second; auto it = upper_bound(all(st), i); --it; --it; ll l2 = *(it++); ll l1 = *(it++); ll r1 = *(it++); ll r2 = *it; if (r1 < N) { // r1 must be in [0, N-1] ans += v * (i - l1) * (r2 - r1); } if (l1 >= 0) { // l1 must be in [0, N-1] ans += v * (l1 - l2) * (r1 - i); } st.insert(i); } cout << ans << endl; }
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <string.h> #include <unordered_map> #include <vector> using namespace std; #define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl #define rep(i, n) for (int i = 0; i < n; ++i) #define all(s) s.begin(), s.end() typedef long long ll; typedef unsigned long long ull; typedef pair<ll, ll> P; typedef tuple<ll, ll, ll> triple; typedef double D; const ll INF = 1e9; const ll MOD = 1000000007; // 1e9 + 7 int main(int argc, char **argv) { int N; cin >> N; vector<P> sorted; rep(i, N) { ll p; cin >> p; sorted.emplace_back(p, i); // first is value, second is index } sort(all(sorted)); reverse(all(sorted)); set<ll> st; // Contains the indices of larger values in each step. st.insert(-2); st.insert(-1); // left guard st.insert(N); st.insert(N + 1); // right guard ll ans = 0; for (auto e : sorted) { // Check value and index order by decreasing. ll v = e.first; ll i = e.second; auto it = st.upper_bound(i); --it; --it; ll l2 = *(it++); ll l1 = *(it++); ll r1 = *(it++); ll r2 = *it; if (r1 < N) { // r1 must be in [0, N-1] ans += v * (i - l1) * (r2 - r1); } if (l1 >= 0) { // l1 must be in [0, N-1] ans += v * (l1 - l2) * (r1 - i); } st.insert(i); } cout << ans << endl; }
replace
53
54
53
54
TLE
p02919
C++
Time Limit Exceeded
#include <algorithm> #include <cassert> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <string> #include <vector> using namespace std; typedef long long ll; const int INF = 1 << 30; const ll MOD = 1e9 + 7; const double EPS = 1e-9; int main(int argc, const char *argv[]) { int N; cin >> N; vector<ll> p(N); for (int i = 0; i < N; i++) cin >> p[i]; vector<ll> v(N); iota(v.begin(), v.end(), 0LL); sort(v.begin(), v.end(), [&](int i, int j) { return p[i] > p[j]; }); multiset<ll> st; st.insert(-1); st.insert(-1); st.insert(N); st.insert(N); ll wi, xi, yi, zi, ans; ans = 0; for (auto i : v) { auto itr = lower_bound(st.begin(), st.end(), i); yi = *itr; itr++; zi = *itr; itr--; itr--; xi = *itr; itr--; wi = *itr; ans += p[i] * (xi - wi) * (yi - i); ans += p[i] * (i - xi) * (zi - yi); st.insert(i); } cout << ans << endl; return 0; }
#include <algorithm> #include <cassert> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <string> #include <vector> using namespace std; typedef long long ll; const int INF = 1 << 30; const ll MOD = 1e9 + 7; const double EPS = 1e-9; int main(int argc, const char *argv[]) { int N; cin >> N; vector<ll> p(N); for (int i = 0; i < N; i++) cin >> p[i]; vector<ll> v(N); iota(v.begin(), v.end(), 0LL); sort(v.begin(), v.end(), [&](int i, int j) { return p[i] > p[j]; }); multiset<ll> st; st.insert(-1); st.insert(-1); st.insert(N); st.insert(N); ll wi, xi, yi, zi, ans; ans = 0; for (auto i : v) { auto itr = st.lower_bound(i); yi = *itr; itr++; zi = *itr; itr--; itr--; xi = *itr; itr--; wi = *itr; ans += p[i] * (xi - wi) * (yi - i); ans += p[i] * (i - xi) * (zi - yi); st.insert(i); } cout << ans << endl; return 0; }
replace
36
37
36
37
TLE
p02919
C++
Runtime Error
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <set> #include <sstream> #include <string> #include <tuple> #include <utility> #include <vector> #define ll long long int #define rep(i, x, y) for (int i = x; i < y; i++) #define rel(i, x, y) for (int i = x - 1; i >= y; i--) #define all(x) x.begin(), x.end() using namespace std; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n; cin >> n; vector<int> id(n); rep(i, 0, n) { int tmp; cin >> tmp; id[tmp] = i + 1; // はいれつの中=つよさ,id[i]=順番 } multiset<ll> ms; ms.insert(0); ms.insert(0); ms.insert(n + 1); ms.insert(n + 1); ll ans = 0; rel(i, n + 1, 1) { auto c = ms.lower_bound(id[i]); auto d = c; ++d; auto b = c; --b; auto a = b; --a; ll add = (*b - *a) * (*c - id[i]) + (id[i] - *b) * (*d - *c); ans += add * i; ms.insert(id[i]); } cout << ans << endl; return 0; }
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <set> #include <sstream> #include <string> #include <tuple> #include <utility> #include <vector> #define ll long long int #define rep(i, x, y) for (int i = x; i < y; i++) #define rel(i, x, y) for (int i = x - 1; i >= y; i--) #define all(x) x.begin(), x.end() using namespace std; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n; cin >> n; vector<int> id(n + 1); rep(i, 0, n) { int tmp; cin >> tmp; id[tmp] = i + 1; // はいれつの中=つよさ,id[i]=順番 } multiset<ll> ms; ms.insert(0); ms.insert(0); ms.insert(n + 1); ms.insert(n + 1); ll ans = 0; rel(i, n + 1, 1) { auto c = ms.lower_bound(id[i]); auto d = c; ++d; auto b = c; --b; auto a = b; --a; ll add = (*b - *a) * (*c - id[i]) + (id[i] - *b) * (*d - *c); ans += add * i; ms.insert(id[i]); } cout << ans << endl; return 0; }
replace
21
22
21
22
0
p02919
C++
Runtime Error
#pragma GCC optimize("Ofast") #include <algorithm> #include <bitset> #include <cassert> #include <chrono> #include <complex> #include <cstring> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <unordered_map> #include <unordered_set> using namespace std; signed main() { void solve(); void input(); void init(); int t = 1; // std::cin >> t; while (t--) { init(); input(); solve(); } } int n; int p[1 << 17]; int l[1 << 17]; int r[1 << 17]; void init() {} void input() { std::cin >> n; for (int i = 0; i < n; ++i) cin >> p[i]; } #include <cassert> #include <vector> template <class monoid> class segment_tree { using size_type = typename std::vector<monoid>::size_type; class unique_queue { size_type *que, *begin, *end; bool *in; public: unique_queue() : que(), begin(), end(), in() {} unique_queue(size_type n) : que(new size_type[n]), begin(que), end(que), in(new bool[n]{}) {} ~unique_queue() { delete[] que; delete[] in; } void clear() { begin = end = que; } bool empty() const { return begin == end; } bool push(size_type index) { if (in[index]) return false; return in[*end++ = index] = true; } size_type pop() { return in[*begin] = false, *begin++; } }; // struct unique_queue size_type orig_n, height, ext_n; std::vector<monoid> data; unique_queue que; void recalc(const size_type node) { data[node] = data[node << 1] + data[node << 1 | 1]; } void rebuild() { while (!que.empty()) { const size_type index = que.pop() >> 1; if (index && que.push(index)) recalc(index); } que.clear(); } template <class pred_type> size_type right_search_subtree(size_type index, const pred_type pred, monoid mono) const { assert(index); while (index < ext_n) { const monoid tmp = mono + data[index <<= 1]; if (pred(tmp)) ++index, mono = tmp; } return (index -= ext_n) < orig_n ? index : orig_n; } template <class pred_type> size_type left_search_subtree(size_type index, const pred_type pred, monoid mono) const { assert(index); while (index < ext_n) { const monoid tmp = data[(index <<= 1) | 1] + mono; if (pred(tmp)) mono = tmp; else ++index; } return ++index -= ext_n; } public: segment_tree(const size_type n = 0) : orig_n{n}, height(orig_n > 1 ? 32 - __builtin_clz(orig_n - 1) : 0), ext_n{1u << height}, data(ext_n << 1), que(ext_n << 1) {} segment_tree(const size_type n, const monoid &init) : segment_tree(n) { std::fill(std::next(std::begin(data), ext_n), std::end(data), init); for (size_type i{ext_n}; --i;) recalc(i); } template <class iter_type, class value_type = typename std::iterator_traits< iter_type>::value_type> segment_tree(iter_type first, iter_type last) : orig_n(std::distance(first, last)), height(orig_n > 1 ? 32 - __builtin_clz(orig_n - 1) : 0), ext_n{1u << height}, data(ext_n << 1), que(ext_n << 1) { static_assert(std::is_constructible<monoid, value_type>::value, "monoid(iter_type::value_type) is not constructible."); for (auto iter{std::next(std::begin(data), ext_n)}; iter != std::end(data) && first != last; ++iter, ++first) *iter = monoid{*first}; for (size_type i{ext_n}; --i;) recalc(i); } template <class container_type, typename = typename container_type::value_type> segment_tree(const container_type &cont) : segment_tree(std::begin(cont), std::end(cont)) {} // reference to the element at the index. typename decltype(data)::reference operator[](size_type index) { assert(index < orig_n); que.push(index |= ext_n); return data[index]; } // const reference to the element at the index. typename decltype(data)::const_reference operator[](size_type index) const { assert(index < orig_n); return data[index |= orig_n]; } monoid fold(size_type first, size_type last) { assert(last <= orig_n); rebuild(); monoid leftval{}, rightval{}; first += ext_n, last += ext_n; while (first < last) { if (first & 1) leftval = leftval + data[first++]; if (last & 1) rightval = data[--last] + rightval; first >>= 1, last >>= 1; } return leftval + rightval; } monoid fold() { return fold(0, orig_n); } template <class pred_type> size_type left_search(size_type right, const pred_type pred) { assert(right <= orig_n); rebuild(); right += ext_n; monoid mono{}; for (size_type left{ext_n}; left != right; left >>= 1, right >>= 1) { if ((left & 1) != (right & 1)) { const monoid tmp = data[--right] + mono; if (!pred(tmp)) return left_search_subtree(right, pred, mono); mono = tmp; } } return 0; } template <class pred_type> size_type right_search(size_type left, const pred_type pred) { assert(left < orig_n); rebuild(); left += ext_n; monoid mono{}; for (size_type right{ext_n << 1}; left != right; left >>= 1, right >>= 1) { if ((left & 1) != (right & 1)) { const monoid tmp = mono + data[left]; if (!pred(tmp)) return right_search_subtree(left, pred, mono); mono = tmp; ++left; } } return orig_n; } }; // class segment_tree void solve() { for (int i = 0, t = 0, stk[1 << 17]; i < n; i++) { while (t and p[stk[t - 1]] < p[i]) { t--; } l[i] = t ? stk[t - 1] : -1; stk[t++] = i; } for (int i = n - 1, t = 0, stk[1 << 17]; i >= 0; i--) { while (t and p[stk[t - 1]] < p[i]) { t--; } r[i] = t ? stk[t - 1] : n; stk[t++] = i; } struct mano { int val = 0; mano() {} mano(int v) : val(v) {} mano operator+(mano const &rhs) const { return val > rhs.val ? *this : rhs; } }; segment_tree<mano> seg_max(p, p + n); long long ans = 0; for (int i = 0; i < n; i++) { int lll = ~l[i] ? seg_max.left_search(l[i], [&](mano x) { return x.val <= p[i]; }) - 1 : -1; int rrr = r[i] < n ? seg_max.right_search( r[i] + 1, [&](mano x) { return x.val <= p[i]; }) : n; ans += (long long)p[i] * (l[i] - lll) * (r[i] - i); ans += (long long)p[i] * (rrr - r[i]) * (i - l[i]); } std::cout << ans << "\n"; }
#pragma GCC optimize("Ofast") #include <algorithm> #include <bitset> #include <cassert> #include <chrono> #include <complex> #include <cstring> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <unordered_map> #include <unordered_set> using namespace std; signed main() { void solve(); void input(); void init(); int t = 1; // std::cin >> t; while (t--) { init(); input(); solve(); } } int n; int p[1 << 17]; int l[1 << 17]; int r[1 << 17]; void init() {} void input() { std::cin >> n; for (int i = 0; i < n; ++i) cin >> p[i]; } #include <cassert> #include <vector> template <class monoid> class segment_tree { using size_type = typename std::vector<monoid>::size_type; class unique_queue { size_type *que, *begin, *end; bool *in; public: unique_queue() : que(), begin(), end(), in() {} unique_queue(size_type n) : que(new size_type[n]), begin(que), end(que), in(new bool[n]{}) {} ~unique_queue() { delete[] que; delete[] in; } void clear() { begin = end = que; } bool empty() const { return begin == end; } bool push(size_type index) { if (in[index]) return false; return in[*end++ = index] = true; } size_type pop() { return in[*begin] = false, *begin++; } }; // struct unique_queue size_type orig_n, height, ext_n; std::vector<monoid> data; unique_queue que; void recalc(const size_type node) { data[node] = data[node << 1] + data[node << 1 | 1]; } void rebuild() { while (!que.empty()) { const size_type index = que.pop() >> 1; if (index && que.push(index)) recalc(index); } que.clear(); } template <class pred_type> size_type right_search_subtree(size_type index, const pred_type pred, monoid mono) const { assert(index); while (index < ext_n) { const monoid tmp = mono + data[index <<= 1]; if (pred(tmp)) ++index, mono = tmp; } return (index -= ext_n) < orig_n ? index : orig_n; } template <class pred_type> size_type left_search_subtree(size_type index, const pred_type pred, monoid mono) const { assert(index); while (index < ext_n) { const monoid tmp = data[(index <<= 1) | 1] + mono; if (pred(tmp)) mono = tmp; else ++index; } return ++index -= ext_n; } public: segment_tree(const size_type n = 0) : orig_n{n}, height(orig_n > 1 ? 32 - __builtin_clz(orig_n - 1) : 0), ext_n{1u << height}, data(ext_n << 1), que(ext_n << 1) {} segment_tree(const size_type n, const monoid &init) : segment_tree(n) { std::fill(std::next(std::begin(data), ext_n), std::end(data), init); for (size_type i{ext_n}; --i;) recalc(i); } template <class iter_type, class value_type = typename std::iterator_traits< iter_type>::value_type> segment_tree(iter_type first, iter_type last) : orig_n(std::distance(first, last)), height(orig_n > 1 ? 32 - __builtin_clz(orig_n - 1) : 0), ext_n{1u << height}, data(ext_n << 1), que(ext_n << 1) { static_assert(std::is_constructible<monoid, value_type>::value, "monoid(iter_type::value_type) is not constructible."); for (auto iter{std::next(std::begin(data), ext_n)}; iter != std::end(data) && first != last; ++iter, ++first) *iter = monoid{*first}; for (size_type i{ext_n}; --i;) recalc(i); } template <class container_type, typename = typename container_type::value_type> segment_tree(const container_type &cont) : segment_tree(std::begin(cont), std::end(cont)) {} // reference to the element at the index. typename decltype(data)::reference operator[](size_type index) { assert(index < orig_n); que.push(index |= ext_n); return data[index]; } // const reference to the element at the index. typename decltype(data)::const_reference operator[](size_type index) const { assert(index < orig_n); return data[index |= orig_n]; } monoid fold(size_type first, size_type last) { assert(last <= orig_n); rebuild(); monoid leftval{}, rightval{}; first += ext_n, last += ext_n; while (first < last) { if (first & 1) leftval = leftval + data[first++]; if (last & 1) rightval = data[--last] + rightval; first >>= 1, last >>= 1; } return leftval + rightval; } monoid fold() { return fold(0, orig_n); } template <class pred_type> size_type left_search(size_type right, const pred_type pred) { assert(right <= orig_n); rebuild(); right += ext_n; monoid mono{}; for (size_type left{ext_n}; left != right; left >>= 1, right >>= 1) { if ((left & 1) != (right & 1)) { const monoid tmp = data[--right] + mono; if (!pred(tmp)) return left_search_subtree(right, pred, mono); mono = tmp; } } return 0; } template <class pred_type> size_type right_search(size_type left, const pred_type pred) { assert(left <= orig_n); rebuild(); left += ext_n; monoid mono{}; for (size_type right{ext_n << 1}; left != right; left >>= 1, right >>= 1) { if ((left & 1) != (right & 1)) { const monoid tmp = mono + data[left]; if (!pred(tmp)) return right_search_subtree(left, pred, mono); mono = tmp; ++left; } } return orig_n; } }; // class segment_tree void solve() { for (int i = 0, t = 0, stk[1 << 17]; i < n; i++) { while (t and p[stk[t - 1]] < p[i]) { t--; } l[i] = t ? stk[t - 1] : -1; stk[t++] = i; } for (int i = n - 1, t = 0, stk[1 << 17]; i >= 0; i--) { while (t and p[stk[t - 1]] < p[i]) { t--; } r[i] = t ? stk[t - 1] : n; stk[t++] = i; } struct mano { int val = 0; mano() {} mano(int v) : val(v) {} mano operator+(mano const &rhs) const { return val > rhs.val ? *this : rhs; } }; segment_tree<mano> seg_max(p, p + n); long long ans = 0; for (int i = 0; i < n; i++) { int lll = ~l[i] ? seg_max.left_search(l[i], [&](mano x) { return x.val <= p[i]; }) - 1 : -1; int rrr = r[i] < n ? seg_max.right_search( r[i] + 1, [&](mano x) { return x.val <= p[i]; }) : n; ans += (long long)p[i] * (l[i] - lll) * (r[i] - i); ans += (long long)p[i] * (rrr - r[i]) * (i - l[i]); } std::cout << ans << "\n"; }
replace
202
203
202
203
0
p02919
C++
Time Limit Exceeded
#include <bits/stdc++.h> #include <stdlib.h> using namespace std; typedef long long ll; const ll mod = 1000000007; const ll BIG = 1e15; int main() { int N; cin >> N; vector<int> d(N + 1); for (int i = 1; i <= N; i++) { int x; cin >> x; d.at(x) = i; } ll ans = 0; multiset<int> S; S.insert(N + 1); S.insert(N + 1); S.insert(0); S.insert(0); for (ll i = N; i >= 1; i--) { S.insert(d.at(i)); auto itr = lower_bound(S.begin(), S.end(), d.at(i)); ll m0, l1, l2, r1, r2; itr++; r1 = *itr; itr++; r2 = *itr; itr--; itr--; m0 = *itr; itr--; l2 = *itr; itr--; l1 = *itr; ans += i * ((r2 - r1) * (m0 - l2) + (r1 - m0) * (l2 - l1)); } cout << ans << endl; }
#include <bits/stdc++.h> #include <stdlib.h> using namespace std; typedef long long ll; const ll mod = 1000000007; const ll BIG = 1e15; int main() { int N; cin >> N; vector<int> d(N + 1); for (int i = 1; i <= N; i++) { int x; cin >> x; d.at(x) = i; } ll ans = 0; multiset<int> S; S.insert(N + 1); S.insert(N + 1); S.insert(0); S.insert(0); for (ll i = N; i >= 1; i--) { S.insert(d.at(i)); auto itr = S.find(d.at(i)); ll m0, l1, l2, r1, r2; itr++; r1 = *itr; itr++; r2 = *itr; itr--; itr--; m0 = *itr; itr--; l2 = *itr; itr--; l1 = *itr; ans += i * ((r2 - r1) * (m0 - l2) + (r1 - m0) * (l2 - l1)); } cout << ans << endl; }
replace
25
26
25
26
TLE
p02919
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int M = 1e3 + 5; int seg[M * 4], arr[M], o[M], n; int build(int l, int r, int i) { if (l == r) seg[i] = arr[l]; else seg[i] = max(build(l, (l + r) / 2, i * 2 + 1), build((l + r) / 2 + 1, r, i * 2 + 2)); return seg[i]; } int q(int l, int r, int L, int R, int i) { if (L >= l && R <= r) return seg[i]; if (L > r || R < l) return -1; return max(q(l, r, L, (L + R) / 2, i * 2 + 1), q(l, r, (L + R) / 2 + 1, R, i * 2 + 2)); } int firstGreater(int l, int r, int x) { if (l == r) return (arr[l] > x ? arr[l] : -1); int mid = (l + r) / 2; if (q(l, mid, 0, n + 3, 0) > x) return firstGreater(l, mid, x); return firstGreater(mid + 1, r, x); } int lastGreater(int l, int r, int x) { if (l == r) return (arr[l] > x ? arr[l] : -1); int mid = (l + r) / 2; if (q(mid + 1, r, 0, n + 3, 0) > x) return lastGreater(mid + 1, r, x); return lastGreater(l, mid, x); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; long long ans = 0; arr[0] = n + 1; arr[1] = n + 2; arr[n + 2] = n + 3; arr[n + 3] = n + 4; o[n + 1] = 0; o[n + 2] = 1; o[n + 3] = n + 2; o[n + 4] = n + 3; for (int i = 2; i <= n + 1; i++) cin >> arr[i], o[arr[i]] = i; build(0, n + 3, 0); for (int i = 2; i <= n + 1; i++) { int j, r, l; j = o[firstGreater(i + 1, n + 3, arr[i])]; r = o[firstGreater(j + 1, n + 3, arr[i])]; l = o[lastGreater(0, i - 1, arr[i])]; if (j <= n + 1) ans += 1ll * (r - j) * (i - l) * arr[i]; j = o[lastGreater(0, i - 1, arr[i])]; l = o[lastGreater(0, j - 1, arr[i])]; r = o[firstGreater(i + 1, n + 3, arr[i])]; if (j > 1) ans += 1ll * (r - i) * (j - l) * arr[i]; } cout << ans << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; const int M = 1e5 + 5; int seg[M * 4], arr[M], o[M], n; int build(int l, int r, int i) { if (l == r) seg[i] = arr[l]; else seg[i] = max(build(l, (l + r) / 2, i * 2 + 1), build((l + r) / 2 + 1, r, i * 2 + 2)); return seg[i]; } int q(int l, int r, int L, int R, int i) { if (L >= l && R <= r) return seg[i]; if (L > r || R < l) return -1; return max(q(l, r, L, (L + R) / 2, i * 2 + 1), q(l, r, (L + R) / 2 + 1, R, i * 2 + 2)); } int firstGreater(int l, int r, int x) { if (l == r) return (arr[l] > x ? arr[l] : -1); int mid = (l + r) / 2; if (q(l, mid, 0, n + 3, 0) > x) return firstGreater(l, mid, x); return firstGreater(mid + 1, r, x); } int lastGreater(int l, int r, int x) { if (l == r) return (arr[l] > x ? arr[l] : -1); int mid = (l + r) / 2; if (q(mid + 1, r, 0, n + 3, 0) > x) return lastGreater(mid + 1, r, x); return lastGreater(l, mid, x); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; long long ans = 0; arr[0] = n + 1; arr[1] = n + 2; arr[n + 2] = n + 3; arr[n + 3] = n + 4; o[n + 1] = 0; o[n + 2] = 1; o[n + 3] = n + 2; o[n + 4] = n + 3; for (int i = 2; i <= n + 1; i++) cin >> arr[i], o[arr[i]] = i; build(0, n + 3, 0); for (int i = 2; i <= n + 1; i++) { int j, r, l; j = o[firstGreater(i + 1, n + 3, arr[i])]; r = o[firstGreater(j + 1, n + 3, arr[i])]; l = o[lastGreater(0, i - 1, arr[i])]; if (j <= n + 1) ans += 1ll * (r - j) * (i - l) * arr[i]; j = o[lastGreater(0, i - 1, arr[i])]; l = o[lastGreater(0, j - 1, arr[i])]; r = o[firstGreater(i + 1, n + 3, arr[i])]; if (j > 1) ans += 1ll * (r - i) * (j - l) * arr[i]; } cout << ans << '\n'; return 0; }
replace
4
5
4
5
0
p02919
C++
Runtime Error
#include <algorithm> #include <array> #include <bitset> #include <cmath> #include <complex> #include <cstdio> #include <deque> #include <float.h> #include <functional> #include <iomanip> #include <iostream> #include <limits.h> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <string.h> #include <string> #include <tuple> #include <unordered_map> #include <vector> // #include<assert.h> #include <random> using namespace std; #define int long long #define I32_MAX 2147483647 #define I64_MAX 9223372036854775807LL #define I64_MAX2 1223372036854775807LL #define INF I64_MAX2 // #define MOD 1000000007 #define MOD 998244353 #define MEM_SIZE 100010 #define DEBUG_OUT true #define ALL(x) (x).begin(), (x).end() template <typename T> void DEBUG(T e) { if (DEBUG_OUT == false) return; std::cout << e << " "; } template <typename T> void DEBUG(const std::vector<T> &v) { if (DEBUG_OUT == false) return; for (const auto &e : v) { std::cout << e << " "; } std::cout << std::endl; } template <typename T> void DEBUG(const std::vector<std::vector<T>> &vv) { if (DEBUG_OUT == false) return; for (const auto &v : vv) { DEBUG(v); } } template <class T, class... Ts> void DEBUG(T d, Ts... e) { if (DEBUG_OUT == false) return; DEBUG(d); DEBUG(e...); } template <class T> void corner(bool flg, T hoge) { if (flg) { cout << hoge << endl; abort(); } } template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); } template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); } void solve(void) { int n; cin >> n; vector<int> vec(n, 0); vector<int> pos(n, 0); for (int i = 0; i < n; i++) { cin >> vec[i]; pos[vec[i]] = i; } multiset<int> st = {-1, -1, n, n}; int res = 0; for (int i = n; i > 0; i--) { st.insert(pos[i]); auto itr = st.lower_bound(pos[i]); auto l1 = prev(itr); //(前に小さい数のイテレーター) auto l2 = prev(l1); auto r1 = next(itr); //(次に大きい数のイテレーター) auto r2 = next(r1); res += ((*l1 - *l2) * (*r1 - pos[i]) + (*r2 - *r1) * (pos[i] - *l1)) * vec[pos[i]]; // DEBUG(res,"\n"); } cout << res << endl; return; } int32_t main(int32_t argc, const char *argv[]) { std::ios::sync_with_stdio(false); std::cin.tie(0); std::cout << std::fixed; std::cout << std::setprecision(11); solve(); return 0; }
#include <algorithm> #include <array> #include <bitset> #include <cmath> #include <complex> #include <cstdio> #include <deque> #include <float.h> #include <functional> #include <iomanip> #include <iostream> #include <limits.h> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <string.h> #include <string> #include <tuple> #include <unordered_map> #include <vector> // #include<assert.h> #include <random> using namespace std; #define int long long #define I32_MAX 2147483647 #define I64_MAX 9223372036854775807LL #define I64_MAX2 1223372036854775807LL #define INF I64_MAX2 // #define MOD 1000000007 #define MOD 998244353 #define MEM_SIZE 100010 #define DEBUG_OUT true #define ALL(x) (x).begin(), (x).end() template <typename T> void DEBUG(T e) { if (DEBUG_OUT == false) return; std::cout << e << " "; } template <typename T> void DEBUG(const std::vector<T> &v) { if (DEBUG_OUT == false) return; for (const auto &e : v) { std::cout << e << " "; } std::cout << std::endl; } template <typename T> void DEBUG(const std::vector<std::vector<T>> &vv) { if (DEBUG_OUT == false) return; for (const auto &v : vv) { DEBUG(v); } } template <class T, class... Ts> void DEBUG(T d, Ts... e) { if (DEBUG_OUT == false) return; DEBUG(d); DEBUG(e...); } template <class T> void corner(bool flg, T hoge) { if (flg) { cout << hoge << endl; abort(); } } template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); } template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); } void solve(void) { int n; cin >> n; vector<int> vec(n, 0); vector<int> pos(n + 1, 0); for (int i = 0; i < n; i++) { cin >> vec[i]; pos[vec[i]] = i; } multiset<int> st = {-1, -1, n, n}; int res = 0; for (int i = n; i > 0; i--) { st.insert(pos[i]); auto itr = st.lower_bound(pos[i]); auto l1 = prev(itr); //(前に小さい数のイテレーター) auto l2 = prev(l1); auto r1 = next(itr); //(次に大きい数のイテレーター) auto r2 = next(r1); res += ((*l1 - *l2) * (*r1 - pos[i]) + (*r2 - *r1) * (pos[i] - *l1)) * vec[pos[i]]; // DEBUG(res,"\n"); } cout << res << endl; return; } int32_t main(int32_t argc, const char *argv[]) { std::ios::sync_with_stdio(false); std::cin.tie(0); std::cout << std::fixed; std::cout << std::setprecision(11); solve(); return 0; }
replace
84
85
84
85
-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)
p02919
Python
Runtime Error
import os import sys import numpy as np # Global variables # N is large enough size to store nodes at one time. # children: ndarray(N, 2) current [left / right] child node index # values: ndarray(N) -1 is represents EOT # colors: ndarray(N) 0:black 1:red # counts: ndarray(N) number of nodes in subtree # available: set available indices, set(range(N)) at initial # root: int root index, 0 at initial def solve(inp): RBT_SIZE = 20 RBT_EOT = -2 RBT_AVAILABLE_INDICES = set(range(RBT_SIZE - 1)) RBT_CHILDREN = np.zeros((RBT_SIZE, 2), dtype=np.int64) RBT_VALUES = np.zeros(RBT_SIZE, dtype=np.int64) RBT_COLORS = np.zeros(RBT_SIZE, dtype=np.int64) RBT_COUNTS = np.zeros(RBT_SIZE, dtype=np.int64) RBT_ROOT = -1 RBT_CHILDREN[RBT_ROOT, 0] = RBT_CHILDREN[RBT_ROOT, 1] = RBT_ROOT def _rbt_rotate(i, is_right): """ Rotate node i to right if is_right=1 and left if 0. Return the index of new node that replaces node i. """ r = is_right ci = RBT_CHILDREN[i, r ^ 1] RBT_CHILDREN[i, r ^ 1] = RBT_CHILDREN[ci, r] RBT_CHILDREN[ci, r] = i RBT_COLORS[ci] = RBT_COLORS[i] RBT_COLORS[i] = 1 child_count = RBT_COUNTS[ci] RBT_COUNTS[ci] = RBT_COUNTS[i] RBT_COUNTS[i] -= child_count - RBT_COUNTS[RBT_CHILDREN[i, r ^ 1]] return ci def _rbt_balance_insert(i, is_right): flag = True r = is_right if RBT_COLORS[RBT_CHILDREN[RBT_CHILDREN[i, r], r ^ 1]] == 1: RBT_CHILDREN[i, r] = _rbt_rotate(RBT_CHILDREN[i, r], r) if RBT_COLORS[RBT_CHILDREN[RBT_CHILDREN[i, r], r]] == 1: if RBT_COLORS[RBT_CHILDREN[i, r ^ 1]] == 1: RBT_COLORS[i] = 1 RBT_COLORS[RBT_CHILDREN[i, r]] = RBT_COLORS[RBT_CHILDREN[i, r ^ 1]] = 0 flag = False else: i = _rbt_rotate(i, r ^ 1) return i, flag def _rbt_balance_delete(i, is_right): flag = True stack = [(i, is_right)] while stack: i, r = stack.pop() if ( RBT_COLORS[RBT_CHILDREN[RBT_CHILDREN[i, r ^ 1], r]] == 0 and RBT_COLORS[RBT_CHILDREN[RBT_CHILDREN[i, r ^ 1], r ^ 1]] == 0 ): if RBT_COLORS[RBT_CHILDREN[i, r ^ 1]] == 0: RBT_COLORS[RBT_CHILDREN[i, r ^ 1]] = 1 if RBT_COLORS[i] == 0: flag = False RBT_COLORS[i] = 0 break else: i = _rbt_rotate(i, r) stack.append((i, r)) stack.append((RBT_CHILDREN[i, r], r)) continue else: if RBT_COLORS[RBT_CHILDREN[RBT_CHILDREN[i, r ^ 1], r]] == 1: RBT_CHILDREN[i, r ^ 1] = _rbt_rotate(RBT_CHILDREN[i, r ^ 1], r ^ 1) i = _rbt_rotate(i, r) RBT_COLORS[RBT_CHILDREN[i, r]] = 0 RBT_COLORS[RBT_CHILDREN[i, r ^ 1]] = 0 break while stack: pi, r = stack.pop() RBT_CHILDREN[pi, r] = i i = pi return i, flag def _rbt_get_min_in_subtree(i, stack): while RBT_CHILDREN[i, 0] != -1: stack.append((i, 0)) i = RBT_CHILDREN[i, 0] return i def rbt_insert(root, x): """ Insert value x. Return new root index """ stack = [] i = root while i != -1: to_right = 1 if x >= RBT_VALUES[i] else 0 stack.append((i, to_right)) i = RBT_CHILDREN[i, to_right] for pi, _ in stack: RBT_COUNTS[pi] += 1 i = RBT_AVAILABLE_INDICES.pop() RBT_CHILDREN[i, 0] = -1 RBT_CHILDREN[i, 1] = -1 RBT_VALUES[i] = x RBT_COLORS[i] = 1 RBT_COUNTS[i] = 1 if stack: pi, r = stack[-1] RBT_CHILDREN[pi, r] = i while stack: pi, is_right = stack.pop() if RBT_COLORS[pi] == 1: i = pi continue RBT_CHILDREN[pi, is_right] = i i, flag = _rbt_balance_insert(pi, is_right) if stack and flag: pi, is_right = stack.pop() RBT_CHILDREN[pi, is_right] = i break else: RBT_COLORS[i] = 0 return i return root def rbt_delete(root, x): """ Delete value x. Return new root index """ i = root stack = [] while i != -1 and RBT_VALUES[i] != x: is_right = 1 if RBT_VALUES[i] < x else 0 stack.append((i, is_right)) i = RBT_CHILDREN[i, is_right] if i == -1: return root if RBT_CHILDREN[i, 0] != -1 and RBT_CHILDREN[i, 1] != -1: stack.append((i, 1)) mi = _rbt_get_min_in_subtree(RBT_CHILDREN[i, 1], stack) RBT_COLORS[i] = RBT_COLORS[mi] i = mi for pi, _ in stack: RBT_COUNTS[pi] -= 1 pi, is_right = stack[-1] if RBT_CHILDREN[i, 0] == -1: RBT_CHILDREN[pi, is_right] = RBT_CHILDREN[i, 1] RBT_COLORS[RBT_CHILDREN[i, 1]] = 0 if RBT_CHILDREN[i, 1] != -1 or RBT_COLORS[i] == 1: return root elif RBT_CHILDREN[i, 1] == -1: RBT_CHILDREN[pi, is_right] = RBT_CHILDREN[i, 0] RBT_COLORS[RBT_CHILDREN[i, 0]] = 0 return root while stack: pi, is_right = stack.pop() i, flag = _rbt_balance_delete(pi, is_right) if stack and flag: pi, is_right = stack.pop() RBT_CHILDREN[pi, is_right] = i break else: RBT_COLORS[i] = 0 return i return root def rbt_upper_bound(root, x): i = root y = RBT_EOT c = RBT_COUNTS[i] j = 0 while i != -1: if x < RBT_VALUES[i]: y = RBT_VALUES[i] c = j + RBT_COUNTS[RBT_CHILDREN[i, 0]] i = RBT_CHILDREN[i, 0] else: j += RBT_COUNTS[RBT_CHILDREN[i, 0]] + 1 i = RBT_CHILDREN[i, 1] return y, c def rbt_lower_bound(root, x): i = root y = RBT_EOT c = RBT_COUNTS[i] j = 0 while i != -1: if x <= RBT_VALUES[i]: y = RBT_VALUES[i] c = j + RBT_COUNTS[RBT_CHILDREN[i, 0]] i = RBT_CHILDREN[i, 0] else: j += RBT_COUNTS[RBT_CHILDREN[i, 0]] + 1 i = RBT_CHILDREN[i, 1] return y, c def rbt_get_k_th(root, k): i = root if RBT_COUNTS[i] <= k: return RBT_EOT j = k while i != -1: left_count = RBT_COUNTS[RBT_CHILDREN[i, 0]] if left_count == j: return RBT_VALUES[i] elif left_count > j: i = RBT_CHILDREN[i, 0] else: j -= left_count + 1 i = RBT_CHILDREN[i, 1] return RBT_EOT n = inp[0] ppp = inp[1:] idx = np.argsort(ppp)[::-1] RBT_ROOT = rbt_insert(RBT_ROOT, -1) RBT_ROOT = rbt_insert(RBT_ROOT, -1) RBT_ROOT = rbt_insert(RBT_ROOT, n) RBT_ROOT = rbt_insert(RBT_ROOT, n) ans = 0 for i in idx: p = ppp[i] r1, j = rbt_upper_bound(RBT_ROOT, i) r2 = rbt_get_k_th(RBT_ROOT, j + 1) l1 = rbt_get_k_th(RBT_ROOT, j - 1) l2 = rbt_get_k_th(RBT_ROOT, j - 2) ans += p * ((l1 - l2) * (r1 - i) + (r2 - r1) * (i - l1)) RBT_ROOT = rbt_insert(RBT_ROOT, i) return ans def debug_print(children, values, colors, counts, root): print(children.T) print(values) print(colors) print(counts) _debug_print(children, values, colors, counts, root, 0) def _debug_print(children, values, colors, counts, i, depth): if i != -1: _debug_print(children, values, colors, counts, children[i, 0], depth + 1) print(" " * depth, "BR"[colors[i]], values[i], counts[i]) _debug_print(children, values, colors, counts, children[i, 1], depth + 1) if sys.argv[-1] == "ONLINE_JUDGE": from numba.pycc import CC cc = CC("my_module") cc.export("solve", "(i8[:],)")(solve) cc.compile() exit() if os.name == "posix": # noinspection PyUnresolvedReferences from my_module import solve else: from numba import njit solve = njit("(i8[:],)", cache=True)(solve) print("compiled!", file=sys.stderr) pass inp = np.fromstring(sys.stdin.read(), dtype=np.int64, sep=" ") ans = solve(inp) print(ans)
import os import sys import numpy as np # Global variables # N is large enough size to store nodes at one time. # children: ndarray(N, 2) current [left / right] child node index # values: ndarray(N) -1 is represents EOT # colors: ndarray(N) 0:black 1:red # counts: ndarray(N) number of nodes in subtree # available: set available indices, set(range(N)) at initial # root: int root index, 0 at initial def solve(inp): RBT_SIZE = 100010 RBT_EOT = -2 RBT_AVAILABLE_INDICES = set(range(RBT_SIZE - 1)) RBT_CHILDREN = np.zeros((RBT_SIZE, 2), dtype=np.int64) RBT_VALUES = np.zeros(RBT_SIZE, dtype=np.int64) RBT_COLORS = np.zeros(RBT_SIZE, dtype=np.int64) RBT_COUNTS = np.zeros(RBT_SIZE, dtype=np.int64) RBT_ROOT = -1 RBT_CHILDREN[RBT_ROOT, 0] = RBT_CHILDREN[RBT_ROOT, 1] = RBT_ROOT def _rbt_rotate(i, is_right): """ Rotate node i to right if is_right=1 and left if 0. Return the index of new node that replaces node i. """ r = is_right ci = RBT_CHILDREN[i, r ^ 1] RBT_CHILDREN[i, r ^ 1] = RBT_CHILDREN[ci, r] RBT_CHILDREN[ci, r] = i RBT_COLORS[ci] = RBT_COLORS[i] RBT_COLORS[i] = 1 child_count = RBT_COUNTS[ci] RBT_COUNTS[ci] = RBT_COUNTS[i] RBT_COUNTS[i] -= child_count - RBT_COUNTS[RBT_CHILDREN[i, r ^ 1]] return ci def _rbt_balance_insert(i, is_right): flag = True r = is_right if RBT_COLORS[RBT_CHILDREN[RBT_CHILDREN[i, r], r ^ 1]] == 1: RBT_CHILDREN[i, r] = _rbt_rotate(RBT_CHILDREN[i, r], r) if RBT_COLORS[RBT_CHILDREN[RBT_CHILDREN[i, r], r]] == 1: if RBT_COLORS[RBT_CHILDREN[i, r ^ 1]] == 1: RBT_COLORS[i] = 1 RBT_COLORS[RBT_CHILDREN[i, r]] = RBT_COLORS[RBT_CHILDREN[i, r ^ 1]] = 0 flag = False else: i = _rbt_rotate(i, r ^ 1) return i, flag def _rbt_balance_delete(i, is_right): flag = True stack = [(i, is_right)] while stack: i, r = stack.pop() if ( RBT_COLORS[RBT_CHILDREN[RBT_CHILDREN[i, r ^ 1], r]] == 0 and RBT_COLORS[RBT_CHILDREN[RBT_CHILDREN[i, r ^ 1], r ^ 1]] == 0 ): if RBT_COLORS[RBT_CHILDREN[i, r ^ 1]] == 0: RBT_COLORS[RBT_CHILDREN[i, r ^ 1]] = 1 if RBT_COLORS[i] == 0: flag = False RBT_COLORS[i] = 0 break else: i = _rbt_rotate(i, r) stack.append((i, r)) stack.append((RBT_CHILDREN[i, r], r)) continue else: if RBT_COLORS[RBT_CHILDREN[RBT_CHILDREN[i, r ^ 1], r]] == 1: RBT_CHILDREN[i, r ^ 1] = _rbt_rotate(RBT_CHILDREN[i, r ^ 1], r ^ 1) i = _rbt_rotate(i, r) RBT_COLORS[RBT_CHILDREN[i, r]] = 0 RBT_COLORS[RBT_CHILDREN[i, r ^ 1]] = 0 break while stack: pi, r = stack.pop() RBT_CHILDREN[pi, r] = i i = pi return i, flag def _rbt_get_min_in_subtree(i, stack): while RBT_CHILDREN[i, 0] != -1: stack.append((i, 0)) i = RBT_CHILDREN[i, 0] return i def rbt_insert(root, x): """ Insert value x. Return new root index """ stack = [] i = root while i != -1: to_right = 1 if x >= RBT_VALUES[i] else 0 stack.append((i, to_right)) i = RBT_CHILDREN[i, to_right] for pi, _ in stack: RBT_COUNTS[pi] += 1 i = RBT_AVAILABLE_INDICES.pop() RBT_CHILDREN[i, 0] = -1 RBT_CHILDREN[i, 1] = -1 RBT_VALUES[i] = x RBT_COLORS[i] = 1 RBT_COUNTS[i] = 1 if stack: pi, r = stack[-1] RBT_CHILDREN[pi, r] = i while stack: pi, is_right = stack.pop() if RBT_COLORS[pi] == 1: i = pi continue RBT_CHILDREN[pi, is_right] = i i, flag = _rbt_balance_insert(pi, is_right) if stack and flag: pi, is_right = stack.pop() RBT_CHILDREN[pi, is_right] = i break else: RBT_COLORS[i] = 0 return i return root def rbt_delete(root, x): """ Delete value x. Return new root index """ i = root stack = [] while i != -1 and RBT_VALUES[i] != x: is_right = 1 if RBT_VALUES[i] < x else 0 stack.append((i, is_right)) i = RBT_CHILDREN[i, is_right] if i == -1: return root if RBT_CHILDREN[i, 0] != -1 and RBT_CHILDREN[i, 1] != -1: stack.append((i, 1)) mi = _rbt_get_min_in_subtree(RBT_CHILDREN[i, 1], stack) RBT_COLORS[i] = RBT_COLORS[mi] i = mi for pi, _ in stack: RBT_COUNTS[pi] -= 1 pi, is_right = stack[-1] if RBT_CHILDREN[i, 0] == -1: RBT_CHILDREN[pi, is_right] = RBT_CHILDREN[i, 1] RBT_COLORS[RBT_CHILDREN[i, 1]] = 0 if RBT_CHILDREN[i, 1] != -1 or RBT_COLORS[i] == 1: return root elif RBT_CHILDREN[i, 1] == -1: RBT_CHILDREN[pi, is_right] = RBT_CHILDREN[i, 0] RBT_COLORS[RBT_CHILDREN[i, 0]] = 0 return root while stack: pi, is_right = stack.pop() i, flag = _rbt_balance_delete(pi, is_right) if stack and flag: pi, is_right = stack.pop() RBT_CHILDREN[pi, is_right] = i break else: RBT_COLORS[i] = 0 return i return root def rbt_upper_bound(root, x): i = root y = RBT_EOT c = RBT_COUNTS[i] j = 0 while i != -1: if x < RBT_VALUES[i]: y = RBT_VALUES[i] c = j + RBT_COUNTS[RBT_CHILDREN[i, 0]] i = RBT_CHILDREN[i, 0] else: j += RBT_COUNTS[RBT_CHILDREN[i, 0]] + 1 i = RBT_CHILDREN[i, 1] return y, c def rbt_lower_bound(root, x): i = root y = RBT_EOT c = RBT_COUNTS[i] j = 0 while i != -1: if x <= RBT_VALUES[i]: y = RBT_VALUES[i] c = j + RBT_COUNTS[RBT_CHILDREN[i, 0]] i = RBT_CHILDREN[i, 0] else: j += RBT_COUNTS[RBT_CHILDREN[i, 0]] + 1 i = RBT_CHILDREN[i, 1] return y, c def rbt_get_k_th(root, k): i = root if RBT_COUNTS[i] <= k: return RBT_EOT j = k while i != -1: left_count = RBT_COUNTS[RBT_CHILDREN[i, 0]] if left_count == j: return RBT_VALUES[i] elif left_count > j: i = RBT_CHILDREN[i, 0] else: j -= left_count + 1 i = RBT_CHILDREN[i, 1] return RBT_EOT n = inp[0] ppp = inp[1:] idx = np.argsort(ppp)[::-1] RBT_ROOT = rbt_insert(RBT_ROOT, -1) RBT_ROOT = rbt_insert(RBT_ROOT, -1) RBT_ROOT = rbt_insert(RBT_ROOT, n) RBT_ROOT = rbt_insert(RBT_ROOT, n) ans = 0 for i in idx: p = ppp[i] r1, j = rbt_upper_bound(RBT_ROOT, i) r2 = rbt_get_k_th(RBT_ROOT, j + 1) l1 = rbt_get_k_th(RBT_ROOT, j - 1) l2 = rbt_get_k_th(RBT_ROOT, j - 2) ans += p * ((l1 - l2) * (r1 - i) + (r2 - r1) * (i - l1)) RBT_ROOT = rbt_insert(RBT_ROOT, i) return ans def debug_print(children, values, colors, counts, root): print(children.T) print(values) print(colors) print(counts) _debug_print(children, values, colors, counts, root, 0) def _debug_print(children, values, colors, counts, i, depth): if i != -1: _debug_print(children, values, colors, counts, children[i, 0], depth + 1) print(" " * depth, "BR"[colors[i]], values[i], counts[i]) _debug_print(children, values, colors, counts, children[i, 1], depth + 1) if sys.argv[-1] == "ONLINE_JUDGE": from numba.pycc import CC cc = CC("my_module") cc.export("solve", "(i8[:],)")(solve) cc.compile() exit() if os.name == "posix": # noinspection PyUnresolvedReferences from my_module import solve else: from numba import njit solve = njit("(i8[:],)", cache=True)(solve) print("compiled!", file=sys.stderr) pass inp = np.fromstring(sys.stdin.read(), dtype=np.int64, sep=" ") ans = solve(inp) print(ans)
replace
17
18
17
18
TLE
p02919
C++
Time Limit Exceeded
/*** author: yuji9511 ***/ #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> lpair; const ll MOD = 1e9 + 7; const ll INF = 1e18; #define rep(i, m, n) for (ll i = (m); i < (n); i++) #define rrep(i, m, n) for (ll i = (m); i >= (n); i--) #define print(x) cout << (x) << endl; #define print2(x, y) cout << (x) << " " << (y) << endl; #define printa(x, n) \ for (ll i = 0; i < n; i++) { \ cout << (x[i]) << " \n"[i == n - 1]; \ }; typedef struct { ll lv; ll rv; } Q; int main() { cin.tie(0); ios::sync_with_stdio(false); ll N; scanf("%lld", &N); ll P[100010] = {}; rep(i, 0, N) scanf("%lld", &P[i]); vector<Q> arr(100010); arr[0] = (Q){-1, 1}; arr[1] = (Q){0, 2}; ll pos[100010] = {}; rep(i, 0, N) { arr[i + 2] = (Q){i + 1, i + 3}; rep(i, 0, N) pos[P[i]] = i + 2; } arr[N + 2] = (Q){N + 1, N + 3}; arr[N + 3] = (Q){N + 2, N + 4}; ll ans = 0; rep(n, 1, N) { ll p = pos[n]; ll lv = arr[p].lv; ll rv = arr[p].rv; if (lv != 1) { ans += n * (lv - arr[lv].lv) * (rv - p); } if (rv != N + 2) { ans += n * (arr[rv].rv - rv) * (p - lv); } arr[lv].rv = arr[p].rv; arr[rv].lv = arr[p].lv; } print(ans); }
/*** author: yuji9511 ***/ #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> lpair; const ll MOD = 1e9 + 7; const ll INF = 1e18; #define rep(i, m, n) for (ll i = (m); i < (n); i++) #define rrep(i, m, n) for (ll i = (m); i >= (n); i--) #define print(x) cout << (x) << endl; #define print2(x, y) cout << (x) << " " << (y) << endl; #define printa(x, n) \ for (ll i = 0; i < n; i++) { \ cout << (x[i]) << " \n"[i == n - 1]; \ }; typedef struct { ll lv; ll rv; } Q; int main() { cin.tie(0); ios::sync_with_stdio(false); ll N; scanf("%lld", &N); ll P[100010] = {}; rep(i, 0, N) scanf("%lld", &P[i]); vector<Q> arr(100010); arr[0] = (Q){-1, 1}; arr[1] = (Q){0, 2}; ll pos[100010] = {}; rep(i, 0, N) { arr[i + 2] = (Q){i + 1, i + 3}; pos[P[i]] = i + 2; } arr[N + 2] = (Q){N + 1, N + 3}; arr[N + 3] = (Q){N + 2, N + 4}; ll ans = 0; rep(n, 1, N) { ll p = pos[n]; ll lv = arr[p].lv; ll rv = arr[p].rv; if (lv != 1) { ans += n * (lv - arr[lv].lv) * (rv - p); } if (rv != N + 2) { ans += n * (arr[rv].rv - rv) * (p - lv); } arr[lv].rv = arr[p].rv; arr[rv].lv = arr[p].lv; } print(ans); }
replace
32
33
32
33
TLE
p02919
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int a[100001], ST[(1 << 17) + 1]; long long ans = 0; void build(int l, int r, int p) { if (l == r) { ST[p] = r; return; } int mid = (l + r) / 2; build(l, mid, p * 2); build(mid + 1, r, p * 2 + 1); ST[p] = (a[ST[p * 2]] > a[ST[p * 2 + 1]]) ? ST[p * 2] : ST[p * 2 + 1]; } int query(int l, int r, int p, int i, int j) { if (l > j | r < i) { return -1; } if (l >= i && r <= j) { return ST[p]; } int mid = (l + r) / 2; int p1 = query(l, mid, p * 2, i, j); int p2 = query(mid + 1, r, p * 2 + 1, i, j); if (p1 == -1) return p2; if (p2 == -1) return p1; return (a[p1] > a[p2]) ? p1 : p2; } int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } build(1, n, 1); for (int i = 1; i <= n; i++) { int l = 1, r = i - 1, pre1 = 0, pre2, next1, next2; while (l <= r) { if (l == r) { int pos = query(1, n, 1, l, l); if (a[pos] > a[i]) { pre1 = pos; } break; } int mid = (l + r) / 2; int pos = query(1, n, 1, mid + 1, r); if (a[pos] > a[i]) { pre1 = pos; l = pos + 1; } else { pos = query(1, n, 1, l, mid); if (a[pos] > a[i]) { pre1 = pos; l = pos + 1; } else { break; } } } l = 1, r = pre1 - 1, pre2 = 0; while (l <= r) { if (l == r) { int pos = query(1, n, 1, l, l); if (a[pos] > a[i]) { pre2 = pos; } break; } int mid = (l + r) / 2; int pos = query(1, n, 1, mid + 1, r); if (a[pos] > a[i]) { pre2 = pos; l = pos + 1; } else { pos = query(1, n, 1, l, mid); if (a[pos] > a[i]) { pre2 = pos; l = pos + 1; } else { break; } } } l = i + 1, r = n, next1 = n + 1; while (l <= r) { if (l == r) { int pos = query(1, n, 1, l, l); if (a[pos] > a[i]) { next1 = pos; } break; } int mid = (l + r) / 2; int pos = query(1, n, 1, l, mid); if (a[pos] > a[i]) { next1 = pos; r = pos - 1; } else { pos = query(1, n, 1, mid + 1, r); if (a[pos] > a[i]) { next1 = pos; r = pos - 1; } else { break; } } } l = next1 + 1, r = n, next2 = n + 1; while (l <= r) { if (l == r) { int pos = query(1, n, 1, l, l); if (a[pos] > a[i]) { next2 = pos; } break; } int mid = (l + r) / 2; int pos = query(1, n, 1, l, mid); if (a[pos] > a[i]) { next2 = pos; r = pos - 1; } else { pos = query(1, n, 1, mid + 1, r); if (a[pos] > a[i]) { next2 = pos; r = pos - 1; } else { break; } } } long long cnt = 0; cnt += (long long)((pre1 - pre2) * (next1 - i)); cnt += (long long)((next2 - next1) * (i - pre1)); ans += (long long)(cnt * a[i]); // cout<<pre1<<" "<<pre2<<" "<<next1<<" "<<next2<<endl; } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int a[100001], ST[(1 << 18)]; long long ans = 0; void build(int l, int r, int p) { if (l == r) { ST[p] = r; return; } int mid = (l + r) / 2; build(l, mid, p * 2); build(mid + 1, r, p * 2 + 1); ST[p] = (a[ST[p * 2]] > a[ST[p * 2 + 1]]) ? ST[p * 2] : ST[p * 2 + 1]; } int query(int l, int r, int p, int i, int j) { if (l > j | r < i) { return -1; } if (l >= i && r <= j) { return ST[p]; } int mid = (l + r) / 2; int p1 = query(l, mid, p * 2, i, j); int p2 = query(mid + 1, r, p * 2 + 1, i, j); if (p1 == -1) return p2; if (p2 == -1) return p1; return (a[p1] > a[p2]) ? p1 : p2; } int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } build(1, n, 1); for (int i = 1; i <= n; i++) { int l = 1, r = i - 1, pre1 = 0, pre2, next1, next2; while (l <= r) { if (l == r) { int pos = query(1, n, 1, l, l); if (a[pos] > a[i]) { pre1 = pos; } break; } int mid = (l + r) / 2; int pos = query(1, n, 1, mid + 1, r); if (a[pos] > a[i]) { pre1 = pos; l = pos + 1; } else { pos = query(1, n, 1, l, mid); if (a[pos] > a[i]) { pre1 = pos; l = pos + 1; } else { break; } } } l = 1, r = pre1 - 1, pre2 = 0; while (l <= r) { if (l == r) { int pos = query(1, n, 1, l, l); if (a[pos] > a[i]) { pre2 = pos; } break; } int mid = (l + r) / 2; int pos = query(1, n, 1, mid + 1, r); if (a[pos] > a[i]) { pre2 = pos; l = pos + 1; } else { pos = query(1, n, 1, l, mid); if (a[pos] > a[i]) { pre2 = pos; l = pos + 1; } else { break; } } } l = i + 1, r = n, next1 = n + 1; while (l <= r) { if (l == r) { int pos = query(1, n, 1, l, l); if (a[pos] > a[i]) { next1 = pos; } break; } int mid = (l + r) / 2; int pos = query(1, n, 1, l, mid); if (a[pos] > a[i]) { next1 = pos; r = pos - 1; } else { pos = query(1, n, 1, mid + 1, r); if (a[pos] > a[i]) { next1 = pos; r = pos - 1; } else { break; } } } l = next1 + 1, r = n, next2 = n + 1; while (l <= r) { if (l == r) { int pos = query(1, n, 1, l, l); if (a[pos] > a[i]) { next2 = pos; } break; } int mid = (l + r) / 2; int pos = query(1, n, 1, l, mid); if (a[pos] > a[i]) { next2 = pos; r = pos - 1; } else { pos = query(1, n, 1, mid + 1, r); if (a[pos] > a[i]) { next2 = pos; r = pos - 1; } else { break; } } } long long cnt = 0; cnt += (long long)((pre1 - pre2) * (next1 - i)); cnt += (long long)((next2 - next1) * (i - pre1)); ans += (long long)(cnt * a[i]); // cout<<pre1<<" "<<pre2<<" "<<next1<<" "<<next2<<endl; } cout << ans << endl; }
replace
2
3
2
3
0
p02919
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int a[100001], ST[(1 << 17)]; long long ans = 0; void build(int l, int r, int p) { if (l == r) { ST[p] = r; return; } int mid = (l + r) / 2; build(l, mid, p * 2); build(mid + 1, r, p * 2 + 1); ST[p] = (a[ST[p * 2]] > a[ST[p * 2 + 1]]) ? ST[p * 2] : ST[p * 2 + 1]; } int query(int l, int r, int p, int i, int j) { if (l > j | r < i) { return -1; } if (l >= i && r <= j) { return ST[p]; } int mid = (l + r) / 2; int p1 = query(l, mid, p * 2, i, j); int p2 = query(mid + 1, r, p * 2 + 1, i, j); if (p1 == -1) return p2; if (p2 == -1) return p1; return (a[p1] > a[p2]) ? p1 : p2; } int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } build(1, n, 1); for (int i = 1; i <= n; i++) { int l = 1, r = i - 1, pre1 = 0, pre2, next1, next2; while (l <= r) { if (l == r) { int pos = query(1, n, 1, l, l); if (a[pos] > a[i]) { pre1 = pos; } break; } int mid = (l + r) / 2; int pos = query(1, n, 1, mid + 1, r); if (a[pos] > a[i]) { pre1 = pos; l = pos + 1; } else { pos = query(1, n, 1, l, mid); if (a[pos] > a[i]) { pre1 = pos; l = pos + 1; } else { break; } } } l = 1, r = pre1 - 1, pre2 = 0; while (l <= r) { if (l == r) { int pos = query(1, n, 1, l, l); if (a[pos] > a[i]) { pre2 = pos; } break; } int mid = (l + r) / 2; int pos = query(1, n, 1, mid + 1, r); if (a[pos] > a[i]) { pre2 = pos; l = pos + 1; } else { pos = query(1, n, 1, l, mid); if (a[pos] > a[i]) { pre2 = pos; l = pos + 1; } else { break; } } } l = i + 1, r = n, next1 = n + 1; while (l <= r) { if (l == r) { int pos = query(1, n, 1, l, l); if (a[pos] > a[i]) { next1 = pos; } break; } int mid = (l + r) / 2; int pos = query(1, n, 1, l, mid); if (a[pos] > a[i]) { next1 = pos; r = pos - 1; } else { pos = query(1, n, 1, mid + 1, r); if (a[pos] > a[i]) { next1 = pos; r = pos - 1; } else { break; } } } l = next1 + 1, r = n, next2 = n + 1; while (l <= r) { if (l == r) { int pos = query(1, n, 1, l, l); if (a[pos] > a[i]) { next2 = pos; } break; } int mid = (l + r) / 2; int pos = query(1, n, 1, l, mid); if (a[pos] > a[i]) { next2 = pos; r = pos - 1; } else { pos = query(1, n, 1, mid + 1, r); if (a[pos] > a[i]) { next2 = pos; r = pos - 1; } else { break; } } } long long cnt = 0; cnt += (long long)((pre1 - pre2) * (next1 - i)); cnt += (long long)((next2 - next1) * (i - pre1)); ans += (long long)(cnt * a[i]); // cout<<pre1<<" "<<pre2<<" "<<next1<<" "<<next2<<endl; } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int a[100001], ST[(1 << 18)]; long long ans = 0; void build(int l, int r, int p) { if (l == r) { ST[p] = r; return; } int mid = (l + r) / 2; build(l, mid, p * 2); build(mid + 1, r, p * 2 + 1); ST[p] = (a[ST[p * 2]] > a[ST[p * 2 + 1]]) ? ST[p * 2] : ST[p * 2 + 1]; } int query(int l, int r, int p, int i, int j) { if (l > j | r < i) { return -1; } if (l >= i && r <= j) { return ST[p]; } int mid = (l + r) / 2; int p1 = query(l, mid, p * 2, i, j); int p2 = query(mid + 1, r, p * 2 + 1, i, j); if (p1 == -1) return p2; if (p2 == -1) return p1; return (a[p1] > a[p2]) ? p1 : p2; } int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } build(1, n, 1); for (int i = 1; i <= n; i++) { int l = 1, r = i - 1, pre1 = 0, pre2, next1, next2; while (l <= r) { if (l == r) { int pos = query(1, n, 1, l, l); if (a[pos] > a[i]) { pre1 = pos; } break; } int mid = (l + r) / 2; int pos = query(1, n, 1, mid + 1, r); if (a[pos] > a[i]) { pre1 = pos; l = pos + 1; } else { pos = query(1, n, 1, l, mid); if (a[pos] > a[i]) { pre1 = pos; l = pos + 1; } else { break; } } } l = 1, r = pre1 - 1, pre2 = 0; while (l <= r) { if (l == r) { int pos = query(1, n, 1, l, l); if (a[pos] > a[i]) { pre2 = pos; } break; } int mid = (l + r) / 2; int pos = query(1, n, 1, mid + 1, r); if (a[pos] > a[i]) { pre2 = pos; l = pos + 1; } else { pos = query(1, n, 1, l, mid); if (a[pos] > a[i]) { pre2 = pos; l = pos + 1; } else { break; } } } l = i + 1, r = n, next1 = n + 1; while (l <= r) { if (l == r) { int pos = query(1, n, 1, l, l); if (a[pos] > a[i]) { next1 = pos; } break; } int mid = (l + r) / 2; int pos = query(1, n, 1, l, mid); if (a[pos] > a[i]) { next1 = pos; r = pos - 1; } else { pos = query(1, n, 1, mid + 1, r); if (a[pos] > a[i]) { next1 = pos; r = pos - 1; } else { break; } } } l = next1 + 1, r = n, next2 = n + 1; while (l <= r) { if (l == r) { int pos = query(1, n, 1, l, l); if (a[pos] > a[i]) { next2 = pos; } break; } int mid = (l + r) / 2; int pos = query(1, n, 1, l, mid); if (a[pos] > a[i]) { next2 = pos; r = pos - 1; } else { pos = query(1, n, 1, mid + 1, r); if (a[pos] > a[i]) { next2 = pos; r = pos - 1; } else { break; } } } long long cnt = 0; cnt += (long long)((pre1 - pre2) * (next1 - i)); cnt += (long long)((next2 - next1) * (i - pre1)); ans += (long long)(cnt * a[i]); // cout<<pre1<<" "<<pre2<<" "<<next1<<" "<<next2<<endl; } cout << ans << endl; }
replace
2
3
2
3
0
p02919
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> ii; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, t; cin >> n; vector<ii> ar; for (int ctr1 = 0; ctr1 < n; ++ctr1) cin >> t, ar.push_back(make_pair(t, ctr1)); sort(ar.begin(), ar.end()); reverse(ar.begin(), ar.end()); set<int> larger, smaller; ll rez = 0; for (auto nxt : ar) { auto up = upper_bound(larger.begin(), larger.end(), nxt.second); auto down = upper_bound(smaller.begin(), smaller.end(), -nxt.second); if (up != larger.end()) { auto it = up; ++it; int up_two = (it == larger.end() ? n : *it); int down_two = -(down == smaller.end() ? 1 : *down); rez += 1LL * nxt.first * (up_two - *up) * (nxt.second - down_two); } if (down != smaller.end()) { auto it = down; ++it; int down_two = (it == smaller.end() ? 1 : *it); int up_two = (up == larger.end() ? n : *up); rez += 1LL * nxt.first * (up_two - nxt.second) * (down_two - *down); } larger.insert(nxt.second); smaller.insert(-nxt.second); } cout << rez << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> ii; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, t; cin >> n; vector<ii> ar; for (int ctr1 = 0; ctr1 < n; ++ctr1) cin >> t, ar.push_back(make_pair(t, ctr1)); sort(ar.begin(), ar.end()); reverse(ar.begin(), ar.end()); set<int> larger, smaller; ll rez = 0; for (auto nxt : ar) { auto up = larger.upper_bound(nxt.second); auto down = smaller.upper_bound(-nxt.second); if (up != larger.end()) { auto it = up; ++it; int up_two = (it == larger.end() ? n : *it); int down_two = -(down == smaller.end() ? 1 : *down); rez += 1LL * nxt.first * (up_two - *up) * (nxt.second - down_two); } if (down != smaller.end()) { auto it = down; ++it; int down_two = (it == smaller.end() ? 1 : *it); int up_two = (up == larger.end() ? n : *up); rez += 1LL * nxt.first * (up_two - nxt.second) * (down_two - *down); } larger.insert(nxt.second); smaller.insert(-nxt.second); } cout << rez << "\n"; return 0; }
replace
22
24
22
24
TLE
p02919
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define INCANT \ cin.tie(0), cout.tie(0), ios::sync_with_stdio(false), \ cout << fixed << setprecision(20); #define int long long #define gcd __gcd #define all(x) (x).begin(), (x).end() template <class T> bool chmax(T &a, T b) { return (a = max(a, b)) == b; } template <class T> bool chmin(T &a, T b) { return (a = min(a, b)) == b; } #define _overload(_1, _2, _3, name, ...) name #define _rep(i, n) repi(i, 0, n) #define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define rep(...) _overload(__VA_ARGS__, repi, _rep)(__VA_ARGS__) #define _rev(i, n) revi(i, n, 0) #define revi(i, a, b) for (int i = (int)(a - 1); i >= (int)(b); i--) #define rev(...) _overload(__VA_ARGS__, revi, _rev)(__VA_ARGS__) #define each(i, n) for (auto &&i : n) const int INF = 1e18, MOD = 1e9 + 7; signed main() { INCANT; int n, p, l2, l1, r1, r2, res = 0; cin >> n; int index[111111]; rep(i, n) { cin >> p; index[p] = i; } multiset<int> s; s.insert(-1); s.insert(-1); s.insert(n); s.insert(n); rev(i, n + 1, 1) { auto now = lower_bound(all(s), index[i]); r1 = *now; r2 = *++now; l1 = *-- --now; l2 = *--now; res += ((l1 - l2) * (r1 - index[i]) + (r2 - r1) * (index[i] - l1)) * i; s.insert(index[i]); } cout << res << endl; }
#include <bits/stdc++.h> using namespace std; #define INCANT \ cin.tie(0), cout.tie(0), ios::sync_with_stdio(false), \ cout << fixed << setprecision(20); #define int long long #define gcd __gcd #define all(x) (x).begin(), (x).end() template <class T> bool chmax(T &a, T b) { return (a = max(a, b)) == b; } template <class T> bool chmin(T &a, T b) { return (a = min(a, b)) == b; } #define _overload(_1, _2, _3, name, ...) name #define _rep(i, n) repi(i, 0, n) #define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define rep(...) _overload(__VA_ARGS__, repi, _rep)(__VA_ARGS__) #define _rev(i, n) revi(i, n, 0) #define revi(i, a, b) for (int i = (int)(a - 1); i >= (int)(b); i--) #define rev(...) _overload(__VA_ARGS__, revi, _rev)(__VA_ARGS__) #define each(i, n) for (auto &&i : n) const int INF = 1e18, MOD = 1e9 + 7; signed main() { INCANT; int n, p, l2, l1, r1, r2, res = 0; cin >> n; int index[111111]; rep(i, n) { cin >> p; index[p] = i; } multiset<int> s; s.insert(-1); s.insert(-1); s.insert(n); s.insert(n); rev(i, n + 1, 1) { auto now = s.lower_bound(index[i]); r1 = *now; r2 = *++now; l1 = *-- --now; l2 = *--now; res += ((l1 - l2) * (r1 - index[i]) + (r2 - r1) * (index[i] - l1)) * i; s.insert(index[i]); } cout << res << endl; }
replace
34
35
34
35
TLE
p02920
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <set> #include <string> #include <vector> using namespace std; using ll = long long; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; int m = 1 << n; multiset<int> st; for (int i = 0; i < m; i++) { int s; cin >> s; st.insert(s); } vector<int> c(n); auto it = st.end(); it--; c[0] = *it; st.erase(it); for (int k = 0; k < n; k++) { int l = 1 << k; for (int i = 0; i < l; i++) { auto it = st.lower_bound(c[i]); if (it == st.begin()) { cout << "No" << endl; exit(0); } it--; c[l + i] = *it; st.erase(it); } } cout << "Yes" << endl; return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <set> #include <string> #include <vector> using namespace std; using ll = long long; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; int m = 1 << n; multiset<int> st; for (int i = 0; i < m; i++) { int s; cin >> s; st.insert(s); } vector<int> c(m); auto it = st.end(); it--; c[0] = *it; st.erase(it); for (int k = 0; k < n; k++) { int l = 1 << k; for (int i = 0; i < l; i++) { auto it = st.lower_bound(c[i]); if (it == st.begin()) { cout << "No" << endl; exit(0); } it--; c[l + i] = *it; st.erase(it); } } cout << "Yes" << endl; return 0; }
replace
27
28
27
28
0
p02920
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <iostream> #include <queue> #define N 30005 using namespace std; int n, s[N]; bool v[N]; priority_queue<int> pq[2]; int main() { int i, j; cin >> n; for (i = 0; i < 1 << n; i++) { scanf("%d", &s[i]); } sort(s, s + (1 << n), greater<int>()); pq[0].push(s[0]); for (i = 0; i < n; i++) { for (j = 1; j < 1 << n; j++) { if (!v[j] && s[j] < pq[i % 2].top()) { v[j] = 1; pq[1 - i % 2].push(pq[i % 2].top()); pq[1 - i % 2].push(s[j]); pq[i % 2].pop(); if (pq[i % 2].empty()) break; } } if (!pq[i % 2].empty()) { puts("No"); return 0; } } puts("Yes"); return 0; }
#include <algorithm> #include <cstdio> #include <iostream> #include <queue> #define N 300005 using namespace std; int n, s[N]; bool v[N]; priority_queue<int> pq[2]; int main() { int i, j; cin >> n; for (i = 0; i < 1 << n; i++) { scanf("%d", &s[i]); } sort(s, s + (1 << n), greater<int>()); pq[0].push(s[0]); for (i = 0; i < n; i++) { for (j = 1; j < 1 << n; j++) { if (!v[j] && s[j] < pq[i % 2].top()) { v[j] = 1; pq[1 - i % 2].push(pq[i % 2].top()); pq[1 - i % 2].push(s[j]); pq[i % 2].pop(); if (pq[i % 2].empty()) break; } } if (!pq[i % 2].empty()) { puts("No"); return 0; } } puts("Yes"); return 0; }
replace
4
5
4
5
0
p02920
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int n, val; multiset<int> m1, m2; int main() { cin >> n; for (int i = 1; i <= (1 << n); i++) { cin >> val; m1.insert(val); } auto it = m1.end(); it--; int nr = *it; m2.insert(nr); m1.erase(it); for (int i = 1; i <= n; i++) { for (auto it = m2.begin(); it != m2.end(); it++) { nr = *it; auto it2 = m1.lower_bound(nr); it2--; int nr2 = *it2; if (nr2 >= nr) { cout << "No"; return 0; } m2.insert(nr2); m1.erase(it2); } } cout << "Yes"; return 0; }
#include <bits/stdc++.h> using namespace std; int n, val; multiset<int> m1, m2; int main() { cin >> n; for (int i = 1; i <= (1 << n); i++) { cin >> val; m1.insert(val); } auto it = m1.end(); it--; int nr = *it; m2.insert(nr); m1.erase(it); for (int i = 1; i <= n; i++) { for (auto it = m2.begin(); it != m2.end(); it++) { nr = *it; auto it2 = m1.lower_bound(nr); if (it2 == m1.begin()) { cout << "No"; return 0; } it2--; int nr2 = *it2; if (nr2 >= nr) { cout << "No"; return 0; } m2.insert(nr2); m1.erase(it2); } } cout << "Yes"; return 0; }
insert
20
20
20
24
0
p02920
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define de(x) cout << #x << "=" << x << endl #define dd(x) cout << #x << "=" << x << " " #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 repp(i, a, b, t) for (int i = a; i < (b); i += t) #define mt(a, b) memset(a, b, sizeof(a)) #define fi first #define se second #define mp(u, v) make_pair(u, v) #define sz(a) (int)a.size() #define pb push_back #define PI acos(-1.0) #define qc std::ios::sync_with_stdio(false) #define all(a) a.begin(), a.end() using namespace std; typedef vector<int> vi; typedef long long ll; typedef double db; typedef pair<ll, int> pli; typedef pair<int, int> pii; const ll mod = 1000000007; const int N = 5e5 + 6; const int M = 2e6 + 6; const double eps = 1e-6; const int inf = 0x3f3f3f3f; bool eq(const db &a, const db &b) { return fabs(a - b) < eps; } bool ls(const db &a, const db &b) { return a + eps < b; } bool le(const db &a, const db &b) { return eq(a, b) || ls(a, b); } ll gcd(ll a, ll b) { return a == 0 ? b : gcd(b % a, a); }; ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll kpow(ll a, ll b) { ll res = 1; a %= mod; if (b < 0) return 1; for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } ll read() { ll 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; } // inv[1]=1; // for(int i=2;i<=n;i++) inv[i]=(mod-mod/i)*inv[mod%i]%mod; int n; int a[N]; int main() { scanf("%d", &n); int all = 1 << n; multiset<int> S; rep(i, 0, all) scanf("%d", a + i); rep(i, 0, all) S.insert(-a[i]); vi cur, nxt; cur.pb(*S.begin()); S.erase(S.begin()); rep(i, 0, n) { nxt = cur; for (auto x : cur) { auto it = upper_bound(all(S), x); if (it == S.end()) return puts("No"), 0; nxt.pb(*it); S.erase(it); } cur = nxt; } puts("Yes"); // 1 1 3 3 4 4 4 8 return 0; }
#include <bits/stdc++.h> #define de(x) cout << #x << "=" << x << endl #define dd(x) cout << #x << "=" << x << " " #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 repp(i, a, b, t) for (int i = a; i < (b); i += t) #define mt(a, b) memset(a, b, sizeof(a)) #define fi first #define se second #define mp(u, v) make_pair(u, v) #define sz(a) (int)a.size() #define pb push_back #define PI acos(-1.0) #define qc std::ios::sync_with_stdio(false) #define all(a) a.begin(), a.end() using namespace std; typedef vector<int> vi; typedef long long ll; typedef double db; typedef pair<ll, int> pli; typedef pair<int, int> pii; const ll mod = 1000000007; const int N = 5e5 + 6; const int M = 2e6 + 6; const double eps = 1e-6; const int inf = 0x3f3f3f3f; bool eq(const db &a, const db &b) { return fabs(a - b) < eps; } bool ls(const db &a, const db &b) { return a + eps < b; } bool le(const db &a, const db &b) { return eq(a, b) || ls(a, b); } ll gcd(ll a, ll b) { return a == 0 ? b : gcd(b % a, a); }; ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll kpow(ll a, ll b) { ll res = 1; a %= mod; if (b < 0) return 1; for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } ll read() { ll 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; } // inv[1]=1; // for(int i=2;i<=n;i++) inv[i]=(mod-mod/i)*inv[mod%i]%mod; int n; int a[N]; int main() { scanf("%d", &n); int all = 1 << n; multiset<int> S; rep(i, 0, all) scanf("%d", a + i); rep(i, 0, all) S.insert(-a[i]); vi cur, nxt; cur.pb(*S.begin()); S.erase(S.begin()); rep(i, 0, n) { nxt = cur; for (auto x : cur) { auto it = S.upper_bound(x); if (it == S.end()) return puts("No"), 0; nxt.pb(*it); S.erase(it); } cur = nxt; } puts("Yes"); // 1 1 3 3 4 4 4 8 return 0; }
replace
75
76
75
76
TLE
p02920
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = int_fast64_t; using ld = long double; using pll = pair<ll, ll>; using pld = pair<ld, ld>; const ll INF = 1LL << 60; void solve(); int main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(10); solve(); } #define SELECTOR(_1, _2, _3, _4, SELECT, ...) SELECT #define rep(...) SELECTOR(__VA_ARGS__, _rep2, _rep1, _rep0)(__VA_ARGS__) #define _rep0(i, n) for (ll i = 0; i < n; ++i) #define _rep1(i, k, n) for (ll i = k; i < n; ++i) #define _rep2(i, k, n, d) \ for (ll i = k; d != 0 && d > 0 ? i < n : i > n; i += d) #define foreach(i, A) for (auto &i : A) #define all(A) A.begin(), A.end() #define len(A) (ll) A.size() template <class T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); } template <class... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); } template <class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } void solve() { ll n; cin >> n; multiset<ll> S{-1}; rep(_, 1 << n) { ll s; cin >> s; S.insert(s); } vector<ll> now{*S.rbegin()}; // 初めは最大値から rep(_, n) { auto next = now; foreach (v, now) { auto it = lower_bound(all(S), v); if (it == S.begin()) { cout << "No" << "\n"; return; } ll s = *(--it); if (s == -1) { cout << "No" << "\n"; return; } S.erase(it); next.push_back(s); } now = next; } cout << "Yes" << "\n"; }
#include <bits/stdc++.h> using namespace std; using ll = int_fast64_t; using ld = long double; using pll = pair<ll, ll>; using pld = pair<ld, ld>; const ll INF = 1LL << 60; void solve(); int main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(10); solve(); } #define SELECTOR(_1, _2, _3, _4, SELECT, ...) SELECT #define rep(...) SELECTOR(__VA_ARGS__, _rep2, _rep1, _rep0)(__VA_ARGS__) #define _rep0(i, n) for (ll i = 0; i < n; ++i) #define _rep1(i, k, n) for (ll i = k; i < n; ++i) #define _rep2(i, k, n, d) \ for (ll i = k; d != 0 && d > 0 ? i < n : i > n; i += d) #define foreach(i, A) for (auto &i : A) #define all(A) A.begin(), A.end() #define len(A) (ll) A.size() template <class T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); } template <class... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); } template <class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } void solve() { ll n; cin >> n; multiset<ll> S{-1}; rep(_, 1 << n) { ll s; cin >> s; S.insert(s); } vector<ll> now{*S.rbegin()}; // 初めは最大値から rep(_, n) { auto next = now; foreach (v, now) { auto it = S.lower_bound(v); ll s = *(--it); if (s == -1) { cout << "No" << "\n"; return; } S.erase(it); next.push_back(s); } now = next; } cout << "Yes" << "\n"; }
replace
56
62
56
57
TLE
p02920
C++
Runtime Error
#include <cstdio> #include <iostream> #include <set> #include <vector> using namespace std; int n; int m; vector<int> ve; multiset<int> se; int main() { // freopen("input.txt", "r", stdin); scanf("%d", &n); m = (1 << n); for (int i = 0; i < m; i++) { int t; scanf("%d", &t); se.insert(t); } auto it = se.end(); it--; ve.push_back(*it); se.erase(it); for (int i = 1; i < m; i += i) { for (int j = ve.size() - 1; j >= 0; j--) { auto it2 = se.lower_bound(ve[j]); if (*it2 >= ve[j] && it2 != se.begin()) it2--; else if (*it2 >= ve[j]) { printf("No"); return 0; } ve.push_back(*it2); se.erase(it2); } } printf("Yes"); return 0; }
#include <cstdio> #include <iostream> #include <set> #include <vector> using namespace std; int n; int m; vector<int> ve; multiset<int> se; int main() { // freopen("input.txt", "r", stdin); scanf("%d", &n); m = (1 << n); for (int i = 0; i < m; i++) { int t; scanf("%d", &t); se.insert(t); } auto it = se.end(); it--; ve.push_back(*it); se.erase(it); for (int i = 1; i < m; i += i) { for (int j = ve.size() - 1; j >= 0; j--) { auto it2 = se.lower_bound(ve[j]); if (it2 == se.end() || *it2 >= ve[j] && it2 != se.begin()) it2--; else if (*it2 >= ve[j]) { printf("No"); return 0; } ve.push_back(*it2); se.erase(it2); } } printf("Yes"); return 0; }
replace
28
29
28
29
-6
free(): invalid pointer
p02920
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define whlie while #define pb push_back #define eb emplace_back #define fi first #define se second #define rep(i, N) for (int i = 0; i < (N); i++) #define repr(i, N) for (int i = (N)-1; i >= 0; i--) #define rep1(i, N) for (int i = 1; i <= (N); i++) #define repr1(i, N) for (int i = (N); i > 0; i--) #define each(x, v) for (auto &x : v) #define all(v) (v).begin(), (v).end() #define sz(v) ((int)(v).size()) #define ini(...) \ int __VA_ARGS__; \ in(__VA_ARGS__) #define inl(...) \ ll __VA_ARGS__; \ in(__VA_ARGS__) #define ins(...) \ string __VA_ARGS__; \ in(__VA_ARGS__) using namespace std; void solve(); using ll = long long; using vl = vector<ll>; using vi = vector<int>; using vvi = vector<vector<int>>; constexpr int inf = 1001001001; constexpr ll infLL = (1LL << 61) - 1; struct IoSetupNya { IoSetupNya() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); cerr << fixed << setprecision(7); } } iosetupnya; template <typename T, typename U> inline bool amin(T &x, U y) { return (y < x) ? (x = y, true) : false; } template <typename T, typename U> inline bool amax(T &x, U y) { return (x < y) ? (x = y, true) : false; } template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << p.first << " " << p.second; return os; } template <typename T, typename U> istream &operator>>(istream &is, pair<T, U> &p) { is >> p.first >> p.second; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { int s = (int)v.size(); rep(i, s) os << (i ? " " : "") << v[i]; return os; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (auto &x : v) is >> x; return is; } void in() {} template <typename T, class... U> void in(T &t, U &...u) { cin >> t; in(u...); } void out() { cout << "\n"; } template <typename T, class... U> void out(const T &t, const U &...u) { cout << t; if (sizeof...(u)) cout << " "; out(u...); } template <typename T> void die(T x) { out(x); exit(0); } #ifdef NyaanDebug #include "NyaanDebug.h" #define trc(...) \ do { \ cerr << #__VA_ARGS__ << " = "; \ dbg_out(__VA_ARGS__); \ } while (0) #define trca(v, N) \ do { \ cerr << #v << " = "; \ array_out(v, N); \ cout << endl; \ } while (0) #else #define trc(...) #define trca(...) int main() { solve(); } #endif using P = pair<ll, ll>; using vp = vector<P>; constexpr int MOD = /**/ 1000000007; //*/ 998244353; ///////// void solve() { ini(N); vi a(1 << N); in(a); sort(all(a), greater<int>()); multiset<int, greater<int>> used, unused; used.insert(a[0]); rep1(i, (1 << N) - 1) unused.insert(a[i]); rep(i, N) { auto cur = used; auto it = unused.begin(); each(x, used) cerr << x << " "; cerr << endl; each(x, unused) cerr << x << " "; cerr << endl; each(x, cur) { if (it == unused.end()) die("No"); while (*it >= x) { if (++it == unused.end()) die("No"); } used.insert(*it); it = unused.erase(it); } } out("Yes"); }
#include <bits/stdc++.h> #define whlie while #define pb push_back #define eb emplace_back #define fi first #define se second #define rep(i, N) for (int i = 0; i < (N); i++) #define repr(i, N) for (int i = (N)-1; i >= 0; i--) #define rep1(i, N) for (int i = 1; i <= (N); i++) #define repr1(i, N) for (int i = (N); i > 0; i--) #define each(x, v) for (auto &x : v) #define all(v) (v).begin(), (v).end() #define sz(v) ((int)(v).size()) #define ini(...) \ int __VA_ARGS__; \ in(__VA_ARGS__) #define inl(...) \ ll __VA_ARGS__; \ in(__VA_ARGS__) #define ins(...) \ string __VA_ARGS__; \ in(__VA_ARGS__) using namespace std; void solve(); using ll = long long; using vl = vector<ll>; using vi = vector<int>; using vvi = vector<vector<int>>; constexpr int inf = 1001001001; constexpr ll infLL = (1LL << 61) - 1; struct IoSetupNya { IoSetupNya() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); cerr << fixed << setprecision(7); } } iosetupnya; template <typename T, typename U> inline bool amin(T &x, U y) { return (y < x) ? (x = y, true) : false; } template <typename T, typename U> inline bool amax(T &x, U y) { return (x < y) ? (x = y, true) : false; } template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << p.first << " " << p.second; return os; } template <typename T, typename U> istream &operator>>(istream &is, pair<T, U> &p) { is >> p.first >> p.second; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { int s = (int)v.size(); rep(i, s) os << (i ? " " : "") << v[i]; return os; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (auto &x : v) is >> x; return is; } void in() {} template <typename T, class... U> void in(T &t, U &...u) { cin >> t; in(u...); } void out() { cout << "\n"; } template <typename T, class... U> void out(const T &t, const U &...u) { cout << t; if (sizeof...(u)) cout << " "; out(u...); } template <typename T> void die(T x) { out(x); exit(0); } #ifdef NyaanDebug #include "NyaanDebug.h" #define trc(...) \ do { \ cerr << #__VA_ARGS__ << " = "; \ dbg_out(__VA_ARGS__); \ } while (0) #define trca(v, N) \ do { \ cerr << #v << " = "; \ array_out(v, N); \ cout << endl; \ } while (0) #else #define trc(...) #define trca(...) int main() { solve(); } #endif using P = pair<ll, ll>; using vp = vector<P>; constexpr int MOD = /**/ 1000000007; //*/ 998244353; ///////// void solve() { ini(N); vi a(1 << N); in(a); sort(all(a), greater<int>()); multiset<int, greater<int>> used, unused; used.insert(a[0]); rep1(i, (1 << N) - 1) unused.insert(a[i]); rep(i, N) { auto cur = used; auto it = unused.begin(); each(x, cur) { if (it == unused.end()) die("No"); while (*it >= x) { if (++it == unused.end()) die("No"); } used.insert(*it); it = unused.erase(it); } } out("Yes"); }
delete
117
122
117
117
TLE
p02920
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> P; typedef vector<int> VI; 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; } #define _overload3(_1, _2, _3, name, ...) name #define _rep(i, n) repi(i, 0, n) #define repi(i, a, b) for (int i = int(a); i < int(b); ++i) #define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__) #define all(x) (x).begin(), (x).end() const int mod = 1e9 + 7; int main() { int n; cin >> n; int n0 = (1 << n); multiset<int> ideal; rep(i, n0) { int tmp; cin >> tmp; ideal.insert(tmp); } vector<int> ans; auto ite = ideal.end(); ans.push_back(*(--ite)); ideal.erase(ite); rep(I, n) { vector<int> children = ans; rep(i, ans.size()) { int t = ans[i]; auto it = lower_bound(all(ideal), t); if (ideal.begin() == it) { cout << "No" << endl; exit(0); } it--; children.push_back(*it); ideal.erase(it); } swap(ans, children); } cout << "Yes" << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> P; typedef vector<int> VI; 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; } #define _overload3(_1, _2, _3, name, ...) name #define _rep(i, n) repi(i, 0, n) #define repi(i, a, b) for (int i = int(a); i < int(b); ++i) #define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__) #define all(x) (x).begin(), (x).end() const int mod = 1e9 + 7; int main() { int n; cin >> n; int n0 = (1 << n); multiset<int> ideal; rep(i, n0) { int tmp; cin >> tmp; ideal.insert(tmp); } vector<int> ans; auto ite = ideal.end(); ans.push_back(*(--ite)); ideal.erase(ite); rep(I, n) { vector<int> children = ans; rep(i, ans.size()) { int t = ans[i]; auto it = ideal.lower_bound(t); if (ideal.begin() == it) { cout << "No" << endl; exit(0); } it--; children.push_back(*it); ideal.erase(it); } swap(ans, children); } cout << "Yes" << endl; }
replace
45
46
45
46
TLE
p02920
C++
Runtime Error
#include <algorithm> // minmax, sort, swap #include <climits> // INT_MIN, LLONG_MIN #include <cmath> // long, trig, pow #include <cstdio> // printf, scanf #include <deque> // deque #include <functional> // std::function<void(int)> #include <iomanip> // cout<<setprecision(n) #include <iostream> // cin, cout, cerr, clog #include <map> // key-value pairs sorted by keys #include <numeric> // iota, accumulate, inner_product #include <queue> // queue, priority_queue #include <set> // set #include <stack> // stack #include <string> // string, stoi, to_string #include <unordered_map> // hashed by keys #include <unordered_set> // hashed by keys #include <vector> // vector #define rep(i, n) for (int i = 0; i < (n); i++) #define ENDL '\n' #define print(i) std::cout << (i) << '\n' #define int long long // at least int64 > 9*10^18 #define all(v) (v).begin(), (v).end() /* libraries */ struct FastIO { FastIO() { std::cin.tie(0); std::ios_base::sync_with_stdio(0); } } fastio; int zpowl(int v, int k) { int r(1), t(v); while (k) { if (k & 1) r *= t; t *= t; k >>= 1; } return r; } signed main() { int n; std::cin >> n; int m = zpowl(2, n); std::vector<int> s(m); rep(i, m) std::cin >> s[i]; std::multiset<int, std::greater<int>> set; for (int i : s) set.emplace(i); std::vector<int> created; auto it = set.begin(); created.emplace_back(*it); set.erase(it); rep(i, n) { for (int c : created) { auto it = set.upper_bound(c); if (it == set.end()) { print("No"); return 0; } created.emplace_back(*it); set.erase(it); } } print("Yes"); return 0; }
#include <algorithm> // minmax, sort, swap #include <climits> // INT_MIN, LLONG_MIN #include <cmath> // long, trig, pow #include <cstdio> // printf, scanf #include <deque> // deque #include <functional> // std::function<void(int)> #include <iomanip> // cout<<setprecision(n) #include <iostream> // cin, cout, cerr, clog #include <map> // key-value pairs sorted by keys #include <numeric> // iota, accumulate, inner_product #include <queue> // queue, priority_queue #include <set> // set #include <stack> // stack #include <string> // string, stoi, to_string #include <unordered_map> // hashed by keys #include <unordered_set> // hashed by keys #include <vector> // vector #define rep(i, n) for (int i = 0; i < (n); i++) #define ENDL '\n' #define print(i) std::cout << (i) << '\n' #define int long long // at least int64 > 9*10^18 #define all(v) (v).begin(), (v).end() /* libraries */ struct FastIO { FastIO() { std::cin.tie(0); std::ios_base::sync_with_stdio(0); } } fastio; int zpowl(int v, int k) { int r(1), t(v); while (k) { if (k & 1) r *= t; t *= t; k >>= 1; } return r; } signed main() { int n; std::cin >> n; int m = zpowl(2, n); std::vector<int> s(m); rep(i, m) std::cin >> s[i]; std::multiset<int, std::greater<int>> set; for (int i : s) set.emplace(i); std::vector<int> created; auto it = set.begin(); created.emplace_back(*it); set.erase(it); rep(i, n) { rep(j, zpowl(2, i)) { auto it = set.upper_bound(created[j]); if (it == set.end()) { print("No"); return 0; } created.emplace_back(*it); set.erase(it); } } print("Yes"); return 0; }
replace
58
60
58
60
0
p02920
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < (n); i++) #define rep1(i, n) for (int i = 1; i <= (n); i++) #define co(x) cout << (x) << "\n" #define cosp(x) cout << (x) << " " #define ce(x) cerr << (x) << "\n" #define cesp(x) cerr << (x) << " " #define pb push_back #define mp make_pair #define Would #define you #define please void pakuri_sort(int N, int A[]) { const int b = 8; int tmp[200001]; rep(k, 4) { int kazu[1 << b] = {}, kazu2[1 << b] = {}; rep(i, N) kazu[A[i] >> k * b & ((1 << b) - 1)]++; rep(i, (1 << b) - 1) kazu[i + 1] += kazu[i]; for (int i = N - 1; i >= 0; i--) tmp[--kazu[A[i] >> k * b & ((1 << b) - 1)]] = A[i]; k++; rep(i, N) kazu2[tmp[i] >> k * b & ((1 << b) - 1)]++; rep(i, (1 << b) - 1) kazu2[i + 1] += kazu2[i]; for (int i = N - 1; i >= 0; i--) A[--kazu2[tmp[i] >> k * b & ((1 << b) - 1)]] = tmp[i]; } } int main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; int n = (1 << N); int S[1 << 18]; rep(i, (1 << N)) { cin >> S[i]; } pakuri_sort(n, S); int A[1 << 17]; A[0] = S[n - 1]; int OK = 1; rep(i, N - 1) { int p = n - (1 << i); for (int j = (1 << i) - 1; j >= 0; j--) { while (p >= 0 && S[p] >= A[j]) p--; if (p < 0) { OK = 0; break; } A[j + (1 << i)] = S[p]; S[p] = 1e9; p--; } if (!OK) break; pakuri_sort(n - (1 << i), S); pakuri_sort(1 << (i + 1), A); } int i = N - 1; int p = n - (1 << i); for (int j = (1 << i) - 1; j >= 0; j--) { while (p >= 0 && S[p] >= A[j]) p--; if (p < 0) { OK = 0; break; } S[p] = 1e9; p--; } if (OK) co("Yes"); else co("No"); Would you please return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < (n); i++) #define rep1(i, n) for (int i = 1; i <= (n); i++) #define co(x) cout << (x) << "\n" #define cosp(x) cout << (x) << " " #define ce(x) cerr << (x) << "\n" #define cesp(x) cerr << (x) << " " #define pb push_back #define mp make_pair #define Would #define you #define please void pakuri_sort(int N, int A[]) { const int b = 8; int tmp[1 << 18]; rep(k, 4) { int kazu[1 << b] = {}, kazu2[1 << b] = {}; rep(i, N) kazu[A[i] >> k * b & ((1 << b) - 1)]++; rep(i, (1 << b) - 1) kazu[i + 1] += kazu[i]; for (int i = N - 1; i >= 0; i--) tmp[--kazu[A[i] >> k * b & ((1 << b) - 1)]] = A[i]; k++; rep(i, N) kazu2[tmp[i] >> k * b & ((1 << b) - 1)]++; rep(i, (1 << b) - 1) kazu2[i + 1] += kazu2[i]; for (int i = N - 1; i >= 0; i--) A[--kazu2[tmp[i] >> k * b & ((1 << b) - 1)]] = tmp[i]; } } int main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; int n = (1 << N); int S[1 << 18]; rep(i, (1 << N)) { cin >> S[i]; } pakuri_sort(n, S); int A[1 << 17]; A[0] = S[n - 1]; int OK = 1; rep(i, N - 1) { int p = n - (1 << i); for (int j = (1 << i) - 1; j >= 0; j--) { while (p >= 0 && S[p] >= A[j]) p--; if (p < 0) { OK = 0; break; } A[j + (1 << i)] = S[p]; S[p] = 1e9; p--; } if (!OK) break; pakuri_sort(n - (1 << i), S); pakuri_sort(1 << (i + 1), A); } int i = N - 1; int p = n - (1 << i); for (int j = (1 << i) - 1; j >= 0; j--) { while (p >= 0 && S[p] >= A[j]) p--; if (p < 0) { OK = 0; break; } S[p] = 1e9; p--; } if (OK) co("Yes"); else co("No"); Would you please return 0; }
replace
17
18
17
18
0
p02920
C++
Time Limit Exceeded
#include <algorithm> #include <assert.h> #include <bitset> #include <cctype> #include <cmath> #include <complex> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <limits.h> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <vector> using ll = long long; using P = std::pair<ll, ll>; #define rep(i, a, b) for (ll(i) = (a); i < (b); i++) #define all(i) i.begin(), i.end() #define debug(i) std::cerr << "debug " << i << std::endl // const ll MOD = 998244353; const ll MOD = 1e9 + 7; int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); // 問題文中の添え字が0-indexか1-indexか確認! ll n; std::cin >> n; /*unusedはまだ生成してないスライム slimeは生成したスライム*/ std::multiset<ll> unused, slime; rep(i, 0, 1 << n) { ll s; std::cin >> s; unused.insert(s); } // unusedの最大値を最初のスライムにする slime.insert(*unused.rbegin()); unused.erase(*unused.rbegin()); // 各秒を貪欲で考える rep(i, 0, n) { // この秒で生成するスライムを保管しておくtemp std::vector<ll> temp; // 今いるスライム各々が生成できる最大のスライムを作る for (auto itr = slime.rbegin(); itr != slime.rend(); itr++) { auto itr2 = std::lower_bound(all(unused), *itr); // 自分より小さいスライムがいないならNo if (itr2 == unused.begin()) { std::cout << "No"; return 0; } itr2--; temp.push_back(*itr2); unused.erase(itr2); } rep(j, 0, 1 << i) slime.insert(temp[j]); } std::cout << "Yes"; return 0; }
#include <algorithm> #include <assert.h> #include <bitset> #include <cctype> #include <cmath> #include <complex> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <limits.h> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <vector> using ll = long long; using P = std::pair<ll, ll>; #define rep(i, a, b) for (ll(i) = (a); i < (b); i++) #define all(i) i.begin(), i.end() #define debug(i) std::cerr << "debug " << i << std::endl // const ll MOD = 998244353; const ll MOD = 1e9 + 7; int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); // 問題文中の添え字が0-indexか1-indexか確認! ll n; std::cin >> n; /*unusedはまだ生成してないスライム slimeは生成したスライム*/ std::multiset<ll> unused, slime; rep(i, 0, 1 << n) { ll s; std::cin >> s; unused.insert(s); } // unusedの最大値を最初のスライムにする slime.insert(*unused.rbegin()); unused.erase(*unused.rbegin()); // 各秒を貪欲で考える rep(i, 0, n) { // この秒で生成するスライムを保管しておくtemp std::vector<ll> temp; // 今いるスライム各々が生成できる最大のスライムを作る for (auto itr = slime.rbegin(); itr != slime.rend(); itr++) { auto itr2 = unused.lower_bound(*itr); // 自分より小さいスライムがいないならNo if (itr2 == unused.begin()) { std::cout << "No"; return 0; } itr2--; temp.push_back(*itr2); unused.erase(itr2); } rep(j, 0, 1 << i) slime.insert(temp[j]); } std::cout << "Yes"; return 0; }
replace
58
59
58
59
TLE
p02920
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define REP(i, n) for (int i = 0, _n = (int)(n); i < _n; ++i) #define ALL(v) (v).begin(), (v).end() #define CLR(t, v) memset(t, (v), sizeof(t)) template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << "," << a.second << ")"; } template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << (*i) << " "; cout << endl; } template <class T> void chmin(T &a, const T &b) { if (a > b) a = b; } template <class T> void chmax(T &a, const T &b) { if (a < b) a = b; } int nextInt() { int x; scanf("%d", &x); return x; } const int MAX_N = 112345; int A[MAX_N]; multiset<int> S, have; bool solve(int N, multiset<int> &S, multiset<int> &have) { REP(age, N) { vector<int> nxt; for (int x : have) { auto it = S.lower_bound(x); if (it == S.begin()) return false; --it; nxt.push_back(*it); S.erase(it); } for (int x : nxt) have.insert(x); } return true; } int main2() { S.clear(); have.clear(); int N = nextInt(); int T = 1 << N; REP(i, T) A[i] = nextInt(); sort(A, A + T); REP(i, T - 1) S.insert(A[i]); have.insert(A[T - 1]); cout << (solve(N, S, have) ? "Yes" : "No") << endl; return 0; } int main() { #ifdef LOCAL for (; !cin.eof(); cin >> ws) #endif main2(); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define REP(i, n) for (int i = 0, _n = (int)(n); i < _n; ++i) #define ALL(v) (v).begin(), (v).end() #define CLR(t, v) memset(t, (v), sizeof(t)) template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << "," << a.second << ")"; } template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << (*i) << " "; cout << endl; } template <class T> void chmin(T &a, const T &b) { if (a > b) a = b; } template <class T> void chmax(T &a, const T &b) { if (a < b) a = b; } int nextInt() { int x; scanf("%d", &x); return x; } const int MAX_N = (1 << 18) + 10; int A[MAX_N]; multiset<int> S, have; bool solve(int N, multiset<int> &S, multiset<int> &have) { REP(age, N) { vector<int> nxt; for (int x : have) { auto it = S.lower_bound(x); if (it == S.begin()) return false; --it; nxt.push_back(*it); S.erase(it); } for (int x : nxt) have.insert(x); } return true; } int main2() { S.clear(); have.clear(); int N = nextInt(); int T = 1 << N; REP(i, T) A[i] = nextInt(); sort(A, A + T); REP(i, T - 1) S.insert(A[i]); have.insert(A[T - 1]); cout << (solve(N, S, have) ? "Yes" : "No") << endl; return 0; } int main() { #ifdef LOCAL for (; !cin.eof(); cin >> ws) #endif main2(); return 0; }
replace
31
32
31
32
0
p02920
C++
Time Limit Exceeded
#include <bits/stdc++.h> #include <iomanip> using namespace std; #define reps(i, s, n) for (int i = s; i < n; i++) #define rep(i, n) reps(i, 0, n) #define fi first #define se second #define mp make_pair typedef long long ll; typedef vector<ll> vec; typedef vector<vec> mat; ll N, M, H, W, K, Q, A, B, C, L, R; string S, T; const ll MOD = (1e+9) + 7; const ll INF = 1LL << 60; typedef pair<ll, ll> P; typedef vector<P> vp; typedef vector<vp> matP; int main() { cin >> N; multiset<ll> goal; rep(j, (1LL << N)) { cin >> A; goal.insert(A); } queue<ll> now; auto big = goal.end(); --big; now.push(*big); goal.erase(big); rep(i, N) { rep(j, (1LL << i)) { ll sli = now.front(); now.pop(); now.push(sli); auto son = lower_bound(goal.begin(), goal.end(), sli); /*cout<<i<<j<<endl; for(ll c : goal) cout<<c; cout<<endl; */ if (son == goal.begin()) { cout << "No" << endl; return 0; } --son; now.push(*son); goal.erase(son); } } cout << "Yes" << endl; }
#include <bits/stdc++.h> #include <iomanip> using namespace std; #define reps(i, s, n) for (int i = s; i < n; i++) #define rep(i, n) reps(i, 0, n) #define fi first #define se second #define mp make_pair typedef long long ll; typedef vector<ll> vec; typedef vector<vec> mat; ll N, M, H, W, K, Q, A, B, C, L, R; string S, T; const ll MOD = (1e+9) + 7; const ll INF = 1LL << 60; typedef pair<ll, ll> P; typedef vector<P> vp; typedef vector<vp> matP; int main() { cin >> N; multiset<ll> goal; rep(j, (1LL << N)) { cin >> A; goal.insert(A); } queue<ll> now; auto big = goal.end(); --big; now.push(*big); goal.erase(big); rep(i, N) { rep(j, (1LL << i)) { ll sli = now.front(); now.pop(); now.push(sli); auto son = goal.lower_bound(sli); /*cout<<i<<j<<endl; for(ll c : goal) cout<<c; cout<<endl; */ if (son == goal.begin()) { cout << "No" << endl; return 0; } --son; now.push(*son); goal.erase(son); } } cout << "Yes" << endl; }
replace
38
39
38
39
TLE
p02920
C++
Runtime Error
#include <bits/stdc++.h> int main() { using namespace std; unsigned long N; cin >> N; vector<unsigned long> S(1UL << N); copy_n(istream_iterator<unsigned long>(cin), 1UL << N, S.begin()); sort(S.rbegin(), S.rend()); for (unsigned long i = 1; i < 1UL << N; ++i) if (S[i] == S[i - (1UL << (63 - __builtin_clzl(i)))]) abort(); // return 0 & puts("No"); puts("Yes"); return 0; }
#include <bits/stdc++.h> int main() { using namespace std; unsigned long N; cin >> N; vector<unsigned long> S(1UL << N); copy_n(istream_iterator<unsigned long>(cin), 1UL << N, S.begin()); sort(S.rbegin(), S.rend()); vector<unsigned long> T{S[0]}; list<unsigned long> l(S.begin() + 1, S.end()); for (unsigned long i = 0, cnt; i < N; ++i) { cnt = 0; auto it = l.begin(); while (it != l.end() && (cnt < 1UL << i)) { auto t = *it; if (T[cnt] > t) { T.push_back(t); it = l.erase(it); ++cnt; } else ++it; } if (cnt < 1UL << i) return 0 & puts("No"); sort(T.rbegin(), T.rend()); } puts("Yes"); return 0; }
replace
9
12
9
27
0
p02920
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; template <typename A> string to_string(multiset<A> v) { bool first = true; string result = "["; for (A i : v) { if (!first) result += ", "; first = false; result += to_string(i); } return result + "]"; } int main() { int n; cin >> n; multiset<int> slimes; for (int i = 0; i < pow(2, n); ++i) { int slime; cin >> slime; slimes.insert(slime); } cout << endl; multiset<int> created; ll firstSlime = *(slimes.rbegin()); created.insert(firstSlime); slimes.erase(firstSlime); for (int i = 0; i < n; ++i) { multiset<int> buffer; for (multiset<int>::reverse_iterator it = created.rbegin(); it != created.rend(); ++it) { ll slime = *it; if (slimes.lower_bound(slime) == slimes.begin()) { cout << "No" << endl; return 0; } ll toCreate = *max_element(slimes.begin(), slimes.lower_bound(slime)); buffer.insert(toCreate); slimes.erase(slimes.lower_bound(toCreate)); } for (int s : buffer) created.insert(s); } cout << "Yes" << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; template <typename A> string to_string(multiset<A> v) { bool first = true; string result = "["; for (A i : v) { if (!first) result += ", "; first = false; result += to_string(i); } return result + "]"; } int main() { int n; cin >> n; multiset<int> slimes; for (int i = 0; i < pow(2, n); ++i) { int slime; cin >> slime; slimes.insert(slime); } cout << endl; multiset<int> created; ll firstSlime = *(slimes.rbegin()); created.insert(firstSlime); slimes.erase(firstSlime); for (int i = 0; i < n; ++i) { multiset<int> buffer; for (multiset<int>::reverse_iterator it = created.rbegin(); it != created.rend(); ++it) { ll slime = *it; if (slimes.lower_bound(slime) == slimes.begin()) { cout << "No" << endl; return 0; } ll toCreate = *(--slimes.lower_bound(slime)); buffer.insert(toCreate); slimes.erase(slimes.lower_bound(toCreate)); } for (int s : buffer) created.insert(s); } cout << "Yes" << endl; }
replace
43
44
43
44
TLE
p02920
C++
Time Limit Exceeded
#define rep(i, m, n) for (int i = m; i < n; i++) #define repM(i, m, n) for (int i = n - 1; i >= m; i--) #define all(x) (x).begin(), (x).end() typedef long long ll; using namespace std; // typedef pair<int,int> pii; // d = distance(lower_bound(all(x)), upper_bound(all(x))); int INF = 1e9; int M = 1e9 + 7; #include <algorithm> #include <cmath> #include <iostream> #include <set> #include <vector> int main() { int N; cin >> N; int K = pow(2, N); int b; multiset<int> p; for (int i = 0; i < K; i++) { cin >> b; p.insert(b); } auto r = p.end(); r--; vector<int> v; v.push_back(*r); p.erase(r); // int mx = *min_element(p.begin(), p.end()); // vector<int> v; v.push_back(mx); p.erase(mx); for (int i = 1; i <= N; i++) { for (int j = 0; j < pow(2, i - 1); j++) { r = lower_bound(p.begin(), p.end(), v[j]); // int r = distance(p.begin(), lower_bound(p.begin(), p.end(), v[j])); // cout << *r << " "; if (r == p.begin()) { cout << "No" << endl; return 0; } else { v.push_back(*(--r)); p.erase(r); } } } cout << "Yes" << endl; }
#define rep(i, m, n) for (int i = m; i < n; i++) #define repM(i, m, n) for (int i = n - 1; i >= m; i--) #define all(x) (x).begin(), (x).end() typedef long long ll; using namespace std; // typedef pair<int,int> pii; // d = distance(lower_bound(all(x)), upper_bound(all(x))); int INF = 1e9; int M = 1e9 + 7; #include <algorithm> #include <cmath> #include <iostream> #include <set> #include <vector> int main() { int N; cin >> N; int K = pow(2, N); int b; multiset<int> p; for (int i = 0; i < K; i++) { cin >> b; p.insert(b); } auto r = p.end(); r--; vector<int> v; v.push_back(*r); p.erase(r); // int mx = *min_element(p.begin(), p.end()); // vector<int> v; v.push_back(mx); p.erase(mx); for (int i = 1; i <= N; i++) { for (int j = 0; j < pow(2, i - 1); j++) { r = p.lower_bound(v[j]); // int r = distance(p.begin(), lower_bound(p.begin(), p.end(), v[j])); // cout << *r << " "; if (r == p.begin()) { cout << "No" << endl; return 0; } else { v.push_back(*(--r)); p.erase(r); } } } cout << "Yes" << endl; }
replace
35
36
35
36
TLE
p02920
Python
Runtime Error
n = int(input()) s = [int(i) for i in input().split()] used = [0] * (2**n) used[0] = 1 s.sort(reverse=True) a = 1 b = [s[0]] for _ in range(n): c = b.copy() j = 0 for i in b: while j < 2**n: if used[j] == 0 and s[j] < i: c.append(s[j]) used[j] = 1 break j += 1 if j >= 2**n: print("No") exit() b = sorted(c, reverse=True) print("Yes")
n = int(input()) s = [int(i) for i in input().split()] used = [0] * (2**n) used[0] = 1 s.sort(reverse=True) a = 1 b = [s[0]] for _ in range(n): c = b[:] j = 0 for i in b: while j < 2**n: if used[j] == 0 and s[j] < i: c.append(s[j]) used[j] = 1 break j += 1 if j >= 2**n: print("No") exit() b = sorted(c, reverse=True) print("Yes")
replace
8
9
8
9
0
p02920
C++
Runtime Error
#pragma GCC optimize("Ofast,unroll-loops") #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 rep(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; set<P<int, int>> s; V<int> v, p; int a[300000]; int main() { int n; cin >> n; int m = (1 << n); FOR(i, 1, m) cin >> a[i]; sort(a + 1, a + m + 1, greater<int>()); FOR(i, 2, m) { s.insert(MP(a[i], i)); } v.PB(a[1]); rep(t, 0, n) { p.clear(); for (auto u : v) { auto iter = s.lower_bound(MP(u, 0)); iter--; if ((*iter).F >= u) { cout << "No"; RE 0; } p.PB((*iter).F); s.erase(iter); } for (auto u : p) v.PB(u); } cout << "Yes"; RE 0; }
#pragma GCC optimize("Ofast,unroll-loops") #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 rep(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; set<P<int, int>> s; V<int> v, p; int a[300000]; int main() { int n; cin >> n; int m = (1 << n); FOR(i, 1, m) cin >> a[i]; sort(a + 1, a + m + 1, greater<int>()); FOR(i, 2, m) { s.insert(MP(a[i], i)); } v.PB(a[1]); rep(t, 0, n) { p.clear(); for (auto u : v) { auto iter = s.lower_bound(MP(u, 0)); if (iter == s.begin()) { cout << "No"; RE 0; } iter--; if ((*iter).F >= u) { cout << "No"; RE 0; } p.PB((*iter).F); s.erase(iter); } for (auto u : p) v.PB(u); } cout << "Yes"; RE 0; }
insert
31
31
31
35
0
p02920
C++
Runtime Error
#include <algorithm> #include <cmath> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <tuple> #include <utility> #include <vector> #define REP(i, n) for (int i = 0; i < (n); ++i) #define ALL(x) (x).begin(), (x).end() #define SZ(x) (int)(x).size() using namespace std; using ll = long long; constexpr ll INF = 3000000000000000000; int main() { int n; cin >> n; vector<int> s(n); REP(i, 1 << n) cin >> s[i]; int mx = 0; multiset<int> st1, st2; REP(i, 1 << n) { st1.insert(-s[i]); mx = max(mx, s[i]); } st1.erase(st1.find(-mx)); st2.insert(-mx); REP(i, n) { multiset<int> st; for (int x : st2) { auto iter = st1.upper_bound(x); if (iter == st1.end()) { cout << "No\n"; return 0; } st.insert(*iter); st1.erase(iter); } for (int x : st) st2.insert(x); } cout << "Yes\n"; return 0; }
#include <algorithm> #include <cmath> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <tuple> #include <utility> #include <vector> #define REP(i, n) for (int i = 0; i < (n); ++i) #define ALL(x) (x).begin(), (x).end() #define SZ(x) (int)(x).size() using namespace std; using ll = long long; constexpr ll INF = 3000000000000000000; int main() { int n; cin >> n; vector<int> s(1 << n); REP(i, 1 << n) cin >> s[i]; int mx = 0; multiset<int> st1, st2; REP(i, 1 << n) { st1.insert(-s[i]); mx = max(mx, s[i]); } st1.erase(st1.find(-mx)); st2.insert(-mx); REP(i, n) { multiset<int> st; for (int x : st2) { auto iter = st1.upper_bound(x); if (iter == st1.end()) { cout << "No\n"; return 0; } st.insert(*iter); st1.erase(iter); } for (int x : st) st2.insert(x); } cout << "Yes\n"; return 0; }
replace
22
23
22
23
0
p02920
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; const int N = 1 << n; multiset<int> st1; for (int i = 0; i < N; ++i) { int s; cin >> s; st1.insert(s); } int maxi = *st1.rbegin(); st1.erase(st1.find(maxi)); multiset<int> st2; st2.insert(maxi); while (st1.size()) { multiset<int> tmp; for (auto i = st2.rbegin(); i != st2.rend(); ++i) { int s = *i; auto itr = lower_bound(st1.begin(), st1.end(), s); if (itr == st1.begin()) { cout << "No\n"; return 0; } itr = prev(itr); tmp.insert(*itr); st1.erase(itr); } for (int i : tmp) { st2.insert(i); } } cout << "Yes\n"; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; const int N = 1 << n; multiset<int> st1; for (int i = 0; i < N; ++i) { int s; cin >> s; st1.insert(s); } int maxi = *st1.rbegin(); st1.erase(st1.find(maxi)); multiset<int> st2; st2.insert(maxi); while (st1.size()) { multiset<int> tmp; for (auto i = st2.rbegin(); i != st2.rend(); ++i) { int s = *i; auto itr = st1.lower_bound(s); if (itr == st1.begin()) { cout << "No\n"; return 0; } itr = prev(itr); tmp.insert(*itr); st1.erase(itr); } for (int i : tmp) { st2.insert(i); } } cout << "Yes\n"; return 0; }
replace
21
22
21
22
TLE
p02920
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define mp make_pair #define pb push_back #define rep(i, n) for (int i = 0; i < n; i++) #define rep1(i, n) for (int i = 1; i <= (int)(n); i++) int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; int sz = pow(2, n); vector<int> s(sz); rep(i, sz) cin >> s[i]; sort(s.rbegin(), s.rend()); if (sz == 1) cout << "Yes" << endl; else { if (s[0] == s[1]) cout << "No" << endl; else { ll cap = n; ll ncap = n; priority_queue<ll> pq; pq.push(n); rep1(i, sz - 1) { vector<ll> tmp; ll same = 1; ll cn; cn = pq.top(); pq.pop(); if (cn > 0) { pq.push(cn - 1); tmp.pb(cn - 1); ncap += (cn - 1) * 2 - cn; } while (i < sz - 1 && s[i] == s[i + 1]) { i++; same++; if (!pq.empty()) { cn = pq.top(); pq.pop(); } else continue; if (cn > 0) { pq.push(cn - 1); tmp.pb(cn - 1); ncap += (cn - 1) * 2 - cn; } } // cout << "cap same " << cap << " " << same << endl; // cout << "ncap" << ncap <<endl; if (same > cap) { cout << "No" << endl; return 0; } cap = ncap; rep(j, tmp.size()) pq.push(tmp[i]); if (pq.empty()) break; } cout << "Yes" << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define mp make_pair #define pb push_back #define rep(i, n) for (int i = 0; i < n; i++) #define rep1(i, n) for (int i = 1; i <= (int)(n); i++) int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; int sz = pow(2, n); vector<int> s(sz); rep(i, sz) cin >> s[i]; sort(s.rbegin(), s.rend()); if (sz == 1) cout << "Yes" << endl; else { if (s[0] == s[1]) cout << "No" << endl; else { ll cap = n; ll ncap = n; priority_queue<ll> pq; pq.push(n); rep1(i, sz - 1) { vector<ll> tmp; ll same = 1; ll cn; cn = pq.top(); pq.pop(); if (cn > 0) { pq.push(cn - 1); tmp.pb(cn - 1); ncap += (cn - 1) * 2 - cn; } while (i < sz - 1 && s[i] == s[i + 1]) { i++; same++; if (!pq.empty()) { cn = pq.top(); pq.pop(); } else continue; if (cn > 0) { pq.push(cn - 1); tmp.pb(cn - 1); ncap += (cn - 1) * 2 - cn; } } // cout << "cap same " << cap << " " << same << endl; // cout << "ncap" << ncap <<endl; if (same > cap) { cout << "No" << endl; return 0; } cap = ncap; rep(j, tmp.size()) pq.push(tmp[j]); if (pq.empty()) break; } cout << "Yes" << endl; } } return 0; }
replace
62
63
62
63
0
p02920
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #include <set> using namespace std; #define N 1000010 multiset<int> s; inline int read() { int x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 3) + (x << 1) + c - '0'; c = getchar(); } return x * f; } int n, a[N], tot; int b[N], bn; int main() { n = read(); tot = 1 << n; for (int i = 1; i <= tot; i++) { a[i] = read(); s.insert(a[i]); } b[++bn] = (*--s.end()); s.erase(s.find(b[bn])); for (int i = 1; i <= n; i++) { for (int j = 1; j <= (1 << (i - 1)); j++) { if (*s.begin() > b[j]) { cout << "No" << endl; return 0; } b[++bn] = (*--s.lower_bound(b[j])); s.erase(s.find(b[bn])); } } cout << "Yes" << endl; return 0; }
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #include <set> using namespace std; #define N 1000010 multiset<int> s; inline int read() { int x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 3) + (x << 1) + c - '0'; c = getchar(); } return x * f; } int n, a[N], tot; int b[N], bn; int main() { n = read(); tot = 1 << n; for (int i = 1; i <= tot; i++) { a[i] = read(); s.insert(a[i]); } b[++bn] = (*--s.end()); s.erase(s.find(b[bn])); for (int i = 1; i <= n; i++) { for (int j = 1; j <= (1 << (i - 1)); j++) { if (*s.begin() >= b[j]) { cout << "No" << endl; return 0; } b[++bn] = (*--s.lower_bound(b[j])); s.erase(s.find(b[bn])); } } cout << "Yes" << endl; return 0; }
replace
35
36
35
36
0
p02920
C++
Runtime Error
#include <bits/stdc++.h> #pragma g++ optimize(2) using namespace std; int n, sum = 1; int tot, root; struct node { int l, r; int dat, val; int size; } f[200010]; void update(int p) { f[p].size = f[f[p].l].size + f[f[p].r].size + 1; } int New(int val) { f[++tot].val = val; f[tot].dat = rand(); f[tot].size = 1; return tot; } void split(int p, int val, int &x, int &y) { if (p == 0) { x = 0, y = 0; return; } if (f[p].val <= val) { x = p; split(f[p].r, val, f[p].r, y); } else { y = p; split(f[p].l, val, x, f[p].l); } update(p); } int merge(int a, int b) { if (!a || !b) return a + b; if (f[a].dat < f[b].dat) { f[a].r = merge(f[a].r, b); update(a); return a; } else { f[b].l = merge(a, f[b].l); update(b); return b; } } void insert(int val) { int x, y; split(root, val, x, y); root = merge(merge(x, New(val)), y); } void remove(int val) { int x, y, z; split(root, val, x, z); split(x, val - 1, x, y); y = merge(f[y].l, f[y].r); root = merge(merge(x, y), z); } int getRank(int val) { int x, y; split(root, val - 1, x, y); int ans = f[x].size + 1; root = merge(x, y); return ans; } int getVal(int p, int rank) { while (p) { if (f[f[p].l].size >= rank) p = f[p].l; else if (f[f[p].l].size + 1 == rank) return f[p].val; else rank -= f[f[p].l].size + 1, p = f[p].r; } } int getPre(int val) { int x, y; split(root, val - 1, x, y); int ans = getVal(x, f[x].size); root = merge(x, y); return ans; } int getNext(int val) { int x, y; split(root, val, x, y); int ans = getVal(y, 1); root = merge(x, y); return ans; } int a[2000010]; int main() { scanf("%d", &n); for (int i = 1; i <= pow(2, n); i++) { int x; scanf("%d", &x); insert(x); } insert(-1); a[1] = getPre(1999999999); remove(a[1]); for (int i = 0; i < n; i++) { for (int j = 1; j <= pow(2, i); j++) { int x = getPre(a[j]); remove(x); if (x == -1) { cout << "No" << endl; return 0; } tot++, a[tot] = x; } sort(a + 1, a + tot + 1, greater<int>()); } cout << "Yes" << endl; return 0; }
#include <bits/stdc++.h> #pragma g++ optimize(2) using namespace std; int n, sum = 1; int tot, root; struct node { int l, r; int dat, val; int size; } f[2000010]; void update(int p) { f[p].size = f[f[p].l].size + f[f[p].r].size + 1; } int New(int val) { f[++tot].val = val; f[tot].dat = rand(); f[tot].size = 1; return tot; } void split(int p, int val, int &x, int &y) { if (p == 0) { x = 0, y = 0; return; } if (f[p].val <= val) { x = p; split(f[p].r, val, f[p].r, y); } else { y = p; split(f[p].l, val, x, f[p].l); } update(p); } int merge(int a, int b) { if (!a || !b) return a + b; if (f[a].dat < f[b].dat) { f[a].r = merge(f[a].r, b); update(a); return a; } else { f[b].l = merge(a, f[b].l); update(b); return b; } } void insert(int val) { int x, y; split(root, val, x, y); root = merge(merge(x, New(val)), y); } void remove(int val) { int x, y, z; split(root, val, x, z); split(x, val - 1, x, y); y = merge(f[y].l, f[y].r); root = merge(merge(x, y), z); } int getRank(int val) { int x, y; split(root, val - 1, x, y); int ans = f[x].size + 1; root = merge(x, y); return ans; } int getVal(int p, int rank) { while (p) { if (f[f[p].l].size >= rank) p = f[p].l; else if (f[f[p].l].size + 1 == rank) return f[p].val; else rank -= f[f[p].l].size + 1, p = f[p].r; } } int getPre(int val) { int x, y; split(root, val - 1, x, y); int ans = getVal(x, f[x].size); root = merge(x, y); return ans; } int getNext(int val) { int x, y; split(root, val, x, y); int ans = getVal(y, 1); root = merge(x, y); return ans; } int a[2000010]; int main() { scanf("%d", &n); for (int i = 1; i <= pow(2, n); i++) { int x; scanf("%d", &x); insert(x); } insert(-1); a[1] = getPre(1999999999); remove(a[1]); for (int i = 0; i < n; i++) { for (int j = 1; j <= pow(2, i); j++) { int x = getPre(a[j]); remove(x); if (x == -1) { cout << "No" << endl; return 0; } tot++, a[tot] = x; } sort(a + 1, a + tot + 1, greater<int>()); } cout << "Yes" << endl; return 0; }
replace
9
10
9
10
0
p02921
C++
Time Limit Exceeded
#include <bits/stdc++.h> // #pragma GCC optimize("Ofast","no-stack-protector","unroll-loops") // #pragma GCC // target("sse,sse2,sse3,ssse3,sse4,sse4.2,popcnt,abm,mmx,avx,tune=naive") // #pragma GCC diagnostic ignored "-W" // #include<bits/extc++.h> // using namespace __gnu_pbds; using namespace std; #define F(i, L, R) for (int i = L; i < R; i++) #define FE(i, L, R) for (int i = L; i <= R; i++) #define getI(a) scanf("%d", &a) #define getII(a, b) scanf("%d%d", &a, &b) #define getIII(a, b, c) scanf("%d%d%d", &a, &b, &c) #define VgetI(n) \ int(n); \ scanf("%d", &(n)) #define VgetII(n, m) \ int(n), (m); \ scanf("%d %d", &(n), &(m)) #define VgetIII(n, m, k) \ int(n), (m), (k); \ scanf("%d %d %d", &(n), &(m), &(k)) #define pb push_back #define pii pair<int, int> #define ll long long #define INF 2000000000 #define PI 3.1415926535897932384626 const ll MOD = 1e9 + 7; char in[1005], in2[5]; int main() { while (scanf("%s%s", in, in2)) { int ans = 0; F(i, 0, 3) if (in[i] == in2[i]) ans++; printf("%d\n", ans); } }
#include <bits/stdc++.h> // #pragma GCC optimize("Ofast","no-stack-protector","unroll-loops") // #pragma GCC // target("sse,sse2,sse3,ssse3,sse4,sse4.2,popcnt,abm,mmx,avx,tune=naive") // #pragma GCC diagnostic ignored "-W" // #include<bits/extc++.h> // using namespace __gnu_pbds; using namespace std; #define F(i, L, R) for (int i = L; i < R; i++) #define FE(i, L, R) for (int i = L; i <= R; i++) #define getI(a) scanf("%d", &a) #define getII(a, b) scanf("%d%d", &a, &b) #define getIII(a, b, c) scanf("%d%d%d", &a, &b, &c) #define VgetI(n) \ int(n); \ scanf("%d", &(n)) #define VgetII(n, m) \ int(n), (m); \ scanf("%d %d", &(n), &(m)) #define VgetIII(n, m, k) \ int(n), (m), (k); \ scanf("%d %d %d", &(n), &(m), &(k)) #define pb push_back #define pii pair<int, int> #define ll long long #define INF 2000000000 #define PI 3.1415926535897932384626 const ll MOD = 1e9 + 7; char in[1005], in2[5]; int main() { scanf("%s%s", in, in2); int ans = 0; F(i, 0, 3) if (in[i] == in2[i]) ans++; printf("%d\n", ans); }
replace
30
35
30
34
TLE
p02921
C++
Runtime Error
#include <algorithm> #include <iomanip> #include <iostream> #include <math.h> #include <numeric> #include <string> #include <vector> using namespace std; int main() { string S; string T; cin >> S; cin >> T; int ans = 0; for (int i = 0; S.size(); i++) { if (S[i] == T[i]) { ans += 1; } } cout << ans << endl; return 0; }
#include <algorithm> #include <iomanip> #include <iostream> #include <math.h> #include <numeric> #include <string> #include <vector> using namespace std; int main() { string S; string T; cin >> S; cin >> T; int ans = 0; for (int i = 0; i < S.size(); i++) { if (S[i] == T[i]) { ans += 1; } } cout << ans << endl; return 0; }
replace
19
20
19
20
-11
p02921
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int ctoi(char c) { if (c >= '0' && c <= '9') { return c - '0'; } return 0; } int main() { string s, t; int count = 0; for (int i = 0; i < 3; i++) { if (s.at(i) == t.at(i)) { count++; } } cout << count; }
#include <bits/stdc++.h> using namespace std; int ctoi(char c) { if (c >= '0' && c <= '9') { return c - '0'; } return 0; } int main() { string s, t; cin >> s >> t; int count = 0; for (int i = 0; i < 3; i++) { if (s.at(i) == t.at(i)) { count++; } } cout << count; }
insert
10
10
10
11
-6
terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::at: __n (which is 0) >= this->size() (which is 0)
p02921
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { string S, T; int count = 0; cin >> S >> T; for (int i = 1; i <= 3; i++) { if (S.at(i) == T.at(i)) { count++; } } cout << count << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string S, T; int count = 0; cin >> S >> T; for (int i = 0; i < 3; i++) { if (S.at(i) == T.at(i)) { count++; } } cout << count << endl; return 0; }
replace
7
8
7
8
-6
terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::at: __n (which is 3) >= this->size() (which is 3)
p02921
C++
Runtime Error
#include <bits/stdc++.h> #include <stdlib.h> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; typedef long long ll; int main() { string s; string t; int ans = 0; for (int i = 0; i < 3; i++) { if (s.at(i) == t.at(i)) { ans++; } } cout << ans << endl; }
#include <bits/stdc++.h> #include <stdlib.h> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; typedef long long ll; int main() { string s; string t; cin >> s >> t; int ans = 0; for (int i = 0; i < 3; i++) { if (s.at(i) == t.at(i)) { ans++; } } cout << ans << endl; }
insert
9
9
9
10
-6
terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::at: __n (which is 0) >= this->size() (which is 0)
p02921
C++
Runtime Error
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; typedef long long ll; int main() { string s, t; int ans = 0; for (int i = 0; i < 3; i++) { if (s.at(i) == t.at(i)) { ans++; } } cout << ans << endl; }
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; typedef long long ll; int main() { string s, t; cin >> s >> t; int ans = 0; for (int i = 0; i < 3; i++) { if (s.at(i) == t.at(i)) { ans++; } } cout << ans << endl; }
insert
9
9
9
10
-6
terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::at: __n (which is 0) >= this->size() (which is 0)
p02921
C++
Time Limit Exceeded
#include <bits/stdc++.h> #include <iostream> #include <string> using namespace std; int main() { string s, t; cin >> s >> t; int ans = 0; for (int i = 0; i, 3; i++) { if (s[i] == t[i]) ans++; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #include <iostream> #include <string> using namespace std; int main() { string s, t; cin >> s >> t; int ans = 0; for (int i = 0; i < 3; i++) { if (s[i] == t[i]) ans++; } cout << ans << endl; return 0; }
replace
11
12
11
12
TLE
p02921
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long #define mp make_pair #define pb push_back #define x first #define y second #define gc getchar_unlocked #define cases \ int t; \ cin >> t; \ while (t--) ll mod = 1e9 + 7; // template<typename T> inline ll pwr(ll base, ll n) { ll ans = 1; while (n > 0) { if (n % 2 == 1) ans = ans * base; base = base * base; n /= 2; } return ans; } struct range { int l, h; }; using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif string s, t; cin >> s >> t; int ct = 0; for (int i = 0; i < s.length(); i++) { if (s[i] == t[i]) ct++; } cout << ct; return 0; }
#include <bits/stdc++.h> #define ll long long #define mp make_pair #define pb push_back #define x first #define y second #define gc getchar_unlocked #define cases \ int t; \ cin >> t; \ while (t--) ll mod = 1e9 + 7; // template<typename T> inline ll pwr(ll base, ll n) { ll ans = 1; while (n > 0) { if (n % 2 == 1) ans = ans * base; base = base * base; n /= 2; } return ans; } struct range { int l, h; }; using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); string s, t; cin >> s >> t; int ct = 0; for (int i = 0; i < s.length(); i++) { if (s[i] == t[i]) ct++; } cout << ct; return 0; }
delete
31
35
31
31
0
p02921
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int cnt = 0; string s, t; for (int i = 0; i < 3; i++) { if (s.at(i) == t.at(i)) cnt++; } cout << cnt << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int cnt = 0; string s, t; cin >> s >> t; for (int i = 0; i < 3; i++) { if (s.at(i) == t.at(i)) cnt++; } cout << cnt << endl; return 0; }
insert
7
7
7
8
-6
terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::at: __n (which is 0) >= this->size() (which is 0)
p02921
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr); string s, t; cin >> s >> t; int count = 0; if (s[0] == t[0]) count++; if (s[1] == t[1]) count++; if (s[2] == t[2]) count++; return count; cout << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr); string s, t; cin >> s >> t; int count = 0; if (s[0] == t[0]) count++; if (s[1] == t[1]) count++; if (s[2] == t[2]) count++; cout << count; cout << '\n'; return 0; }
replace
20
21
20
21
2
p02921
Python
Runtime Error
s = input().split() t = input().split() ans = 0 for i in range(3): if s[i] == t[i]: ans += 1 print(ans)
s = list(input()) t = list(input()) ans = 0 for i in range(3): if s[i] == t[i]: ans += 1 print(ans)
replace
0
2
0
2
IndexError: list index out of range
Traceback (most recent call last): File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02921/Python/s694576581.py", line 6, in <module> if s[i] == t[i]: IndexError: list index out of range
p02921
Python
Runtime Error
s, t = input() print(sum(s[i] == t[i] for i in range(3)))
s = input() t = input() print(sum(s[i] == t[i] for i in range(3)))
replace
0
1
0
2
ValueError: too many values to unpack (expected 2)
Traceback (most recent call last): File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02921/Python/s393555435.py", line 1, in <module> s, t = input() ValueError: too many values to unpack (expected 2)
p02921
Python
Runtime Error
def main(S, T): count = 0 for i in range(len(S)): if T[i] == S[i]: count += 1 return count if __name__ == "__main__": S, T = map(str, input().split()) print(main(S, T))
def main(S, T): count = 0 for i in range(len(S)): if T[i] == S[i]: count += 1 return count if __name__ == "__main__": S = input() T = input() print(main(S, T))
replace
11
12
11
13
ValueError: not enough values to unpack (expected 2, got 1)
Traceback (most recent call last): File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02921/Python/s746445364.py", line 12, in <module> S, T = map(str, input().split()) ValueError: not enough values to unpack (expected 2, got 1)
p02921
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { string s, t; cin >> s, t; int c = 0; for (int i = 0; i < 3; i++) { if (s.at(i) == t.at(i)) { c++; } } cout << c << endl; }
#include <bits/stdc++.h> using namespace std; int main() { string s, t; cin >> s >> t; int c = 0; for (int i = 0; i < 3; i++) { if (s.at(i) == t.at(i)) { c++; } } cout << c << endl; }
replace
5
6
5
6
-6
terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::at: __n (which is 0) >= this->size() (which is 0)
p02921
C++
Runtime Error
#include <algorithm> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <tuple> #include <vector> typedef long long ll; using namespace std; #define rep(i, n) for (int i = 0; i < int(n); i++) #define mod 1000000007 #define fst first #define pb push_back int main() { string S, T; cin >> S >> T; int ans = 0; for (int i = 0; i <= 3; i++) { for (int j = 0; j <= 3; j++) { if (S.at(i) == T.at(i)) { ans++; } } } cout << ans << endl; }
#include <algorithm> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <tuple> #include <vector> typedef long long ll; using namespace std; #define rep(i, n) for (int i = 0; i < int(n); i++) #define mod 1000000007 #define fst first #define pb push_back int main() { string S, T; cin >> S >> T; int ans = 0; for (int i = 0; i <= 2; i++) { if (S.at(i) == T.at(i)) { ans++; } } cout << ans << endl; }
replace
19
24
19
22
-6
terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::at: __n (which is 3) >= this->size() (which is 3)
p02921
C++
Runtime Error
#include <bits/stdc++.h> #define N 100005 #define task "t" #define mu(x) x *x using namespace std; using ll = long long; using pii = pair<int, int>; using piii = pair<int, pii>; const int inf = 1e9 + 7; string s, t; int main() { ios::sync_with_stdio(false); cin.tie(); cout.tie(); #ifndef ONLINE_JUDGE freopen(task ".inp", "r", stdin); freopen(task ".out", "w", stdout); #endif // ONLINE_JUDGE cin >> s >> t; int res = 0; for (int i = 0; i < 3; i++) if (s[i] == t[i]) res++; cout << res; return 0; }
#include <bits/stdc++.h> #define N 100005 #define task "t" #define mu(x) x *x using namespace std; using ll = long long; using pii = pair<int, int>; using piii = pair<int, pii>; const int inf = 1e9 + 7; string s, t; int main() { ios::sync_with_stdio(false); cin.tie(); cout.tie(); #ifndef ONLINE_JUDGE // freopen(task".inp","r",stdin); // freopen(task".out","w",stdout); #endif // ONLINE_JUDGE cin >> s >> t; int res = 0; for (int i = 0; i < 3; i++) if (s[i] == t[i]) res++; cout << res; return 0; }
replace
18
20
18
20
0
p02921
C++
Runtime Error
// ###### ###### ###### ###### // ###### ###### ###### ###### // ###### ###### ###### ###### // ###### ###### ###### ###### // ############ ############ // ############ ############ // ###### ###### ###### ###### // ###### ###### ###### ###### // ###### ###### ###### ###### // ###### ###### ###### ###### #include <bits/stdc++.h> #define int long long #define pb push_back #define ppb pop_back #define si set<int> #define endl '\n' #define fr first #define sc second #define mii map<int, int> #define msi map<string, int> #define mis map<int, string> #define rep(i, a, b) for (int i = a; i < b; i++) #define f(i, n) for (int i = 0; i < n; i++) #define all(v) v.begin(), v.end() // #define sort(v) sort(all(v)) #define sm(v) accumulate(v.begin(), v.end(), 0) #define pii pair<int, int> #define vi vector<int> #define vii vector<pair<int, int>> #define vs vector<string> #define sz(x) (int)x.size() #define M 1000000007 #define bs binary_search #define mp make_pair #define sp(n) setprecision(n) #define spl " " #define arr(a, n) rep(i, 0, n) cin >> a[i] #define mod 998244353 #define MAX 1000000 #define pi 3.1415926536 using namespace std; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int t = 1; // cin>>t; while (t--) { int c = 0; string s, t; cin >> s >> t; rep(i, 0, 3) { if (s[i] == t[i]) c++; } cout << c; } }
// ###### ###### ###### ###### // ###### ###### ###### ###### // ###### ###### ###### ###### // ###### ###### ###### ###### // ############ ############ // ############ ############ // ###### ###### ###### ###### // ###### ###### ###### ###### // ###### ###### ###### ###### // ###### ###### ###### ###### #include <bits/stdc++.h> #define int long long #define pb push_back #define ppb pop_back #define si set<int> #define endl '\n' #define fr first #define sc second #define mii map<int, int> #define msi map<string, int> #define mis map<int, string> #define rep(i, a, b) for (int i = a; i < b; i++) #define f(i, n) for (int i = 0; i < n; i++) #define all(v) v.begin(), v.end() // #define sort(v) sort(all(v)) #define sm(v) accumulate(v.begin(), v.end(), 0) #define pii pair<int, int> #define vi vector<int> #define vii vector<pair<int, int>> #define vs vector<string> #define sz(x) (int)x.size() #define M 1000000007 #define bs binary_search #define mp make_pair #define sp(n) setprecision(n) #define spl " " #define arr(a, n) rep(i, 0, n) cin >> a[i] #define mod 998244353 #define MAX 1000000 #define pi 3.1415926536 using namespace std; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); #endif int t = 1; // cin>>t; while (t--) { int c = 0; string s, t; cin >> s >> t; rep(i, 0, 3) { if (s[i] == t[i]) c++; } cout << c; } }
replace
47
49
47
49
0
p02921
C++
Runtime Error
/* while(!cin.eof()) */ #include <bits/stdc++.h> using namespace std; #define int long long #define mod (int)(1e9 + 7) #define MAXI (int)(1e18 + 100) #define N 1000200 int fre[N]; // Driver code to test above functions int32_t main() { #ifndef ONLINE_JUDGE // for getting input from inpu.txt freopen("input.txt", "r", stdin); // for writing output to output.txt // freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); string s, t; cin >> s >> t; int ans = 0; for (int i = 0; i < 3; i++) if (s[i] == t[i]) ans++; cout << ans; return 0; }
/* while(!cin.eof()) */ #include <bits/stdc++.h> using namespace std; #define int long long #define mod (int)(1e9 + 7) #define MAXI (int)(1e18 + 100) #define N 1000200 int fre[N]; // Driver code to test above functions int32_t main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); string s, t; cin >> s >> t; int ans = 0; for (int i = 0; i < 3; i++) if (s[i] == t[i]) ans++; cout << ans; return 0; }
delete
13
20
13
13
0
p02921
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) typedef pair<int, int> P; string s, t; int main() { int cnt = 0; rep(i, 3) { if (s.at(i) == t.at(i)) { cnt++; } } cout << cnt << 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; string s, t; int main() { cin >> s >> t; int cnt = 0; rep(i, 3) { if (s.at(i) == t.at(i)) { cnt++; } } cout << cnt << endl; return 0; }
insert
8
8
8
9
-6
terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::at: __n (which is 0) >= this->size() (which is 0)