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 |
|---|---|---|---|---|---|---|---|---|---|---|---|
p02698 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(obj) (obj).begin(), (obj).end()
typedef pair<int, int> P;
const int mod = 1e9 + 7;
vector<int> dp, ans, a, g[200005];
void dfs(int v, int p = -1) {
int i = lower_bound(all(dp), a[v]) - dp.begin();
int old = dp[i];
dp[i] = a[v];
ans[v] = i;
if (p != -1)
ans[v] = max(ans[v], ans[p]);
for (auto u : g[v]) {
if (u == p)
continue;
dfs(u, v);
}
dp[i] = old;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
a = ans = vector<int>(n);
rep(i, n) cin >> a[i];
rep(i, n) {
int u, v;
cin >> u >> v;
--u;
--v;
g[u].push_back(v);
g[v].push_back(u);
}
dp = vector<int>(n + 1, mod);
dp[0] = -1;
dfs(0);
rep(i, n) cout << ans[i] << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(obj) (obj).begin(), (obj).end()
typedef pair<int, int> P;
const int mod = 1e9 + 7;
vector<int> dp, ans, a, g[200005];
void dfs(int v, int p = -1) {
int i = lower_bound(all(dp), a[v]) - dp.begin();
int old = dp[i];
dp[i] = a[v];
ans[v] = i;
if (p != -1)
ans[v] = max(ans[v], ans[p]);
for (auto u : g[v]) {
if (u == p)
continue;
dfs(u, v);
}
dp[i] = old;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
a = ans = vector<int>(n);
rep(i, n) cin >> a[i];
rep(i, n - 1) {
int u, v;
cin >> u >> v;
--u;
--v;
g[u].push_back(v);
g[v].push_back(u);
}
dp = vector<int>(n + 1, mod);
dp[0] = -1;
dfs(0);
rep(i, n) cout << ans[i] << endl;
return 0;
}
| replace | 29 | 30 | 29 | 30 | 0 | |
p02698 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define mp make_pair
#define sqr(x) (x) * (x)
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
const int maxn = 200007;
const ll inf = 0x3f3f3f3f;
const int mod = 998244353;
#define rep(i, x, y) for (auto i = (x); i <= (y); ++i)
#define dep(i, x, y) for (auto i = (x); i >= (y); --i)
int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
x = x * 10 + ch - '0', ch = getchar();
return x * f;
}
vector<int> g[maxn];
int aa[maxn], pre[maxn];
int a[maxn];
int lowbit(int x) { return x & (-x); }
int ans[maxn];
map<int, int> aaa;
void dfs(int u, int fa) {
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (v ^ fa) {
ans[v] = 1;
queue<int> G;
for (int i = a[v]; i; i -= lowbit(i))
ans[v] = max(ans[v], aa[i]);
int x = ans[v];
aaa[ans[v]]++;
for (int i = a[v] + 1; i <= 200000; i += lowbit(i))
G.push(aa[i]), aa[i] = max(aa[i], ans[v] + 1);
for (int i = 200000; i; i--)
ans[v] = max(ans[v], aa[i] - 1);
dfs(v, u);
aaa[x]--;
if (!aaa[x])
aaa.erase(x);
for (int i = a[v] + 1; i <= 200000; i += lowbit(i))
aa[i] = G.front(), G.pop();
}
}
}
int A[maxn];
int main() {
int n = read();
for (int i = 1; i <= n; i++)
A[i] = read(), a[i] = A[i];
sort(A + 1, A + n + 1);
int len = unique(A + 1, A + n + 1) - A - 1;
for (int i = 1; i <= n; i++)
a[i] = lower_bound(A + 1, A + len + 1, a[i]) - A;
for (int i = 1; i < n; i++) {
int u = read(), v = read();
g[u].push_back(v);
g[v].push_back(u);
}
ans[1] = 1;
for (int i = a[1] + 1; i <= 200000; i += lowbit(i))
aa[i] = max(aa[i], ans[1] + 1);
aaa[1]++;
dfs(1, 0);
for (int i = 1; i <= n; i++) {
printf("%d\n", ans[i]);
}
return 0;
} | #include <bits/stdc++.h>
#define mp make_pair
#define sqr(x) (x) * (x)
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
const int maxn = 200007;
const ll inf = 0x3f3f3f3f;
const int mod = 998244353;
#define rep(i, x, y) for (auto i = (x); i <= (y); ++i)
#define dep(i, x, y) for (auto i = (x); i >= (y); --i)
int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
x = x * 10 + ch - '0', ch = getchar();
return x * f;
}
vector<int> g[maxn];
int aa[maxn], pre[maxn];
int a[maxn];
int lowbit(int x) { return x & (-x); }
int ans[maxn];
map<int, int> aaa;
void dfs(int u, int fa) {
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (v ^ fa) {
ans[v] = 1;
queue<int> G;
for (int i = a[v]; i; i -= lowbit(i))
ans[v] = max(ans[v], aa[i]);
int x = ans[v];
aaa[ans[v]]++;
for (int i = a[v] + 1; i <= 200000; i += lowbit(i))
G.push(aa[i]), aa[i] = max(aa[i], ans[v] + 1);
for (int i = 200000; i; i -= lowbit(i))
ans[v] = max(ans[v], aa[i] - 1);
dfs(v, u);
aaa[x]--;
if (!aaa[x])
aaa.erase(x);
for (int i = a[v] + 1; i <= 200000; i += lowbit(i))
aa[i] = G.front(), G.pop();
}
}
}
int A[maxn];
int main() {
int n = read();
for (int i = 1; i <= n; i++)
A[i] = read(), a[i] = A[i];
sort(A + 1, A + n + 1);
int len = unique(A + 1, A + n + 1) - A - 1;
for (int i = 1; i <= n; i++)
a[i] = lower_bound(A + 1, A + len + 1, a[i]) - A;
for (int i = 1; i < n; i++) {
int u = read(), v = read();
g[u].push_back(v);
g[v].push_back(u);
}
ans[1] = 1;
for (int i = a[1] + 1; i <= 200000; i += lowbit(i))
aa[i] = max(aa[i], ans[1] + 1);
aaa[1]++;
dfs(1, 0);
for (int i = 1; i <= n; i++) {
printf("%d\n", ans[i]);
}
return 0;
} | replace | 41 | 42 | 41 | 42 | TLE | |
p02698 | C++ | Runtime Error | #include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 2e5 + 5;
int ans[MAXN];
vector<int> G[MAXN];
vector<pii> tag___[MAXN];
vector<int> pool;
int VV[MAXN];
int bit[MAXN];
int Q(int x) {
int ret = 0;
while (x) {
ret = max(ret, bit[x]);
x -= x & (-x);
}
return ret;
}
int M(int x, int v, int u) {
while (x <= (int)pool.size()) {
tag___[u].push_back({x, bit[x]});
bit[x] = max(bit[x], v);
x += x & (-x);
}
}
int magic(int x) {
return lower_bound(pool.begin(), pool.end(), x) - pool.begin() + 1;
}
void dfs(int u, int p) {
int x = magic(VV[u]);
int nt = Q(x - 1) + 1;
M(x, nt, u);
ans[u] = Q(pool.size());
for (auto &v : G[u]) {
if (v == p)
continue;
dfs(v, u);
}
for (auto &p : tag___[u]) {
bit[p.F] = p.S;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> VV[i];
pool.push_back(VV[i]);
}
sort(pool.begin(), pool.end());
pool.erase(unique(pool.begin(), pool.end()), pool.end());
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
dfs(1, 0);
for (int i = 1; i <= n; i++)
cout << ans[i] << '\n';
} | #include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 2e5 + 5;
int ans[MAXN];
vector<int> G[MAXN];
vector<pii> tag___[MAXN];
vector<int> pool;
int VV[MAXN];
int bit[MAXN];
int Q(int x) {
int ret = 0;
while (x) {
ret = max(ret, bit[x]);
x -= x & (-x);
}
return ret;
}
void M(int x, int v, int u) {
while (x <= (int)pool.size()) {
tag___[u].push_back({x, bit[x]});
bit[x] = max(bit[x], v);
x += x & (-x);
}
}
int magic(int x) {
return lower_bound(pool.begin(), pool.end(), x) - pool.begin() + 1;
}
void dfs(int u, int p) {
int x = magic(VV[u]);
int nt = Q(x - 1) + 1;
M(x, nt, u);
ans[u] = Q(pool.size());
for (auto &v : G[u]) {
if (v == p)
continue;
dfs(v, u);
}
for (auto &p : tag___[u]) {
bit[p.F] = p.S;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> VV[i];
pool.push_back(VV[i]);
}
sort(pool.begin(), pool.end());
pool.erase(unique(pool.begin(), pool.end()), pool.end());
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
dfs(1, 0);
for (int i = 1; i <= n; i++)
cout << ans[i] << '\n';
} | replace | 22 | 23 | 22 | 23 | 0 | |
p02698 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, s, n) for (ll i = s; i < (ll)(n); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define pb push_back
#define COUT(x) cout << (x) << "\n"
#define COUTF(x) cout << setprecision(15) << (x) << "\n"
#define ENDL cout << "\n"
#define DF(x) x.erase(x.begin())
#define ALL(x) x.begin(), x.end()
#define SZ(x) (ll) x.size()
#define SORT(x) sort(ALL(x))
#define REVERSE(x) reverse(ALL(x))
#define ANS cout << ans << "\n"
#define init() \
cin.tie(0); \
ios::sync_with_stdio(false)
#define LINE cerr << "[debug] line: " << __LINE__ << "\n";
#define debug(x) cerr << "[debug] " << #x << ": " << x << "\n";
#define debugV(v) \
cerr << "[debugV] " << #v << ":"; \
rep(i, v.size()) cerr << " " << v[i]; \
cerr << "\n";
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using mll = map<ll, ll>;
using qll = queue<ll>;
using P = pair<ll, ll>;
constexpr ll INF = 0x3f3f3f3f3f3f3f3f;
constexpr ld PI = 3.141592653589793238462643383279;
ll get_digit(ll x) { return to_string(x).size(); }
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
vector<P> factorize(ll n) {
vector<P> result;
for (ll i = 2; i * i <= n; ++i) {
if (n % i == 0) {
result.pb({i, 0});
while (n % i == 0) {
n /= i;
result.back().second++;
}
}
}
if (n != 1) {
result.pb({n, 1});
}
return result;
}
vll divisor(ll n) {
vll ret;
for (ll i = 1; i * i <= n; i++) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n)
ret.push_back(n / i);
}
}
SORT(ret);
return (ret);
}
const int mod = 1000000007;
struct mint {
ll x;
mint(ll x = 0) : x((x % mod + mod) % mod) {}
mint operator-() const { return mint(-x); }
mint &operator+=(const mint a) {
if ((x += a.x) >= mod)
x -= mod;
return *this;
}
mint &operator-=(const mint a) {
if ((x += mod - a.x) >= mod)
x -= mod;
return *this;
}
mint &operator*=(const mint a) {
(x *= a.x) %= mod;
return *this;
}
mint operator+(const mint a) const { return mint(*this) += a; }
mint operator-(const mint a) const { return mint(*this) -= a; }
mint operator*(const mint a) const { return mint(*this) *= a; }
mint pow(ll t) const {
if (!t)
return 1;
mint a = pow(t >> 1);
a *= a;
if (t & 1)
a *= *this;
return a;
}
// for prime mod
mint inv() const { return pow(mod - 2); }
mint &operator/=(const mint a) { return *this *= a.inv(); }
mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
struct combination {
vector<mint> fact, ifact;
combination(ll n) : fact(n + 1), ifact(n + 1) {
assert(n < mod);
fact[0] = 1;
for (ll i = 1; i <= n; ++i) {
fact[i] = fact[i - 1] * i;
}
ifact[n] = fact[n].inv();
for (ll i = n; i >= 1; --i) {
ifact[i - 1] = ifact[i] * i;
}
}
mint operator()(ll n, ll k) {
if (k < 0 || k > n)
return 0;
return fact[n] * ifact[k] * ifact[n - k];
}
};
const ll MAX_N = 20005;
vll A, ans;
vll to[MAX_N];
ll dp[MAX_N];
void dfs(ll v, ll p = -1) {
ll i = lower_bound(dp, dp + MAX_N, A[v]) - dp;
ll old = dp[i];
dp[i] = A[v];
ans[v] = i;
if (p != -1)
ans[v] = max(ans[v], ans[p]);
for (ll u : to[v]) {
if (u == p)
continue;
dfs(u, v);
}
dp[i] = old;
}
signed main() {
init();
ll N;
cin >> N;
A = ans = vll(N);
rep(i, N) cin >> A[i];
rep(i, N - 1) {
ll u, v;
cin >> u >> v;
u--;
v--;
to[u].pb(v);
to[v].pb(u);
}
rep(i, MAX_N) dp[i] = INF;
dp[0] = -INF;
dfs(0);
rep(i, N) COUT(ans[i]);
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, s, n) for (ll i = s; i < (ll)(n); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define pb push_back
#define COUT(x) cout << (x) << "\n"
#define COUTF(x) cout << setprecision(15) << (x) << "\n"
#define ENDL cout << "\n"
#define DF(x) x.erase(x.begin())
#define ALL(x) x.begin(), x.end()
#define SZ(x) (ll) x.size()
#define SORT(x) sort(ALL(x))
#define REVERSE(x) reverse(ALL(x))
#define ANS cout << ans << "\n"
#define init() \
cin.tie(0); \
ios::sync_with_stdio(false)
#define LINE cerr << "[debug] line: " << __LINE__ << "\n";
#define debug(x) cerr << "[debug] " << #x << ": " << x << "\n";
#define debugV(v) \
cerr << "[debugV] " << #v << ":"; \
rep(i, v.size()) cerr << " " << v[i]; \
cerr << "\n";
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using mll = map<ll, ll>;
using qll = queue<ll>;
using P = pair<ll, ll>;
constexpr ll INF = 0x3f3f3f3f3f3f3f3f;
constexpr ld PI = 3.141592653589793238462643383279;
ll get_digit(ll x) { return to_string(x).size(); }
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
vector<P> factorize(ll n) {
vector<P> result;
for (ll i = 2; i * i <= n; ++i) {
if (n % i == 0) {
result.pb({i, 0});
while (n % i == 0) {
n /= i;
result.back().second++;
}
}
}
if (n != 1) {
result.pb({n, 1});
}
return result;
}
vll divisor(ll n) {
vll ret;
for (ll i = 1; i * i <= n; i++) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n)
ret.push_back(n / i);
}
}
SORT(ret);
return (ret);
}
const int mod = 1000000007;
struct mint {
ll x;
mint(ll x = 0) : x((x % mod + mod) % mod) {}
mint operator-() const { return mint(-x); }
mint &operator+=(const mint a) {
if ((x += a.x) >= mod)
x -= mod;
return *this;
}
mint &operator-=(const mint a) {
if ((x += mod - a.x) >= mod)
x -= mod;
return *this;
}
mint &operator*=(const mint a) {
(x *= a.x) %= mod;
return *this;
}
mint operator+(const mint a) const { return mint(*this) += a; }
mint operator-(const mint a) const { return mint(*this) -= a; }
mint operator*(const mint a) const { return mint(*this) *= a; }
mint pow(ll t) const {
if (!t)
return 1;
mint a = pow(t >> 1);
a *= a;
if (t & 1)
a *= *this;
return a;
}
// for prime mod
mint inv() const { return pow(mod - 2); }
mint &operator/=(const mint a) { return *this *= a.inv(); }
mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
struct combination {
vector<mint> fact, ifact;
combination(ll n) : fact(n + 1), ifact(n + 1) {
assert(n < mod);
fact[0] = 1;
for (ll i = 1; i <= n; ++i) {
fact[i] = fact[i - 1] * i;
}
ifact[n] = fact[n].inv();
for (ll i = n; i >= 1; --i) {
ifact[i - 1] = ifact[i] * i;
}
}
mint operator()(ll n, ll k) {
if (k < 0 || k > n)
return 0;
return fact[n] * ifact[k] * ifact[n - k];
}
};
const ll MAX_N = 200005;
vll A, ans;
vll to[MAX_N];
ll dp[MAX_N];
void dfs(ll v, ll p = -1) {
ll i = lower_bound(dp, dp + MAX_N, A[v]) - dp;
ll old = dp[i];
dp[i] = A[v];
ans[v] = i;
if (p != -1)
ans[v] = max(ans[v], ans[p]);
for (ll u : to[v]) {
if (u == p)
continue;
dfs(u, v);
}
dp[i] = old;
}
signed main() {
init();
ll N;
cin >> N;
A = ans = vll(N);
rep(i, N) cin >> A[i];
rep(i, N - 1) {
ll u, v;
cin >> u >> v;
u--;
v--;
to[u].pb(v);
to[v].pb(u);
}
rep(i, MAX_N) dp[i] = INF;
dp[0] = -INF;
dfs(0);
rep(i, N) COUT(ans[i]);
return 0;
} | replace | 130 | 131 | 130 | 131 | 0 | |
p02698 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define mx 20005
vector<int> adj[mx];
vector<int> val(mx);
vector<int> lis(mx, -1);
vector<int> currseq;
void dfs(int a) {
auto it = lower_bound(currseq.begin(), currseq.end(), val[a]);
int prevval = -1;
int index = -1;
if (it == currseq.end()) {
currseq.push_back(val[a]);
} else {
index = it - currseq.begin();
prevval = currseq[index];
currseq[index] = val[a];
}
lis[a] = currseq.size();
for (int s : adj[a]) {
if (lis[s] == -1) {
dfs(s);
}
}
if (prevval == -1) {
currseq.pop_back();
} else {
currseq[index] = prevval;
}
}
int main() {
int n;
cin >> n;
for (int q = 1; q <= n; q++) {
cin >> val[q];
}
for (int q = 0; q < n - 1; q++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(1);
for (int q = 1; q <= n; q++) {
cout << lis[q] << endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define mx 200005
vector<int> adj[mx];
vector<int> val(mx);
vector<int> lis(mx, -1);
vector<int> currseq;
void dfs(int a) {
auto it = lower_bound(currseq.begin(), currseq.end(), val[a]);
int prevval = -1;
int index = -1;
if (it == currseq.end()) {
currseq.push_back(val[a]);
} else {
index = it - currseq.begin();
prevval = currseq[index];
currseq[index] = val[a];
}
lis[a] = currseq.size();
for (int s : adj[a]) {
if (lis[s] == -1) {
dfs(s);
}
}
if (prevval == -1) {
currseq.pop_back();
} else {
currseq[index] = prevval;
}
}
int main() {
int n;
cin >> n;
for (int q = 1; q <= n; q++) {
cin >> val[q];
}
for (int q = 0; q < n - 1; q++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(1);
for (int q = 1; q <= n; q++) {
cout << lis[q] << endl;
}
return 0;
}
| replace | 2 | 3 | 2 | 3 | 0 | |
p02698 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 1e18;
#define rep(i, n) for (ll i = 0; i < (int)(n); i++)
vector<ll> a(200005);
vector<ll> to[200005];
vector<ll> dp(200005, INF);
vector<ll> ans(200005, 0);
int dfs(int t, int p) {
ll id = lower_bound(dp.begin(), dp.end(), a[t]) - dp.begin();
ll prev = dp[id];
dp[id] = a[t];
if (p != -1)
ans[t] = max(id, ans[p]);
else
ans[t] = id;
// cout<<t<<' '<<id<<' '<<a[t]<<endl;
for (int u : to[t]) {
if (u == p)
continue;
dfs(u, t);
}
dp[id] = prev;
}
int main() {
ll n;
cin >> n;
rep(i, n) { cin >> a[i]; }
rep(i, n - 1) {
ll u1, v1;
cin >> u1 >> v1;
u1--;
v1--;
to[u1].push_back(v1);
to[v1].push_back(u1);
}
dp[0] = -INF;
dfs(0, -1);
rep(i, n) cout << ans[i] << endl;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 1e18;
#define rep(i, n) for (ll i = 0; i < (int)(n); i++)
vector<ll> a(200005);
vector<ll> to[200005];
vector<ll> dp(200005, INF);
vector<ll> ans(200005, 0);
int dfs(int t, int p) {
ll id = lower_bound(dp.begin(), dp.end(), a[t]) - dp.begin();
ll prev = dp[id];
dp[id] = a[t];
if (p != -1)
ans[t] = max(id, ans[p]);
else
ans[t] = id;
// cout<<t<<' '<<id<<' '<<a[t]<<endl;
for (int u : to[t]) {
if (u == p)
continue;
dfs(u, t);
}
dp[id] = prev;
return 0;
}
int main() {
ll n;
cin >> n;
rep(i, n) { cin >> a[i]; }
rep(i, n - 1) {
ll u1, v1;
cin >> u1 >> v1;
u1--;
v1--;
to[u1].push_back(v1);
to[v1].push_back(u1);
}
dp[0] = -INF;
dfs(0, -1);
rep(i, n) cout << ans[i] << endl;
}
| insert | 28 | 28 | 28 | 29 | 0 | |
p02698 | C++ | Time Limit Exceeded | #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define repd(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) repd(i, 0, n)
using ll = long long;
using ld = long double;
#define int ll
#define double ld
#define VARNAME(v) #v
using P = pair<int, int>;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr ll INF = 1e16;
#pragma region tmp
constexpr double EPS = 1e-10;
constexpr double PI = 3.141592653589793;
const string endn = "\n";
const string abc = "abcdefghijklmnopqrstuvwxyz";
const string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
template <class T1, class T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &o) {
out << "(" << o.first << ", " << o.second << ")";
return out;
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &o) {
for (auto itr = o.begin(); itr != o.end(); itr++)
out << *itr << (itr + 1 != o.end() ? " " : "");
return out;
}
template <class T> ostream &operator<<(ostream &out, const deque<T> &o) {
for (auto itr = o.begin(); itr != o.end(); itr++)
out << *itr << (itr + 1 != o.end() ? " " : "");
return out;
}
template <class T> ostream &operator<<(ostream &out, const set<T> &o) {
out << "{";
for (auto itr = o.begin(); itr != o.end(); itr++)
out << *itr << (next(itr, 1) != o.end() ? "," : "");
out << "}";
return out;
}
template <class T> ostream &operator<<(ostream &out, const multiset<T> &o) {
out << "{";
for (auto itr = o.begin(); itr != o.end(); itr++)
out << *itr << (next(itr, 1) != o.end() ? "," : "");
out << "}";
return out;
}
template <class T, class U>
ostream &operator<<(ostream &out, const map<T, U> &o) {
out << "{";
for (auto itr = o.begin(); itr != o.end(); itr++)
out << *itr << (next(itr, 1) != o.end() ? " " : "");
out << "}";
return out;
}
template <class T, class U>
ostream &operator<<(ostream &out, const multimap<T, U> &o) {
out << "{";
for (auto itr = o.begin(); itr != o.end(); itr++)
out << *itr << (next(itr, 1) != o.end() ? " " : "");
out << "}";
return out;
}
template <class T> ostream &operator<<(ostream &out, queue<T> o) {
while (!o.empty())
out << o.front() << (o.size() > 0 ? " " : ""), o.pop();
return out;
}
template <class T, class U>
ostream &operator<<(ostream &out, priority_queue<T, vector<T>, U> o) {
while (!o.empty())
out << o.top() << (o.size() > 0 ? " " : ""), o.pop();
return out;
}
template <class T> ostream &operator<<(ostream &out, stack<T> o) {
while (!o.empty())
out << o.top() << (o.size() > 0 ? " " : ""), o.pop();
return out;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 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, initializer_list<T> b) {
T c = min(b);
if (a > c) {
a = c;
return 1;
}
return 0;
}
template <class T> inline bool chmax(T &a, initializer_list<T> b) {
T c = max(b);
if (a < c) {
a = c;
return 1;
}
return 0;
}
inline int mrep(int &a, int M = MOD) {
a %= M;
return a = (a < 0 ? a + M : a);
}
inline int add(int &a, int b, int M = MOD) {
return a = (mrep(a, M) + mrep(b, M)) % M;
}
inline int mul(int &a, int b, int M = MOD) {
return a = (mrep(a, M) * mrep(b, M)) % M;
}
inline int add(int &a, initializer_list<int> b, int M = MOD) {
return a = (mrep(a, M) + accumulate(b.begin(), b.end(), (int)0,
[&M](int acc, int i) {
return (acc + mrep(i, M)) % M;
})) %
M;
}
inline int mul(int &a, initializer_list<int> b, int M = MOD) {
return a = (mrep(a, M) * accumulate(b.begin(), b.end(), (int)1,
[&M](int acc, int i) {
return (acc * mrep(i, M)) % M;
})) %
M;
}
inline int modpow(int b, int e, int M = MOD) {
int ret = 1;
while (e > 0) {
if (e % 2)
mul(ret, b, M);
mul(b, b, M);
e /= 2;
};
return ret;
}
inline int modinv(int a, int M = MOD) {
int b = M, u = 1, v = 0;
while (b) {
int t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
return mrep(u, M);
}
template <class T> inline void print(T a) { cout << a << endl; }
template <class T> inline void print(vector<T> a, int n) {
chmin(n, int(a.size()));
rep(i, n) cout << a[i] << (i != n - 1 ? " " : "\n");
}
template <class T, size_t SIZE> inline void print(T (&a)[SIZE], int n = SIZE) {
chmin(n, int(SIZE));
rep(i, n) cout << a[i] << (i != n - 1 ? " " : "\n");
}
#pragma #endregion tmp
int n, sz;
vector<int> g[210000];
vector<int> ans, seg, a, b;
void update(int i, int x) {
i += (sz - 1);
seg[i] = x;
while (i > 0) {
i = (i - 1) / 2;
seg[i] = max(seg[2 * i + 1], seg[2 * i + 2]);
}
}
int get(int a, int b, int k = 0, int l = 0, int r = -1) {
if (r < 0)
r = sz;
if (a <= l && r <= b)
return seg[k];
if (r <= a || b <= l)
return 0;
int vl = get(a, b, 2 * k + 1, l, (l + r) / 2),
vr = get(a, b, 2 * k + 2, (l + r) / 2, r);
return max(vl, vr);
}
void dfs(int x, int p) {
ans[x] = get(0, n);
for (auto to : g[x]) {
if (to == p)
continue;
int m = get(0, a[to]) + 1, org = get(a[to], a[to] + 1);
chmax(m, org);
update(a[to], m);
dfs(to, x);
update(a[to], org);
}
}
signed main() {
cin >> n;
a.resize(n);
b.resize(n);
rep(i, n) cin >> a[i], b[i] = a[i];
rep(i, n - 1) {
int uu, vv;
cin >> uu >> vv;
uu--;
vv--;
g[uu].push_back(vv);
g[vv].push_back(uu);
}
sz = 1;
while (sz < n)
sz *= 2;
ans.resize(n);
seg.resize(2 * sz - 1, 0);
sort(b.begin(), b.end());
b.erase(unique(b.begin(), b.end()), b.end());
rep(i, n) a[i] = lower_bound(b.begin(), b.end(), a[i]) - b.begin();
update(a[0], 1);
dfs(0, -1);
rep(i, n) cout << ans[i] << endl;
}
| #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define repd(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) repd(i, 0, n)
using ll = long long;
using ld = long double;
#define int ll
#define double ld
#define VARNAME(v) #v
using P = pair<int, int>;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr ll INF = 1e16;
#pragma region tmp
constexpr double EPS = 1e-10;
constexpr double PI = 3.141592653589793;
const string endn = "\n";
const string abc = "abcdefghijklmnopqrstuvwxyz";
const string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
template <class T1, class T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &o) {
out << "(" << o.first << ", " << o.second << ")";
return out;
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &o) {
for (auto itr = o.begin(); itr != o.end(); itr++)
out << *itr << (itr + 1 != o.end() ? " " : "");
return out;
}
template <class T> ostream &operator<<(ostream &out, const deque<T> &o) {
for (auto itr = o.begin(); itr != o.end(); itr++)
out << *itr << (itr + 1 != o.end() ? " " : "");
return out;
}
template <class T> ostream &operator<<(ostream &out, const set<T> &o) {
out << "{";
for (auto itr = o.begin(); itr != o.end(); itr++)
out << *itr << (next(itr, 1) != o.end() ? "," : "");
out << "}";
return out;
}
template <class T> ostream &operator<<(ostream &out, const multiset<T> &o) {
out << "{";
for (auto itr = o.begin(); itr != o.end(); itr++)
out << *itr << (next(itr, 1) != o.end() ? "," : "");
out << "}";
return out;
}
template <class T, class U>
ostream &operator<<(ostream &out, const map<T, U> &o) {
out << "{";
for (auto itr = o.begin(); itr != o.end(); itr++)
out << *itr << (next(itr, 1) != o.end() ? " " : "");
out << "}";
return out;
}
template <class T, class U>
ostream &operator<<(ostream &out, const multimap<T, U> &o) {
out << "{";
for (auto itr = o.begin(); itr != o.end(); itr++)
out << *itr << (next(itr, 1) != o.end() ? " " : "");
out << "}";
return out;
}
template <class T> ostream &operator<<(ostream &out, queue<T> o) {
while (!o.empty())
out << o.front() << (o.size() > 0 ? " " : ""), o.pop();
return out;
}
template <class T, class U>
ostream &operator<<(ostream &out, priority_queue<T, vector<T>, U> o) {
while (!o.empty())
out << o.top() << (o.size() > 0 ? " " : ""), o.pop();
return out;
}
template <class T> ostream &operator<<(ostream &out, stack<T> o) {
while (!o.empty())
out << o.top() << (o.size() > 0 ? " " : ""), o.pop();
return out;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 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, initializer_list<T> b) {
T c = min(b);
if (a > c) {
a = c;
return 1;
}
return 0;
}
template <class T> inline bool chmax(T &a, initializer_list<T> b) {
T c = max(b);
if (a < c) {
a = c;
return 1;
}
return 0;
}
inline int mrep(int &a, int M = MOD) {
a %= M;
return a = (a < 0 ? a + M : a);
}
inline int add(int &a, int b, int M = MOD) {
return a = (mrep(a, M) + mrep(b, M)) % M;
}
inline int mul(int &a, int b, int M = MOD) {
return a = (mrep(a, M) * mrep(b, M)) % M;
}
inline int add(int &a, initializer_list<int> b, int M = MOD) {
return a = (mrep(a, M) + accumulate(b.begin(), b.end(), (int)0,
[&M](int acc, int i) {
return (acc + mrep(i, M)) % M;
})) %
M;
}
inline int mul(int &a, initializer_list<int> b, int M = MOD) {
return a = (mrep(a, M) * accumulate(b.begin(), b.end(), (int)1,
[&M](int acc, int i) {
return (acc * mrep(i, M)) % M;
})) %
M;
}
inline int modpow(int b, int e, int M = MOD) {
int ret = 1;
while (e > 0) {
if (e % 2)
mul(ret, b, M);
mul(b, b, M);
e /= 2;
};
return ret;
}
inline int modinv(int a, int M = MOD) {
int b = M, u = 1, v = 0;
while (b) {
int t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
return mrep(u, M);
}
template <class T> inline void print(T a) { cout << a << endl; }
template <class T> inline void print(vector<T> a, int n) {
chmin(n, int(a.size()));
rep(i, n) cout << a[i] << (i != n - 1 ? " " : "\n");
}
template <class T, size_t SIZE> inline void print(T (&a)[SIZE], int n = SIZE) {
chmin(n, int(SIZE));
rep(i, n) cout << a[i] << (i != n - 1 ? " " : "\n");
}
#pragma #endregion tmp
int n, sz;
vector<int> g[210000];
vector<int> ans, seg, a, b;
void update(int i, int x) {
i += (sz - 1);
seg[i] = x;
while (i > 0) {
i = (i - 1) / 2;
seg[i] = max(seg[2 * i + 1], seg[2 * i + 2]);
}
}
int get(int a, int b, int k = 0, int l = 0, int r = -1) {
if (r < 0)
r = sz;
if (a <= l && r <= b)
return seg[k];
if (r <= a || b <= l)
return 0;
int vl = get(a, b, 2 * k + 1, l, (l + r) / 2),
vr = get(a, b, 2 * k + 2, (l + r) / 2, r);
return max(vl, vr);
}
void dfs(int x, int p) {
ans[x] = get(0, n);
for (auto to : g[x]) {
if (to == p)
continue;
int m = get(0, a[to]) + 1, org = get(a[to], a[to] + 1);
chmax(m, org);
update(a[to], m);
dfs(to, x);
update(a[to], org);
}
}
signed main() {
cin >> n;
a.resize(n);
b.resize(n);
rep(i, n) cin >> a[i], b[i] = a[i];
rep(i, n - 1) {
int uu, vv;
cin >> uu >> vv;
uu--;
vv--;
g[uu].push_back(vv);
g[vv].push_back(uu);
}
sz = 1;
while (sz < n)
sz *= 2;
ans.resize(n);
seg.resize(2 * sz - 1, 0);
sort(b.begin(), b.end());
b.erase(unique(b.begin(), b.end()), b.end());
map<int, int> mp;
rep(i, b.size()) mp[b[i]] = i;
rep(i, n) a[i] = mp[a[i]];
update(a[0], 1);
dfs(0, -1);
rep(i, n) cout << ans[i] << endl;
}
| replace | 224 | 225 | 224 | 227 | TLE | |
p02698 | C++ | Time Limit Exceeded | #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define repd(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) repd(i, 0, n)
using ll = long long;
using ld = long double;
#define int ll
#define double ld
#define VARNAME(v) #v
using P = pair<int, int>;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr ll INF = 1e16;
#pragma region tmp
constexpr double EPS = 1e-10;
constexpr double PI = 3.141592653589793;
const string endn = "\n";
const string abc = "abcdefghijklmnopqrstuvwxyz";
const string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
template <class T1, class T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &o) {
out << "(" << o.first << ", " << o.second << ")";
return out;
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &o) {
for (auto itr = o.begin(); itr != o.end(); itr++)
out << *itr << (itr + 1 != o.end() ? " " : "");
return out;
}
template <class T> ostream &operator<<(ostream &out, const deque<T> &o) {
for (auto itr = o.begin(); itr != o.end(); itr++)
out << *itr << (itr + 1 != o.end() ? " " : "");
return out;
}
template <class T> ostream &operator<<(ostream &out, const set<T> &o) {
out << "{";
for (auto itr = o.begin(); itr != o.end(); itr++)
out << *itr << (next(itr, 1) != o.end() ? "," : "");
out << "}";
return out;
}
template <class T> ostream &operator<<(ostream &out, const multiset<T> &o) {
out << "{";
for (auto itr = o.begin(); itr != o.end(); itr++)
out << *itr << (next(itr, 1) != o.end() ? "," : "");
out << "}";
return out;
}
template <class T, class U>
ostream &operator<<(ostream &out, const map<T, U> &o) {
out << "{";
for (auto itr = o.begin(); itr != o.end(); itr++)
out << *itr << (next(itr, 1) != o.end() ? " " : "");
out << "}";
return out;
}
template <class T, class U>
ostream &operator<<(ostream &out, const multimap<T, U> &o) {
out << "{";
for (auto itr = o.begin(); itr != o.end(); itr++)
out << *itr << (next(itr, 1) != o.end() ? " " : "");
out << "}";
return out;
}
template <class T> ostream &operator<<(ostream &out, queue<T> o) {
while (!o.empty())
out << o.front() << (o.size() > 0 ? " " : ""), o.pop();
return out;
}
template <class T, class U>
ostream &operator<<(ostream &out, priority_queue<T, vector<T>, U> o) {
while (!o.empty())
out << o.top() << (o.size() > 0 ? " " : ""), o.pop();
return out;
}
template <class T> ostream &operator<<(ostream &out, stack<T> o) {
while (!o.empty())
out << o.top() << (o.size() > 0 ? " " : ""), o.pop();
return out;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 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, initializer_list<T> b) {
T c = min(b);
if (a > c) {
a = c;
return 1;
}
return 0;
}
template <class T> inline bool chmax(T &a, initializer_list<T> b) {
T c = max(b);
if (a < c) {
a = c;
return 1;
}
return 0;
}
inline int mrep(int &a, int M = MOD) {
a %= M;
return a = (a < 0 ? a + M : a);
}
inline int add(int &a, int b, int M = MOD) {
return a = (mrep(a, M) + mrep(b, M)) % M;
}
inline int mul(int &a, int b, int M = MOD) {
return a = (mrep(a, M) * mrep(b, M)) % M;
}
inline int add(int &a, initializer_list<int> b, int M = MOD) {
return a = (mrep(a, M) + accumulate(b.begin(), b.end(), (int)0,
[&M](int acc, int i) {
return (acc + mrep(i, M)) % M;
})) %
M;
}
inline int mul(int &a, initializer_list<int> b, int M = MOD) {
return a = (mrep(a, M) * accumulate(b.begin(), b.end(), (int)1,
[&M](int acc, int i) {
return (acc * mrep(i, M)) % M;
})) %
M;
}
inline int modpow(int b, int e, int M = MOD) {
int ret = 1;
while (e > 0) {
if (e % 2)
mul(ret, b, M);
mul(b, b, M);
e /= 2;
};
return ret;
}
inline int modinv(int a, int M = MOD) {
int b = M, u = 1, v = 0;
while (b) {
int t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
return mrep(u, M);
}
template <class T> inline void print(T a) { cout << a << endl; }
template <class T> inline void print(vector<T> a, int n) {
chmin(n, int(a.size()));
rep(i, n) cout << a[i] << (i != n - 1 ? " " : "\n");
}
template <class T, size_t SIZE> inline void print(T (&a)[SIZE], int n = SIZE) {
chmin(n, int(SIZE));
rep(i, n) cout << a[i] << (i != n - 1 ? " " : "\n");
}
#pragma #endregion tmp
int n, sz;
vector<int> g[210000];
vector<int> ans, seg, a, b;
void update(int i, int x) {
i += (sz - 1);
seg[i] = x;
while (i > 0) {
i = (i - 1) / 2;
seg[i] = max(seg[2 * i + 1], seg[2 * i + 2]);
}
}
int get(int a, int b, int k = 0, int l = 0, int r = -1) {
if (r < 0)
r = sz;
if (a <= l && r <= b)
return seg[k];
if (r <= a || b <= l)
return 0;
int vl = get(a, b, 2 * k + 1, l, (l + r) / 2),
vr = get(a, b, 2 * k + 2, (l + r) / 2, r);
return max(vl, vr);
}
void dfs(int x, int p) {
ans[x] = get(0, n);
for (auto to : g[x]) {
if (to == p)
continue;
int m = get(0, a[to]) + 1, org = get(a[to], a[to] + 1);
chmax(m, org);
update(a[to], m);
dfs(to, x);
update(a[to], org);
}
}
signed main() {
cin >> n;
a.resize(n);
b.resize(n);
rep(i, n) cin >> a[i], b[i] = a[i];
rep(i, n - 1) {
int uu, vv;
cin >> uu >> vv;
uu--;
vv--;
g[uu].push_back(vv);
g[vv].push_back(uu);
}
sz = 1;
while (sz < n)
sz *= 2;
ans.resize(n);
seg.resize(2 * sz - 1, 0);
sort(b.begin(), b.end());
b.erase(unique(b.begin(), b.end()), b.end());
rep(i, n) a[i] = distance(b.begin(), lower_bound(b.begin(), b.end(), a[i]));
update(a[0], 1);
dfs(0, -1);
rep(i, n) cout << ans[i] << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define repd(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) repd(i, 0, n)
using ll = long long;
using ld = long double;
#define int ll
#define double ld
#define VARNAME(v) #v
using P = pair<int, int>;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr ll INF = 1e16;
#pragma region tmp
constexpr double EPS = 1e-10;
constexpr double PI = 3.141592653589793;
const string endn = "\n";
const string abc = "abcdefghijklmnopqrstuvwxyz";
const string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
template <class T1, class T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &o) {
out << "(" << o.first << ", " << o.second << ")";
return out;
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &o) {
for (auto itr = o.begin(); itr != o.end(); itr++)
out << *itr << (itr + 1 != o.end() ? " " : "");
return out;
}
template <class T> ostream &operator<<(ostream &out, const deque<T> &o) {
for (auto itr = o.begin(); itr != o.end(); itr++)
out << *itr << (itr + 1 != o.end() ? " " : "");
return out;
}
template <class T> ostream &operator<<(ostream &out, const set<T> &o) {
out << "{";
for (auto itr = o.begin(); itr != o.end(); itr++)
out << *itr << (next(itr, 1) != o.end() ? "," : "");
out << "}";
return out;
}
template <class T> ostream &operator<<(ostream &out, const multiset<T> &o) {
out << "{";
for (auto itr = o.begin(); itr != o.end(); itr++)
out << *itr << (next(itr, 1) != o.end() ? "," : "");
out << "}";
return out;
}
template <class T, class U>
ostream &operator<<(ostream &out, const map<T, U> &o) {
out << "{";
for (auto itr = o.begin(); itr != o.end(); itr++)
out << *itr << (next(itr, 1) != o.end() ? " " : "");
out << "}";
return out;
}
template <class T, class U>
ostream &operator<<(ostream &out, const multimap<T, U> &o) {
out << "{";
for (auto itr = o.begin(); itr != o.end(); itr++)
out << *itr << (next(itr, 1) != o.end() ? " " : "");
out << "}";
return out;
}
template <class T> ostream &operator<<(ostream &out, queue<T> o) {
while (!o.empty())
out << o.front() << (o.size() > 0 ? " " : ""), o.pop();
return out;
}
template <class T, class U>
ostream &operator<<(ostream &out, priority_queue<T, vector<T>, U> o) {
while (!o.empty())
out << o.top() << (o.size() > 0 ? " " : ""), o.pop();
return out;
}
template <class T> ostream &operator<<(ostream &out, stack<T> o) {
while (!o.empty())
out << o.top() << (o.size() > 0 ? " " : ""), o.pop();
return out;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 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, initializer_list<T> b) {
T c = min(b);
if (a > c) {
a = c;
return 1;
}
return 0;
}
template <class T> inline bool chmax(T &a, initializer_list<T> b) {
T c = max(b);
if (a < c) {
a = c;
return 1;
}
return 0;
}
inline int mrep(int &a, int M = MOD) {
a %= M;
return a = (a < 0 ? a + M : a);
}
inline int add(int &a, int b, int M = MOD) {
return a = (mrep(a, M) + mrep(b, M)) % M;
}
inline int mul(int &a, int b, int M = MOD) {
return a = (mrep(a, M) * mrep(b, M)) % M;
}
inline int add(int &a, initializer_list<int> b, int M = MOD) {
return a = (mrep(a, M) + accumulate(b.begin(), b.end(), (int)0,
[&M](int acc, int i) {
return (acc + mrep(i, M)) % M;
})) %
M;
}
inline int mul(int &a, initializer_list<int> b, int M = MOD) {
return a = (mrep(a, M) * accumulate(b.begin(), b.end(), (int)1,
[&M](int acc, int i) {
return (acc * mrep(i, M)) % M;
})) %
M;
}
inline int modpow(int b, int e, int M = MOD) {
int ret = 1;
while (e > 0) {
if (e % 2)
mul(ret, b, M);
mul(b, b, M);
e /= 2;
};
return ret;
}
inline int modinv(int a, int M = MOD) {
int b = M, u = 1, v = 0;
while (b) {
int t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
return mrep(u, M);
}
template <class T> inline void print(T a) { cout << a << endl; }
template <class T> inline void print(vector<T> a, int n) {
chmin(n, int(a.size()));
rep(i, n) cout << a[i] << (i != n - 1 ? " " : "\n");
}
template <class T, size_t SIZE> inline void print(T (&a)[SIZE], int n = SIZE) {
chmin(n, int(SIZE));
rep(i, n) cout << a[i] << (i != n - 1 ? " " : "\n");
}
#pragma #endregion tmp
int n, sz;
vector<int> g[210000];
vector<int> ans, seg, a, b;
void update(int i, int x) {
i += (sz - 1);
seg[i] = x;
while (i > 0) {
i = (i - 1) / 2;
seg[i] = max(seg[2 * i + 1], seg[2 * i + 2]);
}
}
int get(int a, int b, int k = 0, int l = 0, int r = -1) {
if (r < 0)
r = sz;
if (a <= l && r <= b)
return seg[k];
if (r <= a || b <= l)
return 0;
int vl = get(a, b, 2 * k + 1, l, (l + r) / 2),
vr = get(a, b, 2 * k + 2, (l + r) / 2, r);
return max(vl, vr);
}
void dfs(int x, int p) {
ans[x] = get(0, n);
for (auto to : g[x]) {
if (to == p)
continue;
int m = get(0, a[to]) + 1, org = get(a[to], a[to] + 1);
chmax(m, org);
update(a[to], m);
dfs(to, x);
update(a[to], org);
}
}
signed main() {
cin >> n;
a.resize(n);
b.resize(n);
rep(i, n) cin >> a[i], b[i] = a[i];
rep(i, n - 1) {
int uu, vv;
cin >> uu >> vv;
uu--;
vv--;
g[uu].push_back(vv);
g[vv].push_back(uu);
}
sz = 1;
while (sz < n)
sz *= 2;
ans.resize(n);
seg.resize(2 * sz - 1, 0);
sort(b.begin(), b.end());
b.erase(unique(b.begin(), b.end()), b.end());
rep(i, n) a[i] = distance(b.begin(), lower_bound(b.begin(), b.end(), a[i]));
update(a[0], 1);
dfs(0, -1);
rep(i, n) cout << ans[i] << endl;
}
| delete | 0 | 1 | 0 | 0 | TLE | |
p02698 | C++ | Runtime Error | #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstddef>
#include <deque>
#include <functional>
#include <initializer_list>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <variant>
#include <vector>
#define endl codeforces
#define ALL(v) std::begin(v), std::end(v)
#define ALLR(v) std::rbegin(v), std::rend(v)
using ll = std::int64_t;
using ull = std::uint64_t;
using pii = std::pair<int, int>;
using tii = std::tuple<int, int, int>;
using pll = std::pair<ll, ll>;
using tll = std::tuple<ll, ll, ll>;
template <typename T> using vec = std::vector<T>;
template <typename T> using vvec = vec<vec<T>>;
template <typename T> const T &var_min(const T &t) { return t; }
template <typename T> const T &var_max(const T &t) { return t; }
template <typename T, typename... Tail>
const T &var_min(const T &t, const Tail &...tail) {
return std::min(t, var_min(tail...));
}
template <typename T, typename... Tail>
const T &var_max(const T &t, const Tail &...tail) {
return std::max(t, var_max(tail...));
}
template <typename T, typename... Tail> void chmin(T &t, const Tail &...tail) {
t = var_min(t, tail...);
}
template <typename T, typename... Tail> void chmax(T &t, const Tail &...tail) {
t = var_max(t, tail...);
}
template <typename T> T make_v(T init) { return init; }
template <typename T, typename... Tail>
auto make_v(T init, std::size_t s, Tail... tail) {
auto v = std::move(make_v(init, tail...));
return vec<decltype(v)>(s, v);
}
template <typename T, std::size_t Head, std::size_t... Tail>
struct multi_dem_array {
using type = std::array<typename multi_dem_array<T, Tail...>::type, Head>;
};
template <typename T, std::size_t Head> struct multi_dem_array<T, Head> {
using type = std::array<T, Head>;
};
template <typename T, std::size_t... Args>
using mdarray = typename multi_dem_array<T, Args...>::type;
namespace init__ {
struct InitIO {
InitIO() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
std::cout << std::fixed << std::setprecision(30);
}
} init_io;
} // namespace init__
namespace graph {
using Node = ll;
using Weight = ll;
using Edge = std::pair<Node, Weight>;
template <bool Directed> struct Graph : public vvec<Edge> {
using vvec<Edge>::vvec;
void add_edge(Node f, Node t, Weight w = 1) {
(*this)[f].emplace_back(t, w);
if (!Directed)
(*this)[t].emplace_back(f, w);
}
Graph<Directed> build_inv() const {
Graph<Directed> ret(this->size());
for (Node i = 0; i < this->size(); i++) {
for (const Edge &e : (*this)[i]) {
Node j;
Weight w;
std::tie(j, w) = e;
if (!Directed && j < i)
continue;
ret.add_edge(j, i, w);
}
}
return ret;
}
};
template <typename Iterator> class dst_iterator {
Iterator ite;
public:
dst_iterator(Iterator ite) : ite(ite) {}
bool operator==(const dst_iterator<Iterator> &oth) const {
return ite == oth.ite;
}
bool operator!=(const dst_iterator<Iterator> &oth) const {
return !(*this == oth);
}
bool operator<(const dst_iterator<Iterator> &oth) const {
return ite < oth.ite;
}
bool operator>(const dst_iterator<Iterator> &oth) const {
return ite > oth.ite;
}
bool operator<=(const dst_iterator<Iterator> &oth) const {
return ite <= oth.ite;
}
bool operator>=(const dst_iterator<Iterator> &oth) const {
return ite >= oth.ite;
}
const Node &operator*() { return ite->first; }
const Node &operator*() const { return ite->first; }
dst_iterator operator++() {
++ite;
return ite;
}
};
class dst_iteration {
using ite_type = vec<Edge>::const_iterator;
const vec<Edge> &edges;
public:
dst_iteration(const vec<Edge> &edges) : edges(edges) {}
auto begin() const { return dst_iterator<ite_type>(edges.cbegin()); }
auto end() const { return dst_iterator<ite_type>(edges.cend()); }
};
dst_iteration dst(const vec<Edge> &edges) { return dst_iteration(edges); }
} // namespace graph
const ll inf = 5e15;
void dfs(ll cur, ll pre, const graph::Graph<false> &tree, vec<ll> &col,
vec<ll> &ans, const vec<ll> &av) {
ll a = av[cur];
auto ite = std::lower_bound(ALL(col), a);
ll idx = -1, pval;
if (*ite != a) {
idx = std::distance(col.begin(), ite);
pval = *ite;
col[idx] = a;
}
ans[cur] = std::distance(col.begin(), std::lower_bound(ALL(col), inf));
for (ll nxt : graph::dst(tree[cur]))
if (nxt != pre)
dfs(nxt, cur, tree, col, ans, av);
col[idx] = pval;
}
int main() {
ll n;
std::cin >> n;
vec<ll> av(n);
for (ll &e : av)
std::cin >> e;
graph::Graph<false> tree(n);
for (ll i = 1; i < n; i++) {
ll u, v;
std::cin >> u >> v;
tree.add_edge(u - 1, v - 1);
}
vec<ll> col(n + 10, inf);
vec<ll> ans(n);
dfs(0, -1, tree, col, ans, av);
for (ll e : ans)
std::cout << e << "\n";
return 0;
}
| #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstddef>
#include <deque>
#include <functional>
#include <initializer_list>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <variant>
#include <vector>
#define endl codeforces
#define ALL(v) std::begin(v), std::end(v)
#define ALLR(v) std::rbegin(v), std::rend(v)
using ll = std::int64_t;
using ull = std::uint64_t;
using pii = std::pair<int, int>;
using tii = std::tuple<int, int, int>;
using pll = std::pair<ll, ll>;
using tll = std::tuple<ll, ll, ll>;
template <typename T> using vec = std::vector<T>;
template <typename T> using vvec = vec<vec<T>>;
template <typename T> const T &var_min(const T &t) { return t; }
template <typename T> const T &var_max(const T &t) { return t; }
template <typename T, typename... Tail>
const T &var_min(const T &t, const Tail &...tail) {
return std::min(t, var_min(tail...));
}
template <typename T, typename... Tail>
const T &var_max(const T &t, const Tail &...tail) {
return std::max(t, var_max(tail...));
}
template <typename T, typename... Tail> void chmin(T &t, const Tail &...tail) {
t = var_min(t, tail...);
}
template <typename T, typename... Tail> void chmax(T &t, const Tail &...tail) {
t = var_max(t, tail...);
}
template <typename T> T make_v(T init) { return init; }
template <typename T, typename... Tail>
auto make_v(T init, std::size_t s, Tail... tail) {
auto v = std::move(make_v(init, tail...));
return vec<decltype(v)>(s, v);
}
template <typename T, std::size_t Head, std::size_t... Tail>
struct multi_dem_array {
using type = std::array<typename multi_dem_array<T, Tail...>::type, Head>;
};
template <typename T, std::size_t Head> struct multi_dem_array<T, Head> {
using type = std::array<T, Head>;
};
template <typename T, std::size_t... Args>
using mdarray = typename multi_dem_array<T, Args...>::type;
namespace init__ {
struct InitIO {
InitIO() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
std::cout << std::fixed << std::setprecision(30);
}
} init_io;
} // namespace init__
namespace graph {
using Node = ll;
using Weight = ll;
using Edge = std::pair<Node, Weight>;
template <bool Directed> struct Graph : public vvec<Edge> {
using vvec<Edge>::vvec;
void add_edge(Node f, Node t, Weight w = 1) {
(*this)[f].emplace_back(t, w);
if (!Directed)
(*this)[t].emplace_back(f, w);
}
Graph<Directed> build_inv() const {
Graph<Directed> ret(this->size());
for (Node i = 0; i < this->size(); i++) {
for (const Edge &e : (*this)[i]) {
Node j;
Weight w;
std::tie(j, w) = e;
if (!Directed && j < i)
continue;
ret.add_edge(j, i, w);
}
}
return ret;
}
};
template <typename Iterator> class dst_iterator {
Iterator ite;
public:
dst_iterator(Iterator ite) : ite(ite) {}
bool operator==(const dst_iterator<Iterator> &oth) const {
return ite == oth.ite;
}
bool operator!=(const dst_iterator<Iterator> &oth) const {
return !(*this == oth);
}
bool operator<(const dst_iterator<Iterator> &oth) const {
return ite < oth.ite;
}
bool operator>(const dst_iterator<Iterator> &oth) const {
return ite > oth.ite;
}
bool operator<=(const dst_iterator<Iterator> &oth) const {
return ite <= oth.ite;
}
bool operator>=(const dst_iterator<Iterator> &oth) const {
return ite >= oth.ite;
}
const Node &operator*() { return ite->first; }
const Node &operator*() const { return ite->first; }
dst_iterator operator++() {
++ite;
return ite;
}
};
class dst_iteration {
using ite_type = vec<Edge>::const_iterator;
const vec<Edge> &edges;
public:
dst_iteration(const vec<Edge> &edges) : edges(edges) {}
auto begin() const { return dst_iterator<ite_type>(edges.cbegin()); }
auto end() const { return dst_iterator<ite_type>(edges.cend()); }
};
dst_iteration dst(const vec<Edge> &edges) { return dst_iteration(edges); }
} // namespace graph
const ll inf = 5e15;
void dfs(ll cur, ll pre, const graph::Graph<false> &tree, vec<ll> &col,
vec<ll> &ans, const vec<ll> &av) {
ll a = av[cur];
auto ite = std::lower_bound(ALL(col), a);
ll idx = -1, pval;
if (*ite != a) {
idx = std::distance(col.begin(), ite);
pval = *ite;
col[idx] = a;
}
ans[cur] = std::distance(col.begin(), std::lower_bound(ALL(col), inf));
for (ll nxt : graph::dst(tree[cur]))
if (nxt != pre)
dfs(nxt, cur, tree, col, ans, av);
if (idx != -1)
col[idx] = pval;
}
int main() {
ll n;
std::cin >> n;
vec<ll> av(n);
for (ll &e : av)
std::cin >> e;
graph::Graph<false> tree(n);
for (ll i = 1; i < n; i++) {
ll u, v;
std::cin >> u >> v;
tree.add_edge(u - 1, v - 1);
}
vec<ll> col(n + 10, inf);
vec<ll> ans(n);
dfs(0, -1, tree, col, ans, av);
for (ll e : ans)
std::cout << e << "\n";
return 0;
}
| replace | 186 | 187 | 186 | 188 | 0 | |
p02698 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
// #define int long long
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i < n; ++i)
#define exrep(i, a, b) for (ll i = a; i < b; i++)
#define out(x) cout << x << endl
#define EPS (1e-7)
#define gearup \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
typedef long long int ll;
typedef long double ld;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<pair<int, int>> vpii;
typedef vector<vector<int>> vvi;
typedef vector<vector<char>> vvc;
typedef vector<vector<bool>> vvb;
typedef vector<vector<double>> vvd;
typedef vector<vector<string>> vvs;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<vector<vector<ll>>> vvvl;
ll MOD = 1000000007;
const long long L_INF = 1LL << 60;
const int INF = 2147483647; // 2^31-1
const double PI = acos(-1);
// cout<<fixed<<setprecision(10);
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 <class T> void debug(T v) {
rep(i, v.size()) cout << v[i] << " ";
cout << endl;
}
const ll dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int n;
int a[100001], dp[100001];
vvl g(100001);
vl ans(100001);
void dfs(int node, int pre_node) {
ans[node] = lower_bound(dp, dp + n, INF) - dp;
for (auto nv : g[node]) {
if (nv == pre_node)
continue;
// LIS
auto itr = lower_bound(dp, dp + n, a[nv]);
int pre_num = *itr;
*itr = a[nv];
dfs(nv, node);
*itr = pre_num;
}
}
signed main() {
cin >> n;
rep(i, n) cin >> a[i];
rep(i, n) dp[i] = INF;
rep(i, n - 1) {
int u, v;
cin >> u >> v;
u--;
v--;
g[u].push_back(v);
g[v].push_back(u);
}
dp[0] = a[0];
dfs(0, -1);
rep(i, n) out(ans[i]);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
// #define int long long
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i < n; ++i)
#define exrep(i, a, b) for (ll i = a; i < b; i++)
#define out(x) cout << x << endl
#define EPS (1e-7)
#define gearup \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
typedef long long int ll;
typedef long double ld;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<pair<int, int>> vpii;
typedef vector<vector<int>> vvi;
typedef vector<vector<char>> vvc;
typedef vector<vector<bool>> vvb;
typedef vector<vector<double>> vvd;
typedef vector<vector<string>> vvs;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<vector<vector<ll>>> vvvl;
ll MOD = 1000000007;
const long long L_INF = 1LL << 60;
const int INF = 2147483647; // 2^31-1
const double PI = acos(-1);
// cout<<fixed<<setprecision(10);
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 <class T> void debug(T v) {
rep(i, v.size()) cout << v[i] << " ";
cout << endl;
}
const ll dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int n;
int a[200001], dp[200001];
vvl g(200001);
vl ans(200001);
void dfs(int node, int pre_node) {
ans[node] = lower_bound(dp, dp + n, INF) - dp;
for (auto nv : g[node]) {
if (nv == pre_node)
continue;
// LIS
auto itr = lower_bound(dp, dp + n, a[nv]);
int pre_num = *itr;
*itr = a[nv];
dfs(nv, node);
*itr = pre_num;
}
}
signed main() {
cin >> n;
rep(i, n) cin >> a[i];
rep(i, n) dp[i] = INF;
rep(i, n - 1) {
int u, v;
cin >> u >> v;
u--;
v--;
g[u].push_back(v);
g[v].push_back(u);
}
dp[0] = a[0];
dfs(0, -1);
rep(i, n) out(ans[i]);
return 0;
} | replace | 61 | 64 | 61 | 64 | 0 | |
p02698 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
const int INF = 1001001001;
const int MAX_N = 20005;
vector<int> a, ans;
vector<int> to[MAX_N];
int dp[MAX_N];
void dfs(int v, int p = -1) {
int i = lower_bound(dp, dp + MAX_N, a[v]) - dp;
int old = dp[i];
dp[i] = a[v];
ans[v] = i;
if (p != -1)
ans[v] = max(ans[v], ans[p]); // exclude first turn
for (int u : to[v]) {
if (u == p)
continue; // parent
dfs(u, v);
}
// roll back
dp[i] = old;
}
int main() {
int n;
cin >> n;
a = ans = vector<int>(n);
rep(i, n) { cin >> a[i]; }
rep(i, n - 1) {
int u, v;
cin >> u >> v;
--u, --v;
to[u].push_back(v);
to[v].push_back(u);
}
rep(i, MAX_N) dp[i] = INF;
dp[0] = -INF;
dfs(0);
rep(i, n) { cout << ans[i] << endl; }
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
const int INF = 1001001001;
const int MAX_N = 200005;
vector<int> a, ans;
vector<int> to[MAX_N];
int dp[MAX_N];
void dfs(int v, int p = -1) {
int i = lower_bound(dp, dp + MAX_N, a[v]) - dp;
int old = dp[i];
dp[i] = a[v];
ans[v] = i;
if (p != -1)
ans[v] = max(ans[v], ans[p]); // exclude first turn
for (int u : to[v]) {
if (u == p)
continue; // parent
dfs(u, v);
}
// roll back
dp[i] = old;
}
int main() {
int n;
cin >> n;
a = ans = vector<int>(n);
rep(i, n) { cin >> a[i]; }
rep(i, n - 1) {
int u, v;
cin >> u >> v;
--u, --v;
to[u].push_back(v);
to[v].push_back(u);
}
rep(i, MAX_N) dp[i] = INF;
dp[0] = -INF;
dfs(0);
rep(i, n) { cout << ans[i] << endl; }
return 0;
}
| replace | 5 | 6 | 5 | 6 | 0 | |
p02698 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 1000000000
#define pb push_back
vector<ll> ans(1000001, 0);
vector<int> dp(1000001, INF);
vector<int> val(1000001);
vector<ll> adj[100001];
void dfs(ll u, ll p) {
// cout<<"U::"<<u<<"P::"<<p<<endl;
ll k = lower_bound(dp.begin(), dp.end(), val[u]) - dp.begin();
ll temp = dp[k];
dp[k] = val[u];
ans[u] = max(k, ans[p]);
for (int j = 0; j < adj[u].size(); j++) {
if (adj[u][j] == p)
continue;
dfs(adj[u][j], u);
}
dp[k] = temp;
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> val[i];
for (int i = 1; i < n; i++) {
ll u, v;
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
dp[0] = -INF;
dfs(1, 0);
for (int i = 1; i <= n; i++)
cout << ans[i] << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 1000000000
#define pb push_back
vector<ll> ans(2000001, 0);
vector<int> dp(2000001, INF);
vector<int> val(2000001);
vector<ll> adj[200001];
void dfs(ll u, ll p) {
// cout<<"U::"<<u<<"P::"<<p<<endl;
ll k = lower_bound(dp.begin(), dp.end(), val[u]) - dp.begin();
ll temp = dp[k];
dp[k] = val[u];
ans[u] = max(k, ans[p]);
for (int j = 0; j < adj[u].size(); j++) {
if (adj[u][j] == p)
continue;
dfs(adj[u][j], u);
}
dp[k] = temp;
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> val[i];
for (int i = 1; i < n; i++) {
ll u, v;
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
dp[0] = -INF;
dfs(1, 0);
for (int i = 1; i <= n; i++)
cout << ans[i] << endl;
}
| replace | 5 | 9 | 5 | 9 | 0 | |
p02698 | C++ | Runtime Error | // https://www.codechef.com/viewsolution/30791639
#include <bits/stdc++.h>
#define FAST \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
typedef long long ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define mod 1000000007
#define inf 1000000000000000
#define bpc(x) __builtin_popcountll(x)
#define autoit(x, it) for (auto it = x.begin(); it != x.end(); it++)
#define rep(n) for (ll i = 0; i < n; i++)
#define repi(i, n) for (ll i = 0; i < n; i++)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set \
tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
using namespace std;
#define N 2000
ll val[N];
ll fans[N];
ll que(ll st[], ll l, ll r, ll l1, ll r1, ll i) {
if (l > r1 || l1 > r)
return 0;
if (l >= l1 && r <= r1)
return st[i];
ll mid = (l + r) / 2;
return max(que(st, l, mid, l1, r1, 2 * i + 1),
que(st, mid + 1, r, l1, r1, 2 * i + 2));
}
void upd(ll st[], ll l, ll r, ll l1, ll i, ll val) {
if (l > l1 || r < l1)
return;
if (l == r) {
st[i] = val;
return;
}
ll mid = (l + r) / 2;
upd(st, l, mid, l1, 2 * i + 1, val);
upd(st, mid + 1, r, l1, 2 * i + 2, val);
st[i] = max(st[2 * i + 1], st[2 * i + 2]);
}
void dfs(ll curr, vector<ll> v[], ll st[], ll par, ll lim) {
ll prev = que(st, 0, lim, val[curr], val[curr], 0);
ll now = que(st, 0, lim, 0, val[curr] - 1, 0) + 1;
fans[curr] = max(st[0], now);
if (now > prev) {
upd(st, 0, lim, val[curr], 0, now);
autoit(v[curr], it) {
if (*it == par)
continue;
dfs(*it, v, st, curr, lim);
}
upd(st, 0, lim, val[curr], 0, prev);
}
else {
autoit(v[curr], it) {
if (*it == par)
continue;
dfs(*it, v, st, curr, lim);
}
}
}
int main() {
FAST /**/
ll n;
cin >> n;
rep(n) cin >> val[i];
map<ll, ll> comp;
rep(n) comp[val[i]] = 1;
ll curr = 0;
autoit(comp, it) it->ss = curr++;
rep(n) val[i] = comp[val[i]];
vector<ll> v[n];
rep(n - 1) {
ll a, b;
cin >> a >> b;
a--, b--;
v[a].pb(b);
v[b].pb(a);
}
ll lim = (comp.size());
ll ht = ceil(log2(lim));
// cout<<"ht = "<<ht<<"\n";
ht = (1ll << (ht + 1)) - 1;
ll st[ht];
memset(st, 0, sizeof(st));
dfs(0, v, st, -1, lim - 1);
rep(n) cout << fans[i] << "\n";
return 0;
}
| // https://www.codechef.com/viewsolution/30791639
#include <bits/stdc++.h>
#define FAST \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
typedef long long ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define mod 1000000007
#define inf 1000000000000000
#define bpc(x) __builtin_popcountll(x)
#define autoit(x, it) for (auto it = x.begin(); it != x.end(); it++)
#define rep(n) for (ll i = 0; i < n; i++)
#define repi(i, n) for (ll i = 0; i < n; i++)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set \
tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
using namespace std;
#define N 200005
ll val[N];
ll fans[N];
ll que(ll st[], ll l, ll r, ll l1, ll r1, ll i) {
if (l > r1 || l1 > r)
return 0;
if (l >= l1 && r <= r1)
return st[i];
ll mid = (l + r) / 2;
return max(que(st, l, mid, l1, r1, 2 * i + 1),
que(st, mid + 1, r, l1, r1, 2 * i + 2));
}
void upd(ll st[], ll l, ll r, ll l1, ll i, ll val) {
if (l > l1 || r < l1)
return;
if (l == r) {
st[i] = val;
return;
}
ll mid = (l + r) / 2;
upd(st, l, mid, l1, 2 * i + 1, val);
upd(st, mid + 1, r, l1, 2 * i + 2, val);
st[i] = max(st[2 * i + 1], st[2 * i + 2]);
}
void dfs(ll curr, vector<ll> v[], ll st[], ll par, ll lim) {
ll prev = que(st, 0, lim, val[curr], val[curr], 0);
ll now = que(st, 0, lim, 0, val[curr] - 1, 0) + 1;
fans[curr] = max(st[0], now);
if (now > prev) {
upd(st, 0, lim, val[curr], 0, now);
autoit(v[curr], it) {
if (*it == par)
continue;
dfs(*it, v, st, curr, lim);
}
upd(st, 0, lim, val[curr], 0, prev);
}
else {
autoit(v[curr], it) {
if (*it == par)
continue;
dfs(*it, v, st, curr, lim);
}
}
}
int main() {
FAST /**/
ll n;
cin >> n;
rep(n) cin >> val[i];
map<ll, ll> comp;
rep(n) comp[val[i]] = 1;
ll curr = 0;
autoit(comp, it) it->ss = curr++;
rep(n) val[i] = comp[val[i]];
vector<ll> v[n];
rep(n - 1) {
ll a, b;
cin >> a >> b;
a--, b--;
v[a].pb(b);
v[b].pb(a);
}
ll lim = (comp.size());
ll ht = ceil(log2(lim));
// cout<<"ht = "<<ht<<"\n";
ht = (1ll << (ht + 1)) - 1;
ll st[ht];
memset(st, 0, sizeof(st));
dfs(0, v, st, -1, lim - 1);
rep(n) cout << fans[i] << "\n";
return 0;
}
| replace | 29 | 30 | 29 | 30 | 0 | |
p02698 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bits/stdc++.h>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef long double ld;
using edge = struct {
ll to;
ll cost;
};
struct point {
ll x;
ll y;
bool operator<(const point &p) const {
if (x == p.x)
return y < p.y;
return x < p.x;
}
};
struct undirected_edge {
ll from;
ll to;
ll cost;
bool operator<(const undirected_edge &ue) const { return cost < ue.cost; }
};
typedef string str;
typedef std::pair<ll, ll> pl;
typedef std::pair<ll, pl> pl3;
typedef std::map<string, ll> msl;
typedef std::map<char, ll> mcl;
typedef std::map<ll, ll> mll;
typedef std::vector<ll> vl;
typedef std::vector<pl> vpl;
typedef std::vector<point> points;
typedef std::vector<pl3> vpl3;
// priority queue. Taking from the higher value. Don't forget calling !q.empty()
typedef std::priority_queue<ll> pq;
// priority queue. Taking from the lower value
typedef std::priority_queue<ll, vl, greater<ll>> pql;
typedef std::vector<edge> Graph;
const ll N_DIGITS = 60;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll INF = MOD * MOD;
const long double EPS = 1e-9;
const long double PI = 3.14159265358979323846;
points dirs = {
{-1, 0}, {1, 0}, {0, 1}, {0, -1}, // four directions
{1, 1}, {-1, 1}, {-1, -1}, {1, -1}, // diagonal
{0, 0} // self
};
template <typename A, typename B> string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string &s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(vector<bool> v) {
bool first = true;
string res = "{";
for (int i = 0; i < static_cast<int>(v.size()); i++) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(v[i]);
}
res += "}";
return res;
}
template <size_t N> string to_string(bitset<N> v) {
string res = "";
for (size_t i = 0; i < N; i++) {
res += static_cast<char>('0' + v[i]);
}
return res;
}
template <typename A> string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
template <typename A, typename B> string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
#define rep(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define revrep(i, n) for (ll(i) = n - 1; (i) >= 0; (i)--)
#define For(i, a, b) for (ll(i) = (a); (i) < (b); (i)++)
#define revFor(i, b, a) for (ll(i) = (b)-1; (i) >= (a); (i)--)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define isUpper(c) ('a' - c > 0)
#define isNum(c) (0 <= (c) - '0' && (c) - '0' <= 9)
#define toLower(c) char((c) + 0x20)
#define toUpper(c) char((c)-0x20)
#define pb push_back
#define mp make_pair
#define pr(a) cout << (a)
#define prl(a) cout << (a) << endl
#define prl2(a, b) cout << (a) << " " << (b) << endl
#define prl3(a, b, c) cout << (a) << " " << (b) << " " << (c) << endl
#define prl4(a, b, c, d) \
cout << (a) << " " << (b) << " " << (c) << " " << (d) << endl
#define prs(a) cout << (a) << " "
#define prs2(a, b) cout << (a) << " " << (b) << " "
#define prs3(a, b, c) cout << (a) << " " << (b) << " " << (c) << " "
#define prs4(a, b, c, d) \
cout << (a) << " " << (b) << " " << (c) << " " << (d) << " "
#define yn(condition) \
if ((condition)) \
prl("Yes"); \
else \
prl("No");
#define YN(condition) \
if ((condition)) \
prl("YES"); \
else \
prl("NO");
#define in1(a) cin >> (a)
#define in2(a, b) cin >> (a) >> (b)
#define in3(a, b, c) cin >> (a) >> (b) >> (c)
#define in4(a, b, c, d) cin >> (a) >> (b) >> (c) >> (d)
#define e1 first
#define e2 second
#define ctol(c) ll((c)) - ll('0')
#define ltos(n) to_string((n))
#define items(kv, v) for (auto &(kv) : (v))
#define ndig(N, n) ctol(ll(ltos((N))[ll(ltos((N)).length()) - (n)]))
#define rsort(a, n) sort(a, a + n, greater<>())
#define Forchar(c, a, z) for (char(c) = (a); (c) <= (z); (c)++)
#define cntchar(s, c) count(all((s)), c)
#define substring(s, start, end) s.substr((start), (end) - (start) + 1)
#define prl_nd(num, digits) \
cout << fixed << setprecision(digits) << (num) << endl;
#define XOR(a, b) (a) ^ (b)
#define prl_time(s) \
prl3("Elapsed Time:", 1000.0 * (clock() - s) / CLOCKS_PER_SEC, "[ms]");
#define char_to_str(c) string(1, (c))
#define DEBUG(x) cerr << #x << ": " << (x) << endl
#define DEBUG_VEC(v) \
cerr << #v << ": "; \
rep(__i, (v).size()) cerr << ((v)[__i]) << ", "; \
cerr << endl
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
struct MaxFlow {
struct F_edge {
ll to, rev, capacity;
F_edge(ll to, ll rev, ll capacity) : to(to), rev(rev), capacity(capacity) {}
};
typedef vector<F_edge> F_edges;
vector<F_edges> graph;
ll n_vertex;
// level is the shortest path to get a given node from the source node.
vl level, iter;
MaxFlow(ll n_vertex) : n_vertex(n_vertex) { graph.resize(n_vertex); }
void add_edge(ll from, ll to, ll capacity) {
graph[from].pb({to, ll(graph[to].size()), capacity});
graph[to].pb({from, ll(graph[from].size()) - 1, 0});
}
void bfs(ll source) {
level = vl(n_vertex, -1);
level[source] = 0;
queue<ll> q;
q.push(source);
while (!q.empty()) {
ll vertex = q.front();
q.pop();
rep(i, graph[vertex].size()) {
ll target = graph[vertex][i].to;
ll cap_target = graph[vertex][i].capacity;
// if the flow can be into the target node, implement below.
if (cap_target > 0 && level[target] < 0) {
level[target] = level[vertex] + 1;
q.push(target);
}
}
}
}
ll dfs(ll vertex, ll sink, ll flow) {
if (vertex == sink)
return flow;
for (ll &i = iter[vertex]; i < graph[vertex].size(); i++) {
ll target = graph[vertex][i].to;
ll cap_target = graph[vertex][i].capacity;
ll rev_target = graph[vertex][i].rev;
// if capasitiy is not full yet and target is farther,
// then assign current flow
if (cap_target > 0 && level[vertex] < level[target]) {
ll d = dfs(target, sink, min(cap_target, flow));
if (d > 0) { // if the flow successfully reaches the sink, reduce the
// flow from the capacity
graph[vertex][i].capacity -= d;
graph[target][rev_target].capacity += d;
return d;
}
}
}
return 0;
}
ll dinic(ll source, ll sink) {
ll flow = 0;
while (true) {
bfs(source);
// if there is no path leading to the sink, the maximum flow is 0.
if (level[sink] < 0)
return flow;
iter = vl(n_vertex, 0);
ll f;
while ((f = dfs(source, sink, INF)) > 0)
flow += f;
}
}
};
class UnionFind {
vl parents, set_size;
ll n_groups;
public:
UnionFind() {}
UnionFind(ll n) {
parents = set_size = vl(n);
n_groups = n;
rep(i, n) {
parents[i] = i;
set_size[i] = 1LL;
}
}
ll root_find(ll x) {
if (parents[x] == x)
return x;
return parents[x] = root_find(parents[x]);
}
void unite(ll x, ll y) {
// priority for x is larger than that of y
x = root_find(x);
y = root_find(y);
if (x == y)
return;
parents[y] = x, set_size[x] += set_size[y];
n_groups--;
}
bool is_same(ll x, ll y) { // connected or not
return root_find(x) == root_find(y);
}
ll size(ll x) { return set_size[root_find(x)]; }
ll union_count() const { return n_groups; }
};
struct Doubling { // ABC167D
ll n;
ll sz;
vector<vl> next;
/*
next[k + 1][i] := next[k][next[k][i]]
next[0][i] := edge[i]
e.g. a0, a1, ..., an-1 / 0 <= ai <= n - 1
a0 -> a[a0] -> a[a[a0]] -> ... -> a[a[...[a[0]]]] (m times)
Let the function repeatedly input a[i] m times be f[m](a[i])
- get(i, x) returns f[x](a[i])
- lower_bound(i, j) returns minimum x which satisfies f[x](a[i]) >= j.
if not possible returns n.
*/
// edge[i]: the step size for one iteration
Doubling(vl &edge) : n(edge.size()), sz(62) {
next.resize(sz, vl(n, -1));
rep(i, n) next[0][i] = edge[i];
rep(k, sz - 1) rep(i, n) next[k + 1][i] = next[k][next[k][i]];
}
ll get(ll i, ll x) {
ll ret = i;
rep(bit, sz) {
if (!(x >> bit & 1))
continue;
ret = next[bit][ret];
}
return ret;
}
ll lower_bound(ll i, ll j) {
ll cur = i, acc = 0;
revrep(wid, sz) {
if (next[wid][cur] < j) {
acc += 1LL << wid;
cur = next[wid][cur];
}
}
return min(n, acc + 1);
}
};
class LowestCommonAncestor {
public:
ll N, logN;
vl depth, len;
vector<Graph> tree;
vector<vl> parents;
LowestCommonAncestor(ll n, ll offset = -1, bool is_weighted = true) {
N = n;
logN = 0;
while (N > (1LL << logN))
logN++;
depth = vl(N);
len = vl(N);
parents = vector<vl>(logN, vl(N));
tree = vector<Graph>(N);
input(N, offset, is_weighted);
init(0, -1, 0, 0);
build();
}
void add_edge(ll from, ll to, ll dist) {
tree[from].pb({to, dist});
tree[to].pb({from, dist});
}
void input(ll n, ll offset = -1, bool is_weighted = true) {
rep(i, n - 1) {
ll a, b, d = 1;
in2(a, b);
a += offset, b += offset;
if (is_weighted)
in1(d);
add_edge(a, b, d);
}
}
void init(ll source, ll parent, ll d, ll l) {
depth[source] = d;
parents[0][source] = parent;
len[source] = l;
rep(i, tree[source].size()) {
ll target = tree[source][i].to;
ll cost = tree[source][i].cost;
if (target == parent)
continue;
init(target, source, d + 1, cost + l);
}
}
void build() {
rep(k, logN - 1) rep(n, N) {
// if there is no parent, -1.
// otherwise, the parent of the parent is the parent.
if (parents[k][n] < 0)
parents[k + 1][n] = -1;
else
parents[k + 1][n] = parents[k][parents[k][n]];
}
}
ll query(ll u, ll v) {
if (depth[u] > depth[v])
swap(u, v);
rep(k, logN) if ((depth[v] - depth[u]) >> k & 1) v = parents[k][v];
if (u == v)
return u;
revrep(k, logN) {
if (parents[k][u] != parents[k][v]) {
u = parents[k][u];
v = parents[k][v];
}
}
return parents[0][u];
}
ll distance(ll u, ll v) {
ll w = query(u, v);
return len[u] + len[v] - 2 * len[w];
}
};
struct BIT {
ll n;
vl dat;
BIT(ll n, ll ini = 0) : dat(n + 1, ini), n(n){};
// x: 1001 1010 1100 1011 1101 1111
// x & - x: 0001 0010 0100 0001 0001 0001
// ->: 1010 1100 10000 1100 1100 10000
ll update_func(ll val, ll d) {
// if maximum -> max(val, dat)
// return max(val, dat);
// if cumulative sum
return val + d;
}
ll query(ll i) {
/*
v[0] + v[1] + ... + v[i]
e.g.) i = 10101
itr1. 10101 -> 10100
itr2. 10100 -> 10000
itr3. 10000 -> 00000 (break)
*/
ll ret = 0;
for (ll j = i; j >= 0; j = (j & (j + 1)) - 1) {
ret = update_func(ret, dat[j]);
}
return ret;
}
ll query(ll l, ll r) { return query(r) - query(l); }
ll lower_bound(ll key) {
// v[0] + v[1] + ... + v[left - 1] < key <= v[0] + v[1] + ... + v[left]
if (key <= 0)
return 0;
ll left = 0, right = 1;
while (right <= n)
right *= 2;
for (ll i = right; i > 0; i /= 2) {
if (left + i <= n && dat[left + i - 1] < key) {
key -= dat[left + i - 1];
left += i;
}
}
return left;
}
void update(ll i, ll val) {
/*
e.g.) i = 10101, n = 11111
itr1. i: 10101, i+1: 10110 -> 10111
itr2. i: 10111, i+1: 11000 -> 11111 (break)
*/
if (i < 0)
return;
for (ll j = i; j < n; j |= j + 1) {
dat[j] = update_func(val, dat[j]);
}
}
};
struct SegmentTree {
ll n, ini, minimize;
vl dat;
// when seeking minimum
// ini = INF
// when seeking maximum
// ini = -INF
SegmentTree(ll n_, bool minimize_ = true) {
n = 1;
minimize = minimize_;
if (minimize)
ini = INF;
else
ini = -INF;
while (n < n_)
n *= 2;
dat.resize(2 * n - 1);
rep(i, 2 * n - 1) dat[i] = ini;
};
void update(ll idx, ll val) {
idx += n - 1;
if (minimize && dat[idx] <= val)
return;
if (!minimize && dat[idx] >= val)
return;
dat[idx] = val;
while (idx > 0) {
idx = (idx - 1) / 2;
// when seeking minimum
if (minimize)
dat[idx] = min(dat[idx * 2 + 1], dat[idx * 2 + 2]);
// when seeking maximum
else
dat[idx] = max(dat[idx * 2 + 1], dat[idx * 2 + 2]);
}
}
ll query(ll l, ll r) {
// ### NOTE ###
// the range is [l, r]
// l, l + 1, ..., r
r++; // to adjust to this method
return query_segment(l, r, 0, 0, n);
}
ll query_segment(ll a, ll b, ll idx, ll l, ll r) {
assert(a < b);
if (r <= a || b <= l)
return ini;
if (a <= l && r <= b)
return dat[idx];
else {
ll seg1 = query_segment(a, b, idx * 2 + 1, l, (l + r) / 2);
ll seg2 = query_segment(a, b, idx * 2 + 2, (l + r) / 2, r);
// when seeking minimum
if (minimize)
return min(seg1, seg2);
// when seeking maximum
else
return max(seg1, seg2);
}
}
};
template <class Target> class RerootingTreeDP {
public:
using T = typename Target::type;
struct DP_edge {
ll to, rev; // rev is the index to trace the source node.
T value; // objective value
};
private:
ll n;
void dfs_fwd(ll source, ll parent) {
ll par_idx = -1;
vector<T> values;
rep(i, tree[source].size()) {
const DP_edge &e = tree[source][i];
if (e.to == parent) {
par_idx = i;
continue;
}
dfs_fwd(e.to, source);
values.pb(e.value);
}
// If the parent != -1, update the value on edge from parent to source
if (par_idx != -1) {
ll src_idx = tree[source][par_idx].rev;
// update values on the edge from parent to source
tree[parent][src_idx].value = Target::merge(values);
}
}
void dfs_bwd(ll source, ll parent) {
vector<T> values;
for (auto &&e : tree[source])
values.pb(e.value);
values = Target::evaluate(values);
rep(i, tree[source].size()) {
const DP_edge &e = tree[source][i];
if (e.to == parent)
continue;
// tree[e.to][e.rev]: e.to -> source
tree[e.to][e.rev].value = values[i];
dfs_bwd(e.to, source);
}
}
public:
UnionFind uf;
vector<vector<DP_edge>> tree;
RerootingTreeDP(ll n) : n(n), uf(n), tree(n) {}
void add_edge(ll u, ll v, T val) {
assert(!uf.is_same(u, v));
tree[u].pb({v, ll(tree[v].size()), val});
tree[v].pb({u, ll(tree[u].size()) - 1, val});
uf.unite(u, v);
}
void dp() {
vector<bool> visited(n, false);
rep(i, n) {
if (visited[uf.root_find(i)])
continue;
dfs_fwd(i, -1);
visited[uf.root_find(i)] = true;
}
visited.assign(n, false);
rep(i, n) {
if (visited[uf.root_find(i)])
continue;
dfs_bwd(i, -1);
visited[uf.root_find(i)] = true;
}
}
ll size() const { return tree.size(); }
};
// ABC160F is one example
// Modify the functions evaluate and merge based on given problems
struct Merger {
using type = ll;
static type merge(const vector<type> &value) {
// merge the result below the source node
// each value is from each child node of the source node
// value[i] := f(child i)
// Here, we would like to obtain f(source) using f(child i) (i = 0, 1, ...,
// n_children)
ll ret = 1;
for (auto &&v : value)
ret += v;
return ret;
}
static vector<type> evaluate(const vector<type> &value) {
// value[i] := f(source -> child i)
// we would like to obtain f(child i -> source)
// child j (j != i) is the grandchildren of child i
// represent f(child i -> source) using f(source -> j) (j != i)
// L[i + 1] := the result using f(source -> k) (k = 0, 1, ..., i)
// R[i] := the result using f(source -> k) (k = i, i + 1, ..., n_children)
const ll n_children = value.size();
vl L(n_children + 1, 0), R(n_children + 1, 0);
rep(i, n_children) L[i + 1] = L[i] + value[i];
revrep(i, n_children) R[i] = R[i + 1] + value[i];
vl ret(n_children);
rep(i, n_children) ret[i] = L[i] + R[i + 1] + 1;
return ret;
}
};
struct StronglyConnectedComponents {
ll n;
vector<vl> graph, graph_rev, dag, cmp;
vl order, visited, cmp_idx;
StronglyConnectedComponents() {}
StronglyConnectedComponents(ll sz)
: n(sz), graph(sz), graph_rev(sz), visited(sz), cmp_idx(sz) {}
void add_edge(ll from, ll to) {
graph[from].pb(to);
graph_rev[to].pb(from);
}
void input(ll m, ll offset = -1) {
ll a, b;
rep(i, m) {
in2(a, b);
add_edge(a + offset, b + offset);
}
}
ll operator[](ll k) { return cmp_idx[k]; }
void dfs_fwd(ll source) {
visited[source] = 1;
rep(i, graph[source].size()) {
ll target = graph[source][i];
if (!visited[target])
dfs_fwd(target);
}
order.pb(source);
}
void dfs_bwd(ll source, ll num) {
visited[source] = 1, cmp_idx[source] = num;
cmp[num].pb(source);
rep(i, graph_rev[source].size()) {
ll target = graph_rev[source][i];
if (!visited[target])
dfs_bwd(target, num);
}
}
ll build() {
fill(all(visited), 0);
order.clear();
rep(i, n) if (!visited[i]) dfs_fwd(i);
fill(all(visited), 0);
ll num = 0;
revrep(i, order.size()) {
if (!visited[order[i]]) {
dag.pb(vl());
cmp.pb(vl());
dfs_bwd(order[i], num++);
}
}
rep(i, n) for (ll to : graph[i]) if (cmp_idx[i] != cmp_idx[to])
dag[cmp_idx[i]]
.pb(cmp_idx[to]);
rep(i, num) {
sort(all(dag[i]));
dag[i].erase(unique(all(dag[i])), dag[i].end());
}
return num;
}
};
struct CombinationMemo {
ll sz, mod;
vl facts, facts_inv, minv;
CombinationMemo(ll sz, ll _mod) : sz(sz), mod(_mod) {
facts.resize(sz + 5);
facts_inv.resize(sz + 5);
minv.resize(sz + 5);
init();
}
void init() {
facts[0] = facts[1] = 1;
minv[1] = 1;
facts_inv[0] = facts_inv[1] = 1;
For(i, 2, sz + 3) {
facts[i] = (i * facts[i - 1]) % mod;
minv[i] = mod - minv[mod % i] * (mod / i) % mod;
facts_inv[i] = facts_inv[i - 1] * minv[i] % mod;
}
}
ll nCk(ll n, ll r) {
if (n == r && n == 0)
return 1;
else if (n <= 0 || r < 0 || r > n)
return 0;
ll val = (facts[n] * facts_inv[n - r]) % mod;
val *= facts_inv[r];
return val % mod;
}
};
ll gcd(ll m, ll n) {
ll a = max(m, n);
ll b = min(m, n);
while (b != 1 && b != 0) {
a %= b;
swap(a, b);
}
return b == 1 ? 1 : a;
}
ll lcm(ll m, ll n) { return m / gcd(m, n) * n; }
ll power_mod(ll a, ll power, ll mod) {
ll value = 1;
while (power != 0) {
if (power & 1)
value = (value * a) % mod;
a = (a * a) % mod;
power = power >> 1;
}
return value % mod;
}
ll modinv(ll a, ll mod) { return power_mod(a, mod - 2, mod); }
ll power_normal(ll a, ll power) {
ll value = 1;
while (power != 0) {
if (power & 1)
value = value * a;
a = a * a;
power = power >> 1;
}
return value;
}
vector<vl> pascal_triangle(ll n) {
/*
Complexity: O(n^2)
The upper bound of n is nearly 50.
Parameters
----------
n; the size of returned vector
Returns
-------
comb[i][j]: combination(i, j). 0 <= i <= n, 0 <= j <= i
*/
vector<vl> comb(n, vl(n));
comb[0][0] = 1;
For(i, 1, n + 1) rep(j, i + 1) {
comb[i][j] += comb[i - 1][j];
if (j > 0)
comb[i][j] += comb[i - 1][j - 1];
}
return comb;
}
ll combination(ll n, ll r, ll mod) {
if (n == r && n == 0)
return 1;
else if (n <= 0 || r < 0 || r > n)
return 0;
ll numerator = 1;
ll denomenator = 1;
for (ll i = 0; i < r; i++) {
ll num = (n - i) % mod, den = (i + 1) % mod;
(numerator *= num) %= mod;
(denomenator *= modinv(den, mod)) %= mod;
}
return (numerator * denomenator) % mod;
}
ld log_combination(ll n, ll r) {
if (n == r && n == 0)
return 0;
else if (n <= 0 || r < 0 || r > n)
return -INF;
ld val = 0;
for (ll i = 0; i < r; i++) {
val += log(n - i);
val -= log(i + 1);
}
return val;
}
ll bin_search(ll key, ll A[], ll left, ll right) {
// return the index idx where A[idx] = key.
// A[left] is start and A[right] is end..
// In other words, A[right], not A[right - 1], must be defined.
while (right >= left) {
ll mid = left + (right - left) / 2;
if (A[mid] == key)
return mid;
else if (A[mid] > key)
right = mid - 1;
else if (A[mid] < key)
left = mid + 1;
}
return -1;
}
/*
// vs[lb - 1] < key <= vs[lb]
lb = lower_bound(all(vs), key);
// vs[ub - 1] <= key < vs[lb]
ub = upper_bound(all(vs), key);
ll bin_search_temp(ll left, ll right, callable judge){
while(right > left){
// when seeking lower bound
ll mid = (right + left) / 2;
if (judge(mid)) right = mid;
else left = mid + 1;
// when seeking upper bound
ll mid = (right + left + 1) / 2;
if (judge(mid)) left = mid;
else right = mid - 1;
}
return right;
}
trinary_search
// Care the value EPS!!! Compare to the condition
ld left = 0; ld right = p;
while(abs(right - left) > EPS){
ld left2 = (2 * left + right) / 3.0;
ld right2 = (left + 2 * right) / 3.0;
ld f1 = tak_func(left2);
ld f2 = tak_func(right2);
if (f1 <= f2) right = right2;
else if (f2 <= f1) left = left2;
}
*/
str bin_expression(ll n, ll dig) {
str s = "";
rep(i, dig) {
s += ltos(n % 2);
n /= 2;
}
reverse(all(s));
return s;
}
ll fact(ll n) {
if (n == 0)
return 1;
return n * fact(n - 1);
}
ll fact_mod(ll n, ll mod) {
if (n == 0)
return 1;
return n * fact_mod(n - 1, mod);
}
bool is_prime(ll n) {
if (n <= 1)
return false;
for (ll i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
mll prime_factorization(ll n) {
ll i = 2;
mll table;
while (i * i <= n) {
while (n % i == 0) {
table[i]++;
n /= i;
}
i++;
}
if (n > 1)
table[n] = 1;
return table;
}
vl divisor_table(ll n) {
vl table;
ll i = 1;
while (i * i <= n) {
if (n % i == 0) {
table.pb(i);
if (i * i != n)
table.pb(n / i);
}
i++;
}
sort(all(table));
return table;
}
ll char_to_idx(char c) {
ll idx = 0;
Forchar(cc, 'a', 'z') {
if (c == cc)
return idx;
else
idx++;
}
}
ll next_combination(ll sub) {
/*
### Attention ###
if k is 0 or 1 and n is 0, it does not work well.
ll n, k; ll bit = (1 << k) - 1;
for (; bit < (1 << n); bit = next_combination(bit)){
bool ith = bit & (1 << i);
procedures...
}
sub & -sub: the binary which shares the last digit whose value is 1 in sub
sub + x : carry up the last digit
~y : the binary whose digits are 1 if y's digit is 0.
(sub & ~y) / x: reduce the same number of 0s after first 1 in x from (sub &
~y).
*/
ll x = sub & -sub, y = sub + x;
return (((sub & ~y) / x) >> 1) | y;
}
// just change the input if you want to change the target.
// If you want to check the common sequences in two strings,
// combine them. e.g. ABC150F
vl z_algorithm(str s) {
/*
Paramters
---------
str: the string of interest
Returns
-------
res[i] is the maximum number of K which satisfies
s[:K] == s[i:i + K]
for each i = 0, 1, 2, ..., n - 1.
*/
ll n = s.length();
vl res(n);
res[0] = n;
ll i1 = 1, i2 = 0;
while (i1 < n) {
/*
i1: the starting point
i2: the length of substring
*/
while (i1 + i2 < n && s[i2] == s[i1 + i2])
++i2;
res[i1] = i2;
if (i2 == 0) {
++i1;
continue;
}
ll i3 = 1;
// update the already seen points
while (i1 + i3 < n && i3 + res[i3] < i2) {
res[i1 + i3] = res[i3];
++i3;
}
// update up to i1 + i3 and the next possible minimum length is i2 - i3 (=
// res[i3])
i1 += i3, i2 -= i3;
}
return res;
}
ll big_number_mod(str s, ll mod) {
ll l = s.length();
ll idx = 0;
ll val = 0;
ll tenth = 1;
while (idx < l) {
ll m = ctol(s[l - 1 - idx]);
val += (m * tenth) % mod;
val %= mod;
tenth *= 10;
tenth %= mod;
idx++;
}
return val;
}
ll big_number_compare(str s1, str s2) {
if (s1.length() > s2.length())
return 1;
else if (s1.length() < s2.length())
return -1;
else if (s1 == s2)
return 0;
return 2 * (s1 > s2) - 1;
}
ll string_to_ll(str s) {
ll l = s.length();
ll idx = 0;
ll val = 0;
ll tenth = 1;
while (idx < l) {
ll m = ctol(s[l - 1 - idx]);
val += (m * tenth);
tenth *= 10;
idx++;
}
return val;
}
str reflected_string(str s) {
str t, u;
ll n = s.length();
t = s;
reverse(all(t));
u = substring(t, 0, n - 2) + s + substring(t, 1, n - 1);
return u;
}
ld distance_between_point_line(point l_begin, point l_end, point p) {
ll xl1 = l_begin.x, yl1 = l_begin.y;
ll xl2 = l_end.x, yl2 = l_end.y;
ll xp = p.x, yp = p.y;
ll a = yl2 - yl1;
ll b = -xl2 + xl1;
ll c = -a * xl2 - b * yl2;
return abs(ld(a * xp + b * yp + c)) / ld(sqrt(a * a + b * b));
}
bool is_cross(point l1_begin, point l1_end, point l2_begin, point l2_end) {
ll x1 = l1_begin.x, y1 = l1_begin.y;
ll x2 = l1_end.x, y2 = l1_end.y;
ll x3 = l2_begin.x, y3 = l2_begin.y;
ll x4 = l2_end.x, y4 = l2_end.y;
ll val1 = (x1 - x2) * (y3 - y1) + (y1 - y2) * (x1 - x3);
ll val2 = (x1 - x2) * (y4 - y1) + (y1 - y2) * (x1 - x4);
ll val3 = (x3 - x4) * (y1 - y3) + (y3 - y4) * (x3 - x1);
ll val4 = (x3 - x4) * (y2 - y3) + (y3 - y4) * (x3 - x2);
return val1 * val2 < 0 && val3 * val4 < 0;
}
ld space_of_triangle(point p1, point p2, point p3) {
ll x1 = p1.x, y1 = p1.y;
ll x2 = p2.x, y2 = p2.y;
ll x3 = p3.x, y3 = p3.y;
ll v1 = x2 - x1;
ll u1 = y2 - y1;
ll v2 = x3 - x1;
ll u2 = y3 - y1;
ld s = ld(v1 * u2 - u1 * v2) / ld(2);
return abs(s);
}
pair<point, ll> center_2det_of_3points(point p1, point p2, point p3) {
// the center of circle on the given three points
// return the determinant value and the product of center points and 2 *
// determinant value
ll x1 = p1.x, y1 = p1.y;
ll x2 = p2.x, y2 = p2.y;
ll x3 = p3.x, y3 = p3.y;
// center of 2 points
ll c2x_2 = x2 + x1, c2y_2 = y2 + y1;
ll c3x_2 = x3 + x1, c3y_2 = y3 + y1;
// vertical vector of 2 lines
ll b2y = x2 - x1, b2x = -y2 + y1;
ll b3y = x3 - x1, b3x = -y3 + y1;
ll x1_2 = c3x_2 * b3y - c3y_2 * b3x, y1_2 = c2x_2 * b2y - c2y_2 * b2x;
ll det = -b3y * b2x + b3x * b2y;
if (det == 0)
return {{INF, INF}, det};
ll cx_2det = -b2x * x1_2 + b3x * y1_2, cy_2det = -b2y * x1_2 + b3y * y1_2;
return {{cx_2det, cy_2det}, det};
}
ll inversion_number(vl a, ll a_max) {
/*
Paramters
---------
a: vector<ll>
All the elements must be non-negative.
Prefably the elements are compressed to reduce the computational cost.
a_max: ll
The maximum value of the vector a or the value bigger than the value
stated previously.
*/
BIT bit(a_max + 1);
ll val = 0;
rep(i, a.size()) {
// i is the number of elements that have lower index than a[i].
// call the number of elements that have lower value than a[i]
// by subtracting these two, the residual number is the number of elements
// that have larger value
val += i - bit.query(a[i] - 1); // cumulative sum from 0 to a[i] - 1
bit.update(a[i], 1);
}
return val;
}
template <typename T> vector<T> compress(vector<T> v) {
// sort and remove all the duplicated values
sort(all(v));
v.erase(unique(all(v)), v.end());
return v;
}
template <typename T> map<T, ll> dict(const vector<T> &v) {
map<T, ll> d;
rep(i, v.size()) d[v[i]] = i;
return d;
}
points compress2D(vl xs, vl ys) {
/*
NOTE
----
Add the corner points if required
*/
ll n = xs.size();
vl xcs = compress(xs), ycs = compress(ys);
map<ll, ll> xd = dict(xcs), yd = dict(ycs);
points ps(n);
rep(i, n) xs[i] = xd[xs[i]], ys[i] = yd[ys[i]];
rep(i, n) ps[i] = {xs[i], ys[i]};
sort(all(ps));
return ps;
}
void GaussJordanBitVector(vl &bs) {
ll n = bs.size();
ll rank = 0;
ll j = 0;
revrep(i, N_DIGITS) {
for (j = rank; j < n; j++)
if (bs[j] & (1LL << i))
break;
if (j == n)
continue;
if (j > rank)
bs[rank] ^= bs[j];
for (j = rank + 1; j < n; j++)
bs[j] = min(bs[j], bs[j] ^ bs[rank]);
rank++;
}
}
ll kruskal(vector<undirected_edge> &es, ll n_vertex) {
sort(all(es));
UnionFind uf(n_vertex);
ll min_cost = 0;
rep(i, es.size()) {
undirected_edge &e = es[i];
if (!uf.is_same(e.from, e.to)) {
min_cost += e.cost;
uf.unite(e.from, e.to);
}
}
return min_cost;
}
ll LongestIncreasedSequence(vl &v, ll n) {
vl dp(n, INF);
rep(i, n) * lower_bound(all(dp), v[i]) = v[i];
return find(all(dp), INF) - dp.begin();
}
/*
diameter of tree
Graph tree[MAX_N];
ll dM = 0, vM = 0, v2 = 0;
void dfs1(ll source, ll parent, ll d){
if (d > dM) dM = d, vM = source;
rep(i, tree[source].size()){
ll target = tree[source][i].to;
if (target == parent) continue;
dfs1(target, source, d + 1);
}
}
void dfs2(ll source, ll parent, ll d){
if (dM <= d) dM = d, v2 = source;
rep(i, tree[source].size()){
ll target = tree[source][i].to;
if (target == parent) continue;
dfs2(target, source, d + 1);
}
}
dfs(0, -1, 0);
dfs2(vM, -1, 0);
prl2(vM + 1, v2 + 1); // the two edges of tree
const ll N_VERTEX = 310;
ll a, b, t;
vector<vl> dist;
void warshall_floyd(ll n){
// rep(i, n) rep(j, n) dist[i][j] = INF * (i != j);
rep(k, n) rep(i, n) rep(j, n) dist[i][j] = min(dist[i][j], dist[i][k] +
dist[k][j]);
}
int main(void){
in2(n, m);
rep(i, n) rep(j, n) dist[i][j] = INF * (i != j);
rep(i, m){
in3(a, b, t);
a--; b--;
dist[a][b] = t;
dist[b][a] = t;
}
warshall_floyd(n);
}
vl dist, vertex_pre;
void dijkstra(ll start, ll n) {
priority_queue<pl, vector<pl>, greater<pl>> edge_costs;
dist = vl(n, INF); // vertex_pre = vl(n, -1);
dist[start] = 0;
edge_costs.push(pl(0, start));
while (!edge_costs.empty()) {
pl edge_cost = edge_costs.top();
edge_costs.pop();
ll idx = edge_cost.second;
ll cost = edge_cost.first;
if (dist[idx] < cost) continue;
rep(i, graph[idx].size()){
edge e = graph[idx][i];
if (dist[e.to] > dist[idx] + e.cost){
dist[e.to] = dist[idx] + e.cost;
// vertex_pre[e.to] = idx;
edge_costs.push(pl(dist[e.to], e.to));
}
}
}
}
vl get_predecessor(ll g){
vl path;
for (; g != -1; g = vertex_pre[g]) path.pb(g);
reverse(all(path));
return path;
}
int main(void){
in2(n, m);
rep(i, m){
in3(a, b, t);
a--; b--;
G[a].pb({b, t});
G[b].pb({a, t});
}
dijkstra(0, n);
}
# ABC061D
bool find_negative_cycle(ll goal){
rep(i, n) rep(v, n) rep(k, graph[v].size()){
edge e = graph[v][k];
if (dist[e.to] != INF && dist[e.to] > dist[v] + e.cost){
dist[e.to] = -INF;
if (goal == -1) return true;
else if (goal == e.to) return true;
}
}
return false;
}
bool bellman_ford(ll start, ll n, ll goal){
// if there is a closed circuit, it returns false. (when goal == -1)
// if the distance to goal cannot be obtained, it returns false (when goal
!= -1) fill(dist, dist + n, INF); dist[start] = 0; rep(i, n) rep(v, n) rep(k,
graph[v].size()){ edge e = graph[v][k]; if (dist[v] != INF && dist[e.to] >
dist[v] + e.cost) dist[e.to] = dist[v] + e.cost;
}
if (find_negative_cycle(goal)) return false;
return true;
}
*/
/*
# 2. The usage of next_permutation and combination (factorial search)
ll a[8];
rep(i, 8) a[i] = i;
sort(a, a + 8);
do{
}while(next_permutation(a, a+n));
# 4. imos method
// used when we would like to count the number which
// shows how many times the numbers between l and r belongs to smt.
// This method is composed of three process.
ll n, m, s[MAX_M], l, r;
in2(n, m);
rep(i, m) s[i] = 0;
// 1st step
rep(i, n){
in3(l, r, c);
l--; r--; // if l starts from 1.
s[l] += c; s[r + 1] -= c;
}
// 2nd step
rep(i, m - 1) s[i + 1] += s[i];
// 3rd step: judgement...
#5. shakutori method (syakutori, two pointers technique)
// 1. strech right side while the condition is met.
// 2. renew the answer
// 3. increments left side
// 4. Back to 1. (l <= r must be satisfied all the time.)
ll l = 0; ll r = 0;
while (l < n){
r = max(r, l);
if (l == r) r++;
while(r < n && cond) r++;
answer += r - l; l++;
}
prl(answer);
#7. The shortest path (distance) between the k-th edge and another edge
(Tree) Graph tree[MAX_N]; ll depth[MAX_N];
void dfs(ll source, ll parent, ll all_cost){
depth[source] = all_cost;
items(e, tree[source]){
if (e.to == parent) continue;
dfs(e.to, source, all_cost + e.cost);
}
}
dfs(k, -1, 0);
#11. bfs ABC146D, ABC007C
1. first create a tree.
2. start searching from a node.
3. do some processes and push nodes connected with a given target node in
BFS.
4. repeat a series of procedure until queue is empty.
queue<pl> q;
void bfs(ll source, ll parents){
ll n_edge = graph[source].size();
if (parents != -1) dist[source] = min(dist[source], dist[parents] + 1);
if (visited[source]) return;
visited[source] = true;
rep(idx, n_edge){
ll target = graph[source][idx].to;
if (target == parents) continue;
q.push(mp(target, source));
}
}
q.push(mp(sg.e1, -1));
while(!q.empty()){
pl source = q.front(); q.pop();
bfs(source.e1, source.e2);
}
#12. grid to distance matrix (dx, dy)
ll w, h;
ll pos_to_idx(ll x, ll y){
return y * w + x;
}
pl idx_to_pos(ll idx){
return mp(idx % w, idx / w);
}
rep(y, h){
in1(s);
rep(x, w){
if (s[x] == '#') wall[x][y] = true;
else wall[x][y] = false;
}
}
rep(i1, h * w)rep(i2, h * w) dist[i1][i2] = INF * (i1 != i2);
rep(x, w)rep(y, h){
ll idx1 = pos_to_idx(x, y); ll idx2;
if (wall[x][y]) continue;
if (x != 0 && !wall[x - 1][y]){
idx2 = pos_to_idx(x - 1, y);
// if warshall floyd
dist[idx1][idx2] = 1;
// if dijkstra
// graph[idx1].pb({idx2, 1});
}
if (x != w - 1 && !wall[x + 1][y]){
idx2 = pos_to_idx(x + 1, y);
dist[idx1][idx2] = 1;
// graph[idx1].pb({idx2, 1});
}
if (y != 0 && !wall[x][y - 1]){
idx2 = pos_to_idx(x, y - 1);
dist[idx1][idx2] = 1;
// graph[idx1].pb({idx2, 1});
}
if (y != h - 1 && !wall[x][y + 1]){
idx2 = pos_to_idx(x, y + 1);
dist[idx1][idx2] = 1;
// graph[idx1].pb({idx2, 1});
}
}
# Cumulative sum (2 dimension)
ll func(ll x, ll y, ll dx, ll dy){
if (x + dx > w || y + dy > h) return - INF;
ll val = cum[x + dx][y + dy];
val += cum[x][y];
val -= cum[x][y + dy];
val -= cum[x + dx][y];
return val;
}
rep(x, w + 1) cum[x][0] = 0;
rep(y, h + 1) cum[0][y] = 0;
rep(y, h + 1) rep(x, w)
cum[x + 1][y + 1] = cum[x][y + 1] + vs[x][y];
rep(x, w + 1) rep(y, h)
cum[x][y + 1] += cum[x][y];
*/
/*
# the operators regarding bit
& (AND), | (OR), ^ (XOR)
- (REVERSE), >> (SMALLER SHIFT)
<< (BIGGER SHIFT)
x1: 0000 0001 0010 0101 0110 0111 0111
x2: xxxx 0001 0011 0100 0101 1000 0110
x1 & x2: 0000 0001 0010 0100 0100 0000 0110
x: 1001 1010 1100 1011 1101 1111
x & - x: 0001 0010 0100 0001 0001 0001
sum: 1010 1100 10000 1100 1100 10000
x << y is x * 2 ** y
x >> y is rep(i, y) x = x // 2
####### Attention #######
1 << i and 1LL << i is different. If programs show WA, try switch to this one.
Let S be a bit sequence and i be a non-negative integer
S & (1 << i) -> if true, i in S
S | (1 << i) -> S union {i}
S & -(1 << i) -> S - {i}
__builtin_popcount(S) -> the number of elements in S
S = 0 -> S is an empty set
__builtin_popcountl(i) -> the number of 1 in binary
S = (1 << n) - 1 -> S includes all the elements up to the n-th
#Conditional Operator
condition ? true : false;
#iterator
type declaration: auto
value reference: *itr
increment: itr++
decrement: itr--
substitution of value: *itr = smt
# inclusion-exclusion principle
|U[i = 1 to n] Ai| = sum[i = 1 to n] |Ai| - sum[i < j]|Ai ^ Aj| + ... + (-1)^(n
- 1) |^[i = 1 to n]Ai|
*/
const ll MAX_N = 200005;
bool okay = false;
ll answer = 0;
ll n;
vl as, ans, dp;
Graph tree[MAX_N];
void dfs(ll source, ll parent) {
ll v = as[source];
ll lb = lower_bound(all(dp), v) - dp.begin();
ll val = dp[lb];
dp[lb] = v;
ans[source] = find(all(dp), INF) - dp.begin();
for (auto e : tree[source])
if (e.to != parent)
dfs(e.to, source);
dp[lb] = val;
}
void solve() {
as = ans = vl(n);
rep(i, n) in1(as[i]);
rep(i, n - 1) {
ll a, b;
in2(a, b);
a--, b--;
tree[a].pb({b, 1});
tree[b].pb({a, 1});
}
dp = vl(n, INF);
dfs(0, -1);
rep(i, n) prl(ans[i]);
}
int main(void) {
in1(n);
// assert(n <= 9);
solve();
return 0;
}
| #include <algorithm>
#include <bits/stdc++.h>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef long double ld;
using edge = struct {
ll to;
ll cost;
};
struct point {
ll x;
ll y;
bool operator<(const point &p) const {
if (x == p.x)
return y < p.y;
return x < p.x;
}
};
struct undirected_edge {
ll from;
ll to;
ll cost;
bool operator<(const undirected_edge &ue) const { return cost < ue.cost; }
};
typedef string str;
typedef std::pair<ll, ll> pl;
typedef std::pair<ll, pl> pl3;
typedef std::map<string, ll> msl;
typedef std::map<char, ll> mcl;
typedef std::map<ll, ll> mll;
typedef std::vector<ll> vl;
typedef std::vector<pl> vpl;
typedef std::vector<point> points;
typedef std::vector<pl3> vpl3;
// priority queue. Taking from the higher value. Don't forget calling !q.empty()
typedef std::priority_queue<ll> pq;
// priority queue. Taking from the lower value
typedef std::priority_queue<ll, vl, greater<ll>> pql;
typedef std::vector<edge> Graph;
const ll N_DIGITS = 60;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll INF = MOD * MOD;
const long double EPS = 1e-9;
const long double PI = 3.14159265358979323846;
points dirs = {
{-1, 0}, {1, 0}, {0, 1}, {0, -1}, // four directions
{1, 1}, {-1, 1}, {-1, -1}, {1, -1}, // diagonal
{0, 0} // self
};
template <typename A, typename B> string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string &s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(vector<bool> v) {
bool first = true;
string res = "{";
for (int i = 0; i < static_cast<int>(v.size()); i++) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(v[i]);
}
res += "}";
return res;
}
template <size_t N> string to_string(bitset<N> v) {
string res = "";
for (size_t i = 0; i < N; i++) {
res += static_cast<char>('0' + v[i]);
}
return res;
}
template <typename A> string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
template <typename A, typename B> string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
#define rep(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define revrep(i, n) for (ll(i) = n - 1; (i) >= 0; (i)--)
#define For(i, a, b) for (ll(i) = (a); (i) < (b); (i)++)
#define revFor(i, b, a) for (ll(i) = (b)-1; (i) >= (a); (i)--)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define isUpper(c) ('a' - c > 0)
#define isNum(c) (0 <= (c) - '0' && (c) - '0' <= 9)
#define toLower(c) char((c) + 0x20)
#define toUpper(c) char((c)-0x20)
#define pb push_back
#define mp make_pair
#define pr(a) cout << (a)
#define prl(a) cout << (a) << endl
#define prl2(a, b) cout << (a) << " " << (b) << endl
#define prl3(a, b, c) cout << (a) << " " << (b) << " " << (c) << endl
#define prl4(a, b, c, d) \
cout << (a) << " " << (b) << " " << (c) << " " << (d) << endl
#define prs(a) cout << (a) << " "
#define prs2(a, b) cout << (a) << " " << (b) << " "
#define prs3(a, b, c) cout << (a) << " " << (b) << " " << (c) << " "
#define prs4(a, b, c, d) \
cout << (a) << " " << (b) << " " << (c) << " " << (d) << " "
#define yn(condition) \
if ((condition)) \
prl("Yes"); \
else \
prl("No");
#define YN(condition) \
if ((condition)) \
prl("YES"); \
else \
prl("NO");
#define in1(a) cin >> (a)
#define in2(a, b) cin >> (a) >> (b)
#define in3(a, b, c) cin >> (a) >> (b) >> (c)
#define in4(a, b, c, d) cin >> (a) >> (b) >> (c) >> (d)
#define e1 first
#define e2 second
#define ctol(c) ll((c)) - ll('0')
#define ltos(n) to_string((n))
#define items(kv, v) for (auto &(kv) : (v))
#define ndig(N, n) ctol(ll(ltos((N))[ll(ltos((N)).length()) - (n)]))
#define rsort(a, n) sort(a, a + n, greater<>())
#define Forchar(c, a, z) for (char(c) = (a); (c) <= (z); (c)++)
#define cntchar(s, c) count(all((s)), c)
#define substring(s, start, end) s.substr((start), (end) - (start) + 1)
#define prl_nd(num, digits) \
cout << fixed << setprecision(digits) << (num) << endl;
#define XOR(a, b) (a) ^ (b)
#define prl_time(s) \
prl3("Elapsed Time:", 1000.0 * (clock() - s) / CLOCKS_PER_SEC, "[ms]");
#define char_to_str(c) string(1, (c))
#define DEBUG(x) cerr << #x << ": " << (x) << endl
#define DEBUG_VEC(v) \
cerr << #v << ": "; \
rep(__i, (v).size()) cerr << ((v)[__i]) << ", "; \
cerr << endl
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
struct MaxFlow {
struct F_edge {
ll to, rev, capacity;
F_edge(ll to, ll rev, ll capacity) : to(to), rev(rev), capacity(capacity) {}
};
typedef vector<F_edge> F_edges;
vector<F_edges> graph;
ll n_vertex;
// level is the shortest path to get a given node from the source node.
vl level, iter;
MaxFlow(ll n_vertex) : n_vertex(n_vertex) { graph.resize(n_vertex); }
void add_edge(ll from, ll to, ll capacity) {
graph[from].pb({to, ll(graph[to].size()), capacity});
graph[to].pb({from, ll(graph[from].size()) - 1, 0});
}
void bfs(ll source) {
level = vl(n_vertex, -1);
level[source] = 0;
queue<ll> q;
q.push(source);
while (!q.empty()) {
ll vertex = q.front();
q.pop();
rep(i, graph[vertex].size()) {
ll target = graph[vertex][i].to;
ll cap_target = graph[vertex][i].capacity;
// if the flow can be into the target node, implement below.
if (cap_target > 0 && level[target] < 0) {
level[target] = level[vertex] + 1;
q.push(target);
}
}
}
}
ll dfs(ll vertex, ll sink, ll flow) {
if (vertex == sink)
return flow;
for (ll &i = iter[vertex]; i < graph[vertex].size(); i++) {
ll target = graph[vertex][i].to;
ll cap_target = graph[vertex][i].capacity;
ll rev_target = graph[vertex][i].rev;
// if capasitiy is not full yet and target is farther,
// then assign current flow
if (cap_target > 0 && level[vertex] < level[target]) {
ll d = dfs(target, sink, min(cap_target, flow));
if (d > 0) { // if the flow successfully reaches the sink, reduce the
// flow from the capacity
graph[vertex][i].capacity -= d;
graph[target][rev_target].capacity += d;
return d;
}
}
}
return 0;
}
ll dinic(ll source, ll sink) {
ll flow = 0;
while (true) {
bfs(source);
// if there is no path leading to the sink, the maximum flow is 0.
if (level[sink] < 0)
return flow;
iter = vl(n_vertex, 0);
ll f;
while ((f = dfs(source, sink, INF)) > 0)
flow += f;
}
}
};
class UnionFind {
vl parents, set_size;
ll n_groups;
public:
UnionFind() {}
UnionFind(ll n) {
parents = set_size = vl(n);
n_groups = n;
rep(i, n) {
parents[i] = i;
set_size[i] = 1LL;
}
}
ll root_find(ll x) {
if (parents[x] == x)
return x;
return parents[x] = root_find(parents[x]);
}
void unite(ll x, ll y) {
// priority for x is larger than that of y
x = root_find(x);
y = root_find(y);
if (x == y)
return;
parents[y] = x, set_size[x] += set_size[y];
n_groups--;
}
bool is_same(ll x, ll y) { // connected or not
return root_find(x) == root_find(y);
}
ll size(ll x) { return set_size[root_find(x)]; }
ll union_count() const { return n_groups; }
};
struct Doubling { // ABC167D
ll n;
ll sz;
vector<vl> next;
/*
next[k + 1][i] := next[k][next[k][i]]
next[0][i] := edge[i]
e.g. a0, a1, ..., an-1 / 0 <= ai <= n - 1
a0 -> a[a0] -> a[a[a0]] -> ... -> a[a[...[a[0]]]] (m times)
Let the function repeatedly input a[i] m times be f[m](a[i])
- get(i, x) returns f[x](a[i])
- lower_bound(i, j) returns minimum x which satisfies f[x](a[i]) >= j.
if not possible returns n.
*/
// edge[i]: the step size for one iteration
Doubling(vl &edge) : n(edge.size()), sz(62) {
next.resize(sz, vl(n, -1));
rep(i, n) next[0][i] = edge[i];
rep(k, sz - 1) rep(i, n) next[k + 1][i] = next[k][next[k][i]];
}
ll get(ll i, ll x) {
ll ret = i;
rep(bit, sz) {
if (!(x >> bit & 1))
continue;
ret = next[bit][ret];
}
return ret;
}
ll lower_bound(ll i, ll j) {
ll cur = i, acc = 0;
revrep(wid, sz) {
if (next[wid][cur] < j) {
acc += 1LL << wid;
cur = next[wid][cur];
}
}
return min(n, acc + 1);
}
};
class LowestCommonAncestor {
public:
ll N, logN;
vl depth, len;
vector<Graph> tree;
vector<vl> parents;
LowestCommonAncestor(ll n, ll offset = -1, bool is_weighted = true) {
N = n;
logN = 0;
while (N > (1LL << logN))
logN++;
depth = vl(N);
len = vl(N);
parents = vector<vl>(logN, vl(N));
tree = vector<Graph>(N);
input(N, offset, is_weighted);
init(0, -1, 0, 0);
build();
}
void add_edge(ll from, ll to, ll dist) {
tree[from].pb({to, dist});
tree[to].pb({from, dist});
}
void input(ll n, ll offset = -1, bool is_weighted = true) {
rep(i, n - 1) {
ll a, b, d = 1;
in2(a, b);
a += offset, b += offset;
if (is_weighted)
in1(d);
add_edge(a, b, d);
}
}
void init(ll source, ll parent, ll d, ll l) {
depth[source] = d;
parents[0][source] = parent;
len[source] = l;
rep(i, tree[source].size()) {
ll target = tree[source][i].to;
ll cost = tree[source][i].cost;
if (target == parent)
continue;
init(target, source, d + 1, cost + l);
}
}
void build() {
rep(k, logN - 1) rep(n, N) {
// if there is no parent, -1.
// otherwise, the parent of the parent is the parent.
if (parents[k][n] < 0)
parents[k + 1][n] = -1;
else
parents[k + 1][n] = parents[k][parents[k][n]];
}
}
ll query(ll u, ll v) {
if (depth[u] > depth[v])
swap(u, v);
rep(k, logN) if ((depth[v] - depth[u]) >> k & 1) v = parents[k][v];
if (u == v)
return u;
revrep(k, logN) {
if (parents[k][u] != parents[k][v]) {
u = parents[k][u];
v = parents[k][v];
}
}
return parents[0][u];
}
ll distance(ll u, ll v) {
ll w = query(u, v);
return len[u] + len[v] - 2 * len[w];
}
};
struct BIT {
ll n;
vl dat;
BIT(ll n, ll ini = 0) : dat(n + 1, ini), n(n){};
// x: 1001 1010 1100 1011 1101 1111
// x & - x: 0001 0010 0100 0001 0001 0001
// ->: 1010 1100 10000 1100 1100 10000
ll update_func(ll val, ll d) {
// if maximum -> max(val, dat)
// return max(val, dat);
// if cumulative sum
return val + d;
}
ll query(ll i) {
/*
v[0] + v[1] + ... + v[i]
e.g.) i = 10101
itr1. 10101 -> 10100
itr2. 10100 -> 10000
itr3. 10000 -> 00000 (break)
*/
ll ret = 0;
for (ll j = i; j >= 0; j = (j & (j + 1)) - 1) {
ret = update_func(ret, dat[j]);
}
return ret;
}
ll query(ll l, ll r) { return query(r) - query(l); }
ll lower_bound(ll key) {
// v[0] + v[1] + ... + v[left - 1] < key <= v[0] + v[1] + ... + v[left]
if (key <= 0)
return 0;
ll left = 0, right = 1;
while (right <= n)
right *= 2;
for (ll i = right; i > 0; i /= 2) {
if (left + i <= n && dat[left + i - 1] < key) {
key -= dat[left + i - 1];
left += i;
}
}
return left;
}
void update(ll i, ll val) {
/*
e.g.) i = 10101, n = 11111
itr1. i: 10101, i+1: 10110 -> 10111
itr2. i: 10111, i+1: 11000 -> 11111 (break)
*/
if (i < 0)
return;
for (ll j = i; j < n; j |= j + 1) {
dat[j] = update_func(val, dat[j]);
}
}
};
struct SegmentTree {
ll n, ini, minimize;
vl dat;
// when seeking minimum
// ini = INF
// when seeking maximum
// ini = -INF
SegmentTree(ll n_, bool minimize_ = true) {
n = 1;
minimize = minimize_;
if (minimize)
ini = INF;
else
ini = -INF;
while (n < n_)
n *= 2;
dat.resize(2 * n - 1);
rep(i, 2 * n - 1) dat[i] = ini;
};
void update(ll idx, ll val) {
idx += n - 1;
if (minimize && dat[idx] <= val)
return;
if (!minimize && dat[idx] >= val)
return;
dat[idx] = val;
while (idx > 0) {
idx = (idx - 1) / 2;
// when seeking minimum
if (minimize)
dat[idx] = min(dat[idx * 2 + 1], dat[idx * 2 + 2]);
// when seeking maximum
else
dat[idx] = max(dat[idx * 2 + 1], dat[idx * 2 + 2]);
}
}
ll query(ll l, ll r) {
// ### NOTE ###
// the range is [l, r]
// l, l + 1, ..., r
r++; // to adjust to this method
return query_segment(l, r, 0, 0, n);
}
ll query_segment(ll a, ll b, ll idx, ll l, ll r) {
assert(a < b);
if (r <= a || b <= l)
return ini;
if (a <= l && r <= b)
return dat[idx];
else {
ll seg1 = query_segment(a, b, idx * 2 + 1, l, (l + r) / 2);
ll seg2 = query_segment(a, b, idx * 2 + 2, (l + r) / 2, r);
// when seeking minimum
if (minimize)
return min(seg1, seg2);
// when seeking maximum
else
return max(seg1, seg2);
}
}
};
template <class Target> class RerootingTreeDP {
public:
using T = typename Target::type;
struct DP_edge {
ll to, rev; // rev is the index to trace the source node.
T value; // objective value
};
private:
ll n;
void dfs_fwd(ll source, ll parent) {
ll par_idx = -1;
vector<T> values;
rep(i, tree[source].size()) {
const DP_edge &e = tree[source][i];
if (e.to == parent) {
par_idx = i;
continue;
}
dfs_fwd(e.to, source);
values.pb(e.value);
}
// If the parent != -1, update the value on edge from parent to source
if (par_idx != -1) {
ll src_idx = tree[source][par_idx].rev;
// update values on the edge from parent to source
tree[parent][src_idx].value = Target::merge(values);
}
}
void dfs_bwd(ll source, ll parent) {
vector<T> values;
for (auto &&e : tree[source])
values.pb(e.value);
values = Target::evaluate(values);
rep(i, tree[source].size()) {
const DP_edge &e = tree[source][i];
if (e.to == parent)
continue;
// tree[e.to][e.rev]: e.to -> source
tree[e.to][e.rev].value = values[i];
dfs_bwd(e.to, source);
}
}
public:
UnionFind uf;
vector<vector<DP_edge>> tree;
RerootingTreeDP(ll n) : n(n), uf(n), tree(n) {}
void add_edge(ll u, ll v, T val) {
assert(!uf.is_same(u, v));
tree[u].pb({v, ll(tree[v].size()), val});
tree[v].pb({u, ll(tree[u].size()) - 1, val});
uf.unite(u, v);
}
void dp() {
vector<bool> visited(n, false);
rep(i, n) {
if (visited[uf.root_find(i)])
continue;
dfs_fwd(i, -1);
visited[uf.root_find(i)] = true;
}
visited.assign(n, false);
rep(i, n) {
if (visited[uf.root_find(i)])
continue;
dfs_bwd(i, -1);
visited[uf.root_find(i)] = true;
}
}
ll size() const { return tree.size(); }
};
// ABC160F is one example
// Modify the functions evaluate and merge based on given problems
struct Merger {
using type = ll;
static type merge(const vector<type> &value) {
// merge the result below the source node
// each value is from each child node of the source node
// value[i] := f(child i)
// Here, we would like to obtain f(source) using f(child i) (i = 0, 1, ...,
// n_children)
ll ret = 1;
for (auto &&v : value)
ret += v;
return ret;
}
static vector<type> evaluate(const vector<type> &value) {
// value[i] := f(source -> child i)
// we would like to obtain f(child i -> source)
// child j (j != i) is the grandchildren of child i
// represent f(child i -> source) using f(source -> j) (j != i)
// L[i + 1] := the result using f(source -> k) (k = 0, 1, ..., i)
// R[i] := the result using f(source -> k) (k = i, i + 1, ..., n_children)
const ll n_children = value.size();
vl L(n_children + 1, 0), R(n_children + 1, 0);
rep(i, n_children) L[i + 1] = L[i] + value[i];
revrep(i, n_children) R[i] = R[i + 1] + value[i];
vl ret(n_children);
rep(i, n_children) ret[i] = L[i] + R[i + 1] + 1;
return ret;
}
};
struct StronglyConnectedComponents {
ll n;
vector<vl> graph, graph_rev, dag, cmp;
vl order, visited, cmp_idx;
StronglyConnectedComponents() {}
StronglyConnectedComponents(ll sz)
: n(sz), graph(sz), graph_rev(sz), visited(sz), cmp_idx(sz) {}
void add_edge(ll from, ll to) {
graph[from].pb(to);
graph_rev[to].pb(from);
}
void input(ll m, ll offset = -1) {
ll a, b;
rep(i, m) {
in2(a, b);
add_edge(a + offset, b + offset);
}
}
ll operator[](ll k) { return cmp_idx[k]; }
void dfs_fwd(ll source) {
visited[source] = 1;
rep(i, graph[source].size()) {
ll target = graph[source][i];
if (!visited[target])
dfs_fwd(target);
}
order.pb(source);
}
void dfs_bwd(ll source, ll num) {
visited[source] = 1, cmp_idx[source] = num;
cmp[num].pb(source);
rep(i, graph_rev[source].size()) {
ll target = graph_rev[source][i];
if (!visited[target])
dfs_bwd(target, num);
}
}
ll build() {
fill(all(visited), 0);
order.clear();
rep(i, n) if (!visited[i]) dfs_fwd(i);
fill(all(visited), 0);
ll num = 0;
revrep(i, order.size()) {
if (!visited[order[i]]) {
dag.pb(vl());
cmp.pb(vl());
dfs_bwd(order[i], num++);
}
}
rep(i, n) for (ll to : graph[i]) if (cmp_idx[i] != cmp_idx[to])
dag[cmp_idx[i]]
.pb(cmp_idx[to]);
rep(i, num) {
sort(all(dag[i]));
dag[i].erase(unique(all(dag[i])), dag[i].end());
}
return num;
}
};
struct CombinationMemo {
ll sz, mod;
vl facts, facts_inv, minv;
CombinationMemo(ll sz, ll _mod) : sz(sz), mod(_mod) {
facts.resize(sz + 5);
facts_inv.resize(sz + 5);
minv.resize(sz + 5);
init();
}
void init() {
facts[0] = facts[1] = 1;
minv[1] = 1;
facts_inv[0] = facts_inv[1] = 1;
For(i, 2, sz + 3) {
facts[i] = (i * facts[i - 1]) % mod;
minv[i] = mod - minv[mod % i] * (mod / i) % mod;
facts_inv[i] = facts_inv[i - 1] * minv[i] % mod;
}
}
ll nCk(ll n, ll r) {
if (n == r && n == 0)
return 1;
else if (n <= 0 || r < 0 || r > n)
return 0;
ll val = (facts[n] * facts_inv[n - r]) % mod;
val *= facts_inv[r];
return val % mod;
}
};
ll gcd(ll m, ll n) {
ll a = max(m, n);
ll b = min(m, n);
while (b != 1 && b != 0) {
a %= b;
swap(a, b);
}
return b == 1 ? 1 : a;
}
ll lcm(ll m, ll n) { return m / gcd(m, n) * n; }
ll power_mod(ll a, ll power, ll mod) {
ll value = 1;
while (power != 0) {
if (power & 1)
value = (value * a) % mod;
a = (a * a) % mod;
power = power >> 1;
}
return value % mod;
}
ll modinv(ll a, ll mod) { return power_mod(a, mod - 2, mod); }
ll power_normal(ll a, ll power) {
ll value = 1;
while (power != 0) {
if (power & 1)
value = value * a;
a = a * a;
power = power >> 1;
}
return value;
}
vector<vl> pascal_triangle(ll n) {
/*
Complexity: O(n^2)
The upper bound of n is nearly 50.
Parameters
----------
n; the size of returned vector
Returns
-------
comb[i][j]: combination(i, j). 0 <= i <= n, 0 <= j <= i
*/
vector<vl> comb(n, vl(n));
comb[0][0] = 1;
For(i, 1, n + 1) rep(j, i + 1) {
comb[i][j] += comb[i - 1][j];
if (j > 0)
comb[i][j] += comb[i - 1][j - 1];
}
return comb;
}
ll combination(ll n, ll r, ll mod) {
if (n == r && n == 0)
return 1;
else if (n <= 0 || r < 0 || r > n)
return 0;
ll numerator = 1;
ll denomenator = 1;
for (ll i = 0; i < r; i++) {
ll num = (n - i) % mod, den = (i + 1) % mod;
(numerator *= num) %= mod;
(denomenator *= modinv(den, mod)) %= mod;
}
return (numerator * denomenator) % mod;
}
ld log_combination(ll n, ll r) {
if (n == r && n == 0)
return 0;
else if (n <= 0 || r < 0 || r > n)
return -INF;
ld val = 0;
for (ll i = 0; i < r; i++) {
val += log(n - i);
val -= log(i + 1);
}
return val;
}
ll bin_search(ll key, ll A[], ll left, ll right) {
// return the index idx where A[idx] = key.
// A[left] is start and A[right] is end..
// In other words, A[right], not A[right - 1], must be defined.
while (right >= left) {
ll mid = left + (right - left) / 2;
if (A[mid] == key)
return mid;
else if (A[mid] > key)
right = mid - 1;
else if (A[mid] < key)
left = mid + 1;
}
return -1;
}
/*
// vs[lb - 1] < key <= vs[lb]
lb = lower_bound(all(vs), key);
// vs[ub - 1] <= key < vs[lb]
ub = upper_bound(all(vs), key);
ll bin_search_temp(ll left, ll right, callable judge){
while(right > left){
// when seeking lower bound
ll mid = (right + left) / 2;
if (judge(mid)) right = mid;
else left = mid + 1;
// when seeking upper bound
ll mid = (right + left + 1) / 2;
if (judge(mid)) left = mid;
else right = mid - 1;
}
return right;
}
trinary_search
// Care the value EPS!!! Compare to the condition
ld left = 0; ld right = p;
while(abs(right - left) > EPS){
ld left2 = (2 * left + right) / 3.0;
ld right2 = (left + 2 * right) / 3.0;
ld f1 = tak_func(left2);
ld f2 = tak_func(right2);
if (f1 <= f2) right = right2;
else if (f2 <= f1) left = left2;
}
*/
str bin_expression(ll n, ll dig) {
str s = "";
rep(i, dig) {
s += ltos(n % 2);
n /= 2;
}
reverse(all(s));
return s;
}
ll fact(ll n) {
if (n == 0)
return 1;
return n * fact(n - 1);
}
ll fact_mod(ll n, ll mod) {
if (n == 0)
return 1;
return n * fact_mod(n - 1, mod);
}
bool is_prime(ll n) {
if (n <= 1)
return false;
for (ll i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
mll prime_factorization(ll n) {
ll i = 2;
mll table;
while (i * i <= n) {
while (n % i == 0) {
table[i]++;
n /= i;
}
i++;
}
if (n > 1)
table[n] = 1;
return table;
}
vl divisor_table(ll n) {
vl table;
ll i = 1;
while (i * i <= n) {
if (n % i == 0) {
table.pb(i);
if (i * i != n)
table.pb(n / i);
}
i++;
}
sort(all(table));
return table;
}
ll char_to_idx(char c) {
ll idx = 0;
Forchar(cc, 'a', 'z') {
if (c == cc)
return idx;
else
idx++;
}
}
ll next_combination(ll sub) {
/*
### Attention ###
if k is 0 or 1 and n is 0, it does not work well.
ll n, k; ll bit = (1 << k) - 1;
for (; bit < (1 << n); bit = next_combination(bit)){
bool ith = bit & (1 << i);
procedures...
}
sub & -sub: the binary which shares the last digit whose value is 1 in sub
sub + x : carry up the last digit
~y : the binary whose digits are 1 if y's digit is 0.
(sub & ~y) / x: reduce the same number of 0s after first 1 in x from (sub &
~y).
*/
ll x = sub & -sub, y = sub + x;
return (((sub & ~y) / x) >> 1) | y;
}
// just change the input if you want to change the target.
// If you want to check the common sequences in two strings,
// combine them. e.g. ABC150F
vl z_algorithm(str s) {
/*
Paramters
---------
str: the string of interest
Returns
-------
res[i] is the maximum number of K which satisfies
s[:K] == s[i:i + K]
for each i = 0, 1, 2, ..., n - 1.
*/
ll n = s.length();
vl res(n);
res[0] = n;
ll i1 = 1, i2 = 0;
while (i1 < n) {
/*
i1: the starting point
i2: the length of substring
*/
while (i1 + i2 < n && s[i2] == s[i1 + i2])
++i2;
res[i1] = i2;
if (i2 == 0) {
++i1;
continue;
}
ll i3 = 1;
// update the already seen points
while (i1 + i3 < n && i3 + res[i3] < i2) {
res[i1 + i3] = res[i3];
++i3;
}
// update up to i1 + i3 and the next possible minimum length is i2 - i3 (=
// res[i3])
i1 += i3, i2 -= i3;
}
return res;
}
ll big_number_mod(str s, ll mod) {
ll l = s.length();
ll idx = 0;
ll val = 0;
ll tenth = 1;
while (idx < l) {
ll m = ctol(s[l - 1 - idx]);
val += (m * tenth) % mod;
val %= mod;
tenth *= 10;
tenth %= mod;
idx++;
}
return val;
}
ll big_number_compare(str s1, str s2) {
if (s1.length() > s2.length())
return 1;
else if (s1.length() < s2.length())
return -1;
else if (s1 == s2)
return 0;
return 2 * (s1 > s2) - 1;
}
ll string_to_ll(str s) {
ll l = s.length();
ll idx = 0;
ll val = 0;
ll tenth = 1;
while (idx < l) {
ll m = ctol(s[l - 1 - idx]);
val += (m * tenth);
tenth *= 10;
idx++;
}
return val;
}
str reflected_string(str s) {
str t, u;
ll n = s.length();
t = s;
reverse(all(t));
u = substring(t, 0, n - 2) + s + substring(t, 1, n - 1);
return u;
}
ld distance_between_point_line(point l_begin, point l_end, point p) {
ll xl1 = l_begin.x, yl1 = l_begin.y;
ll xl2 = l_end.x, yl2 = l_end.y;
ll xp = p.x, yp = p.y;
ll a = yl2 - yl1;
ll b = -xl2 + xl1;
ll c = -a * xl2 - b * yl2;
return abs(ld(a * xp + b * yp + c)) / ld(sqrt(a * a + b * b));
}
bool is_cross(point l1_begin, point l1_end, point l2_begin, point l2_end) {
ll x1 = l1_begin.x, y1 = l1_begin.y;
ll x2 = l1_end.x, y2 = l1_end.y;
ll x3 = l2_begin.x, y3 = l2_begin.y;
ll x4 = l2_end.x, y4 = l2_end.y;
ll val1 = (x1 - x2) * (y3 - y1) + (y1 - y2) * (x1 - x3);
ll val2 = (x1 - x2) * (y4 - y1) + (y1 - y2) * (x1 - x4);
ll val3 = (x3 - x4) * (y1 - y3) + (y3 - y4) * (x3 - x1);
ll val4 = (x3 - x4) * (y2 - y3) + (y3 - y4) * (x3 - x2);
return val1 * val2 < 0 && val3 * val4 < 0;
}
ld space_of_triangle(point p1, point p2, point p3) {
ll x1 = p1.x, y1 = p1.y;
ll x2 = p2.x, y2 = p2.y;
ll x3 = p3.x, y3 = p3.y;
ll v1 = x2 - x1;
ll u1 = y2 - y1;
ll v2 = x3 - x1;
ll u2 = y3 - y1;
ld s = ld(v1 * u2 - u1 * v2) / ld(2);
return abs(s);
}
pair<point, ll> center_2det_of_3points(point p1, point p2, point p3) {
// the center of circle on the given three points
// return the determinant value and the product of center points and 2 *
// determinant value
ll x1 = p1.x, y1 = p1.y;
ll x2 = p2.x, y2 = p2.y;
ll x3 = p3.x, y3 = p3.y;
// center of 2 points
ll c2x_2 = x2 + x1, c2y_2 = y2 + y1;
ll c3x_2 = x3 + x1, c3y_2 = y3 + y1;
// vertical vector of 2 lines
ll b2y = x2 - x1, b2x = -y2 + y1;
ll b3y = x3 - x1, b3x = -y3 + y1;
ll x1_2 = c3x_2 * b3y - c3y_2 * b3x, y1_2 = c2x_2 * b2y - c2y_2 * b2x;
ll det = -b3y * b2x + b3x * b2y;
if (det == 0)
return {{INF, INF}, det};
ll cx_2det = -b2x * x1_2 + b3x * y1_2, cy_2det = -b2y * x1_2 + b3y * y1_2;
return {{cx_2det, cy_2det}, det};
}
ll inversion_number(vl a, ll a_max) {
/*
Paramters
---------
a: vector<ll>
All the elements must be non-negative.
Prefably the elements are compressed to reduce the computational cost.
a_max: ll
The maximum value of the vector a or the value bigger than the value
stated previously.
*/
BIT bit(a_max + 1);
ll val = 0;
rep(i, a.size()) {
// i is the number of elements that have lower index than a[i].
// call the number of elements that have lower value than a[i]
// by subtracting these two, the residual number is the number of elements
// that have larger value
val += i - bit.query(a[i] - 1); // cumulative sum from 0 to a[i] - 1
bit.update(a[i], 1);
}
return val;
}
template <typename T> vector<T> compress(vector<T> v) {
// sort and remove all the duplicated values
sort(all(v));
v.erase(unique(all(v)), v.end());
return v;
}
template <typename T> map<T, ll> dict(const vector<T> &v) {
map<T, ll> d;
rep(i, v.size()) d[v[i]] = i;
return d;
}
points compress2D(vl xs, vl ys) {
/*
NOTE
----
Add the corner points if required
*/
ll n = xs.size();
vl xcs = compress(xs), ycs = compress(ys);
map<ll, ll> xd = dict(xcs), yd = dict(ycs);
points ps(n);
rep(i, n) xs[i] = xd[xs[i]], ys[i] = yd[ys[i]];
rep(i, n) ps[i] = {xs[i], ys[i]};
sort(all(ps));
return ps;
}
void GaussJordanBitVector(vl &bs) {
ll n = bs.size();
ll rank = 0;
ll j = 0;
revrep(i, N_DIGITS) {
for (j = rank; j < n; j++)
if (bs[j] & (1LL << i))
break;
if (j == n)
continue;
if (j > rank)
bs[rank] ^= bs[j];
for (j = rank + 1; j < n; j++)
bs[j] = min(bs[j], bs[j] ^ bs[rank]);
rank++;
}
}
ll kruskal(vector<undirected_edge> &es, ll n_vertex) {
sort(all(es));
UnionFind uf(n_vertex);
ll min_cost = 0;
rep(i, es.size()) {
undirected_edge &e = es[i];
if (!uf.is_same(e.from, e.to)) {
min_cost += e.cost;
uf.unite(e.from, e.to);
}
}
return min_cost;
}
ll LongestIncreasedSequence(vl &v, ll n) {
vl dp(n, INF);
rep(i, n) * lower_bound(all(dp), v[i]) = v[i];
return find(all(dp), INF) - dp.begin();
}
/*
diameter of tree
Graph tree[MAX_N];
ll dM = 0, vM = 0, v2 = 0;
void dfs1(ll source, ll parent, ll d){
if (d > dM) dM = d, vM = source;
rep(i, tree[source].size()){
ll target = tree[source][i].to;
if (target == parent) continue;
dfs1(target, source, d + 1);
}
}
void dfs2(ll source, ll parent, ll d){
if (dM <= d) dM = d, v2 = source;
rep(i, tree[source].size()){
ll target = tree[source][i].to;
if (target == parent) continue;
dfs2(target, source, d + 1);
}
}
dfs(0, -1, 0);
dfs2(vM, -1, 0);
prl2(vM + 1, v2 + 1); // the two edges of tree
const ll N_VERTEX = 310;
ll a, b, t;
vector<vl> dist;
void warshall_floyd(ll n){
// rep(i, n) rep(j, n) dist[i][j] = INF * (i != j);
rep(k, n) rep(i, n) rep(j, n) dist[i][j] = min(dist[i][j], dist[i][k] +
dist[k][j]);
}
int main(void){
in2(n, m);
rep(i, n) rep(j, n) dist[i][j] = INF * (i != j);
rep(i, m){
in3(a, b, t);
a--; b--;
dist[a][b] = t;
dist[b][a] = t;
}
warshall_floyd(n);
}
vl dist, vertex_pre;
void dijkstra(ll start, ll n) {
priority_queue<pl, vector<pl>, greater<pl>> edge_costs;
dist = vl(n, INF); // vertex_pre = vl(n, -1);
dist[start] = 0;
edge_costs.push(pl(0, start));
while (!edge_costs.empty()) {
pl edge_cost = edge_costs.top();
edge_costs.pop();
ll idx = edge_cost.second;
ll cost = edge_cost.first;
if (dist[idx] < cost) continue;
rep(i, graph[idx].size()){
edge e = graph[idx][i];
if (dist[e.to] > dist[idx] + e.cost){
dist[e.to] = dist[idx] + e.cost;
// vertex_pre[e.to] = idx;
edge_costs.push(pl(dist[e.to], e.to));
}
}
}
}
vl get_predecessor(ll g){
vl path;
for (; g != -1; g = vertex_pre[g]) path.pb(g);
reverse(all(path));
return path;
}
int main(void){
in2(n, m);
rep(i, m){
in3(a, b, t);
a--; b--;
G[a].pb({b, t});
G[b].pb({a, t});
}
dijkstra(0, n);
}
# ABC061D
bool find_negative_cycle(ll goal){
rep(i, n) rep(v, n) rep(k, graph[v].size()){
edge e = graph[v][k];
if (dist[e.to] != INF && dist[e.to] > dist[v] + e.cost){
dist[e.to] = -INF;
if (goal == -1) return true;
else if (goal == e.to) return true;
}
}
return false;
}
bool bellman_ford(ll start, ll n, ll goal){
// if there is a closed circuit, it returns false. (when goal == -1)
// if the distance to goal cannot be obtained, it returns false (when goal
!= -1) fill(dist, dist + n, INF); dist[start] = 0; rep(i, n) rep(v, n) rep(k,
graph[v].size()){ edge e = graph[v][k]; if (dist[v] != INF && dist[e.to] >
dist[v] + e.cost) dist[e.to] = dist[v] + e.cost;
}
if (find_negative_cycle(goal)) return false;
return true;
}
*/
/*
# 2. The usage of next_permutation and combination (factorial search)
ll a[8];
rep(i, 8) a[i] = i;
sort(a, a + 8);
do{
}while(next_permutation(a, a+n));
# 4. imos method
// used when we would like to count the number which
// shows how many times the numbers between l and r belongs to smt.
// This method is composed of three process.
ll n, m, s[MAX_M], l, r;
in2(n, m);
rep(i, m) s[i] = 0;
// 1st step
rep(i, n){
in3(l, r, c);
l--; r--; // if l starts from 1.
s[l] += c; s[r + 1] -= c;
}
// 2nd step
rep(i, m - 1) s[i + 1] += s[i];
// 3rd step: judgement...
#5. shakutori method (syakutori, two pointers technique)
// 1. strech right side while the condition is met.
// 2. renew the answer
// 3. increments left side
// 4. Back to 1. (l <= r must be satisfied all the time.)
ll l = 0; ll r = 0;
while (l < n){
r = max(r, l);
if (l == r) r++;
while(r < n && cond) r++;
answer += r - l; l++;
}
prl(answer);
#7. The shortest path (distance) between the k-th edge and another edge
(Tree) Graph tree[MAX_N]; ll depth[MAX_N];
void dfs(ll source, ll parent, ll all_cost){
depth[source] = all_cost;
items(e, tree[source]){
if (e.to == parent) continue;
dfs(e.to, source, all_cost + e.cost);
}
}
dfs(k, -1, 0);
#11. bfs ABC146D, ABC007C
1. first create a tree.
2. start searching from a node.
3. do some processes and push nodes connected with a given target node in
BFS.
4. repeat a series of procedure until queue is empty.
queue<pl> q;
void bfs(ll source, ll parents){
ll n_edge = graph[source].size();
if (parents != -1) dist[source] = min(dist[source], dist[parents] + 1);
if (visited[source]) return;
visited[source] = true;
rep(idx, n_edge){
ll target = graph[source][idx].to;
if (target == parents) continue;
q.push(mp(target, source));
}
}
q.push(mp(sg.e1, -1));
while(!q.empty()){
pl source = q.front(); q.pop();
bfs(source.e1, source.e2);
}
#12. grid to distance matrix (dx, dy)
ll w, h;
ll pos_to_idx(ll x, ll y){
return y * w + x;
}
pl idx_to_pos(ll idx){
return mp(idx % w, idx / w);
}
rep(y, h){
in1(s);
rep(x, w){
if (s[x] == '#') wall[x][y] = true;
else wall[x][y] = false;
}
}
rep(i1, h * w)rep(i2, h * w) dist[i1][i2] = INF * (i1 != i2);
rep(x, w)rep(y, h){
ll idx1 = pos_to_idx(x, y); ll idx2;
if (wall[x][y]) continue;
if (x != 0 && !wall[x - 1][y]){
idx2 = pos_to_idx(x - 1, y);
// if warshall floyd
dist[idx1][idx2] = 1;
// if dijkstra
// graph[idx1].pb({idx2, 1});
}
if (x != w - 1 && !wall[x + 1][y]){
idx2 = pos_to_idx(x + 1, y);
dist[idx1][idx2] = 1;
// graph[idx1].pb({idx2, 1});
}
if (y != 0 && !wall[x][y - 1]){
idx2 = pos_to_idx(x, y - 1);
dist[idx1][idx2] = 1;
// graph[idx1].pb({idx2, 1});
}
if (y != h - 1 && !wall[x][y + 1]){
idx2 = pos_to_idx(x, y + 1);
dist[idx1][idx2] = 1;
// graph[idx1].pb({idx2, 1});
}
}
# Cumulative sum (2 dimension)
ll func(ll x, ll y, ll dx, ll dy){
if (x + dx > w || y + dy > h) return - INF;
ll val = cum[x + dx][y + dy];
val += cum[x][y];
val -= cum[x][y + dy];
val -= cum[x + dx][y];
return val;
}
rep(x, w + 1) cum[x][0] = 0;
rep(y, h + 1) cum[0][y] = 0;
rep(y, h + 1) rep(x, w)
cum[x + 1][y + 1] = cum[x][y + 1] + vs[x][y];
rep(x, w + 1) rep(y, h)
cum[x][y + 1] += cum[x][y];
*/
/*
# the operators regarding bit
& (AND), | (OR), ^ (XOR)
- (REVERSE), >> (SMALLER SHIFT)
<< (BIGGER SHIFT)
x1: 0000 0001 0010 0101 0110 0111 0111
x2: xxxx 0001 0011 0100 0101 1000 0110
x1 & x2: 0000 0001 0010 0100 0100 0000 0110
x: 1001 1010 1100 1011 1101 1111
x & - x: 0001 0010 0100 0001 0001 0001
sum: 1010 1100 10000 1100 1100 10000
x << y is x * 2 ** y
x >> y is rep(i, y) x = x // 2
####### Attention #######
1 << i and 1LL << i is different. If programs show WA, try switch to this one.
Let S be a bit sequence and i be a non-negative integer
S & (1 << i) -> if true, i in S
S | (1 << i) -> S union {i}
S & -(1 << i) -> S - {i}
__builtin_popcount(S) -> the number of elements in S
S = 0 -> S is an empty set
__builtin_popcountl(i) -> the number of 1 in binary
S = (1 << n) - 1 -> S includes all the elements up to the n-th
#Conditional Operator
condition ? true : false;
#iterator
type declaration: auto
value reference: *itr
increment: itr++
decrement: itr--
substitution of value: *itr = smt
# inclusion-exclusion principle
|U[i = 1 to n] Ai| = sum[i = 1 to n] |Ai| - sum[i < j]|Ai ^ Aj| + ... + (-1)^(n
- 1) |^[i = 1 to n]Ai|
*/
const ll MAX_N = 200005;
bool okay = false;
ll answer = 0;
ll n;
vl as, ans, dp;
Graph tree[MAX_N];
void dfs(ll source, ll parent) {
ll v = as[source];
ll lb = lower_bound(all(dp), v) - dp.begin();
ll val = dp[lb];
dp[lb] = v;
ans[source] = lower_bound(all(dp), INF) - dp.begin();
for (auto e : tree[source])
if (e.to != parent)
dfs(e.to, source);
dp[lb] = val;
}
void solve() {
as = ans = vl(n);
rep(i, n) in1(as[i]);
rep(i, n - 1) {
ll a, b;
in2(a, b);
a--, b--;
tree[a].pb({b, 1});
tree[b].pb({a, 1});
}
dp = vl(n, INF);
dfs(0, -1);
rep(i, n) prl(ans[i]);
}
int main(void) {
in1(n);
// assert(n <= 9);
solve();
return 0;
}
| replace | 1,648 | 1,649 | 1,648 | 1,649 | TLE | |
p02698 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define rep(i, m, n) for (int(i) = (int)(m); i < (int)(n); i++)
#define rep2(i, m, n) for (int(i) = (int)(n)-1; i >= (int)(m); i--)
#define REP(i, n) rep(i, 0, n)
#define REP2(i, n) rep2(i, 0, n)
#define FOR(i, c) \
for (decltype((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define all(hoge) (hoge).begin(), (hoge).end()
#define en '\n'
using ll = long long;
using ull = unsigned long long;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;
typedef pair<ll, ll> P;
constexpr long long INF = 1LL << 60;
constexpr int INF_INT = 1 << 25;
constexpr long long MOD = (ll)1e9 + 7;
// constexpr long long MOD = 998244353LL;
typedef vector<ll> Array;
typedef vector<Array> Matrix;
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
struct Edge {
ll to, cap, rev;
Edge(ll _to, ll _cap, ll _rev) {
to = _to;
cap = _cap;
rev = _rev;
}
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;
void add_edge(Graph &G, ll from, ll to, ll cap, bool revFlag, ll revCap) {
G[from].push_back(Edge(to, cap, (ll)G[to].size()));
if (revFlag)
G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1));
}
void dfs(Graph &g, ll v, ll p, ll t, Array &dp, Array &ans, Array &a) {
auto it = lower_bound(dp.begin(), dp.begin() + t, a[v]);
bool flag = false;
P change = {-1, -1};
if (it - dp.begin() == t) {
change = {t, dp[t]};
dp[t] = a[v];
t++;
} else {
if (*it > a[v]) {
change = {it - dp.begin(), *it};
*it = a[v];
}
}
ans[v] = t;
Array temp = dp;
for (auto e : g[v]) {
if (e.to == p)
continue;
dfs(g, e.to, v, t, dp, ans, a);
}
if (change.first != -1) {
dp[change.first] = change.second;
}
}
void solve() {
ll n;
cin >> n;
Array a(n);
REP(i, n) cin >> a[i];
Graph g(n);
REP(i, n - 1) {
ll a, b;
cin >> a >> b;
a--;
b--;
add_edge(g, a, b, 1, true, 1);
}
Array ans(n, 0);
Array dp(n + 1, INF);
dp[0] = a[0];
dfs(g, 0, -1, 1, dp, ans, a);
for (auto i : ans)
cout << i << en;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
solve();
// ll t;cin>>t;REP(i,t) solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, m, n) for (int(i) = (int)(m); i < (int)(n); i++)
#define rep2(i, m, n) for (int(i) = (int)(n)-1; i >= (int)(m); i--)
#define REP(i, n) rep(i, 0, n)
#define REP2(i, n) rep2(i, 0, n)
#define FOR(i, c) \
for (decltype((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define all(hoge) (hoge).begin(), (hoge).end()
#define en '\n'
using ll = long long;
using ull = unsigned long long;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;
typedef pair<ll, ll> P;
constexpr long long INF = 1LL << 60;
constexpr int INF_INT = 1 << 25;
constexpr long long MOD = (ll)1e9 + 7;
// constexpr long long MOD = 998244353LL;
typedef vector<ll> Array;
typedef vector<Array> Matrix;
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
struct Edge {
ll to, cap, rev;
Edge(ll _to, ll _cap, ll _rev) {
to = _to;
cap = _cap;
rev = _rev;
}
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;
void add_edge(Graph &G, ll from, ll to, ll cap, bool revFlag, ll revCap) {
G[from].push_back(Edge(to, cap, (ll)G[to].size()));
if (revFlag)
G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1));
}
void dfs(Graph &g, ll v, ll p, ll t, Array &dp, Array &ans, Array &a) {
auto it = lower_bound(dp.begin(), dp.begin() + t, a[v]);
bool flag = false;
P change = {-1, -1};
if (it - dp.begin() == t) {
change = {t, dp[t]};
dp[t] = a[v];
t++;
} else {
if (*it > a[v]) {
change = {it - dp.begin(), *it};
*it = a[v];
}
}
ans[v] = t;
for (auto e : g[v]) {
if (e.to == p)
continue;
dfs(g, e.to, v, t, dp, ans, a);
}
if (change.first != -1) {
dp[change.first] = change.second;
}
}
void solve() {
ll n;
cin >> n;
Array a(n);
REP(i, n) cin >> a[i];
Graph g(n);
REP(i, n - 1) {
ll a, b;
cin >> a >> b;
a--;
b--;
add_edge(g, a, b, 1, true, 1);
}
Array ans(n, 0);
Array dp(n + 1, INF);
dp[0] = a[0];
dfs(g, 0, -1, 1, dp, ans, a);
for (auto i : ans)
cout << i << en;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
solve();
// ll t;cin>>t;REP(i,t) solve();
return 0;
}
| delete | 73 | 74 | 73 | 73 | TLE | |
p02698 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
#define REP(i, n) for (int i = 0; (i) < (n); ++(i))
#define FOR(i, n) for (int i = 1; (i) <= (n); ++(i))
#define dump(x) cout << #x << " = " << (x) << endl;
#define pb push_back
#define int long long
const int INF = 1e18;
const int MOD = 1e9 + 7;
// const lint LINF = 1e18;
const double eps = 0.000000001; // もとの値の10^(-16)まで
using namespace std;
// typedef pair<int, int> P;
// priority_queue< P, vector<P>, greater<P> >
// q;//ダイクストラの時、greaterで小さい順 cout << fixed << setprecision(10) <<
// ans << endl; int gcd(int a,int b){return b?gcd(b,a%b):a;}
// 最長増加部分列(Longest Increasing Subsequence) を求める
/*
signed main(){
MAX_N = 200000;
int lis_dp[MAX_N];
int n;
cin >> n;
REP(i,n){
cin >> a[i];
}
fill(lis_dp,lis_dp+n,INF);
REP(i,n){
*lower_bound(lis_dp,lis_dp+n,a[i]) = a[i];
}
int lis_ans = lower_bound(lis_dp,lis_dp+n,INF)-lis_dp;
cout << lis_ans << endl;
return 0;
}
*/
/*
lower_boundnのtips
探索したいkey以上のイテレータを返す
vector<int> a = {1, 4, 4, 7, 7, 8, 8, 11, 13, 19};
//イテレータを返す
auto Iter1 = lower_bound(ALL(a), 4);
auto Iter2 = lower_bound(ALL(a), 6);
auto Iter3 = lower_bound(ALL(a), 7);
auto Iter4 = lower_bound(ALL(a), 19);
auto Iter5 = lower_bound(ALL(a), 20);
//値の表示
cout << "----------value----------" << endl;
cout << "Iter1 = " << *Iter1 << endl; //Iter1 = 4
cout << "Iter2 = " << *Iter2 << endl; //Iter2 = 7
cout << "Iter3 = " << *Iter3 << endl; //Iter3 = 7
cout << "Iter4 = " << *Iter4 << endl; //Iter4 = 19
cout << "Iter5 = " << *Iter5 << endl; //Iter5 = 1326115891
//先頭からの距離
cout << "----------from----------" << endl;
cout << "Iter1 = " << Iter1 - a.begin() << endl; //Iter1 = 1
cout << "Iter2 = " << Iter2 - a.begin() << endl; //Iter2 = 3
cout << "Iter3 = " << Iter3 - a.begin() << endl; //Iter3 = 3
cout << "Iter4 = " << Iter4 - a.begin() << endl; //Iter4 = 9
cout << "Iter5 = " << Iter5 - a.begin() << endl; //Iter5 = 10
//末尾までの距離
cout << "----------to----------" << endl;
cout << "Iter1 = " << a.end() - Iter1 << endl; //Iter1 = 9
cout << "Iter2 = " << a.end() - Iter2 << endl; //Iter2 = 7
cout << "Iter3 = " << a.end() - Iter3 << endl; //Iter3 = 7
cout << "Iter4 = " << a.end() - Iter4 << endl; //Iter4 = 1
cout << "Iter5 = " << a.end() - Iter5 << endl; //Iter5 = 0
return 0;
*/
int n;
int a[30];
int lis_dp[30];
vector<int> edge[30];
int ans[30];
void dfs(int s, int p) {
int nowbasyo = lower_bound(lis_dp, lis_dp + n, a[s]) - lis_dp;
int nowatai = *lower_bound(lis_dp, lis_dp + n, a[s]);
*lower_bound(lis_dp, lis_dp + n, a[s]) = a[s];
ans[s] = lower_bound(lis_dp, lis_dp + n, INF) - lis_dp;
REP(i, edge[s].size()) {
if (edge[s][i] == p)
continue;
dfs(edge[s][i], s);
}
lis_dp[nowbasyo] = nowatai;
}
signed main() {
cin >> n;
REP(i, n) { cin >> a[i]; }
fill(lis_dp, lis_dp + n, INF);
REP(i, n - 1) {
int u1, v1;
cin >> u1 >> v1;
u1--;
v1--;
edge[u1].pb(v1);
edge[v1].pb(u1);
}
dfs(0, 0);
REP(i, n) {
cout << ans[i] << endl;
// cout << lis_dp[i] << endl;
}
return 0;
} | #include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
#define REP(i, n) for (int i = 0; (i) < (n); ++(i))
#define FOR(i, n) for (int i = 1; (i) <= (n); ++(i))
#define dump(x) cout << #x << " = " << (x) << endl;
#define pb push_back
#define int long long
const int INF = 1e18;
const int MOD = 1e9 + 7;
// const lint LINF = 1e18;
const double eps = 0.000000001; // もとの値の10^(-16)まで
using namespace std;
// typedef pair<int, int> P;
// priority_queue< P, vector<P>, greater<P> >
// q;//ダイクストラの時、greaterで小さい順 cout << fixed << setprecision(10) <<
// ans << endl; int gcd(int a,int b){return b?gcd(b,a%b):a;}
// 最長増加部分列(Longest Increasing Subsequence) を求める
/*
signed main(){
MAX_N = 200000;
int lis_dp[MAX_N];
int n;
cin >> n;
REP(i,n){
cin >> a[i];
}
fill(lis_dp,lis_dp+n,INF);
REP(i,n){
*lower_bound(lis_dp,lis_dp+n,a[i]) = a[i];
}
int lis_ans = lower_bound(lis_dp,lis_dp+n,INF)-lis_dp;
cout << lis_ans << endl;
return 0;
}
*/
/*
lower_boundnのtips
探索したいkey以上のイテレータを返す
vector<int> a = {1, 4, 4, 7, 7, 8, 8, 11, 13, 19};
//イテレータを返す
auto Iter1 = lower_bound(ALL(a), 4);
auto Iter2 = lower_bound(ALL(a), 6);
auto Iter3 = lower_bound(ALL(a), 7);
auto Iter4 = lower_bound(ALL(a), 19);
auto Iter5 = lower_bound(ALL(a), 20);
//値の表示
cout << "----------value----------" << endl;
cout << "Iter1 = " << *Iter1 << endl; //Iter1 = 4
cout << "Iter2 = " << *Iter2 << endl; //Iter2 = 7
cout << "Iter3 = " << *Iter3 << endl; //Iter3 = 7
cout << "Iter4 = " << *Iter4 << endl; //Iter4 = 19
cout << "Iter5 = " << *Iter5 << endl; //Iter5 = 1326115891
//先頭からの距離
cout << "----------from----------" << endl;
cout << "Iter1 = " << Iter1 - a.begin() << endl; //Iter1 = 1
cout << "Iter2 = " << Iter2 - a.begin() << endl; //Iter2 = 3
cout << "Iter3 = " << Iter3 - a.begin() << endl; //Iter3 = 3
cout << "Iter4 = " << Iter4 - a.begin() << endl; //Iter4 = 9
cout << "Iter5 = " << Iter5 - a.begin() << endl; //Iter5 = 10
//末尾までの距離
cout << "----------to----------" << endl;
cout << "Iter1 = " << a.end() - Iter1 << endl; //Iter1 = 9
cout << "Iter2 = " << a.end() - Iter2 << endl; //Iter2 = 7
cout << "Iter3 = " << a.end() - Iter3 << endl; //Iter3 = 7
cout << "Iter4 = " << a.end() - Iter4 << endl; //Iter4 = 1
cout << "Iter5 = " << a.end() - Iter5 << endl; //Iter5 = 0
return 0;
*/
int n;
int a[300000];
int lis_dp[300000];
vector<int> edge[300000];
int ans[300000];
void dfs(int s, int p) {
int nowbasyo = lower_bound(lis_dp, lis_dp + n, a[s]) - lis_dp;
int nowatai = *lower_bound(lis_dp, lis_dp + n, a[s]);
*lower_bound(lis_dp, lis_dp + n, a[s]) = a[s];
ans[s] = lower_bound(lis_dp, lis_dp + n, INF) - lis_dp;
REP(i, edge[s].size()) {
if (edge[s][i] == p)
continue;
dfs(edge[s][i], s);
}
lis_dp[nowbasyo] = nowatai;
}
signed main() {
cin >> n;
REP(i, n) { cin >> a[i]; }
fill(lis_dp, lis_dp + n, INF);
REP(i, n - 1) {
int u1, v1;
cin >> u1 >> v1;
u1--;
v1--;
edge[u1].pb(v1);
edge[v1].pb(u1);
}
dfs(0, 0);
REP(i, n) {
cout << ans[i] << endl;
// cout << lis_dp[i] << endl;
}
return 0;
} | replace | 89 | 93 | 89 | 93 | 0 | |
p02698 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 1e18;
int N;
vector<int> g[100100];
ll a[200100];
ll dp[200100];
ll ans[200100];
void solve(int u, int pa = -1) {
int ind = lower_bound(dp, dp + N, a[u]) - dp;
ll prev = dp[ind];
dp[ind] = a[u];
/*
for(int i=0;i<N;i++){
if(dp[i]==inf) cout << "inf" << " ";
else cout << dp[i] << " ";
}
cout << endl;
*/
ans[u] = lower_bound(dp, dp + N, inf) - dp;
for (auto v : g[u]) {
if (v == pa)
continue;
solve(v, u);
}
dp[ind] = prev;
}
int main() {
cin >> N;
for (int i = 0; i < N; i++)
cin >> a[i];
for (int i = 0; i < N - 1; i++) {
int u, v;
cin >> u >> v;
u--;
v--;
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 0; i < N; i++) {
dp[i] = inf;
}
solve(0);
for (int i = 0; i < N; i++) {
cout << ans[i] << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 1e18;
int N;
vector<int> g[200100];
ll a[200100];
ll dp[200100];
ll ans[200100];
void solve(int u, int pa = -1) {
int ind = lower_bound(dp, dp + N, a[u]) - dp;
ll prev = dp[ind];
dp[ind] = a[u];
/*
for(int i=0;i<N;i++){
if(dp[i]==inf) cout << "inf" << " ";
else cout << dp[i] << " ";
}
cout << endl;
*/
ans[u] = lower_bound(dp, dp + N, inf) - dp;
for (auto v : g[u]) {
if (v == pa)
continue;
solve(v, u);
}
dp[ind] = prev;
}
int main() {
cin >> N;
for (int i = 0; i < N; i++)
cin >> a[i];
for (int i = 0; i < N - 1; i++) {
int u, v;
cin >> u >> v;
u--;
v--;
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 0; i < N; i++) {
dp[i] = inf;
}
solve(0);
for (int i = 0; i < N; i++) {
cout << ans[i] << endl;
}
} | replace | 6 | 7 | 6 | 7 | 0 | |
p02698 | C++ | Runtime Error | #include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/detail/standard_policies.hpp>
// using namespace __gnu_pbds;
using namespace std;
// template <typename T>
// using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,
// tree_order_statistics_node_update>;
typedef long long int ll;
const ll N = 1e5 + 9;
const ll p = 31;
// const ll m = 1e9 + 7;
const ll inf = 1e14;
const ll mod = 1e9 + 7;
#define x first
#define y second
#define pb push_back
#define mp make_pair
using pii = pair<ll, ll>;
ll powm(ll a, ll b) {
a = a % mod;
ll res = 1;
while (b) {
if (b & 1)
res = (res * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return res;
}
std::vector<ll> adj[N];
ll t[4 * N], lis[N], ans[N];
map<ll, ll> comp;
ll a[N], b[N];
void build(ll v, ll tl, ll tr) {
if (tl == tr) {
t[v] = lis[tl];
} else {
ll tm = (tl + tr) / 2;
build(2 * v, tl, tm);
build(2 * v + 1, tm + 1, tr);
t[v] = max(t[2 * v], t[2 * v + 1]);
}
}
ll query(ll v, ll tl, ll tr, ll l, ll r) {
if (l > r) {
return 0;
}
if (l == tl && r == tr) {
return t[v];
}
ll tm = (tl + tr) / 2;
return max(query(2 * v, tl, tm, l, min(tm, r)),
query(2 * v + 1, tm + 1, tr, max(tm + 1, l), r));
}
void update(ll v, ll tl, ll tr, ll pos, ll val) {
if (tl == tr) {
t[v] = val;
} else {
ll tm = (tl + tr) / 2;
if (pos <= tm) {
update(2 * v, tl, tm, pos, val);
} else {
update(2 * v + 1, tm + 1, tr, pos, val);
}
t[v] = max(t[2 * v], t[2 * v + 1]);
}
}
void dfs(ll v, ll p, ll ct) {
ll cur = query(1, 0, ct, comp[a[v]], comp[a[v]]);
ll ma = query(1, 0, ct, 0, comp[a[v]] - 1);
update(1, 0, ct, comp[a[v]], ma + 1);
ll an = query(1, 0, ct, 0, ct);
// cout<<v<<" "<<an<<" "<<ma<<'\n';
ans[v] = an;
for (auto u : adj[v]) {
if (u != p) {
dfs(u, v, ct);
}
}
update(1, 0, ct, comp[a[v]], cur);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
memset(lis, 0, sizeof(lis));
ll T = 1;
// cin >> T;
while (T--) {
ll n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
b[i] = a[i];
}
for (int i = 0; i < n - 1; i++) {
ll u, v;
cin >> u >> v;
u--;
v--;
adj[u].pb(v);
adj[v].pb(u);
}
ll ct = 0;
sort(b, b + n);
comp[b[0]] = ct++;
for (int i = 1; i < n; i++) {
// cout<<b[i]<<'\n';
if (b[i] != b[i - 1]) {
comp[b[i]] = ct++;
}
}
build(1, 0, ct);
dfs(0, -1, ct);
// update(1,0,ct,0,4);
// cout<<query(1,0,ct,0,ct)<<'\n';
for (int i = 0; i < n; i++) {
cout << ans[i] << " ";
}
cout << '\n';
}
} | #include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/detail/standard_policies.hpp>
// using namespace __gnu_pbds;
using namespace std;
// template <typename T>
// using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,
// tree_order_statistics_node_update>;
typedef long long int ll;
const ll N = 2e5 + 9;
const ll p = 31;
// const ll m = 1e9 + 7;
const ll inf = 1e14;
const ll mod = 1e9 + 7;
#define x first
#define y second
#define pb push_back
#define mp make_pair
using pii = pair<ll, ll>;
ll powm(ll a, ll b) {
a = a % mod;
ll res = 1;
while (b) {
if (b & 1)
res = (res * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return res;
}
std::vector<ll> adj[N];
ll t[4 * N], lis[N], ans[N];
map<ll, ll> comp;
ll a[N], b[N];
void build(ll v, ll tl, ll tr) {
if (tl == tr) {
t[v] = lis[tl];
} else {
ll tm = (tl + tr) / 2;
build(2 * v, tl, tm);
build(2 * v + 1, tm + 1, tr);
t[v] = max(t[2 * v], t[2 * v + 1]);
}
}
ll query(ll v, ll tl, ll tr, ll l, ll r) {
if (l > r) {
return 0;
}
if (l == tl && r == tr) {
return t[v];
}
ll tm = (tl + tr) / 2;
return max(query(2 * v, tl, tm, l, min(tm, r)),
query(2 * v + 1, tm + 1, tr, max(tm + 1, l), r));
}
void update(ll v, ll tl, ll tr, ll pos, ll val) {
if (tl == tr) {
t[v] = val;
} else {
ll tm = (tl + tr) / 2;
if (pos <= tm) {
update(2 * v, tl, tm, pos, val);
} else {
update(2 * v + 1, tm + 1, tr, pos, val);
}
t[v] = max(t[2 * v], t[2 * v + 1]);
}
}
void dfs(ll v, ll p, ll ct) {
ll cur = query(1, 0, ct, comp[a[v]], comp[a[v]]);
ll ma = query(1, 0, ct, 0, comp[a[v]] - 1);
update(1, 0, ct, comp[a[v]], ma + 1);
ll an = query(1, 0, ct, 0, ct);
// cout<<v<<" "<<an<<" "<<ma<<'\n';
ans[v] = an;
for (auto u : adj[v]) {
if (u != p) {
dfs(u, v, ct);
}
}
update(1, 0, ct, comp[a[v]], cur);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
memset(lis, 0, sizeof(lis));
ll T = 1;
// cin >> T;
while (T--) {
ll n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
b[i] = a[i];
}
for (int i = 0; i < n - 1; i++) {
ll u, v;
cin >> u >> v;
u--;
v--;
adj[u].pb(v);
adj[v].pb(u);
}
ll ct = 0;
sort(b, b + n);
comp[b[0]] = ct++;
for (int i = 1; i < n; i++) {
// cout<<b[i]<<'\n';
if (b[i] != b[i - 1]) {
comp[b[i]] = ct++;
}
}
build(1, 0, ct);
dfs(0, -1, ct);
// update(1,0,ct,0,4);
// cout<<query(1,0,ct,0,ct)<<'\n';
for (int i = 0; i < n; i++) {
cout << ans[i] << " ";
}
cout << '\n';
}
} | replace | 11 | 12 | 11 | 12 | -11 | |
p02698 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define fine cout << "fine" << endl;
#define ll long long int
#define pb push_back
#define FASTIO \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define endl "\n"
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
const ll inf = (ll)2e18;
const int mod = 1e9 + 7;
bool overflow(ll a, ll b) {
if (a <= (inf + b - 1) / b)
return false;
return true;
}
const int NAX = 2e5 + 5;
int n, a[NAX];
int ans[NAX];
vector<vector<int>> dp;
int bSearch(int x) {
int start = 0, last = dp.size() - 1, mid, ans = -1;
while (start <= last) {
mid = (start + last) / 2;
if (dp[mid][dp[mid].size() - 1] < x)
start = mid + 1;
else {
ans = mid;
last = mid - 1;
}
}
return ans;
}
map<int, int> m;
stack<int> s;
void dfs(vector<vector<int>> &v, int i, int par) {
int ind = bSearch(a[i]);
if (ind == -1) {
dp.pb({mod, a[i]});
m[i] = dp.size() - 1;
ans[i] = dp.size();
s.push(ans[i]);
} else {
ans[i] = max(s.top(), ind + 1);
s.push(ans[i]);
dp[ind].pb(a[i]);
}
for (auto it : v[i]) {
if (it != par) {
dfs(v, it, i);
}
}
dp[m[i]].pop_back();
s.pop();
}
int main() {
FASTIO;
cin >> n;
vector<vector<int>> v(n + 1);
dp.reserve(n + 1);
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 0; i < n - 1; i++) {
int x, y;
cin >> x >> y;
v[x].pb(y);
v[y].pb(x);
}
dfs(v, 1, -1);
for (int i = 1; i <= n; i++) {
cout << ans[i] << endl;
}
}
| #include <bits/stdc++.h>
using namespace std;
#define fine cout << "fine" << endl;
#define ll long long int
#define pb push_back
#define FASTIO \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define endl "\n"
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
const ll inf = (ll)2e18;
const int mod = 1e9 + 7;
bool overflow(ll a, ll b) {
if (a <= (inf + b - 1) / b)
return false;
return true;
}
const int NAX = 2e5 + 5;
int n, a[NAX];
int ans[NAX];
vector<vector<int>> dp;
int bSearch(int x) {
int start = 0, last = dp.size() - 1, mid, ans = -1;
while (start <= last) {
mid = (start + last) / 2;
if (dp[mid][dp[mid].size() - 1] < x)
start = mid + 1;
else {
ans = mid;
last = mid - 1;
}
}
return ans;
}
map<int, int> m;
stack<int> s;
void dfs(vector<vector<int>> &v, int i, int par) {
int ind = bSearch(a[i]);
if (ind == -1) {
dp.pb({mod, a[i]});
m[i] = dp.size() - 1;
ans[i] = dp.size();
s.push(ans[i]);
} else {
ans[i] = max(s.top(), ind + 1);
m[i] = ind;
s.push(ans[i]);
dp[ind].pb(a[i]);
}
for (auto it : v[i]) {
if (it != par) {
dfs(v, it, i);
}
}
dp[m[i]].pop_back();
s.pop();
}
int main() {
FASTIO;
cin >> n;
vector<vector<int>> v(n + 1);
dp.reserve(n + 1);
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 0; i < n - 1; i++) {
int x, y;
cin >> x >> y;
v[x].pb(y);
v[y].pb(x);
}
dfs(v, 1, -1);
for (int i = 1; i <= n; i++) {
cout << ans[i] << endl;
}
}
| insert | 48 | 48 | 48 | 49 | 0 | |
p02698 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int n;
int a[N];
vector<int> edges[N];
struct SegTree {
int maxi[N];
void update(int node, int l, int r, int x, int val) {
if (l > x || r < x)
return;
if (l == x && r == x) {
maxi[node] = val;
return;
}
int mid = (l + r) >> 1;
update(node * 2 + 1, l, mid + 0, x, val);
update(node * 2 + 2, mid + 1, r, x, val);
maxi[node] = max(maxi[node * 2 + 1], maxi[node * 2 + 2]);
}
int query(int node, int l, int r, int ll, int rr) {
if (l > rr || r < ll)
return 0;
if (l >= ll && r <= rr)
return maxi[node];
int mid = (l + r) >> 1;
return max(query(node * 2 + 1, l, mid + 0, ll, rr),
query(node * 2 + 2, mid + 1, r, ll, rr));
}
inline void update(int x, int val) { update(0, 0, n - 1, x, val); }
inline int query(int l, int r) { return query(0, 0, n - 1, l, r); }
} tree;
void compress(int n, int a[]) {
vector<int> v(a, a + n);
sort(v.begin(), v.end());
v.resize(unique(v.begin(), v.end()) - v.begin());
for (int i = 0; i < n; ++i) {
a[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin();
}
}
int ans[N];
void dfs(int u, int p, int maxi) {
int tq = tree.query(a[u], a[u]);
int nq = tree.query(0, a[u] - 1) + 1;
if (nq > tq)
tree.update(a[u], nq);
maxi = max(maxi, nq);
ans[u] = maxi;
for (int v : edges[u]) {
if (v == p)
continue;
dfs(v, u, maxi);
}
if (nq > tq)
tree.update(a[u], tq);
}
int solve() {
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
}
compress(n, a);
for (int i = 1; i < n; ++i) {
int u, v;
scanf("%d %d", &u, &v);
--u, --v;
edges[u].push_back(v);
edges[v].push_back(u);
}
dfs(0, 0, 0);
for (int i = 0; i < n; ++i) {
printf("%d\n", ans[i]);
}
return 0;
}
int main() {
int t = 1;
// scanf("%d", &t);
while (t--)
solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int n;
int a[N];
vector<int> edges[N];
struct SegTree {
int maxi[N << 2];
void update(int node, int l, int r, int x, int val) {
if (l > x || r < x)
return;
if (l == x && r == x) {
maxi[node] = val;
return;
}
int mid = (l + r) >> 1;
update(node * 2 + 1, l, mid + 0, x, val);
update(node * 2 + 2, mid + 1, r, x, val);
maxi[node] = max(maxi[node * 2 + 1], maxi[node * 2 + 2]);
}
int query(int node, int l, int r, int ll, int rr) {
if (l > rr || r < ll)
return 0;
if (l >= ll && r <= rr)
return maxi[node];
int mid = (l + r) >> 1;
return max(query(node * 2 + 1, l, mid + 0, ll, rr),
query(node * 2 + 2, mid + 1, r, ll, rr));
}
inline void update(int x, int val) { update(0, 0, n - 1, x, val); }
inline int query(int l, int r) { return query(0, 0, n - 1, l, r); }
} tree;
void compress(int n, int a[]) {
vector<int> v(a, a + n);
sort(v.begin(), v.end());
v.resize(unique(v.begin(), v.end()) - v.begin());
for (int i = 0; i < n; ++i) {
a[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin();
}
}
int ans[N];
void dfs(int u, int p, int maxi) {
int tq = tree.query(a[u], a[u]);
int nq = tree.query(0, a[u] - 1) + 1;
if (nq > tq)
tree.update(a[u], nq);
maxi = max(maxi, nq);
ans[u] = maxi;
for (int v : edges[u]) {
if (v == p)
continue;
dfs(v, u, maxi);
}
if (nq > tq)
tree.update(a[u], tq);
}
int solve() {
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
}
compress(n, a);
for (int i = 1; i < n; ++i) {
int u, v;
scanf("%d %d", &u, &v);
--u, --v;
edges[u].push_back(v);
edges[v].push_back(u);
}
dfs(0, 0, 0);
for (int i = 0; i < n; ++i) {
printf("%d\n", ans[i]);
}
return 0;
}
int main() {
int t = 1;
// scanf("%d", &t);
while (t--)
solve();
return 0;
}
| replace | 11 | 12 | 11 | 12 | 0 | |
p02698 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
using ll = long long;
const int INF = 1e9;
typedef pair<int, int> P;
const int mx = 200000;
vector<int> v[mx];
int an[mx], a[mx];
int n;
int pp[mx];
int dfs(int from, int now, int pc) {
// cout<<from<<" "<<now<<" "<<pc<<" ";
auto it = lower_bound(pp, pp + mx, a[now]);
int pc2 = max(pc, (int)(it - pp));
// cout<<pc2<<" ";
int bf = *it;
// cout<<bf<<endl;
*it = a[now];
an[now] = pc2;
for (int x : v[now]) {
if (x == from)
continue;
dfs(now, x, pc2);
}
*it = bf;
}
int main() {
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 0; i < n - 1; i++) {
int s, b;
cin >> s >> b;
s--;
b--;
v[s].push_back(b);
v[b].push_back(s);
}
fill(pp, pp + mx, INF);
dfs(-1, 0, 0);
for (int i = 0; i < n; i++) {
cout << an[i] + 1 << endl;
}
} | #include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
using ll = long long;
const int INF = 1e9;
typedef pair<int, int> P;
const int mx = 200000;
vector<int> v[mx];
int an[mx], a[mx];
int n;
int pp[mx];
void dfs(int from, int now, int pc) {
// cout<<from<<" "<<now<<" "<<pc<<" ";
auto it = lower_bound(pp, pp + mx, a[now]);
int pc2 = max(pc, (int)(it - pp));
// cout<<pc2<<" ";
int bf = *it;
// cout<<bf<<endl;
*it = a[now];
an[now] = pc2;
for (int x : v[now]) {
if (x == from)
continue;
dfs(now, x, pc2);
}
*it = bf;
}
int main() {
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 0; i < n - 1; i++) {
int s, b;
cin >> s >> b;
s--;
b--;
v[s].push_back(b);
v[b].push_back(s);
}
fill(pp, pp + mx, INF);
dfs(-1, 0, 0);
for (int i = 0; i < n; i++) {
cout << an[i] + 1 << endl;
}
} | replace | 17 | 18 | 17 | 18 | 0 | |
p02698 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
// x^y≡x^(y%(p-1))mod p
// but there is a condition that x needs to be coprime to p
/// assic value of ('0'-'9') is(48 - 57) and (a-z) is (97-122) and (A-Z)
/// is(65-90) and 32 for space
typedef long long int ll;
typedef pair<ll, ll> pii;
typedef vector<pii> vii;
#define pb push_back
#define f(i, a, b) for (ll i = a; i < b; i++)
#define fo(i, a, b) for (ll i = a; i <= b; i += 1)
#define rf(i, a, b) for (ll i = a; i >= b; i--)
#define vll vector<ll>
#define tests() \
int test_cases; \
cin >> test_cases; \
while (test_cases--)
#define ub upper_bound
#define lb lower_bound
#define all(v) v.begin(), v.end()
#define MAXN 100010
#define MOD 1000000007
#define mod 998244353
#define deb(x) cout << '>' << #x << ':' << x << endl;
#define sp " "
#define MS(x, y) fill_n(*x, sizeof x / sizeof **x, y);
#define mem(x, y) memset(x, y, sizeof(x));
#define INF 1000000000000000000
ll power(ll x, ll y, ll p) {
ll res = 1; // Initialize result
x = x % p;
if (y == 0)
return 1;
if (x == 0)
return 0;
while (y > 0) {
if (y & 1)
res = (res * x) % p;
y = y >> 1; // y = y/2
x = (x * x) % p; // Change x to x^2
}
return res;
}
int gcd(int a, int b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
ll modinv(ll a, ll p) { return power(a, p - 2, p); }
vll adj[2 * MAXN];
ll LIS[MAXN];
vll a;
vll dp;
void dfs(ll s, ll p) {
auto it = lower_bound(all(dp), a[s]) - dp.begin();
ll sz = dp.size();
ll store;
if (it != sz) {
store = dp[it];
dp[it] = a[s];
} else {
dp.pb(a[s]);
}
LIS[s] = dp.size();
for (auto v : adj[s]) {
if (v == p) {
continue;
}
dfs(v, s);
}
if (it != sz) {
dp[it] = store;
} else {
dp.resize(sz);
}
return;
}
int solve() {
ll n;
cin >> n;
a.resize(n + 1);
f(i, 0, n) { cin >> a[i + 1]; }
f(i, 0, n - 1) {
ll a, b;
cin >> a >> b;
adj[a].pb(b);
adj[b].pb(a);
}
dfs(1, 1);
fo(i, 1, n) { cout << LIS[i] << endl; }
return 0;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
// tests(){solve();}
solve();
return 0;
} /*#ifndef ONLINE_JUDGE
#endif*/
// make_pair make_tuple tuple<int,int,int> | #include <bits/stdc++.h>
using namespace std;
// x^y≡x^(y%(p-1))mod p
// but there is a condition that x needs to be coprime to p
/// assic value of ('0'-'9') is(48 - 57) and (a-z) is (97-122) and (A-Z)
/// is(65-90) and 32 for space
typedef long long int ll;
typedef pair<ll, ll> pii;
typedef vector<pii> vii;
#define pb push_back
#define f(i, a, b) for (ll i = a; i < b; i++)
#define fo(i, a, b) for (ll i = a; i <= b; i += 1)
#define rf(i, a, b) for (ll i = a; i >= b; i--)
#define vll vector<ll>
#define tests() \
int test_cases; \
cin >> test_cases; \
while (test_cases--)
#define ub upper_bound
#define lb lower_bound
#define all(v) v.begin(), v.end()
#define MAXN 100010
#define MOD 1000000007
#define mod 998244353
#define deb(x) cout << '>' << #x << ':' << x << endl;
#define sp " "
#define MS(x, y) fill_n(*x, sizeof x / sizeof **x, y);
#define mem(x, y) memset(x, y, sizeof(x));
#define INF 1000000000000000000
ll power(ll x, ll y, ll p) {
ll res = 1; // Initialize result
x = x % p;
if (y == 0)
return 1;
if (x == 0)
return 0;
while (y > 0) {
if (y & 1)
res = (res * x) % p;
y = y >> 1; // y = y/2
x = (x * x) % p; // Change x to x^2
}
return res;
}
int gcd(int a, int b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
ll modinv(ll a, ll p) { return power(a, p - 2, p); }
vll adj[2 * MAXN];
ll LIS[2 * MAXN];
vll a;
vll dp;
void dfs(ll s, ll p) {
auto it = lower_bound(all(dp), a[s]) - dp.begin();
ll sz = dp.size();
ll store;
if (it != sz) {
store = dp[it];
dp[it] = a[s];
} else {
dp.pb(a[s]);
}
LIS[s] = dp.size();
for (auto v : adj[s]) {
if (v == p) {
continue;
}
dfs(v, s);
}
if (it != sz) {
dp[it] = store;
} else {
dp.resize(sz);
}
return;
}
int solve() {
ll n;
cin >> n;
a.resize(n + 1);
f(i, 0, n) { cin >> a[i + 1]; }
f(i, 0, n - 1) {
ll a, b;
cin >> a >> b;
adj[a].pb(b);
adj[b].pb(a);
}
dfs(1, 1);
fo(i, 1, n) { cout << LIS[i] << endl; }
return 0;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
// tests(){solve();}
solve();
return 0;
} /*#ifndef ONLINE_JUDGE
#endif*/
// make_pair make_tuple tuple<int,int,int> | replace | 52 | 53 | 52 | 53 | -6 | terminate called after throwing an instance of 'std::length_error'
what(): vector::_M_default_append
|
p02698 | C++ | Runtime Error | #include <iostream>
#include <vector>
using namespace std;
int a[2 * 100000];
int lis[2 * 100000];
void dfs(const vector<vector<int>> &edges, int node, vector<int> *state) {
vector<int>::iterator it = lower_bound(state->begin(), state->end(), a[node]);
bool extend = it == state->end();
int original;
if (extend) {
state->push_back(a[node]);
} else {
original = *it;
*it = a[node];
}
lis[node] = state->size();
for (int i = 0; i < edges[node].size(); ++i) {
int n = edges[node][i];
if (lis[n] == -1) {
dfs(edges, n, state);
}
}
if (extend) {
state->pop_back();
} else {
*it = original;
}
}
int main() {
int N;
cin >> N;
for (int i = 0; i < N; ++i) {
cin >> a[i];
}
vector<vector<int>> edges;
edges.resize(N);
for (int i = 0; i < N - 1; ++i) {
int u, v;
cin >> u >> v;
edges[u - 1].push_back(v - 1);
edges[v - 1].push_back(u - 1);
}
for (int i = 0; i < N; ++i) {
lis[i] = -1;
}
vector<int> state;
dfs(edges, 0, &state);
for (int i = 0; i < N; ++i) {
cout << lis[i] << endl;
}
} | #include <iostream>
#include <vector>
using namespace std;
int a[2 * 100000];
int lis[2 * 100000];
void dfs(const vector<vector<int>> &edges, int node, vector<int> *state) {
vector<int>::iterator it = lower_bound(state->begin(), state->end(), a[node]);
bool extend = it == state->end();
int original;
if (extend) {
state->push_back(a[node]);
} else {
original = *it;
*it = a[node];
}
lis[node] = state->size();
for (int i = 0; i < edges[node].size(); ++i) {
int n = edges[node][i];
if (lis[n] == -1) {
dfs(edges, n, state);
}
}
if (extend) {
state->pop_back();
} else {
*it = original;
}
}
int main() {
int N;
cin >> N;
for (int i = 0; i < N; ++i) {
cin >> a[i];
}
vector<vector<int>> edges;
edges.resize(N);
for (int i = 0; i < N - 1; ++i) {
int u, v;
cin >> u >> v;
edges[u - 1].push_back(v - 1);
edges[v - 1].push_back(u - 1);
}
for (int i = 0; i < N; ++i) {
lis[i] = -1;
}
vector<int> state;
state.reserve(N);
dfs(edges, 0, &state);
for (int i = 0; i < N; ++i) {
cout << lis[i] << endl;
}
} | insert | 52 | 52 | 52 | 53 | 0 | |
p02698 | C++ | Time Limit Exceeded | #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define INF 1000000001
struct node {
int a;
vector<int> to;
int ans;
};
stack<pair<int, int>> s;
void dfs(int v, int p, vector<node> &nodes, vector<int> &dp) {
int changed =
distance(dp.begin(), lower_bound(dp.begin(), dp.end(), nodes[v].a));
s.push(make_pair(changed, dp[changed]));
dp[changed] = nodes[v].a;
for (int i = 0; i < nodes[v].to.size(); i++) {
int v_to = nodes[v].to[i];
if (v_to == p)
continue;
dfs(v_to, v, nodes, dp);
}
nodes[v].ans =
(int)distance(dp.begin(), lower_bound(dp.begin(), dp.end(), INF));
dp[s.top().first] = s.top().second;
s.pop();
}
int main() {
int N;
cin >> N;
vector<node> nodes(N);
vector<int> dp(N, INF);
for (int i = 0; i < N; i++)
cin >> nodes[i].a;
for (int i = 0; i < N - 1; i++) {
int u, v;
cin >> u >> v;
nodes[u - 1].to.push_back(v - 1);
nodes[v - 1].to.push_back(u - 1);
}
dfs(0, -1, nodes, dp);
for (int i = 0; i < N; i++) {
cout << nodes[i].ans << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
#define INF 1000000001
struct node {
int a;
vector<int> to;
int ans;
};
stack<pair<int, int>> s;
void dfs(int v, int p, vector<node> &nodes, vector<int> &dp) {
int changed =
distance(dp.begin(), lower_bound(dp.begin(), dp.end(), nodes[v].a));
s.push(make_pair(changed, dp[changed]));
dp[changed] = nodes[v].a;
for (int i = 0; i < nodes[v].to.size(); i++) {
int v_to = nodes[v].to[i];
if (v_to == p)
continue;
dfs(v_to, v, nodes, dp);
}
nodes[v].ans =
(int)distance(dp.begin(), lower_bound(dp.begin(), dp.end(), INF));
dp[s.top().first] = s.top().second;
s.pop();
}
int main() {
int N;
cin >> N;
vector<node> nodes(N);
vector<int> dp(N, INF);
for (int i = 0; i < N; i++)
cin >> nodes[i].a;
for (int i = 0; i < N - 1; i++) {
int u, v;
cin >> u >> v;
nodes[u - 1].to.push_back(v - 1);
nodes[v - 1].to.push_back(u - 1);
}
dfs(0, -1, nodes, dp);
for (int i = 0; i < N; i++) {
cout << nodes[i].ans << endl;
}
} | delete | 0 | 1 | 0 | 0 | TLE | |
p02698 | C++ | Runtime Error | #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define fi first
#define se second
constexpr int MAXN = 1 << 18;
vector<int> grafo[MAXN];
int A[MAXN];
int res[MAXN];
vector<int> lis;
void dfs(int v, int u) {
auto it = lower_bound(lis.begin(), lis.end(), A[v]);
int prev = -1;
if (it == lis.end()) {
lis.push_back(A[v]);
} else {
prev = *it;
*it = A[v];
}
res[v] = lis.size();
for (int i : grafo[v]) {
if (i != u)
dfs(i, v);
}
if (prev == -1) {
lis.pop_back();
} else {
*it = prev;
}
}
int main() {
ios::sync_with_stdio(false);
int N;
cin >> N;
for (int i = 1; i <= N; i++) {
cin >> A[i];
}
for (int i = 0; i < N - 1; i++) {
int u, v;
cin >> u >> v;
grafo[u].push_back(v);
grafo[v].push_back(u);
}
dfs(1, 0);
for (int i = 1; i <= N; i++) {
cout << res[i] << '\n';
}
return 0;
}
| #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define fi first
#define se second
constexpr int MAXN = 1 << 18;
vector<int> grafo[MAXN];
int A[MAXN];
int res[MAXN];
vector<int> lis;
void dfs(int v, int u) {
auto it = lower_bound(lis.begin(), lis.end(), A[v]);
int prev = -1;
if (it == lis.end()) {
lis.push_back(A[v]);
} else {
prev = *it;
*it = A[v];
}
res[v] = lis.size();
for (int i : grafo[v]) {
if (i != u)
dfs(i, v);
}
if (prev == -1) {
lis.pop_back();
} else {
*it = prev;
}
}
int main() {
ios::sync_with_stdio(false);
int N;
cin >> N;
for (int i = 1; i <= N; i++) {
cin >> A[i];
}
for (int i = 0; i < N - 1; i++) {
int u, v;
cin >> u >> v;
grafo[u].push_back(v);
grafo[v].push_back(u);
}
lis.reserve(N);
dfs(1, 0);
for (int i = 1; i <= N; i++) {
cout << res[i] << '\n';
}
return 0;
}
| insert | 50 | 50 | 50 | 51 | 0 | |
p02698 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); ++i)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define fi first
#define se second
#define INF 1000000009
#define LLINF 1000000000000000009LL
#define MAX_N 100009
using ll = long long;
vector<int> a;
vector<int> to[MAX_N];
int dp[MAX_N];
int ans[MAX_N];
void dfs(int v, int p = -1) {
int i = lower_bound(dp, dp + MAX_N, a[v]) - dp;
int old = dp[i];
dp[i] = a[v];
ans[v] = i;
if (p != -1)
ans[v] = max(ans[v], ans[p]);
for (auto u : to[v]) {
if (u == p)
continue;
dfs(u, v);
}
dp[i] = old;
}
int main() {
int n;
cin >> n;
a = vector<int>(n);
rep(i, n) cin >> a[i];
rep(i, n - 1) {
int s, t;
cin >> s >> t;
s--;
t--;
to[s].push_back(t);
to[t].push_back(s);
}
rep(i, MAX_N) dp[i] = INF;
dp[0] = -INF;
dfs(0);
rep(i, n) { cout << ans[i] << endl; }
return (0);
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); ++i)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define fi first
#define se second
#define INF 1000000009
#define LLINF 1000000000000000009LL
#define MAX_N 200009
using ll = long long;
vector<int> a;
vector<int> to[MAX_N];
int dp[MAX_N];
int ans[MAX_N];
void dfs(int v, int p = -1) {
int i = lower_bound(dp, dp + MAX_N, a[v]) - dp;
int old = dp[i];
dp[i] = a[v];
ans[v] = i;
if (p != -1)
ans[v] = max(ans[v], ans[p]);
for (auto u : to[v]) {
if (u == p)
continue;
dfs(u, v);
}
dp[i] = old;
}
int main() {
int n;
cin >> n;
a = vector<int>(n);
rep(i, n) cin >> a[i];
rep(i, n - 1) {
int s, t;
cin >> s >> t;
s--;
t--;
to[s].push_back(t);
to[t].push_back(s);
}
rep(i, MAX_N) dp[i] = INF;
dp[0] = -INF;
dfs(0);
rep(i, n) { cout << ans[i] << endl; }
return (0);
}
| replace | 10 | 11 | 10 | 11 | 0 | |
p02698 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> vec;
typedef vector<vec> mat;
int r[200001] = {0};
ll inf = 1e18;
vec G[200001], A(200001), dp(200001, inf), ans(200001);
int dfs(int x) {
if (r[x])
return 0;
r[x] = 1;
ll a = lower_bound(all(dp), A[x]) - dp.begin(),
b = *lower_bound(all(dp), A[x]);
*lower_bound(all(dp), A[x]) = A[x];
ans[x] = lower_bound(all(dp), inf) - dp.begin();
for (auto i : G[x])
dfs(i);
dp[a] = b;
}
int main() {
int n, u, v;
cin >> n;
rep(i, n) cin >> A[i + 1];
rep(i, n - 1) cin >> u >> v, G[u].push_back(v), G[v].push_back(u);
dfs(1);
rep(i, n) cout << ans[i + 1] << "\n";
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> vec;
typedef vector<vec> mat;
int r[200001] = {0};
ll inf = 1e18;
vec G[200001], A(200001), dp(200001, inf), ans(200001);
int dfs(int x) {
if (r[x])
return 0;
r[x] = 1;
ll a = lower_bound(all(dp), A[x]) - dp.begin(),
b = *lower_bound(all(dp), A[x]);
*lower_bound(all(dp), A[x]) = A[x];
ans[x] = lower_bound(all(dp), inf) - dp.begin();
for (auto i : G[x])
dfs(i);
dp[a] = b;
return 0;
}
int main() {
int n, u, v;
cin >> n;
rep(i, n) cin >> A[i + 1];
rep(i, n - 1) cin >> u >> v, G[u].push_back(v), G[v].push_back(u);
dfs(1);
rep(i, n) cout << ans[i + 1] << "\n";
} | insert | 22 | 22 | 22 | 23 | 0 | |
p02698 | C++ | Runtime Error | /* Simplicity and Goodness */
#include <bits/stdc++.h>
using namespace std;
#define scn(n) scanf("%d", &n)
#define lscn(n) scanf("%lld", &n)
#define lpri(n) printf("%lld ", n)
#define pri(n) printf("%d ", (int)n)
#define prin(n) printf("%d\n", (int)n)
#define lprin(n) printf("%lld\n", n)
#define deb(x) cout << #x << ' ' << x << '\n'
#define rep(i, ini, n) for (int i = ini; i < (int)n; i++)
#define per(i, ini) for (int i = ini; i >= 0; i--)
#define tc \
int tt; \
scn(tt); \
while (tt--)
#define pb push_back
#define mp make_pair
#define F first
#define S second
using ll = long long;
const int inf = INT_MAX;
const int ninf = INT_MIN;
const int mod = 998244353;
const int N = 1e5 + 2;
vector<int> v[N];
set<int> s;
int val[N], ans[N], removed[N];
void dfs(int node, int par) {
auto i = s.lower_bound(val[node]);
if (i != s.end()) {
removed[node] = *i;
s.erase(*i);
} else
removed[node] = -1;
s.insert(val[node]);
ans[node] = (int)s.size();
for (int i : v[node])
if (i != par)
dfs(i, node);
s.erase(val[node]);
if (removed[node] != -1)
s.insert(removed[node]);
}
int main() {
int n;
scn(n);
rep(i, 1, n + 1) scn(val[i]);
rep(i, 1, n) {
int a, b;
scn(a);
scn(b);
v[a].pb(b);
v[b].pb(a);
}
dfs(1, -1);
rep(i, 1, n + 1) prin(ans[i]);
return 0;
} | /* Simplicity and Goodness */
#include <bits/stdc++.h>
using namespace std;
#define scn(n) scanf("%d", &n)
#define lscn(n) scanf("%lld", &n)
#define lpri(n) printf("%lld ", n)
#define pri(n) printf("%d ", (int)n)
#define prin(n) printf("%d\n", (int)n)
#define lprin(n) printf("%lld\n", n)
#define deb(x) cout << #x << ' ' << x << '\n'
#define rep(i, ini, n) for (int i = ini; i < (int)n; i++)
#define per(i, ini) for (int i = ini; i >= 0; i--)
#define tc \
int tt; \
scn(tt); \
while (tt--)
#define pb push_back
#define mp make_pair
#define F first
#define S second
using ll = long long;
const int inf = INT_MAX;
const int ninf = INT_MIN;
const int mod = 998244353;
const int N = 2e5 + 2;
vector<int> v[N];
set<int> s;
int val[N], ans[N], removed[N];
void dfs(int node, int par) {
auto i = s.lower_bound(val[node]);
if (i != s.end()) {
removed[node] = *i;
s.erase(*i);
} else
removed[node] = -1;
s.insert(val[node]);
ans[node] = (int)s.size();
for (int i : v[node])
if (i != par)
dfs(i, node);
s.erase(val[node]);
if (removed[node] != -1)
s.insert(removed[node]);
}
int main() {
int n;
scn(n);
rep(i, 1, n + 1) scn(val[i]);
rep(i, 1, n) {
int a, b;
scn(a);
scn(b);
v[a].pb(b);
v[b].pb(a);
}
dfs(1, -1);
rep(i, 1, n + 1) prin(ans[i]);
return 0;
} | replace | 25 | 26 | 25 | 26 | 0 | |
p02698 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
/*---------------------DEBUGGING--------------------------------------------*/
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V> void __print(const pair<T, V> &x) {
cerr << '{';
__print(x.first);
cerr << ',';
__print(x.second);
cerr << '}';
}
template <typename T> void __print(const T &x) {
int f = 0;
cerr << '{';
for (auto &i : x)
cerr << (f++ ? "," : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
__print(t);
if (sizeof...(v))
cerr << ", ";
_print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...) \
cerr << "[" << #x << "] = ["; \
_print(x)
#else
#define debug(x...)
#endif
/*-------------------------------------------------------------------------------------*/
// #define mp make_pair
#define pb push_back
#define ll long long
#define pii pair<int, int>
#define pcc pair<char, char>
#define F first
#define S second
#define int long long
#define pi 3.141592653589793238462643383279502
#define M 1000000007
#define rep(i, a, n) for (int i = a; i < n; i++)
#define INF 10000000000000
#define N 200005
#define vi vector<int>
#define all(v) v.begin(), v.end()
// #define ordered_set tree<int, null_type,less<int>,
// rb_tree_tag,tree_order_statistics_node_update>
vi adj[N];
int n;
int a[N];
int ans[N];
multiset<int> s;
int dfs(int u, int par) {
s.insert(a[u]);
auto it = s.lower_bound(a[u]);
it++;
int p = -1;
if (it != s.end()) {
p = *(it);
s.erase(it);
}
ans[u] = s.size();
for (auto child : adj[u]) {
if (child != par)
dfs(child, u);
}
if (p != -1) {
s.insert(p);
}
s.erase(s.find(a[u]));
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
rep(i, 1, n + 1) { cin >> a[i]; }
rep(i, 0, n - 1) {
int u, v;
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
dfs(1, 0);
for (int i = 1; i <= n; i++)
cout << ans[i] << endl;
}
| #include <bits/stdc++.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
/*---------------------DEBUGGING--------------------------------------------*/
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V> void __print(const pair<T, V> &x) {
cerr << '{';
__print(x.first);
cerr << ',';
__print(x.second);
cerr << '}';
}
template <typename T> void __print(const T &x) {
int f = 0;
cerr << '{';
for (auto &i : x)
cerr << (f++ ? "," : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
__print(t);
if (sizeof...(v))
cerr << ", ";
_print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...) \
cerr << "[" << #x << "] = ["; \
_print(x)
#else
#define debug(x...)
#endif
/*-------------------------------------------------------------------------------------*/
// #define mp make_pair
#define pb push_back
#define ll long long
#define pii pair<int, int>
#define pcc pair<char, char>
#define F first
#define S second
#define int long long
#define pi 3.141592653589793238462643383279502
#define M 1000000007
#define rep(i, a, n) for (int i = a; i < n; i++)
#define INF 10000000000000
#define N 200005
#define vi vector<int>
#define all(v) v.begin(), v.end()
// #define ordered_set tree<int, null_type,less<int>,
// rb_tree_tag,tree_order_statistics_node_update>
vi adj[N];
int n;
int a[N];
int ans[N];
multiset<int> s;
void dfs(int u, int par) {
s.insert(a[u]);
auto it = s.lower_bound(a[u]);
it++;
int p = -1;
if (it != s.end()) {
p = *(it);
s.erase(it);
}
ans[u] = s.size();
for (auto child : adj[u]) {
if (child != par)
dfs(child, u);
}
if (p != -1) {
s.insert(p);
}
s.erase(s.find(a[u]));
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
rep(i, 1, n + 1) { cin >> a[i]; }
rep(i, 0, n - 1) {
int u, v;
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
dfs(1, 0);
for (int i = 1; i <= n; i++)
cout << ans[i] << endl;
}
| replace | 74 | 75 | 74 | 75 | 0 | |
p02698 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll, ll>
#define iii pair<ii, ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << " is " << x << endl;
#define rep(x, start, end) \
for (auto x = (start) - ((start) > (end)); x != (end) - ((start) > (end)); \
((start) < (end) ? x++ : x--))
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
ll MAX(ll a) { return a; }
ll MIN(ll a) { return a; }
template <typename... Args> ll MAX(ll a, Args... args) {
return max(a, MAX(args...));
}
template <typename... Args> ll MIN(ll a, Args... args) {
return min(a, MIN(args...));
}
#define indexed_set \
tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
int n;
int w[200005];
vector<int> al[100005];
int ans[200005];
vector<int> lis{-1};
vector<ii> changes;
//(-1,-1) is insert new element
//(pos,val) is overwrite pos which used to contain val
void dfs(int i, int p) {
if (lis.back() < w[i]) {
lis.push_back(w[i]);
changes.push_back(ii(-1, -1));
} else {
int iter = lower_bound(all(lis), w[i]) - lis.begin();
changes.push_back(ii(iter, lis[iter]));
lis[iter] = w[i];
}
ans[i] = lis.size();
for (auto &it : al[i]) {
if (it == p)
continue;
dfs(it, i);
}
if (changes.back() == ii(-1, -1))
lis.pop_back();
else {
lis[changes.back().fi] = changes.back().se;
}
changes.pop_back();
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
rep(x, 1, n + 1) cin >> w[x];
int a, b;
rep(x, 1, n) {
cin >> a >> b;
al[a].push_back(b);
al[b].push_back(a);
}
dfs(1, -1);
rep(x, 1, n + 1) cout << ans[x] - 1 << endl;
}
| #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll, ll>
#define iii pair<ii, ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << " is " << x << endl;
#define rep(x, start, end) \
for (auto x = (start) - ((start) > (end)); x != (end) - ((start) > (end)); \
((start) < (end) ? x++ : x--))
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
ll MAX(ll a) { return a; }
ll MIN(ll a) { return a; }
template <typename... Args> ll MAX(ll a, Args... args) {
return max(a, MAX(args...));
}
template <typename... Args> ll MIN(ll a, Args... args) {
return min(a, MIN(args...));
}
#define indexed_set \
tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
int n;
int w[200005];
vector<int> al[200005];
int ans[200005];
vector<int> lis{-1};
vector<ii> changes;
//(-1,-1) is insert new element
//(pos,val) is overwrite pos which used to contain val
void dfs(int i, int p) {
if (lis.back() < w[i]) {
lis.push_back(w[i]);
changes.push_back(ii(-1, -1));
} else {
int iter = lower_bound(all(lis), w[i]) - lis.begin();
changes.push_back(ii(iter, lis[iter]));
lis[iter] = w[i];
}
ans[i] = lis.size();
for (auto &it : al[i]) {
if (it == p)
continue;
dfs(it, i);
}
if (changes.back() == ii(-1, -1))
lis.pop_back();
else {
lis[changes.back().fi] = changes.back().se;
}
changes.pop_back();
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
rep(x, 1, n + 1) cin >> w[x];
int a, b;
rep(x, 1, n) {
cin >> a >> b;
al[a].push_back(b);
al[b].push_back(a);
}
dfs(1, -1);
rep(x, 1, n + 1) cout << ans[x] - 1 << endl;
}
| replace | 35 | 36 | 35 | 36 | 0 | |
p02698 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int sz = 1e5 + 5;
typedef pair<int, int> pii;
#define x first
#define y second
pii a[sz];
vector<int> g[sz];
int tree[sz * 4], ans[sz];
void update(int node, int b, int e, int x, int v) {
if (x < b || e < x)
return;
if (b == e) {
tree[node] += v;
return;
}
int l = node << 1, r = l + 1, m = b + e >> 1;
update(l, b, m, x, v);
update(r, m + 1, e, x, v);
tree[node] = max(tree[l], tree[r]);
}
int segq(int node, int b, int e, int i, int j) {
if (i > j || e < i || j < b)
return 0;
if (i <= b && e <= j)
return tree[node];
int l = node << 1, r = l + 1, m = b + e >> 1;
return max(segq(l, b, m, i, j), segq(r, m + 1, e, i, j));
}
int heavy[sz], depth[sz], head[sz], par[sz], pos[sz], cpos;
int dfs(int u = 1) {
int subc = 1, mxc = 0;
for (int v : g[u])
if (v ^ par[u]) {
par[v] = u;
depth[v] = depth[u] + 1;
int nxtc = dfs(v);
subc += nxtc;
if (nxtc > mxc)
mxc = nxtc, heavy[u] = v;
}
return subc;
}
void hfs(int u = 1, int h = 1) {
head[u] = h, pos[u] = ++cpos;
int hv = heavy[u];
if (hv)
hfs(hv, h);
for (int v : g[u])
if (v ^ par[u] && v ^ hv)
hfs(v, v);
}
int hldq(int u, int v, int n) {
int ans = 0;
while (head[u] ^ head[v]) {
if (depth[head[u]] < depth[head[v]])
swap(u, v);
ans = max(ans, segq(1, 1, n, pos[head[u]], pos[u]));
u = par[head[u]];
}
if (depth[u] > depth[v])
swap(u, v);
return max(ans, segq(1, 1, n, pos[u], pos[v]));
}
void go(int u = 1, int p = 0) {
for (int v : g[u])
if (v ^ p) {
ans[v] = max(ans[v], ans[u]);
go(v, u);
}
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++)
scanf("%d", &a[i].x), a[i].y = i;
sort(a + 1, a + n + 1);
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d %d", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
}
dfs();
hfs();
for (int i = 1; i <= n;) {
vector<int> now;
for (int j = i; i <= n && a[i].x == a[j].x; i++) {
int u = a[i].y;
now.push_back(u);
ans[u] = hldq(1, u, n) + 1;
}
for (int u : now)
update(1, 1, n, pos[u], ans[u]);
}
go();
for (int i = 1; i <= n; i++)
printf("%d\n", ans[i]);
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int sz = 2e5 + 5;
typedef pair<int, int> pii;
#define x first
#define y second
pii a[sz];
vector<int> g[sz];
int tree[sz * 4], ans[sz];
void update(int node, int b, int e, int x, int v) {
if (x < b || e < x)
return;
if (b == e) {
tree[node] += v;
return;
}
int l = node << 1, r = l + 1, m = b + e >> 1;
update(l, b, m, x, v);
update(r, m + 1, e, x, v);
tree[node] = max(tree[l], tree[r]);
}
int segq(int node, int b, int e, int i, int j) {
if (i > j || e < i || j < b)
return 0;
if (i <= b && e <= j)
return tree[node];
int l = node << 1, r = l + 1, m = b + e >> 1;
return max(segq(l, b, m, i, j), segq(r, m + 1, e, i, j));
}
int heavy[sz], depth[sz], head[sz], par[sz], pos[sz], cpos;
int dfs(int u = 1) {
int subc = 1, mxc = 0;
for (int v : g[u])
if (v ^ par[u]) {
par[v] = u;
depth[v] = depth[u] + 1;
int nxtc = dfs(v);
subc += nxtc;
if (nxtc > mxc)
mxc = nxtc, heavy[u] = v;
}
return subc;
}
void hfs(int u = 1, int h = 1) {
head[u] = h, pos[u] = ++cpos;
int hv = heavy[u];
if (hv)
hfs(hv, h);
for (int v : g[u])
if (v ^ par[u] && v ^ hv)
hfs(v, v);
}
int hldq(int u, int v, int n) {
int ans = 0;
while (head[u] ^ head[v]) {
if (depth[head[u]] < depth[head[v]])
swap(u, v);
ans = max(ans, segq(1, 1, n, pos[head[u]], pos[u]));
u = par[head[u]];
}
if (depth[u] > depth[v])
swap(u, v);
return max(ans, segq(1, 1, n, pos[u], pos[v]));
}
void go(int u = 1, int p = 0) {
for (int v : g[u])
if (v ^ p) {
ans[v] = max(ans[v], ans[u]);
go(v, u);
}
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++)
scanf("%d", &a[i].x), a[i].y = i;
sort(a + 1, a + n + 1);
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d %d", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
}
dfs();
hfs();
for (int i = 1; i <= n;) {
vector<int> now;
for (int j = i; i <= n && a[i].x == a[j].x; i++) {
int u = a[i].y;
now.push_back(u);
ans[u] = hldq(1, u, n) + 1;
}
for (int u : now)
update(1, 1, n, pos[u], ans[u]);
}
go();
for (int i = 1; i <= n; i++)
printf("%d\n", ans[i]);
}
| replace | 3 | 4 | 3 | 4 | 0 | |
p02698 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
long long N;
long long A[200005];
vector<long long> tree[200005];
long long dp[100005];
long long ans[100005];
// dp[i]:= 長さが i+1
// であるような増加部分列における最終要素の最小値(存在しない場合は INF)
void dfs(long long pa, long long cur) {
// cout << "pa " << pa << " cur " << cur << endl;
long long num = A[cur];
auto it = lower_bound(dp, dp + N, num);
long long old = *it;
*it = num;
ans[cur] = lower_bound(dp, dp + N, LONG_MAX) - dp;
for (long long i = 0; i < tree[cur].size(); i++) {
if (tree[cur][i] != pa) {
dfs(cur, tree[cur][i]);
}
}
*it = old;
}
int main() {
cin >> N;
for (long long i = 0; i < N; i++) {
cin >> A[i];
}
for (long long i = 0; i < N - 1; i++) {
long long u, v;
cin >> u >> v;
u--;
v--;
tree[u].push_back(v);
tree[v].push_back(u);
}
fill(dp, dp + N, LONG_MAX);
dfs(-1, 0);
for (long long i = 0; i < N; i++) {
cout << ans[i] << endl;
}
}
| #include <bits/stdc++.h>
using namespace std;
long long N;
long long A[200005];
vector<long long> tree[200005];
long long dp[200005];
long long ans[200005];
// dp[i]:= 長さが i+1
// であるような増加部分列における最終要素の最小値(存在しない場合は INF)
void dfs(long long pa, long long cur) {
// cout << "pa " << pa << " cur " << cur << endl;
long long num = A[cur];
auto it = lower_bound(dp, dp + N, num);
long long old = *it;
*it = num;
ans[cur] = lower_bound(dp, dp + N, LONG_MAX) - dp;
for (long long i = 0; i < tree[cur].size(); i++) {
if (tree[cur][i] != pa) {
dfs(cur, tree[cur][i]);
}
}
*it = old;
}
int main() {
cin >> N;
for (long long i = 0; i < N; i++) {
cin >> A[i];
}
for (long long i = 0; i < N - 1; i++) {
long long u, v;
cin >> u >> v;
u--;
v--;
tree[u].push_back(v);
tree[v].push_back(u);
}
fill(dp, dp + N, LONG_MAX);
dfs(-1, 0);
for (long long i = 0; i < N; i++) {
cout << ans[i] << endl;
}
}
| replace | 6 | 8 | 6 | 8 | 0 | |
p02698 | C++ | Time Limit Exceeded | #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define FOR_LT(i, beg, end) for (int i = (int)(beg); i < (int)(end); i++)
#define FOR_LE(i, beg, end) for (int i = (int)(beg); i <= (int)(end); i++)
#define FOR_DW(i, beg, end) for (int i = (int)(beg); (int)(end) <= i; i--)
#define REP(n) for (int repeat_index = 0; repeat_index < (int)n; repeat_index++)
using namespace std;
void dfs(vector<int> &as, vector<vector<int>> &edge, vector<bool> &iss,
vector<int> dp, vector<int> &ans, int n, int p, int cur) {
auto it = std::lower_bound(dp.begin(), dp.end(), as[p]);
int v = (it - dp.begin());
int pre = dp[v];
if (as[p] < dp[v]) {
dp[v] = as[p];
}
cur = max(cur, v);
ans[p] = cur;
iss[p] = true;
for (auto b : edge[p]) {
if (iss[b])
continue;
dfs(as, edge, iss, dp, ans, n, b, cur);
}
dp[v] = pre;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << fixed << setprecision(20);
int n;
cin >> n;
vector<int> as(n + 1);
FOR_LE(i, 1, n) { cin >> as[i]; }
vector<int> dp(n + 1);
FOR_LE(i, 1, n) { dp[i] = INT_MAX / 2; }
dp[0] = -1;
vector<vector<int>> edge(n + 1);
REP(n - 1) {
int a, b;
cin >> a >> b;
edge[a].push_back(b);
edge[b].push_back(a);
}
vector<bool> iss(n + 1);
vector<int> ans(n + 1);
dfs(as, edge, iss, dp, ans, n, 1, 0);
FOR_LE(i, 1, n) { cout << ans[i] << endl; }
return 0;
}
| #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define FOR_LT(i, beg, end) for (int i = (int)(beg); i < (int)(end); i++)
#define FOR_LE(i, beg, end) for (int i = (int)(beg); i <= (int)(end); i++)
#define FOR_DW(i, beg, end) for (int i = (int)(beg); (int)(end) <= i; i--)
#define REP(n) for (int repeat_index = 0; repeat_index < (int)n; repeat_index++)
using namespace std;
void dfs(vector<int> &as, vector<vector<int>> &edge, vector<bool> &iss,
vector<int> &dp, vector<int> &ans, int n, int p, int cur) {
auto it = std::lower_bound(dp.begin(), dp.end(), as[p]);
int v = (it - dp.begin());
int pre = dp[v];
if (as[p] < dp[v]) {
dp[v] = as[p];
}
cur = max(cur, v);
ans[p] = cur;
iss[p] = true;
for (auto b : edge[p]) {
if (iss[b])
continue;
dfs(as, edge, iss, dp, ans, n, b, cur);
}
dp[v] = pre;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << fixed << setprecision(20);
int n;
cin >> n;
vector<int> as(n + 1);
FOR_LE(i, 1, n) { cin >> as[i]; }
vector<int> dp(n + 1);
FOR_LE(i, 1, n) { dp[i] = INT_MAX / 2; }
dp[0] = -1;
vector<vector<int>> edge(n + 1);
REP(n - 1) {
int a, b;
cin >> a >> b;
edge[a].push_back(b);
edge[b].push_back(a);
}
vector<bool> iss(n + 1);
vector<int> ans(n + 1);
dfs(as, edge, iss, dp, ans, n, 1, 0);
FOR_LE(i, 1, n) { cout << ans[i] << endl; }
return 0;
}
| replace | 34 | 35 | 34 | 35 | TLE | |
p02698 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, x, n) for (int i = x; i <= n; i++)
#define rep3(i, x, n) for (int i = x; i >= n; i--)
#define elif else if
#define sp(x) fixed << setprecision(x)
#define pb(x) push_back(x)
#define all(x) x.begin(), x.end()
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const int inf = 1e9;
const ll INF = 1e18;
const ld EPS = 1e-10;
const int MAX_V = 1e5;
vector<int> es[MAX_V];
ll dp[MAX_V];
int ans[MAX_V];
ll A[MAX_V];
void dfs(int now, int pre) {
int j = lower_bound(dp, dp + MAX_V, A[now]) - dp;
ll memo = dp[j];
dp[j] = A[now];
ans[now] = lower_bound(dp, dp + MAX_V, INF) - dp;
for (auto &e : es[now]) {
if (e == pre)
continue;
dfs(e, now);
}
dp[j] = memo;
}
int main() {
int N;
cin >> N;
rep(i, N) cin >> A[i];
rep(i, N - 1) {
int a, b;
cin >> a >> b;
a--, b--;
es[a].pb(b), es[b].pb(a);
}
fill(dp, dp + MAX_V, INF);
dfs(0, -1);
rep(i, N) { cout << ans[i] << endl; }
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, x, n) for (int i = x; i <= n; i++)
#define rep3(i, x, n) for (int i = x; i >= n; i--)
#define elif else if
#define sp(x) fixed << setprecision(x)
#define pb(x) push_back(x)
#define all(x) x.begin(), x.end()
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const int inf = 1e9;
const ll INF = 1e18;
const ld EPS = 1e-10;
const int MAX_V = 2e5;
vector<int> es[MAX_V];
ll dp[MAX_V];
int ans[MAX_V];
ll A[MAX_V];
void dfs(int now, int pre) {
int j = lower_bound(dp, dp + MAX_V, A[now]) - dp;
ll memo = dp[j];
dp[j] = A[now];
ans[now] = lower_bound(dp, dp + MAX_V, INF) - dp;
for (auto &e : es[now]) {
if (e == pre)
continue;
dfs(e, now);
}
dp[j] = memo;
}
int main() {
int N;
cin >> N;
rep(i, N) cin >> A[i];
rep(i, N - 1) {
int a, b;
cin >> a >> b;
a--, b--;
es[a].pb(b), es[b].pb(a);
}
fill(dp, dp + MAX_V, INF);
dfs(0, -1);
rep(i, N) { cout << ans[i] << endl; }
} | replace | 21 | 22 | 21 | 22 | 0 | |
p02698 | C++ | Runtime Error | #include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace __gnu_pbds;
using namespace __gnu_cxx;
using namespace std;
using ll = long long;
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define len(v) ((int)v.size())
#define all(v) v.begin(), v.end()
#define precision(n) fixed << setprecision(n)
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
void io() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
template <typename T>
using oset =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T>
using MinPriorityQueue = priority_queue<T, vector<T>, greater<T>>;
template <typename T> void done(T ans) {
cout << ans << "\n";
exit(0);
}
template <typename A> istream &operator>>(istream &input, vector<A> &x) {
for (auto &i : x)
input >> i;
return input;
}
template <typename A> ostream &operator<<(ostream &output, vector<A> &x) {
for (auto &i : x)
output << i << ' ';
return output;
}
const int mod = 1e9;
const int N = 3e5 + 5;
auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
mt19937_64 mt(seed);
template <typename T> long long myrand(T m) {
long long mod = m;
long long x = mt() % mod;
return x;
}
int a[N];
vector<int> ans(N);
vector<int> adj[N];
multiset<int> s;
void dfs(int node, int parent = 0) {
s.insert(a[node]);
int del = -1;
// auto it = s.upper_bound(a[node]);
auto it = s.lower_bound(a[node]);
if (++it != s.end()) {
del = *it;
s.erase(it);
}
ans[node] = s.size();
for (auto child : adj[node])
if (child != parent)
dfs(child, node);
if (del != -1)
s.insert(del);
s.erase(s.find(a[node]));
}
vector<int> lis;
void dfs2(int node, int parent = 0) {
auto it = lower_bound(lis.begin(), lis.end(), a[node]);
int del = -1;
if (it == lis.end()) {
del = 1;
lis.push_back(a[node]);
} else {
del = *it;
*it = a[node];
}
ans[node - 1] = lis.size();
for (auto child : adj[node])
if (child != parent)
dfs2(child, node);
if (del == 1)
lis.pop_back();
else
*it = del;
}
void dfs3(int node, int parent = -1) {
int k = lower_bound(lis.begin(), lis.end(), a[node]) - lis.begin();
int x = lis[k];
lis[k] = a[node];
ans[node - 1] = lower_bound(lis.begin(), lis.end(), INT_MAX) - lis.begin();
for (int v : adj[node]) {
if (v != parent) {
dfs3(v, node);
}
}
lis[k] = x;
}
int main() {
io();
int n, m, q;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
// lis.assign(n + 4, INT_MAX);
dfs3(1);
ans.resize(n);
cout << ans << " ";
// while(q--) {
// int x;
// cin >> x;
// cout << ans[x] << "\n";
// }
}
| #include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace __gnu_pbds;
using namespace __gnu_cxx;
using namespace std;
using ll = long long;
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define len(v) ((int)v.size())
#define all(v) v.begin(), v.end()
#define precision(n) fixed << setprecision(n)
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
void io() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
template <typename T>
using oset =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T>
using MinPriorityQueue = priority_queue<T, vector<T>, greater<T>>;
template <typename T> void done(T ans) {
cout << ans << "\n";
exit(0);
}
template <typename A> istream &operator>>(istream &input, vector<A> &x) {
for (auto &i : x)
input >> i;
return input;
}
template <typename A> ostream &operator<<(ostream &output, vector<A> &x) {
for (auto &i : x)
output << i << ' ';
return output;
}
const int mod = 1e9;
const int N = 3e5 + 5;
auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
mt19937_64 mt(seed);
template <typename T> long long myrand(T m) {
long long mod = m;
long long x = mt() % mod;
return x;
}
int a[N];
vector<int> ans(N);
vector<int> adj[N];
multiset<int> s;
void dfs(int node, int parent = 0) {
s.insert(a[node]);
int del = -1;
// auto it = s.upper_bound(a[node]);
auto it = s.lower_bound(a[node]);
if (++it != s.end()) {
del = *it;
s.erase(it);
}
ans[node] = s.size();
for (auto child : adj[node])
if (child != parent)
dfs(child, node);
if (del != -1)
s.insert(del);
s.erase(s.find(a[node]));
}
vector<int> lis;
void dfs2(int node, int parent = 0) {
auto it = lower_bound(lis.begin(), lis.end(), a[node]);
int del = -1;
if (it == lis.end()) {
del = 1;
lis.push_back(a[node]);
} else {
del = *it;
*it = a[node];
}
ans[node - 1] = lis.size();
for (auto child : adj[node])
if (child != parent)
dfs2(child, node);
if (del == 1)
lis.pop_back();
else
*it = del;
}
void dfs3(int node, int parent = -1) {
int k = lower_bound(lis.begin(), lis.end(), a[node]) - lis.begin();
int x = lis[k];
lis[k] = a[node];
ans[node - 1] = lower_bound(lis.begin(), lis.end(), INT_MAX) - lis.begin();
for (int v : adj[node]) {
if (v != parent) {
dfs3(v, node);
}
}
lis[k] = x;
}
int main() {
io();
int n, m, q;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
lis.assign(n + 4, INT_MAX);
dfs3(1);
ans.resize(n);
cout << ans << " ";
// while(q--) {
// int x;
// cin >> x;
// cout << ans[x] << "\n";
// }
}
| replace | 144 | 145 | 144 | 145 | -11 | |
p02698 | C++ | Time Limit Exceeded | #include "bits/stdc++.h"
using namespace std;
using ll = long long;
#define rep(i, begin, n) for (int i = begin; i < n; i++)
#define repe(i, begin, n) for (int i = begin; i <= n; i++)
#define repr(i, begin, n) for (int i = begin; i > begin - n; i--)
#define repre(i, begin, end) for (int i = begin; i >= end; i--)
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 int inf = 1000000007;
const int MOD = 1000000007;
const long long INF = 1000000000000000007;
// -------------------------------------------------------
int N;
int a[201010];
vector<int> T[201010];
int dp[201010];
using P = pair<int, int>;
stack<P> S;
int ans[201010];
bool used[201010];
void dfs(int cur) {
int n = a[cur];
auto lb = lower_bound(dp, dp + N, n);
int idx = lb - dp;
int prev_value = *lb;
S.push(make_pair(idx, prev_value));
*lb = n;
for (int i = 0; i < N; i++) {
if (dp[i] != inf) {
ans[cur] = i + 1;
} else {
break;
}
}
used[cur] = true;
auto node = T[cur];
for (int i = 0; i < node.size(); i++) {
if (used[node[i]]) {
continue;
}
dfs(node[i]);
P last_op = S.top();
S.pop();
dp[last_op.first] = last_op.second;
}
used[cur] = false;
}
int main() {
rep(i, 0, 201000) { dp[i] = inf; }
cin >> N;
rep(i, 0, N) { cin >> a[i]; }
rep(i, 0, N - 1) {
int u, v;
cin >> u >> v;
u--;
v--;
T[u].push_back(v);
T[v].push_back(u);
}
dfs(0);
rep(i, 0, N) { cout << ans[i] << endl; }
}
| #include "bits/stdc++.h"
using namespace std;
using ll = long long;
#define rep(i, begin, n) for (int i = begin; i < n; i++)
#define repe(i, begin, n) for (int i = begin; i <= n; i++)
#define repr(i, begin, n) for (int i = begin; i > begin - n; i--)
#define repre(i, begin, end) for (int i = begin; i >= end; i--)
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 int inf = 1000000007;
const int MOD = 1000000007;
const long long INF = 1000000000000000007;
// -------------------------------------------------------
int N;
int a[201010];
vector<int> T[201010];
int dp[201010];
using P = pair<int, int>;
stack<P> S;
int ans[201010];
bool used[201010];
void dfs(int cur) {
int n = a[cur];
auto lb = lower_bound(dp, dp + N, n);
int idx = lb - dp;
int prev_value = *lb;
S.push(make_pair(idx, prev_value));
*lb = n;
auto lb_inf = lower_bound(dp, dp + N, inf);
int inf_idx = lb_inf - dp;
ans[cur] = inf_idx;
used[cur] = true;
auto node = T[cur];
for (int i = 0; i < node.size(); i++) {
if (used[node[i]]) {
continue;
}
dfs(node[i]);
P last_op = S.top();
S.pop();
dp[last_op.first] = last_op.second;
}
used[cur] = false;
}
int main() {
rep(i, 0, 201000) { dp[i] = inf; }
cin >> N;
rep(i, 0, N) { cin >> a[i]; }
rep(i, 0, N - 1) {
int u, v;
cin >> u >> v;
u--;
v--;
T[u].push_back(v);
T[v].push_back(u);
}
dfs(0);
rep(i, 0, N) { cout << ans[i] << endl; }
}
| replace | 48 | 55 | 48 | 51 | TLE | |
p02698 | C++ | Runtime Error | #include <bits/stdc++.h>
// ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
// clock_t start=clock();clock_t
// end=clock();cout<<(double)(end-start)/CLOCKS_PER_SEC<<endl;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef pair<int, int> pii;
typedef pair<pii, int> ppii;
typedef pair<int, pii> pipi;
typedef pair<ll, ll> pll;
typedef pair<pll, ll> ppll;
typedef pair<ll, pll> plpl;
typedef pair<pii, pii> P;
typedef tuple<ll, ll, ll> tl;
typedef pair<double, double> pdd;
typedef vector<vector<ll>> mat;
ll mod = 1000000007;
ll mod2 = 998244353;
ll mod3 = 1000003;
ll mod4 = 998244853;
ll mod5 = 1000000009;
ll inf = 1LL << 62;
double pi = 3.141592653589793238462643383279L;
double eps = 1e-14;
#define rep(i, m, n) for (int i = m; i < n; i++)
#define rrep(i, n, m) for (ll i = n; i >= m; i--)
#define srep(itr, st) for (auto itr = st.begin(); itr != st.end(); itr++)
#define mrep(itr, mp) for (auto &itr : mp)
#define Max(a, b) a = max(a, b)
#define Min(a, b) a = min(a, b)
int dh[4] = {1, -1, 0, 0};
int dw[4] = {0, 0, 1, -1};
int ddh[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
int ddw[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
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);
}
};
#define umh unordered_map<ll, ll, custom_hash>
ll gcd(ll a, ll b) {
if (a < b)
swap(a, b);
if (b == 0)
return a;
if (a % b == 0)
return b;
return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
ll c = gcd(a, b);
return a * b / c;
}
ll Pow(ll n, ll k) {
ll ret = 1;
ll now = n;
while (k > 0) {
if (k & 1)
ret *= now;
now *= now;
k /= 2;
}
return ret;
}
ll beki(ll n, ll k, ll md) {
ll ret = 1;
ll now = n;
now %= md;
while (k > 0) {
if (k % 2 == 1) {
ret *= now;
ret %= md;
}
now *= now;
now %= md;
k /= 2;
}
return ret;
}
ll gyaku(ll n, ll md) { return beki(n, md - 2, md); }
ll popcount(ll n) {
ll ret = 0;
ll u = n;
while (u > 0) {
ret += u % 2;
u /= 2;
}
return ret;
}
int a[200010];
vector<int> v[200010];
int ans[200010];
deque<int> dq;
void dfs(int now, int par) {
int m = (int)dq.size();
int u = m, d = -1;
while (u - d > 1) {
int mid = (u + d) / 2;
if (dq[mid] >= a[now])
u = mid;
else
d = mid;
}
int pre = -1;
if (u == m) {
ans[now] = u + 1;
dq.push_back(a[now]);
} else {
ans[now] = m;
pre = dq[u];
dq[u] = a[now];
}
rep(i, 0, v[now].size()) {
int ne = v[now][i];
if (ne == par)
continue;
dfs(ne, now);
}
if (pre == -1)
dq.pop_back();
else
dq[now] = pre;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
rep(i, 0, n) cin >> a[i];
rep(i, 0, n - 1) {
int f, g;
cin >> f >> g;
f--;
g--;
v[f].push_back(g);
v[g].push_back(f);
}
dfs(0, -1);
rep(i, 0, n) cout << ans[i] << endl;
}
| #include <bits/stdc++.h>
// ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
// clock_t start=clock();clock_t
// end=clock();cout<<(double)(end-start)/CLOCKS_PER_SEC<<endl;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef pair<int, int> pii;
typedef pair<pii, int> ppii;
typedef pair<int, pii> pipi;
typedef pair<ll, ll> pll;
typedef pair<pll, ll> ppll;
typedef pair<ll, pll> plpl;
typedef pair<pii, pii> P;
typedef tuple<ll, ll, ll> tl;
typedef pair<double, double> pdd;
typedef vector<vector<ll>> mat;
ll mod = 1000000007;
ll mod2 = 998244353;
ll mod3 = 1000003;
ll mod4 = 998244853;
ll mod5 = 1000000009;
ll inf = 1LL << 62;
double pi = 3.141592653589793238462643383279L;
double eps = 1e-14;
#define rep(i, m, n) for (int i = m; i < n; i++)
#define rrep(i, n, m) for (ll i = n; i >= m; i--)
#define srep(itr, st) for (auto itr = st.begin(); itr != st.end(); itr++)
#define mrep(itr, mp) for (auto &itr : mp)
#define Max(a, b) a = max(a, b)
#define Min(a, b) a = min(a, b)
int dh[4] = {1, -1, 0, 0};
int dw[4] = {0, 0, 1, -1};
int ddh[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
int ddw[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
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);
}
};
#define umh unordered_map<ll, ll, custom_hash>
ll gcd(ll a, ll b) {
if (a < b)
swap(a, b);
if (b == 0)
return a;
if (a % b == 0)
return b;
return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
ll c = gcd(a, b);
return a * b / c;
}
ll Pow(ll n, ll k) {
ll ret = 1;
ll now = n;
while (k > 0) {
if (k & 1)
ret *= now;
now *= now;
k /= 2;
}
return ret;
}
ll beki(ll n, ll k, ll md) {
ll ret = 1;
ll now = n;
now %= md;
while (k > 0) {
if (k % 2 == 1) {
ret *= now;
ret %= md;
}
now *= now;
now %= md;
k /= 2;
}
return ret;
}
ll gyaku(ll n, ll md) { return beki(n, md - 2, md); }
ll popcount(ll n) {
ll ret = 0;
ll u = n;
while (u > 0) {
ret += u % 2;
u /= 2;
}
return ret;
}
int a[200010];
vector<int> v[200010];
int ans[200010];
deque<int> dq;
void dfs(int now, int par) {
int m = (int)dq.size();
int u = m, d = -1;
while (u - d > 1) {
int mid = (u + d) / 2;
if (dq[mid] >= a[now])
u = mid;
else
d = mid;
}
int pre = -1;
if (u == m) {
ans[now] = u + 1;
dq.push_back(a[now]);
} else {
ans[now] = m;
pre = dq[u];
dq[u] = a[now];
}
rep(i, 0, v[now].size()) {
int ne = v[now][i];
if (ne == par)
continue;
dfs(ne, now);
}
if (pre == -1)
dq.pop_back();
else
dq[u] = pre;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
rep(i, 0, n) cin >> a[i];
rep(i, 0, n - 1) {
int f, g;
cin >> f >> g;
f--;
g--;
v[f].push_back(g);
v[g].push_back(f);
}
dfs(0, -1);
rep(i, 0, n) cout << ans[i] << endl;
}
| replace | 133 | 134 | 133 | 134 | 0 | |
p02698 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 5;
const int mod = 1e9 + 7;
ll qpow(ll a, ll b) {
ll res = 1;
for (; b; b >>= 1) {
if (b & 1)
res = res * a % mod;
a = a * a % mod;
}
return res;
}
struct graph {
int head[maxn], nxt[maxn << 1], to[maxn << 1], w[maxn << 1], sz;
void init() { memset(head, -1, sizeof(head)); }
graph() { init(); }
void push(int a, int b, int c) {
nxt[sz] = head[a], to[sz] = b, w[sz] = c, head[a] = sz++;
}
int &operator[](const int a) { return to[a]; }
} g;
int n, a[maxn], b[maxn], m;
struct BIT {
int bit[maxn];
vector<pair<int, int>> update(int x, int k) {
vector<pair<int, int>> v;
while (x <= m) {
if (bit[x] < k) {
v.push_back(make_pair(x, bit[x]));
bit[x] = k;
}
x += x & -x;
}
return v;
}
int query(int x) {
int tmp = 0;
while (x) {
tmp = max(tmp, bit[x]);
x -= x & -x;
}
return tmp;
}
} bit;
int ans[maxn];
void dfs(int now, int pre) {
ans[now] = bit.query(a[now] - 1) + 1;
vector<pair<int, int>> v = bit.update(a[now], ans[now]);
;
ans[now] = max(ans[now], ans[pre]);
for (int i = g.head[now]; ~i; i = g.nxt[i]) {
if (g[i] == pre)
continue;
dfs(g[i], now);
}
for (int i = 0; i < v.size(); ++i) {
bit.bit[v[i].first] = v[i].second;
}
}
int main() {
freopen("creatTree10.txt", "r", stdin);
freopen("simple.out", "w", stdout);
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
scanf("%d", &a[i]), b[i] = a[i];
sort(b + 1, b + 1 + n);
m = unique(b + 1, b + 1 + n) - b - 1;
for (int i = 1; i <= n; ++i)
a[i] = lower_bound(b + 1, b + 1 + m, a[i]) - b;
for (int i = 1; i < n; ++i) {
int c, d;
scanf("%d%d", &c, &d);
g.push(c, d, 0), g.push(d, c, 0);
}
dfs(1, 0);
for (int i = 1; i <= n; ++i)
printf("%d\n", ans[i]);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 5;
const int mod = 1e9 + 7;
ll qpow(ll a, ll b) {
ll res = 1;
for (; b; b >>= 1) {
if (b & 1)
res = res * a % mod;
a = a * a % mod;
}
return res;
}
struct graph {
int head[maxn], nxt[maxn << 1], to[maxn << 1], w[maxn << 1], sz;
void init() { memset(head, -1, sizeof(head)); }
graph() { init(); }
void push(int a, int b, int c) {
nxt[sz] = head[a], to[sz] = b, w[sz] = c, head[a] = sz++;
}
int &operator[](const int a) { return to[a]; }
} g;
int n, a[maxn], b[maxn], m;
struct BIT {
int bit[maxn];
vector<pair<int, int>> update(int x, int k) {
vector<pair<int, int>> v;
while (x <= m) {
if (bit[x] < k) {
v.push_back(make_pair(x, bit[x]));
bit[x] = k;
}
x += x & -x;
}
return v;
}
int query(int x) {
int tmp = 0;
while (x) {
tmp = max(tmp, bit[x]);
x -= x & -x;
}
return tmp;
}
} bit;
int ans[maxn];
void dfs(int now, int pre) {
ans[now] = bit.query(a[now] - 1) + 1;
vector<pair<int, int>> v = bit.update(a[now], ans[now]);
;
ans[now] = max(ans[now], ans[pre]);
for (int i = g.head[now]; ~i; i = g.nxt[i]) {
if (g[i] == pre)
continue;
dfs(g[i], now);
}
for (int i = 0; i < v.size(); ++i) {
bit.bit[v[i].first] = v[i].second;
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
scanf("%d", &a[i]), b[i] = a[i];
sort(b + 1, b + 1 + n);
m = unique(b + 1, b + 1 + n) - b - 1;
for (int i = 1; i <= n; ++i)
a[i] = lower_bound(b + 1, b + 1 + m, a[i]) - b;
for (int i = 1; i < n; ++i) {
int c, d;
scanf("%d%d", &c, &d);
g.push(c, d, 0), g.push(d, c, 0);
}
dfs(1, 0);
for (int i = 1; i <= n; ++i)
printf("%d\n", ans[i]);
return 0;
}
| delete | 62 | 64 | 62 | 62 | -11 | |
p02698 | C++ | Runtime Error | #include <bits/stdc++.h>
#define int long long
#define x first
#define y second
#define getbit(x, i) (((x) >> (i)) & 1)
using namespace std;
typedef pair<int, int> pii;
#define hashset unordered_set
#define hashmap unordered_map
#define newline fast_writechar('\n')
#define unify(arr) arr.resize(unique(arr.begin(), arr.end()) - arr.begin())
#define getbit(x, i) (((x) >> (i)) & 1)
template <typename T> vector<T> readvector(size_t sz) {
vector<T> res(sz);
for (size_t i = 0; i < sz; ++i) {
cin >> res[i];
}
return res;
}
template <typename T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) {
std::copy(v.begin(), v.end(), std::ostream_iterator<T>(out, " "));
return out;
}
int mylog(int n) { return 63 - __builtin_clzll(n); }
inline int binPow(int x, int deg, int mod) {
int ans = 1;
for (int i = sizeof(deg) * CHAR_BIT - 1; i >= 0; i--) {
ans *= ans;
ans %= mod;
if (getbit(deg, i))
ans *= x;
ans %= mod;
}
return ans;
}
/** Interface */
inline int readInt();
inline int readUInt();
inline void readWord(char *s);
inline int fast_readchar(); // you may use readchar() instead of it
inline void writeInt(int x);
inline void fast_writechar(int x); // you may use putchar() instead of it
inline void writeWord(const char *s);
inline void fast_flush();
// ====================== END ======================
const int MAXN = 2e5 + 10;
const int MOD = 1e9 + 7;
const int INF = 1e18;
int tree[MAXN];
vector<int> graph[MAXN];
int arr[MAXN];
int ans[MAXN];
vector<pii> updates;
int get(int v, int tl, int tr, int l, int r) {
if (tr <= l || r <= tl)
return 0;
if (l <= tl && tr <= r)
return tree[v];
int tm = (tl + tr) / 2;
return max(get(v * 2 + 0, tl, tm, l, r), get(v * 2 + 1, tm, tr, l, r));
}
void update(int v, int tl, int tr, int pos, int value) {
if (tree[v] < value) {
updates.push_back({v, tree[v]});
tree[v] = value;
}
if (tl + 1 == tr)
return;
int tm = (tl + tr) / 2;
if (tm > pos)
update(v * 2 + 0, tl, tm, pos, value);
else
update(v * 2 + 1, tm, tr, pos, value);
}
void dfs(int v, int p, int n) {
int updates_size = updates.size();
int upd = get(1, 0, n, 0, arr[v]) + 1;
update(1, 0, n, arr[v], upd);
ans[v] = tree[1];
for (auto u : graph[v]) {
if (u != p)
dfs(u, v, n);
}
while (updates.size() > updates_size) {
auto e = updates.back();
updates.pop_back();
tree[e.x] = e.y;
}
}
void solve() {
int n;
cin >> n;
for (int i = 0; i < n; i++)
cin >> arr[i];
int cnt = 0;
hashmap<int, int> mp;
set<int> elems(arr, arr + n);
for (auto e : elems) {
mp[e] = cnt++;
}
for (int i = 0; i < n; i++)
arr[i] = mp[arr[i]];
for (int i = 1; i < n; i++) {
int v, u;
cin >> v >> u;
v--;
u--;
graph[v].push_back(u);
graph[u].push_back(v);
}
dfs(0, 0, n);
copy(ans, ans + n, std::ostream_iterator<int>(cout, "\n"));
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
// t = readInt();
// cin >> t;
for (int i = 1; i <= t; ++i) {
// cout << "Case #" << i << ": ";
solve();
}
// fast_flush();
return 0;
}
/** Read */
static const int buf_size = 4096;
inline int fast_readchar() {
static char buf[buf_size];
static int len = 0, pos = 0;
if (pos == len)
pos = 0, len = fread(buf, 1, buf_size, stdin);
if (pos == len)
return -1;
return buf[pos++];
}
inline int readUInt() {
int c = fast_readchar(), x = 0;
while (c <= 32)
c = fast_readchar();
while ('0' <= c && c <= '9')
x = x * 10 + c - '0', c = fast_readchar();
return x;
}
inline int readInt() {
int s = 1, c = fast_readchar();
int x = 0;
while (c <= 32)
c = fast_readchar();
if (c == '-')
s = -1, c = fast_readchar();
while ('0' <= c && c <= '9')
x = x * 10 + c - '0', c = fast_readchar();
return x * s;
}
inline void readWord(char *s) {
int c = fast_readchar();
while (c <= 32)
c = fast_readchar();
while (c > 32)
*s++ = c, c = fast_readchar();
*s = 0;
}
/** Write */
static int write_pos = 0;
static char write_buf[buf_size];
inline void fast_writechar(int x) {
if (write_pos == buf_size)
fwrite(write_buf, 1, buf_size, stdout), write_pos = 0;
write_buf[write_pos++] = x;
}
inline void fast_flush() {
if (write_pos)
fwrite(write_buf, 1, write_pos, stdout), write_pos = 0;
}
inline void writeInt(int x) {
if (x < 0)
fast_writechar('-'), x = -x;
char s[24];
int n = 0;
while (x || !n)
s[n++] = '0' + x % 10, x /= 10;
while (n--)
fast_writechar(s[n]);
}
inline void writeWord(const char *s) {
while (*s)
fast_writechar(*s++);
}
| #include <bits/stdc++.h>
#define int long long
#define x first
#define y second
#define getbit(x, i) (((x) >> (i)) & 1)
using namespace std;
typedef pair<int, int> pii;
#define hashset unordered_set
#define hashmap unordered_map
#define newline fast_writechar('\n')
#define unify(arr) arr.resize(unique(arr.begin(), arr.end()) - arr.begin())
#define getbit(x, i) (((x) >> (i)) & 1)
template <typename T> vector<T> readvector(size_t sz) {
vector<T> res(sz);
for (size_t i = 0; i < sz; ++i) {
cin >> res[i];
}
return res;
}
template <typename T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) {
std::copy(v.begin(), v.end(), std::ostream_iterator<T>(out, " "));
return out;
}
int mylog(int n) { return 63 - __builtin_clzll(n); }
inline int binPow(int x, int deg, int mod) {
int ans = 1;
for (int i = sizeof(deg) * CHAR_BIT - 1; i >= 0; i--) {
ans *= ans;
ans %= mod;
if (getbit(deg, i))
ans *= x;
ans %= mod;
}
return ans;
}
/** Interface */
inline int readInt();
inline int readUInt();
inline void readWord(char *s);
inline int fast_readchar(); // you may use readchar() instead of it
inline void writeInt(int x);
inline void fast_writechar(int x); // you may use putchar() instead of it
inline void writeWord(const char *s);
inline void fast_flush();
// ====================== END ======================
const int MAXN = 2e5 + 10;
const int MOD = 1e9 + 7;
const int INF = 1e18;
int tree[MAXN * 4];
vector<int> graph[MAXN];
int arr[MAXN];
int ans[MAXN];
vector<pii> updates;
int get(int v, int tl, int tr, int l, int r) {
if (tr <= l || r <= tl)
return 0;
if (l <= tl && tr <= r)
return tree[v];
int tm = (tl + tr) / 2;
return max(get(v * 2 + 0, tl, tm, l, r), get(v * 2 + 1, tm, tr, l, r));
}
void update(int v, int tl, int tr, int pos, int value) {
if (tree[v] < value) {
updates.push_back({v, tree[v]});
tree[v] = value;
}
if (tl + 1 == tr)
return;
int tm = (tl + tr) / 2;
if (tm > pos)
update(v * 2 + 0, tl, tm, pos, value);
else
update(v * 2 + 1, tm, tr, pos, value);
}
void dfs(int v, int p, int n) {
int updates_size = updates.size();
int upd = get(1, 0, n, 0, arr[v]) + 1;
update(1, 0, n, arr[v], upd);
ans[v] = tree[1];
for (auto u : graph[v]) {
if (u != p)
dfs(u, v, n);
}
while (updates.size() > updates_size) {
auto e = updates.back();
updates.pop_back();
tree[e.x] = e.y;
}
}
void solve() {
int n;
cin >> n;
for (int i = 0; i < n; i++)
cin >> arr[i];
int cnt = 0;
hashmap<int, int> mp;
set<int> elems(arr, arr + n);
for (auto e : elems) {
mp[e] = cnt++;
}
for (int i = 0; i < n; i++)
arr[i] = mp[arr[i]];
for (int i = 1; i < n; i++) {
int v, u;
cin >> v >> u;
v--;
u--;
graph[v].push_back(u);
graph[u].push_back(v);
}
dfs(0, 0, n);
copy(ans, ans + n, std::ostream_iterator<int>(cout, "\n"));
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
// t = readInt();
// cin >> t;
for (int i = 1; i <= t; ++i) {
// cout << "Case #" << i << ": ";
solve();
}
// fast_flush();
return 0;
}
/** Read */
static const int buf_size = 4096;
inline int fast_readchar() {
static char buf[buf_size];
static int len = 0, pos = 0;
if (pos == len)
pos = 0, len = fread(buf, 1, buf_size, stdin);
if (pos == len)
return -1;
return buf[pos++];
}
inline int readUInt() {
int c = fast_readchar(), x = 0;
while (c <= 32)
c = fast_readchar();
while ('0' <= c && c <= '9')
x = x * 10 + c - '0', c = fast_readchar();
return x;
}
inline int readInt() {
int s = 1, c = fast_readchar();
int x = 0;
while (c <= 32)
c = fast_readchar();
if (c == '-')
s = -1, c = fast_readchar();
while ('0' <= c && c <= '9')
x = x * 10 + c - '0', c = fast_readchar();
return x * s;
}
inline void readWord(char *s) {
int c = fast_readchar();
while (c <= 32)
c = fast_readchar();
while (c > 32)
*s++ = c, c = fast_readchar();
*s = 0;
}
/** Write */
static int write_pos = 0;
static char write_buf[buf_size];
inline void fast_writechar(int x) {
if (write_pos == buf_size)
fwrite(write_buf, 1, buf_size, stdout), write_pos = 0;
write_buf[write_pos++] = x;
}
inline void fast_flush() {
if (write_pos)
fwrite(write_buf, 1, write_pos, stdout), write_pos = 0;
}
inline void writeInt(int x) {
if (x < 0)
fast_writechar('-'), x = -x;
char s[24];
int n = 0;
while (x || !n)
s[n++] = '0' + x % 10, x /= 10;
while (n--)
fast_writechar(s[n]);
}
inline void writeWord(const char *s) {
while (*s)
fast_writechar(*s++);
}
| replace | 57 | 58 | 57 | 58 | 0 | |
p02698 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define FOR(i, a, b) for (auto i = a; i <= b; ++i)
#define REP(i, a, b) for (auto i = a; i < b; ++i)
#define FORI(i, a, b) \
for (auto i = a; i != b + 1 - 2 * (a > b); i += 1 - 2 * (a > b))
#define REPI(i, a, b) \
for (auto i = a - (a > b); i != b - (a > b); i += 1 - 2 * (a > b))
#define ALL(v) v.begin(), v.end()
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define pf(a) push_front(a)
#define eb(a, b) emplace_back(a, b)
#define fir first
#define sec second
#define what_is(x) cout << #x << " is " << x << endl;
#define type(x) typeid(x).name()
#define ms(arr, val) memset(arr, val, sizeof(arr))
#define min3(a, b, c) min(min(a, b), c)
#define max3(a, b, c) max(max(a, b), c)
#define PI acos(-1)
#define open_read freopen("christmas.in", "r", stdin)
#define open_write freopen("christmas.out", "w", stdout)
using namespace std;
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef pair<double, double> PDD;
typedef pair<int, int> PII;
typedef pair<int, PII> PIPII;
typedef pair<PII, PII> PPIIPII;
typedef pair<LL, LL> PLL;
typedef pair<ULL, ULL> PUU;
typedef pair<LL, PLL> PLPLL;
typedef pair<PLL, PLL> PPLLPLL;
typedef pair<int, LL> PIL;
typedef pair<LL, int> PLI;
template <typename T, typename T1>
ostream &operator<<(ostream &out, pair<T, T1> obj) {
out << "(" << obj.first << ", " << obj.second << ")";
return out;
}
template <typename T, typename T1>
ostream &operator<<(ostream &out, map<T, T1> cont) {
typename map<T, T1>::const_iterator itr = cont.begin();
typename map<T, T1>::const_iterator ends = cont.end();
for (; itr != ends; ++itr) {
out << *itr << " ";
}
out << endl;
return out;
}
template <typename T> ostream &operator<<(ostream &out, set<T> cont) {
typename set<T>::const_iterator itr = cont.begin();
typename set<T>::const_iterator ends = cont.end();
for (; itr != ends; ++itr) {
out << *itr << " ";
}
out << endl;
return out;
}
template <typename T> ostream &operator<<(ostream &out, multiset<T> cont) {
typename multiset<T>::const_iterator itr = cont.begin();
typename multiset<T>::const_iterator ends = cont.end();
for (; itr != ends; ++itr) {
out << *itr << " ";
}
out << endl;
return out;
}
template <typename T,
template <typename ELEM, typename ALLOC = allocator<ELEM>> class CONT>
ostream &operator<<(ostream &out, CONT<T> cont) {
typename CONT<T>::const_iterator itr = cont.begin();
typename CONT<T>::const_iterator ends = cont.end();
for (; itr != ends; ++itr) {
out << *itr << " ";
}
out << endl;
return out;
}
template <typename T, unsigned int N, typename CTy, typename CTr>
typename enable_if<!is_same<T, char>::value, basic_ostream<CTy, CTr> &>::type
operator<<(basic_ostream<CTy, CTr> &out, const T (&arr)[N]) {
REP(i, 0, N) { out << arr[i] << " "; }
out << endl;
return out;
}
template <typename T> T GCD(T a, T b) {
T min_v = min(a, b);
T max_v = max(a, b);
while (min_v) {
T temp = max_v % min_v;
max_v = min_v;
min_v = temp;
}
return max_v;
}
template <typename T> T LCM(T a, T b) { return (a * b) / gcd(a, b); }
template <typename T> T fastExpPow(T base, T exp, T mod) {
T res = 1;
while (exp) {
if (exp & 1) {
res *= base;
res %= mod;
}
exp >>= 1;
base *= base;
base %= mod;
}
return res % mod;
}
/*#################################################################################################################
###################################################################################################################
###################################################################################################################
#################################################################################################################*/
#define SIZE 200010
int N;
int numbers[SIZE], segTree[4 * SIZE], lis[SIZE];
vector<int> tree[SIZE], order[SIZE];
int getMax(int Qbeg, int Qend, int Tbeg = 0, int Tend = N - 1, int ind = 0) {
if (Tbeg > Qend - 1 || Tend < Qbeg - 1) {
return 0;
}
if (Qbeg - 1 <= Tbeg && Tend <= Qend - 1) {
return segTree[ind];
}
int Tmid = (Tbeg + Tend) >> 1;
int max1 = getMax(Qbeg, Qend, Tbeg, Tmid, 2 * ind + 1);
int max2 = getMax(Qbeg, Qend, Tmid + 1, Tend, 2 * ind + 2);
return max(max1, max2);
}
void update(int pos, int val, int Tbeg = 0, int Tend = N - 1, int ind = 0) {
if (Tbeg >= Tend) {
if (Tbeg == pos - 1) {
segTree[ind] = val;
}
return;
}
int Tmid = (Tbeg + Tend) >> 1;
update(pos, val, Tbeg, Tmid, 2 * ind + 1);
update(pos, val, Tmid + 1, Tend, 2 * ind + 2);
segTree[ind] = max(segTree[2 * ind + 1], segTree[2 * ind + 2]);
}
void dfs(int node = 1, int par = 0) {
int num = numbers[node];
int val = getMax(0, num - 1) + 1;
order[num].pb(val);
update(num, order[num].back());
lis[node] = getMax(1, N);
for (auto connNode : tree[node]) {
if (connNode != par) {
dfs(connNode, node);
}
}
order[num].pop_back();
if (order[num].empty()) {
update(num, 0);
} else {
update(num, order[num].back());
}
}
int main() {
scanf("%d", &N);
vector<PII> sorted;
FOR(i, 1, N) {
scanf("%d", numbers + i);
sorted.eb(numbers[i], i);
}
sort(ALL(sorted));
int cnt = 1;
numbers[sorted[0].sec] = cnt;
REP(i, 1, N) {
if (sorted[i].fir > sorted[i - 1].fir) {
++cnt;
}
numbers[sorted[i].sec] = cnt;
}
FOR(i, 1, N - 1) {
int U, V;
scanf("%d%d", &U, &V);
tree[U].pb(V);
tree[V].pb(U);
}
dfs();
FOR(i, 1, N) { printf("%d\n", lis[i]); }
return 0;
}
| #include <bits/stdc++.h>
#define FOR(i, a, b) for (auto i = a; i <= b; ++i)
#define REP(i, a, b) for (auto i = a; i < b; ++i)
#define FORI(i, a, b) \
for (auto i = a; i != b + 1 - 2 * (a > b); i += 1 - 2 * (a > b))
#define REPI(i, a, b) \
for (auto i = a - (a > b); i != b - (a > b); i += 1 - 2 * (a > b))
#define ALL(v) v.begin(), v.end()
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define pf(a) push_front(a)
#define eb(a, b) emplace_back(a, b)
#define fir first
#define sec second
#define what_is(x) cout << #x << " is " << x << endl;
#define type(x) typeid(x).name()
#define ms(arr, val) memset(arr, val, sizeof(arr))
#define min3(a, b, c) min(min(a, b), c)
#define max3(a, b, c) max(max(a, b), c)
#define PI acos(-1)
#define open_read freopen("christmas.in", "r", stdin)
#define open_write freopen("christmas.out", "w", stdout)
using namespace std;
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef pair<double, double> PDD;
typedef pair<int, int> PII;
typedef pair<int, PII> PIPII;
typedef pair<PII, PII> PPIIPII;
typedef pair<LL, LL> PLL;
typedef pair<ULL, ULL> PUU;
typedef pair<LL, PLL> PLPLL;
typedef pair<PLL, PLL> PPLLPLL;
typedef pair<int, LL> PIL;
typedef pair<LL, int> PLI;
template <typename T, typename T1>
ostream &operator<<(ostream &out, pair<T, T1> obj) {
out << "(" << obj.first << ", " << obj.second << ")";
return out;
}
template <typename T, typename T1>
ostream &operator<<(ostream &out, map<T, T1> cont) {
typename map<T, T1>::const_iterator itr = cont.begin();
typename map<T, T1>::const_iterator ends = cont.end();
for (; itr != ends; ++itr) {
out << *itr << " ";
}
out << endl;
return out;
}
template <typename T> ostream &operator<<(ostream &out, set<T> cont) {
typename set<T>::const_iterator itr = cont.begin();
typename set<T>::const_iterator ends = cont.end();
for (; itr != ends; ++itr) {
out << *itr << " ";
}
out << endl;
return out;
}
template <typename T> ostream &operator<<(ostream &out, multiset<T> cont) {
typename multiset<T>::const_iterator itr = cont.begin();
typename multiset<T>::const_iterator ends = cont.end();
for (; itr != ends; ++itr) {
out << *itr << " ";
}
out << endl;
return out;
}
template <typename T,
template <typename ELEM, typename ALLOC = allocator<ELEM>> class CONT>
ostream &operator<<(ostream &out, CONT<T> cont) {
typename CONT<T>::const_iterator itr = cont.begin();
typename CONT<T>::const_iterator ends = cont.end();
for (; itr != ends; ++itr) {
out << *itr << " ";
}
out << endl;
return out;
}
template <typename T, unsigned int N, typename CTy, typename CTr>
typename enable_if<!is_same<T, char>::value, basic_ostream<CTy, CTr> &>::type
operator<<(basic_ostream<CTy, CTr> &out, const T (&arr)[N]) {
REP(i, 0, N) { out << arr[i] << " "; }
out << endl;
return out;
}
template <typename T> T GCD(T a, T b) {
T min_v = min(a, b);
T max_v = max(a, b);
while (min_v) {
T temp = max_v % min_v;
max_v = min_v;
min_v = temp;
}
return max_v;
}
template <typename T> T LCM(T a, T b) { return (a * b) / gcd(a, b); }
template <typename T> T fastExpPow(T base, T exp, T mod) {
T res = 1;
while (exp) {
if (exp & 1) {
res *= base;
res %= mod;
}
exp >>= 1;
base *= base;
base %= mod;
}
return res % mod;
}
/*#################################################################################################################
###################################################################################################################
###################################################################################################################
#################################################################################################################*/
#define SIZE 200010
int N;
int numbers[SIZE], segTree[4 * SIZE], lis[SIZE];
vector<int> tree[SIZE], order[SIZE];
int getMax(int Qbeg, int Qend, int Tbeg = 0, int Tend = N - 1, int ind = 0) {
if (Tbeg > Qend - 1 || Tend < Qbeg - 1) {
return 0;
}
if (Qbeg - 1 <= Tbeg && Tend <= Qend - 1) {
return segTree[ind];
}
int Tmid = (Tbeg + Tend) >> 1;
int max1 = getMax(Qbeg, Qend, Tbeg, Tmid, 2 * ind + 1);
int max2 = getMax(Qbeg, Qend, Tmid + 1, Tend, 2 * ind + 2);
return max(max1, max2);
}
void update(int pos, int val, int Tbeg = 0, int Tend = N - 1, int ind = 0) {
if (Tbeg > pos - 1 || Tend < pos - 1) {
return;
}
if (Tbeg == pos - 1 && Tbeg == Tend) {
segTree[ind] = val;
return;
}
int Tmid = (Tbeg + Tend) >> 1;
update(pos, val, Tbeg, Tmid, 2 * ind + 1);
update(pos, val, Tmid + 1, Tend, 2 * ind + 2);
segTree[ind] = max(segTree[2 * ind + 1], segTree[2 * ind + 2]);
}
void dfs(int node = 1, int par = 0) {
int num = numbers[node];
int val = getMax(0, num - 1) + 1;
order[num].pb(val);
update(num, order[num].back());
lis[node] = getMax(1, N);
for (auto connNode : tree[node]) {
if (connNode != par) {
dfs(connNode, node);
}
}
order[num].pop_back();
if (order[num].empty()) {
update(num, 0);
} else {
update(num, order[num].back());
}
}
int main() {
scanf("%d", &N);
vector<PII> sorted;
FOR(i, 1, N) {
scanf("%d", numbers + i);
sorted.eb(numbers[i], i);
}
sort(ALL(sorted));
int cnt = 1;
numbers[sorted[0].sec] = cnt;
REP(i, 1, N) {
if (sorted[i].fir > sorted[i - 1].fir) {
++cnt;
}
numbers[sorted[i].sec] = cnt;
}
FOR(i, 1, N - 1) {
int U, V;
scanf("%d%d", &U, &V);
tree[U].pb(V);
tree[V].pb(U);
}
dfs();
FOR(i, 1, N) { printf("%d\n", lis[i]); }
return 0;
}
| replace | 167 | 171 | 167 | 173 | TLE | |
p02698 | C++ | Time Limit Exceeded | #include "bits/stdc++.h"
using namespace std;
///////////////////////////////////////////
const long long int INF = 1LL << 60;
const long long int Mod = 1000000007;
using ll = long long int;
using ci = const int;
using vi = vector<int>;
using Vi = vector<long long int>;
using P = pair<int, int>;
using PLL = pair<ll, ll>;
using matrix = vector<vector<ll>>;
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define all(x) (x).begin(), (x).end()
#define rep(i, N) for (ll i = 0; i < (ll)N; i++)
#define repi(i, a, b) for (ll i = ll(a); i < ll(b); ++i)
template <class T> bool chmax(T &former, const T &b) {
if (former < b) {
former = b;
return true;
}
return false;
}
template <class T> bool chmin(T &former, const T &b) {
if (b < former) {
former = b;
return true;
}
return false;
}
template <class T> T sqar(T x) { return x * x; } // sqrt(x)は平方根;
#define Sort(v) \
std::sort(v.begin(), v.end(), \
std::greater<decltype(v[0])>()) // 降順でVをソート
#define p_queue(v) priority_queue<v, vector<v>, greater<v>>
template <class T> inline void princ(T x) { cout << x << " "; };
template <class T> inline void print(T x) { cout << x << "\n"; };
template <class T> inline void Yes(T condition) {
if (condition)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
template <class T> inline void YES(T condition) {
if (condition)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
///////////////////////////////////////////////////////////////////////////////////
struct edge { /*重み付き,有向*/
int to, cost; /*toは接続先*/
edge(ll to_, ll cost_ = 1) {
to = to_;
cost = cost_;
}; /*全てのcost=1 <=> 無向グラフ*/
};
bool operator<(edge e1, edge e2) { return e1.cost < e2.cost; }
bool operator>(edge e1, edge e2) { return e1.cost > e2.cost; }
using graph = vector<vector<edge>>;
ll n;
Vi a;
graph g;
Vi as;
void dfs(ll idn, ll prev, Vi dp) {
ll tmp = lower_bound(all(dp), a[idn]) - dp.begin();
ll mem = dp[tmp];
dp[tmp] = a[idn];
as[idn] = lower_bound(all(dp), INF) - dp.begin();
rep(i, g[idn].size()) {
edge e = g[idn][i];
if (e.to != prev) {
dfs(e.to, idn, dp);
}
}
dp[tmp] = mem;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
std::cout << std::fixed << std::setprecision(30);
cin >> n;
g.resize(n);
a.resize(n);
as.resize(n);
rep(i, n) { cin >> a[i]; }
rep(i, n - 1) {
ll in, inp;
cin >> in >> inp;
in--;
inp--;
g[in].pb(edge(inp));
g[inp].pb(edge(in));
}
Vi tp;
tp.resize(n, INF);
dfs(0, -1, tp);
rep(i, as.size()) { print(as[i]); }
return 0;
}
| #include "bits/stdc++.h"
using namespace std;
///////////////////////////////////////////
const long long int INF = 1LL << 60;
const long long int Mod = 1000000007;
using ll = long long int;
using ci = const int;
using vi = vector<int>;
using Vi = vector<long long int>;
using P = pair<int, int>;
using PLL = pair<ll, ll>;
using matrix = vector<vector<ll>>;
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define all(x) (x).begin(), (x).end()
#define rep(i, N) for (ll i = 0; i < (ll)N; i++)
#define repi(i, a, b) for (ll i = ll(a); i < ll(b); ++i)
template <class T> bool chmax(T &former, const T &b) {
if (former < b) {
former = b;
return true;
}
return false;
}
template <class T> bool chmin(T &former, const T &b) {
if (b < former) {
former = b;
return true;
}
return false;
}
template <class T> T sqar(T x) { return x * x; } // sqrt(x)は平方根;
#define Sort(v) \
std::sort(v.begin(), v.end(), \
std::greater<decltype(v[0])>()) // 降順でVをソート
#define p_queue(v) priority_queue<v, vector<v>, greater<v>>
template <class T> inline void princ(T x) { cout << x << " "; };
template <class T> inline void print(T x) { cout << x << "\n"; };
template <class T> inline void Yes(T condition) {
if (condition)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
template <class T> inline void YES(T condition) {
if (condition)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
///////////////////////////////////////////////////////////////////////////////////
struct edge { /*重み付き,有向*/
int to, cost; /*toは接続先*/
edge(ll to_, ll cost_ = 1) {
to = to_;
cost = cost_;
}; /*全てのcost=1 <=> 無向グラフ*/
};
bool operator<(edge e1, edge e2) { return e1.cost < e2.cost; }
bool operator>(edge e1, edge e2) { return e1.cost > e2.cost; }
using graph = vector<vector<edge>>;
ll n;
Vi a;
graph g;
Vi as;
void dfs(ll idn, ll prev, Vi &dp) {
ll tmp = lower_bound(all(dp), a[idn]) - dp.begin();
ll mem = dp[tmp];
dp[tmp] = a[idn];
as[idn] = lower_bound(all(dp), INF) - dp.begin();
rep(i, g[idn].size()) {
edge e = g[idn][i];
if (e.to != prev) {
dfs(e.to, idn, dp);
}
}
dp[tmp] = mem;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
std::cout << std::fixed << std::setprecision(30);
cin >> n;
g.resize(n);
a.resize(n);
as.resize(n);
rep(i, n) { cin >> a[i]; }
rep(i, n - 1) {
ll in, inp;
cin >> in >> inp;
in--;
inp--;
g[in].pb(edge(inp));
g[inp].pb(edge(in));
}
Vi tp;
tp.resize(n, INF);
dfs(0, -1, tp);
rep(i, as.size()) { print(as[i]); }
return 0;
}
| replace | 68 | 69 | 68 | 69 | TLE | |
p02698 | C++ | Runtime Error | // #pragma GCC optimize ("O3")
// #pragma GCC target ("avx")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please
int N;
int A[200001], nozoki[200001];
vector<int> E[200001];
int kotae[200001];
deque<int> dq;
void dfs(int p, int mae) {
auto itr = lower_bound(dq.begin(), dq.end(), A[p]);
if (itr != dq.end()) {
nozoki[p] = *itr;
*itr = A[p];
} else {
dq.pb(A[p]);
}
kotae[p] = dq.size();
for (auto to : E[p]) {
if (to != mae) {
dfs(to, p);
}
}
if (!nozoki[p]) {
dq.pop_back();
} else {
itr = lower_bound(dq.begin(), dq.end(), nozoki[p]);
*(--itr) = nozoki[p];
}
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> N;
rep1(i, N) cin >> A[i];
rep(i, N - 1) {
int u, v;
cin >> u >> v;
E[u].pb(v);
E[v].pb(u);
}
dq.pb(2000000000);
dfs(1, -1);
rep1(i, N) co(kotae[i]);
Would you please return 0;
} | // #pragma GCC optimize ("O3")
// #pragma GCC target ("avx")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please
int N;
int A[200001], nozoki[200001];
vector<int> E[200001];
int kotae[200001];
deque<int> dq;
void dfs(int p, int mae) {
auto itr = lower_bound(dq.begin(), dq.end(), A[p]);
if (itr != dq.end()) {
nozoki[p] = *itr;
*itr = A[p];
} else {
dq.pb(A[p]);
}
kotae[p] = dq.size();
for (auto to : E[p]) {
if (to != mae) {
dfs(to, p);
}
}
if (!nozoki[p]) {
dq.pop_back();
} else {
itr = upper_bound(dq.begin(), dq.end(), nozoki[p]);
*(--itr) = nozoki[p];
}
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> N;
rep1(i, N) cin >> A[i];
rep(i, N - 1) {
int u, v;
cin >> u >> v;
E[u].pb(v);
E[v].pb(u);
}
dq.pb(2000000000);
dfs(1, -1);
rep1(i, N) co(kotae[i]);
Would you please return 0;
} | replace | 47 | 48 | 47 | 48 | 0 | |
p02698 | C++ | Runtime Error | #include <algorithm>
#include <assert.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#define F first
#define S second
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<ll, ll> ii;
const int N = 1e5 + 5;
struct node {
int val;
node *left, *right;
node(int val = 0, node *left = NULL, node *right = NULL) {
this->val = val;
this->left = left;
this->right = right;
}
};
int ar[N];
int cnt = 0;
map<int, int> codex;
vector<int> lst[N];
node *root[N];
node *upd(node *bef, int target, int val, int l, int r) {
if (l == r)
return new node(val, NULL, NULL);
int m = (l + r) / 2;
if (target <= m) {
node *left = upd(bef->left, target, val, l, m);
return new node(max(bef->val, val), left, bef->right);
}
node *right = upd(bef->right, target, val, m + 1, r);
return new node(max(bef->val, val), bef->left, right);
}
int get(node *cur, int x, int l, int r) {
if (l >= x)
return 0;
if (r < x)
return cur->val;
int m = (l + r) / 2;
return max(get(cur->left, x, l, m), get(cur->right, x, m + 1, r));
}
int ans[N];
void dfs(int idx, int par) {
int cur_lis = get(root[par], ar[idx], 0, cnt - 1) + 1;
root[idx] = upd(root[par], ar[idx], cur_lis, 0, cnt - 1);
ans[idx] = root[idx]->val;
for (auto nx : lst[idx]) {
if (nx == par)
continue;
dfs(nx, idx);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> ar[i];
codex[ar[i]];
}
for (int i = 1; i < n; ++i) {
int a, b;
cin >> a >> b;
lst[a].pb(b);
lst[b].pb(a);
}
for (auto &x : codex) {
x.S = cnt++;
}
for (int i = 1; i <= n; ++i)
ar[i] = codex[ar[i]];
root[0] = new node(0, NULL, NULL);
root[0]->left = root[0]->right = root[0];
dfs(1, 0);
for (int i = 1; i <= n; ++i) {
cout << ans[i] << endl;
}
}
| #include <algorithm>
#include <assert.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#define F first
#define S second
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<ll, ll> ii;
const int N = 2e5 + 5;
struct node {
int val;
node *left, *right;
node(int val = 0, node *left = NULL, node *right = NULL) {
this->val = val;
this->left = left;
this->right = right;
}
};
int ar[N];
int cnt = 0;
map<int, int> codex;
vector<int> lst[N];
node *root[N];
node *upd(node *bef, int target, int val, int l, int r) {
if (l == r)
return new node(val, NULL, NULL);
int m = (l + r) / 2;
if (target <= m) {
node *left = upd(bef->left, target, val, l, m);
return new node(max(bef->val, val), left, bef->right);
}
node *right = upd(bef->right, target, val, m + 1, r);
return new node(max(bef->val, val), bef->left, right);
}
int get(node *cur, int x, int l, int r) {
if (l >= x)
return 0;
if (r < x)
return cur->val;
int m = (l + r) / 2;
return max(get(cur->left, x, l, m), get(cur->right, x, m + 1, r));
}
int ans[N];
void dfs(int idx, int par) {
int cur_lis = get(root[par], ar[idx], 0, cnt - 1) + 1;
root[idx] = upd(root[par], ar[idx], cur_lis, 0, cnt - 1);
ans[idx] = root[idx]->val;
for (auto nx : lst[idx]) {
if (nx == par)
continue;
dfs(nx, idx);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> ar[i];
codex[ar[i]];
}
for (int i = 1; i < n; ++i) {
int a, b;
cin >> a >> b;
lst[a].pb(b);
lst[b].pb(a);
}
for (auto &x : codex) {
x.S = cnt++;
}
for (int i = 1; i <= n; ++i)
ar[i] = codex[ar[i]];
root[0] = new node(0, NULL, NULL);
root[0]->left = root[0]->right = root[0];
dfs(1, 0);
for (int i = 1; i <= n; ++i) {
cout << ans[i] << endl;
}
}
| replace | 21 | 22 | 21 | 22 | 0 | |
p02698 | C++ | Runtime Error | #include <cstdio>
#include <iostream>
#include <stack>
#include <vector>
#define PR pair<int, int>
#define F first
#define S second
#define N 100005
using namespace std;
int n, sz, a[N], v[N], d[N];
vector<int> gr[N];
stack<PR> st;
void f(int p) {
if (v[p])
return;
int k = lower_bound(d, d + sz, a[p]) - d;
if (k == sz)
sz++;
v[p] = sz;
st.push({k, d[k]});
d[k] = a[p];
for (auto x : gr[p])
f(x);
PR t = st.top();
st.pop();
d[t.F] = t.S;
if (t.S == 1e9)
sz--;
}
int main() {
int i, t1, t2;
cin >> n;
for (i = 1; i <= n; i++)
scanf("%d", &a[i]);
for (i = 0; i < n - 1; i++) {
scanf("%d%d", &t1, &t2);
gr[t1].push_back(t2);
gr[t2].push_back(t1);
}
for (i = 0; i < n; i++)
d[i] = 1e9;
f(1);
for (i = 1; i <= n; i++)
printf("%d\n", v[i]);
return 0;
} | #include <cstdio>
#include <iostream>
#include <stack>
#include <vector>
#define PR pair<int, int>
#define F first
#define S second
#define N 200005
using namespace std;
int n, sz, a[N], v[N], d[N];
vector<int> gr[N];
stack<PR> st;
void f(int p) {
if (v[p])
return;
int k = lower_bound(d, d + sz, a[p]) - d;
if (k == sz)
sz++;
v[p] = sz;
st.push({k, d[k]});
d[k] = a[p];
for (auto x : gr[p])
f(x);
PR t = st.top();
st.pop();
d[t.F] = t.S;
if (t.S == 1e9)
sz--;
}
int main() {
int i, t1, t2;
cin >> n;
for (i = 1; i <= n; i++)
scanf("%d", &a[i]);
for (i = 0; i < n - 1; i++) {
scanf("%d%d", &t1, &t2);
gr[t1].push_back(t2);
gr[t2].push_back(t1);
}
for (i = 0; i < n; i++)
d[i] = 1e9;
f(1);
for (i = 1; i <= n; i++)
printf("%d\n", v[i]);
return 0;
} | replace | 7 | 8 | 7 | 8 | 0 | |
p02698 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
typedef long long ll;
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define clr(a, h) memset(a, (h), sizeof(a))
#define mem(a, h) memset(a, (h), sizeof(a))
#define fi first
#define se second
#define por(a, b) (((a % MOD) * (b % MOD)) % MOD)
#define forg(i, b, e, c) for (ll i = (ll)b; i < (ll)e; i += c)
#define forr(i, b, e) for (ll i = b; i < e; i++)
using namespace std;
using namespace __gnu_pbds;
typedef double lldb;
typedef pair<ll, ll> ii;
typedef pair<ii, ll> iii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<ll> vll;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
const ll INF = 1e9;
const double PI = acos(-1);
#define tam 120000
#define offset
const ll MOD = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
vector<int> colas[tam], g[tam];
ordered_set s;
int resp[tam], v[tam];
int lstuniv = 0;
void dfs(int num, int pa) {
int delta = 0;
int p = -1, ant;
if (lstuniv == 0 || colas[lstuniv].back() < v[num]) {
delta = 1;
p = ++lstuniv;
colas[lstuniv].push_back(v[num]);
s.insert(v[num]);
} else {
if (s.find(v[num]) == s.end()) {
int aux = *s.upper_bound(v[num]);
p = s.order_of_key(aux + 1);
ant = colas[p].back();
s.erase(ant);
colas[p].push_back(v[num]);
s.insert(v[num]);
}
}
// cout<<num<<": "<<pa<<endl;
/*for(auto ite=s.begin();ite!=s.end();ite++)
cout<<*ite<<' ';
cout<<endl;*/
resp[num] = s.size();
for (int v : g[num]) {
if (v == pa)
continue;
dfs(v, num);
}
if (p != -1) {
lstuniv -= delta;
int aux = colas[p].back();
s.erase(aux);
colas[p].pop_back();
if (!colas[p].empty())
s.insert(colas[p].back());
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
forr(i, 1, n + 1) cin >> v[i];
int iz, der;
forr(i, 0, n - 1) {
cin >> iz >> der;
g[iz].pb(der);
g[der].pb(iz);
}
dfs(1, 1);
forr(i, 1, n + 1) cout << resp[i] << endl;
}
// PLUS ULTRA!
| #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
typedef long long ll;
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define clr(a, h) memset(a, (h), sizeof(a))
#define mem(a, h) memset(a, (h), sizeof(a))
#define fi first
#define se second
#define por(a, b) (((a % MOD) * (b % MOD)) % MOD)
#define forg(i, b, e, c) for (ll i = (ll)b; i < (ll)e; i += c)
#define forr(i, b, e) for (ll i = b; i < e; i++)
using namespace std;
using namespace __gnu_pbds;
typedef double lldb;
typedef pair<ll, ll> ii;
typedef pair<ii, ll> iii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<ll> vll;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
const ll INF = 1e9;
const double PI = acos(-1);
#define tam 220000
#define offset
const ll MOD = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
vector<int> colas[tam], g[tam];
ordered_set s;
int resp[tam], v[tam];
int lstuniv = 0;
void dfs(int num, int pa) {
int delta = 0;
int p = -1, ant;
if (lstuniv == 0 || colas[lstuniv].back() < v[num]) {
delta = 1;
p = ++lstuniv;
colas[lstuniv].push_back(v[num]);
s.insert(v[num]);
} else {
if (s.find(v[num]) == s.end()) {
int aux = *s.upper_bound(v[num]);
p = s.order_of_key(aux + 1);
ant = colas[p].back();
s.erase(ant);
colas[p].push_back(v[num]);
s.insert(v[num]);
}
}
// cout<<num<<": "<<pa<<endl;
/*for(auto ite=s.begin();ite!=s.end();ite++)
cout<<*ite<<' ';
cout<<endl;*/
resp[num] = s.size();
for (int v : g[num]) {
if (v == pa)
continue;
dfs(v, num);
}
if (p != -1) {
lstuniv -= delta;
int aux = colas[p].back();
s.erase(aux);
colas[p].pop_back();
if (!colas[p].empty())
s.insert(colas[p].back());
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
forr(i, 1, n + 1) cin >> v[i];
int iz, der;
forr(i, 0, n - 1) {
cin >> iz >> der;
g[iz].pb(der);
g[der].pb(iz);
}
dfs(1, 1);
forr(i, 1, n + 1) cout << resp[i] << endl;
}
// PLUS ULTRA!
| replace | 29 | 30 | 29 | 30 | 0 | |
p02698 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits.h>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;
const int NAX = 2e5 + 3;
vector<int> G[NAX];
int ans[NAX], A[NAX];
vector<stack<int>> lis(NAX);
int upperBound(int x, int N) {
int L = -1, R = N, M;
while (L < R) {
M = L + (R - L + 1) / 2;
if (lis[M].top() <= x) {
L = M;
} else {
R = M - 1;
}
}
return L + 1;
}
int lowerBound(int x, int N) {
int L = -1, R = N, M;
while (L < R) {
M = L + (R - L + 1) / 2;
if (lis[M].top() >= x) {
R = M - 1;
} else {
L = M;
}
}
return L + 1;
}
void dfs(int u, int N, int pi = -1) {
int place = lowerBound(A[u], N);
lis[place].push(A[u]);
ans[u] = lowerBound(INT_MAX, N) - 1;
for (int &v : G[u]) {
if (v == pi) {
continue;
}
dfs(v, N, u);
}
lis[place].pop();
}
int main() {
ios::sync_with_stdio(0);
int N;
cin >> N;
for (int i = 0; i < N; ++i) {
cin >> A[i];
lis[i + 1].push(INT_MAX);
}
lis[N + 1].push(INT_MAX);
lis[N + 2].push(INT_MAX);
lis[N + 3].push(INT_MAX);
lis[0].push(INT_MIN);
for (int i = 0; i + 1 < N; ++i) {
int x, y;
cin >> x >> y;
G[x - 1].push_back(y - 1);
G[y - 1].push_back(x - 1);
}
dfs(0, N);
for (int i = 0; i < N; ++i) {
cout << ans[i] << "\n";
}
return 0;
}
| #include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits.h>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;
const int NAX = 2e5 + 11;
vector<int> G[NAX];
int ans[NAX], A[NAX];
vector<stack<int>> lis(NAX);
int upperBound(int x, int N) {
int L = -1, R = N, M;
while (L < R) {
M = L + (R - L + 1) / 2;
if (lis[M].top() <= x) {
L = M;
} else {
R = M - 1;
}
}
return L + 1;
}
int lowerBound(int x, int N) {
int L = -1, R = N, M;
while (L < R) {
M = L + (R - L + 1) / 2;
if (lis[M].top() >= x) {
R = M - 1;
} else {
L = M;
}
}
return L + 1;
}
void dfs(int u, int N, int pi = -1) {
int place = lowerBound(A[u], N);
lis[place].push(A[u]);
ans[u] = lowerBound(INT_MAX, N) - 1;
for (int &v : G[u]) {
if (v == pi) {
continue;
}
dfs(v, N, u);
}
lis[place].pop();
}
int main() {
ios::sync_with_stdio(0);
int N;
cin >> N;
for (int i = 0; i < N; ++i) {
cin >> A[i];
lis[i + 1].push(INT_MAX);
}
lis[N + 1].push(INT_MAX);
lis[N + 2].push(INT_MAX);
lis[N + 3].push(INT_MAX);
lis[0].push(INT_MIN);
for (int i = 0; i + 1 < N; ++i) {
int x, y;
cin >> x >> y;
G[x - 1].push_back(y - 1);
G[y - 1].push_back(x - 1);
}
dfs(0, N);
for (int i = 0; i < N; ++i) {
cout << ans[i] << "\n";
}
return 0;
}
| replace | 34 | 35 | 34 | 35 | -6 | terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
|
p02698 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vll = vector<long long>;
using vp = vector<pair<int, int>>;
using vvi = vector<vector<int>>;
using vvc = vector<vector<char>>;
using vvll = vector<vector<long long>>;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
const int INF = 1001001001;
vvi to;
vi ans;
vi a;
void dfs(int v, vi dp, int p = -1) {
for (int u : to[v]) {
if (u == p)
continue;
int idx = lower_bound(dp.begin(), dp.end(), a[u]) - dp.begin();
int pre = dp[idx];
dp[idx] = a[u];
ans[u] = lower_bound(dp.begin(), dp.end(), INF) - dp.begin();
dfs(u, dp, v);
dp[idx] = pre;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
a.resize(n);
rep(i, n) cin >> a[i];
to.resize(n);
rep(i, n - 1) {
int u, v;
cin >> u >> v;
u--;
v--;
to[u].pb(v);
to[v].pb(u);
}
ans.resize(n);
ans[0] = 1;
vi dp(n, INF);
dp[0] = a[0];
dfs(0, dp);
rep(i, n) cout << ans[i] << endl;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vll = vector<long long>;
using vp = vector<pair<int, int>>;
using vvi = vector<vector<int>>;
using vvc = vector<vector<char>>;
using vvll = vector<vector<long long>>;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
const int INF = 1001001001;
vvi to;
vi ans;
vi a;
void dfs(int v, vi &dp, int p = -1) {
for (int u : to[v]) {
if (u == p)
continue;
int idx = lower_bound(dp.begin(), dp.end(), a[u]) - dp.begin();
int pre = dp[idx];
dp[idx] = a[u];
ans[u] = lower_bound(dp.begin(), dp.end(), INF) - dp.begin();
dfs(u, dp, v);
dp[idx] = pre;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
a.resize(n);
rep(i, n) cin >> a[i];
to.resize(n);
rep(i, n - 1) {
int u, v;
cin >> u >> v;
u--;
v--;
to[u].pb(v);
to[v].pb(u);
}
ans.resize(n);
ans[0] = 1;
vi dp(n, INF);
dp[0] = a[0];
dfs(0, dp);
rep(i, n) cout << ans[i] << endl;
} | replace | 39 | 40 | 39 | 40 | TLE | |
p02698 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define INF 2000000000000000000
#define ll long long
using namespace std;
ll N;
vector<ll> lcs;
vector<multiset<ll>> st;
vector<ll> ans;
vector<vector<ll>> connection;
vector<ll> A;
void dfs(ll now, ll before) {
ll a = A.at(now);
ll index = lower_bound(lcs.begin(), lcs.end(), a) - lcs.begin();
lcs.at(index) = a;
st.at(index).insert(a);
ans.at(now) = lower_bound(lcs.begin(), lcs.end(), INF) - lcs.begin();
for (ll i = 0; i < connection.at(now).size(); ++i) {
ll next = connection.at(now).at(i);
if (next == before) {
continue;
}
dfs(next, now);
}
st.at(index).erase(lower_bound(st.at(index).begin(), st.at(index).end(), a));
lcs.at(index) = *st.at(index).begin();
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N;
lcs = vector<ll>(N, INF);
ans = vector<ll>(N);
st = vector<multiset<ll>>(N);
for (ll i = 0; i < N; ++i) {
st.at(i).insert(INF);
}
A = vector<ll>(N);
connection = vector<vector<ll>>(N);
for (ll i = 0; i < N; ++i) {
cin >> A.at(i);
}
for (ll i = 0; i < N - 1; ++i) {
ll u, v;
cin >> u >> v;
u -= 1;
v -= 1;
connection.at(u).push_back(v);
connection.at(v).push_back(u);
}
dfs(0, -1);
for (ll i = 0; i < N; ++i) {
cout << ans.at(i) << "\n";
}
}
| #include <bits/stdc++.h>
#define INF 2000000000000000000
#define ll long long
using namespace std;
ll N;
vector<ll> lcs;
vector<multiset<ll>> st;
vector<ll> ans;
vector<vector<ll>> connection;
vector<ll> A;
void dfs(ll now, ll before) {
ll a = A.at(now);
ll index = lower_bound(lcs.begin(), lcs.end(), a) - lcs.begin();
lcs.at(index) = a;
st.at(index).insert(a);
ans.at(now) = lower_bound(lcs.begin(), lcs.end(), INF) - lcs.begin();
for (ll i = 0; i < connection.at(now).size(); ++i) {
ll next = connection.at(now).at(i);
if (next == before) {
continue;
}
dfs(next, now);
}
st.at(index).erase(st.at(index).lower_bound(a));
lcs.at(index) = *st.at(index).begin();
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N;
lcs = vector<ll>(N, INF);
ans = vector<ll>(N);
st = vector<multiset<ll>>(N);
for (ll i = 0; i < N; ++i) {
st.at(i).insert(INF);
}
A = vector<ll>(N);
connection = vector<vector<ll>>(N);
for (ll i = 0; i < N; ++i) {
cin >> A.at(i);
}
for (ll i = 0; i < N - 1; ++i) {
ll u, v;
cin >> u >> v;
u -= 1;
v -= 1;
connection.at(u).push_back(v);
connection.at(v).push_back(u);
}
dfs(0, -1);
for (ll i = 0; i < N; ++i) {
cout << ans.at(i) << "\n";
}
}
| replace | 26 | 27 | 26 | 27 | TLE | |
p02698 | C++ | Runtime Error | // g++ -std=c++11 a.cpp
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include <vector>
#define loop(i, a, b) for (long long i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define FOR(i, a) for (auto i : a)
#define pb push_back
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
#define show1d(v) \
{ \
rep(_, v.size()) cout << " " << v[_]; \
cout << endl; \
}
#define show2d(v) \
{ rep(__, v.size()) show1d(v[__]); }
using namespace std;
// kaewasuretyuui
typedef long long ll;
#define int ll
typedef int Def;
typedef pair<Def, Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def, pii> pip;
typedef vector<pip> vip;
#define mt make_tuple
typedef tuple<int, int, int> tp;
typedef vector<tp> vt;
template <typename A, typename B> bool cmin(A &a, const B &b) {
return a > b ? (a = b, true) : false;
}
template <typename A, typename B> bool cmax(A &a, const B &b) {
return a < b ? (a = b, true) : false;
}
const double PI = acos(-1);
const double EPS = 1e-9;
Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9 + 10;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
#define yes cout << "Yes\n"
#define no cout << "No\n"
// nCr mod m
#define MOD 998244353
#define M 2200000
vector<ll> fact;
bool h = false;
void init() {
h = true;
fact = vector<ll>(M);
fact[0] = fact[1] = 1;
loop(i, 2, M) fact[i] = fact[i - 1] * i % MOD;
}
// a^b mod MOD
ll powmod(ll a, ll b, ll m = MOD) {
ll out = 1;
ll p = a % m;
while (b) {
if (b & 1)
out = out * p % m;
p = p * p % m;
b >>= 1;
}
return out;
}
// nCr
ll nCr(ll n, ll r, ll m = MOD) {
if (!h)
init();
if (n < 0 || r < 0 || n < r)
return 1; //??????
ll out = fact[n] * powmod(fact[r] * fact[n - r] % m, m - 2, m) % m;
return out;
}
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }
int LIS(vi in) { // Longest Increasing Subsequence
int n = in.size();
vi out(n + 1, inf);
rep(i, n) { *lower_bound(all(out), in[i]) = in[i]; }
return lower_bound(all(out), inf) - out.begin();
}
int n;
vi in, out;
vvi G;
vi lis;
void dfs(int a, int p) {
int prei = lower_bound(all(lis), in[a]) - lis.begin();
int pre = lis[prei];
lis[prei] = in[a];
int t = lower_bound(all(lis), inf) - lis.begin();
out[a] = t;
rep(i, G[a].size()) {
int to = G[a][i];
if (p == to)
continue;
dfs(to, a);
}
lis[prei] = pre;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
in = out = vi(n);
rep(i, n) cin >> in[i];
G = vvi(n);
G[n].pb(0);
rep(i, n - 1) {
int a, b;
cin >> a >> b;
a--;
b--;
G[a].pb(b);
G[b].pb(a);
}
lis = vi(n + 1, inf);
dfs(0, -1);
rep(i, n) cout << out[i] << endl;
}
| // g++ -std=c++11 a.cpp
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include <vector>
#define loop(i, a, b) for (long long i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define FOR(i, a) for (auto i : a)
#define pb push_back
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
#define show1d(v) \
{ \
rep(_, v.size()) cout << " " << v[_]; \
cout << endl; \
}
#define show2d(v) \
{ rep(__, v.size()) show1d(v[__]); }
using namespace std;
// kaewasuretyuui
typedef long long ll;
#define int ll
typedef int Def;
typedef pair<Def, Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def, pii> pip;
typedef vector<pip> vip;
#define mt make_tuple
typedef tuple<int, int, int> tp;
typedef vector<tp> vt;
template <typename A, typename B> bool cmin(A &a, const B &b) {
return a > b ? (a = b, true) : false;
}
template <typename A, typename B> bool cmax(A &a, const B &b) {
return a < b ? (a = b, true) : false;
}
const double PI = acos(-1);
const double EPS = 1e-9;
Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9 + 10;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
#define yes cout << "Yes\n"
#define no cout << "No\n"
// nCr mod m
#define MOD 998244353
#define M 2200000
vector<ll> fact;
bool h = false;
void init() {
h = true;
fact = vector<ll>(M);
fact[0] = fact[1] = 1;
loop(i, 2, M) fact[i] = fact[i - 1] * i % MOD;
}
// a^b mod MOD
ll powmod(ll a, ll b, ll m = MOD) {
ll out = 1;
ll p = a % m;
while (b) {
if (b & 1)
out = out * p % m;
p = p * p % m;
b >>= 1;
}
return out;
}
// nCr
ll nCr(ll n, ll r, ll m = MOD) {
if (!h)
init();
if (n < 0 || r < 0 || n < r)
return 1; //??????
ll out = fact[n] * powmod(fact[r] * fact[n - r] % m, m - 2, m) % m;
return out;
}
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }
int LIS(vi in) { // Longest Increasing Subsequence
int n = in.size();
vi out(n + 1, inf);
rep(i, n) { *lower_bound(all(out), in[i]) = in[i]; }
return lower_bound(all(out), inf) - out.begin();
}
int n;
vi in, out;
vvi G;
vi lis;
void dfs(int a, int p) {
int prei = lower_bound(all(lis), in[a]) - lis.begin();
int pre = lis[prei];
lis[prei] = in[a];
int t = lower_bound(all(lis), inf) - lis.begin();
out[a] = t;
rep(i, G[a].size()) {
int to = G[a][i];
if (p == to)
continue;
dfs(to, a);
}
lis[prei] = pre;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
in = out = vi(n);
rep(i, n) cin >> in[i];
G = vvi(n);
rep(i, n - 1) {
int a, b;
cin >> a >> b;
a--;
b--;
G[a].pb(b);
G[b].pb(a);
}
lis = vi(n + 1, inf);
dfs(0, -1);
rep(i, n) cout << out[i] << endl;
}
| delete | 136 | 137 | 136 | 136 | -11 | |
p02698 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define REP(i, s, n) for (int i = s; i < n; i++)
#define NUM 2520
#define INF (1LL << 50)
#define DEBUG 0
#define mp(a, b) make_pair(a, b)
#define SORT(V) sort(V.begin(), V.end())
#define PI (3.141592653589794)
#define TO_STRING(VariableName) #VariableName
#define LOG(x) \
if (DEBUG) \
cout << TO_STRING(x) << "=" << x << " " << endl;
#define LOG2(x, y) \
if (DEBUG) \
cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y << endl;
#define LOG3(x, y, z) \
if (DEBUG) \
cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y << " " \
<< TO_STRING(z) << "=" << z << endl;
#define LOG4(w, x, y, z) \
if (DEBUG) \
cout << TO_STRING(w) << "=" << w << " " << TO_STRING(x) << "=" << x << " " \
<< TO_STRING(y) << "=" << y << " " << TO_STRING(z) << "=" << z \
<< endl;
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;
}
vector<lli> dp;
vector<lli> G[100100];
lli N;
vector<lli> a, u, v;
map<lli, lli> ans;
void dfs(lli from, lli now) {
lli updateMethod;
lli updateIndex;
lli prevNum;
auto itr = lower_bound(dp.begin(), dp.end(), a[now]);
if (itr == dp.end()) {
dp.push_back(a[now]);
updateMethod = 1;
} else {
updateMethod = 2;
updateIndex = (itr)-dp.begin();
prevNum = *itr;
*itr = a[now];
}
ans[now] = dp.size();
for (auto e : G[now]) {
if (e == from)
continue;
dfs(now, e);
}
if (updateMethod == 1) {
dp.pop_back();
} else {
dp[updateIndex] = prevNum;
}
}
void func() {
REP(i, 0, N) dp[i] = INF;
dfs(-1, 0);
for (auto e : ans)
cout << e.second << endl;
}
int main() {
// cout << fixed << setprecision(5);
long long N;
scanf("%lld", &N);
a.resize(N);
for (int i = 0; i < N; i++) {
scanf("%lld", &a[i]);
}
u.resize(N - 1);
v.resize(N - 1);
for (int i = 0; i < N - 1; i++) {
scanf("%lld", &u[i]);
scanf("%lld", &v[i]);
u[i]--, v[i]--;
G[u[i]].push_back(v[i]);
G[v[i]].push_back(u[i]);
}
func();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define REP(i, s, n) for (int i = s; i < n; i++)
#define NUM 2520
#define INF (1LL << 50)
#define DEBUG 0
#define mp(a, b) make_pair(a, b)
#define SORT(V) sort(V.begin(), V.end())
#define PI (3.141592653589794)
#define TO_STRING(VariableName) #VariableName
#define LOG(x) \
if (DEBUG) \
cout << TO_STRING(x) << "=" << x << " " << endl;
#define LOG2(x, y) \
if (DEBUG) \
cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y << endl;
#define LOG3(x, y, z) \
if (DEBUG) \
cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y << " " \
<< TO_STRING(z) << "=" << z << endl;
#define LOG4(w, x, y, z) \
if (DEBUG) \
cout << TO_STRING(w) << "=" << w << " " << TO_STRING(x) << "=" << x << " " \
<< TO_STRING(y) << "=" << y << " " << TO_STRING(z) << "=" << z \
<< endl;
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;
}
vector<lli> dp;
vector<lli> G[200100];
lli N;
vector<lli> a, u, v;
map<lli, lli> ans;
void dfs(lli from, lli now) {
lli updateMethod;
lli updateIndex;
lli prevNum;
auto itr = lower_bound(dp.begin(), dp.end(), a[now]);
if (itr == dp.end()) {
dp.push_back(a[now]);
updateMethod = 1;
} else {
updateMethod = 2;
updateIndex = (itr)-dp.begin();
prevNum = *itr;
*itr = a[now];
}
ans[now] = dp.size();
for (auto e : G[now]) {
if (e == from)
continue;
dfs(now, e);
}
if (updateMethod == 1) {
dp.pop_back();
} else {
dp[updateIndex] = prevNum;
}
}
void func() {
REP(i, 0, N) dp[i] = INF;
dfs(-1, 0);
for (auto e : ans)
cout << e.second << endl;
}
int main() {
// cout << fixed << setprecision(5);
long long N;
scanf("%lld", &N);
a.resize(N);
for (int i = 0; i < N; i++) {
scanf("%lld", &a[i]);
}
u.resize(N - 1);
v.resize(N - 1);
for (int i = 0; i < N - 1; i++) {
scanf("%lld", &u[i]);
scanf("%lld", &v[i]);
u[i]--, v[i]--;
G[u[i]].push_back(v[i]);
G[v[i]].push_back(u[i]);
}
func();
return 0;
}
| replace | 44 | 45 | 44 | 45 | 0 | |
p02698 | C++ | Runtime Error | /*
读题不规范,爆零两行泪。
数据不清空,爆零两行泪。
多测不读完,爆零两行泪。
边界不特判,爆零两行泪。
贪心不证明,爆零两行泪。
D P 顺序错,爆零两行泪。
大小少等号,爆零两行泪。
变量不统一,爆零两行泪。
越界不判断,爆零两行泪。
调试不注释,爆零两行泪。
溢出不 l l,爆零两行泪。
*/
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
const int N = 200000;
int n;
int a[N + 1];
vector<int> nums;
void discrete() {
sort(nums.begin(), nums.end());
nums.resize(unique(nums.begin(), nums.end()) - nums.begin());
for (int i = 1; i <= n; i++)
a[i] = lower_bound(nums.begin(), nums.end(), a[i]) - nums.begin();
}
vector<int> nei[N + 1];
multiset<int> st[N];
struct segtree {
struct node {
int l, r, mx;
} nd[N << 2];
#define l(p) nd[p].l
#define r(p) nd[p].r
#define mx(p) nd[p].mx
void bld(int l = 0, int r = nums.size() - 1, int p = 1) {
l(p) = l;
r(p) = r;
mx(p) = 0;
if (l == r)
return;
int mid = l + r >> 1;
bld(l, mid, p << 1);
bld(mid + 1, r, p << 1 | 1);
}
void init() { bld(); }
void sprup(int p) { mx(p) = max(mx(p << 1), mx(p << 1 | 1)); }
void chg(int x, int v, int p = 1) {
if (l(p) == r(p))
return mx(p) = v, void();
int mid = l(p) + r(p) >> 1;
chg(x, v, p << 1 | (x > mid));
sprup(p);
}
int _mx(int l, int r, int p = 1) {
if (l > r)
return 0;
if (l <= l(p) && r >= r(p))
return mx(p);
int mid = l(p) + r(p) >> 1, res = 0;
if (l <= mid)
res = max(res, _mx(l, r, p << 1));
if (r > mid)
res = max(res, _mx(l, r, p << 1 | 1));
return res;
}
} segt;
int dp[N + 1], ans[N + 1];
void dfs(int x = 1, int fa = 0) {
dp[x] = segt._mx(0, a[x] - 1) + 1;
ans[x] = max(ans[fa], dp[x]);
st[a[x]].insert(dp[x]);
segt.chg(a[x], *--st[a[x]].end());
for (int i = 0; i < nei[x].size(); i++) {
int y = nei[x][i];
if (y == fa)
continue;
dfs(y, x);
}
st[a[x]].erase(st[a[x]].find(dp[x]));
segt.chg(a[x], *--st[a[x]].end());
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i], nums.pb(a[i]);
for (int i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
nei[x].pb(y);
nei[y].pb(x);
}
for (int i = 0; i < nums.size(); i++)
st[i].insert(0);
segt.init();
dfs();
for (int i = 1; i <= n; i++)
cout << ans[i] << "\n";
return 0;
}
/*1
10
1 2 5 3 4 6 7 3 2 4
1 2
2 3
3 4
4 5
3 6
6 7
1 8
8 9
9 10
*/ | /*
读题不规范,爆零两行泪。
数据不清空,爆零两行泪。
多测不读完,爆零两行泪。
边界不特判,爆零两行泪。
贪心不证明,爆零两行泪。
D P 顺序错,爆零两行泪。
大小少等号,爆零两行泪。
变量不统一,爆零两行泪。
越界不判断,爆零两行泪。
调试不注释,爆零两行泪。
溢出不 l l,爆零两行泪。
*/
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
const int N = 200000;
int n;
int a[N + 1];
vector<int> nums;
void discrete() {
sort(nums.begin(), nums.end());
nums.resize(unique(nums.begin(), nums.end()) - nums.begin());
for (int i = 1; i <= n; i++)
a[i] = lower_bound(nums.begin(), nums.end(), a[i]) - nums.begin();
}
vector<int> nei[N + 1];
multiset<int> st[N];
struct segtree {
struct node {
int l, r, mx;
} nd[N << 2];
#define l(p) nd[p].l
#define r(p) nd[p].r
#define mx(p) nd[p].mx
void bld(int l = 0, int r = nums.size() - 1, int p = 1) {
l(p) = l;
r(p) = r;
mx(p) = 0;
if (l == r)
return;
int mid = l + r >> 1;
bld(l, mid, p << 1);
bld(mid + 1, r, p << 1 | 1);
}
void init() { bld(); }
void sprup(int p) { mx(p) = max(mx(p << 1), mx(p << 1 | 1)); }
void chg(int x, int v, int p = 1) {
if (l(p) == r(p))
return mx(p) = v, void();
int mid = l(p) + r(p) >> 1;
chg(x, v, p << 1 | (x > mid));
sprup(p);
}
int _mx(int l, int r, int p = 1) {
if (l > r)
return 0;
if (l <= l(p) && r >= r(p))
return mx(p);
int mid = l(p) + r(p) >> 1, res = 0;
if (l <= mid)
res = max(res, _mx(l, r, p << 1));
if (r > mid)
res = max(res, _mx(l, r, p << 1 | 1));
return res;
}
} segt;
int dp[N + 1], ans[N + 1];
void dfs(int x = 1, int fa = 0) {
dp[x] = segt._mx(0, a[x] - 1) + 1;
ans[x] = max(ans[fa], dp[x]);
st[a[x]].insert(dp[x]);
segt.chg(a[x], *--st[a[x]].end());
for (int i = 0; i < nei[x].size(); i++) {
int y = nei[x][i];
if (y == fa)
continue;
dfs(y, x);
}
st[a[x]].erase(st[a[x]].find(dp[x]));
segt.chg(a[x], *--st[a[x]].end());
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i], nums.pb(a[i]);
discrete();
for (int i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
nei[x].pb(y);
nei[y].pb(x);
}
for (int i = 0; i < nums.size(); i++)
st[i].insert(0);
segt.init();
dfs();
for (int i = 1; i <= n; i++)
cout << ans[i] << "\n";
return 0;
}
/*1
10
1 2 5 3 4 6 7 3 2 4
1 2
2 3
3 4
4 5
3 6
6 7
1 8
8 9
9 10
*/ | insert | 86 | 86 | 86 | 87 | 0 | |
p02698 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define lp(var, start, end) for (ll var = start; var < end; ++var)
#define rlp(var, start, end) for (ll var = start; var >= end; var--)
#define pb push_back
#define mp make_pair
#define pf push_front
#define ff first
#define ss second
#define vll vector<ll>
#define vld vector<ld>
#define pll pair<ll, ll>
#define pld pair<ld, ld>
#define vpll vector<pll>
#define vpld vector<pld>
#define all(X) X.begin(), X.end()
#define endl "\n"
#define sz(x) ((ll)((x).size()))
const ll N = 2e5 + 5, inf = 1e9;
ll ary[N];
ll ans[N];
vll G[N];
ll n;
ll tree[4 * N];
multiset<ll> res;
ll query(ll nd, ll st, ll ed, ll l, ll r) {
if (st > r || ed < l || st > ed)
return 0;
if (st >= l && ed <= r)
return tree[nd];
ll mid = (st + ed) / 2;
return max(query(2 * nd, st, mid, l, r),
query(2 * nd + 1, mid + 1, ed, l, r));
}
void update(ll nd, ll st, ll ed, ll id, ll val) {
if (st >= id && ed <= id) {
tree[nd] = val;
} else {
ll mid = (st + ed) / 2;
if (mid <= id)
update(2 * nd, st, mid, id, val);
else
update(2 * nd + 1, mid + 1, ed, id, val);
tree[nd] = max(tree[2 * nd], tree[2 * nd + 1]);
}
}
ll dfs(ll a, ll p) {
ll x = query(1, 0, n - 1, 0, ary[a - 1] - 1);
ll pre = query(1, 0, n - 1, ary[a - 1], ary[a - 1]);
res.insert(x + 1);
update(1, 0, n - 1, ary[a - 1], x + 1);
ans[a] = *(--res.end());
lp(i, 0, G[a].size()) {
ll x = G[a][i];
if (x != p)
dfs(x, a);
}
update(1, 0, n - 1, ary[a - 1], pre);
res.erase(res.find(x + 1));
return 0;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
vll v;
lp(i, 0, n) {
cin >> ary[i];
v.pb(ary[i]);
}
map<ll, ll> M;
sort(all(v));
ll c = 0;
lp(i, 0, n) {
if (M.count(v[i]) == 0) {
M[v[i]] = c++;
}
}
lp(i, 0, n) { ary[i] = M[ary[i]]; }
lp(i, 0, n - 1) {
ll u, v;
cin >> u >> v;
G[u].pb(v);
G[v].pb(u);
}
dfs(1, 0);
lp(i, 0, n) { cout << ans[i + 1] << endl; }
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define lp(var, start, end) for (ll var = start; var < end; ++var)
#define rlp(var, start, end) for (ll var = start; var >= end; var--)
#define pb push_back
#define mp make_pair
#define pf push_front
#define ff first
#define ss second
#define vll vector<ll>
#define vld vector<ld>
#define pll pair<ll, ll>
#define pld pair<ld, ld>
#define vpll vector<pll>
#define vpld vector<pld>
#define all(X) X.begin(), X.end()
#define endl "\n"
#define sz(x) ((ll)((x).size()))
const ll N = 2e5 + 5, inf = 1e9;
ll ary[N];
ll ans[N];
vll G[N];
ll n;
ll tree[4 * N];
multiset<ll> res;
ll query(ll nd, ll st, ll ed, ll l, ll r) {
if (st > r || ed < l || st > ed)
return 0;
if (st >= l && ed <= r)
return tree[nd];
ll mid = (st + ed) / 2;
return max(query(2 * nd, st, mid, l, r),
query(2 * nd + 1, mid + 1, ed, l, r));
}
void update(ll nd, ll st, ll ed, ll id, ll val) {
if (st >= id && ed <= id) {
tree[nd] = val;
} else {
ll mid = (st + ed) / 2;
if (mid >= id)
update(2 * nd, st, mid, id, val);
else
update(2 * nd + 1, mid + 1, ed, id, val);
tree[nd] = max(tree[2 * nd], tree[2 * nd + 1]);
}
}
ll dfs(ll a, ll p) {
ll x = query(1, 0, n - 1, 0, ary[a - 1] - 1);
ll pre = query(1, 0, n - 1, ary[a - 1], ary[a - 1]);
res.insert(x + 1);
update(1, 0, n - 1, ary[a - 1], x + 1);
ans[a] = *(--res.end());
lp(i, 0, G[a].size()) {
ll x = G[a][i];
if (x != p)
dfs(x, a);
}
update(1, 0, n - 1, ary[a - 1], pre);
res.erase(res.find(x + 1));
return 0;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
vll v;
lp(i, 0, n) {
cin >> ary[i];
v.pb(ary[i]);
}
map<ll, ll> M;
sort(all(v));
ll c = 0;
lp(i, 0, n) {
if (M.count(v[i]) == 0) {
M[v[i]] = c++;
}
}
lp(i, 0, n) { ary[i] = M[ary[i]]; }
lp(i, 0, n - 1) {
ll u, v;
cin >> u >> v;
G[u].pb(v);
G[v].pb(u);
}
dfs(1, 0);
lp(i, 0, n) { cout << ans[i + 1] << endl; }
return 0;
} | replace | 42 | 43 | 42 | 43 | -11 | |
p02698 | C++ | Time Limit Exceeded | #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
template <class T> using V = vector<T>;
template <class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define rep(i, k, n) for (ll i = k; i < (ll)n; ++i)
#define REP(i, n) rep(i, 0, n)
#define ALL(v) v.begin(), v.end()
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
#define DEBUG_VLL(vec) \
REP(sz, vec.size()) \
std::cerr << vec[sz] << (sz == vec.size() - 1 ? '\n' : ' ');
const long long MOD = 1000000007;
const long long HIGHINF = (long long)1e18;
const int INF = (int)1e9;
int n;
vll a;
V<V<int>> edges;
V<int> ans;
vll dp;
void dfs(int i, int p) {
int idx = lower_bound(ALL(dp), a[i]) - dp.begin();
ll tmp = dp[idx];
dp[idx] = a[i];
ans[i] = lower_bound(ALL(dp), HIGHINF) - dp.begin();
for (int e : edges[i]) {
if (e == p)
continue;
dfs(e, i);
}
dp[idx] = tmp;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
a.resize(n);
for (int i = 0; i < n; i++)
cin >> a[i];
edges.resize(n);
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u--, v--;
edges[u].emplace_back(v);
edges[v].emplace_back(u);
}
ans.resize(n);
ans[0] = 1;
for (int e : edges[0]) {
dp.clear();
dp.resize(n, HIGHINF);
dp[0] = a[0];
dfs(e, 0);
}
for (int i = 0; i < n; i++)
cout << ans[i] << '\n';
return 0;
}
| #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
template <class T> using V = vector<T>;
template <class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define rep(i, k, n) for (ll i = k; i < (ll)n; ++i)
#define REP(i, n) rep(i, 0, n)
#define ALL(v) v.begin(), v.end()
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
#define DEBUG_VLL(vec) \
REP(sz, vec.size()) \
std::cerr << vec[sz] << (sz == vec.size() - 1 ? '\n' : ' ');
const long long MOD = 1000000007;
const long long HIGHINF = (long long)1e18;
const int INF = (int)1e9;
int n;
vll a;
V<V<int>> edges;
V<int> ans;
vll dp;
void dfs(int i, int p) {
int idx = lower_bound(ALL(dp), a[i]) - dp.begin();
ll tmp = dp[idx];
dp[idx] = a[i];
ans[i] = lower_bound(ALL(dp), HIGHINF) - dp.begin();
for (int e : edges[i]) {
if (e == p)
continue;
dfs(e, i);
}
dp[idx] = tmp;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
a.resize(n);
for (int i = 0; i < n; i++)
cin >> a[i];
edges.resize(n);
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u--, v--;
edges[u].emplace_back(v);
edges[v].emplace_back(u);
}
ans.resize(n);
dp.resize(n, HIGHINF);
dfs(0, -1);
for (int i = 0; i < n; i++)
cout << ans[i] << '\n';
return 0;
}
| replace | 93 | 100 | 93 | 95 | TLE | |
p02699 | C++ | Runtime Error | #include <cstdio>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int S;
int W;
S = atoi(argv[1]);
W = atoi(argv[2]);
if (W >= S) {
printf("unsafe");
} else {
printf("safe");
}
// printf("羊の数は%d、オオカミの数は%d",S,W);
}
| #include <cstdio>
#include <stdlib.h>
int main() {
int S, W;
scanf("%d%d", &S, &W);
if (W >= S) {
printf("unsafe");
} else {
printf("safe");
}
// printf("羊の数は%d、オオカミの数は%d",S,W);
}
| replace | 3 | 8 | 3 | 6 | -11 | |
p02699 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using Map = map<string, ll>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
ll INF = 1LL << 60;
ll MOD = 1000000007;
int main() {
ll S;
cin >> S;
ll W;
cin >> W;
for (ll i = 0; i < 10000000000; i++) {
swap(S, W);
}
if (S <= W)
cout << "unsafe" << endl;
else
cout << "safe" << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using Map = map<string, ll>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
ll INF = 1LL << 60;
ll MOD = 1000000007;
int main() {
ll S;
cin >> S;
ll W;
cin >> W;
for (ll i = 0; i < 1000000000; i++) {
swap(S, W);
}
if (S <= W)
cout << "unsafe" << endl;
else
cout << "safe" << endl;
return 0;
}
| replace | 15 | 16 | 15 | 16 | TLE | |
p02699 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
vector<string> S(11);
cin >> N;
for (int i = 0; i < 11; i++) {
S.at(i);
}
int count = N;
sort(S.begin(), S.end());
for (int i = 0; i < N - 1; i++) {
if (S.at(i) == S.at(i + 1))
count--;
}
cout << count << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int S, W;
cin >> S >> W;
if (S <= W) {
cout << "unsafe" << endl;
} else
cout << "safe" << endl;
} | replace | 4 | 19 | 4 | 10 | 0 | |
p02699 | Python | Runtime Error | A, B = map(int.input().split())
if A > B:
print("safe")
else:
print("unsafe")
| A, B = map(int, input().split())
if A > B:
print("safe")
else:
print("unsafe")
| replace | 0 | 1 | 0 | 1 | AttributeError: type object 'int' has no attribute 'input' | Traceback (most recent call last):
File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02699/Python/s743314648.py", line 1, in <module>
A, B = map(int.input().split())
AttributeError: type object 'int' has no attribute 'input'
|
p02699 | Python | Runtime Error | s, w = map(int, input().split(""))
res = "safe"
if w >= s:
res = "unsafe"
print(res)
| s, w = map(int, input().split(" "))
res = "safe"
if w >= s:
res = "unsafe"
print(res)
| replace | 0 | 1 | 0 | 1 | ValueError: empty separator | Traceback (most recent call last):
File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02699/Python/s552737943.py", line 1, in <module>
s, w = map(int, input().split(""))
ValueError: empty separator
|
p02699 | Python | Runtime Error | def main():
s, w = map(int, input())
if w >= s:
print("unsafe")
else:
print("safe")
if __name__ == "__main__":
main()
| def main():
s, w = map(int, input().split())
if w >= s:
print("unsafe")
else:
print("safe")
if __name__ == "__main__":
main()
| replace | 1 | 2 | 1 | 2 | ValueError: invalid literal for int() with base 10: ' ' | Traceback (most recent call last):
File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02699/Python/s696020761.py", line 11, in <module>
main()
File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02699/Python/s696020761.py", line 3, in main
s, w = map(int, input())
ValueError: invalid literal for int() with base 10: ' '
|
p02700 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
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;
}
using ll = long long;
ll n, m, k, a, b, c, d;
string s, t;
int main() {
cin >> a >> b >> c >> d;
int cnt = 0;
while (a && c) {
cnt % 2 == 0 ? c -= b : a -= d;
cnt++;
}
cout << (cnt % 2 == 0 ? "No" : "Yes") << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
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;
}
using ll = long long;
ll n, m, k, a, b, c, d;
string s, t;
int main() {
cin >> a >> b >> c >> d;
int cnt = 0;
while (a > 0 && c > 0) {
cnt % 2 == 0 ? c -= b : a -= d;
cnt++;
}
cout << (cnt % 2 == 0 ? "No" : "Yes") << endl;
} | replace | 26 | 27 | 26 | 27 | TLE | |
p02700 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, c, d;
int x = (a + d - 1) / d;
int y = (c + b - 1) / b;
x >= y ? (cout << "YES\n") : (cout << "NO\n");
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, c, d;
cin >> a >> b >> c >> d;
int i = 0;
while (a > 0 && c > 0) {
if (i)
a -= d, i--;
else
c -= b, i++;
}
if (a > 0)
cout << "Yes\n";
else
cout << "No\n";
}
| replace | 4 | 7 | 4 | 16 | 0 | |
p02700 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int A, B, C, D;
cin >> A >> B >> C >> D;
while (true) {
C -= B;
if (C == 0) {
cout << "Yes";
return 1;
}
A -= D;
if (A == 0) {
cout << "No";
return 1;
}
}
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int A, B, C, D;
cin >> A >> B >> C >> D;
int X = (A + D - 1) / D;
int Y = (C + B - 1) / B;
cout << (X >= Y ? "Yes" : "No") << "\n";
} | replace | 6 | 19 | 6 | 9 | 1 | |
p02700 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int A, B, C, D;
cin >> A >> B >> C >> D;
if (A / (A - B) < C / (C - D)) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
int A, B, C, D;
cin >> A >> B >> C >> D;
if (C - B <= 0) {
cout << "Yes" << endl;
} else if (A - D <= 0) {
cout << "No" << endl;
} else if (C % B == 0 && A % D != 0) {
if (C / B <= A / D + 1) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
} else if (C % B != 0 && A % D == 0) {
if (C / B + 1 <= A / D) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
} else if (C / B <= A / D) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
}
| replace | 6 | 7 | 6 | 23 | -8 | |
p02700 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, c, d;
cin >> a >> b >> c >> d;
for (int i = 0; i < (a + c - 1) / c; i++) {
c -= b;
if (c <= 0) {
cout << "Yes" << endl;
}
}
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, c, d;
cin >> a >> b >> c >> d;
int count = 0;
count = (a + d - 1) / d;
if (b * count >= c) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
}
| replace | 6 | 11 | 6 | 12 | 0 | |
p02700 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <climits>
using namespace std;
#define endl '\n'
// #define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define c0(x) cout << (x) << " "
#define c1(x) cout << (x) << endl
#define rep for (ll i = 0; i < n; i++)
#define f(i, s, n) for (ll i = s; i < n; i++)
#define fe(i, s, n) for (ll i = s; i <= n; i++)
#define rfe(i, s, n) for (ll i = s; i >= n; i--)
#define TCs(x) \
ll x; \
cin >> x; \
while (x--)
#define TC(x) \
ll x = 1; \
while (x--)
#define f2(it, v) for (it = v.begin(); it != v.end(); ++it)
#define auto(m) for (auto &it : m)
#define ip(n, a) \
ll n; \
cin >> n; \
ll a[n]; \
rep { cin >> a[i]; }
#define ip2(n, a, b) \
ll n; \
cin >> n; \
ll a[n]; \
ll b[n]; \
rep { cin >> a[i]; } \
rep { cin >> b[i]; }
// #define ip(n,a) ll n;cin>>n;ll a[n];rep{cin>>a[i];}
#define op(n, a) \
f(i, 0, n) { cout << a[i] << " "; }
#define i2p(n, k, a) \
ll n, k; \
cin >> n >> k; \
ll a[n]; \
rep { cin >> a[i]; }
// #define ll long long
// #define ld long double
// #define vl vector<ll>
#define mll map<ll, ll>
#define mcl map<char, ll>
#define msl map<string, ll>
#define mp make_pair
#define pii pair<ll, ll>
#define pb push_back
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define vsort(v) sort(all(v))
#define vrsort(v) reverse(all(v))
#define Max(x, y, z) max(x, max(y, z))
#define Min(x, y, z) min(x, min(y, z))
#define F first
#define S second
// #define ps(x,y) fixed<<setprecision(y)<<x
// #define ps(x) std::cout << std::fixed; std::cout <<
// std::setprecision(x);
#define clz(a) \
__builtin_clz(a) // count leading zeroes
// (geeksforgeeks.org/builtin-functions-gcc-compiler/)
#define ctz(a) __builtin_ctz(a) // count trailing zeroes
// #define ctz(a) __builtin_ctzll(a)
#define popc(a) \
__builtin_popcount(a) // count set bits (for ints only diff for ll)
#define GCD(A, B) __gcd(A, B)
// #define LCM(A,B) boost::math::lcm(A,B)
// #define COUNT(v,x) count(all(v),x)
// #define COUNT(v,x) count(all(s),'x')
// Typedefs:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
typedef long long ll;
typedef long double ld;
// typedef pair<int,int> pii;
typedef pair<long long, long long> pll;
typedef pair<string, string> pss;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef pair<ll, ll> pairs;
// typedef vector<pair<ll, pair<ll, ll>>> vppl;
const int N = 1e5 + 5;
const long long MOD = (long long)(1e9 + 7);
const long long INIT = (long long)(1e6 + 1);
// Functions:----------------------------------------------------------------------------------------------------------------------------------------------------------------------
inline string IntToString(ll a) {
char x[100];
sprintf(x, "%lld", a);
string s = x;
return s;
}
//....................................................................................//
inline ll StringToInt(string a) {
char x[100];
ll res;
strcpy(x, a.c_str());
sscanf(x, "%lld", &res);
return res;
}
//....................................................................................//
inline string GetString(void) {
char x[1000005];
scanf("%s", x);
string s = x;
return s;
}
//....................................................................................//
inline string uppercase(string s) {
int n = s.size();
f(i, 0, n) if (s[i] >= 'a' && s[i] <= 'z') s[i] = s[i] - 'a' + 'A';
return s;
}
//....................................................................................//
inline string lowercase(string s) {
int n = s.size();
f(i, 0, n) if (s[i] >= 'A' && s[i] <= 'Z') s[i] = s[i] - 'A' + 'a';
return s;
}
//....................................................................................//
inline void OPEN(string s) {
#ifndef TESTING
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
#endif
}
//....................................................................................//
template <typename T, typename U> // Same as max_val = max (max_val, val)
static inline void amin(T &x, U y) // Same as min_val = min (min_val,val)
{
if (y < x) // maximum = amax(max_val, val);
x = y; // minimum = amin (min_val, val);
}
//....................................................................................//
template <typename T, typename U> static inline void amax(T &x, U y) {
if (x < y)
x = y;
}
//....................................................................................//
ll power(ll x, ll y) {
if (y == 0)
return 1;
ll p = power(x, y / 2) % MOD;
p = (p * p) % MOD;
return (y % 2 == 0) ? p : (x * p) % MOD;
}
//....................................................................................//
int lcm(ll a, ll b) {
ll lar = max(a, b);
ll small = min(a, b);
for (ll i = lar;; i += lar) {
if (i % small == 0)
return i;
}
}
//....................................................................................//
int bsearch(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;
}
//....................................................................................//
template <typename T> T gcd(T a, T b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
template <typename T> T pow(T a, T b, ll m) {
T ans = 1;
while (b > 0) {
if (b % 2 == 1)
ans = (ans * a) % m;
b /= 2;
a = (a * a) % m;
}
return ans % m;
}
// int bsearch(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;}}}
// Solve:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
void solve() {
ll a, b, c, d;
cin >> a >> b >> c >> d;
ll x = (c + b - 1) / b;
ll y = (a + d - 1) / d;
if (x <= y) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
TCs(t) { solve(); }
return 0;
}
| #include <bits/stdc++.h>
#include <climits>
using namespace std;
#define endl '\n'
// #define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define c0(x) cout << (x) << " "
#define c1(x) cout << (x) << endl
#define rep for (ll i = 0; i < n; i++)
#define f(i, s, n) for (ll i = s; i < n; i++)
#define fe(i, s, n) for (ll i = s; i <= n; i++)
#define rfe(i, s, n) for (ll i = s; i >= n; i--)
#define TCs(x) \
ll x; \
cin >> x; \
while (x--)
#define TC(x) \
ll x = 1; \
while (x--)
#define f2(it, v) for (it = v.begin(); it != v.end(); ++it)
#define auto(m) for (auto &it : m)
#define ip(n, a) \
ll n; \
cin >> n; \
ll a[n]; \
rep { cin >> a[i]; }
#define ip2(n, a, b) \
ll n; \
cin >> n; \
ll a[n]; \
ll b[n]; \
rep { cin >> a[i]; } \
rep { cin >> b[i]; }
// #define ip(n,a) ll n;cin>>n;ll a[n];rep{cin>>a[i];}
#define op(n, a) \
f(i, 0, n) { cout << a[i] << " "; }
#define i2p(n, k, a) \
ll n, k; \
cin >> n >> k; \
ll a[n]; \
rep { cin >> a[i]; }
// #define ll long long
// #define ld long double
// #define vl vector<ll>
#define mll map<ll, ll>
#define mcl map<char, ll>
#define msl map<string, ll>
#define mp make_pair
#define pii pair<ll, ll>
#define pb push_back
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define vsort(v) sort(all(v))
#define vrsort(v) reverse(all(v))
#define Max(x, y, z) max(x, max(y, z))
#define Min(x, y, z) min(x, min(y, z))
#define F first
#define S second
// #define ps(x,y) fixed<<setprecision(y)<<x
// #define ps(x) std::cout << std::fixed; std::cout <<
// std::setprecision(x);
#define clz(a) \
__builtin_clz(a) // count leading zeroes
// (geeksforgeeks.org/builtin-functions-gcc-compiler/)
#define ctz(a) __builtin_ctz(a) // count trailing zeroes
// #define ctz(a) __builtin_ctzll(a)
#define popc(a) \
__builtin_popcount(a) // count set bits (for ints only diff for ll)
#define GCD(A, B) __gcd(A, B)
// #define LCM(A,B) boost::math::lcm(A,B)
// #define COUNT(v,x) count(all(v),x)
// #define COUNT(v,x) count(all(s),'x')
// Typedefs:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
typedef long long ll;
typedef long double ld;
// typedef pair<int,int> pii;
typedef pair<long long, long long> pll;
typedef pair<string, string> pss;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef pair<ll, ll> pairs;
// typedef vector<pair<ll, pair<ll, ll>>> vppl;
const int N = 1e5 + 5;
const long long MOD = (long long)(1e9 + 7);
const long long INIT = (long long)(1e6 + 1);
// Functions:----------------------------------------------------------------------------------------------------------------------------------------------------------------------
inline string IntToString(ll a) {
char x[100];
sprintf(x, "%lld", a);
string s = x;
return s;
}
//....................................................................................//
inline ll StringToInt(string a) {
char x[100];
ll res;
strcpy(x, a.c_str());
sscanf(x, "%lld", &res);
return res;
}
//....................................................................................//
inline string GetString(void) {
char x[1000005];
scanf("%s", x);
string s = x;
return s;
}
//....................................................................................//
inline string uppercase(string s) {
int n = s.size();
f(i, 0, n) if (s[i] >= 'a' && s[i] <= 'z') s[i] = s[i] - 'a' + 'A';
return s;
}
//....................................................................................//
inline string lowercase(string s) {
int n = s.size();
f(i, 0, n) if (s[i] >= 'A' && s[i] <= 'Z') s[i] = s[i] - 'A' + 'a';
return s;
}
//....................................................................................//
inline void OPEN(string s) {
#ifndef TESTING
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
#endif
}
//....................................................................................//
template <typename T, typename U> // Same as max_val = max (max_val, val)
static inline void amin(T &x, U y) // Same as min_val = min (min_val,val)
{
if (y < x) // maximum = amax(max_val, val);
x = y; // minimum = amin (min_val, val);
}
//....................................................................................//
template <typename T, typename U> static inline void amax(T &x, U y) {
if (x < y)
x = y;
}
//....................................................................................//
ll power(ll x, ll y) {
if (y == 0)
return 1;
ll p = power(x, y / 2) % MOD;
p = (p * p) % MOD;
return (y % 2 == 0) ? p : (x * p) % MOD;
}
//....................................................................................//
int lcm(ll a, ll b) {
ll lar = max(a, b);
ll small = min(a, b);
for (ll i = lar;; i += lar) {
if (i % small == 0)
return i;
}
}
//....................................................................................//
int bsearch(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;
}
//....................................................................................//
template <typename T> T gcd(T a, T b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
template <typename T> T pow(T a, T b, ll m) {
T ans = 1;
while (b > 0) {
if (b % 2 == 1)
ans = (ans * a) % m;
b /= 2;
a = (a * a) % m;
}
return ans % m;
}
// int bsearch(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;}}}
// Solve:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
void solve() {
ll a, b, c, d;
cin >> a >> b >> c >> d;
ll x = (c + b - 1) / b;
ll y = (a + d - 1) / d;
if (x <= y) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
TC(t) { solve(); }
return 0;
}
| replace | 216 | 217 | 216 | 217 | TLE | |
p02700 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#ifdef LOCAL_DEBUG
#define DEBUG 1
#define CERR \
if (DEBUG) \
cerr
#define MACRO(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, NAME, ...) NAME
#define pr(...) \
CERR << MACRO(__VA_ARGS__, pr10, pr9, pr8, pr7, pr6, pr5, pr4, pr3, pr2, \
pr1)(__VA_ARGS__) \
<< endl
#define pr1(a) (#a) << "=" << (a) << " "
#define pr2(a, b) pr1(a) << pr1(b)
#define pr3(a, b, c) pr1(a) << pr2(b, c)
#define pr4(a, b, c, d) pr1(a) << pr3(b, c, d)
#define pr5(a, b, c, d, e) pr1(a) << pr4(b, c, d, e)
#define pr6(a, b, c, d, e, f) pr1(a) << pr5(b, c, d, e, f)
#define pr7(a, b, c, d, e, f, g) pr1(a) << pr6(b, c, d, e, f, g)
#define pr8(a, b, c, d, e, f, g, h) pr1(a) << pr7(b, c, d, e, f, g, h)
#define pr9(a, b, c, d, e, f, g, h, i) pr1(a) << pr8(b, c, d, e, f, g, h, i)
#define pr10(a, b, c, d, e, f, g, h, i, j) \
pr1(a) << pr9(b, c, d, e, f, g, h, i, j)
#define prArr(a) \
{ \
CERR << (#a) << "={"; \
int i = 0; \
for (auto t : (a)) \
CERR << (i++ ? ", " : "") << t; \
CERR << "}" << endl; \
}
#else
#define DEBUG 0
#define pr(...)
#define prArr(a)
#endif
using namespace std;
using Int = long long;
using _int = int;
using ll = long long;
using Double = long double;
const Int INF = (1LL << 60) + 1e9; // ~ 1.15 * 1e18
const Int mod = (1e9) + 7;
const Double EPS = 1e-8;
const Double PI = 6.0 * asin((Double)0.5);
using P = pair<Int, Int>;
template <class T> T Max(T &a, T b) { return a = max(a, b); }
template <class T> T Min(T &a, T b) { return a = min(a, b); }
template <class T1, class T2> ostream &operator<<(ostream &o, pair<T1, T2> p) {
return o << "(" << p.first << "," << p.second << ")";
}
template <class T1, class T2, class T3>
ostream &operator<<(ostream &o, tuple<T1, T2, T3> t) {
return o << "(" << get<0>(t) << "," << get<1>(t) << "," << get<2>(t) << ")";
}
template <class T1, class T2> istream &operator>>(istream &i, pair<T1, T2> &p) {
return i >> p.first >> p.second;
}
template <class T> ostream &operator<<(ostream &o, vector<T> a) {
Int i = 0;
for (T t : a)
o << (i++ ? " " : "") << t;
return o;
}
template <class T> istream &operator>>(istream &i, vector<T> &a) {
for (T &t : a)
i >> t;
return i;
}
// INSERT ABOVE HERE
signed main() {
srand((unsigned)time(NULL));
cin.tie(0);
ios_base::sync_with_stdio(0);
cout << fixed << setprecision(12);
Int A, B, C, D;
cin >> A >> B >> C >> D;
Int turn = 0;
while (A != 0 && C != 0) {
if (turn == 0)
C -= B;
else
A -= D;
turn = !turn;
}
cout << (turn ? "Yes" : "No") << endl;
return 0;
}
| #include <bits/stdc++.h>
#ifdef LOCAL_DEBUG
#define DEBUG 1
#define CERR \
if (DEBUG) \
cerr
#define MACRO(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, NAME, ...) NAME
#define pr(...) \
CERR << MACRO(__VA_ARGS__, pr10, pr9, pr8, pr7, pr6, pr5, pr4, pr3, pr2, \
pr1)(__VA_ARGS__) \
<< endl
#define pr1(a) (#a) << "=" << (a) << " "
#define pr2(a, b) pr1(a) << pr1(b)
#define pr3(a, b, c) pr1(a) << pr2(b, c)
#define pr4(a, b, c, d) pr1(a) << pr3(b, c, d)
#define pr5(a, b, c, d, e) pr1(a) << pr4(b, c, d, e)
#define pr6(a, b, c, d, e, f) pr1(a) << pr5(b, c, d, e, f)
#define pr7(a, b, c, d, e, f, g) pr1(a) << pr6(b, c, d, e, f, g)
#define pr8(a, b, c, d, e, f, g, h) pr1(a) << pr7(b, c, d, e, f, g, h)
#define pr9(a, b, c, d, e, f, g, h, i) pr1(a) << pr8(b, c, d, e, f, g, h, i)
#define pr10(a, b, c, d, e, f, g, h, i, j) \
pr1(a) << pr9(b, c, d, e, f, g, h, i, j)
#define prArr(a) \
{ \
CERR << (#a) << "={"; \
int i = 0; \
for (auto t : (a)) \
CERR << (i++ ? ", " : "") << t; \
CERR << "}" << endl; \
}
#else
#define DEBUG 0
#define pr(...)
#define prArr(a)
#endif
using namespace std;
using Int = long long;
using _int = int;
using ll = long long;
using Double = long double;
const Int INF = (1LL << 60) + 1e9; // ~ 1.15 * 1e18
const Int mod = (1e9) + 7;
const Double EPS = 1e-8;
const Double PI = 6.0 * asin((Double)0.5);
using P = pair<Int, Int>;
template <class T> T Max(T &a, T b) { return a = max(a, b); }
template <class T> T Min(T &a, T b) { return a = min(a, b); }
template <class T1, class T2> ostream &operator<<(ostream &o, pair<T1, T2> p) {
return o << "(" << p.first << "," << p.second << ")";
}
template <class T1, class T2, class T3>
ostream &operator<<(ostream &o, tuple<T1, T2, T3> t) {
return o << "(" << get<0>(t) << "," << get<1>(t) << "," << get<2>(t) << ")";
}
template <class T1, class T2> istream &operator>>(istream &i, pair<T1, T2> &p) {
return i >> p.first >> p.second;
}
template <class T> ostream &operator<<(ostream &o, vector<T> a) {
Int i = 0;
for (T t : a)
o << (i++ ? " " : "") << t;
return o;
}
template <class T> istream &operator>>(istream &i, vector<T> &a) {
for (T &t : a)
i >> t;
return i;
}
// INSERT ABOVE HERE
signed main() {
srand((unsigned)time(NULL));
cin.tie(0);
ios_base::sync_with_stdio(0);
cout << fixed << setprecision(12);
Int A, B, C, D;
cin >> A >> B >> C >> D;
Int turn = 0;
while (A > 0 && C > 0) {
if (turn == 0)
C -= B;
else
A -= D;
turn = !turn;
}
cout << (turn ? "Yes" : "No") << endl;
return 0;
}
| replace | 80 | 81 | 80 | 81 | TLE | |
p02700 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll MOD = 1000000007;
int main(int argc, char **argv) {
ll a, b, c, d;
cin >> a >> b >> c >> d;
bool t{true};
while (1) {
if (t)
c -= b;
else
a -= d;
if (a == 0 || c == 0)
break;
t ^= true;
}
std::cout << (t ? "Yes" : "No") << std::endl;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll MOD = 1000000007;
int main(int argc, char **argv) {
ll a, b, c, d;
cin >> a >> b >> c >> d;
bool t{true};
while (1) {
if (t)
c -= b;
else
a -= d;
if (a <= 0 || c <= 0)
break;
t ^= true;
}
std::cout << (t ? "Yes" : "No") << std::endl;
}
| replace | 17 | 18 | 17 | 18 | TLE | |
p02700 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, c, d;
cin >> a >> b >> c >> d;
int res = 0;
while (a > 0 || c > 0) {
if ((c - b) <= 0) {
res = 1;
break;
} else if ((a - d) <= 0) {
res = -1;
break;
}
}
if (res == 1)
cout << "Yes";
else
cout << "No";
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, c, d;
cin >> a >> b >> c >> d;
int res = 0;
while (a > 0 || c > 0) {
if ((c - b) <= 0) {
res = 1;
break;
} else if ((a - d) <= 0) {
res = -1;
break;
}
c = c - b;
a = a - d;
}
if (res == 1)
cout << "Yes";
else
cout << "No";
}
| insert | 17 | 17 | 17 | 19 | TLE | |
p02700 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int A, B, C, D;
cin >> A >> B >> C >> D;
while (A != 0 || B != 0) {
A = A - C;
D = D - B;
}
if (A == 0) {
cout << "No" << endl;
} else {
cout << "Yes" << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int A, B, C, D;
cin >> A >> B >> C >> D;
while (A != 0 || C != 0) {
C = C - B;
if (C <= 0) {
cout << "Yes";
break;
}
A = A - D;
if (A <= 0) {
cout << "No";
break;
}
}
return 0;
} | replace | 7 | 15 | 7 | 18 | TLE | |
p02700 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ll a, b, c, d;
cin >> a >> b >> c >> d;
int turn = 1;
while (c != 0 && a != 0) {
if (turn == 1) {
turn ^= 1;
c = c - b;
} else {
turn ^= 1;
a = a - d;
}
}
if (turn == 0)
cout << "Yes";
else
cout << "No";
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ll a, b, c, d;
cin >> a >> b >> c >> d;
int turn = 1;
while (c > 0 && a > 0) {
if (turn == 1) {
turn ^= 1;
c = c - b;
} else {
turn ^= 1;
a = a - d;
}
}
if (turn == 0)
cout << "Yes";
else
cout << "No";
}
| replace | 7 | 8 | 7 | 8 | TLE | |
p02700 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
int A, B, C, D;
cin >> A >> B >> C >> D;
while (true) {
bool chokudai = false;
for (int i = 0; i < 101; i++) {
A -= B;
C -= D;
if (A < 0) {
chokudai = true;
}
if (C < 0) {
chokudai = true;
}
if (A < C) {
cout << "Yes" << endl;
} else if (A > C) {
cout << "No" << endl;
}
}
}
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
int A, B, C, D;
cin >> A >> B >> C >> D;
if ((A + D - 1) / D >= (C + B - 1) / B) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
}
| replace | 6 | 23 | 6 | 12 | TLE | |
p02700 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main() {
ll a, b, c, d, count = 1;
cin >> a >> b >> c >> d;
while (a != 0 && c != 0) {
if (count % 2 != 0)
c = c - b;
else
a = a - d;
count++;
}
if (a <= 0)
cout << "No" << endl;
else
cout << "Yes" << endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main() {
ll a, b, c, d, count = 1;
cin >> a >> b >> c >> d;
while (a > 0 && c > 0) {
if (count % 2 != 0)
c = c - b;
else
a = a - d;
count++;
}
if (a <= 0)
cout << "No" << endl;
else
cout << "Yes" << endl;
} | replace | 6 | 7 | 6 | 7 | TLE | |
p02700 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
// #ifndef ONLINE_JUDGE
// # define LOG(x) (cerr << #x << " = " << (x) << endl)
// #else
// # define LOG(x) 0
// #endif
#define pb push_back
#define mod 1000000007
#define endl '\n'
typedef int integer;
#define int long long
using namespace std;
int powmod(int a, int b) {
int res = 1;
a %= mod;
assert(b >= 0);
for (; b; b >>= 1) {
if (b & 1)
res = res * a % mod;
a = a * a % mod;
}
return res;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
const integer NN = 200005;
int ar[NN];
int n, m, k;
integer main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int a, b, c, d;
cin >> a >> b >> c >> d;
k = 1;
while (a && c) {
if (k) {
c -= b;
} else {
a -= d;
}
k ^= 1;
}
if (!k)
cout << "Yes";
else
cout << "No";
} | #include <bits/stdc++.h>
// #ifndef ONLINE_JUDGE
// # define LOG(x) (cerr << #x << " = " << (x) << endl)
// #else
// # define LOG(x) 0
// #endif
#define pb push_back
#define mod 1000000007
#define endl '\n'
typedef int integer;
#define int long long
using namespace std;
int powmod(int a, int b) {
int res = 1;
a %= mod;
assert(b >= 0);
for (; b; b >>= 1) {
if (b & 1)
res = res * a % mod;
a = a * a % mod;
}
return res;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
const integer NN = 200005;
int ar[NN];
int n, m, k;
integer main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int a, b, c, d;
cin >> a >> b >> c >> d;
k = 1;
while (a > 0 && c > 0) {
if (k) {
c -= b;
} else {
a -= d;
}
k ^= 1;
}
if (!k)
cout << "Yes";
else
cout << "No";
} | replace | 38 | 39 | 38 | 39 | TLE | |
p02700 | Python | Time Limit Exceeded | ht, st, ha, sa = list(map(int, input().split(" ")))
bool_player = True # Takashi
while ht > 0 or st > 0:
if bool_player:
ha -= st
bool_player = False
else:
ht -= sa
bool_player = True
if ht <= 0:
print("No")
else:
print("Yes")
| ht, st, ha, sa = list(map(int, input().split(" ")))
bool_player = True # Takashi
while ht > 0 and ha > 0:
if bool_player:
ha -= st
bool_player = False
else:
ht -= sa
bool_player = True
if ht <= 0:
print("No")
else:
print("Yes")
| replace | 2 | 3 | 2 | 3 | TLE | |
p02700 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define ll long long
#define ull unsigned long long
#define si set<ll>
#define vi vector<ll>
#define popcount(x) __builtin_popcountll(x)
#define mii map<ll, ll>
#define vpi vector<pair<ll, ll>>
#define sz(c) (int)c.size()
#define fr first
#define ll long long
#define fastio \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define sc second
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define mem0(a) memset(a, 0, sizeof(a))
#define rep(i, a, n) for (ll i = a; i < n; i++)
#define ld long double
#define rall(a) (a).rbegin(), (a).rend()
ll power(ll b, ll e, ll m) {
if (e == 0)
return 1;
if (e & 1)
return b * power(b * b % m, e / 2, m) % m;
return power(b * b % m, e / 2, m);
}
ll power(ll b, ll e) {
if (e == 0)
return 1;
if (e & 1)
return b * power(b * b, e / 2);
return power(b * b, e / 2);
}
bool isPowerOfTwo(ll x) {
// x will check if x == 0 and !(x & (x - 1)) will check if x is a power of 2
// or not
return (x && !(x & (x - 1)));
}
void solve() {
ll a, b, c, d;
cin >> a >> b >> c >> d;
while (c and a) {
c -= b;
a -= d;
}
if (c <= 0)
cout << "Yes";
else
cout << "No";
}
int main() {
fastio ll tt = 1;
// cin>>tt;
while (tt--) {
solve();
cout << "\n";
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define ll long long
#define ull unsigned long long
#define si set<ll>
#define vi vector<ll>
#define popcount(x) __builtin_popcountll(x)
#define mii map<ll, ll>
#define vpi vector<pair<ll, ll>>
#define sz(c) (int)c.size()
#define fr first
#define ll long long
#define fastio \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define sc second
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define mem0(a) memset(a, 0, sizeof(a))
#define rep(i, a, n) for (ll i = a; i < n; i++)
#define ld long double
#define rall(a) (a).rbegin(), (a).rend()
ll power(ll b, ll e, ll m) {
if (e == 0)
return 1;
if (e & 1)
return b * power(b * b % m, e / 2, m) % m;
return power(b * b % m, e / 2, m);
}
ll power(ll b, ll e) {
if (e == 0)
return 1;
if (e & 1)
return b * power(b * b, e / 2);
return power(b * b, e / 2);
}
bool isPowerOfTwo(ll x) {
// x will check if x == 0 and !(x & (x - 1)) will check if x is a power of 2
// or not
return (x && !(x & (x - 1)));
}
void solve() {
ll a, b, c, d;
cin >> a >> b >> c >> d;
while (c > 0 and a > 0) {
c -= b;
a -= d;
}
if (c <= 0)
cout << "Yes";
else
cout << "No";
}
int main() {
fastio ll tt = 1;
// cin>>tt;
while (tt--) {
solve();
cout << "\n";
}
return 0;
}
| replace | 49 | 50 | 49 | 50 | TLE | |
p02700 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
long long a, b, c, d;
int main() {
cin >> a >> b >> c >> d;
while (a && c) {
c -= b;
if (c == 0)
break;
a -= d;
if (a == 0)
break;
}
if (a)
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
long long a, b, c, d;
int main() {
cin >> a >> b >> c >> d;
if ((c + b - 1) / b <= (a + d - 1) / d)
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
} | replace | 7 | 16 | 7 | 8 | TLE | |
p02700 | Python | Time Limit Exceeded | A, B, C, D = list(map(int, input().split()))
while A and C:
A -= D
C -= B
if C <= 0:
print("Yes")
elif A <= 0:
print("No")
| A, B, C, D = list(map(int, input().split()))
t1 = (A + D - 1) // D
t2 = (C + B - 1) // B
print("Yes" if t2 <= t1 else "No")
| replace | 1 | 8 | 1 | 4 | TLE | |
p02701 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main() {
int n;
cin >> n;
vector<string> s;
rep(i, n) cin >> s[i];
set<string> a;
rep(i, n) a.insert(s[i]);
int ans = a.size();
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main() {
int n;
cin >> n;
vector<string> s(n);
rep(i, n) cin >> s[i];
set<string> a;
rep(i, n) a.insert(s[i]);
int ans = a.size();
cout << ans << endl;
}
| replace | 7 | 8 | 7 | 8 | -11 | |
p02701 | C++ | Runtime Error | #include <algorithm>
#include <cstring>
#include <iostream>
#include <set>
#include <vector>
#define MAX_N 100000
using namespace std;
long long N;
string S[MAX_N];
set<string> gift;
int main() {
cin >> N;
for (int i = 0; i < N; i++) {
cin >> S[i];
gift.insert(S[i]);
}
cout << gift.size();
return 0;
}
| #include <algorithm>
#include <cstring>
#include <iostream>
#include <set>
#include <vector>
#define MAX_N 200000
using namespace std;
long long N;
string S[MAX_N];
set<string> gift;
int main() {
cin >> N;
for (int i = 0; i < N; i++) {
cin >> S[i];
gift.insert(S[i]);
}
cout << gift.size();
return 0;
}
| replace | 5 | 6 | 5 | 6 | 0 | |
p02701 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <map>
#include <memory.h>
#include <string>
#include <vector>
using namespace std;
int main() {
int n;
cin >> n;
map<string, int> t;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
t[s];
};
cout << t.size() << endl;
return 1;
} | #include <algorithm>
#include <iostream>
#include <map>
#include <memory.h>
#include <string>
#include <vector>
using namespace std;
int main() {
int n;
cin >> n;
map<string, int> t;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
t[s];
};
cout << t.size() << endl;
return 0;
}
| replace | 23 | 24 | 23 | 24 | 1 | |
p02701 | C++ | Time Limit Exceeded | #include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
int main(void) {
int n;
cin >> n;
vector<size_t> v(20000);
int sum = 0;
unordered_map<size_t, int> um;
for (int i = 0; i < n; i++) {
string str;
cin >> str;
size_t hs = hash<string>()(str);
auto itr = lower_bound(v.begin(), v.end(), hs);
if (itr == v.end()) {
v.push_back(hs);
sum++;
} else if (*itr != hs) {
v.push_back(hs);
sort(v.begin(), v.end());
sum++;
} else {
}
}
cout << sum << endl;
return 0;
} | #include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
int main(void) {
int n;
cin >> n;
vector<size_t> v(20000);
int sum = 0;
unordered_map<size_t, int> um;
for (int i = 0; i < n; i++) {
string str;
cin >> str;
size_t hs = hash<string>()(str);
auto itr = lower_bound(v.begin(), v.end(), hs);
if (itr == v.end()) {
v.push_back(hs);
sum++;
} else if (*itr != hs) {
v.insert(itr, hs);
sum++;
} else {
}
}
cout << sum << endl;
return 0;
} | replace | 27 | 29 | 27 | 28 | TLE | |
p02701 | C++ | Runtime Error | #include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
const int maxn = 2e5 + 100;
int tree[maxn][30];
bool flag[maxn];
int tot;
void insertt(string s) {
int len = s.size();
int root = 0;
for (int i = 0; i < len; i++) {
int id = s[i] - 'a';
if (!tree[root][id])
tree[root][id] = ++tot;
root = tree[root][id];
}
flag[root] = true;
}
bool findt(string s) {
int len = s.size();
int root = 0;
for (int i = 0; i < len; i++) {
int id = s[i] - 'a';
if (!tree[root][id])
return true;
root = tree[root][id];
}
if (flag[root])
return false;
else
return true;
}
string s;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
int ans = 0;
for (int i = 1; i <= n; i++) {
cin >> s;
if (findt(s)) {
ans++;
insertt(s);
}
}
cout << ans;
return 0;
} | #include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
const int maxn = 5e6 + 100;
int tree[maxn][30];
bool flag[maxn];
int tot;
void insertt(string s) {
int len = s.size();
int root = 0;
for (int i = 0; i < len; i++) {
int id = s[i] - 'a';
if (!tree[root][id])
tree[root][id] = ++tot;
root = tree[root][id];
}
flag[root] = true;
}
bool findt(string s) {
int len = s.size();
int root = 0;
for (int i = 0; i < len; i++) {
int id = s[i] - 'a';
if (!tree[root][id])
return true;
root = tree[root][id];
}
if (flag[root])
return false;
else
return true;
}
string s;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
int ans = 0;
for (int i = 1; i <= n; i++) {
cin >> s;
if (findt(s)) {
ans++;
insertt(s);
}
}
cout << ans;
return 0;
} | replace | 9 | 10 | 9 | 10 | 0 | |
p02701 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <math.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N, ans;
ans = 1;
cin >> N;
vector<string> vec(N);
for (int i = 0; i < N; i++) {
cin >> vec.at(i);
}
sort(vec.begin(), vec.end());
for (int i = 0; i < N; i++) {
if (vec.at(i) != vec.at(i + 1)) {
ans++;
}
}
cout << ans << "\n";
} | #include <algorithm>
#include <iostream>
#include <math.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N, ans;
ans = 1;
cin >> N;
vector<string> vec(N);
for (int i = 0; i < N; i++) {
cin >> vec.at(i);
}
sort(vec.begin(), vec.end());
for (int i = 0; i < N - 1; i++) {
if (vec.at(i) != vec.at(i + 1)) {
ans++;
}
}
cout << ans << "\n";
} | replace | 22 | 23 | 22 | 23 | -6 | terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check: __n (which is 3) >= this->size() (which is 3)
|
p02701 | Python | Runtime Error | import collections
n = int(input())
a = [list(input() for i in range(n))]
b = collections.Counter(a)
print(len(b))
| import collections
n = int(input())
a = [input() for i in range(n)]
b = collections.Counter(a)
print(len(b))
| replace | 3 | 4 | 3 | 4 | TypeError: unhashable type: 'list' | Traceback (most recent call last):
File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02701/Python/s293744193.py", line 5, in <module>
b = collections.Counter(a)
File "/usr/lib/python3.10/collections/__init__.py", line 577, in __init__
self.update(iterable, **kwds)
File "/usr/lib/python3.10/collections/__init__.py", line 670, in update
_count_elements(self, iterable)
TypeError: unhashable type: 'list'
|
p02701 | Python | Runtime Error | n = int(input)
lis = [input() for i in range(n)]
print(len(set(lis)))
| n = int(input())
lis = [input() for i in range(n)]
print(len(set(lis)))
| replace | 0 | 1 | 0 | 1 | TypeError: int() argument must be a string, a bytes-like object or a real number, not 'builtin_function_or_method' | Traceback (most recent call last):
File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02701/Python/s310001383.py", line 1, in <module>
n = int(input)
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'builtin_function_or_method'
|
p02701 | Python | Runtime Error | N = input()
List = list(input() for i in range(N))
print(len(set(List)))
| N = int(input())
List = list(input() for i in range(N))
print(len(set(List)))
| replace | 0 | 1 | 0 | 1 | TypeError: 'str' object cannot be interpreted as an integer | Traceback (most recent call last):
File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02701/Python/s903293880.py", line 2, in <module>
List = list(input() for i in range(N))
TypeError: 'str' object cannot be interpreted as an integer
|
p02701 | Python | Runtime Error | n = int(input())
s = (input() for _ in range(n))
print(len(s))
| n = int(input())
s = {input() for _ in range(n)}
print(len(s))
| replace | 1 | 2 | 1 | 2 | TypeError: object of type 'generator' has no len() | Traceback (most recent call last):
File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02701/Python/s709317664.py", line 3, in <module>
print(len(s))
TypeError: object of type 'generator' has no len()
|
p02701 | Python | Time Limit Exceeded | n = int(input())
ans = 0
u_l = []
for _ in range(n):
s = input()
if s not in u_l:
u_l.append(s)
ans += 1
print(ans)
| n = int(input())
l = [input() for _ in range(n)]
print(len(set(l)))
| replace | 2 | 10 | 2 | 4 | TLE | |
p02701 | Python | Time Limit Exceeded | import sys
def main():
readline = sys.stdin.readline
N = int(readline())
li = []
for _ in range(N):
S = readline()
if S not in li:
li.append(S)
print(len(li))
if __name__ == "__main__":
main()
| import sys
def main():
readline = sys.stdin.readline
N = int(readline())
se = set([readline() for _ in range(N)])
print(len(se))
if __name__ == "__main__":
main()
| replace | 6 | 12 | 6 | 8 | TLE | |
p02701 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define rep11(i, n) for (int i = 1; i < (int)(n); ++i)
#define rep0(i, n) for (int i = 1; i <= (int)(n); ++i)
#define repo(i, o, n) for (int i = o; i < (int)(n); ++i)
#define repm(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define pb(n) push_back(n)
#define mp make_pair
#define MOD 1002000007
#define INF LO
int main() {
int n;
vector<string> s;
cin >> n;
rep(i, n) {
string S;
cin >> S;
s.push_back(S);
}
rep(i, n) { sort(s.begin(), s.end()); }
int ans = 1;
rep(i, n - 1) {
if (s[i] != s[i + 1]) {
ans++;
}
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define rep11(i, n) for (int i = 1; i < (int)(n); ++i)
#define rep0(i, n) for (int i = 1; i <= (int)(n); ++i)
#define repo(i, o, n) for (int i = o; i < (int)(n); ++i)
#define repm(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define pb(n) push_back(n)
#define mp make_pair
#define MOD 1002000007
#define INF LO
int main() {
int n;
vector<string> s;
cin >> n;
rep(i, n) {
string S;
cin >> S;
s.push_back(S);
}
sort(s.begin(), s.end());
int ans = 1;
rep(i, n - 1) {
if (s[i] != s[i + 1]) {
ans++;
}
}
cout << ans << endl;
}
| replace | 28 | 29 | 28 | 31 | TLE | |
p02701 | Python | Runtime Error | n = int(input())
s = [int(input()) for _ in range(n)]
ans = set()
for i in s:
ans.add(i)
print(len(ans))
| n = int(input())
s = [input() for _ in range(n)]
ans = set()
for i in s:
ans.add(i)
print(len(ans))
| replace | 1 | 2 | 1 | 2 | ValueError: invalid literal for int() with base 10: 'apple' | Traceback (most recent call last):
File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02701/Python/s962277082.py", line 2, in <module>
s = [int(input()) for _ in range(n)]
File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02701/Python/s962277082.py", line 2, in <listcomp>
s = [int(input()) for _ in range(n)]
ValueError: invalid literal for int() with base 10: 'apple'
|
p02701 | Python | Runtime Error | a = map(int, input().split())
s = list(map(int, input().split()))
print(len(set(s)))
| a = int(input())
s = [input() for i in range(a)]
print(len(set(s)))
| replace | 0 | 2 | 0 | 2 | ValueError: invalid literal for int() with base 10: 'apple' | Traceback (most recent call last):
File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02701/Python/s227226698.py", line 2, in <module>
s = list(map(int, input().split()))
ValueError: invalid literal for int() with base 10: 'apple'
|
p02701 | Python | Runtime Error | import collections
N = int(input())
S = []
for i in range(N):
S.append(input())
c = collections.counter(S)
print(len(c))
| import collections
N = int(input())
S = []
for i in range(N):
S.append(input())
c = collections.Counter(S)
print(len(c))
| replace | 8 | 9 | 8 | 9 | AttributeError: module 'collections' has no attribute 'counter'. Did you mean: 'Counter'? | Traceback (most recent call last):
File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02701/Python/s677938637.py", line 9, in <module>
c = collections.counter(S)
AttributeError: module 'collections' has no attribute 'counter'. Did you mean: 'Counter'?
|
p02701 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
using in = int64_t;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define yes cout << "yes" << endl
#define no cout << "no" << endl
#define vec(a, y, x) vector<vector<char>> a(y, vector<char>(x))
const double PI = 3.14159265358979323846;
const in MOD = 1000000007;
const in INF = 1e18 + 7;
const int inf = 1e9 + 7;
const vector<int> dx = {1, 0, -1, 0};
const vector<int> dy = {0, 1, 0, -1};
int main() {
int n;
cin >> n;
vector<string> num = {};
int ans = 1;
rep(i, n) {
string a;
cin >> a;
if (find(all(num), a) == num.end()) {
// ans++;
num.push_back(a);
// sort(all(num));
}
}
sort(all(num));
for (int i = 1; i < n; i++) {
if (num.at(i - 1) != num.at(i))
ans++;
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
using in = int64_t;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define yes cout << "yes" << endl
#define no cout << "no" << endl
#define vec(a, y, x) vector<vector<char>> a(y, vector<char>(x))
const double PI = 3.14159265358979323846;
const in MOD = 1000000007;
const in INF = 1e18 + 7;
const int inf = 1e9 + 7;
const vector<int> dx = {1, 0, -1, 0};
const vector<int> dy = {0, 1, 0, -1};
int main() {
int n;
cin >> n;
vector<string> num = {};
int ans = 1;
rep(i, n) {
string a;
cin >> a;
num.push_back(a);
}
sort(all(num));
for (int i = 1; i < n; i++) {
if (num.at(i - 1) != num.at(i))
ans++;
}
cout << ans << endl;
} | replace | 26 | 31 | 26 | 28 | -6 | terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check: __n (which is 2) >= this->size() (which is 2)
|
p02701 | C++ | Runtime Error | #include <bits/stdc++.h>
#define LL long long
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
using namespace std;
const int mn = 1e5 + 7;
string s[mn], t;
int main() {
int n, ans = 0;
scanf("%d", &n);
FOR(i, 1, n) cin >> s[i];
sort(s + 1, s + n + 1);
FOR(i, 1, n) if (s[i] != t) t = s[i], ans++;
cout << ans;
} | #include <bits/stdc++.h>
#define LL long long
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
using namespace std;
const int mn = 2e5 + 7;
string s[mn], t;
int main() {
int n, ans = 0;
scanf("%d", &n);
FOR(i, 1, n) cin >> s[i];
sort(s + 1, s + n + 1);
FOR(i, 1, n) if (s[i] != t) t = s[i], ans++;
cout << ans;
} | replace | 5 | 6 | 5 | 6 | 0 | |
p02701 | C++ | Runtime Error | #include <algorithm>
#include <assert.h>
#include <bitset>
#include <cfloat>
#include <complex>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i <= n; i++)
// ----GCC ONLY---- //
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// ---- ---- //
#define ll long long
#define PB push_back
#define MOD 10000007
#define PI 3.14159265359
using namespace std;
int main() {
int n;
vector<string> s{100010};
cin >> n;
rep(i, n) cin >> s[i];
int count = 0;
sort(s.begin(), s.begin() + n);
for (int i = 0; i < n; i++) {
if (s.at(i) == s.at(i + 1)) {
count += 0;
} else {
count++;
}
}
cout << count << "\n";
}
| #include <algorithm>
#include <assert.h>
#include <bitset>
#include <cfloat>
#include <complex>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i <= n; i++)
// ----GCC ONLY---- //
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// ---- ---- //
#define ll long long
#define PB push_back
#define MOD 10000007
#define PI 3.14159265359
using namespace std;
int main() {
int n;
vector<string> s{20000000};
cin >> n;
rep(i, n) cin >> s[i];
int count = 0;
sort(s.begin(), s.begin() + n);
for (int i = 0; i < n; i++) {
if (s.at(i) == s.at(i + 1)) {
count += 0;
} else {
count++;
}
}
cout << count << "\n";
}
| replace | 40 | 41 | 40 | 41 | 0 | |
p02701 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
typedef long long LL;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
template <typename Container>
bool include(const Container &c, const typename Container::value_type &v) {
return (c.end() != std::find(c.begin(), c.end(), v));
}
using namespace std;
int main() {
int N;
cin >> N;
vector<string> S(N);
int cnt = 0;
rep(i, N) {
string tmp;
cin >> tmp;
if (!include(S, tmp)) {
cnt++;
}
S[i] = tmp;
}
cout << cnt << endl;
return 0;
}
| #include <bits/stdc++.h>
typedef long long LL;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
template <typename Container>
bool include(const Container &c, const typename Container::value_type &v) {
return (c.end() != std::find(c.begin(), c.end(), v));
}
using namespace std;
int main() {
int N;
cin >> N;
vector<string> S(N);
int cnt = 0;
rep(i, N) cin >> S[i];
std::sort(S.begin(), S.end());
S.erase(std::unique(S.begin(), S.end()), S.end());
cout << S.size() << endl;
return 0;
}
| replace | 17 | 26 | 17 | 23 | TLE | |
p02701 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n;
cin >> n;
vector<string> s(n);
int count = 1;
for (int i = 0; i < n; i++) {
cin >> s[n];
}
sort(s.begin(), s.end());
for (int i = 0; i < n - 1; i++) {
if (s[i] != s[i + 1]) {
count++;
}
}
cout << count << endl;
return 0;
} | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n;
cin >> n;
vector<string> s(n);
int count = 1;
for (int i = 0; i < n; i++) {
cin >> s[i];
}
sort(s.begin(), s.end());
for (int i = 0; i < n - 1; i++) {
if (s[i] != s[i + 1]) {
count++;
}
}
cout << count << endl;
return 0;
} | replace | 13 | 14 | 13 | 14 | -11 | |
p02701 | C++ | Runtime Error | #include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main() {
unordered_map<string, int> m;
int n;
string s;
std::cin >> n;
while (n > 0) {
std::cin >> s;
if (m.count(s)) {
m[s] += 1;
} else {
m.insert(pair<string, int>(s, 1));
}
n--;
}
return m.size();
} | #include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main() {
unordered_map<string, int> m;
int n;
string s;
std::cin >> n;
while (n > 0) {
std::cin >> s;
if (m.count(s)) {
m[s] += 1;
} else {
m.insert(pair<string, int>(s, 1));
}
n--;
}
cout << m.size() << "\n";
return 0;
} | replace | 18 | 19 | 18 | 20 | 2 | |
p02701 | Python | Runtime Error | N = int(input())
S = []
while True:
Si = input()
if Si:
S.append(Si)
else:
break
g = S[:N]
print(g)
| n = int(input())
s = set(input() for _ in range(n))
print(len(s))
| replace | 0 | 10 | 0 | 3 | EOFError: EOF when reading a line | Traceback (most recent call last):
File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02701/Python/s240465038.py", line 4, in <module>
Si = input()
EOFError: EOF when reading a line
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.