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 |
|---|---|---|---|---|---|---|---|---|---|---|---|
p02707 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
long long n, m;
int tem, cnt[maxn] = {0};
int read() {
int x = 0, w = 1;
char ch = 0;
while (ch < '0' || ch > '9') {
if (ch == '-')
w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + (ch - '0');
ch = getchar();
}
return x * w;
}
inline void write(int x) {
static int sta[35];
int top = 0;
do {
sta[top++] = x % 10, x /= 10;
} while (x);
while (top)
putchar(sta[--top] + 48);
}
int main() {
n = read();
m = n;
n--;
while (n--) {
tem = read();
cnt[tem]++;
}
for (int i = 1; i <= m; i++) {
printf("%d\n", cnt[i]);
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
const int maxn = 200010;
long long n, m;
int tem, cnt[maxn] = {0};
int read() {
int x = 0, w = 1;
char ch = 0;
while (ch < '0' || ch > '9') {
if (ch == '-')
w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + (ch - '0');
ch = getchar();
}
return x * w;
}
inline void write(int x) {
static int sta[35];
int top = 0;
do {
sta[top++] = x % 10, x /= 10;
} while (x);
while (top)
putchar(sta[--top] + 48);
}
int main() {
n = read();
m = n;
n--;
while (n--) {
tem = read();
cnt[tem]++;
}
for (int i = 1; i <= m; i++) {
printf("%d\n", cnt[i]);
}
return 0;
} | replace | 4 | 5 | 4 | 5 | 0 | |
p02707 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <iostream>
#include <math.h>
#include <string.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define sf scanf
#define pf printf
#define srev reverse(s.begin(), s.end())
#define sortstr sort(s.begin(), s.end());
#define gtl(x) getline(cin, (x))
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) ((a) * ((b) / gcd(a, b)))
#define PI acos(-1)
#define Precision(a) cout << fixed << setprecision(a)
#define FasterIO \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
template <typename T> T Sqr(T x) {
T n = x * x;
return n;
}
template <typename T> T Pow(T B, T P) {
if (P == 0)
return 1;
if (P & 1)
return B * Pow(B, P - 1);
else
return Sqr(Pow(B, P / 2));
}
template <typename T> T Abs(T a) {
if (a < 0)
return -a;
else
return a;
}
template <typename T> T Gcd(T a, T b) {
if (a < 0)
return Gcd(-a, b);
if (b < 0)
return Gcd(a, -b);
return (b == 0) ? a : Gcd(b, a % b);
}
template <typename T> T Lcm(T a, T b) {
if (a < 0)
return Lcm(-a, b);
if (b < 0)
return Lcm(a, -b);
return a * (b / Gcd(a, b));
}
// scanf("%[^\n]%*c", str);
// sort(s,s+n,greater<int>());
int main() {
int n, a;
int s[100000];
cin >> n;
for (int i = 2; i <= n; i++) {
cin >> a;
s[a]++;
}
for (int i = 1; i <= n; i++) {
cout << s[i] << endl;
}
return 0;
}
| #include <bits/stdc++.h>
#include <iostream>
#include <math.h>
#include <string.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define sf scanf
#define pf printf
#define srev reverse(s.begin(), s.end())
#define sortstr sort(s.begin(), s.end());
#define gtl(x) getline(cin, (x))
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) ((a) * ((b) / gcd(a, b)))
#define PI acos(-1)
#define Precision(a) cout << fixed << setprecision(a)
#define FasterIO \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
template <typename T> T Sqr(T x) {
T n = x * x;
return n;
}
template <typename T> T Pow(T B, T P) {
if (P == 0)
return 1;
if (P & 1)
return B * Pow(B, P - 1);
else
return Sqr(Pow(B, P / 2));
}
template <typename T> T Abs(T a) {
if (a < 0)
return -a;
else
return a;
}
template <typename T> T Gcd(T a, T b) {
if (a < 0)
return Gcd(-a, b);
if (b < 0)
return Gcd(a, -b);
return (b == 0) ? a : Gcd(b, a % b);
}
template <typename T> T Lcm(T a, T b) {
if (a < 0)
return Lcm(-a, b);
if (b < 0)
return Lcm(a, -b);
return a * (b / Gcd(a, b));
}
// scanf("%[^\n]%*c", str);
// sort(s,s+n,greater<int>());
int main() {
int n, a;
int s[1000000];
cin >> n;
for (int i = 2; i <= n; i++) {
cin >> a;
s[a]++;
}
for (int i = 1; i <= n; i++) {
cout << s[i] << endl;
}
return 0;
}
| replace | 60 | 61 | 60 | 61 | 0 | |
p02707 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
typedef long long ll;
int main() {
ll N;
cin >> N;
vector<ll> ans(100100, 0);
for (ll i = 1; i < N; ++i) {
ll temp;
cin >> temp;
++ans[temp - 1];
}
for (ll i = 0; i < N; ++i) {
cout << ans[i] << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
typedef long long ll;
int main() {
ll N;
cin >> N;
vector<ll> ans(200100, 0);
for (ll i = 1; i < N; ++i) {
ll temp;
cin >> temp;
++ans[temp - 1];
}
for (ll i = 0; i < N; ++i) {
cout << ans[i] << endl;
}
} | replace | 8 | 9 | 8 | 9 | 0 | |
p02707 | C++ | Runtime Error | #include <iostream>
using namespace std;
typedef long long ll;
int main() {
int N;
int b[20000] = {0};
cin >> N;
for (int i = 1; i < N; i++) {
int tmp;
cin >> tmp;
b[tmp - 1]++;
}
for (int i = 0; i < N; i++)
cout << b[i] << endl;
return 0;
} | #include <iostream>
using namespace std;
typedef long long ll;
int main() {
int N;
int b[200000] = {0};
cin >> N;
for (int i = 1; i < N; i++) {
int tmp;
cin >> tmp;
b[tmp - 1]++;
}
for (int i = 0; i < N; i++)
cout << b[i] << endl;
return 0;
} | replace | 7 | 8 | 7 | 8 | 0 | |
p02707 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int nmax = 20005;
int v[nmax];
int main() {
int n;
cin >> n;
for (int i = 2; i <= n; i++) {
int x;
cin >> x;
if (x < i)
v[x]++;
}
for (int i = 1; i <= n; i++)
cout << v[i] << "\n";
}
| #include <bits/stdc++.h>
using namespace std;
const int nmax = 200005;
int v[nmax];
int main() {
int n;
cin >> n;
for (int i = 2; i <= n; i++) {
int x;
cin >> x;
if (x < i)
v[x]++;
}
for (int i = 1; i <= n; i++)
cout << v[i] << "\n";
}
| replace | 2 | 3 | 2 | 3 | 0 | |
p02707 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define debug(n) cerr << #n << ':' << n << endl;
#define dline cerr << __LINE__ << endl;
using ll = long long;
using ull = unsigned long long;
template <class T, class U> using P = pair<T, U>;
template <class T> using Heap = priority_queue<T>;
template <class T> using heaP = priority_queue<T, vector<T>, greater<T>>;
template <class T, class U> using umap = unordered_map<T, U>;
template <class T> using uset = unordered_set<T>;
template <class T> bool ChangeMax(T &a, const T &b) {
if (a >= b)
return false;
a = b;
return true;
}
template <class T> bool ChangeMin(T &a, const T &b) {
if (a <= b)
return false;
a = b;
return true;
}
template <class T, size_t N, class U> void Fill(T (&a)[N], const U &v) {
fill((U *)a, (U *)(a + N), v);
}
template <class T> istream &operator>>(istream &is, vector<T> &v) {
for (auto &e : v)
is >> e;
return is;
}
int main() {
int n;
cin >> n;
vector<int> v(n);
cin >> v;
vector<int> ans(n);
for (auto e : v) {
ans[e - 1]++;
}
for (auto e : ans)
cout << e << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define debug(n) cerr << #n << ':' << n << endl;
#define dline cerr << __LINE__ << endl;
using ll = long long;
using ull = unsigned long long;
template <class T, class U> using P = pair<T, U>;
template <class T> using Heap = priority_queue<T>;
template <class T> using heaP = priority_queue<T, vector<T>, greater<T>>;
template <class T, class U> using umap = unordered_map<T, U>;
template <class T> using uset = unordered_set<T>;
template <class T> bool ChangeMax(T &a, const T &b) {
if (a >= b)
return false;
a = b;
return true;
}
template <class T> bool ChangeMin(T &a, const T &b) {
if (a <= b)
return false;
a = b;
return true;
}
template <class T, size_t N, class U> void Fill(T (&a)[N], const U &v) {
fill((U *)a, (U *)(a + N), v);
}
template <class T> istream &operator>>(istream &is, vector<T> &v) {
for (auto &e : v)
is >> e;
return is;
}
int main() {
int n;
cin >> n;
vector<int> v(n - 1);
cin >> v;
vector<int> ans(n);
for (auto e : v) {
ans[e - 1]++;
}
for (auto e : ans)
cout << e << endl;
return 0;
}
| replace | 42 | 43 | 42 | 43 | -6 | double free or corruption (out)
|
p02707 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
const int INF = 1e9;
int main() {
ll n;
cin >> n;
vector<ll> a(n), ans(n);
for (int i = 2; i <= n; ++i)
cin >> a[i], ans[a[i]]++;
for (int i = 1; i <= n; ++i)
cout << ans[i] << endl;
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
const int INF = 1e9;
int main() {
ll n;
cin >> n;
vector<ll> a(n + 5), ans(n + 5);
for (int i = 2; i <= n; ++i)
cin >> a[i], ans[a[i]]++;
for (int i = 1; i <= n; ++i)
cout << ans[i] << endl;
return 0;
}
| replace | 10 | 11 | 10 | 11 | -6 | munmap_chunk(): invalid pointer
|
p02707 | C++ | Runtime Error | #include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
using namespace std;
using dou = long double;
string yes = "yes";
string Yes = "Yes";
string YES = "YES";
string no = "no";
string No = "No";
string NO = "NO";
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
// const ll mod = 1000000007;
const ll mod = 1000003;
struct mint {
ll x; // typedef long long ll;
mint(ll x = 0) : x((x % mod + mod) % mod) {}
mint operator-() const { return mint(-x); }
mint &operator+=(const mint a) {
if ((x += a.x) >= mod)
x -= mod;
return *this;
}
mint &operator-=(const mint a) {
if ((x += mod - a.x) >= mod)
x -= mod;
return *this;
}
mint &operator*=(const mint a) {
(x *= a.x) %= mod;
return *this;
}
mint operator+(const mint a) const { return mint(*this) += a; }
mint operator-(const mint a) const { return mint(*this) -= a; }
mint operator*(const mint a) const { return mint(*this) *= a; }
mint pow(ll t) const {
if (!t)
return 1;
mint a = pow(t >> 1);
a *= a;
if (t & 1)
a *= *this;
return a;
}
// for prime mod
mint inv() const { return pow(mod - 2); }
mint &operator/=(const mint a) { return *this *= a.inv(); }
mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define brep(n) for (int bit = 0; bit < (1 << n); bit++)
#define bbrep(n) for (int bbit = 0; bbit < (1 << n); bbit++)
#define erep(i, container) for (auto &i : container)
#define itrep(i, container) for (auto i : container)
#define irep(i, n) for (ll i = n - 1; i >= (ll)0ll; i--)
#define rrep(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define reprep(i, j, h, w) rep(i, h) rep(j, w)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define VEC(type, name, n) \
std::vector<type> name(n); \
rep(i, n) std::cin >> name[i];
#define pb push_back
#define pf push_front
#define query \
int qq; \
std::cin >> qq; \
rep(qqq, qq)
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define itn int
#define mp make_pair
#define sum(a) accumulate(all(a), 0ll)
#define keta fixed << setprecision
#define vout(a) \
erep(qxqxqx, a) std::cout << qxqxqx << ' '; \
std::cout << std::endl;
#define vvector(name, typ, m, n, a) \
vector<vector<typ>> name(m, vector<typ>(n, a))
// #define vvector(name,typ,m,n)vector<vector<typ> > name(m,vector<typ> (n))
#define vvvector(name, t, l, m, n, a) \
vector<vector<vector<t>>> name(l, vector<vector<t>>(m, vector<t>(n, a)));
#define vvvvector(name, t, k, l, m, n, a) \
vector<vector<vector<vector<t>>>> name( \
k, vector<vector<vector<t>>>(l, vector<vector<t>>(m, vector<t>(n, a))));
#define case std::cout << "Case #" << qqq + 1 << ": "
#define res resize
#define as assign
#define ffor for (;;)
#define ppri(a, b) std::cout << a << " " << b << std::endl
#define pppri(a, b, c) std::cout << a << " " << b << " " << c << std::endl
#define aall(x, n) (x).begin(), (x).begin() + (n)
#define ssum(a) accumulate(a, 0ll)
#define stirng string
// #define grid_input(a,type) int h,w;std::cin >>
// h>>w;vvector(a,type,h,w,0);reprep(i,j,h,w)std::cin >> a[i][j];
// typedef long long T;
ll ceil(ll a, ll b) { return ((a + b - 1) / b); }
const int INF = 2000000000;
// const ll INF64 =3223372036854775807ll;
// const ll INF64 = 9223372036854775807ll;
const ll INF64 = 9223372036854775ll;
// const ll MOD = 1000000007ll;
const ll MOD = 1000003ll;
const ll OD = 1000000000000007ll;
const dou pi = 3.141592653589793;
long long modpow(long long a, long long n) { // 累乗の余剰
long long res = 1;
while (n > 0) {
if (n & 1)
res = res * a % MOD;
a = a * a % MOD;
n >>= 1;
}
return res;
}
// メモ
// tuple<hoge,huga,foo> tのget関数はget<i>(t)
// resizeはメンバ関数
// 三項演算子を覚えよう
// a?b:cと書くと aならばbの処理をする
// i本目にbitをたてる -> (1<<i)
int main() {
int n;
std::cin >> n;
VEC(int, a, n - 1);
std::vector<int> c(n);
rep(i, n) c[a[i] - 1]++;
rep(i, n) { std::cout << c[i] << std::endl; }
} | #include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
using namespace std;
using dou = long double;
string yes = "yes";
string Yes = "Yes";
string YES = "YES";
string no = "no";
string No = "No";
string NO = "NO";
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
// const ll mod = 1000000007;
const ll mod = 1000003;
struct mint {
ll x; // typedef long long ll;
mint(ll x = 0) : x((x % mod + mod) % mod) {}
mint operator-() const { return mint(-x); }
mint &operator+=(const mint a) {
if ((x += a.x) >= mod)
x -= mod;
return *this;
}
mint &operator-=(const mint a) {
if ((x += mod - a.x) >= mod)
x -= mod;
return *this;
}
mint &operator*=(const mint a) {
(x *= a.x) %= mod;
return *this;
}
mint operator+(const mint a) const { return mint(*this) += a; }
mint operator-(const mint a) const { return mint(*this) -= a; }
mint operator*(const mint a) const { return mint(*this) *= a; }
mint pow(ll t) const {
if (!t)
return 1;
mint a = pow(t >> 1);
a *= a;
if (t & 1)
a *= *this;
return a;
}
// for prime mod
mint inv() const { return pow(mod - 2); }
mint &operator/=(const mint a) { return *this *= a.inv(); }
mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define brep(n) for (int bit = 0; bit < (1 << n); bit++)
#define bbrep(n) for (int bbit = 0; bbit < (1 << n); bbit++)
#define erep(i, container) for (auto &i : container)
#define itrep(i, container) for (auto i : container)
#define irep(i, n) for (ll i = n - 1; i >= (ll)0ll; i--)
#define rrep(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define reprep(i, j, h, w) rep(i, h) rep(j, w)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define VEC(type, name, n) \
std::vector<type> name(n); \
rep(i, n) std::cin >> name[i];
#define pb push_back
#define pf push_front
#define query \
int qq; \
std::cin >> qq; \
rep(qqq, qq)
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define itn int
#define mp make_pair
#define sum(a) accumulate(all(a), 0ll)
#define keta fixed << setprecision
#define vout(a) \
erep(qxqxqx, a) std::cout << qxqxqx << ' '; \
std::cout << std::endl;
#define vvector(name, typ, m, n, a) \
vector<vector<typ>> name(m, vector<typ>(n, a))
// #define vvector(name,typ,m,n)vector<vector<typ> > name(m,vector<typ> (n))
#define vvvector(name, t, l, m, n, a) \
vector<vector<vector<t>>> name(l, vector<vector<t>>(m, vector<t>(n, a)));
#define vvvvector(name, t, k, l, m, n, a) \
vector<vector<vector<vector<t>>>> name( \
k, vector<vector<vector<t>>>(l, vector<vector<t>>(m, vector<t>(n, a))));
#define case std::cout << "Case #" << qqq + 1 << ": "
#define res resize
#define as assign
#define ffor for (;;)
#define ppri(a, b) std::cout << a << " " << b << std::endl
#define pppri(a, b, c) std::cout << a << " " << b << " " << c << std::endl
#define aall(x, n) (x).begin(), (x).begin() + (n)
#define ssum(a) accumulate(a, 0ll)
#define stirng string
// #define grid_input(a,type) int h,w;std::cin >>
// h>>w;vvector(a,type,h,w,0);reprep(i,j,h,w)std::cin >> a[i][j];
// typedef long long T;
ll ceil(ll a, ll b) { return ((a + b - 1) / b); }
const int INF = 2000000000;
// const ll INF64 =3223372036854775807ll;
// const ll INF64 = 9223372036854775807ll;
const ll INF64 = 9223372036854775ll;
// const ll MOD = 1000000007ll;
const ll MOD = 1000003ll;
const ll OD = 1000000000000007ll;
const dou pi = 3.141592653589793;
long long modpow(long long a, long long n) { // 累乗の余剰
long long res = 1;
while (n > 0) {
if (n & 1)
res = res * a % MOD;
a = a * a % MOD;
n >>= 1;
}
return res;
}
// メモ
// tuple<hoge,huga,foo> tのget関数はget<i>(t)
// resizeはメンバ関数
// 三項演算子を覚えよう
// a?b:cと書くと aならばbの処理をする
// i本目にbitをたてる -> (1<<i)
int main() {
int n;
std::cin >> n;
VEC(int, a, n - 1);
std::vector<int> c(n);
rep(i, n - 1) c[a[i] - 1]++;
rep(i, n) { std::cout << c[i] << std::endl; }
} | replace | 152 | 153 | 152 | 153 | -6 | double free or corruption (out)
|
p02707 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main(void) {
// Your code here!
int n;
vector<int> data(20001, 0);
cin >> n;
int x;
for (int i = 0; i < n - 1; i++) {
cin >> x;
data.at(x - 1) += 1;
}
for (int i = 0; i < n; i++) {
cout << data.at(i) << endl;
}
}
| #include <bits/stdc++.h>
using namespace std;
int main(void) {
// Your code here!
int n;
vector<int> data(200001, 0);
cin >> n;
int x;
for (int i = 0; i < n - 1; i++) {
cin >> x;
data.at(x - 1) += 1;
}
for (int i = 0; i < n; i++) {
cout << data.at(i) << endl;
}
}
| replace | 5 | 6 | 5 | 6 | 0 | |
p02707 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n, i, x;
cin >> n;
vector<int> a(n);
for (i = 0; i < n; i++) {
cin >> x;
x--;
a[x]++;
}
for (i = 0; i < n; i++)
cout << a[i] << "\n";
} | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n, i, x;
cin >> n;
vector<int> a(n);
for (i = 0; i < n - 1; i++) {
cin >> x;
x--;
a[x]++;
}
for (i = 0; i < n; i++)
cout << a[i] << "\n";
}
| replace | 6 | 7 | 6 | 7 | 0 | |
p02707 | C++ | Runtime Error | #include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <deque> // deque
#include <iostream> // cout, endl, cin
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <string> // string, to_string, stoi
#include <tuple> // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility> // pair, make_pair
#include <vector> // vector
using namespace std;
int main() {
int N;
cin >> N;
int A[20010];
int ans[20010];
for (int i = 2; i <= N; i++) {
cin >> A[i];
}
for (int i = 2; i <= N; i++) {
ans[A[i]]++;
}
for (int i = 1; i <= N; i++) {
cout << ans[i] << endl;
}
return 0;
} | #include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <deque> // deque
#include <iostream> // cout, endl, cin
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <string> // string, to_string, stoi
#include <tuple> // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility> // pair, make_pair
#include <vector> // vector
using namespace std;
int main() {
int N;
cin >> N;
int A[200010];
int ans[200010];
for (int i = 2; i <= N; i++) {
cin >> A[i];
}
for (int i = 2; i <= N; i++) {
ans[A[i]]++;
}
for (int i = 1; i <= N; i++) {
cout << ans[i] << endl;
}
return 0;
} | replace | 22 | 24 | 22 | 24 | 0 | |
p02707 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int a;
cin >> a;
vector<int> c(a);
c[a] = 0;
for (int i = 0; i < a - 1; i++) {
int b;
cin >> b;
c[b - 1]++;
}
for (int i = 0; i < a; i++) {
cout << c[i] << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int a;
cin >> a;
vector<int> c(a);
for (int i = 0; i < a; i++) {
c[i] = 0;
}
for (int i = 0; i < a - 1; i++) {
int b;
cin >> b;
c[b - 1]++;
}
for (int i = 0; i < a; i++) {
cout << c[i] << endl;
}
return 0;
} | replace | 7 | 8 | 7 | 10 | 0 | |
p02707 | C++ | Runtime Error | #define _GLIBCXX_DEBUG // TLEの原因になるので注意!!!!!!!!!!!
#include <bits/stdc++.h>
#include <cmath>
using ll = long long;
using namespace std;
vector<int> arr;
stack<int> st;
queue<int> qu;
queue<pair<int, int>> qu2;
priority_queue<int> pq;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, n) for (int i = 1; i <= (int)(n); i++)
#define mins(x, y) x = min(x, y)
#define maxs(x, y) x = max(x, y)
typedef set<int> set_t;
typedef set<string> set_g;
typedef complex<double> xy_t;
static const int NIL = -1;
static const int INF = 1000000007;
#define mp make_pair
#define sz(x) int(x.sise())
#define mod 1000000007
#define reps(i, s, n) for (int i = s; i < n; i++)
#define Rreps(i, n, e) for (int i = n - 1; i >= e; --i)
#define Rrep(i, n) Rreps(i, n, 0)
deque<int> deq;
#define fi first
#define se second
// #define int long long
// cerrはコメントアウトしなくても通る//ただしTLEを引き起こすことがある
// const ll MOD = 998244353;
const ll MOD = (1e+9) + 7;
// const ll INF = 1LL << 60;
// typedef pair<ll,ll> P;
typedef pair<int, int> P;
bool chmx(int &a, int b) {
if (b > a) {
a = b;
return true;
}
return false;
}
bool chmn(int &a, int b) {
if (b < a) {
a = b;
return true;
}
return false;
}
int A[20005];
int main() {
int n;
cin >> n;
rep(i, n - 1) {
int a;
cin >> a;
a--;
A[a]++;
}
rep(i, n) { cout << A[i] << endl; }
return 0;
}
| #define _GLIBCXX_DEBUG // TLEの原因になるので注意!!!!!!!!!!!
#include <bits/stdc++.h>
#include <cmath>
using ll = long long;
using namespace std;
vector<int> arr;
stack<int> st;
queue<int> qu;
queue<pair<int, int>> qu2;
priority_queue<int> pq;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, n) for (int i = 1; i <= (int)(n); i++)
#define mins(x, y) x = min(x, y)
#define maxs(x, y) x = max(x, y)
typedef set<int> set_t;
typedef set<string> set_g;
typedef complex<double> xy_t;
static const int NIL = -1;
static const int INF = 1000000007;
#define mp make_pair
#define sz(x) int(x.sise())
#define mod 1000000007
#define reps(i, s, n) for (int i = s; i < n; i++)
#define Rreps(i, n, e) for (int i = n - 1; i >= e; --i)
#define Rrep(i, n) Rreps(i, n, 0)
deque<int> deq;
#define fi first
#define se second
// #define int long long
// cerrはコメントアウトしなくても通る//ただしTLEを引き起こすことがある
// const ll MOD = 998244353;
const ll MOD = (1e+9) + 7;
// const ll INF = 1LL << 60;
// typedef pair<ll,ll> P;
typedef pair<int, int> P;
bool chmx(int &a, int b) {
if (b > a) {
a = b;
return true;
}
return false;
}
bool chmn(int &a, int b) {
if (b < a) {
a = b;
return true;
}
return false;
}
int A[200005];
int main() {
int n;
cin >> n;
rep(i, n - 1) {
int a;
cin >> a;
a--;
A[a]++;
}
rep(i, n) { cout << A[i] << endl; }
return 0;
}
| replace | 53 | 54 | 53 | 54 | 0 | |
p02707 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <queue>
using namespace std;
int main(void) {
int n, i, a, cnt[100001];
scanf("%d", &n);
for (i = 1; i <= n; i++)
cnt[i] = 0;
for (i = 2; i <= n; i++) {
scanf("%d", &a);
cnt[a]++;
}
for (i = 1; i <= n; i++)
printf("%d\n", cnt[i]);
return 0;
} | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <queue>
using namespace std;
int main(void) {
int n, i, a, cnt[200001];
scanf("%d", &n);
for (i = 1; i <= n; i++)
cnt[i] = 0;
for (i = 2; i <= n; i++) {
scanf("%d", &a);
cnt[a]++;
}
for (i = 1; i <= n; i++)
printf("%d\n", cnt[i]);
return 0;
} | replace | 8 | 9 | 8 | 9 | 0 | |
p02707 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
const int N = 1e5 + 7;
vector<int> g[N];
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
for (int i = 1; i < n; i++) {
int p;
cin >> p;
p--;
g[p].push_back(i);
}
for (int i = 0; i < n; i++) {
cout << g[i].size() << ' ';
}
cout << '\n';
}
| #include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
const int N = 3e5 + 7;
vector<int> g[N];
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
for (int i = 1; i < n; i++) {
int p;
cin >> p;
p--;
g[p].push_back(i);
}
for (int i = 0; i < n; i++) {
cout << g[i].size() << ' ';
}
cout << '\n';
}
| replace | 25 | 26 | 25 | 26 | 0 | |
p02707 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> A(N - 1);
for (int i = 0; i < N - 1; i++) {
cin >> A.at(i);
}
vector<int> buka(N);
for (int i = 0; i < N; i++) {
buka.at(i) = 0;
}
for (int i = 0; i < N - 1; i++) {
for (int j = 1; j < N + 1; j++) {
if (j == A.at(i)) {
buka.at(j - 1)++;
}
}
}
for (int i = 0; i < N; i++) {
cout << buka.at(i) << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> A(N - 1);
for (int i = 0; i < N - 1; i++) {
cin >> A.at(i);
}
vector<int> buka(N);
for (int i = 0; i < N; i++) {
buka.at(i) = 0;
}
for (int i = 0; i < N - 1; i++) {
buka.at(A.at(i) - 1)++;
}
for (int i = 0; i < N; i++) {
cout << buka.at(i) << endl;
}
} | replace | 17 | 22 | 17 | 18 | TLE | |
p02707 | C++ | Runtime Error | #include <algorithm>
#include <complex>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define INF 100000000
#define rep(i, a) for (int i = 0; i < (a); i++)
using namespace std;
using P = pair<int, int>;
/*
const int MAX = 1000000;
const int MOD = 1000000007;
long long fac[MAX], finv[MAX], inv[MAX];
//テーブルの前処理
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++){
fac[i] = fac[i - 1] * i % MOD;
//そのまま計算すると負の値になるのでMODを足す
inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
// 二項係数計算
long long COM(int n, int k){
if (n < k) return 0;
if (n < 0 || k < 0) return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
//二分累乗法
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;
}
*/
/*
//UnionFind木
struct UnionFind{
vector<long long int> par; //par[i] : iの親の番号
vector<long long int> rnk; //root[i] : iの木のサイズ
//コンストラクタ
UnionFind(long long int n): par(n), rnk(n){
//木の初期化
for(long long int i = 0; i < n; i++){
par[i] = i;
rnk[i] = 1;
}
}
//データxが属する木の根を再帰で取得
long long int root(long long int x){
if(par[x] == x){
return x;
}else{
return par[x] = root(par[x]);
}
}
//xとyが同じ木に属しているかを判定
bool same(long long int x, long long int y){
return root(x) == root(y);
}
//xとyの木を併合
void unite(long long int x, long long int y){
long long int rx = root(x); //xの根
long long int ry = root(y); //yの根
if(rx == ry) return; //根が同じならそのまま
//併合するときにサイズを足し合わせる
if(rnk[rx] < rnk[ry]){
par[rx] = ry;
rnk[ry] += rnk[rx];
}else{
par[ry] = rx;
rnk[rx] += rnk[ry];
}
}
//属している木のサイズを取得
long long int size(long long int x){
return rnk[root(x)];
}
};
*/
/*
//重み付きUnionFind
template<class Abel> struct UnionFind {
vector<int> par;
vector<int> rank;
vector<Abel> diff_weight;
UnionFind(int n = 1, Abel SUM_UNITY = 0) {
init(n, SUM_UNITY);
}
void init(int n = 1, Abel SUM_UNITY = 0) {
par.resize(n); rank.resize(n); diff_weight.resize(n);
for (int i = 0; i < n; ++i) par[i] = i, rank[i] = 0, diff_weight[i] =
SUM_UNITY;
}
int root(int x) {
if (par[x] == x) {
return x;
}
else {
int r = root(par[x]);
diff_weight[x] += diff_weight[par[x]];
return par[x] = r;
}
}
Abel weight(int x) {
root(x);
return diff_weight[x];
}
bool issame(int x, int y) {
return root(x) == root(y);
}
bool merge(int x, int y, Abel w) {
w += weight(x); w -= weight(y);
x = root(x); y = root(y);
if (x == y) return false;
if (rank[x] < rank[y]) swap(x, y), w = -w;
if (rank[x] == rank[y]) ++rank[x];
par[y] = x;
diff_weight[y] = w;
return true;
}
Abel diff(int x, int y) {
return weight(y) - weight(x);
}
};
*/
/*
//ダイクストラ
typedef struct edge{
int to;
int cost;
} edge;
vector<edge> graph[2010];
long long int d[2010];
void dijkstra(int s){
priority_queue<P, vector<P>, greater<P> > que;
for(int i = 0; i < 2010; i++){
d[i] = INF;
}
d[s] = 0;
que.push(P(s, 0));
while (!que.empty()) {
P p = que.top();
que.pop();
int v = p.first;
if (d[v] < p.second) continue;
for (int i=0; i<graph[v].size(); ++i) {
edge e = graph[v][i];
if (d[e.to] > d[v] + e.cost) {
d[e.to] = d[v] + e.cost;
que.push(P(e.to, d[e.to]));
}
}
}
}
*/
int main() {
int n;
cin >> n;
int data[20010] = {0};
for (int i = 2; i <= n; i++) {
int a;
cin >> a;
data[a]++;
}
for (int i = 1; i <= n; i++) {
cout << data[i] << endl;
}
}
| #include <algorithm>
#include <complex>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define INF 100000000
#define rep(i, a) for (int i = 0; i < (a); i++)
using namespace std;
using P = pair<int, int>;
/*
const int MAX = 1000000;
const int MOD = 1000000007;
long long fac[MAX], finv[MAX], inv[MAX];
//テーブルの前処理
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++){
fac[i] = fac[i - 1] * i % MOD;
//そのまま計算すると負の値になるのでMODを足す
inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
// 二項係数計算
long long COM(int n, int k){
if (n < k) return 0;
if (n < 0 || k < 0) return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
//二分累乗法
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;
}
*/
/*
//UnionFind木
struct UnionFind{
vector<long long int> par; //par[i] : iの親の番号
vector<long long int> rnk; //root[i] : iの木のサイズ
//コンストラクタ
UnionFind(long long int n): par(n), rnk(n){
//木の初期化
for(long long int i = 0; i < n; i++){
par[i] = i;
rnk[i] = 1;
}
}
//データxが属する木の根を再帰で取得
long long int root(long long int x){
if(par[x] == x){
return x;
}else{
return par[x] = root(par[x]);
}
}
//xとyが同じ木に属しているかを判定
bool same(long long int x, long long int y){
return root(x) == root(y);
}
//xとyの木を併合
void unite(long long int x, long long int y){
long long int rx = root(x); //xの根
long long int ry = root(y); //yの根
if(rx == ry) return; //根が同じならそのまま
//併合するときにサイズを足し合わせる
if(rnk[rx] < rnk[ry]){
par[rx] = ry;
rnk[ry] += rnk[rx];
}else{
par[ry] = rx;
rnk[rx] += rnk[ry];
}
}
//属している木のサイズを取得
long long int size(long long int x){
return rnk[root(x)];
}
};
*/
/*
//重み付きUnionFind
template<class Abel> struct UnionFind {
vector<int> par;
vector<int> rank;
vector<Abel> diff_weight;
UnionFind(int n = 1, Abel SUM_UNITY = 0) {
init(n, SUM_UNITY);
}
void init(int n = 1, Abel SUM_UNITY = 0) {
par.resize(n); rank.resize(n); diff_weight.resize(n);
for (int i = 0; i < n; ++i) par[i] = i, rank[i] = 0, diff_weight[i] =
SUM_UNITY;
}
int root(int x) {
if (par[x] == x) {
return x;
}
else {
int r = root(par[x]);
diff_weight[x] += diff_weight[par[x]];
return par[x] = r;
}
}
Abel weight(int x) {
root(x);
return diff_weight[x];
}
bool issame(int x, int y) {
return root(x) == root(y);
}
bool merge(int x, int y, Abel w) {
w += weight(x); w -= weight(y);
x = root(x); y = root(y);
if (x == y) return false;
if (rank[x] < rank[y]) swap(x, y), w = -w;
if (rank[x] == rank[y]) ++rank[x];
par[y] = x;
diff_weight[y] = w;
return true;
}
Abel diff(int x, int y) {
return weight(y) - weight(x);
}
};
*/
/*
//ダイクストラ
typedef struct edge{
int to;
int cost;
} edge;
vector<edge> graph[2010];
long long int d[2010];
void dijkstra(int s){
priority_queue<P, vector<P>, greater<P> > que;
for(int i = 0; i < 2010; i++){
d[i] = INF;
}
d[s] = 0;
que.push(P(s, 0));
while (!que.empty()) {
P p = que.top();
que.pop();
int v = p.first;
if (d[v] < p.second) continue;
for (int i=0; i<graph[v].size(); ++i) {
edge e = graph[v][i];
if (d[e.to] > d[v] + e.cost) {
d[e.to] = d[v] + e.cost;
que.push(P(e.to, d[e.to]));
}
}
}
}
*/
int main() {
int n;
cin >> n;
int data[200100] = {0};
for (int i = 2; i <= n; i++) {
int a;
cin >> a;
data[a]++;
}
for (int i = 1; i <= n; i++) {
cout << data[i] << endl;
}
}
| replace | 201 | 202 | 201 | 202 | 0 | |
p02707 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int M = 2 * 10000 + 5;
int s[M], n, x, i;
int main() {
cin >> n;
for (i = 2; i <= n; i++) {
cin >> x;
s[x]++;
}
for (i = 1; i <= n; i++)
cout << s[i] << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
const int M = 2 * 100000 + 5;
int s[M], n, x, i;
int main() {
cin >> n;
for (i = 2; i <= n; i++) {
cin >> x;
s[x]++;
}
for (i = 1; i <= n; i++)
cout << s[i] << endl;
return 0;
} | replace | 2 | 3 | 2 | 3 | 0 | |
p02707 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<string> VS;
typedef vector<vector<int>> VVI;
typedef vector<vector<ll>> VVL;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()
#define pf push_front
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ub upper_bound
#define lb lower_bound
int main() {
int N;
cin >> N;
VI A(N - 1);
rep(i, N - 1) cin >> A[i];
VI B(N);
rep(i, N) B[A[i] - 1]++;
rep(i, N) cout << B[i] << endl;
}
| #include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<string> VS;
typedef vector<vector<int>> VVI;
typedef vector<vector<ll>> VVL;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()
#define pf push_front
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ub upper_bound
#define lb lower_bound
int main() {
int N;
cin >> N;
VI A(N - 1);
rep(i, N - 1) cin >> A[i];
VI B(N);
rep(i, N - 1) B[A[i] - 1]++;
rep(i, N) cout << B[i] << endl;
}
| replace | 34 | 35 | 34 | 35 | -6 | double free or corruption (out)
|
p02707 | C++ | Runtime Error | #include <bits/stdc++.h>
#define ll long long
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define MOD 1000000000
#define inf -100000
#define fast \
ios_base::sync_with_stdio(0); \
cin.tie(NULL); \
cout.tie(NULL);
using namespace std;
int main() {
fast;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ll int n;
cin >> n;
ll int a[n - 1];
ll int i, z, s = 0;
ll int f[100005] = {0};
for (i = 0; i < n - 1; i++) {
cin >> a[i];
f[a[i]]++;
}
for (i = 1; i <= n; i++) {
cout << f[i] << endl;
}
}
| #include <bits/stdc++.h>
#define ll long long
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define MOD 1000000000
#define inf -100000
#define fast \
ios_base::sync_with_stdio(0); \
cin.tie(NULL); \
cout.tie(NULL);
using namespace std;
int main() {
fast;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ll int n;
cin >> n;
ll int a[n - 1];
ll int i, z, s = 0;
ll int f[200005] = {0};
for (i = 0; i < n - 1; i++) {
cin >> a[i];
f[a[i]]++;
}
for (i = 1; i <= n; i++) {
cout << f[i] << endl;
}
}
| replace | 23 | 24 | 23 | 24 | 0 | |
p02707 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> A(N, 0);
vector<int> count(N, 0);
for (int i = 0; i < N - 1; i++) {
cin >> A.at(i);
count.at(A.at(i - 1)) += 1;
}
for (int i = 0; i < N; i++) {
cout << count.at(i) << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> A(N, 0);
vector<int> count(N, 0);
for (int i = 0; i < N - 1; i++) {
cin >> A.at(i);
count.at(A.at(i) - 1) += 1;
}
for (int i = 0; i < N; i++) {
cout << count.at(i) << endl;
}
} | replace | 10 | 11 | 10 | 11 | -6 | terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check: __n (which is 18446744073709551615) >= this->size() (which is 5)
|
p02707 | C++ | Time Limit Exceeded | #include <iostream>
#include <math.h>
#include <stdio.h>
#include <vector>
using namespace std;
int main(void) {
int N;
cin >> N;
int l;
vector<int> A(N);
vector<int> sum(N);
sum[0] = 0;
A[0] = 0;
for (int i = 1; i < N; i++) {
cin >> l;
A[i] = l;
sum[i] = 0;
}
for (int i = 1; i < N; i++) {
for (int j = 1; j <= i; j++) {
if (j == A[i]) {
sum[A[i] - 1] = sum[A[i] - 1] + 1;
}
}
}
for (int i = 0; i < N; i++) {
printf("%d\n", sum[i]);
}
return 0;
} | #include <iostream>
#include <math.h>
#include <stdio.h>
#include <vector>
using namespace std;
int main(void) {
int N;
cin >> N;
int l;
vector<int> A(N);
vector<int> sum(N);
sum[0] = 0;
A[0] = 0;
for (int i = 1; i < N; i++) {
cin >> l;
A[i] = l;
sum[i] = 0;
}
for (int i = 1; i < N; i++) {
sum[A[i] - 1] = sum[A[i] - 1] + 1;
}
for (int i = 0; i < N; i++) {
printf("%d\n", sum[i]);
}
return 0;
} | replace | 21 | 26 | 21 | 22 | TLE | |
p02707 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define REP(i, m, n) for (int i = (m); i < (int)(n); i++)
#define REPS(i, m, n) for (int i = (m); i <= (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define reps(i, n) for (int i = 0; i <= (int)(n); i++)
#define rrep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define rreps(i, x) for (int i = (int)(x); i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back
#define fi first
#define se second
typedef long long ll;
typedef pair<int, int> P;
const int inf = INT_MAX;
const ll INF = 1LL << 60;
const ll mod = 1e9 + 7;
const double EPS = 1e-10;
const double PI = acos(-1.0);
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
fill((T *)array, (T *)(array + N), val);
}
template <class T1, class T2, class Pred = std::less<T2>>
struct sort_pair_second {
bool operator()(const std::pair<T1, T2> &left,
const std::pair<T1, T2> &right) {
Pred p;
return p(left.second, right.second);
}
};
int a[100010];
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
rep(i, N - 1) {
int A;
cin >> A;
a[A - 1]++;
}
rep(i, N) cout << a[i] << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define REP(i, m, n) for (int i = (m); i < (int)(n); i++)
#define REPS(i, m, n) for (int i = (m); i <= (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define reps(i, n) for (int i = 0; i <= (int)(n); i++)
#define rrep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define rreps(i, x) for (int i = (int)(x); i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back
#define fi first
#define se second
typedef long long ll;
typedef pair<int, int> P;
const int inf = INT_MAX;
const ll INF = 1LL << 60;
const ll mod = 1e9 + 7;
const double EPS = 1e-10;
const double PI = acos(-1.0);
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
fill((T *)array, (T *)(array + N), val);
}
template <class T1, class T2, class Pred = std::less<T2>>
struct sort_pair_second {
bool operator()(const std::pair<T1, T2> &left,
const std::pair<T1, T2> &right) {
Pred p;
return p(left.second, right.second);
}
};
int a[200010];
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
rep(i, N - 1) {
int A;
cin >> A;
a[A - 1]++;
}
rep(i, N) cout << a[i] << endl;
return 0;
} | replace | 48 | 49 | 48 | 49 | 0 | |
p02707 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> boss(N - 1);
for (int i = 0; i < N - 1; i++)
cin >> boss.at(i);
vector<int> num(N);
for (int i = 0; i < N - 1; i++) {
int count = 0;
for (int j : boss) {
if (i == j - 1)
count++;
}
num.at(i) = count;
}
for (int i = 0; i < N; i++)
cout << num.at(i) << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> boss(N - 1);
for (int i = 0; i < N - 1; i++)
cin >> boss.at(i);
vector<int> num(N);
for (int i = 0; i < N - 1; i++) {
num.at(boss.at(i) - 1) += 1;
}
for (int i = 0; i < N; i++)
cout << num.at(i) << endl;
} | replace | 13 | 19 | 13 | 14 | TLE | |
p02707 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define pi 3.14159265358979323846
#define ll long long
#define N 100001
int main() {
ll n;
cin >> n;
ll ara[n - 1];
for (int i = 0; i < n - 1; i++)
cin >> ara[i];
ll x;
ll p[N] = {0};
for (int i = 0; i < n - 1; i++) {
x = ara[i];
p[x]++;
}
for (int i = 1; i <= n; i++)
cout << p[i] << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define pi 3.14159265358979323846
#define ll long long
#define N 200001
int main() {
ll n;
cin >> n;
ll ara[n - 1];
for (int i = 0; i < n - 1; i++)
cin >> ara[i];
ll x;
ll p[N] = {0};
for (int i = 0; i < n - 1; i++) {
x = ara[i];
p[x]++;
}
for (int i = 1; i <= n; i++)
cout << p[i] << endl;
}
| replace | 5 | 6 | 5 | 6 | 0 | |
p02707 | C++ | Runtime Error | // #include "pch.h"
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <ctype.h>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <time.h>
#include <tuple>
#include <vector>
using namespace std;
// #define int long long
#define rep(i, s, n) for (int i = s; i < n; i++)
#define repe(i, s, n) for (int i = s; i <= n; i++)
#define rrep(i, s, n) for (int i = (n)-1; i >= (s); i--)
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define fi first
#define se second
#define chmin(a, b) a = min((a), (b))
#define chmax(a, b) a = max((a), (b))
#define l1 list[index]
#define l2 list[index - 1]
#define l3 list[index + 1]
#define iif(i, j) ((i < 0 && j < 0) || (i > 0 && j > 0)) ? true : false
typedef long long ll;
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
typedef pair<pint, int> P1;
typedef pair<int, pint> P2;
typedef pair<pint, pint> PP;
static const ll maxLL = (ll)1 << 62;
const ll MOD = 1000000007;
const ll INF = 1e18;
const double PI = 3.14159265359;
int ca[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
vector<int> list(100005, 0);
signed main() {
int N;
cin >> N;
for (int i = 1; i < N; i++) {
int num;
cin >> num;
list[num]++;
}
for (int i = 1; i <= N; i++) {
cout << list[i] << endl;
}
return 0;
} | // #include "pch.h"
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <ctype.h>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <time.h>
#include <tuple>
#include <vector>
using namespace std;
// #define int long long
#define rep(i, s, n) for (int i = s; i < n; i++)
#define repe(i, s, n) for (int i = s; i <= n; i++)
#define rrep(i, s, n) for (int i = (n)-1; i >= (s); i--)
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define fi first
#define se second
#define chmin(a, b) a = min((a), (b))
#define chmax(a, b) a = max((a), (b))
#define l1 list[index]
#define l2 list[index - 1]
#define l3 list[index + 1]
#define iif(i, j) ((i < 0 && j < 0) || (i > 0 && j > 0)) ? true : false
typedef long long ll;
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
typedef pair<pint, int> P1;
typedef pair<int, pint> P2;
typedef pair<pint, pint> PP;
static const ll maxLL = (ll)1 << 62;
const ll MOD = 1000000007;
const ll INF = 1e18;
const double PI = 3.14159265359;
int ca[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
vector<int> list(200005, 0);
signed main() {
int N;
cin >> N;
for (int i = 1; i < N; i++) {
int num;
cin >> num;
list[num]++;
}
for (int i = 1; i <= N; i++) {
cout << list[i] << endl;
}
return 0;
} | replace | 50 | 51 | 50 | 51 | 0 | |
p02707 | C++ | Runtime Error | #include <iostream>
using namespace std;
int main() {
int n, j, a[20000], count[20000];
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
count[i] = 0;
}
for (int i = 0; i < n; i++) {
j = a[i] - 1;
count[j]++;
}
for (int i = 0; i < n; i++) {
cout << count[i] << endl;
}
} | #include <iostream>
using namespace std;
int main() {
int n, j, a[200000], count[200000];
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
count[i] = 0;
}
for (int i = 0; i < n; i++) {
j = a[i] - 1;
count[j]++;
}
for (int i = 0; i < n; i++) {
cout << count[i] << endl;
}
} | replace | 5 | 6 | 5 | 6 | 0 | |
p02707 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i > 0; i--)
#define all(v) v.begin(), v.end()
#define debug(x) cout << x << endl
#define vout(v) rep(i, v.size()) cout << v[i] << " ";
#define INF INT_MAX
int N;
int main() {
cin >> N;
vi a(N, 0);
rep(i, N - 1) { cin >> a[i + 1]; }
vi ans(N, 0);
rep(i, N) { ans[a[i] - 1]++; }
rep(i, N) { cout << ans[i] << endl; }
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i > 0; i--)
#define all(v) v.begin(), v.end()
#define debug(x) cout << x << endl
#define vout(v) rep(i, v.size()) cout << v[i] << " ";
#define INF INT_MAX
int N;
int main() {
cin >> N;
vi a(N, 0);
rep(i, N - 1) { cin >> a[i + 1]; }
vi ans(N, 0);
rep(i, N - 1) { ans[a[i + 1] - 1]++; }
rep(i, N) { cout << ans[i] << endl; }
return 0;
}
| replace | 20 | 21 | 20 | 21 | -6 | double free or corruption (out)
|
p02707 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int N;
int a[200001];
int main() {
cin >> N;
for (int i = 1; i < N; i++)
cin >> a[i];
map<int, int> mp;
for (int i = 1; i < N; i++) {
mp[i] = 0;
}
for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
for (int i = 1; i < N; i++) {
if (a[i] == itr->first) {
itr->second++;
}
}
}
for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
cout << itr->second << endl;
}
cout << 0 << endl;
}
| #include <bits/stdc++.h>
using namespace std;
int N;
int a[200001];
int main() {
cin >> N;
for (int i = 1; i < N; i++)
cin >> a[i];
map<int, int> mp;
for (int i = 1; i < N; i++) {
mp[i] = 0;
}
for (int i = 1; i < N; i++) {
mp[a[i]]++;
}
for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
cout << itr->second << endl;
}
cout << 0 << endl;
}
| replace | 13 | 19 | 13 | 15 | TLE | |
p02707 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n - 1);
for (int i = 0; i < n - 1; i++) {
cin >> a[i];
}
vector<int> b(n, 0);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - 1; j++) {
if (a[j] == i + 1) {
b[i]++;
}
}
}
for (int i = 0; i < n; i++) {
cout << b[i] << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n - 1);
for (int i = 0; i < n - 1; i++) {
cin >> a[i];
}
vector<int> b(n, 0);
for (int j = 0; j < n - 1; j++) {
b[a[j] - 1]++;
}
for (int i = 0; i < n; i++) {
cout << b[i] << endl;
}
return 0;
} | replace | 11 | 17 | 11 | 13 | TLE | |
p02707 | C++ | Runtime Error | #define rep(i, n) for (int i = 0; i < (int)(n); i++)
#include <bits/stdc++.h>
using namespace std;
int main() {
int N = 0;
vector<int> A;
vector<int> answer;
cin >> N;
rep(i, N) {
A.push_back(0);
answer.push_back(0);
if (i >= 1) {
cin >> A[i];
}
}
rep(i, N) { answer[A[i] - 1]++; }
rep(i, N) { cout << answer[i] << endl; }
} | #define rep(i, n) for (int i = 0; i < (int)(n); i++)
#include <bits/stdc++.h>
using namespace std;
int main() {
int N = 0;
vector<int> A;
vector<int> answer;
cin >> N;
rep(i, N) {
A.push_back(0);
answer.push_back(0);
if (i >= 1) {
cin >> A[i];
}
}
rep(i, N) {
if (i >= 1) {
answer[A[i] - 1]++;
}
}
rep(i, N) { cout << answer[i] << endl; }
} | replace | 19 | 20 | 19 | 24 | -6 | double free or corruption (out)
|
p02707 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n - 1; i++)
cin >> a[i];
vector<int> c(n);
rep(i, n) { c[a[i] - 1]++; }
rep(i, n) { printf("%d\n", c[i]); }
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n - 1; i++)
cin >> a[i];
vector<int> c(n);
for (int i = 0; i < n - 1; i++) {
c[a[i] - 1]++;
}
rep(i, n) { printf("%d\n", c[i]); }
return 0;
} | replace | 14 | 15 | 14 | 17 | -6 | double free or corruption (out)
|
p02707 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> count(100000, 0);
for (int i = 0; i < n - 1; i++) {
int a;
cin >> a;
count[a - 1]++;
}
for (int i = 0; i < n; i++) {
cout << count[i] << endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> count(200000, 0);
for (int i = 0; i < n - 1; i++) {
int a;
cin >> a;
count[a - 1]++;
}
for (int i = 0; i < n; i++) {
cout << count[i] << endl;
}
return 0;
}
| replace | 5 | 6 | 5 | 6 | 0 | |
p02707 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n - 1], ans[n];
for (int i = 0; i < n; i++)
ans[i] = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
ans[a[i] - 1]++;
}
for (int i = 0; i < n; i++)
cout << ans[i] << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n - 1], ans[n];
for (int i = 0; i < n; i++)
ans[i] = 0;
for (int i = 0; i < n - 1; i++) {
cin >> a[i];
ans[a[i] - 1]++;
}
for (int i = 0; i < n; i++)
cout << ans[i] << endl;
}
| replace | 8 | 9 | 8 | 9 | 0 | |
p02708 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
int main() {
ll n, k;
cin >> n >> k;
ll cnt = 0;
for (int j = k; j <= n + 1; j++) {
ll min = 0;
ll max = 0;
for (int i = 0; i < j; i++) {
min += i;
}
for (int i = n - j + 1; i < n + 1; i++) {
max += i;
}
cnt = (cnt + max - min + 1) % MOD;
}
cout << cnt << endl;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
int main() {
ll n, k;
cin >> n >> k;
ll cnt = 0;
for (int j = k; j <= n + 1; j++) {
// ll min=(j-1)*j/2;
// ll max=(n+n-j+1)*j/2;
// for(int i=0; i<j;i++){
// min+=i;
// }
// cout << min << endl;
// for(int i=n-j+1; i<n+1;i++){
// max+=i;
// }
// cout << max << endl;
// cnt = (cnt + max -min+1)%MOD;
cnt = (cnt + (n - j + 1) * j + 1) % MOD;
// cout << (n-j+1)*j << endl;
// cout << max - min << endl;
}
cout << cnt << endl;
}
| replace | 10 | 19 | 10 | 24 | TLE | |
p02708 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define fi first
#define se second
using namespace std;
using lint = long long;
using uint = unsigned int;
using ulint = unsigned long long;
using ldouble = long double;
using pii = pair<int, int>;
using pli = pair<lint, lint>;
using pdd = pair<double, double>;
using pld = pair<ldouble, ldouble>;
using v1i = vector<int>;
using v1li = vector<lint>;
using v2i = vector<vector<int>>;
using v2li = vector<vector<lint>>;
using v3i = vector<vector<vector<int>>>;
using v3li = vector<vector<vector<lint>>>;
using v1b = vector<bool>;
using v2b = vector<vector<bool>>;
using v3b = vector<vector<vector<bool>>>;
using v1c = vector<char>;
using v2c = vector<vector<char>>;
using v3c = vector<vector<vector<char>>>;
constexpr lint mod1 = 1e9 + 7;
constexpr lint mod2 = 998244353;
int main() {
lint n, k, s = 0;
cin >> n >> k;
++n;
v1li v(n + 1);
v[0] = 0;
rep(i, n) v[i + 1] = v[i] + i + 1;
for (int i = k; k <= n; ++i) {
s += v[n] - v[n - k] - v[k];
s %= mod1;
}
cout << s << endl;
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define fi first
#define se second
using namespace std;
using lint = long long;
using uint = unsigned int;
using ulint = unsigned long long;
using ldouble = long double;
using pii = pair<int, int>;
using pli = pair<lint, lint>;
using pdd = pair<double, double>;
using pld = pair<ldouble, ldouble>;
using v1i = vector<int>;
using v1li = vector<lint>;
using v2i = vector<vector<int>>;
using v2li = vector<vector<lint>>;
using v3i = vector<vector<vector<int>>>;
using v3li = vector<vector<vector<lint>>>;
using v1b = vector<bool>;
using v2b = vector<vector<bool>>;
using v3b = vector<vector<vector<bool>>>;
using v1c = vector<char>;
using v2c = vector<vector<char>>;
using v3c = vector<vector<vector<char>>>;
constexpr lint mod1 = 1e9 + 7;
constexpr lint mod2 = 998244353;
int main() {
lint n, k, s = 0;
cin >> n >> k;
++n;
v1li v(n + 1);
v[0] = 0;
rep(i, n) v[i + 1] = v[i] + i + 1;
for (int i = k; i <= n; ++i) {
s += v[n] - v[n - i] - v[i] + 1;
s %= mod1;
}
cout << s << endl;
return 0;
} | replace | 36 | 38 | 36 | 38 | TLE | |
p02708 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define P pair<int, int>
using ll = int64_t;
using namespace std;
#define ketasuu(n) fixed << setprecision(n)
#define btoe(p) p.begin(), p.end()
#define etob(p) p.rbegin(), p.rend()
const ll mod = 1000000007;
int main() {
int n, k;
cin >> n >> k;
vector<ll> m(n + 2);
ll s = 0, e = 0, a = 0, b = n;
rep(i, n + 2) {
s += a;
a++;
e += b;
b--;
m[i + 1] = e - s + 1;
}
ll ans = 0;
for (int i = k; i < n + 2; ++i) {
ans += m[i];
ans %= mod;
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define P pair<int, int>
using ll = int64_t;
using namespace std;
#define ketasuu(n) fixed << setprecision(n)
#define btoe(p) p.begin(), p.end()
#define etob(p) p.rbegin(), p.rend()
const ll mod = 1000000007;
int main() {
int n, k;
cin >> n >> k;
vector<ll> m(10000000);
ll s = 0, e = 0, a = 0, b = n;
rep(i, n + 2) {
s += a;
a++;
e += b;
b--;
m[i + 1] = e - s + 1;
}
ll ans = 0;
for (int i = k; i < n + 2; ++i) {
ans += m[i];
ans %= mod;
}
cout << ans << endl;
return 0;
} | replace | 14 | 15 | 14 | 15 | -6 | malloc(): corrupted top size
|
p02708 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long lint;
typedef long long llint;
static const llint MAX = 2e5 + 5;
// static const int NIL = -1;
// static const ll INF = 1<<21;
static const llint MOD = 1e9 + 7;
bool compPair(const pair<int, int> &arg1, const pair<int, int> &arg2) {
return arg1.first > arg2.first;
}
template <class T> void chmax(T &a, T b) {
if (a < b) {
a = b;
}
}
template <class T> void chmin(T &a, T b) {
if (a > b) {
a = b;
}
}
int main(void) {
llint n, k;
cin >> n >> k;
llint ans = 0, tmp, s, g;
for (llint in = k; in <= n + 1; in++) {
s = 0;
g = 0;
for (llint ip = 0; ip < in; ip++) {
s += ip;
g += n - ip;
}
g = min(n * (n + 1) / 2, g);
tmp = g - s + 1;
tmp %= MOD;
ans += tmp;
// cout << s << g <<tmp << endl;
ans %= MOD;
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long lint;
typedef long long llint;
static const llint MAX = 2e5 + 5;
// static const int NIL = -1;
// static const ll INF = 1<<21;
static const llint MOD = 1e9 + 7;
bool compPair(const pair<int, int> &arg1, const pair<int, int> &arg2) {
return arg1.first > arg2.first;
}
template <class T> void chmax(T &a, T b) {
if (a < b) {
a = b;
}
}
template <class T> void chmin(T &a, T b) {
if (a > b) {
a = b;
}
}
int main(void) {
llint n, k;
cin >> n >> k;
llint ans = 0, tmp, s, g;
for (llint in = k; in <= n + 1; in++) {
s = 0;
g = 0;
s = (in) * (in - 1) / 2;
g = n * in - (in) * (in - 1) / 2;
g = min(n * (n + 1) / 2, g);
tmp = g - s + 1;
tmp %= MOD;
ans += tmp;
// cout << s << g <<tmp << endl;
ans %= MOD;
}
cout << ans << endl;
return 0;
}
| replace | 33 | 37 | 33 | 35 | TLE | |
p02708 | C++ | Runtime Error | // Winners don't quit and Quitters don't win
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vii vector<int>
#define vll vector<ll>
#define mod 1000000007
#define MAX 100005
ll sum[2 * MAX];
ll solve_() {
ll i;
for (i = 1; i < 2 * MAX - 2; ++i) {
sum[i] = sum[i - 1] + i;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
solve_();
ll n, k, i, ans = 1;
cin >> n >> k;
for (i = k; i <= n; ++i) {
ans = (ans + (sum[n] - sum[n - i]) - sum[i - 1] + 1 + mod) % mod;
}
cout << ans % mod;
} | // Winners don't quit and Quitters don't win
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vii vector<int>
#define vll vector<ll>
#define mod 1000000007
#define MAX 100005
ll sum[2 * MAX];
void solve_() {
ll i;
for (i = 1; i < 2 * MAX - 2; ++i) {
sum[i] = sum[i - 1] + i;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
solve_();
ll n, k, i, ans = 1;
cin >> n >> k;
for (i = k; i <= n; ++i) {
ans = (ans + (sum[n] - sum[n - i]) - sum[i - 1] + 1 + mod) % mod;
}
cout << ans % mod;
} | replace | 12 | 13 | 12 | 13 | 0 | |
p02708 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
long long N, K;
cin >> N >> K;
long long tempmin, tempmax;
long long ans = 0;
for (long long i = K; i <= N + 1; i++) {
tempmin = 0;
tempmax = 0;
for (long long j = 0; j < i; j++) {
tempmin = tempmin + j;
tempmax = tempmax + (N - j);
}
// cout<<tempmin<<" "<<tempmax<<endl;
ans = (ans + tempmax - tempmin + 1) % 1000000007;
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
long long N, K;
cin >> N >> K;
long long tempmin, tempmax;
long long ans = 0;
for (long long i = K; i <= N + 1; i++) {
tempmin = (i - 1) * i / 2;
tempmax = (2 * N + 1 - i) * i / 2;
// cout<<tempmin<<" "<<tempmax<<endl;
ans = (ans + tempmax - tempmin + 1) % 1000000007;
}
cout << ans << endl;
return 0;
}
| replace | 8 | 14 | 8 | 10 | TLE | |
p02708 | C++ | Time Limit Exceeded | #include <iostream>
using namespace std;
int main(void) {
int N, K;
long int sum = 0;
long int mod = 1000000007;
cin >> N >> K;
for (int i = K; i <= N + 1; i++) {
long int max = 0;
long int min = 0;
for (int j = 0; j < i; j++) {
min += j;
max += (N - j);
}
sum += (max - min + 1);
}
cout << sum % mod << endl;
return 0;
}
| #include <iostream>
using namespace std;
int main(void) {
int N, K;
long int sum = 0;
long int mod = 1000000007;
cin >> N >> K;
long int max = 0;
long int min = 0;
for (int i = 0; i < K; i++) {
min += i;
max += (N - i);
}
sum += (max - min + 1);
for (int i = K; i < N + 1; i++) {
min += i;
max += (N - i);
sum += (max - min + 1);
}
cout << sum % mod << endl;
return 0;
}
| replace | 10 | 17 | 10 | 21 | TLE | |
p02708 | C++ | Time Limit Exceeded | #include <iostream>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
long long c = 0;
for (int kk = k; kk <= n + 1; kk++) {
long long b = 1;
for (int i = 0; i < kk; i++)
b += (n - 2 * i) % (1000000007);
c = (b + c) % (1000000007);
}
cout << c << endl;
return 0;
}
| #include <iostream>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
long long c = 0;
for (long long kk = k; kk <= n + 1; kk++) {
c = (kk * (n + 1 - kk) + 1 + c) % (1000000007);
}
cout << c << endl;
return 0;
}
| replace | 6 | 11 | 6 | 8 | TLE | |
p02708 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef pair<ll, ll> pair_t;
using Graph = vector<vector<ll>>;
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
/***** define constant start *******/
const ll MOD = 1000000007;
const double PI = 3.1415926535897932;
const ll INF = LLONG_MAX;
/****** define constant end ********/
/****** define variable start ******/
ll n, k;
/******* define variable end *******/
int solve() {
cin >> n >> k;
vl ma(n);
vl mi(n);
ll ans = 0;
for (ll i = k; i <= n + 1; i++) {
mi[i] = i * (i - 1) / 2;
ma[i] = n * (n + 1) / 2 - (n + 1 - i) * (n - i) / 2;
ans += ma[i] - mi[i] + 1;
ans %= MOD;
}
cout << ans % MOD << endl;
return 0;
}
int main() {
// clock_t start = clock();
cout << std::fixed << std::setprecision(10);
ios::sync_with_stdio(false);
cin.tie(nullptr);
int end_solve = solve();
/*
clock_t end = clock();
const double time = static_cast<double>(end - start) / CLOCKS_PER_SEC *
1000.0; printf("time %lf[ms]\n", time);
*/
return end_solve;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef pair<ll, ll> pair_t;
using Graph = vector<vector<ll>>;
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
/***** define constant start *******/
const ll MOD = 1000000007;
const double PI = 3.1415926535897932;
const ll INF = LLONG_MAX;
/****** define constant end ********/
/****** define variable start ******/
ll n, k;
/******* define variable end *******/
int solve() {
cin >> n >> k;
vl ma(n + 5);
vl mi(n + 5);
ll ans = 0;
for (ll i = k; i <= n + 1; i++) {
mi[i] = i * (i - 1) / 2;
ma[i] = n * (n + 1) / 2 - (n + 1 - i) * (n - i) / 2;
ans += ma[i] - mi[i] + 1;
ans %= MOD;
}
cout << ans % MOD << endl;
return 0;
}
int main() {
// clock_t start = clock();
cout << std::fixed << std::setprecision(10);
ios::sync_with_stdio(false);
cin.tie(nullptr);
int end_solve = solve();
/*
clock_t end = clock();
const double time = static_cast<double>(end - start) / CLOCKS_PER_SEC *
1000.0; printf("time %lf[ms]\n", time);
*/
return end_solve;
}
| replace | 21 | 23 | 21 | 23 | -6 | munmap_chunk(): invalid pointer
|
p02708 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ll ans, n, k, i, a, b, j;
cin >> n >> k;
ans = 0;
for (i = k; i <= n + 1; i++) {
a = 0;
b = 0;
for (j = 0; j < i; j++) {
a += j;
b += n - j;
}
ans += b - a + 1;
ans = ans % 1000000007;
}
cout << ans;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ll ans, n, k, i, a, b, j;
cin >> n >> k;
ans = 0;
a = 0;
b = 0;
for (j = 0; j < k; j++) {
a += j;
b += n - j;
}
ans += b - a + 1;
ans = ans % 1000000007;
for (i = k; i <= n; i++) {
a += i;
b += n - i;
ans += b - a + 1;
ans = ans % 1000000007;
}
cout << ans;
} | replace | 8 | 15 | 8 | 19 | TLE | |
p02708 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bits/stdc++.h>
#include <cassert>
#include <stdio.h>
using namespace std;
unsigned long long a(unsigned long long N, unsigned long long K) {
return (N - K + 1) * K + 1;
}
unsigned long long solve(unsigned long long N, unsigned long long K) {
unsigned long long ans = 0;
for (int i = K; i <= N + 1; i++) {
unsigned long long temp = a(N, i) % 1000000007;
ans += temp;
ans %= 1000000007;
// if(temp<0){
cerr << N << " " << K << " " << i << " " << ans << endl;
// }
}
ans %= 1000000007;
cout << ans << "\n";
return ans;
}
// class input{
// public:
// input(int _A, int _B, int _C, int _X, int _ans){
// A = _A;
// B = _B;
// C = _C;
// X = _X;
// ans = _ans;
// }
// int A;
// int B;
// int C;
// int X;
// int ans;
// };
// int solve(input key){
// return solve(key.A, key.B, key.C, key.X);
// }
int main() {
int N, K;
bool use_stdin = true;
// use_stdin = false;
if (use_stdin) {
cin >> N >> K;
solve(N, K);
} else {
// input keys[] = {
// {30,40,50,6000,213},
// {5,1,0,150,0},
// {2,2,2,100,2}
// };
// for(auto key:keys){
// assert(solve(key)==key.ans);
// }
}
return 0;
} | #include <algorithm>
#include <bits/stdc++.h>
#include <cassert>
#include <stdio.h>
using namespace std;
unsigned long long a(unsigned long long N, unsigned long long K) {
return (N - K + 1) * K + 1;
}
unsigned long long solve(unsigned long long N, unsigned long long K) {
unsigned long long ans = 0;
for (int i = K; i <= N + 1; i++) {
unsigned long long temp = a(N, i) % 1000000007;
ans += temp;
ans %= 1000000007;
// if(temp<0){
// cerr << N << " " << K << " " << i << " " << ans << endl;
// }
}
ans %= 1000000007;
cout << ans << "\n";
return ans;
}
// class input{
// public:
// input(int _A, int _B, int _C, int _X, int _ans){
// A = _A;
// B = _B;
// C = _C;
// X = _X;
// ans = _ans;
// }
// int A;
// int B;
// int C;
// int X;
// int ans;
// };
// int solve(input key){
// return solve(key.A, key.B, key.C, key.X);
// }
int main() {
int N, K;
bool use_stdin = true;
// use_stdin = false;
if (use_stdin) {
cin >> N >> K;
solve(N, K);
} else {
// input keys[] = {
// {30,40,50,6000,213},
// {5,1,0,150,0},
// {2,2,2,100,2}
// };
// for(auto key:keys){
// assert(solve(key)==key.ans);
// }
}
return 0;
} | replace | 18 | 19 | 18 | 19 | TLE | |
p02708 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define ALL(a) (a).begin(), (a).end()
#define SORT(c) sort(ALL((c)))
#define debug(x) \
do { \
std::cout << #x << ": "; \
view(x); \
} while (0)
template <typename T> void view(T e) { std::cout << e << std::endl; }
template <typename T> void view(const std::vector<T> &v) {
for (const auto &e : v) {
std::cout << e << ", ";
}
std::cout << std::endl;
}
template <typename T> void view(const std::vector<std::vector<T>> &vv) {
for (const auto &v : vv) {
view(v);
}
}
int main() {
ll N, K;
cin >> N >> K;
ll ans = 0;
for (ll i = K; i <= (N + 1); i++) {
ll mi = i * (i - 1) / 2;
ll ma = 0;
rep(j, i) { ma += N - j; }
ans += (ma - mi + 1) % (1000000000 + 7);
}
cout << ans % (1000000000 + 7) << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define ALL(a) (a).begin(), (a).end()
#define SORT(c) sort(ALL((c)))
#define debug(x) \
do { \
std::cout << #x << ": "; \
view(x); \
} while (0)
template <typename T> void view(T e) { std::cout << e << std::endl; }
template <typename T> void view(const std::vector<T> &v) {
for (const auto &e : v) {
std::cout << e << ", ";
}
std::cout << std::endl;
}
template <typename T> void view(const std::vector<std::vector<T>> &vv) {
for (const auto &v : vv) {
view(v);
}
}
int main() {
ll N, K;
cin >> N >> K;
ll ans = 0;
for (ll i = K; i <= (N + 1); i++) {
ll mi = i * (i - 1) / 2;
ll ma = 0;
rep(j, i) { ma += N - j; }
ma = i * (2 * (N - i + 1) + (i - 1)) / 2;
ans += (ma - mi + 1) % (1000000000 + 7);
}
cout << ans % (1000000000 + 7) << endl;
return 0;
}
| insert | 35 | 35 | 35 | 36 | TLE | |
p02708 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
const ll mod = 1000000007;
ll n, k;
cin >> n >> k;
ll ans = 0;
for (ll i = k; i <= n + 1; i++) {
ll tmpLeft = 0;
for (ll j = 0; j < i; j++) {
tmpLeft += j;
}
ll tmpRight = 0;
for (ll j = n - i + 1; j <= n; j++) {
tmpRight += j;
}
ans += tmpRight - tmpLeft + 1;
ans %= mod;
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
const ll mod = 1000000007;
ll n, k;
cin >> n >> k;
ll ans = 0;
for (ll i = k; i <= n + 1; i++) {
ll tmpLeft = (i - 1) * i / 2;
ll tmpRight = n * (n + 1) / 2 - (n - i) * (n - i + 1) / 2;
ans += tmpRight - tmpLeft + 1;
ans %= mod;
}
cout << ans << endl;
return 0;
} | replace | 11 | 19 | 11 | 13 | TLE | |
p02708 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll sum(ll a, ll b) {
ll count = 0;
for (ll i = a; i <= b; i++)
count += i;
return count;
}
int main(void) {
ll N = 0, M = 0;
cin >> N >> M;
ll s = 0;
for (ll i = M; i <= N + 1; i++) {
s += (sum(N + 1 - i, N) - sum(0, i - 1) + 1);
}
cout << s % 1000000007 << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll sum(ll a, ll b) {
ll count = 0;
count = (b - a + 1) * (a + b) / 2;
return count;
}
int main(void) {
ll N = 0, M = 0;
cin >> N >> M;
ll s = 0;
for (ll i = M; i <= N + 1; i++) {
s += (sum(N + 1 - i, N) - sum(0, i - 1) + 1);
}
cout << s % 1000000007 << endl;
} | replace | 6 | 8 | 6 | 7 | TLE | |
p02708 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bitset>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <limits.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
const int INF = 0x7fffffff;
const int MOD = 1000000007;
using lll = long long;
using ull = unsigned long long;
using namespace std;
int main() {
lll ii, jj, kk;
vector<int> ret;
int n, k;
cin >> n >> k;
lll minNum, maxNum;
lll sum = 0;
lll cnt;
for (ii = k; ii <= n + 1; ii++) {
minNum = 0;
maxNum = 0;
for (jj = 0, cnt = 0; jj < ii; jj++, cnt++) {
minNum += cnt;
maxNum += (n - cnt);
}
// cout << "maxNum:" << maxNum << "minNum:" << minNum << endl;
sum = (sum + maxNum - minNum + 1) % MOD;
}
cout << sum << endl;
return 0;
}
| #include <algorithm>
#include <bitset>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <limits.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
const int INF = 0x7fffffff;
const int MOD = 1000000007;
using lll = long long;
using ull = unsigned long long;
using namespace std;
int main() {
lll ii, jj, kk;
vector<int> ret;
int n, k;
cin >> n >> k;
lll minNum, maxNum;
lll sum = 0;
lll cnt;
for (ii = k; ii <= n + 1; ii++) {
minNum = 0;
maxNum = 0;
// for(jj=0,cnt=0;jj<ii;jj++,cnt++){
// minNum += cnt;
// maxNum += (n-cnt);
// }
minNum = (ii * (ii - 1)) / 2;
maxNum = n * ii - minNum;
// cout << "maxNum:" << maxNum << "minNum:" << minNum << endl;
sum = (sum + maxNum - minNum + 1) % MOD;
}
cout << sum << endl;
return 0;
}
| replace | 38 | 43 | 38 | 44 | TLE | |
p02708 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define sort(A) sort(A.begin(), A.end())
#define reverse(A) reverse(A.begin(), A.end());
#define vecmin(A) *min_element(A.begin(), A.end());
#define vecmax(A) *max_element(A.begin(), A.end());
typedef long long ll;
ll sum(ll n) {
ll ans = 0;
for (ll i = 1; i <= n; i++)
ans += i;
return ans;
}
ll f(ll n, ll k) {
ll left = sum(n) - sum(n - k);
ll right = sum(k - 1);
return left - right + 1;
}
int main() {
ll n, k;
cin >> n >> k;
ll ans = 0;
for (ll i = k; i <= n + 1; i++) {
ans += f(n, i);
ans %= 1000000007;
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define sort(A) sort(A.begin(), A.end())
#define reverse(A) reverse(A.begin(), A.end());
#define vecmin(A) *min_element(A.begin(), A.end());
#define vecmax(A) *max_element(A.begin(), A.end());
typedef long long ll;
ll f(ll n, ll k) { return -k * k + k + n * k + 1; }
int main() {
ll n, k;
cin >> n >> k;
ll ans = 0;
for (ll i = k; i <= n + 1; i++) {
ans += f(n, i);
ans %= 1000000007;
}
cout << ans << endl;
}
| replace | 10 | 22 | 10 | 11 | TLE | |
p02708 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
int main() {
ll n, k;
cin >> n >> k;
ll i, ans = 0, j, mod = 1e9 + 7;
for (i = k; i <= n + 1; i++) {
ll sum = 0, sum1 = 0;
for (j = 0; j < i; j++) {
sum += j;
sum1 += n - j;
}
ans += sum1 - sum + 1;
ans %= mod;
}
cout << ans;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
int main() {
ll n, k;
cin >> n >> k;
ll i, ans = 0, j, mod = 1e9 + 7;
for (i = k; i <= n + 1; i++) {
ll sum = 0, sum1 = 0;
sum = i * (i - 1) / 2;
sum1 = i * (n + n - i + 1) / 2;
ans += sum1 - sum + 1;
ans %= mod;
}
cout << ans;
} | replace | 10 | 14 | 10 | 12 | TLE | |
p02708 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) x.begin(), x.end()
#define int ll
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int MOD = 1e9 + 7;
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
int ans = 0;
for (int i = k; i <= n + 1; i++) {
int minu = 0;
for (int j = 0; j < i; j++)
minu += j;
int mxnu = 0;
for (int j = 0; j < i; j++)
mxnu += (n - j);
ans += (mxnu - minu + 1) % MOD;
ans %= MOD;
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) x.begin(), x.end()
#define int ll
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int MOD = 1e9 + 7;
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
int ans = 0;
for (int i = k; i <= n + 1; i++) {
int minu = i * (i - 1) / 2;
// int minu = 0; for(int j=0;j<i;j++) minu += j;
int mxnu = i * (2 * n - i + 1) / 2;
// int mxnu = 0; for(int j=0;j<i;j++) mxnu += (n-j);
ans += (mxnu - minu + 1) % MOD;
ans %= MOD;
}
cout << ans << endl;
return 0;
}
| replace | 17 | 23 | 17 | 21 | TLE | |
p02708 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
long N;
long K;
cin >> N >> K;
long ans = 0;
vector<long> bb(N + 1);
vector<long> aa(N + 1);
aa.at(0) = 0;
bb.at(0) = 0;
for (int i = 1; i < N + 2; i++) {
bb.at(i) = bb.at(i - 1) + (i - 1);
}
for (int i = 1; i < N + 2; i++) {
aa.at(i) = aa.at(i - 1) + N - (i - 1);
}
for (int j = K; j < N + 2; j++) {
ans = ans + 1 + aa.at(j) - bb.at(j);
ans = ans % 1000000007;
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
long N;
long K;
cin >> N >> K;
long ans = 0;
vector<long> bb(N + 2);
vector<long> aa(N + 2);
aa.at(0) = 0;
bb.at(0) = 0;
for (int i = 1; i < N + 2; i++) {
bb.at(i) = bb.at(i - 1) + (i - 1);
}
for (int i = 1; i < N + 2; i++) {
aa.at(i) = aa.at(i - 1) + N - (i - 1);
}
for (int j = K; j < N + 2; j++) {
ans = ans + 1 + aa.at(j) - bb.at(j);
ans = ans % 1000000007;
}
cout << ans << endl;
return 0;
} | replace | 11 | 13 | 11 | 13 | -6 | terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check: __n (which is 4) >= this->size() (which is 4)
|
p02708 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <cmath>
#include <math.h>
#define rep(i, m, n) for (int i = (m); i < (n); ++i)
#define rem(i, m, n) for (int i = (m); i > (n); --i)
#define MOD 1000000007
using namespace std;
using ll = long long;
using std::swap;
// vector<long long int> v;
vector<long long int> w;
int main() {
ll N, K, MA, MI;
ll ANS = 0;
cin >> N >> K;
rep(i, K, N + 2) {
MA = 0;
MI = 0;
rep(j, 0, i) MI += j;
rem(j, N, N - i) MA += j;
// MI = (0 + (i - 1))*i / 2;
// MA = (N + (N - i + 1))*i / 2;
// cout << MI << " " << MA << '\n';
ANS += (MA - MI + 1) % MOD;
ANS %= MOD;
}
ANS %= MOD;
cout << ANS << '\n';
return 0;
} | #include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <cmath>
#include <math.h>
#define rep(i, m, n) for (int i = (m); i < (n); ++i)
#define rem(i, m, n) for (int i = (m); i > (n); --i)
#define MOD 1000000007
using namespace std;
using ll = long long;
using std::swap;
// vector<long long int> v;
vector<long long int> w;
int main() {
ll N, K, MA, MI;
ll ANS = 0;
cin >> N >> K;
rep(i, K, N + 2) {
MA = 0;
MI = 0;
MI = (0 + (i - 1)) * ll(i) / 2;
MA = (N + (N - i + 1)) * ll(i) / 2;
ANS += (MA - MI + 1) % MOD;
ANS %= MOD;
}
ANS %= MOD;
cout << ANS << '\n';
return 0;
} | replace | 30 | 35 | 30 | 32 | TLE | |
p02709 | C++ | Time Limit Exceeded | // Sakhiya07 - Yagnik Sakhiya
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(), x.end()
#define mp make_pair
#define bp __builtin_popcountll
#define MOD 1000000007
const int N = 2005;
#define FAST \
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); \
srand(time(NULL));
pair<ll, ll> p[N + 1];
ll dp[N][N];
ll solve1(ll id, ll first, ll last) {
if (id == 0)
return 0;
ll crt = p[id].ss;
ll dif1 = abs(crt - first);
ll dif2 = abs(crt - last);
ll ans = dp[id][first];
// if(ans!=-1) return ans;
ans = max((solve1(id - 1, first, last - 1) + p[id].ff * dif2),
(solve1(id - 1, first + 1, last) + p[id].ff * dif1));
return dp[id][first] = ans;
}
void solve() {
ll n;
cin >> n;
for (ll i = 1; i <= n; i++) {
cin >> p[i].ff;
p[i].ss = i;
}
sort(p + 1, p + n + 1);
set<ll> s;
memset(dp, -1, sizeof(dp));
ll ans = solve1(n, 1, n);
cout << ans;
}
int main() {
FAST;
ll t = 1;
// cin >> t;
while (t--) {
solve();
}
} | // Sakhiya07 - Yagnik Sakhiya
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(), x.end()
#define mp make_pair
#define bp __builtin_popcountll
#define MOD 1000000007
const int N = 2005;
#define FAST \
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); \
srand(time(NULL));
pair<ll, ll> p[N + 1];
ll dp[N][N];
ll solve1(ll id, ll first, ll last) {
if (id == 0)
return 0;
ll crt = p[id].ss;
ll dif1 = abs(crt - first);
ll dif2 = abs(crt - last);
ll ans = dp[id][first];
if (ans != -1)
return ans;
ans = max((solve1(id - 1, first, last - 1) + p[id].ff * dif2),
(solve1(id - 1, first + 1, last) + p[id].ff * dif1));
return dp[id][first] = ans;
}
void solve() {
ll n;
cin >> n;
for (ll i = 1; i <= n; i++) {
cin >> p[i].ff;
p[i].ss = i;
}
sort(p + 1, p + n + 1);
set<ll> s;
memset(dp, -1, sizeof(dp));
ll ans = solve1(n, 1, n);
cout << ans;
}
int main() {
FAST;
ll t = 1;
// cin >> t;
while (t--) {
solve();
}
} | replace | 28 | 29 | 28 | 30 | TLE | |
p02709 | Python | Time Limit Exceeded | # 解説AC
n = int(input())
A = list(map(int, input().split()))
B = []
for i, e in enumerate(A):
B.append((e, i + 1))
B.sort(reverse=True)
# dp[i][j]: Aiまで入れた時、左にj個決めた時の最大値
dp = [[-1] * (n + 1) for _ in range(n + 1)]
dp[0][0] = 0
for i in range(n):
for j in range(i + 1): # 左の個数
k = i - j # 右の個数
ni = i + 1
val, idx = B[i]
dp[ni][j] = max(dp[ni][j], dp[i][j] + abs(n - k - idx) * val)
dp[ni][j + 1] = max(dp[ni][j + 1], dp[i][j] + abs(idx - (j + 1)) * val)
ans = 0
for i in range(n + 1):
ans = max(ans, dp[n][i])
print(ans)
| # 解説AC
import sys
input = sys.stdin.buffer.readline
n = int(input())
A = list(map(int, input().split()))
B = []
for i, e in enumerate(A):
B.append((e, i + 1))
B.sort(reverse=True)
# dp[i][j]: Aiまで入れた時、左にj個決めた時の最大値
dp = [[-1] * (n + 1) for _ in range(n + 1)]
dp[0][0] = 0
for i in range(n):
for j in range(i + 1): # 左の個数
k = i - j # 右の個数
ni = i + 1
val, idx = B[i]
dp[ni][j] = max(dp[ni][j], dp[i][j] + abs(n - k - idx) * val)
dp[ni][j + 1] = max(dp[ni][j + 1], dp[i][j] + abs(idx - (j + 1)) * val)
ans = 0
for i in range(n + 1):
ans = max(ans, dp[n][i])
print(ans)
| insert | 1 | 1 | 1 | 5 | TLE | |
p02709 | C++ | Time Limit Exceeded | // #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define INF 0x7fffffff
#define INFL 0x7fffffffffffffffLL
vector<pair<ll, ll>> a(2000);
ll n;
ll dp[2000][2000];
ll calc(ll x, ll y) {
int idx = x + ((n - 1) - y);
if (idx == n)
return 0;
// if (dp[x][y]!=0) return dp[x][y];
ll ans = max(abs(a[idx].second - x) * a[idx].first + calc(x + 1, y),
abs(a[idx].second - y) * a[idx].first + calc(x, y - 1));
dp[x][y] = ans;
return ans;
}
int main() {
ll b, c, i, j, k, m, x, y, ans = 0;
string str;
cin >> n;
for (i = 0; i < n; i++) {
cin >> j;
a[i] = make_pair(j, i);
}
sort(a.begin(), a.end());
reverse(a.begin(), a.end());
x = 0;
y = n - 1;
ans = calc(x, y);
cout << ans << endl;
}
| // #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define INF 0x7fffffff
#define INFL 0x7fffffffffffffffLL
vector<pair<ll, ll>> a(2000);
ll n;
ll dp[2000][2000];
ll calc(ll x, ll y) {
int idx = x + ((n - 1) - y);
if (idx == n)
return 0;
if (dp[x][y] != 0)
return dp[x][y];
ll ans = max(abs(a[idx].second - x) * a[idx].first + calc(x + 1, y),
abs(a[idx].second - y) * a[idx].first + calc(x, y - 1));
dp[x][y] = ans;
return ans;
}
int main() {
ll b, c, i, j, k, m, x, y, ans = 0;
string str;
cin >> n;
for (i = 0; i < n; i++) {
cin >> j;
a[i] = make_pair(j, i);
}
sort(a.begin(), a.end());
reverse(a.begin(), a.end());
x = 0;
y = n - 1;
ans = calc(x, y);
cout << ans << endl;
}
| replace | 15 | 16 | 15 | 17 | TLE | |
p02709 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define SELECTER(_1, _2, _3, SELECT, ...) SELECT
#define REP1(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define REP2(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define REP(...) SELECTER(__VA_ARGS__, REP2, REP1, )(__VA_ARGS__)
#define MOD 1'000'000'007
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
os << "{";
for (size_t i = 0; i < v.size(); i++)
os << v[i] << (i + 1 == v.size() ? "" : ", ");
os << "}";
return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
return os << "{" << p.first << ", " << p.second << "}";
}
long long dp[2222][2222];
int main() {
int N;
cin >> N;
vector<pair<long long, long long>> A(N);
REP(i, N) {
long long a;
cin >> a;
A[i] = make_pair(a, i);
}
sort(A.begin(), A.end(), greater<pair<long long, long long>>());
vector<vector<long long>> dp(2222, vector<long long>(2222, -(1LL << 60)));
dp[0][N] = 0;
for (int i = 0; i < N; i++) {
long long x = A[i].first;
long long pos = A[i].second;
for (int l = 0; l < N; l++) {
int r = l + N - i;
dp[l + 1][r] = max(dp[l + 1][r], dp[l][r] + x * abs(pos - l));
dp[l][r - 1] = max(dp[l][r - 1], dp[l][r] + x * abs(pos - (r - 1)));
}
}
long long ans = 0;
for (int i = 0; i < N; i++)
ans = max(ans, dp[i][i]);
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define SELECTER(_1, _2, _3, SELECT, ...) SELECT
#define REP1(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define REP2(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define REP(...) SELECTER(__VA_ARGS__, REP2, REP1, )(__VA_ARGS__)
#define MOD 1'000'000'007
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
os << "{";
for (size_t i = 0; i < v.size(); i++)
os << v[i] << (i + 1 == v.size() ? "" : ", ");
os << "}";
return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
return os << "{" << p.first << ", " << p.second << "}";
}
long long dp[2222][2222];
int main() {
int N;
cin >> N;
vector<pair<long long, long long>> A(N);
REP(i, N) {
long long a;
cin >> a;
A[i] = make_pair(a, i);
}
sort(A.begin(), A.end(), greater<pair<long long, long long>>());
vector<vector<long long>> dp(2222, vector<long long>(2222, -(1LL << 60)));
dp[0][N] = 0;
for (int i = 0; i < N; i++) {
long long x = A[i].first;
long long pos = A[i].second;
for (int l = 0; l <= i; l++) {
int r = l + N - i;
dp[l + 1][r] = max(dp[l + 1][r], dp[l][r] + x * abs(pos - l));
dp[l][r - 1] = max(dp[l][r - 1], dp[l][r] + x * abs(pos - (r - 1)));
}
}
long long ans = 0;
for (int i = 0; i < N; i++)
ans = max(ans, dp[i][i]);
cout << ans << endl;
return 0;
}
| replace | 39 | 40 | 39 | 40 | 0 | |
p02709 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define F first
#define S second
#define eb emplace_back
#define PI 3.14159265
#define sz(c) (int)c.size()
#define len(c) (int)c.length()
#define hcf(a, b) __gcd(a, b)
#define all(c) c.rbegin(), c.rend()
#define vint vector<int>
#define vll vector<ll>
#define vpint vector<pair<int, int>>
#define vpll vector<pair<ll, ll>>
#define tr(container, it) \
for (typeof(container.begin()) it = container.begin(); \
it != container.end(); it++)
#define fr(it, a, b) for (int it = (a); it < (b); it++)
#define present(container, element) (container.find(element) != container.end())
#define cpresent(container, element) \
(find(all(container), element) != container.end())
#define maximum(a, b, c) (a > b ? (a > c ? a : c) : (b > c ? b : c))
#define minimum(a, b, c) (a < b ? (a < c ? a : c) : (b < c ? b : c))
typedef long long ll;
typedef long double ld;
typedef unsigned long long lu;
const ll mod = 1e9 + 7;
vector<vll> dp(3005, vll(3005, -1));
ll dfs(int nd, int l, int r, vpll &a) {
if (l > r) {
return 0;
} else if (dp[l][r] != -1) {
return dp[l][r];
}
return max(a[nd].F * abs(a[nd].S - l) + dfs(nd + 1, l + 1, r, a),
a[nd].F * abs(a[nd].S - r) + dfs(nd + 1, l, r - 1, a));
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, l, r;
cin >> n;
vpll a(n);
fr(i, 0, n) {
cin >> a[i].F;
a[i].S = i;
}
sort(all(a));
ll ans = dfs(0, 0, n - 1, a);
cout << ans << "\n";
return 0;
} | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define F first
#define S second
#define eb emplace_back
#define PI 3.14159265
#define sz(c) (int)c.size()
#define len(c) (int)c.length()
#define hcf(a, b) __gcd(a, b)
#define all(c) c.rbegin(), c.rend()
#define vint vector<int>
#define vll vector<ll>
#define vpint vector<pair<int, int>>
#define vpll vector<pair<ll, ll>>
#define tr(container, it) \
for (typeof(container.begin()) it = container.begin(); \
it != container.end(); it++)
#define fr(it, a, b) for (int it = (a); it < (b); it++)
#define present(container, element) (container.find(element) != container.end())
#define cpresent(container, element) \
(find(all(container), element) != container.end())
#define maximum(a, b, c) (a > b ? (a > c ? a : c) : (b > c ? b : c))
#define minimum(a, b, c) (a < b ? (a < c ? a : c) : (b < c ? b : c))
typedef long long ll;
typedef long double ld;
typedef unsigned long long lu;
const ll mod = 1e9 + 7;
vector<vll> dp(3005, vll(3005, -1));
ll dfs(int nd, int l, int r, vpll &a) {
if (l > r) {
return 0;
} else if (dp[l][r] != -1) {
return dp[l][r];
}
return dp[l][r] = max(a[nd].F * abs(a[nd].S - l) + dfs(nd + 1, l + 1, r, a),
a[nd].F * abs(a[nd].S - r) + dfs(nd + 1, l, r - 1, a));
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, l, r;
cin >> n;
vpll a(n);
fr(i, 0, n) {
cin >> a[i].F;
a[i].S = i;
}
sort(all(a));
ll ans = dfs(0, 0, n - 1, a);
cout << ans << "\n";
return 0;
} | replace | 37 | 39 | 37 | 39 | TLE | |
p02709 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <iostream>
#include <utility>
using namespace std;
int main() {
pair<long long, int> order[2020];
int n;
cin >> n;
for (int i = 0; i < n; i++) {
long long temp;
cin >> temp;
order[i] = make_pair(temp, i + 1);
}
sort(order, order + n);
reverse(order, order + n);
long long dp[1000][1000];
for (int x = 0; x <= n; x++) {
for (int y = 0; y <= n; y++) {
dp[x][y] = -1;
}
}
dp[0][0] = 0;
for (int i = 0; i < n; i++) {
long long k = order[i].first;
int bef = order[i].second;
for (int x = 0; x <= i; x++) {
int y = i - x;
dp[x + 1][y] = max(dp[x + 1][y], dp[x][y] + abs(bef - (x + 1)) * k);
dp[x][y + 1] = max(dp[x][y + 1], dp[x][y] + abs(bef - (n - y)) * k);
}
}
long long sum = 0;
for (int i = 0; i <= n; i++) {
sum = max(sum, dp[i][n - i]);
}
cout << sum << endl;
return 0;
} | #include <algorithm>
#include <cmath>
#include <iostream>
#include <utility>
using namespace std;
int main() {
pair<long long, int> order[2020];
int n;
cin >> n;
for (int i = 0; i < n; i++) {
long long temp;
cin >> temp;
order[i] = make_pair(temp, i + 1);
}
sort(order, order + n);
reverse(order, order + n);
long long dp[2020][2020];
for (int x = 0; x <= n; x++) {
for (int y = 0; y <= n; y++) {
dp[x][y] = -1;
}
}
dp[0][0] = 0;
for (int i = 0; i < n; i++) {
long long k = order[i].first;
int bef = order[i].second;
for (int x = 0; x <= i; x++) {
int y = i - x;
dp[x + 1][y] = max(dp[x + 1][y], dp[x][y] + abs(bef - (x + 1)) * k);
dp[x][y + 1] = max(dp[x][y + 1], dp[x][y] + abs(bef - (n - y)) * k);
}
}
long long sum = 0;
for (int i = 0; i <= n; i++) {
sum = max(sum, dp[i][n - i]);
}
cout << sum << endl;
return 0;
} | replace | 17 | 18 | 17 | 18 | 0 | |
p02709 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
const int mod = 1e9 + 7;
typedef long long ll;
const int N = 2e3 + 5;
int n;
std::vector<std::pair<ll, int>> v;
ll dp[N][N];
ll solve(int l, int r) {
if (dp[l][r] >= 0)
return dp[l][r];
if (r < l)
return dp[l][r] = 0;
int i = n - r + l; // 当前正在放第i个
return std::max(v[i].first * std::abs(v[i].second - l) + solve(l + 1, r),
v[i].first * std::abs(v[i].second - r) + solve(l, r - 1));
}
int main() {
// freopen("a.in","r",stdin);
scanf("%d", &n);
v.push_back(std::make_pair(0, 0));
for (int i = 1; i <= n; i++) {
ll j;
scanf("%lld", &j);
v.push_back(std::make_pair(j, i));
}
std::sort(v.rbegin(), v.rend() - 1);
memset(dp, -1, sizeof(dp));
printf("%lld\n", solve(1, n));
return 0;
}
| #include <bits/stdc++.h>
const int mod = 1e9 + 7;
typedef long long ll;
const int N = 2e3 + 5;
int n;
std::vector<std::pair<ll, int>> v;
ll dp[N][N];
ll solve(int l, int r) {
if (dp[l][r] >= 0)
return dp[l][r];
if (r < l)
return dp[l][r] = 0;
int i = n - r + l; // 当前正在放第i个
return dp[l][r] =
std::max(v[i].first * std::abs(v[i].second - l) + solve(l + 1, r),
v[i].first * std::abs(v[i].second - r) + solve(l, r - 1));
}
int main() {
// freopen("a.in","r",stdin);
scanf("%d", &n);
v.push_back(std::make_pair(0, 0));
for (int i = 1; i <= n; i++) {
ll j;
scanf("%lld", &j);
v.push_back(std::make_pair(j, i));
}
std::sort(v.rbegin(), v.rend() - 1);
memset(dp, -1, sizeof(dp));
printf("%lld\n", solve(1, n));
return 0;
}
| replace | 14 | 16 | 14 | 17 | TLE | |
p02709 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repl(i, l, r) for (ll i = (l); i < (r); i++)
#define per(i, n) for (ll i = n - 1; i >= 0; i--)
#define perl(i, r, l) for (ll i = r - 1; i >= l; i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x, vector<x>, greater<x>>
#define all(x) (x).begin(), (x).end()
#define CST(x) cout << fixed << setprecision(x)
#define vtpl(x, y, z) vector<tuple<x, y, z>>
#define rev(x) reverse(x);
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pl = pair<ll, ll>;
using vpl = vector<pl>;
using vvpl = vector<vpl>;
const ll MOD = 1000000007;
const ll MOD9 = 998244353;
const int inf = 1e9 + 10;
const ll INF = 4e18;
const ll dy[9] = {1, 0, -1, 0, 1, 1, -1, -1, 0};
const ll dx[9] = {0, -1, 0, 1, 1, -1, 1, -1, 0};
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
ll n;
vpl v;
ll memo[2010][2010];
ll dfs(ll l, ll r) {
if (l > r)
return 0;
ll now = n - (r + 1 - l);
ll ret1 = v[now].fi * abs(v[now].se - l);
ll ret2 = v[now].fi * abs(v[now].se - r);
ll ret = max(ret1 + dfs(l + 1, r), ret2 + dfs(l, r - 1));
memo[l][r] = ret;
return ret;
}
int main() {
cin >> n;
v.resize(n);
rep(i, n) {
ll a;
cin >> a;
v[i] = {a, i};
}
sort(all(v));
rev(all(v));
rep(i, 2010) rep(j, 2010) memo[i][j] = -1;
cout << dfs(0, n - 1) << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repl(i, l, r) for (ll i = (l); i < (r); i++)
#define per(i, n) for (ll i = n - 1; i >= 0; i--)
#define perl(i, r, l) for (ll i = r - 1; i >= l; i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x, vector<x>, greater<x>>
#define all(x) (x).begin(), (x).end()
#define CST(x) cout << fixed << setprecision(x)
#define vtpl(x, y, z) vector<tuple<x, y, z>>
#define rev(x) reverse(x);
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pl = pair<ll, ll>;
using vpl = vector<pl>;
using vvpl = vector<vpl>;
const ll MOD = 1000000007;
const ll MOD9 = 998244353;
const int inf = 1e9 + 10;
const ll INF = 4e18;
const ll dy[9] = {1, 0, -1, 0, 1, 1, -1, -1, 0};
const ll dx[9] = {0, -1, 0, 1, 1, -1, 1, -1, 0};
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
ll n;
vpl v;
ll memo[2010][2010];
ll dfs(ll l, ll r) {
if (l > r)
return 0;
if (memo[l][r] != -1)
return memo[l][r];
ll now = n - (r + 1 - l);
ll ret1 = v[now].fi * abs(v[now].se - l);
ll ret2 = v[now].fi * abs(v[now].se - r);
ll ret = max(ret1 + dfs(l + 1, r), ret2 + dfs(l, r - 1));
memo[l][r] = ret;
return ret;
}
int main() {
cin >> n;
v.resize(n);
rep(i, n) {
ll a;
cin >> a;
v[i] = {a, i};
}
sort(all(v));
rev(all(v));
rep(i, 2010) rep(j, 2010) memo[i][j] = -1;
cout << dfs(0, n - 1) << endl;
} | insert | 47 | 47 | 47 | 49 | TLE | |
p02709 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, int> P;
int n;
vector<P> A;
ll dp[2005][2005];
ll nex[2005][2005];
ll ans;
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
ll a;
cin >> a;
A.push_back(P(a, i));
}
sort(A.begin(), A.end(), greater<P>());
// for(int i = 0; i < n; i++) cout << A[i].first <<" "<<A[i].second<< endl;
for (ll i = 0; i < n; i++) {
for (ll j = 0; j <= i + 1; j++) {
ll k = i - j + 1;
if (j == 0) {
nex[j][k] = dp[j][k - 1] + abs(n - k - A[i].second) * A[i].first;
continue;
}
if (k == 0) {
nex[j][k] = dp[j - 1][k] + abs(A[i].second - j + 1) * A[i].first;
continue;
}
nex[j][k] = max(dp[j - 1][k] + abs(A[i].second - j + 1) * A[i].first,
dp[j][k - 1] + abs(n - k - A[i].second) * A[i].first);
}
swap(nex, dp);
/* cout << i << endl;
for(int ii = 0; ii <= i+1; ii++) {
int j = i - ii + 1;
cout <<"dp["<<ii<<"]["<<j<<"] = "<< dp[ii][j] << endl;
}*/
}
for (int i = 0; i <= n; i++) {
int j = n - i;
ans = max(ans, dp[i][j]);
// cout <<"dp["<<i<<"]["<<j<<"] = "<< dp[i][j] << endl;
}
cout << ans << endl;
}
| #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, int> P;
int n;
vector<P> A;
ll dp[2005][2005];
ll nex[2005][2005];
ll ans;
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
ll a;
cin >> a;
A.push_back(P(a, i));
}
sort(A.begin(), A.end(), greater<P>());
// for(int i = 0; i < n; i++) cout << A[i].first <<" "<<A[i].second<< endl;
for (ll i = 0; i < n; i++) {
for (ll j = 0; j <= i + 1; j++) {
ll k = i - j + 1;
if (j == 0) {
nex[j][k] = dp[j][k - 1] + abs(n - k - A[i].second) * A[i].first;
continue;
}
if (k == 0) {
nex[j][k] = dp[j - 1][k] + abs(A[i].second - j + 1) * A[i].first;
continue;
}
nex[j][k] = max(dp[j - 1][k] + abs(A[i].second - j + 1) * A[i].first,
dp[j][k - 1] + abs(n - k - A[i].second) * A[i].first);
}
for (ll j = 0; j <= i + 1; j++) {
ll k = i - j + 1;
dp[j][k] = nex[j][k];
}
/* cout << i << endl;
for(int ii = 0; ii <= i+1; ii++) {
int j = i - ii + 1;
cout <<"dp["<<ii<<"]["<<j<<"] = "<< dp[ii][j] << endl;
}*/
}
for (int i = 0; i <= n; i++) {
int j = n - i;
ans = max(ans, dp[i][j]);
// cout <<"dp["<<i<<"]["<<j<<"] = "<< dp[i][j] << endl;
}
cout << ans << endl;
}
| replace | 41 | 42 | 41 | 45 | TLE | |
p02709 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <math.h>
#include <vector>
using namespace std;
long long dp[2005][2005] = {};
int main() {
long long n;
cin >> n;
vector<pair<long long, long long>> a(n + 1);
dp[2005][2005] = {};
for (long long i = 1; i < n + 1; ++i)
cin >> a[i].first, a[i].second = i;
sort(a.begin() + 1, a.end(), greater<pair<long long, long long>>());
for (long long i = 1; i < n + 1; ++i) {
for (long long j = 0; j < i; ++j) {
long long L = j;
long long R = i - 1 - j;
dp[L + 1][R] =
max(dp[L + 1][R], dp[L][R] + abs(L + 1 - a[i].second) * a[i].first);
R = j;
L = i - 1 - j;
dp[L][R + 1] =
max(dp[L][R + 1], dp[L][R] + abs(n - R - a[i].second) * a[i].first);
}
}
long long ans = 0;
for (long long i = 1; i < n + 1; ++i) {
ans = max(ans, dp[i][n - i]);
}
cout << ans << endl;
return 0;
} | #include <algorithm>
#include <iostream>
#include <math.h>
#include <vector>
using namespace std;
long long dp[2005][2005] = {};
int main() {
long long n;
cin >> n;
vector<pair<long long, long long>> a(n + 1);
for (long long i = 1; i < n + 1; ++i)
cin >> a[i].first, a[i].second = i;
sort(a.begin() + 1, a.end(), greater<pair<long long, long long>>());
for (long long i = 1; i < n + 1; ++i) {
for (long long j = 0; j < i; ++j) {
long long L = j;
long long R = i - 1 - j;
dp[L + 1][R] =
max(dp[L + 1][R], dp[L][R] + abs(L + 1 - a[i].second) * a[i].first);
R = j;
L = i - 1 - j;
dp[L][R + 1] =
max(dp[L][R + 1], dp[L][R] + abs(n - R - a[i].second) * a[i].first);
}
}
long long ans = 0;
for (long long i = 1; i < n + 1; ++i) {
ans = max(ans, dp[i][n - i]);
}
cout << ans << endl;
return 0;
} | delete | 14 | 15 | 14 | 14 | -11 | |
p02709 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#if __has_include(<boost/multiprecision/cpp_int.hpp>)
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
using bll = boost::multiprecision::cpp_int;
using bdouble = boost::multiprecision::cpp_dec_float_100;
using namespace boost::multiprecision;
#endif
#ifdef LOCAL_DEV
void debug_impl() { std::cerr << '\n'; }
template <typename Head, typename... Tail>
void debug_impl(Head head, Tail... tail) {
std::cerr << " " << head << (sizeof...(tail) ? "," : "");
debug_impl(tail...);
}
#define debug(...) \
do { \
std::cerr << "(" << #__VA_ARGS__ << ") ="; \
debug_impl(__VA_ARGS__); \
} while (false)
#else
#define debug(...) \
do { \
} while (false)
#endif
#ifdef LOCAL_TEST
#define BOOST_STACKTRACE_USE_ADDR2LINE
#define BOOST_STACKTRACE_ADDR2LINE_LOCATION \
/ usr / local / opt / binutils / bin / addr2line
#define _GNU_SOURCE
#include <boost/stacktrace.hpp>
namespace std {
template <typename T> class dvector : public std::vector<T> {
public:
dvector() : std::vector<T>() {}
explicit dvector(size_t n, const T &value = T()) : std::vector<T>(n, value) {}
dvector(const std::vector<T> &v) : std::vector<T>(v) {}
dvector(const std::initializer_list<T> il) : std::vector<T>(il) {}
dvector(const std::string::iterator first, const std::string::iterator last)
: std::vector<T>(first, last) {}
dvector(const typename std::vector<T>::iterator first,
const typename std::vector<T>::iterator last)
: std::vector<T>(first, last) {}
dvector(const typename std::vector<T>::reverse_iterator first,
const typename std::vector<T>::reverse_iterator last)
: std::vector<T>(first, last) {}
dvector(const typename std::vector<T>::const_iterator first,
const typename std::vector<T>::const_iterator last)
: std::vector<T>(first, last) {}
dvector(const typename std::vector<T>::const_reverse_iterator first,
const typename std::vector<T>::const_reverse_iterator last)
: std::vector<T>(first, last) {}
T &operator[](size_t n) {
try {
return this->at(n);
} catch (const std::exception &e) {
std::cerr << boost::stacktrace::stacktrace() << '\n';
return this->at(n);
}
}
const T &operator[](size_t n) const {
try {
return this->at(n);
} catch (const std::exception &e) {
std::cerr << boost::stacktrace::stacktrace() << '\n';
return this->at(n);
}
}
};
} // namespace std
class dbool {
private:
bool boolvalue;
public:
dbool() : boolvalue(false) {}
dbool(bool b) : boolvalue(b) {}
dbool(const dbool &) = default;
dbool(dbool &&) = default;
dbool &operator=(const dbool &) = default;
dbool &operator=(dbool &&) = default;
operator bool &() { return boolvalue; }
operator const bool &() const { return boolvalue; }
};
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::dvector<T> &v) {
for (size_t i = 0; i < v.size(); ++i) {
s << v[i];
if (i < v.size() - 1)
s << "\t";
}
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s,
const std::dvector<std::dvector<T>> &vv) {
s << "\\\n";
for (size_t i = 0; i < vv.size(); ++i) {
s << vv[i] << "\n";
}
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::deque<T> &v) {
for (size_t i = 0; i < v.size(); ++i) {
s << v[i];
if (i < v.size() - 1)
s << "\t";
}
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::set<T> &se) {
s << "{ ";
for (auto itr = se.begin(); itr != se.end(); ++itr) {
s << (*itr) << "\t";
}
s << "}";
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::multiset<T> &se) {
s << "{ ";
for (auto itr = se.begin(); itr != se.end(); ++itr) {
s << (*itr) << "\t";
}
s << "}";
return s;
}
template <typename T, size_t N>
std::ostream &operator<<(std::ostream &s, const std::array<T, N> &a) {
s << "{ ";
for (size_t i = 0; i < N; ++i) {
s << a[i] << "\t";
}
s << "}";
return s;
}
template <typename T1, typename T2>
std::ostream &operator<<(std::ostream &s, const std::map<T1, T2> &m) {
s << "{\n";
for (auto itr = m.begin(); itr != m.end(); ++itr) {
s << "\t" << (*itr).first << " : " << (*itr).second << "\n";
}
s << "}";
return s;
}
template <typename T1, typename T2>
std::ostream &operator<<(std::ostream &s, const std::pair<T1, T2> &p) {
return s << "(" << p.first << ", " << p.second << ")";
}
#define vector dvector
#define bool dbool
class SIGFPE_exception : std::exception {};
class SIGSEGV_exception : std::exception {};
void catch_SIGFPE([[maybe_unused]] int e) {
std::cerr << boost::stacktrace::stacktrace() << '\n';
throw SIGFPE_exception();
}
void catch_SIGSEGV([[maybe_unused]] int e) {
std::cerr << boost::stacktrace::stacktrace() << '\n';
throw SIGSEGV_exception();
}
signed convertedmain();
signed main() {
signal(SIGFPE, catch_SIGFPE);
signal(SIGSEGV, catch_SIGSEGV);
return convertedmain();
}
#define main() convertedmain()
#endif
// #define int long long
using ll = long long;
// constexpr int INF = 1e9;//INT_MAX=(1<<31)-1=2147483647
constexpr ll INF = (ll)1e18; //(1LL<<63)-1=9223372036854775807
constexpr ll MOD = (ll)1e9 + 7;
constexpr double EPS = 1e-9;
constexpr ll dx[4] = {1LL, 0LL, -1LL, 0LL};
constexpr ll dy[4] = {0LL, 1LL, 0LL, -1LL};
constexpr ll dx8[8] = {1LL, 0LL, -1LL, 0LL, 1LL, 1LL, -1LL, -1LL};
constexpr ll dy8[8] = {0LL, 1LL, 0LL, -1LL, 1LL, -1LL, 1LL, -1LL};
#define rep(i, n) for (ll i = 0, i##_length = (n); i < i##_length; ++i)
#define repeq(i, n) for (ll i = 1, i##_length = (n); i <= i##_length; ++i)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; --i)
#define rrepeq(i, n) for (ll i = (n); i >= 1; --i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
void p() { std::cout << '\n'; }
template <typename Head, typename... Tail> void p(Head head, Tail... tail) {
std::cout << head << (sizeof...(tail) ? " " : "");
p(tail...);
}
template <typename T> inline void pv(std::vector<T> &v) {
for (ll i = 0, N = v.size(); i < N; i++)
std::cout << v[i] << " \n"[i == N - 1];
}
template <typename T> inline bool chmax(T &a, T b) {
return a < b && (a = b, true);
}
template <typename T> inline bool chmin(T &a, T b) {
return a > b && (a = b, true);
}
template <typename T> inline void uniq(std::vector<T> &v) {
v.erase(std::unique(v.begin(), v.end()), v.end());
}
/*-----8<-----template-----8<-----*/
/*-----8<-----library-----8<-----*/
constexpr ll N_MAX = 2000;
constexpr ll A_I_MAX = 1e9;
ll N;
vector<ll> A;
void solve() {
scanf("%lld", &N);
A.assign(N, 0);
rep(i, N) { scanf("%lld", &A[i]); }
vector<pair<ll, ll>> v(N);
rep(i, N) { v[i] = {A[i], i}; }
sort(rall(v));
vector<vector<ll>> dp(N, vector<ll>(N, -INF));
auto f = [&](auto &&f, ll index, ll left) {
if (index >= N)
return 0LL;
if (dp[index][left] != -INF)
return dp[index][left];
ll s = f(f, index + 1, left + 1) +
v[index].first * abs(left - v[index].second);
ll t = f(f, index + 1, left) +
v[index].first * abs(N - 1 - (index - left) - v[index].second);
ll ans = max(s, t);
return ans;
};
ll ans = f(f, 0, 0);
printf("%lld\n", ans);
}
// https://atcoder.jp/contests/abc163/tasks/abc163_e
signed main() {
solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#if __has_include(<boost/multiprecision/cpp_int.hpp>)
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
using bll = boost::multiprecision::cpp_int;
using bdouble = boost::multiprecision::cpp_dec_float_100;
using namespace boost::multiprecision;
#endif
#ifdef LOCAL_DEV
void debug_impl() { std::cerr << '\n'; }
template <typename Head, typename... Tail>
void debug_impl(Head head, Tail... tail) {
std::cerr << " " << head << (sizeof...(tail) ? "," : "");
debug_impl(tail...);
}
#define debug(...) \
do { \
std::cerr << "(" << #__VA_ARGS__ << ") ="; \
debug_impl(__VA_ARGS__); \
} while (false)
#else
#define debug(...) \
do { \
} while (false)
#endif
#ifdef LOCAL_TEST
#define BOOST_STACKTRACE_USE_ADDR2LINE
#define BOOST_STACKTRACE_ADDR2LINE_LOCATION \
/ usr / local / opt / binutils / bin / addr2line
#define _GNU_SOURCE
#include <boost/stacktrace.hpp>
namespace std {
template <typename T> class dvector : public std::vector<T> {
public:
dvector() : std::vector<T>() {}
explicit dvector(size_t n, const T &value = T()) : std::vector<T>(n, value) {}
dvector(const std::vector<T> &v) : std::vector<T>(v) {}
dvector(const std::initializer_list<T> il) : std::vector<T>(il) {}
dvector(const std::string::iterator first, const std::string::iterator last)
: std::vector<T>(first, last) {}
dvector(const typename std::vector<T>::iterator first,
const typename std::vector<T>::iterator last)
: std::vector<T>(first, last) {}
dvector(const typename std::vector<T>::reverse_iterator first,
const typename std::vector<T>::reverse_iterator last)
: std::vector<T>(first, last) {}
dvector(const typename std::vector<T>::const_iterator first,
const typename std::vector<T>::const_iterator last)
: std::vector<T>(first, last) {}
dvector(const typename std::vector<T>::const_reverse_iterator first,
const typename std::vector<T>::const_reverse_iterator last)
: std::vector<T>(first, last) {}
T &operator[](size_t n) {
try {
return this->at(n);
} catch (const std::exception &e) {
std::cerr << boost::stacktrace::stacktrace() << '\n';
return this->at(n);
}
}
const T &operator[](size_t n) const {
try {
return this->at(n);
} catch (const std::exception &e) {
std::cerr << boost::stacktrace::stacktrace() << '\n';
return this->at(n);
}
}
};
} // namespace std
class dbool {
private:
bool boolvalue;
public:
dbool() : boolvalue(false) {}
dbool(bool b) : boolvalue(b) {}
dbool(const dbool &) = default;
dbool(dbool &&) = default;
dbool &operator=(const dbool &) = default;
dbool &operator=(dbool &&) = default;
operator bool &() { return boolvalue; }
operator const bool &() const { return boolvalue; }
};
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::dvector<T> &v) {
for (size_t i = 0; i < v.size(); ++i) {
s << v[i];
if (i < v.size() - 1)
s << "\t";
}
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s,
const std::dvector<std::dvector<T>> &vv) {
s << "\\\n";
for (size_t i = 0; i < vv.size(); ++i) {
s << vv[i] << "\n";
}
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::deque<T> &v) {
for (size_t i = 0; i < v.size(); ++i) {
s << v[i];
if (i < v.size() - 1)
s << "\t";
}
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::set<T> &se) {
s << "{ ";
for (auto itr = se.begin(); itr != se.end(); ++itr) {
s << (*itr) << "\t";
}
s << "}";
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::multiset<T> &se) {
s << "{ ";
for (auto itr = se.begin(); itr != se.end(); ++itr) {
s << (*itr) << "\t";
}
s << "}";
return s;
}
template <typename T, size_t N>
std::ostream &operator<<(std::ostream &s, const std::array<T, N> &a) {
s << "{ ";
for (size_t i = 0; i < N; ++i) {
s << a[i] << "\t";
}
s << "}";
return s;
}
template <typename T1, typename T2>
std::ostream &operator<<(std::ostream &s, const std::map<T1, T2> &m) {
s << "{\n";
for (auto itr = m.begin(); itr != m.end(); ++itr) {
s << "\t" << (*itr).first << " : " << (*itr).second << "\n";
}
s << "}";
return s;
}
template <typename T1, typename T2>
std::ostream &operator<<(std::ostream &s, const std::pair<T1, T2> &p) {
return s << "(" << p.first << ", " << p.second << ")";
}
#define vector dvector
#define bool dbool
class SIGFPE_exception : std::exception {};
class SIGSEGV_exception : std::exception {};
void catch_SIGFPE([[maybe_unused]] int e) {
std::cerr << boost::stacktrace::stacktrace() << '\n';
throw SIGFPE_exception();
}
void catch_SIGSEGV([[maybe_unused]] int e) {
std::cerr << boost::stacktrace::stacktrace() << '\n';
throw SIGSEGV_exception();
}
signed convertedmain();
signed main() {
signal(SIGFPE, catch_SIGFPE);
signal(SIGSEGV, catch_SIGSEGV);
return convertedmain();
}
#define main() convertedmain()
#endif
// #define int long long
using ll = long long;
// constexpr int INF = 1e9;//INT_MAX=(1<<31)-1=2147483647
constexpr ll INF = (ll)1e18; //(1LL<<63)-1=9223372036854775807
constexpr ll MOD = (ll)1e9 + 7;
constexpr double EPS = 1e-9;
constexpr ll dx[4] = {1LL, 0LL, -1LL, 0LL};
constexpr ll dy[4] = {0LL, 1LL, 0LL, -1LL};
constexpr ll dx8[8] = {1LL, 0LL, -1LL, 0LL, 1LL, 1LL, -1LL, -1LL};
constexpr ll dy8[8] = {0LL, 1LL, 0LL, -1LL, 1LL, -1LL, 1LL, -1LL};
#define rep(i, n) for (ll i = 0, i##_length = (n); i < i##_length; ++i)
#define repeq(i, n) for (ll i = 1, i##_length = (n); i <= i##_length; ++i)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; --i)
#define rrepeq(i, n) for (ll i = (n); i >= 1; --i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
void p() { std::cout << '\n'; }
template <typename Head, typename... Tail> void p(Head head, Tail... tail) {
std::cout << head << (sizeof...(tail) ? " " : "");
p(tail...);
}
template <typename T> inline void pv(std::vector<T> &v) {
for (ll i = 0, N = v.size(); i < N; i++)
std::cout << v[i] << " \n"[i == N - 1];
}
template <typename T> inline bool chmax(T &a, T b) {
return a < b && (a = b, true);
}
template <typename T> inline bool chmin(T &a, T b) {
return a > b && (a = b, true);
}
template <typename T> inline void uniq(std::vector<T> &v) {
v.erase(std::unique(v.begin(), v.end()), v.end());
}
/*-----8<-----template-----8<-----*/
/*-----8<-----library-----8<-----*/
constexpr ll N_MAX = 2000;
constexpr ll A_I_MAX = 1e9;
ll N;
vector<ll> A;
void solve() {
scanf("%lld", &N);
A.assign(N, 0);
rep(i, N) { scanf("%lld", &A[i]); }
vector<pair<ll, ll>> v(N);
rep(i, N) { v[i] = {A[i], i}; }
sort(rall(v));
vector<vector<ll>> dp(N, vector<ll>(N, -INF));
auto f = [&](auto &&f, ll index, ll left) {
if (index >= N)
return 0LL;
if (dp[index][left] != -INF)
return dp[index][left];
ll s = f(f, index + 1, left + 1) +
v[index].first * abs(left - v[index].second);
ll t = f(f, index + 1, left) +
v[index].first * abs(N - 1 - (index - left) - v[index].second);
ll ans = max(s, t);
return dp[index][left] = ans;
};
ll ans = f(f, 0, 0);
printf("%lld\n", ans);
}
// https://atcoder.jp/contests/abc163/tasks/abc163_e
signed main() {
solve();
return 0;
}
| replace | 237 | 238 | 237 | 238 | TLE | |
p02709 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#define mes(a, b) memset(a, b, sizeof a)
#define pb push_back
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const int maxn = 2e6 + 10;
const double eps = 1e-1;
ll n;
ll l, r;
ll max(ll x, ll y) { return x > y ? x : y; }
struct Node {
ll x, id;
bool operator<(const Node &p) const { return x > p.x; }
} a[maxn];
ll dp[210][2100];
int main() {
scanf("%lld", &n);
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i].x);
a[i].id = i;
}
sort(a + 1, a + n + 1);
ll tmp = 0, ans = 0;
Node b;
for (int i = 1; i <= n; i++) {
b = a[i];
for (ll l = 1, r = n - i + 1; r <= n; l++, r++) {
dp[i][l] = max(dp[i][l], dp[i - 1][l - 1] + abs(b.id - l) * b.x);
dp[i][l - 1] = max(dp[i][l - 1], dp[i - 1][l - 1] + abs(b.id - r) * b.x);
}
}
for (int i = 1; i <= n; i++)
ans = max(ans, dp[n][i]);
printf("%lld\n", ans);
return 0;
}
| #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#define mes(a, b) memset(a, b, sizeof a)
#define pb push_back
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const int maxn = 2e6 + 10;
const double eps = 1e-1;
ll n;
ll l, r;
ll max(ll x, ll y) { return x > y ? x : y; }
struct Node {
ll x, id;
bool operator<(const Node &p) const { return x > p.x; }
} a[maxn];
ll dp[2100][2100];
int main() {
scanf("%lld", &n);
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i].x);
a[i].id = i;
}
sort(a + 1, a + n + 1);
ll tmp = 0, ans = 0;
Node b;
for (int i = 1; i <= n; i++) {
b = a[i];
for (ll l = 1, r = n - i + 1; r <= n; l++, r++) {
dp[i][l] = max(dp[i][l], dp[i - 1][l - 1] + abs(b.id - l) * b.x);
dp[i][l - 1] = max(dp[i][l - 1], dp[i - 1][l - 1] + abs(b.id - r) * b.x);
}
}
for (int i = 1; i <= n; i++)
ans = max(ans, dp[n][i]);
printf("%lld\n", ans);
return 0;
}
| replace | 18 | 19 | 18 | 19 | 0 | |
p02709 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define orep(i, n) for (int i = 1; i <= (n); ++i) // one rep
#define drep(i, n) for (int i = (n)-1; i >= 0; --i) // down rep
#define srep(i, s, t) for (int i = (s); i < (t); ++i) // set rep
#define rng(x) (x).begin(), (x).end() // range
#define rrng(a) (a).rbegin(), (a).rend() // reverse range
#define isin(x, l, r) ((l) <= (x) && (x) < (r))
#define ru(x, y) (((x) + (y)-1) / (y)) // round up
#define fi first
#define se second
#define eb emplace_back
#define fcout cout << fixed << setprecision(15)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vp = vector<P>;
using vl = vector<ll>;
template <typename T> void Yes(T flag) {
cout << (flag ? "Yes" : "No") << endl;
}
template <typename T> void pv(vector<T> vec) {
cout << "[";
for (auto &v : vec) {
cout << v << ",";
}
cout << "]" << endl;
}
template <> void pv(vector<P> vec) {
cout << "[";
for (auto &v : vec) {
cout << "<" << v.fi << "," << v.se << ">"
<< ",";
}
cout << "]" << endl;
}
ll dp[2005][2005];
int main() {
int n;
cin >> n;
vp a(n);
rep(i, n) {
int t;
cin >> t;
a.at(i) = P(t, i);
}
sort(rrng(a));
rep(i, n) rep(j, n) {
if (i + j > n)
continue;
ll u = a.at(i + j).fi;
ll d = a.at(i + j).se;
dp[i][j + 1] = max(dp[i][j] + u * abs(n - d - j - 1), dp[i][j + 1]);
dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + u * abs(d - i));
}
ll res = 0;
rep(i, n + 1) { res = max(res, dp[i][n - i]); }
cout << res << endl;
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define orep(i, n) for (int i = 1; i <= (n); ++i) // one rep
#define drep(i, n) for (int i = (n)-1; i >= 0; --i) // down rep
#define srep(i, s, t) for (int i = (s); i < (t); ++i) // set rep
#define rng(x) (x).begin(), (x).end() // range
#define rrng(a) (a).rbegin(), (a).rend() // reverse range
#define isin(x, l, r) ((l) <= (x) && (x) < (r))
#define ru(x, y) (((x) + (y)-1) / (y)) // round up
#define fi first
#define se second
#define eb emplace_back
#define fcout cout << fixed << setprecision(15)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vp = vector<P>;
using vl = vector<ll>;
template <typename T> void Yes(T flag) {
cout << (flag ? "Yes" : "No") << endl;
}
template <typename T> void pv(vector<T> vec) {
cout << "[";
for (auto &v : vec) {
cout << v << ",";
}
cout << "]" << endl;
}
template <> void pv(vector<P> vec) {
cout << "[";
for (auto &v : vec) {
cout << "<" << v.fi << "," << v.se << ">"
<< ",";
}
cout << "]" << endl;
}
ll dp[2005][2005];
int main() {
int n;
cin >> n;
vp a(n);
rep(i, n) {
int t;
cin >> t;
a.at(i) = P(t, i);
}
sort(rrng(a));
rep(i, n) rep(j, n) {
if (i + j > n - 1)
continue;
ll u = a.at(i + j).fi;
ll d = a.at(i + j).se;
dp[i][j + 1] = max(dp[i][j] + u * abs(n - d - j - 1), dp[i][j + 1]);
dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + u * abs(d - i));
}
ll res = 0;
rep(i, n + 1) { res = max(res, dp[i][n - i]); }
cout << res << endl;
return 0;
}
| replace | 57 | 58 | 57 | 58 | -6 | terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check: __n (which is 4) >= this->size() (which is 4)
|
p02709 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define name ""
#define ini \
freopen(name ".inp", "r", stdin); \
freopen(name ".out", "w", stdout)
#define foe(it, c) for (__typeof(c.begin()) it = c.begin(); it != c.end(); it++)
#define long long long
#define db double
#define pii pair<int, int>
#define pll pair<long, long>
#define all(c) c.begin(), c.end()
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0)
const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const int dx[4] = {0, 0, -1, 1};
const int dy[4] = {-1, 1, 0, 0};
const int N = 2001;
int n;
long dp[N][N];
pll a[N];
int main() {
fastio;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i].first;
a[i].second = i;
}
sort(a + 1, a + 1 + n);
reverse(a + 1, a + 1 + n);
long res = 0;
for (int x = 0; x <= n; x++) {
for (int y = 0; y <= n; y++) {
int cur = x + y;
if (cur == n)
res = max(res, dp[x][y]);
else {
cur++;
dp[x + 1][y] =
max(dp[x + 1][y],
dp[x][y] + abs(a[cur].second - (x + 1)) * a[cur].first);
dp[x][y + 1] =
max(dp[x][y + 1],
dp[x][y] + abs(a[cur].second - (n - y)) * a[cur].first);
if (x == 0 && y == 0) {
}
}
}
}
cout << res;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define name ""
#define ini \
freopen(name ".inp", "r", stdin); \
freopen(name ".out", "w", stdout)
#define foe(it, c) for (__typeof(c.begin()) it = c.begin(); it != c.end(); it++)
#define long long long
#define db double
#define pii pair<int, int>
#define pll pair<long, long>
#define all(c) c.begin(), c.end()
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0)
const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const int dx[4] = {0, 0, -1, 1};
const int dy[4] = {-1, 1, 0, 0};
const int N = 2002;
int n;
long dp[N][N];
pll a[N];
int main() {
fastio;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i].first;
a[i].second = i;
}
sort(a + 1, a + 1 + n);
reverse(a + 1, a + 1 + n);
long res = 0;
for (int x = 0; x <= n; x++) {
for (int y = 0; y <= n; y++) {
int cur = x + y;
if (cur == n)
res = max(res, dp[x][y]);
else {
cur++;
dp[x + 1][y] =
max(dp[x + 1][y],
dp[x][y] + abs(a[cur].second - (x + 1)) * a[cur].first);
dp[x][y + 1] =
max(dp[x][y + 1],
dp[x][y] + abs(a[cur].second - (n - y)) * a[cur].first);
if (x == 0 && y == 0) {
}
}
}
}
cout << res;
return 0;
} | replace | 22 | 23 | 22 | 23 | 0 | |
p02709 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL_DEBUG
#include "LOCAL_DEBUG.hpp"
#endif
#define int long long
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <class T, class V>
typename enable_if<is_class<T>::value == 0>::type fill(T &t, const V &v) {
t = v;
}
template <class T, class V>
typename enable_if<is_class<T>::value != 0>::type fill(T &t, const V &v) {
for (auto &e : t)
fill(e, v);
}
// auto v = make_vec<int>(h, w);
// fill(v, 0);
using P = pair<int, int>;
signed main() {
int n;
cin >> n;
vector<int> a(n);
vector<P> p(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
p[i] = {a[i], i};
}
sort(p.rbegin(), p.rend());
auto dp = make_vec<int>(n + 1, n + 1);
auto dfs = [&](auto &&dfs, int l, int r) -> void {
if (l + r == n)
return;
int i = l + r;
dp[l + 1][r] = max(dp[l + 1][r], dp[l][r] + p[i].first * (p[i].second - l));
dp[l][r + 1] =
max(dp[l][r + 1], dp[l][r] + p[i].first * ((n - 1 - r) - p[i].second));
dfs(dfs, l + 1, r);
dfs(dfs, l, r + 1);
};
dfs(dfs, 0, 0);
int ans = 0;
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
if (i + j == n)
ans = max(ans, dp[i][j]);
}
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL_DEBUG
#include "LOCAL_DEBUG.hpp"
#endif
#define int long long
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <class T, class V>
typename enable_if<is_class<T>::value == 0>::type fill(T &t, const V &v) {
t = v;
}
template <class T, class V>
typename enable_if<is_class<T>::value != 0>::type fill(T &t, const V &v) {
for (auto &e : t)
fill(e, v);
}
// auto v = make_vec<int>(h, w);
// fill(v, 0);
using P = pair<int, int>;
signed main() {
int n;
cin >> n;
vector<int> a(n);
vector<P> p(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
p[i] = {a[i], i};
}
sort(p.rbegin(), p.rend());
auto dp = make_vec<int>(n + 1, n + 1);
for (int i = 0; i < n; i++) {
for (int j = 0; i + j < n; j++) {
dp[i + 1][j] =
max(dp[i + 1][j], dp[i][j] + p[i + j].first * (p[i + j].second - i));
dp[i][j + 1] =
max(dp[i][j + 1],
dp[i][j] + p[i + j].first * ((n - 1 - j) - p[i + j].second));
}
}
int ans = 0;
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
if (i + j == n)
ans = max(ans, dp[i][j]);
}
}
cout << ans << endl;
return 0;
} | replace | 38 | 49 | 38 | 47 | TLE | |
p02709 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef pair<long long, long long> P;
int main() {
int N;
cin >> N;
vector<P> a(N);
for (int i = 0; i < N; i++) {
long long b;
cin >> b;
a[i] = P(b, i);
}
sort(a.begin(), a.end());
long long dp[N][N];
long long ans = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++)
dp[i][j] = 0;
}
dp[0][0] = 0;
for (int i = 1; i <= N; i++) {
for (int j = 0; j <= i; j++) {
int k = i - j;
if (j == 0)
dp[j][k] = dp[j][k - 1] + abs(N - k - a[N - i].second) * a[N - i].first;
else if (k == 0)
dp[j][k] = dp[j - 1][k] + abs(j - 1 - a[N - i].second) * a[N - i].first;
else {
long long m1 =
dp[j][k - 1] + abs(N - k - a[N - i].second) * a[N - i].first;
long long m2 =
dp[j - 1][k] + abs(j - 1 - a[N - i].second) * a[N - i].first;
dp[j][k] = max(m1, m2);
}
}
}
for (int i = 0; i <= N; i++)
ans = max(ans, dp[i][N - i]);
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef pair<long long, long long> P;
int main() {
int N;
cin >> N;
vector<P> a(N);
for (int i = 0; i < N; i++) {
long long b;
cin >> b;
a[i] = P(b, i);
}
sort(a.begin(), a.end());
long long dp[N + 1][N + 1];
long long ans = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++)
dp[i][j] = 0;
}
dp[0][0] = 0;
for (int i = 1; i <= N; i++) {
for (int j = 0; j <= i; j++) {
int k = i - j;
if (j == 0)
dp[j][k] = dp[j][k - 1] + abs(N - k - a[N - i].second) * a[N - i].first;
else if (k == 0)
dp[j][k] = dp[j - 1][k] + abs(j - 1 - a[N - i].second) * a[N - i].first;
else {
long long m1 =
dp[j][k - 1] + abs(N - k - a[N - i].second) * a[N - i].first;
long long m2 =
dp[j - 1][k] + abs(j - 1 - a[N - i].second) * a[N - i].first;
dp[j][k] = max(m1, m2);
}
}
}
for (int i = 0; i <= N; i++)
ans = max(ans, dp[i][N - i]);
cout << ans << endl;
} | replace | 13 | 14 | 13 | 14 | 0 | |
p02709 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
// #define int long long
#define rep(i, n) for (long long i = (long long)(0); i < (long long)(n); ++i)
#define reps(i, n) for (long long i = (long long)(1); i <= (long long)(n); ++i)
#define rrep(i, n) for (long long i = ((long long)(n)-1); i >= 0; i--)
#define rreps(i, n) for (long long i = ((long long)(n)); i > 0; i--)
#define irep(i, m, n) \
for (long long i = (long long)(m); i < (long long)(n); ++i)
#define ireps(i, m, n) \
for (long long i = (long long)(m); i <= (long long)(n); ++i)
#define SORT(v, n) sort(v, v + n);
#define REVERSE(v, n) reverse(v, v + n);
#define vsort(v) sort(v.begin(), v.end());
#define all(v) v.begin(), v.end()
#define mp(n, m) make_pair(n, m);
#define cout(d) cout << d << endl;
#define coutd(d) cout << std::setprecision(10) << d << endl;
#define cinline(n) getline(cin, n);
#define replace_all(s, b, a) replace(s.begin(), s.end(), b, a);
#define PI (acos(-1))
#define FILL(v, n, x) fill(v, v + n, x);
#define sz(x) long long(x.size())
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vs = vector<string>;
using vpll = vector<pair<ll, ll>>;
using vtp = vector<tuple<ll, ll, ll>>;
using vb = vector<bool>;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
const ll INF = 1e15;
const ll MOD = 1e9 + 7;
const ll LINF = 1e18;
ll dp[2005][2005];
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll n;
cin >> n;
vpll v(n);
rep(i, n) {
cin >> v[i].first;
v[i].second = i;
}
vsort(v);
for (ll l = 0; l <= n; l++) {
for (ll r = n; r >= l; r--) {
ll idx = r - l;
if (l > 0) {
ll tmp = dp[l - 1][r];
tmp += abs((l - 1) - v[idx].second) * v[idx].first;
chmax(dp[l][r], tmp);
}
if (r < n) {
ll tmp = dp[l][r + 1];
tmp += abs(r - v[idx].second) * v[idx].first;
chmax(dp[l][r], tmp);
}
cerr << l << " " << r << " " << idx << " " << dp[l][r] << endl;
}
}
ll ans = 0;
for (ll i = 0; i <= n; i++) {
chmax(ans, dp[i][i]);
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
// #define int long long
#define rep(i, n) for (long long i = (long long)(0); i < (long long)(n); ++i)
#define reps(i, n) for (long long i = (long long)(1); i <= (long long)(n); ++i)
#define rrep(i, n) for (long long i = ((long long)(n)-1); i >= 0; i--)
#define rreps(i, n) for (long long i = ((long long)(n)); i > 0; i--)
#define irep(i, m, n) \
for (long long i = (long long)(m); i < (long long)(n); ++i)
#define ireps(i, m, n) \
for (long long i = (long long)(m); i <= (long long)(n); ++i)
#define SORT(v, n) sort(v, v + n);
#define REVERSE(v, n) reverse(v, v + n);
#define vsort(v) sort(v.begin(), v.end());
#define all(v) v.begin(), v.end()
#define mp(n, m) make_pair(n, m);
#define cout(d) cout << d << endl;
#define coutd(d) cout << std::setprecision(10) << d << endl;
#define cinline(n) getline(cin, n);
#define replace_all(s, b, a) replace(s.begin(), s.end(), b, a);
#define PI (acos(-1))
#define FILL(v, n, x) fill(v, v + n, x);
#define sz(x) long long(x.size())
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vs = vector<string>;
using vpll = vector<pair<ll, ll>>;
using vtp = vector<tuple<ll, ll, ll>>;
using vb = vector<bool>;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
const ll INF = 1e15;
const ll MOD = 1e9 + 7;
const ll LINF = 1e18;
ll dp[2005][2005];
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll n;
cin >> n;
vpll v(n);
rep(i, n) {
cin >> v[i].first;
v[i].second = i;
}
vsort(v);
for (ll l = 0; l <= n; l++) {
for (ll r = n; r >= l; r--) {
ll idx = r - l;
if (l > 0) {
ll tmp = dp[l - 1][r];
tmp += abs((l - 1) - v[idx].second) * v[idx].first;
chmax(dp[l][r], tmp);
}
if (r < n) {
ll tmp = dp[l][r + 1];
tmp += abs(r - v[idx].second) * v[idx].first;
chmax(dp[l][r], tmp);
}
// cerr << l << " " << r << " " << idx << " " << dp[l][r] << endl;
}
}
ll ans = 0;
for (ll i = 0; i <= n; i++) {
chmax(ans, dp[i][i]);
}
cout << ans << endl;
} | replace | 82 | 83 | 82 | 83 | TLE | |
p02709 | C++ | Runtime Error | /*
Author: Nguyen Tan Bao
Status:
Idea:
*/
#include <bits/stdc++.h>
#define FI first
#define SE second
#define EPS 1e-9
#define ALL(a) a.begin(), a.end()
#define SZ(a) int((a).size())
#define MS(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORE(i, a, b) for (int i = (a); i >= (b); i--)
#define FORALL(it, a) \
for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
//__builtin_ffs(x) return 1 + index of least significant 1-bit of x
//__builtin_clz(x) return number of leading zeros of x
//__builtin_ctz(x) return number of trailing zeros of x
using namespace std;
using ll = long long;
using ld = double;
typedef pair<int, int> II;
typedef pair<II, int> III;
typedef complex<ld> cd;
typedef vector<cd> vcd;
const ll MODBASE = 1000000007LL;
const int MAXN = 2010;
const int MAXM = 1000;
const int MAXK = 16;
const int MAXQ = 200010;
int n;
II a[MAXN];
ll dp[MAXN][MAXN];
ll solve(int l, int r) {
int remain = r - l + 1;
int start = n - remain + 1;
if (dp[l][r] != -1)
return dp[l][r];
if (l == r)
return dp[l][r] = (ll)a[start].FI * abs(r - a[start].SE);
dp[l][r] = max((ll)a[start].FI * abs(r - a[start].SE) + solve(l, r - 1),
(ll)a[start].FI * abs(l - a[start].SE) + solve(l + 1, r));
}
int main() {
ios::sync_with_stdio(0);
cin.tie(nullptr);
cin >> n;
FOR(i, 1, n) {
cin >> a[i].FI;
a[i].SE = i;
}
sort(a + 1, a + n + 1);
FOR(i, 1, n / 2) swap(a[i], a[n - i + 1]);
FOR(i, 1, n)
FOR(j, 1, n) dp[i][j] = -1;
cout << solve(1, n);
return 0;
}
| /*
Author: Nguyen Tan Bao
Status:
Idea:
*/
#include <bits/stdc++.h>
#define FI first
#define SE second
#define EPS 1e-9
#define ALL(a) a.begin(), a.end()
#define SZ(a) int((a).size())
#define MS(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORE(i, a, b) for (int i = (a); i >= (b); i--)
#define FORALL(it, a) \
for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
//__builtin_ffs(x) return 1 + index of least significant 1-bit of x
//__builtin_clz(x) return number of leading zeros of x
//__builtin_ctz(x) return number of trailing zeros of x
using namespace std;
using ll = long long;
using ld = double;
typedef pair<int, int> II;
typedef pair<II, int> III;
typedef complex<ld> cd;
typedef vector<cd> vcd;
const ll MODBASE = 1000000007LL;
const int MAXN = 2010;
const int MAXM = 1000;
const int MAXK = 16;
const int MAXQ = 200010;
int n;
II a[MAXN];
ll dp[MAXN][MAXN];
ll solve(int l, int r) {
int remain = r - l + 1;
int start = n - remain + 1;
if (dp[l][r] != -1)
return dp[l][r];
if (l == r)
return dp[l][r] = (ll)a[start].FI * abs(r - a[start].SE);
return dp[l][r] =
max((ll)a[start].FI * abs(r - a[start].SE) + solve(l, r - 1),
(ll)a[start].FI * abs(l - a[start].SE) + solve(l + 1, r));
}
int main() {
ios::sync_with_stdio(0);
cin.tie(nullptr);
cin >> n;
FOR(i, 1, n) {
cin >> a[i].FI;
a[i].SE = i;
}
sort(a + 1, a + n + 1);
FOR(i, 1, n / 2) swap(a[i], a[n - i + 1]);
FOR(i, 1, n)
FOR(j, 1, n) dp[i][j] = -1;
cout << solve(1, n);
return 0;
}
| replace | 46 | 47 | 46 | 48 | 0 | |
p02709 | C++ | Runtime Error | #include <algorithm>
#include <array>
#include <cfenv>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <forward_list>
#include <fstream>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <ostream>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <streambuf>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define MOD 1000000009
#define SIZE 101
typedef long long ll;
using namespace std;
int main() {
ll N;
vector<pair<ll, ll>> A;
cin >> N;
for (ll i = 0; i < N; i++) {
ll tmp;
cin >> tmp;
A.push_back({tmp, i});
}
sort(A.begin(), A.end());
reverse(A.begin(), A.end());
ll DP[SIZE][SIZE];
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
DP[i][j] = 0;
}
}
for (int i = 0; i < N; i++) {
ll act = A[i].first;
ll no = A[i].second;
for (int x = 0; x <= i; x++) {
int y = i - x;
if (DP[x + 1][y] < DP[x][y] + abs(no - x) * act) {
DP[x + 1][y] = DP[x][y] + abs(no - x) * act;
}
if (DP[x][y + 1] < DP[x][y] + abs(no - (N - 1 - y)) * act) {
DP[x][y + 1] = DP[x][y] + abs(no - (N - 1 - y)) * act;
}
}
}
ll ans = 0;
for (ll i = 0; i <= N; i++) {
if (ans < DP[i][N - i]) {
ans = DP[i][N - i];
}
}
cout << ans;
return 0;
}
| #include <algorithm>
#include <array>
#include <cfenv>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <forward_list>
#include <fstream>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <ostream>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <streambuf>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define MOD 1000000009
#define SIZE 2002
typedef long long ll;
using namespace std;
int main() {
ll N;
vector<pair<ll, ll>> A;
cin >> N;
for (ll i = 0; i < N; i++) {
ll tmp;
cin >> tmp;
A.push_back({tmp, i});
}
sort(A.begin(), A.end());
reverse(A.begin(), A.end());
ll DP[SIZE][SIZE];
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
DP[i][j] = 0;
}
}
for (int i = 0; i < N; i++) {
ll act = A[i].first;
ll no = A[i].second;
for (int x = 0; x <= i; x++) {
int y = i - x;
if (DP[x + 1][y] < DP[x][y] + abs(no - x) * act) {
DP[x + 1][y] = DP[x][y] + abs(no - x) * act;
}
if (DP[x][y + 1] < DP[x][y] + abs(no - (N - 1 - y)) * act) {
DP[x][y + 1] = DP[x][y] + abs(no - (N - 1 - y)) * act;
}
}
}
ll ans = 0;
for (ll i = 0; i <= N; i++) {
if (ans < DP[i][N - i]) {
ans = DP[i][N - i];
}
}
cout << ans;
return 0;
}
| replace | 32 | 33 | 32 | 33 | 0 | |
p02709 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
#define INF 1145141919810893364
// #define INF 810114514
#define PI 3.141592653589
typedef pair<int, int> PP;
typedef long long ll;
#define int ll
#define setdouble setprecision
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define OREP(i, n) for (int i = 1; i <= (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define GOODBYE \
do { \
cout << "0" << endl; \
return 0; \
} while (false)
#define MM << " " <<
#define Endl endl
#define debug false
signed main(void) {
int N;
const int MEM = 60;
int A[MEM];
cin >> N;
REP(i, N) { cin >> A[i]; }
vector<pair<int, int>> P;
vector<int> Q(N, 0);
REP(i, N) { P.push_back({A[i], i}); }
sort(P.begin(), P.end(), greater<>());
int dp[MEM][MEM] = {};
REP(i, MEM) {
REP(j, MEM) { dp[i][j] = -INF; }
}
dp[0][0] = 0;
REP(j, N) {
REP(i, j + 1) {
int k = j - i;
// 左
dp[i + 1][k] =
max(dp[i + 1][k], dp[i][k] + P[j].first * (abs(i - P[j].second)));
// 右
dp[i][k + 1] = max(
dp[i][k + 1], dp[i][k] + P[j].first * (abs(N - 1 - k - P[j].second)));
}
}
int Ans = 0;
REP(i, N + 1) { Ans = max(Ans, dp[i][N - i]); }
cout << Ans << endl;
#if debug
REP(i, N + 1) {
REP(j, N + 1) {
if (dp[i][j] < -INF / 10) {
cout << "-∞"
<< " ";
} else {
cout << dp[i][j] << " ";
}
}
cout << endl;
}
#endif
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
#define INF 1145141919810893364
// #define INF 810114514
#define PI 3.141592653589
typedef pair<int, int> PP;
typedef long long ll;
#define int ll
#define setdouble setprecision
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define OREP(i, n) for (int i = 1; i <= (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define GOODBYE \
do { \
cout << "0" << endl; \
return 0; \
} while (false)
#define MM << " " <<
#define Endl endl
#define debug false
signed main(void) {
int N;
const int MEM = 2020;
int A[MEM];
cin >> N;
REP(i, N) { cin >> A[i]; }
vector<pair<int, int>> P;
vector<int> Q(N, 0);
REP(i, N) { P.push_back({A[i], i}); }
sort(P.begin(), P.end(), greater<>());
int dp[MEM][MEM] = {};
REP(i, MEM) {
REP(j, MEM) { dp[i][j] = -INF; }
}
dp[0][0] = 0;
REP(j, N) {
REP(i, j + 1) {
int k = j - i;
// 左
dp[i + 1][k] =
max(dp[i + 1][k], dp[i][k] + P[j].first * (abs(i - P[j].second)));
// 右
dp[i][k + 1] = max(
dp[i][k + 1], dp[i][k] + P[j].first * (abs(N - 1 - k - P[j].second)));
}
}
int Ans = 0;
REP(i, N + 1) { Ans = max(Ans, dp[i][N - i]); }
cout << Ans << endl;
#if debug
REP(i, N + 1) {
REP(j, N + 1) {
if (dp[i][j] < -INF / 10) {
cout << "-∞"
<< " ";
} else {
cout << dp[i][j] << " ";
}
}
cout << endl;
}
#endif
return 0;
}
| replace | 25 | 26 | 25 | 26 | 0 | |
p02709 | Python | Time Limit Exceeded | from heapq import heappop, heappush
from collections import defaultdict
n = int(input())
a = list(map(int, input().split()))
hp = []
for i, e in enumerate(a, 1):
heappush(hp, [-e, i])
dp = defaultdict(int)
dp[(0, n + 1)] = 0
while hp:
e, i = heappop(hp)
e = -e
dp2 = defaultdict(int)
for k, v in dp.items():
l, r = k
dp2[(l + 1, r)] = max(dp2[(l + 1, r)], v + abs(l + 1 - i) * e)
dp2[(l, r - 1)] = max(dp2[(l, r - 1)], v + abs(r - 1 - i) * e)
dp = dp2
ans = 0
for k, v in dp.items():
ans = max(ans, v)
print(ans)
| from heapq import heappop, heappush
from collections import defaultdict
n = int(input())
a = list(map(int, input().split()))
hp = []
for i, e in enumerate(a, 1):
heappush(hp, [-e, i])
dp = defaultdict(int)
dp[(0, n + 1)] = 0
while hp:
e, i = heappop(hp)
e = -e
dp2 = defaultdict(int)
for k, v in dp.items():
l, r = k
dp2[(l + 1, r)] = max(dp2[(l + 1, r)], v + abs(l + 1 - i) * e)
dp2[(l, r - 1)] = max(dp2[(l, r - 1)], v + abs(r - 1 - i) * e)
dp = dp2
ans = max(dp.values())
print(ans)
| replace | 23 | 26 | 23 | 24 | TLE | |
p02709 | C++ | Runtime Error | #include <bits/stdc++.h>
// definitions
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define rep(i, l, r) for (long long i = (l); i < (long long)(r); i++)
#define repb(i, r, l) for (long long i = (r); i > (long long)(l); i--)
#define pb push_back
#define mod1 998244353
#define mod 1000000007
#define MAX_CHAR 256
using namespace std;
typedef vector<long long> vl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<pair<long long, long long>> vp;
typedef vector<tuple<long long, long long, long long>> vtup;
typedef deque<long long> dq;
typedef long long int lli;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
// standard functions
ll gcd(ll x, ll y) {
if (x < y)
return gcd(y, x);
if (y == 0)
return x;
return gcd(y, x % y);
}
ll modexp(ll x, ll ex, ll modp) {
ll ans = 1ll;
while (ex > 0) {
if (ex & 1ll)
ans = (ans * x) % modp;
ex >>= 1ll;
x = (x * x) % modp;
}
return ans;
}
ll nCrModp(ll n, ll r, ll p) {
ll C[r + 1];
memset(C, 0, sizeof(C));
C[0] = 1;
for (ll i = 1; i <= n; i++) {
for (ll j = min(i, r); j > 0; j--)
C[j] = (C[j] + C[j - 1]) % p;
}
return C[r];
}
bool isPrime(ll n) {
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (ll i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
vl countFreq(ll arr[], ll n) {
unordered_map<ll, ll> mp;
vector<ll> v;
for (ll i = 0; i < n; i++)
mp[arr[i]]++;
for (auto x : mp)
v.pb(x.second);
return v;
}
ll binarySearch(ll arr[], ll l, ll r, ll x) {
while (l <= r) {
ll m = l + (r - l) / 2;
if (arr[m] == x)
return m;
if (arr[m] < x)
l = m + 1;
else
r = m - 1;
}
return -1;
}
const ll MAXSIEVE = (1e3);
ll spf[MAXSIEVE];
void sieve() {
spf[1] = 1;
for (ll i = 2; i < MAXSIEVE; i++)
spf[i] = i;
for (ll i = 4; i < MAXSIEVE; i += 2)
spf[i] = 2;
for (ll i = 3; i * i < MAXSIEVE; i++) {
if (spf[i] == i) {
for (ll j = i * i; j < MAXSIEVE; j += i)
if (spf[j] == j)
spf[j] = i;
}
}
}
// code beigns in main
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll t = 1;
// cin >> t;
while (t--) {
ll n;
cin >> n;
vp v;
rep(i, 0, n) {
ll val;
cin >> val;
v.pb({val, i + 1});
}
sort(v.begin(), v.end(), greater<>());
ll dp[n + 1][n + 1];
clr(dp);
rep(i, 1, n + 1) {
repb(j, i, 0) {
ll cnt = j - i + n - 1;
dp[j][i] = max(dp[j + 1][i] + v[cnt].F * abs(v[cnt].S - j),
dp[j][i - 1] + v[cnt].F * abs(v[cnt].S - i));
}
}
cout << dp[1][n] << endl;
}
}
| #include <bits/stdc++.h>
// definitions
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define rep(i, l, r) for (long long i = (l); i < (long long)(r); i++)
#define repb(i, r, l) for (long long i = (r); i > (long long)(l); i--)
#define pb push_back
#define mod1 998244353
#define mod 1000000007
#define MAX_CHAR 256
using namespace std;
typedef vector<long long> vl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<pair<long long, long long>> vp;
typedef vector<tuple<long long, long long, long long>> vtup;
typedef deque<long long> dq;
typedef long long int lli;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
// standard functions
ll gcd(ll x, ll y) {
if (x < y)
return gcd(y, x);
if (y == 0)
return x;
return gcd(y, x % y);
}
ll modexp(ll x, ll ex, ll modp) {
ll ans = 1ll;
while (ex > 0) {
if (ex & 1ll)
ans = (ans * x) % modp;
ex >>= 1ll;
x = (x * x) % modp;
}
return ans;
}
ll nCrModp(ll n, ll r, ll p) {
ll C[r + 1];
memset(C, 0, sizeof(C));
C[0] = 1;
for (ll i = 1; i <= n; i++) {
for (ll j = min(i, r); j > 0; j--)
C[j] = (C[j] + C[j - 1]) % p;
}
return C[r];
}
bool isPrime(ll n) {
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (ll i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
vl countFreq(ll arr[], ll n) {
unordered_map<ll, ll> mp;
vector<ll> v;
for (ll i = 0; i < n; i++)
mp[arr[i]]++;
for (auto x : mp)
v.pb(x.second);
return v;
}
ll binarySearch(ll arr[], ll l, ll r, ll x) {
while (l <= r) {
ll m = l + (r - l) / 2;
if (arr[m] == x)
return m;
if (arr[m] < x)
l = m + 1;
else
r = m - 1;
}
return -1;
}
const ll MAXSIEVE = (1e3);
ll spf[MAXSIEVE];
void sieve() {
spf[1] = 1;
for (ll i = 2; i < MAXSIEVE; i++)
spf[i] = i;
for (ll i = 4; i < MAXSIEVE; i += 2)
spf[i] = 2;
for (ll i = 3; i * i < MAXSIEVE; i++) {
if (spf[i] == i) {
for (ll j = i * i; j < MAXSIEVE; j += i)
if (spf[j] == j)
spf[j] = i;
}
}
}
// code beigns in main
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll t = 1;
// cin >> t;
while (t--) {
ll n;
cin >> n;
vp v;
rep(i, 0, n) {
ll val;
cin >> val;
v.pb({val, i + 1});
}
sort(v.begin(), v.end(), greater<>());
ll dp[n + 2][n + 2];
clr(dp);
rep(i, 1, n + 1) {
repb(j, i, 0) {
ll cnt = j - i + n - 1;
dp[j][i] = max(dp[j + 1][i] + v[cnt].F * abs(v[cnt].S - j),
dp[j][i - 1] + v[cnt].F * abs(v[cnt].S - i));
}
}
cout << dp[1][n] << endl;
}
}
| replace | 140 | 141 | 140 | 141 | 0 | |
p02709 | C++ | Runtime Error | // https://twitter.com/armeria_betrue/status/1251868789773238273
// を見て実装
#include <algorithm>
#include <array>
#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 long long ll;
#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())
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <int MOD> struct Fp {
ll val;
constexpr Fp(ll v = 0) noexcept : val(v % MOD) {
if (val < 0)
val += MOD;
}
constexpr int getmod() { return MOD; }
constexpr Fp operator-() const noexcept { return val ? MOD - val : 0; }
constexpr Fp operator+(const Fp &r) const noexcept { return Fp(*this) += r; }
constexpr Fp operator-(const Fp &r) const noexcept { return Fp(*this) -= r; }
constexpr Fp operator*(const Fp &r) const noexcept { return Fp(*this) *= r; }
constexpr Fp operator/(const Fp &r) const noexcept { return Fp(*this) /= r; }
constexpr Fp &operator+=(const Fp &r) noexcept {
val += r.val;
if (val >= MOD)
val -= MOD;
return *this;
}
constexpr Fp &operator-=(const Fp &r) noexcept {
val -= r.val;
if (val < 0)
val += MOD;
return *this;
}
constexpr Fp &operator*=(const Fp &r) noexcept {
val = val * r.val % MOD;
return *this;
}
constexpr Fp &operator/=(const Fp &r) noexcept {
ll a = r.val, b = MOD, u = 1, v = 0;
while (b) {
ll t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
val = val * u % MOD;
if (val < 0)
val += MOD;
return *this;
}
constexpr bool operator==(const Fp &r) const noexcept {
return this->val == r.val;
}
constexpr bool operator!=(const Fp &r) const noexcept {
return this->val != r.val;
}
friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept {
return os << x.val;
}
friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, ll n) noexcept {
if (n == 0)
return 1;
auto t = modpow(a, n / 2);
t = t * t;
if (n & 1)
t = t * a;
return t;
}
};
constexpr ll MOD = 1e9 + 7;
using mint = Fp<MOD>;
#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<ll> As(N);
REP(n, N) cin >> As[n];
// <val, index>
vector<pair<ll, int>> Bs(N);
REP(n, N) { Bs[n] = mp(As[n], n); }
sort(Bs.rbegin(), Bs.rend());
// 大きい要素から順番に、右または左に寄せる
// dp[何番目まで見たか][左に寄せたのは何個か]
vector<vector<ll>> dp(N + 1, vector<ll>(N));
REP(n, N) {
auto a = Bs[n].first;
auto idx = Bs[n].second;
REP(left_num, n + 1) {
// この時点で左にleft_num, 右にright_num存在
int right_num = n - left_num;
// aを左に寄せる場合
chmax(dp[n + 1][left_num + 1], dp[n][left_num] + a * abs(idx - left_num));
// aを右に寄せる場合
auto idx2 = N - 1 - right_num;
chmax(dp[n + 1][left_num], dp[n][left_num] + a * abs(idx - idx2));
}
}
ll ans = 0;
FOR(i, 1, N + 1) chmax(ans, dp[N][i]);
cout << ans << endl;
return 0;
}
| // https://twitter.com/armeria_betrue/status/1251868789773238273
// を見て実装
#include <algorithm>
#include <array>
#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 long long ll;
#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())
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <int MOD> struct Fp {
ll val;
constexpr Fp(ll v = 0) noexcept : val(v % MOD) {
if (val < 0)
val += MOD;
}
constexpr int getmod() { return MOD; }
constexpr Fp operator-() const noexcept { return val ? MOD - val : 0; }
constexpr Fp operator+(const Fp &r) const noexcept { return Fp(*this) += r; }
constexpr Fp operator-(const Fp &r) const noexcept { return Fp(*this) -= r; }
constexpr Fp operator*(const Fp &r) const noexcept { return Fp(*this) *= r; }
constexpr Fp operator/(const Fp &r) const noexcept { return Fp(*this) /= r; }
constexpr Fp &operator+=(const Fp &r) noexcept {
val += r.val;
if (val >= MOD)
val -= MOD;
return *this;
}
constexpr Fp &operator-=(const Fp &r) noexcept {
val -= r.val;
if (val < 0)
val += MOD;
return *this;
}
constexpr Fp &operator*=(const Fp &r) noexcept {
val = val * r.val % MOD;
return *this;
}
constexpr Fp &operator/=(const Fp &r) noexcept {
ll a = r.val, b = MOD, u = 1, v = 0;
while (b) {
ll t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
val = val * u % MOD;
if (val < 0)
val += MOD;
return *this;
}
constexpr bool operator==(const Fp &r) const noexcept {
return this->val == r.val;
}
constexpr bool operator!=(const Fp &r) const noexcept {
return this->val != r.val;
}
friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept {
return os << x.val;
}
friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, ll n) noexcept {
if (n == 0)
return 1;
auto t = modpow(a, n / 2);
t = t * t;
if (n & 1)
t = t * a;
return t;
}
};
constexpr ll MOD = 1e9 + 7;
using mint = Fp<MOD>;
#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<ll> As(N);
REP(n, N) cin >> As[n];
// <val, index>
vector<pair<ll, int>> Bs(N);
REP(n, N) { Bs[n] = mp(As[n], n); }
sort(Bs.rbegin(), Bs.rend());
// 大きい要素から順番に、右または左に寄せる
// dp[何番目まで見たか][左に寄せたのは何個か]
vector<vector<ll>> dp(N + 1, vector<ll>(N + 1));
REP(n, N) {
auto a = Bs[n].first;
auto idx = Bs[n].second;
REP(left_num, n + 1) {
// この時点で左にleft_num, 右にright_num存在
int right_num = n - left_num;
// aを左に寄せる場合
chmax(dp[n + 1][left_num + 1], dp[n][left_num] + a * abs(idx - left_num));
// aを右に寄せる場合
auto idx2 = N - 1 - right_num;
chmax(dp[n + 1][left_num], dp[n][left_num] + a * abs(idx - idx2));
}
}
ll ans = 0;
FOR(i, 1, N + 1) chmax(ans, dp[N][i]);
cout << ans << endl;
return 0;
}
| replace | 124 | 125 | 124 | 125 | 0 | |
p02709 | C++ | Runtime Error |
// E - Active Infants
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// const int INF = 2147483647;
// const ll INF = 9223372036854775807;
// const ll MOD = 1e9 + 7;
pair<ll, int> A[2000]; // 活発度とindexのペア
ll dp[2001][2001]; // dp[l][r]:
// 左端にl人、右端にr人移動させたときのうれしさの合計の最大値
int main() {
int N;
cin >> N;
for (int i = 0; i < N; i++) {
ll a;
cin >> a;
A[i] = make_pair(a, i);
}
sort(A, A + N, greater<pair<ll, int>>());
ll ans = 0;
dp[0][0] = 0;
for (int l = 0; l < N; l++) {
for (int r = 0; r < N; r++) {
int i = l + r;
ll act = A[i].first;
int loc = A[i].second;
dp[l + 1][r] = max(dp[l + 1][r], dp[l][r] + act * abs(l - loc));
dp[l][r + 1] = max(dp[l][r + 1], dp[l][r] + act * abs((N - 1) - r - loc));
ans = max(ans, dp[l + 1][r]);
ans = max(ans, dp[l][r + 1]);
}
}
cout << ans << endl;
return 0;
} |
// E - Active Infants
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// const int INF = 2147483647;
// const ll INF = 9223372036854775807;
// const ll MOD = 1e9 + 7;
pair<ll, int> A[2000]; // 活発度とindexのペア
ll dp[2001][2001]; // dp[l][r]:
// 左端にl人、右端にr人移動させたときのうれしさの合計の最大値
int main() {
int N;
cin >> N;
for (int i = 0; i < N; i++) {
ll a;
cin >> a;
A[i] = make_pair(a, i);
}
sort(A, A + N, greater<pair<ll, int>>());
ll ans = 0;
dp[0][0] = 0;
for (int l = 0; l < N; l++) {
for (int r = 0; r < N; r++) {
int i = l + r;
if (i >= N)
continue;
ll act = A[i].first;
int loc = A[i].second;
dp[l + 1][r] = max(dp[l + 1][r], dp[l][r] + act * abs(l - loc));
dp[l][r + 1] = max(dp[l][r + 1], dp[l][r] + act * abs((N - 1) - r - loc));
ans = max(ans, dp[l + 1][r]);
ans = max(ans, dp[l][r + 1]);
}
}
cout << ans << endl;
return 0;
} | insert | 33 | 33 | 33 | 36 | 0 | |
p02709 | C++ | Time Limit Exceeded | // #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define fo(i, n) for (int i = 0; i < n; i++)
#define nl "\n"
#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define vec(x) vector<x>
#define matrix(x) vector<vector<x>>
#define sz(x) (int)x.size()
#define mem(a, b) memset(a, b, sizeof a)
#define vii vector<pair<int, int>>
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define uii unordered_map<int, int, custom_hash>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) (a * (b / gcd(a, b)))
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define MOD 998244353
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x) \
int x; \
cin >> x; \
while (x--)
#define all(v) v.begin(), v.end()
#define rep(i, begin, end) \
for (__typeof(end) i = (begin) - ((begin) > (end)); \
i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define len(s) s.length()
#define watch(x) cout << #x << " = " << x << endl
#define deb(...) cerr << "[" << #__VA_ARGS__ << "] : [", DBG(__VA_ARGS__)
void DBG() { cerr << "]\n"; }
template <typename T, typename... Args> void DBG(T first, Args... args) {
cerr << first;
if (sizeof...(args))
cerr << ", ";
DBG(args...);
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
pbds; // for storing unique elements
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set; // for repeating elements
int power(int a, int b, int m = mod) {
int ans = 1;
while (b > 0) {
if (b & 1)
ans = (ans * a) % m;
a = (a * a) % m;
b >>= 1;
}
return ans;
}
void babuBhaiya() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM =
chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
const int N = 1e5 + 1;
const double pi = acos(-1);
/*/-----------------------Modular Arithmetic---------------/*/
inline int add(int x, int y) {
x += y;
if (x >= mod)
return x - mod;
return x;
}
inline int sub(int x, int y) {
x -= y;
if (x < 0)
return x + mod;
return x;
}
inline int mul(int x, int y) { return (x * 1ll * y) % mod; }
/*/-----------------------------Code begins----------------------------------/*/
vii v;
int n;
int dp[2202][2202];
bool comp(pii a, pii b) { return a.ff > b.ff; }
int solve1(int l, int r) {
if (r < l)
return 0;
int &ans = dp[l][r];
if (ans != -1)
return ans;
int pos = l + n - 1 - r;
ans = max({ans, v[pos].ff * (abs(l - v[pos].ss)) + solve1(l + 1, r),
v[pos].ff * abs(r - v[pos].ss) + solve1(l, r - 1)});
}
void solve() {
cin >> n;
v.resize(n);
fo(i, n) {
cin >> v[i].ff;
v[i].ss = i;
}
sort(all(v), comp);
mem(dp, -1);
int ans = solve1(0, n - 1);
cout << ans;
}
int32_t main() {
babuBhaiya();
int t;
t = 1;
// cin>>t;
while (t--) {
solve();
}
return 0;
} | // #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define fo(i, n) for (int i = 0; i < n; i++)
#define nl "\n"
#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define vec(x) vector<x>
#define matrix(x) vector<vector<x>>
#define sz(x) (int)x.size()
#define mem(a, b) memset(a, b, sizeof a)
#define vii vector<pair<int, int>>
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define uii unordered_map<int, int, custom_hash>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) (a * (b / gcd(a, b)))
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define MOD 998244353
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x) \
int x; \
cin >> x; \
while (x--)
#define all(v) v.begin(), v.end()
#define rep(i, begin, end) \
for (__typeof(end) i = (begin) - ((begin) > (end)); \
i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define len(s) s.length()
#define watch(x) cout << #x << " = " << x << endl
#define deb(...) cerr << "[" << #__VA_ARGS__ << "] : [", DBG(__VA_ARGS__)
void DBG() { cerr << "]\n"; }
template <typename T, typename... Args> void DBG(T first, Args... args) {
cerr << first;
if (sizeof...(args))
cerr << ", ";
DBG(args...);
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
pbds; // for storing unique elements
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set; // for repeating elements
int power(int a, int b, int m = mod) {
int ans = 1;
while (b > 0) {
if (b & 1)
ans = (ans * a) % m;
a = (a * a) % m;
b >>= 1;
}
return ans;
}
void babuBhaiya() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM =
chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
const int N = 1e5 + 1;
const double pi = acos(-1);
/*/-----------------------Modular Arithmetic---------------/*/
inline int add(int x, int y) {
x += y;
if (x >= mod)
return x - mod;
return x;
}
inline int sub(int x, int y) {
x -= y;
if (x < 0)
return x + mod;
return x;
}
inline int mul(int x, int y) { return (x * 1ll * y) % mod; }
/*/-----------------------------Code begins----------------------------------/*/
vii v;
int n;
int dp[2202][2202];
bool comp(pii a, pii b) { return a.ff > b.ff; }
int solve1(int l, int r) {
if (r < l)
return 0;
int &ans = dp[l][r];
if (ans != -1)
return ans;
int pos = l + n - 1 - r;
ans = max({ans, v[pos].ff * (abs(l - v[pos].ss)) + solve1(l + 1, r),
v[pos].ff * abs(r - v[pos].ss) + solve1(l, r - 1)});
return ans;
}
void solve() {
cin >> n;
v.resize(n);
fo(i, n) {
cin >> v[i].ff;
v[i].ss = i;
}
sort(all(v), comp);
mem(dp, -1);
int ans = solve1(0, n - 1);
cout << ans;
}
int32_t main() {
babuBhaiya();
int t;
t = 1;
// cin>>t;
while (t--) {
solve();
}
return 0;
} | insert | 137 | 137 | 137 | 138 | TLE | |
p02709 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define vcout(v) \
rep(i, v.size()) cout << v[i] << " "; \
cout << endl
#define mcout(m) \
rep(j, m.size()) { vcout(m[j]); }
const ll MOD = 1e9 + 7;
int mainsolve() {
ll n;
cin >> n;
vector<ll> a(n);
rep(i, n) cin >> a[i];
vector<P> ps(n);
rep(i, n) ps[i] = P(a[i], i);
sort(ps.rbegin(), ps.rend());
ll INF = 1e18;
vector<vector<ll>> dp(n + 1, vector<ll>(n + 1, -INF));
rep(i, n + 1) dp[0][i] = 0;
rep(i, n) {
rep(j, n) {
ll chooseL = 0, chooseR = 0;
if (i >= j + 1)
chooseR = dp[i][j + 1] + ps[i].first * (n - (i - j) - ps[i].second);
if (i >= j)
chooseL = dp[i][j] + ps[i].first * (ps[i].second - j);
if (i + 1 >= j + 1)
dp[i + 1][j + 1] = max(chooseL, chooseR);
assert(dp[i + 1][j + 1] >= 0);
}
}
ll res = 0;
rep(i, n + 1) res = max(res, dp[n][i]);
cout << res << endl;
return 0;
}
int main() { return mainsolve(); } | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define vcout(v) \
rep(i, v.size()) cout << v[i] << " "; \
cout << endl
#define mcout(m) \
rep(j, m.size()) { vcout(m[j]); }
const ll MOD = 1e9 + 7;
int mainsolve() {
ll n;
cin >> n;
vector<ll> a(n);
rep(i, n) cin >> a[i];
vector<P> ps(n);
rep(i, n) ps[i] = P(a[i], i);
sort(ps.rbegin(), ps.rend());
ll INF = 1e18;
vector<vector<ll>> dp(n + 1, vector<ll>(n + 1, -INF));
rep(i, n + 1) dp[0][i] = 0;
rep(i, n) {
rep(j, n + 1) {
if (i < j)
continue;
dp[i + 1][j] =
max(dp[i + 1][j],
dp[i][j] + ps[i].first * (n - (i - j) - 1 - ps[i].second));
if (j + 1 <= n)
dp[i + 1][j + 1] =
max(dp[i + 1][j + 1], dp[i][j] + ps[i].first * (ps[i].second - j));
}
}
ll res = 0;
rep(i, n + 1) res = max(res, dp[n][i]);
cout << res << endl;
return 0;
}
int main() { return mainsolve(); } | replace | 28 | 37 | 28 | 37 | -6 | 84cd0eca-6b16-4a88-84a2-fb4ec578664c.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02709/C++/s108658118.cpp:31: int mainsolve(): Assertion `dp[i+1][j+1] >= 0' failed.
|
p02709 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <stdio.h>
#include <vector>
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
typedef double db;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int n;
cin >> n;
vector<pair<int, int>> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i].first;
a[i].second = i;
}
sort(all(a));
reverse(all(a));
vector<vector<ll>> dp(n + 1, vector<ll>(n + 1, 0));
dp[0][0] = 0;
for (int i = 0; i < n; ++i) {
for (int j = 1; j <= i + 1; ++j) {
dp[j][i + 1 - j] = max(dp[j][i + 1 - j],
dp[j - 1][i + 1 - j] +
1ll * a[i].first * abs(a[i].second + 1 - j));
dp[i + 1 - j][j] = max(dp[i + 1 - j][j],
dp[i + 1 - j][j - 1] +
1ll * a[i].first * abs(a[i].second - (n - j)));
}
}
ll ans = 0;
for (int i = 0; i <= n; ++i)
ans = max(ans, dp[i][n - i]);
cout << ans;
return 0;
} | #include <algorithm>
#include <iostream>
#include <stdio.h>
#include <vector>
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
typedef double db;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int n;
cin >> n;
vector<pair<int, int>> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i].first;
a[i].second = i;
}
sort(all(a));
reverse(all(a));
vector<vector<ll>> dp(n + 1, vector<ll>(n + 1, 0));
dp[0][0] = 0;
for (int i = 0; i < n; ++i) {
for (int j = 1; j <= i + 1; ++j) {
dp[j][i + 1 - j] = max(dp[j][i + 1 - j],
dp[j - 1][i + 1 - j] +
1ll * a[i].first * abs(a[i].second + 1 - j));
dp[i + 1 - j][j] = max(dp[i + 1 - j][j],
dp[i + 1 - j][j - 1] +
1ll * a[i].first * abs(a[i].second - (n - j)));
}
}
ll ans = 0;
for (int i = 0; i <= n; ++i)
ans = max(ans, dp[i][n - i]);
cout << ans;
return 0;
} | replace | 11 | 13 | 11 | 13 | TLE | |
p02709 | C++ | Time Limit Exceeded | #ifndef DEBUG
#define NDEBUG
#endif
#include <algorithm>
#include <array>
#include <cassert>
#include <iostream>
#include <iterator>
#include <limits>
#include <numeric>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
using namespace std;
// template {{{
// misc {{{
using Z = long long;
[[maybe_unused]] constexpr Z INF = numeric_limits<Z>::max() / 3;
// }}}
// debug {{{
template <class T>
using enable_if_sequence_t = enable_if_t<
is_same<typename iterator_traits<typename remove_cv_t<
remove_reference_t<T>>::iterator>::iterator_category,
random_access_iterator_tag>{}>;
#ifdef NDEBUG
#define dump(x)
#define debug if (0)
template <class T> inline T &&ix(T &&value) { return value; }
template <class T, class Index, class... Indices,
class = enable_if_sequence_t<T>>
inline auto &&ix(T &&container, Index index, Indices... indices) {
return ix(container[index], indices...);
}
#else
#define dump(x) \
do { \
cerr << "\e[33m[dump: " << __LINE__ << "] " << (#x) << " = " << x \
<< "\e[0m" << endl; \
} while (0);
#define debug
template <class T>
inline auto &&ix_impl([[maybe_unused]] int line,
[[maybe_unused]] const char *code, T &&value) {
return value;
}
template <class T, class Index, class... Indices,
class = enable_if_sequence_t<T>>
inline auto &&ix_impl(int line, const char *code, T &&container, Index index,
Indices... indices) {
if (index < 0) {
cerr << "\e[31mix: negative index at line " << line << ", ix(" << code
<< ")\n"
<< "index = " << index << "\e[0m" << endl;
exit(1);
}
int size = distance(begin(container), end(container));
if (index >= size) {
cerr << "\e[31mix: index out of range at line " << line << ", ix(" << code
<< ")\n"
<< "index = " << index << " whereas size = " << size << "\e[0m"
<< endl;
exit(1);
}
return ix_impl(line, code, container[index], indices...);
}
#define ix(...) ix_impl(__LINE__, #__VA_ARGS__, __VA_ARGS__)
#endif
// }}}
// IO {{{
template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> p) {
os << '(' << p.first << ", " << p.second << ')';
return os;
}
template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) {
is >> p.first >> p.second;
return is;
}
template <class T> false_type is_container_impl(...);
template <class T, class = typename T::iterator> true_type is_container_impl(T);
template <class T>
struct is_container : decltype(is_container_impl<T>(declval<T>())) {};
template <> struct is_container<string> : std::false_type {};
template <class Container, class = enable_if_t<is_container<Container>{}>>
istream &operator>>(istream &is, Container &cont) {
for (auto &v : cont)
is >> v;
return is;
}
template <class Container, class = enable_if_t<is_container<Container>{}>>
ostream &operator<<(ostream &os, Container const &cont) {
bool first = true;
constexpr char sep =
is_container<typename Container::value_type>{} ? '\n' : ' ';
for (auto const &v : cont) {
if (!first) {
os << sep;
}
first = false;
os << v;
}
return os;
}
// }}}
// }}}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.precision(10);
cout << fixed;
Z N;
cin >> N;
vector<pair<Z, Z>> As(N);
for (auto i = 0; i < N; i++) {
cin >> ix(As, i).first;
ix(As, i).second = i;
}
sort(rbegin(As), rend(As));
vector<vector<Z>> dp(N + 1, vector<Z>(N + 1));
auto max_gladness_aux = [&](auto &&self, Z left, Z right) -> Z {
auto recur = [&](auto... args) { return self(self, args...); };
dump(left);
dump(right);
Z ans = ix(dp, left, right);
if (ans > 0)
return ans;
Z next = N - (right - left);
if (next == N)
return 0;
Z orig = ix(As, next).second;
ans = recur(left + 1, right) + ix(As, next).first * abs(left - orig);
ans = max(ans, recur(left, right - 1) +
ix(As, next).first * abs(right - 1 - orig));
return ans;
};
cout << max_gladness_aux(max_gladness_aux, 0, N) << '\n';
}
| #ifndef DEBUG
#define NDEBUG
#endif
#include <algorithm>
#include <array>
#include <cassert>
#include <iostream>
#include <iterator>
#include <limits>
#include <numeric>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
using namespace std;
// template {{{
// misc {{{
using Z = long long;
[[maybe_unused]] constexpr Z INF = numeric_limits<Z>::max() / 3;
// }}}
// debug {{{
template <class T>
using enable_if_sequence_t = enable_if_t<
is_same<typename iterator_traits<typename remove_cv_t<
remove_reference_t<T>>::iterator>::iterator_category,
random_access_iterator_tag>{}>;
#ifdef NDEBUG
#define dump(x)
#define debug if (0)
template <class T> inline T &&ix(T &&value) { return value; }
template <class T, class Index, class... Indices,
class = enable_if_sequence_t<T>>
inline auto &&ix(T &&container, Index index, Indices... indices) {
return ix(container[index], indices...);
}
#else
#define dump(x) \
do { \
cerr << "\e[33m[dump: " << __LINE__ << "] " << (#x) << " = " << x \
<< "\e[0m" << endl; \
} while (0);
#define debug
template <class T>
inline auto &&ix_impl([[maybe_unused]] int line,
[[maybe_unused]] const char *code, T &&value) {
return value;
}
template <class T, class Index, class... Indices,
class = enable_if_sequence_t<T>>
inline auto &&ix_impl(int line, const char *code, T &&container, Index index,
Indices... indices) {
if (index < 0) {
cerr << "\e[31mix: negative index at line " << line << ", ix(" << code
<< ")\n"
<< "index = " << index << "\e[0m" << endl;
exit(1);
}
int size = distance(begin(container), end(container));
if (index >= size) {
cerr << "\e[31mix: index out of range at line " << line << ", ix(" << code
<< ")\n"
<< "index = " << index << " whereas size = " << size << "\e[0m"
<< endl;
exit(1);
}
return ix_impl(line, code, container[index], indices...);
}
#define ix(...) ix_impl(__LINE__, #__VA_ARGS__, __VA_ARGS__)
#endif
// }}}
// IO {{{
template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> p) {
os << '(' << p.first << ", " << p.second << ')';
return os;
}
template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) {
is >> p.first >> p.second;
return is;
}
template <class T> false_type is_container_impl(...);
template <class T, class = typename T::iterator> true_type is_container_impl(T);
template <class T>
struct is_container : decltype(is_container_impl<T>(declval<T>())) {};
template <> struct is_container<string> : std::false_type {};
template <class Container, class = enable_if_t<is_container<Container>{}>>
istream &operator>>(istream &is, Container &cont) {
for (auto &v : cont)
is >> v;
return is;
}
template <class Container, class = enable_if_t<is_container<Container>{}>>
ostream &operator<<(ostream &os, Container const &cont) {
bool first = true;
constexpr char sep =
is_container<typename Container::value_type>{} ? '\n' : ' ';
for (auto const &v : cont) {
if (!first) {
os << sep;
}
first = false;
os << v;
}
return os;
}
// }}}
// }}}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.precision(10);
cout << fixed;
Z N;
cin >> N;
vector<pair<Z, Z>> As(N);
for (auto i = 0; i < N; i++) {
cin >> ix(As, i).first;
ix(As, i).second = i;
}
sort(rbegin(As), rend(As));
vector<vector<Z>> dp(N + 1, vector<Z>(N + 1));
auto max_gladness_aux = [&](auto &&self, Z left, Z right) -> Z {
auto recur = [&](auto... args) { return self(self, args...); };
dump(left);
dump(right);
Z &ans = ix(dp, left, right);
if (ans > 0)
return ans;
Z next = N - (right - left);
if (next == N)
return 0;
Z orig = ix(As, next).second;
ans = recur(left + 1, right) + ix(As, next).first * abs(left - orig);
ans = max(ans, recur(left, right - 1) +
ix(As, next).first * abs(right - 1 - orig));
return ans;
};
cout << max_gladness_aux(max_gladness_aux, 0, N) << '\n';
}
| replace | 129 | 130 | 129 | 130 | TLE | |
p02709 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <stdio.h>
using namespace std;
const double pi = acos(-1);
typedef long long ll;
const int maxn = 2005;
struct pp {
ll v;
int id;
bool operator<(const pp &o) const {
if (v != o.v)
return v > o.v;
return id < o.id;
}
};
pp A[maxn];
int mark[maxn];
ll dp[maxn][maxn];
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
#ifdef acm
freopen("a.in", "r", stdin);
#endif // acm
int n;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> A[i].v, A[i].id = i;
sort(A + 1, A + n + 1);
for (int i = 0; i <= n; i++)
for (int j = 0; j <= n; j++)
dp[i][j] = -1;
dp[0][0] = 0;
ll sol = 0;
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
if (dp[i][j] == -1)
continue;
int t = i + j + 1;
int ini = i + 1;
int fin = n - j;
dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + abs(A[t].id - ini) * A[t].v);
dp[i][j + 1] = max(dp[i][j + 1], dp[i][j] + abs(A[t].id - fin) * A[t].v);
}
}
for (int i = 0; i <= n; i++)
sol = max(sol, dp[i][n - i]);
cout << sol << "\n";
return 0;
}
| #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <stdio.h>
using namespace std;
const double pi = acos(-1);
typedef long long ll;
const int maxn = 2005;
struct pp {
ll v;
int id;
bool operator<(const pp &o) const {
if (v != o.v)
return v > o.v;
return id < o.id;
}
};
pp A[maxn];
int mark[maxn];
ll dp[maxn][maxn];
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
#ifdef acm
freopen("a.in", "r", stdin);
#endif // acm
int n;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> A[i].v, A[i].id = i;
sort(A + 1, A + n + 1);
for (int i = 0; i <= n; i++)
for (int j = 0; j <= n; j++)
dp[i][j] = -1;
dp[0][0] = 0;
ll sol = 0;
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
if (i + j > n)
continue;
if (dp[i][j] == -1)
continue;
int t = i + j + 1;
int ini = i + 1;
int fin = n - j;
dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + abs(A[t].id - ini) * A[t].v);
dp[i][j + 1] = max(dp[i][j + 1], dp[i][j] + abs(A[t].id - fin) * A[t].v);
}
}
for (int i = 0; i <= n; i++)
sol = max(sol, dp[i][n - i]);
cout << sol << "\n";
return 0;
}
| insert | 48 | 48 | 48 | 50 | 0 | |
p02709 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define simplify \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define int long long
#define endl "\n"
using namespace std;
vector<pair<int, int>> arr;
int dp[2005][2005];
int n;
int solve(int pos, int left, int right) {
if (pos >= n)
return 0;
if (left > right)
return -1e10;
int &ans = dp[left][right];
if (ans != -1)
return ans;
int maxi1 = solve(pos + 1, left + 1, right) +
abs(arr[pos].second - left) * arr[pos].first;
int maxi2 = solve(pos + 1, left, right - 1) +
abs(arr[pos].second - right) * arr[pos].first;
return max(maxi1, maxi2);
}
int32_t main() {
simplify cin >> n;
arr.resize(n);
for (int i = 0; i < n; i++) {
cin >> arr[i].first;
arr[i].second = i;
}
sort(arr.rbegin(), arr.rend());
memset(dp, -1, sizeof dp);
int ans = solve(0, 0, n - 1);
cout << ans;
return 0;
}
| #include <bits/stdc++.h>
#define simplify \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define int long long
#define endl "\n"
using namespace std;
vector<pair<int, int>> arr;
int dp[2005][2005];
int n;
int solve(int pos, int left, int right) {
if (pos >= n)
return 0;
if (left > right)
return -1e10;
int &ans = dp[left][right];
if (ans != -1)
return ans;
int maxi1 = solve(pos + 1, left + 1, right) +
abs(arr[pos].second - left) * arr[pos].first;
int maxi2 = solve(pos + 1, left, right - 1) +
abs(arr[pos].second - right) * arr[pos].first;
return ans = max(maxi1, maxi2);
}
int32_t main() {
simplify cin >> n;
arr.resize(n);
for (int i = 0; i < n; i++) {
cin >> arr[i].first;
arr[i].second = i;
}
sort(arr.rbegin(), arr.rend());
memset(dp, -1, sizeof dp);
int ans = solve(0, 0, n - 1);
cout << ans;
return 0;
}
| replace | 26 | 27 | 26 | 27 | TLE | |
p02709 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define F first
#define S second
#define fast ios::sync_with_stdio(false), cin.tie(nullptr);
const int N = 1005;
int dp[N][N];
int n;
vector<pair<int, int>> a;
int solve(int l, int r) {
if (l > r)
return 0;
int &res = dp[l][r];
if (res != -1)
return res;
int id = n - (r - l + 1);
int pos = a[id].S;
int val = a[id].F;
res = max(res, solve(l + 1, r) + abs(pos - l) * val);
res = max(res, solve(l, r - 1) + abs(pos - r) * val);
return res;
}
int32_t main() {
fast;
cin >> n;
a.resize(n);
for (int i = 0; i < n; i++) {
cin >> a[i].F;
a[i].S = i;
}
sort(a.rbegin(), a.rend());
memset(dp, -1, sizeof(dp));
cout << solve(0, n - 1);
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define F first
#define S second
#define fast ios::sync_with_stdio(false), cin.tie(nullptr);
const int N = 2005;
int dp[N][N];
int n;
vector<pair<int, int>> a;
int solve(int l, int r) {
if (l > r)
return 0;
int &res = dp[l][r];
if (res != -1)
return res;
int id = n - (r - l + 1);
int pos = a[id].S;
int val = a[id].F;
res = max(res, solve(l + 1, r) + abs(pos - l) * val);
res = max(res, solve(l, r - 1) + abs(pos - r) * val);
return res;
}
int32_t main() {
fast;
cin >> n;
a.resize(n);
for (int i = 0; i < n; i++) {
cin >> a[i].F;
a[i].S = i;
}
sort(a.rbegin(), a.rend());
memset(dp, -1, sizeof(dp));
cout << solve(0, n - 1);
} | replace | 7 | 8 | 7 | 8 | 0 | |
p02709 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long LL;
typedef pair<LL, LL> P;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
const LL mod = 1000000007;
const LL LINF = 1LL << 62;
const int INF = 1 << 30;
int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};
int main() {
int n;
cin >> n;
vector<P> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i].fs;
a[i].sc = i;
}
sort(RALL(a));
vector<vector<LL>> dp(n + 1, vector<LL>(n + 1, -LINF));
dp[0][n - 1] = 0;
for (int i = 0; i < n; i++) {
for (int l = 0; l <= i; l++) {
int r = n - (i - l) - 1;
chmax(dp[l + 1][r], dp[l][r] + a[i].fs * abs(a[i].sc - l));
chmax(dp[l][r - 1], dp[l][r] + a[i].fs * abs(a[i].sc - r));
}
}
LL ans = 0;
for (int i = 1; i <= n; i++) {
chmax(ans, dp[i][i - 1]);
chmax(ans, dp[i - 1][i]);
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long LL;
typedef pair<LL, LL> P;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
const LL mod = 1000000007;
const LL LINF = 1LL << 62;
const int INF = 1 << 30;
int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};
int main() {
int n;
cin >> n;
vector<P> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i].fs;
a[i].sc = i;
}
sort(RALL(a));
vector<vector<LL>> dp(n + 1, vector<LL>(n + 1, -LINF));
dp[0][n - 1] = 0;
for (int i = 0; i < n; i++) {
for (int l = 0; l <= i; l++) {
int r = n - (i - l) - 1;
if (l + 1 <= n)
chmax(dp[l + 1][r], dp[l][r] + a[i].fs * abs(a[i].sc - l));
if (r - 1 >= 0)
chmax(dp[l][r - 1], dp[l][r] + a[i].fs * abs(a[i].sc - r));
}
}
LL ans = 0;
for (int i = 1; i <= n; i++) {
chmax(ans, dp[i][i - 1]);
chmax(ans, dp[i - 1][i]);
}
cout << ans << endl;
return 0;
}
| replace | 46 | 48 | 46 | 50 | 0 | |
p02709 | C++ | Time Limit Exceeded | #include "bits/stdc++.h"
using namespace std;
#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x) \
sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<( \
c i) {
sim > struct rge {
c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i;
ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
*this << "[";
for (auto it = d.b; it != d.e; ++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c &) { ris; }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define cool \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define int long long int
#define pb push_back
#define fe first
#define lb lower_bound
#define ub upper_bound
#define pii pair<pair<int, int>, pair<int, int>>
#define se second
#define endl "\n"
#define pi pair<int, int>
#define mi map<int, int>
#define mii map<pi, int>
#define vi vector<int>
#define vvi vector<vi>
#define bs binary_search
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rep1(i, a, b) for (int i = a; i <= b; i++)
#define all(c) (c).begin(), (c).end()
#define sz(x) (int)x.size()
#define PI 3.14159265358979323846
const int N = 2e3 + 5;
int mod = 1e9 + 7;
int dx[4] = {0, 0, +1, -1};
int dy[4] = {+1, -1, 0, 0};
int po(int a, int b) {
a %= mod;
int res = 1;
while (b > 0) {
if (b & 1)
res = (res * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return res;
}
int dp[N][N];
vector<pi> a;
int go(int pos, int l, int r) {
if (pos == sz(a))
return 0;
if (dp[pos][l] != -1)
return dp[pos][l];
return max(go(pos + 1, l + 1, r) + a[pos].fe * abs(a[pos].se - l),
go(pos + 1, l, r - 1) + a[pos].fe * abs(a[pos].se - r));
}
void solve() {
int n;
cin >> n;
rep(i, 0, n) {
int x;
cin >> x;
a.pb({x, i});
}
sort(a.rbegin(), a.rend());
memset(dp, -1, sizeof(dp));
cout << go(0, 0, n - 1);
cout << endl;
}
int32_t main() {
cool;
int t = 1;
// cin>>t;
while (t--)
solve();
return 0;
}
| #include "bits/stdc++.h"
using namespace std;
#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x) \
sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<( \
c i) {
sim > struct rge {
c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i;
ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
*this << "[";
for (auto it = d.b; it != d.e; ++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c &) { ris; }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define cool \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define int long long int
#define pb push_back
#define fe first
#define lb lower_bound
#define ub upper_bound
#define pii pair<pair<int, int>, pair<int, int>>
#define se second
#define endl "\n"
#define pi pair<int, int>
#define mi map<int, int>
#define mii map<pi, int>
#define vi vector<int>
#define vvi vector<vi>
#define bs binary_search
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rep1(i, a, b) for (int i = a; i <= b; i++)
#define all(c) (c).begin(), (c).end()
#define sz(x) (int)x.size()
#define PI 3.14159265358979323846
const int N = 2e3 + 5;
int mod = 1e9 + 7;
int dx[4] = {0, 0, +1, -1};
int dy[4] = {+1, -1, 0, 0};
int po(int a, int b) {
a %= mod;
int res = 1;
while (b > 0) {
if (b & 1)
res = (res * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return res;
}
int dp[N][N];
vector<pi> a;
int go(int pos, int l, int r) {
if (pos == sz(a))
return 0;
int &ans = dp[pos][l];
if (ans != -1)
return ans;
return ans = max(go(pos + 1, l + 1, r) + a[pos].fe * abs(a[pos].se - l),
go(pos + 1, l, r - 1) + a[pos].fe * abs(a[pos].se - r));
}
void solve() {
int n;
cin >> n;
rep(i, 0, n) {
int x;
cin >> x;
a.pb({x, i});
}
sort(a.rbegin(), a.rend());
memset(dp, -1, sizeof(dp));
cout << go(0, 0, n - 1);
cout << endl;
}
int32_t main() {
cool;
int t = 1;
// cin>>t;
while (t--)
solve();
return 0;
}
| replace | 84 | 88 | 84 | 89 | TLE | |
p02709 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
using namespace std;
#define INF ((1 << 30) - 1)
#define LINF (1LL << 60)
#define EPS (1e-10)
typedef long long ll;
typedef pair<ll, ll> P;
const int MOD = 1000000007;
const int MOD2 = 998244353;
vector<P> vp;
ll dp[1010][1010];
int main() {
ll n;
cin >> n;
rep(i, n) {
ll a;
cin >> a;
vp.push_back({a, i});
}
sort(vp.begin(), vp.end());
reverse(vp.begin(), vp.end());
rep(i, n + 1) {
for (int j = 0; j + i <= n; j++) {
ll cl = vp[i + j].first * (vp[i + j].second - i);
ll cr = vp[i + j].first * (n - j - 1 - vp[i + j].second);
dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + cl);
dp[i][j + 1] = max(dp[i][j + 1], dp[i][j] + cr);
}
}
ll ans = 0;
rep(i, n) ans = max(ans, dp[i][n - i]);
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
using namespace std;
#define INF ((1 << 30) - 1)
#define LINF (1LL << 60)
#define EPS (1e-10)
typedef long long ll;
typedef pair<ll, ll> P;
const int MOD = 1000000007;
const int MOD2 = 998244353;
vector<P> vp;
ll dp[2010][2010];
int main() {
ll n;
cin >> n;
rep(i, n) {
ll a;
cin >> a;
vp.push_back({a, i});
}
sort(vp.begin(), vp.end());
reverse(vp.begin(), vp.end());
rep(i, n + 1) {
for (int j = 0; j + i <= n; j++) {
ll cl = vp[i + j].first * (vp[i + j].second - i);
ll cr = vp[i + j].first * (n - j - 1 - vp[i + j].second);
dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + cl);
dp[i][j + 1] = max(dp[i][j + 1], dp[i][j] + cr);
}
}
ll ans = 0;
rep(i, n) ans = max(ans, dp[i][n - i]);
cout << ans << endl;
return 0;
}
| replace | 13 | 14 | 13 | 14 | 0 | |
p02710 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define REV(i, n) for (ll(i) = (n)-1; (i) >= 0; --i)
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n) \
{ \
REP(WW, n) cerr << v[WW] << ' '; \
cerr << endl << endl; \
}
#define SHOW2d(v, WW, HH) \
{ \
REP(W_, WW) { \
REP(H_, HH) cerr << v[W_][H_] << ' '; \
cerr << endl; \
} \
cerr << endl; \
}
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 998244353
typedef long long ll;
typedef pair<ll, ll> P;
template <typename T, typename E> struct SegmentTree {
typedef function<T(T, T)> F;
typedef function<T(T, E)> G;
typedef function<E(E, E)> H;
typedef function<E(E, int)> P;
int n;
F f;
G g;
H h;
P p;
T d1;
E d0;
vector<T> dat;
vector<E> laz;
SegmentTree(
int n_,
F f, // 要素と要素をマージする関数
G g, // 要素に作用素を作用させる関数
H h, // 作用素と作用素をマージする関数
T d1, // 要素のモノイド
E d0, // 作用素のモノイド
vector<T> v = vector<T>(), P p = [](E a, int b) { return a; })
: // 区間に対する操作が要素数に比例して変化する場合
f(f), g(g), h(h), d1(d1), d0(d0), p(p) {
init(n_);
if (n_ == (int)v.size())
build(n_, v);
}
void init(int n_) {
n = 1;
while (n < n_)
n *= 2;
dat.clear();
dat.resize(2 * n - 1, d1);
laz.clear();
laz.resize(2 * n - 1, d0);
}
void build(int n_, vector<T> v) {
for (int i = 0; i < n_; i++)
dat[i + n - 1] = v[i];
for (int i = n - 2; i >= 0; i--) {
dat[i] = f(dat[i * 2 + 1], dat[i * 2 + 2]);
}
}
inline void eval(int len, int k) {
if (laz[k] == d0)
return;
if (k * 2 + 1 < n * 2 - 1) {
laz[k * 2 + 1] = h(laz[k * 2 + 1], laz[k]);
laz[k * 2 + 2] = h(laz[k * 2 + 2], laz[k]);
}
dat[k] = g(dat[k], p(laz[k], len));
laz[k] = d0;
}
T update(int a, int b, E x, int k, int l, int r) {
eval(r - l, k);
if (r <= a || b <= l)
return dat[k];
if (a <= l && r <= b) {
laz[k] = h(laz[k], x);
return g(dat[k], p(laz[k], r - l));
}
return dat[k] = f(update(a, b, x, k * 2 + 1, l, (l + r) / 2),
update(a, b, x, k * 2 + 2, (l + r) / 2, r));
}
T update(int a, int b, E x) { return update(a, b, x, 0, 0, n); }
T query(int a, int b, int k, int l, int r) {
eval(r - l, k);
if (r <= a || b <= l)
return d1;
if (a <= l && r <= b)
return dat[k];
T vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
return f(vl, vr);
}
T query(int a, int b) { return query(a, b, 0, 0, n); }
};
/**
SegmentTree(int n_,
F f,//要素と要素をマージする関数
G g,//要素に作用素を作用させる関数
H h,//作用素と作用素をマージする関数
T d1,//要素のモノイド
E d0,//作用素のモノイド
vector<T> v=vector<T>(),//初期配列
P p=[](E a,int b){return
a;})://区間に対する操作が要素数に比例して変化する場合
区間加算、区間和
SegmentTree<ll,ll> seg(n,
[](ll a,ll b){return a + b;},
[](ll a,ll b){return a + b;},
[](ll a,ll b){return a + b;},
0,
0,
vector<ll>(n,0),
[](ll a,int b){return a * b;});
区間更新、区間最小
SegmentTree<ll,ll> seg(n,
[](ll a,ll b){return min(a, b);},
[](ll a,ll b){return b;},
[](ll a,ll b){return b;},
INF,
INF,
vector<ll>(n,INF),
[](ll a,int b){return a;});
区間加算、区間最小
SegmentTree<ll,ll> seg(n,
[](ll a,ll b){return min(a, b);},
[](ll a,ll b){return a + b;},
[](ll a,ll b){return a + b;},
INF,
0,
vector<ll>(n,0),
[](ll a,int b){return a;});
区間更新、区間和
SegmentTree<ll,ll> seg(n,
[](ll a,ll b){return a + b;},
[](ll a,ll b){return b;},
[](ll a,ll b){return b;},
0,
INF,
vector<ll>(n,0),
[](ll a,int b){return a * b;});
**/
SegmentTree<ll, ll> seg(
222222, [](ll a, ll b) { return a + b; }, [](ll a, ll b) { return a + b; },
[](ll a, ll b) { return a + b; }, 0, 0, vector<ll>(222222, 0),
[](ll a, int b) { return a * b; });
ll ans[222222];
ll c[222222];
vector<vector<ll>> v(222222);
ll n;
void test() {
REP(i, n) cout << seg.query(i, i + 1) << " ";
cout << endl;
}
void dfs(int now, int pre) {
ll color = c[now];
ll moto = seg.query(color, color + 1);
REP(i, v[now].size()) {
if (v[now][i] == pre)
continue;
seg.update(color, color + 1, -seg.query(color, color + 1));
dfs(v[now][i], now);
ll tmp = seg.query(color, color + 1);
// cout << "test " << now << " " << v[now][i] << endl;
// test();
ans[color] += tmp + tmp * (tmp - 1) / 2;
}
seg.update(0, 222222, 1);
seg.update(color, color + 1, -seg.query(color, color + 1));
if (moto == 0)
return;
seg.update(color, color + 1, moto);
}
int main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
cin >> n;
REP(i, n) cin >> c[i], c[i]--;
REP(i, n - 1) {
ll a, b;
cin >> a >> b;
a--;
b--;
v[a].PB(b);
v[b].PB(a);
}
dfs(0, -1);
// cout << "test " << -1 << " " << 0 << endl;
// test();
REP(i, n) {
ll tmp = seg.query(i, i + 1);
ans[i] += tmp + tmp * (tmp - 1) / 2;
cout << n + n * (n - 1) / 2 - ans[i] << endl;
}
return 0;
}
| #include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define REV(i, n) for (ll(i) = (n)-1; (i) >= 0; --i)
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n) \
{ \
REP(WW, n) cerr << v[WW] << ' '; \
cerr << endl << endl; \
}
#define SHOW2d(v, WW, HH) \
{ \
REP(W_, WW) { \
REP(H_, HH) cerr << v[W_][H_] << ' '; \
cerr << endl; \
} \
cerr << endl; \
}
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 998244353
typedef long long ll;
typedef pair<ll, ll> P;
template <typename T, typename E> struct SegmentTree {
typedef function<T(T, T)> F;
typedef function<T(T, E)> G;
typedef function<E(E, E)> H;
typedef function<E(E, int)> P;
int n;
F f;
G g;
H h;
P p;
T d1;
E d0;
vector<T> dat;
vector<E> laz;
SegmentTree(
int n_,
F f, // 要素と要素をマージする関数
G g, // 要素に作用素を作用させる関数
H h, // 作用素と作用素をマージする関数
T d1, // 要素のモノイド
E d0, // 作用素のモノイド
vector<T> v = vector<T>(), P p = [](E a, int b) { return a; })
: // 区間に対する操作が要素数に比例して変化する場合
f(f), g(g), h(h), d1(d1), d0(d0), p(p) {
init(n_);
if (n_ == (int)v.size())
build(n_, v);
}
void init(int n_) {
n = 1;
while (n < n_)
n *= 2;
dat.clear();
dat.resize(2 * n - 1, d1);
laz.clear();
laz.resize(2 * n - 1, d0);
}
void build(int n_, vector<T> v) {
for (int i = 0; i < n_; i++)
dat[i + n - 1] = v[i];
for (int i = n - 2; i >= 0; i--) {
dat[i] = f(dat[i * 2 + 1], dat[i * 2 + 2]);
}
}
inline void eval(int len, int k) {
if (laz[k] == d0)
return;
if (k * 2 + 1 < n * 2 - 1) {
laz[k * 2 + 1] = h(laz[k * 2 + 1], laz[k]);
laz[k * 2 + 2] = h(laz[k * 2 + 2], laz[k]);
}
dat[k] = g(dat[k], p(laz[k], len));
laz[k] = d0;
}
T update(int a, int b, E x, int k, int l, int r) {
eval(r - l, k);
if (r <= a || b <= l)
return dat[k];
if (a <= l && r <= b) {
laz[k] = h(laz[k], x);
return g(dat[k], p(laz[k], r - l));
}
return dat[k] = f(update(a, b, x, k * 2 + 1, l, (l + r) / 2),
update(a, b, x, k * 2 + 2, (l + r) / 2, r));
}
T update(int a, int b, E x) { return update(a, b, x, 0, 0, n); }
T query(int a, int b, int k, int l, int r) {
eval(r - l, k);
if (r <= a || b <= l)
return d1;
if (a <= l && r <= b)
return dat[k];
T vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
return f(vl, vr);
}
T query(int a, int b) { return query(a, b, 0, 0, n); }
};
/**
SegmentTree(int n_,
F f,//要素と要素をマージする関数
G g,//要素に作用素を作用させる関数
H h,//作用素と作用素をマージする関数
T d1,//要素のモノイド
E d0,//作用素のモノイド
vector<T> v=vector<T>(),//初期配列
P p=[](E a,int b){return
a;})://区間に対する操作が要素数に比例して変化する場合
区間加算、区間和
SegmentTree<ll,ll> seg(n,
[](ll a,ll b){return a + b;},
[](ll a,ll b){return a + b;},
[](ll a,ll b){return a + b;},
0,
0,
vector<ll>(n,0),
[](ll a,int b){return a * b;});
区間更新、区間最小
SegmentTree<ll,ll> seg(n,
[](ll a,ll b){return min(a, b);},
[](ll a,ll b){return b;},
[](ll a,ll b){return b;},
INF,
INF,
vector<ll>(n,INF),
[](ll a,int b){return a;});
区間加算、区間最小
SegmentTree<ll,ll> seg(n,
[](ll a,ll b){return min(a, b);},
[](ll a,ll b){return a + b;},
[](ll a,ll b){return a + b;},
INF,
0,
vector<ll>(n,0),
[](ll a,int b){return a;});
区間更新、区間和
SegmentTree<ll,ll> seg(n,
[](ll a,ll b){return a + b;},
[](ll a,ll b){return b;},
[](ll a,ll b){return b;},
0,
INF,
vector<ll>(n,0),
[](ll a,int b){return a * b;});
**/
SegmentTree<ll, ll> seg(
222222, [](ll a, ll b) { return a + b; }, [](ll a, ll b) { return a + b; },
[](ll a, ll b) { return a + b; }, 0, 0, vector<ll>(222222, 0),
[](ll a, int b) { return a * b; });
ll ans[222222];
ll c[222222];
vector<vector<ll>> v(222222);
ll n;
void dfs(int now, int pre) {
ll color = c[now];
ll moto = seg.query(color, color + 1);
REP(i, v[now].size()) {
if (v[now][i] == pre)
continue;
seg.update(color, color + 1, -seg.query(color, color + 1));
dfs(v[now][i], now);
ll tmp = seg.query(color, color + 1);
// cout << "test " << now << " " << v[now][i] << endl;
// test();
ans[color] += tmp + tmp * (tmp - 1) / 2;
}
seg.update(0, 222222, 1);
seg.update(color, color + 1, -seg.query(color, color + 1));
if (moto == 0)
return;
seg.update(color, color + 1, moto);
}
int main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
cin >> n;
REP(i, n) cin >> c[i], c[i]--;
REP(i, n - 1) {
ll a, b;
cin >> a >> b;
a--;
b--;
v[a].PB(b);
v[b].PB(a);
}
dfs(0, -1);
// cout << "test " << -1 << " " << 0 << endl;
// test();
REP(i, n) {
ll tmp = seg.query(i, i + 1);
ans[i] += tmp + tmp * (tmp - 1) / 2;
cout << n + n * (n - 1) / 2 - ans[i] << endl;
}
return 0;
}
| delete | 193 | 198 | 193 | 193 | 0 | |
p02710 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
template <typename T> void out(T x) {
cout << x << endl;
exit(0);
}
#define watch(x) cout << (#x) << " is " << (x) << endl
using ll = long long;
const int maxn = 1e6 + 5;
int n;
int c[maxn];
vector<int> g[maxn];
int siz[maxn];
ll ans[maxn];
void dfs(int at, int p, map<ll, ll> &mp) {
siz[at] = 1;
int C = c[at];
for (int to : g[at]) {
if (to == p)
continue;
map<ll, ll> mp_to;
dfs(to, at, mp_to);
siz[at] += siz[to];
ll middle = siz[to] - mp_to[C];
ans[C] -= (middle * (middle + 1) / 2);
for (auto pr : mp_to) {
mp[pr.first] += pr.second;
}
}
mp[C] = siz[at];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> c[i];
}
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 1; i <= n; i++) {
ans[i] = 1ll * n * (n + 1) / 2;
}
map<ll, ll> mp;
dfs(1, 0, mp);
for (int i = 1; i <= n; i++) {
ll top = n - mp[i];
ans[i] -= (top * (top + 1) / 2);
}
for (int i = 1; i <= n; i++) {
cout << ans[i] << "\n";
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
template <typename T> void out(T x) {
cout << x << endl;
exit(0);
}
#define watch(x) cout << (#x) << " is " << (x) << endl
using ll = long long;
const int maxn = 1e6 + 5;
int n;
int c[maxn];
vector<int> g[maxn];
int siz[maxn];
ll ans[maxn];
void dfs(int at, int p, map<ll, ll> &mp) {
siz[at] = 1;
int C = c[at];
for (int to : g[at]) {
if (to == p)
continue;
map<ll, ll> mp_to;
dfs(to, at, mp_to);
siz[at] += siz[to];
ll middle = siz[to] - mp_to[C];
ans[C] -= (middle * (middle + 1) / 2);
if (mp.size() < mp_to.size()) {
swap(mp, mp_to);
}
for (auto pr : mp_to) {
mp[pr.first] += pr.second;
}
}
mp[C] = siz[at];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> c[i];
}
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 1; i <= n; i++) {
ans[i] = 1ll * n * (n + 1) / 2;
}
map<ll, ll> mp;
dfs(1, 0, mp);
for (int i = 1; i <= n; i++) {
ll top = n - mp[i];
ans[i] -= (top * (top + 1) / 2);
}
for (int i = 1; i <= n; i++) {
cout << ans[i] << "\n";
}
return 0;
}
| insert | 31 | 31 | 31 | 34 | TLE | |
p02710 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define REV(i, n) for (ll(i) = (n)-1; (i) >= 0; --i)
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n) \
{ \
REP(WW, n) cerr << v[WW] << ' '; \
cerr << endl << endl; \
}
#define SHOW2d(v, WW, HH) \
{ \
REP(W_, WW) { \
REP(H_, HH) cerr << v[W_][H_] << ' '; \
cerr << endl; \
} \
cerr << endl; \
}
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 998244353
typedef long long ll;
typedef pair<ll, ll> P;
template <typename T, typename E> struct SegmentTree {
typedef function<T(T, T)> F;
typedef function<T(T, E)> G;
typedef function<E(E, E)> H;
typedef function<E(E, int)> P;
int n;
F f;
G g;
H h;
P p;
T d1;
E d0;
vector<T> dat;
vector<E> laz;
SegmentTree(
int n_,
F f, // 要素と要素をマージする関数
G g, // 要素に作用素を作用させる関数
H h, // 作用素と作用素をマージする関数
T d1, // 要素のモノイド
E d0, // 作用素のモノイド
vector<T> v = vector<T>(), P p = [](E a, int b) { return a; })
: // 区間に対する操作が要素数に比例して変化する場合
f(f), g(g), h(h), d1(d1), d0(d0), p(p) {
init(n_);
if (n_ == (int)v.size())
build(n_, v);
}
void init(int n_) {
n = 1;
while (n < n_)
n *= 2;
dat.clear();
dat.resize(2 * n - 1, d1);
laz.clear();
laz.resize(2 * n - 1, d0);
}
void build(int n_, vector<T> v) {
for (int i = 0; i < n_; i++)
dat[i + n - 1] = v[i];
for (int i = n - 2; i >= 0; i--) {
dat[i] = f(dat[i * 2 + 1], dat[i * 2 + 2]);
}
}
inline void eval(int len, int k) {
if (laz[k] == d0)
return;
if (k * 2 + 1 < n * 2 - 1) {
laz[k * 2 + 1] = h(laz[k * 2 + 1], laz[k]);
laz[k * 2 + 2] = h(laz[k * 2 + 2], laz[k]);
}
dat[k] = g(dat[k], p(laz[k], len));
laz[k] = d0;
}
T update(int a, int b, E x, int k, int l, int r) {
eval(r - l, k);
if (r <= a || b <= l)
return dat[k];
if (a <= l && r <= b) {
laz[k] = h(laz[k], x);
return g(dat[k], p(laz[k], r - l));
}
return dat[k] = f(update(a, b, x, k * 2 + 1, l, (l + r) / 2),
update(a, b, x, k * 2 + 2, (l + r) / 2, r));
}
T update(int a, int b, E x) { return update(a, b, x, 0, 0, n); }
T query(int a, int b, int k, int l, int r) {
eval(r - l, k);
if (r <= a || b <= l)
return d1;
if (a <= l && r <= b)
return dat[k];
T vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
return f(vl, vr);
}
T query(int a, int b) { return query(a, b, 0, 0, n); }
};
/**
SegmentTree(int n_,
F f,//要素と要素をマージする関数
G g,//要素に作用素を作用させる関数
H h,//作用素と作用素をマージする関数
T d1,//要素のモノイド
E d0,//作用素のモノイド
vector<T> v=vector<T>(),//初期配列
P p=[](E a,int b){return
a;})://区間に対する操作が要素数に比例して変化する場合
区間加算、区間和
SegmentTree<ll,ll> seg(n,
[](ll a,ll b){return a + b;},
[](ll a,ll b){return a + b;},
[](ll a,ll b){return a + b;},
0,
0,
vector<ll>(n,0),
[](ll a,int b){return a * b;});
区間更新、区間最小
SegmentTree<ll,ll> seg(n,
[](ll a,ll b){return min(a, b);},
[](ll a,ll b){return b;},
[](ll a,ll b){return b;},
INF,
INF,
vector<ll>(n,INF),
[](ll a,int b){return a;});
区間加算、区間最小
SegmentTree<ll,ll> seg(n,
[](ll a,ll b){return min(a, b);},
[](ll a,ll b){return a + b;},
[](ll a,ll b){return a + b;},
INF,
0,
vector<ll>(n,0),
[](ll a,int b){return a;});
区間更新、区間和
SegmentTree<ll,ll> seg(n,
[](ll a,ll b){return a + b;},
[](ll a,ll b){return b;},
[](ll a,ll b){return b;},
0,
INF,
vector<ll>(n,0),
[](ll a,int b){return a * b;});
**/
SegmentTree<ll, ll> seg(
222222, [](ll a, ll b) { return a + b; }, [](ll a, ll b) { return a + b; },
[](ll a, ll b) { return a + b; }, 0, 0, vector<ll>(222222, 0),
[](ll a, int b) { return a * b; });
ll ans[222222];
ll c[222222];
vector<vector<ll>> v(222222);
ll n;
void dfs(int now, int pre) {
ll color = c[now];
ll moto = seg.query(color, color + 1);
REP(i, v[now].size()) {
if (v[now][i] == pre)
continue;
seg.update(color, color + 1, -seg.query(color, color + 1));
dfs(v[now][i], now);
ll tmp = seg.query(color, color + 1);
// cout << "test " << now << " " << v[now][i] << endl;
// test();
ans[color] += tmp + tmp * (tmp - 1) / 2;
}
seg.update(0, 222222, 1);
seg.update(color, color + 1, -seg.query(color, color + 1));
if (moto == 0)
return;
seg.update(color, color + 1, moto);
}
int main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
cin >> n;
REP(i, n) cin >> c[i], c[i]--;
REP(i, n - 1) {
ll a, b;
cin >> a >> b;
a--;
b--;
v[a].PB(b);
v[b].PB(a);
}
dfs(0, -1);
// cout << "test " << -1 << " " << 0 << endl;
// test();
REP(i, n) {
ll tmp = seg.query(i, i + 1);
ans[i] += tmp + tmp * (tmp - 1) / 2;
cout << n + n * (n - 1) / 2 - ans[i] << endl;
}
return 0;
}
| #include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define REV(i, n) for (ll(i) = (n)-1; (i) >= 0; --i)
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n) \
{ \
REP(WW, n) cerr << v[WW] << ' '; \
cerr << endl << endl; \
}
#define SHOW2d(v, WW, HH) \
{ \
REP(W_, WW) { \
REP(H_, HH) cerr << v[W_][H_] << ' '; \
cerr << endl; \
} \
cerr << endl; \
}
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 998244353
typedef long long ll;
typedef pair<ll, ll> P;
template <typename T, typename E> struct SegmentTree {
typedef function<T(T, T)> F;
typedef function<T(T, E)> G;
typedef function<E(E, E)> H;
typedef function<E(E, int)> P;
int n;
F f;
G g;
H h;
P p;
T d1;
E d0;
vector<T> dat;
vector<E> laz;
SegmentTree(
int n_,
F f, // 要素と要素をマージする関数
G g, // 要素に作用素を作用させる関数
H h, // 作用素と作用素をマージする関数
T d1, // 要素のモノイド
E d0, // 作用素のモノイド
vector<T> v = vector<T>(), P p = [](E a, int b) { return a; })
: // 区間に対する操作が要素数に比例して変化する場合
f(f), g(g), h(h), d1(d1), d0(d0), p(p) {
init(n_);
if (n_ == (int)v.size())
build(n_, v);
}
void init(int n_) {
n = 1;
while (n < n_)
n *= 2;
dat.clear();
dat.resize(2 * n - 1, d1);
laz.clear();
laz.resize(2 * n - 1, d0);
}
void build(int n_, vector<T> v) {
for (int i = 0; i < n_; i++)
dat[i + n - 1] = v[i];
for (int i = n - 2; i >= 0; i--) {
dat[i] = f(dat[i * 2 + 1], dat[i * 2 + 2]);
}
}
inline void eval(int len, int k) {
if (laz[k] == d0)
return;
if (k * 2 + 1 < n * 2 - 1) {
laz[k * 2 + 1] = h(laz[k * 2 + 1], laz[k]);
laz[k * 2 + 2] = h(laz[k * 2 + 2], laz[k]);
}
dat[k] = g(dat[k], p(laz[k], len));
laz[k] = d0;
}
T update(int a, int b, E x, int k, int l, int r) {
eval(r - l, k);
if (r <= a || b <= l)
return dat[k];
if (a <= l && r <= b) {
laz[k] = h(laz[k], x);
return g(dat[k], p(laz[k], r - l));
}
return dat[k] = f(update(a, b, x, k * 2 + 1, l, (l + r) / 2),
update(a, b, x, k * 2 + 2, (l + r) / 2, r));
}
T update(int a, int b, E x) { return update(a, b, x, 0, 0, n); }
T query(int a, int b, int k, int l, int r) {
eval(r - l, k);
if (r <= a || b <= l)
return d1;
if (a <= l && r <= b)
return dat[k];
T vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
return f(vl, vr);
}
T query(int a, int b) { return query(a, b, 0, 0, n); }
};
/**
SegmentTree(int n_,
F f,//要素と要素をマージする関数
G g,//要素に作用素を作用させる関数
H h,//作用素と作用素をマージする関数
T d1,//要素のモノイド
E d0,//作用素のモノイド
vector<T> v=vector<T>(),//初期配列
P p=[](E a,int b){return
a;})://区間に対する操作が要素数に比例して変化する場合
区間加算、区間和
SegmentTree<ll,ll> seg(n,
[](ll a,ll b){return a + b;},
[](ll a,ll b){return a + b;},
[](ll a,ll b){return a + b;},
0,
0,
vector<ll>(n,0),
[](ll a,int b){return a * b;});
区間更新、区間最小
SegmentTree<ll,ll> seg(n,
[](ll a,ll b){return min(a, b);},
[](ll a,ll b){return b;},
[](ll a,ll b){return b;},
INF,
INF,
vector<ll>(n,INF),
[](ll a,int b){return a;});
区間加算、区間最小
SegmentTree<ll,ll> seg(n,
[](ll a,ll b){return min(a, b);},
[](ll a,ll b){return a + b;},
[](ll a,ll b){return a + b;},
INF,
0,
vector<ll>(n,0),
[](ll a,int b){return a;});
区間更新、区間和
SegmentTree<ll,ll> seg(n,
[](ll a,ll b){return a + b;},
[](ll a,ll b){return b;},
[](ll a,ll b){return b;},
0,
INF,
vector<ll>(n,0),
[](ll a,int b){return a * b;});
**/
SegmentTree<ll, ll> seg(
222222, [](ll a, ll b) { return a + b; }, [](ll a, ll b) { return a + b; },
[](ll a, ll b) { return a + b; }, 0, 0, vector<ll>(222222, 0),
[](ll a, int b) { return a * b; });
ll ans[222222];
ll c[222222];
vector<vector<ll>> v(222222);
ll n;
void dfs(int now, int pre) {
ll color = c[now];
ll moto = seg.query(color, color + 1);
REP(i, v[now].size()) {
if (v[now][i] == pre)
continue;
seg.update(color, color + 1, -seg.query(color, color + 1));
dfs(v[now][i], now);
ll tmp = seg.query(color, color + 1);
// cout << "test " << now << " " << v[now][i] << endl;
// test();
ans[color] += tmp + tmp * (tmp - 1) / 2;
}
seg.update(0, 222222, 1);
seg.update(color, color + 1, -seg.query(color, color + 1));
if (moto == 0)
return;
seg.update(color, color + 1, moto);
}
int main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
cin >> n;
REP(i, n) cin >> c[i], c[i]--;
REP(i, n - 1) {
ll a, b;
cin >> a >> b;
a--;
b--;
v[a].PB(b);
v[b].PB(a);
}
dfs(n / 2, -1);
// cout << "test " << -1 << " " << 0 << endl;
// test();
REP(i, n) {
ll tmp = seg.query(i, i + 1);
ans[i] += tmp + tmp * (tmp - 1) / 2;
cout << n + n * (n - 1) / 2 - ans[i] << endl;
}
return 0;
}
| replace | 229 | 230 | 229 | 230 | 0 | |
p02710 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <iostream>
using namespace std;
const long long MOD = 1e9 + 7;
const long long MAXN = 5e3 + 5;
long long n;
vector<long long> v1[MAXN];
long long ans[MAXN];
long long res[MAXN];
long long col[MAXN];
long long sz[MAXN];
long long f(long long x) { return 1LL * x * (x + 1) / 2; }
void dfs(long long curr, long long par) {
long long tmp = res[col[curr]];
for (long long x : v1[curr]) {
if (x != par) {
res[col[curr]] = 0;
dfs(x, curr);
sz[curr] += sz[x];
ans[col[curr]] -= f(sz[x] - res[col[curr]]);
}
}
res[col[curr]] = tmp + sz[curr];
}
int main() {
cin >> n;
for (long long i = 1; i <= n; i++) {
cin >> col[i];
sz[i] = 1;
}
for (long long i = 1; i < n; i++) {
long long x, y;
cin >> x >> y;
v1[x].push_back(y);
v1[y].push_back(x);
}
dfs(1, 1);
for (long long i = 1; i <= n; i++) {
// cout<<col[i]<<endl;
cout << (ans[i] + f(n) - f(n - res[i])) << endl;
}
} | #include <bits/stdc++.h>
#include <iostream>
using namespace std;
const long long MOD = 1e9 + 7;
const long long MAXN = 2e5 + 5;
long long n;
vector<long long> v1[MAXN];
long long ans[MAXN];
long long res[MAXN];
long long col[MAXN];
long long sz[MAXN];
long long f(long long x) { return 1LL * x * (x + 1) / 2; }
void dfs(long long curr, long long par) {
long long tmp = res[col[curr]];
for (long long x : v1[curr]) {
if (x != par) {
res[col[curr]] = 0;
dfs(x, curr);
sz[curr] += sz[x];
ans[col[curr]] -= f(sz[x] - res[col[curr]]);
}
}
res[col[curr]] = tmp + sz[curr];
}
int main() {
cin >> n;
for (long long i = 1; i <= n; i++) {
cin >> col[i];
sz[i] = 1;
}
for (long long i = 1; i < n; i++) {
long long x, y;
cin >> x >> y;
v1[x].push_back(y);
v1[y].push_back(x);
}
dfs(1, 1);
for (long long i = 1; i <= n; i++) {
// cout<<col[i]<<endl;
cout << (ans[i] + f(n) - f(n - res[i])) << endl;
}
} | replace | 4 | 5 | 4 | 5 | 0 | |
p02710 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define For(i, a, b) for (int(i) = (int)(a); (i) < (int)(b); ++(i))
#define rFor(i, a, b) for (int(i) = (int)(a)-1; (i) >= (int)(b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<lint, lint> pll;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> T div_floor(T a, T b) {
if (b < 0)
a *= -1, b *= -1;
return a >= 0 ? a / b : (a + 1) / b - 1;
}
template <class T> T div_ceil(T a, T b) {
if (b < 0)
a *= -1, b *= -1;
return a > 0 ? (a - 1) / b + 1 : a / b;
}
constexpr lint mod = 1000000007;
constexpr lint INF = mod * mod;
constexpr int MAX = 200010;
int n, sz[MAX], c[MAX];
vector<int> G[MAX];
map<int, int> mp[MAX];
lint ans[MAX];
int sz_dfs(int v, int pv) {
sz[v] = 1;
for (auto nv : G[v])
if (nv != pv) {
sz[v] += sz_dfs(nv, v);
}
return sz[v];
}
void dfs(int v, int pv) {
int max_id = -1;
rep(i, G[v].size()) if (G[v][i] != pv) {
int nv = G[v][i];
dfs(nv, v);
lint tmp = sz[nv] + mp[nv][c[v]];
ans[c[v]] -= (lint)tmp * (tmp - 1) / 2 + tmp;
if (max_id < 0 || mp[nv].size() > mp[max_id].size()) {
max_id = i;
}
}
if (max_id >= 0) {
swap(mp[v], mp[G[v][max_id]]);
rep(i, G[v].size()) if (i != max_id && G[v][i] != pv) {
int nv = G[v][i];
for (auto p : mp[nv])
mp[v][p.fi] += p.se;
mp[nv].clear();
}
}
mp[v][c[v]] = -sz[v];
}
int main() {
scanf("%d", &n);
rep(i, n) scanf("%d", &c[i]), --c[i];
rep(i, n - 1) {
int a, b;
scanf("%d%d", &a, &b);
--a;
--b;
G[a].push_back(b);
G[b].push_back(a);
}
sz_dfs(0, -1);
fill(ans, ans + n, (lint)n * (n - 1) / 2 + n);
dfs(0, -1);
rep(i, n) {
lint tmp = n + mp[0][i];
ans[i] -= (lint)tmp * (tmp - 1) / 2 + tmp;
printf("%lld\n", ans[i]);
}
} | #include <bits/stdc++.h>
#define For(i, a, b) for (int(i) = (int)(a); (i) < (int)(b); ++(i))
#define rFor(i, a, b) for (int(i) = (int)(a)-1; (i) >= (int)(b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<lint, lint> pll;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> T div_floor(T a, T b) {
if (b < 0)
a *= -1, b *= -1;
return a >= 0 ? a / b : (a + 1) / b - 1;
}
template <class T> T div_ceil(T a, T b) {
if (b < 0)
a *= -1, b *= -1;
return a > 0 ? (a - 1) / b + 1 : a / b;
}
constexpr lint mod = 1000000007;
constexpr lint INF = mod * mod;
constexpr int MAX = 200010;
int n, sz[MAX], c[MAX];
vector<int> G[MAX];
map<int, int> mp[MAX];
lint ans[MAX];
int sz_dfs(int v, int pv) {
sz[v] = 1;
for (auto nv : G[v])
if (nv != pv) {
sz[v] += sz_dfs(nv, v);
}
return sz[v];
}
void dfs(int v, int pv) {
int max_id = -1;
rep(i, G[v].size()) if (G[v][i] != pv) {
int nv = G[v][i];
dfs(nv, v);
lint tmp = sz[nv] + mp[nv][c[v]];
ans[c[v]] -= (lint)tmp * (tmp - 1) / 2 + tmp;
if (max_id < 0 || mp[nv].size() > mp[G[v][max_id]].size()) {
max_id = i;
}
}
if (max_id >= 0) {
swap(mp[v], mp[G[v][max_id]]);
rep(i, G[v].size()) if (i != max_id && G[v][i] != pv) {
int nv = G[v][i];
for (auto p : mp[nv])
mp[v][p.fi] += p.se;
mp[nv].clear();
}
}
mp[v][c[v]] = -sz[v];
}
int main() {
scanf("%d", &n);
rep(i, n) scanf("%d", &c[i]), --c[i];
rep(i, n - 1) {
int a, b;
scanf("%d%d", &a, &b);
--a;
--b;
G[a].push_back(b);
G[b].push_back(a);
}
sz_dfs(0, -1);
fill(ans, ans + n, (lint)n * (n - 1) / 2 + n);
dfs(0, -1);
rep(i, n) {
lint tmp = n + mp[0][i];
ans[i] -= (lint)tmp * (tmp - 1) / 2 + tmp;
printf("%lld\n", ans[i]);
}
} | replace | 64 | 65 | 64 | 65 | TLE | |
p02710 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAX = 510000;
const int MOD = 1000000007;
int sz[200001], zen[200001], c[200001], ans[200001];
bool used[200001];
vector<int> hen[200000];
int dfssz(int a) {
sz[a] = 1;
for (auto &&i : hen[a])
if (!sz[i])
sz[a] += dfssz(i);
return sz[a];
}
void dfs(int a) {
used[a] = true;
ans[c[a]] += 1 + zen[c[a]] * (zen[c[a]] - 1) / 2;
zen[c[a]] -= sz[a];
ans[c[a]] -= (zen[c[a]] - 1) * zen[c[a]] / 2;
int tmp = zen[c[a]];
for (auto &&i : hen[a])
if (!used[i]) {
ans[c[a]] -= sz[i] * (sz[i] - 1) / 2;
zen[c[a]] = sz[i];
dfs(i);
}
zen[c[a]] = tmp;
return;
}
signed main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> c[i];
}
int a, b;
for (int i = 0; i < n - 1; i++) {
cin >> a >> b;
hen[a].push_back(b);
hen[b].push_back(a);
}
fill(ans, ans + n + 1, 0);
fill(sz, sz + n + 1, 0);
fill(zen, zen + n + 1, n);
dfssz(1);
fill(used, used + n + 1, false);
dfs(1);
for (int i = 1; i <= n; i++)
cout << ans[i] << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAX = 510000;
const int MOD = 1000000007;
int sz[200001], zen[200001], c[200001], ans[200001];
bool used[200001];
vector<int> hen[200001];
int dfssz(int a) {
sz[a] = 1;
for (auto &&i : hen[a])
if (!sz[i])
sz[a] += dfssz(i);
return sz[a];
}
void dfs(int a) {
used[a] = true;
ans[c[a]] += 1 + zen[c[a]] * (zen[c[a]] - 1) / 2;
zen[c[a]] -= sz[a];
ans[c[a]] -= (zen[c[a]] - 1) * zen[c[a]] / 2;
int tmp = zen[c[a]];
for (auto &&i : hen[a])
if (!used[i]) {
ans[c[a]] -= sz[i] * (sz[i] - 1) / 2;
zen[c[a]] = sz[i];
dfs(i);
}
zen[c[a]] = tmp;
return;
}
signed main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> c[i];
}
int a, b;
for (int i = 0; i < n - 1; i++) {
cin >> a >> b;
hen[a].push_back(b);
hen[b].push_back(a);
}
fill(ans, ans + n + 1, 0);
fill(sz, sz + n + 1, 0);
fill(zen, zen + n + 1, n);
dfssz(1);
fill(used, used + n + 1, false);
dfs(1);
for (int i = 1; i <= n; i++)
cout << ans[i] << endl;
}
| replace | 7 | 8 | 7 | 8 | 0 | |
p02710 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
int n;
int c[200005], dead[200005], treesize[200005], major[200005], used[200005];
vi G[200005];
ll sum[200005];
vector<tuple<int, int, ll>> colorsubtrees;
// this DFS finds the centroid.
void dfs1(int n, int p = -1) {
treesize[n] = 1;
major[n] = 0;
for (int h : G[n])
if (h != p && !dead[h]) {
dfs1(h, n);
treesize[n] += treesize[h];
if (!major[n] || treesize[h] > treesize[major[n]])
major[n] = h;
}
}
// this DFS calculates the score.
void dfs2(int n, int p, int treeroot, int centroid) {
bool uniq = !used[c[n]];
// printf("DFS2 node = %d (color = %d, color used = %d), treeroot = %d, parent
// = %d\n", n, c[n], used[c[n]], treeroot, p);
if (uniq) {
used[c[n]] = true;
// printf("Centroid %d, From %d, Subtree %d: %lld paths with color %d\n",
// centroid, n, treeroot, 1ll * (treesize[centroid] - treesize[treeroot]) *
// treesize[n], c[n]);
sum[c[n]] += 1ll * (treesize[centroid] - treesize[treeroot]) * treesize[n];
// colorsubtrees[c[n]][treeroot] += treesize[n];
colorsubtrees.push_back({c[n], treeroot, treesize[n]});
}
for (int h : G[n])
if (h != p && !dead[h]) {
dfs2(h, n, treeroot, centroid);
}
if (uniq) {
used[c[n]] = false;
}
}
void centroid(int n) {
dfs1(n);
int rootsize = treesize[n];
while (major[n] && treesize[major[n]] * 2 > rootsize) {
n = major[n];
}
// centroid is at n
// cout << n << " is a centroid" << endl;
dfs1(n);
used[c[n]] = true;
sum[c[n]] += 1; // self -> self
sum[c[n]] += 1ll * treesize[n] * (treesize[n] - 1) / 2;
ll paths = 0;
paths += 1;
paths += 1ll * treesize[n] * (treesize[n] - 1) / 2;
// printf("Centroid %d (Color: %d) has paths: %lld + %lld\n", n, c[n], 1ll *
// treesize[n] * (treesize[n] - 1) / 2, 1ll);
for (int h : G[n])
if (!dead[h]) {
// printf(" .. subtract %lld from subtree %d\n", 1ll * treesize[h] *
// (treesize[h] - 1) / 2, h);
sum[c[n]] -= 1ll * treesize[h] * (treesize[h] - 1) / 2;
paths -= 1ll * treesize[h] * (treesize[h] - 1) / 2;
}
// printf(" .. Total: %lld\n", paths);
// process each subtree
for (int h : G[n])
if (!dead[h]) {
dfs2(h, n, h, n);
}
// remove double counts from colorsubtrees
/*for (auto subtree : colorsubtrees) {
int color = subtree.first;
unordered_map<int, ll> sizes = subtree.second;
ll total = 0;
ll doubled = 0;
for (auto p : sizes) total += p.second;
for (auto p : sizes) {
doubled += p.second * (total - p.second);
}
if (doubled) {
//printf("!! Centroid %d, color %d: doubled %lld\n", n, c[n],
doubled);
}
sum[color] -= doubled / 2;
}*/
{
sort(all(colorsubtrees));
vector<int> groups;
groups.push_back(0);
for (int i = 1; i < colorsubtrees.size(); i++) {
if (get<0>(colorsubtrees[i]) != get<0>(colorsubtrees[i - 1]))
groups.push_back(i);
}
groups.push_back(colorsubtrees.size());
for (int g = 0; g < groups.size() - 1; g++) {
int color = get<0>(colorsubtrees[groups[g]]);
ll total = 0;
for (int i = groups[g]; i < groups[g + 1]; i++) {
total += get<2>(colorsubtrees[i]);
}
ll doubled = 0, ctotal = 0;
for (int i = groups[g]; i < groups[g + 1]; i++) {
if (i && get<1>(colorsubtrees[i]) != get<1>(colorsubtrees[i - 1])) {
doubled += ctotal * (total - ctotal);
ctotal = 0;
}
ctotal += get<2>(colorsubtrees[i]);
}
doubled += ctotal * (total - ctotal);
sum[color] -= doubled / 2;
}
}
colorsubtrees.clear();
used[c[n]] = false;
// recur
dead[n] = true;
for (int h : G[n])
if (!dead[h])
centroid(h);
}
int main() {
cin >> n;
rep(i, 1, n + 1) cin >> c[i];
rep(i, 1, n) {
int a, b;
cin >> a >> b;
G[a].push_back(b), G[b].push_back(a);
}
centroid(1);
rep(i, 1, n + 1) { cout << sum[i] << endl; }
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
int n;
int c[200005], dead[200005], treesize[200005], major[200005], used[200005];
vi G[200005];
ll sum[200005];
vector<tuple<int, int, ll>> colorsubtrees;
// this DFS finds the centroid.
void dfs1(int n, int p = -1) {
treesize[n] = 1;
major[n] = 0;
for (int h : G[n])
if (h != p && !dead[h]) {
dfs1(h, n);
treesize[n] += treesize[h];
if (!major[n] || treesize[h] > treesize[major[n]])
major[n] = h;
}
}
// this DFS calculates the score.
void dfs2(int n, int p, int treeroot, int centroid) {
bool uniq = !used[c[n]];
// printf("DFS2 node = %d (color = %d, color used = %d), treeroot = %d, parent
// = %d\n", n, c[n], used[c[n]], treeroot, p);
if (uniq) {
used[c[n]] = true;
// printf("Centroid %d, From %d, Subtree %d: %lld paths with color %d\n",
// centroid, n, treeroot, 1ll * (treesize[centroid] - treesize[treeroot]) *
// treesize[n], c[n]);
sum[c[n]] += 1ll * (treesize[centroid] - treesize[treeroot]) * treesize[n];
// colorsubtrees[c[n]][treeroot] += treesize[n];
colorsubtrees.push_back({c[n], treeroot, treesize[n]});
}
for (int h : G[n])
if (h != p && !dead[h]) {
dfs2(h, n, treeroot, centroid);
}
if (uniq) {
used[c[n]] = false;
}
}
void centroid(int n) {
dfs1(n);
int rootsize = treesize[n];
while (major[n] && treesize[major[n]] * 2 > rootsize) {
n = major[n];
}
// centroid is at n
// cout << n << " is a centroid" << endl;
dfs1(n);
used[c[n]] = true;
sum[c[n]] += 1; // self -> self
sum[c[n]] += 1ll * treesize[n] * (treesize[n] - 1) / 2;
ll paths = 0;
paths += 1;
paths += 1ll * treesize[n] * (treesize[n] - 1) / 2;
// printf("Centroid %d (Color: %d) has paths: %lld + %lld\n", n, c[n], 1ll *
// treesize[n] * (treesize[n] - 1) / 2, 1ll);
for (int h : G[n])
if (!dead[h]) {
// printf(" .. subtract %lld from subtree %d\n", 1ll * treesize[h] *
// (treesize[h] - 1) / 2, h);
sum[c[n]] -= 1ll * treesize[h] * (treesize[h] - 1) / 2;
paths -= 1ll * treesize[h] * (treesize[h] - 1) / 2;
}
// printf(" .. Total: %lld\n", paths);
// process each subtree
for (int h : G[n])
if (!dead[h]) {
dfs2(h, n, h, n);
}
// remove double counts from colorsubtrees
/*for (auto subtree : colorsubtrees) {
int color = subtree.first;
unordered_map<int, ll> sizes = subtree.second;
ll total = 0;
ll doubled = 0;
for (auto p : sizes) total += p.second;
for (auto p : sizes) {
doubled += p.second * (total - p.second);
}
if (doubled) {
//printf("!! Centroid %d, color %d: doubled %lld\n", n, c[n],
doubled);
}
sum[color] -= doubled / 2;
}*/
if (colorsubtrees.size() >= 2) {
sort(all(colorsubtrees));
vector<int> groups;
groups.push_back(0);
for (int i = 1; i < colorsubtrees.size(); i++) {
if (get<0>(colorsubtrees[i]) != get<0>(colorsubtrees[i - 1]))
groups.push_back(i);
}
groups.push_back(colorsubtrees.size());
for (int g = 0; g < groups.size() - 1; g++) {
int color = get<0>(colorsubtrees[groups[g]]);
ll total = 0;
for (int i = groups[g]; i < groups[g + 1]; i++) {
total += get<2>(colorsubtrees[i]);
}
ll doubled = 0, ctotal = 0;
for (int i = groups[g]; i < groups[g + 1]; i++) {
if (i && get<1>(colorsubtrees[i]) != get<1>(colorsubtrees[i - 1])) {
doubled += ctotal * (total - ctotal);
ctotal = 0;
}
ctotal += get<2>(colorsubtrees[i]);
}
doubled += ctotal * (total - ctotal);
sum[color] -= doubled / 2;
}
}
colorsubtrees.clear();
used[c[n]] = false;
// recur
dead[n] = true;
for (int h : G[n])
if (!dead[h])
centroid(h);
}
int main() {
cin >> n;
rep(i, 1, n + 1) cin >> c[i];
rep(i, 1, n) {
int a, b;
cin >> a >> b;
G[a].push_back(b), G[b].push_back(a);
}
centroid(1);
rep(i, 1, n + 1) { cout << sum[i] << endl; }
}
| replace | 107 | 108 | 107 | 108 | 0 | |
p02710 | C++ | Runtime Error | #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> P;
const int L = 2e5;
LL c[L];
vector<int> es[L];
LL ps[L];
LL w[L];
LL cnt[L];
LL ret[L];
LL N;
LL dfs(int r, int p) {
int pcnt = cnt[c[r]];
int pps = ps[c[r]];
ps[c[r]] = r;
LL s = 1;
ret[c[r]] += s;
LL sum = 1;
for (auto i : es[r]) {
if (i == p) {
continue;
}
w[r] = 0;
auto v = dfs(i, r);
auto t = w[r] + v;
ret[c[r]] += t * s;
s += t;
sum += v;
}
{
auto t = N - sum - pcnt;
ret[c[r]] += t * s;
}
cnt[c[r]] += s;
w[pps] -= sum;
ps[c[r]] = pps;
return sum;
}
int main() {
scanf("%lld", &N);
for (int i = 1; i <= N; i++) {
scanf("%lld", &c[i]);
}
for (int i = 0; i < N - 1; i++) {
LL a, b;
scanf("%lld", &a);
scanf("%lld", &b);
es[a].push_back(b);
es[b].push_back(a);
}
dfs(1, 0);
for (int i = 1; i <= N; i++) {
cout << ret[i] << endl;
}
return 0;
}
| #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> P;
const int L = 2e5 + 9;
LL c[L];
vector<int> es[L];
LL ps[L];
LL w[L];
LL cnt[L];
LL ret[L];
LL N;
LL dfs(int r, int p) {
int pcnt = cnt[c[r]];
int pps = ps[c[r]];
ps[c[r]] = r;
LL s = 1;
ret[c[r]] += s;
LL sum = 1;
for (auto i : es[r]) {
if (i == p) {
continue;
}
w[r] = 0;
auto v = dfs(i, r);
auto t = w[r] + v;
ret[c[r]] += t * s;
s += t;
sum += v;
}
{
auto t = N - sum - pcnt;
ret[c[r]] += t * s;
}
cnt[c[r]] += s;
w[pps] -= sum;
ps[c[r]] = pps;
return sum;
}
int main() {
scanf("%lld", &N);
for (int i = 1; i <= N; i++) {
scanf("%lld", &c[i]);
}
for (int i = 0; i < N - 1; i++) {
LL a, b;
scanf("%lld", &a);
scanf("%lld", &b);
es[a].push_back(b);
es[b].push_back(a);
}
dfs(1, 0);
for (int i = 1; i <= N; i++) {
cout << ret[i] << endl;
}
return 0;
}
| replace | 29 | 30 | 29 | 30 | 0 | |
p02710 | C++ | Runtime Error | // <head>
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 1002003004;
const ll LINF = 1002003004005006007ll;
struct preprocess {
preprocess() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
}
} ____;
// </head>
// <library>
template <typename T> struct BIT {
int n;
vector<T> d;
BIT(int n = 0) : n(n), d(n + 1) {}
void add(int i, T x = 1) {
for (i++; i <= n; i += i & -i) {
d[i] += x;
}
}
T sum(int i) {
T x = 0;
for (i++; i; i -= i & -i) {
x += d[i];
}
return x;
}
T sum(int l, int r) { return sum(r - 1) - sum(l - 1); }
};
// </library>
const int MX = 10;
int in[MX], out[MX], k;
vector<int> to[MX];
void dfs(int v, int p = -1) {
in[v] = k++;
for (int u : to[v]) {
if (u == p)
continue;
dfs(u, v);
}
out[v] = k;
}
vector<ll> vs[MX];
ll f(ll n) { return n * (n + 1) / 2; }
int main() {
int n;
cin >> n;
vector<ll> c(n);
for (int i = 0; i < n; i++) {
cin >> c[i];
vs[c[i] - 1].emplace_back(i);
}
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
to[a].emplace_back(b);
to[b].emplace_back(a);
}
dfs(0);
ll tot = f(n);
BIT<int> d(n);
for (int i = 0; i < n; i++) {
d.add(i, 1);
}
for (int i = 0; i < n; i++) {
ll ans = tot;
vector<ll> nvs = vs[i];
sort(nvs.begin(), nvs.end(), [&](int a, int b) { return in[a] > in[b]; });
vector<pair<ll, ll>> history;
for (auto j : nvs) {
ll cnt = 1;
for (auto k : to[j]) {
if (in[j] > in[k])
continue;
ll num = d.sum(in[k], out[k]);
ans -= f(num);
cnt += num;
}
d.add(in[j], -cnt);
history.emplace_back(make_pair(in[j], cnt));
}
ans -= f(d.sum(0, n));
cout << ans << '\n';
for (auto p : history) {
d.add(p.first, p.second);
}
}
} | // <head>
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 1002003004;
const ll LINF = 1002003004005006007ll;
struct preprocess {
preprocess() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
}
} ____;
// </head>
// <library>
template <typename T> struct BIT {
int n;
vector<T> d;
BIT(int n = 0) : n(n), d(n + 1) {}
void add(int i, T x = 1) {
for (i++; i <= n; i += i & -i) {
d[i] += x;
}
}
T sum(int i) {
T x = 0;
for (i++; i; i -= i & -i) {
x += d[i];
}
return x;
}
T sum(int l, int r) { return sum(r - 1) - sum(l - 1); }
};
// </library>
const int MX = 200005;
int in[MX], out[MX], k;
vector<int> to[MX];
void dfs(int v, int p = -1) {
in[v] = k++;
for (int u : to[v]) {
if (u == p)
continue;
dfs(u, v);
}
out[v] = k;
}
vector<ll> vs[MX];
ll f(ll n) { return n * (n + 1) / 2; }
int main() {
int n;
cin >> n;
vector<ll> c(n);
for (int i = 0; i < n; i++) {
cin >> c[i];
vs[c[i] - 1].emplace_back(i);
}
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
to[a].emplace_back(b);
to[b].emplace_back(a);
}
dfs(0);
ll tot = f(n);
BIT<int> d(n);
for (int i = 0; i < n; i++) {
d.add(i, 1);
}
for (int i = 0; i < n; i++) {
ll ans = tot;
vector<ll> nvs = vs[i];
sort(nvs.begin(), nvs.end(), [&](int a, int b) { return in[a] > in[b]; });
vector<pair<ll, ll>> history;
for (auto j : nvs) {
ll cnt = 1;
for (auto k : to[j]) {
if (in[j] > in[k])
continue;
ll num = d.sum(in[k], out[k]);
ans -= f(num);
cnt += num;
}
d.add(in[j], -cnt);
history.emplace_back(make_pair(in[j], cnt));
}
ans -= f(d.sum(0, n));
cout << ans << '\n';
for (auto p : history) {
d.add(p.first, p.second);
}
}
} | replace | 35 | 36 | 35 | 36 | 0 | |
p02710 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n - 1); i >= 0; --i)
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
typedef long long ll;
typedef pair<int, int> P;
const int MAX = 200005;
const int INF = 1001001001;
const int MOD = 1000000007;
struct BIT {
int N, id;
vector<int> bit;
vector<pair<int, int>> his;
BIT(int N, int id = 0) : N(N), id(id), bit(N + 1), his() {
for (int i = 1; i <= N; ++i)
add(i, id, false);
}
void add(int i, int v, bool flg = true) {
if (flg)
his.emplace_back(i, v);
for (int j = i; j <= N; j += j & -j)
bit[j] += v;
}
int sum(int i) {
int res = 0;
for (int j = i; j > 0; j -= j & -j)
res += bit[j];
return res;
}
int sum(int l, int r) { return sum(r - 1) - sum(l - 1); }
void clear() {
for (auto &p : his)
add(p.first, -p.second);
his.clear();
}
};
vector<int> G[MAX];
int dfs(int v, vector<int> &in, vector<int> &out, int p = -1, int k = 1) {
in[v] = k++;
for (auto &u : G[v]) {
if (u == p)
continue;
k = dfs(u, in, out, v, k);
}
return out[v] = k;
}
ll calc(ll x) { return x * (x + 1) / 2; }
int main() {
int N;
cin >> N;
vector<int> C(N);
for (auto &i : C)
cin >> i, --i;
rep(i, N - 1) {
int a, b;
cin >> a >> b;
a--, b--;
G[a].push_back(b);
G[b].push_back(a);
}
vector<int> in(N), out(N);
dfs(0, in, out);
BIT bit = BIT(N, 1);
vector<vector<int>> V(N);
rep(i, N) V[C[i]].push_back(i);
rep(i, N) {
sort(V[i].begin(), V[i].end(), [&](int a, int b) { return in[a] > in[b]; });
}
ll total = calc(N);
rep(i, N) {
ll ans = total;
vector<int> &NV = V[i];
for (auto &v : NV) {
int cnt = 1;
for (auto &u : G[v]) {
if (in[v] > in[u])
continue;
int num = bit.sum(in[u], out[u]);
ans -= calc(num);
cnt += num;
}
bit.add(in[v], -cnt);
}
int num = bit.sum(1, N + 1);
ans -= calc(num);
cout << ans << endl;
bit.clear();
}
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n - 1); i >= 0; --i)
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
typedef long long ll;
typedef pair<int, int> P;
const int MAX = 200005;
const int INF = 1001001001;
const int MOD = 1000000007;
struct BIT {
int N, id;
vector<int> bit;
vector<pair<int, int>> his;
BIT(int N, int id = 0) : N(N), id(id), bit(N + 1), his() {
for (int i = 1; i <= N; ++i)
add(i, id, false);
}
void add(int i, int v, bool flg = true) {
if (flg)
his.emplace_back(i, v);
for (int j = i; j <= N; j += j & -j)
bit[j] += v;
}
int sum(int i) {
int res = 0;
for (int j = i; j > 0; j -= j & -j)
res += bit[j];
return res;
}
int sum(int l, int r) { return sum(r - 1) - sum(l - 1); }
void clear() {
for (auto &p : his)
add(p.first, -p.second, false);
his.clear();
}
};
vector<int> G[MAX];
int dfs(int v, vector<int> &in, vector<int> &out, int p = -1, int k = 1) {
in[v] = k++;
for (auto &u : G[v]) {
if (u == p)
continue;
k = dfs(u, in, out, v, k);
}
return out[v] = k;
}
ll calc(ll x) { return x * (x + 1) / 2; }
int main() {
int N;
cin >> N;
vector<int> C(N);
for (auto &i : C)
cin >> i, --i;
rep(i, N - 1) {
int a, b;
cin >> a >> b;
a--, b--;
G[a].push_back(b);
G[b].push_back(a);
}
vector<int> in(N), out(N);
dfs(0, in, out);
BIT bit = BIT(N, 1);
vector<vector<int>> V(N);
rep(i, N) V[C[i]].push_back(i);
rep(i, N) {
sort(V[i].begin(), V[i].end(), [&](int a, int b) { return in[a] > in[b]; });
}
ll total = calc(N);
rep(i, N) {
ll ans = total;
vector<int> &NV = V[i];
for (auto &v : NV) {
int cnt = 1;
for (auto &u : G[v]) {
if (in[v] > in[u])
continue;
int num = bit.sum(in[u], out[u]);
ans -= calc(num);
cnt += num;
}
bit.add(in[v], -cnt);
}
int num = bit.sum(1, N + 1);
ans -= calc(num);
cout << ans << endl;
bit.clear();
}
} | replace | 51 | 52 | 51 | 52 | 0 | |
p02710 | C++ | Runtime Error | #define _CRT_SECURE_NO_WARNINGS
#define REL
#pragma GCC target("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef int64_t i64;
typedef long long LL;
typedef long long ll;
typedef long double LD;
typedef complex<LD> cld;
typedef unsigned int uint;
typedef complex<double> cd;
typedef unsigned long long ULL;
#define endl "\n"
#define dendl "\n\n"
#define mp make_pair
#define mt make_tuple
#define size(v) (int)v.size()
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
/////////////////////////////////////////////////////////////////////
template <typename T> T sqr(T a) { return a * a; }
template <typename T> int sign(T a) {
if (a == 0)
return 0;
return (a > 0 ? 1 : -1);
}
template <typename T> bool uax(T &a, const T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T> bool uin(T &a, const T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &out, pair<T1, T2> &p) {
out << p.first << ' ' << p.second;
return out;
}
mt19937_64 rnd(0);
const int LOG = 64;
const int N = (int)507;
const int MAXN = (int)3e3 + 7;
const int MOD = (int)998244353;
const int INF = (int)1e9 + 7;
const int CONST = 450;
const LL LINF = (i64)1e17;
const LD PI = 3.1415926535897932384626433832795;
const LD EPS = 1;
/////////////////////////////////////////////////////////////////////
LL ans[N];
bool have[N];
int n, col[N];
vector<int> gr[N];
vector<pair<int, int>> ver[N];
int sz[N], tim, tin[N], tout[N];
bool is_par(int u, int v) { return tin[u] <= tin[v] && tin[v] <= tout[u]; }
void calc_time(int u, int par) {
tin[u] = tim++;
for (int v : gr[u]) {
if (v == par)
continue;
calc_time(v, u);
}
tout[u] = tim++;
}
void dfs(int u, int par) {
sz[u] = 1;
for (int v : gr[u]) {
if (v == par)
continue;
dfs(v, u);
sz[u] += sz[v];
}
int pos = col[u];
reverse(all(gr[u]));
for (int v : gr[u]) {
if (v == par)
continue;
int comp = sz[v];
while (size(ver[pos])) {
int w, cnt;
tie(w, cnt) = ver[pos].back();
if (!is_par(v, w))
break;
comp -= cnt;
ver[pos].pop_back();
}
ans[pos - 1] -= 1LL * comp * (comp + 1) / 2;
}
ver[pos].push_back(mp(u, sz[u]));
if (u == 0) {
for (int i = 1; i <= n; i++) {
if (i == col[u])
continue;
int comp = n;
for (auto it : ver[i])
comp -= it.second;
ans[i - 1] -= 1LL * comp * (comp + 1) / 2;
}
}
}
void solve() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> col[i];
have[col[i] - 1] = true;
ans[i] = 1LL * n * (n + 1) / 2;
}
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
u--, v--;
gr[u].push_back(v);
gr[v].push_back(u);
}
calc_time(0, 0);
dfs(0, 0);
for (int i = 0; i < n; i++) {
cout << (have[i] ? ans[i] : 0) << endl;
}
}
int main() {
double start = clock();
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
cout << fixed << setprecision(20);
cerr << fixed << setprecision(20);
#else
cout << fixed << setprecision(20);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
rnd.seed(time(0));
#endif
int t = 1;
for (int i = 0; i < t; i++) {
solve();
}
#ifdef _DEBUG
cout << endl << "Time is " << (clock() - start) / 1000.0;
#endif
} | #define _CRT_SECURE_NO_WARNINGS
#define REL
#pragma GCC target("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef int64_t i64;
typedef long long LL;
typedef long long ll;
typedef long double LD;
typedef complex<LD> cld;
typedef unsigned int uint;
typedef complex<double> cd;
typedef unsigned long long ULL;
#define endl "\n"
#define dendl "\n\n"
#define mp make_pair
#define mt make_tuple
#define size(v) (int)v.size()
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
/////////////////////////////////////////////////////////////////////
template <typename T> T sqr(T a) { return a * a; }
template <typename T> int sign(T a) {
if (a == 0)
return 0;
return (a > 0 ? 1 : -1);
}
template <typename T> bool uax(T &a, const T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T> bool uin(T &a, const T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &out, pair<T1, T2> &p) {
out << p.first << ' ' << p.second;
return out;
}
mt19937_64 rnd(0);
const int LOG = 64;
const int N = (int)2e5 + 7;
const int MAXN = (int)3e3 + 7;
const int MOD = (int)998244353;
const int INF = (int)1e9 + 7;
const int CONST = 450;
const LL LINF = (i64)1e17;
const LD PI = 3.1415926535897932384626433832795;
const LD EPS = 1;
/////////////////////////////////////////////////////////////////////
LL ans[N];
bool have[N];
int n, col[N];
vector<int> gr[N];
vector<pair<int, int>> ver[N];
int sz[N], tim, tin[N], tout[N];
bool is_par(int u, int v) { return tin[u] <= tin[v] && tin[v] <= tout[u]; }
void calc_time(int u, int par) {
tin[u] = tim++;
for (int v : gr[u]) {
if (v == par)
continue;
calc_time(v, u);
}
tout[u] = tim++;
}
void dfs(int u, int par) {
sz[u] = 1;
for (int v : gr[u]) {
if (v == par)
continue;
dfs(v, u);
sz[u] += sz[v];
}
int pos = col[u];
reverse(all(gr[u]));
for (int v : gr[u]) {
if (v == par)
continue;
int comp = sz[v];
while (size(ver[pos])) {
int w, cnt;
tie(w, cnt) = ver[pos].back();
if (!is_par(v, w))
break;
comp -= cnt;
ver[pos].pop_back();
}
ans[pos - 1] -= 1LL * comp * (comp + 1) / 2;
}
ver[pos].push_back(mp(u, sz[u]));
if (u == 0) {
for (int i = 1; i <= n; i++) {
if (i == col[u])
continue;
int comp = n;
for (auto it : ver[i])
comp -= it.second;
ans[i - 1] -= 1LL * comp * (comp + 1) / 2;
}
}
}
void solve() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> col[i];
have[col[i] - 1] = true;
ans[i] = 1LL * n * (n + 1) / 2;
}
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
u--, v--;
gr[u].push_back(v);
gr[v].push_back(u);
}
calc_time(0, 0);
dfs(0, 0);
for (int i = 0; i < n; i++) {
cout << (have[i] ? ans[i] : 0) << endl;
}
}
int main() {
double start = clock();
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
cout << fixed << setprecision(20);
cerr << fixed << setprecision(20);
#else
cout << fixed << setprecision(20);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
rnd.seed(time(0));
#endif
int t = 1;
for (int i = 0; i < t; i++) {
solve();
}
#ifdef _DEBUG
cout << endl << "Time is " << (clock() - start) / 1000.0;
#endif
} | replace | 88 | 89 | 88 | 89 | 0 | |
p02710 | C++ | Time Limit Exceeded | #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
#define ln '\n'
constexpr long long MOD = 1000000007LL;
// constexpr long long MOD = 998244353LL;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
template <class T, class U> inline bool chmax(T &a, U b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T, class U> inline bool chmin(T &a, U b) {
if (a > b) {
a = b;
return true;
}
return false;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
vector<int> G[201010];
int c[201010];
ll ans[201010];
ll sub[201010];
gp_hash_table<int, vector<int>> dic[201010];
void dfs(int v, int pv) {
for (auto nv : G[v]) {
if (nv == pv)
continue;
dfs(nv, v);
sub[v] += sub[nv];
if (dic[nv].find(c[v]) == dic[nv].end())
ans[c[v]] += (sub[nv]) * (sub[nv] + 1) / 2;
else {
ll val = 0;
for (auto j : dic[nv][c[v]]) {
val += sub[j];
}
ans[c[v]] += (sub[nv] - val) * (sub[nv] - val + 1) / 2;
}
if (dic[v].size() < dic[nv].size()) {
swap(dic[v], dic[nv]);
}
for (auto A : dic[nv]) {
for (auto j : A.second) {
dic[v][A.first].emplace_back(j);
}
}
}
dic[v][c[v]].clear();
dic[v][c[v]].emplace_back(v);
sub[v]++;
return;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll N;
cin >> N;
set<int> zzz;
rep(i, N) {
cin >> c[i];
--c[i];
zzz.emplace(c[i]);
}
rep(i, N - 1) {
int u, v;
cin >> u >> v;
--u;
--v;
G[u].emplace_back(v);
G[v].emplace_back(u);
}
dfs(0, -1);
// rep(i,N) cout << ans[i] << ln;
for (auto i : dic[0]) {
ll val = 0;
for (auto j : i.second) {
val += sub[j];
}
ans[i.first] += (sub[0] - val) * (sub[0] - val + 1) / 2;
}
rep(i, N) {
if (!zzz.count(i))
cout << 0 << ln;
else
cout << (N + 1) * N / 2 - ans[i] << ln;
}
}
| #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
#define ln '\n'
constexpr long long MOD = 1000000007LL;
// constexpr long long MOD = 998244353LL;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
template <class T, class U> inline bool chmax(T &a, U b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T, class U> inline bool chmin(T &a, U b) {
if (a > b) {
a = b;
return true;
}
return false;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
vector<int> G[201010];
int c[201010];
ll ans[201010];
ll sub[201010];
unordered_map<int, vector<int>> dic[201010];
void dfs(int v, int pv) {
for (auto nv : G[v]) {
if (nv == pv)
continue;
dfs(nv, v);
sub[v] += sub[nv];
if (dic[nv].find(c[v]) == dic[nv].end())
ans[c[v]] += (sub[nv]) * (sub[nv] + 1) / 2;
else {
ll val = 0;
for (auto j : dic[nv][c[v]]) {
val += sub[j];
}
ans[c[v]] += (sub[nv] - val) * (sub[nv] - val + 1) / 2;
}
if (dic[v].size() < dic[nv].size()) {
swap(dic[v], dic[nv]);
}
for (auto A : dic[nv]) {
for (auto j : A.second) {
dic[v][A.first].emplace_back(j);
}
}
}
dic[v][c[v]].clear();
dic[v][c[v]].emplace_back(v);
sub[v]++;
return;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll N;
cin >> N;
set<int> zzz;
rep(i, N) {
cin >> c[i];
--c[i];
zzz.emplace(c[i]);
}
rep(i, N - 1) {
int u, v;
cin >> u >> v;
--u;
--v;
G[u].emplace_back(v);
G[v].emplace_back(u);
}
dfs(0, -1);
// rep(i,N) cout << ans[i] << ln;
for (auto i : dic[0]) {
ll val = 0;
for (auto j : i.second) {
val += sub[j];
}
ans[i.first] += (sub[0] - val) * (sub[0] - val + 1) / 2;
}
rep(i, N) {
if (!zzz.count(i))
cout << 0 << ln;
else
cout << (N + 1) * N / 2 - ans[i] << ln;
}
}
| replace | 35 | 36 | 35 | 36 | TLE |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.