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 |
|---|---|---|---|---|---|---|---|---|---|---|---|
p02903 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define pi 3.14159265358979323846264338327950L
using namespace std;
int main() {
int H, W, A, B;
cin >> H >> W >> A >> B;
if (B != 0) {
if (A != 0) {
for (int i = 0; i < B; i++) {
for (int j = 0; j < A; j++) {
cout << "0";
}
for (int j = A; j < W; j++) {
cout << "1";
}
cout << endl;
}
for (int i = B; i < H; i++) {
for (int j = 0; j < A; j++) {
cout << "1";
}
for (int j = A; j < W; j++) {
cout << "0";
}
cout << endl;
}
} else {
for (int i = 0; i < B; i++) {
for (int i = 0; i < W; i++) {
cout << "1";
}
cout << endl;
}
for (int i = B; i < H; i++) {
for (int i = 0; i < W; i++) {
cout << "0";
}
cout << endl;
}
}
} else {
if (A != 0) {
for (int i = 0; i < H; i++) {
for (int j = 0; i < A; j++) {
cout << "1";
}
for (int j = A; j < W; j++) {
cout << "0";
}
cout << endl;
}
} else {
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cout << "0";
}
cout << endl;
}
}
}
} | #include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define pi 3.14159265358979323846264338327950L
using namespace std;
int main() {
int H, W, A, B;
cin >> H >> W >> A >> B;
if (B != 0) {
if (A != 0) {
for (int i = 0; i < B; i++) {
for (int j = 0; j < A; j++) {
cout << "0";
}
for (int j = A; j < W; j++) {
cout << "1";
}
cout << endl;
}
for (int i = B; i < H; i++) {
for (int j = 0; j < A; j++) {
cout << "1";
}
for (int j = A; j < W; j++) {
cout << "0";
}
cout << endl;
}
} else {
for (int i = 0; i < B; i++) {
for (int i = 0; i < W; i++) {
cout << "1";
}
cout << endl;
}
for (int i = B; i < H; i++) {
for (int i = 0; i < W; i++) {
cout << "0";
}
cout << endl;
}
}
} else {
if (A != 0) {
for (int i = 0; i < H; i++) {
for (int j = 0; j < A; j++) {
cout << "1";
}
for (int j = A; j < W; j++) {
cout << "0";
}
cout << endl;
}
} else {
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cout << "0";
}
cout << endl;
}
}
}
} | replace | 59 | 60 | 59 | 60 | TLE | |
p02903 | C++ | Runtime Error | #include "bits/stdc++.h"
using namespace std;
typedef long long int ll;
int main() {
ll H, W, A, B;
cin >> H >> W >> A >> B;
ll n_rows = H;
ll n_cols = W;
ll value = 0;
vector<vector<ll>> vec(W, vector<ll>(H, 0));
;
for (ll i = 0; i < H; i++) {
for (ll j = 0; j < W; j++) {
if (i < B && j < A) {
vec[i][j] = 0;
} else if (i >= B && j < A) {
vec[i][j] = 1;
} else if (i < B && j >= A) {
vec[i][j] = 1;
} else if (i >= B && j >= A) {
vec[i][j] = 0;
}
}
}
for (ll i = 0; i < H; i++) {
for (ll j = 0; j < W; j++) {
cout << vec[i][j];
}
cout << endl;
}
} | #include "bits/stdc++.h"
using namespace std;
typedef long long int ll;
int main() {
ll H, W, A, B;
cin >> H >> W >> A >> B;
ll n_rows = H;
ll n_cols = W;
ll value = 0;
vector<vector<ll>> vec(H, vector<ll>(W, 0));
;
for (ll i = 0; i < H; i++) {
for (ll j = 0; j < W; j++) {
if (i < B && j < A) {
vec[i][j] = 0;
} else if (i >= B && j < A) {
vec[i][j] = 1;
} else if (i < B && j >= A) {
vec[i][j] = 1;
} else if (i >= B && j >= A) {
vec[i][j] = 0;
}
}
}
for (ll i = 0; i < H; i++) {
for (ll j = 0; j < W; j++) {
cout << vec[i][j];
}
cout << endl;
}
} | replace | 13 | 14 | 13 | 14 | 0 | |
p02903 | C++ | Runtime Error | #include <iostream>
#include <math.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
int main() {
int H, W, A, B, i, j;
cin >> H >> W >> A >> B;
int h = H - 1;
int w = W - 1;
int matrix[h][w];
for (i = 0; i <= B - 1; i++) {
for (j = 0; j <= A - 1; j++) {
matrix[i][j] = 0;
}
for (j = A; j <= w; j++) {
matrix[i][j] = 1;
}
}
for (i = B; i <= h; i++) {
for (j = 0; j <= A - 1; j++) {
matrix[i][j] = 1;
}
for (j = A; j <= w; j++) {
matrix[i][j] = 0;
}
}
for (i = 0; i < H; i++) {
for (j = 0; j < W; j++) {
cout << matrix[i][j];
}
cout << endl;
}
return 0;
} | #include <iostream>
#include <math.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
int main() {
int H, W, A, B, i, j;
cin >> H >> W >> A >> B;
int h = H - 1;
int w = W - 1;
int matrix[H][W];
for (i = 0; i <= B - 1; i++) {
for (j = 0; j <= A - 1; j++) {
matrix[i][j] = 0;
}
for (j = A; j <= w; j++) {
matrix[i][j] = 1;
}
}
for (i = B; i <= h; i++) {
for (j = 0; j <= A - 1; j++) {
matrix[i][j] = 1;
}
for (j = A; j <= w; j++) {
matrix[i][j] = 0;
}
}
for (i = 0; i < H; i++) {
for (j = 0; j < W; j++) {
cout << matrix[i][j];
}
cout << endl;
}
return 0;
} | replace | 11 | 12 | 11 | 12 | 0 | |
p02903 | C++ | Runtime Error | #include <bits/stdc++.h>
#define ll long long
#define MODV 1000000007
#define INFLL LLONG_MAX // 9223372036854775807
#define EPS 1e-9
#define rep(i, n) for (ll i = 0, i##_len = (ll)(n); i < i##_len; i++)
#define repf(i, n) for (ll i = 1, i##_len = (ll)(n + 1); i < i##_len; i++)
#define all(v) v.begin(), v.end()
#define endl "\n"
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define Yes() cout << "Yes" << endl
#define YES() cout << "YES" << endl
#define No() cout << "No" << endl
#define NO() cout << "NO" << endl
#define Init() \
std::ios::sync_with_stdio(false); \
std::cin.tie(0); \
std::cout << fixed << setprecision(15);
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;
}
using namespace std;
int main() {
Init();
ll h, w, a, b;
cin >> h >> w >> a >> b;
string zo = string(max(a, b), '0') + string(w - max(a, b), '1');
string oz = string(max(a, b), '1') + string(w - max(a, b), '0');
rep(i, h) cout << (i < a ? zo : oz) << endl;
} | #include <bits/stdc++.h>
#define ll long long
#define MODV 1000000007
#define INFLL LLONG_MAX // 9223372036854775807
#define EPS 1e-9
#define rep(i, n) for (ll i = 0, i##_len = (ll)(n); i < i##_len; i++)
#define repf(i, n) for (ll i = 1, i##_len = (ll)(n + 1); i < i##_len; i++)
#define all(v) v.begin(), v.end()
#define endl "\n"
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define Yes() cout << "Yes" << endl
#define YES() cout << "YES" << endl
#define No() cout << "No" << endl
#define NO() cout << "NO" << endl
#define Init() \
std::ios::sync_with_stdio(false); \
std::cin.tie(0); \
std::cout << fixed << setprecision(15);
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;
}
using namespace std;
int main() {
Init();
ll h, w, a, b;
cin >> h >> w >> a >> b;
string zo = string(a, '0') + string(w - a, '1');
string oz = string(a, '1') + string(w - a, '0');
rep(i, h) cout << (i < b ? zo : oz) << endl;
} | replace | 39 | 42 | 39 | 42 | 0 | |
p02903 | C++ | Runtime Error | #include <algorithm>
#include <array>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
// using namespace std;
#define Rep(i, a, b) for (int i = a; i < b; i++)
#define rep(i, b) Rep(i, 0, b)
#define allof(a) (a).begin(), (a).end()
typedef long long ll;
const int inf = 1e9 + 7;
const ll infll = 1ll << 60ll;
const ll mod = 1e9 + 7;
// 0~3までは右左下上 4~7までは斜め
constexpr int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr int dy[] = {0, -1, 0, 1, 1, -1, -1, 1};
template <typename T> void chmax(T &a, T b) { a = std::max(a, b); }
template <typename T> void chmin(T &a, T b) { a = std::min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }
namespace { // 名前なし名前空間
// 最大公約数
ll gcd(ll a, ll b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
// 最小公倍数
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
} // namespace
int main() {
int h, w, a, b;
std::cin >> h >> w >> b >> a;
std::vector<std::vector<int>> ans(h, std::vector<int>(w));
// 逆説的に解ける?
// 一行目の少ないほうを 0 とし, ans[0][0~a] = 0 とする
rep(j, a) ans[0][j] = 0;
Rep(j, a, w) ans[0][j] = 1;
int overA = 0;
rep(i, h) {
if (i >= a)
overA = 1;
int overB = 0;
rep(j, w) {
if (j >= b)
overB = 1;
int cnt = overA + overB;
ans[i][j] = cnt % 2;
}
}
rep(i, h) {
rep(j, w) { std::cout << ans[i][j]; }
std::cout << std::endl;
}
return 0;
}
| #include <algorithm>
#include <array>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
// using namespace std;
#define Rep(i, a, b) for (int i = a; i < b; i++)
#define rep(i, b) Rep(i, 0, b)
#define allof(a) (a).begin(), (a).end()
typedef long long ll;
const int inf = 1e9 + 7;
const ll infll = 1ll << 60ll;
const ll mod = 1e9 + 7;
// 0~3までは右左下上 4~7までは斜め
constexpr int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr int dy[] = {0, -1, 0, 1, 1, -1, -1, 1};
template <typename T> void chmax(T &a, T b) { a = std::max(a, b); }
template <typename T> void chmin(T &a, T b) { a = std::min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }
namespace { // 名前なし名前空間
// 最大公約数
ll gcd(ll a, ll b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
// 最小公倍数
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
} // namespace
int main() {
int h, w, a, b;
std::cin >> h >> w >> b >> a;
std::vector<std::vector<int>> ans(h, std::vector<int>(w));
// 逆説的に解ける?
// 一行目の少ないほうを 0 とし, ans[0][0~a] = 0 とする
int overA = 0;
rep(i, h) {
if (i >= a)
overA = 1;
int overB = 0;
rep(j, w) {
if (j >= b)
overB = 1;
int cnt = overA + overB;
ans[i][j] = cnt % 2;
}
}
rep(i, h) {
rep(j, w) { std::cout << ans[i][j]; }
std::cout << std::endl;
}
return 0;
}
| delete | 57 | 59 | 57 | 57 | 0 | |
p02903 | C++ | Runtime Error | #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
template <class S, class T>
ostream &operator<<(ostream &out, const pair<S, T> &o) {
out << "(" << o.first << "," << o.second << ")";
return out;
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &V) {
for (int i = 0; i < V.size(); i++) {
out << V[i];
if (i != V.size() - 1)
out << " ";
}
return out;
}
template <class T>
ostream &operator<<(ostream &out, const vector<vector<T>> &Mat) {
for (int i = 0; i < Mat.size(); i++) {
if (i != 0)
out << endl;
out << Mat[i];
}
return out;
}
template <class S, class T>
ostream &operator<<(ostream &out, const map<S, T> &mp) {
out << "{ ";
for (auto it = mp.begin(); it != mp.end(); it++) {
out << it->first << ":" << it->second;
if (mp.size() - 1 != distance(mp.begin(), it))
out << ", ";
}
out << " }";
return out;
}
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
t = v;
}
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
for (auto &e : t)
fill_v(e, v);
}
/*
<url:>
問題文============================================================
=================================================================
解説=============================================================
================================================================
*/
template <class Type> Type solve(Type res = Type()) {
int H, W, A, B;
cin >> H >> W >> A >> B;
vector<string> M(H, string(W, '1'));
for (int i = 0; i < A; i++) {
for (int j = 0; j < B; j++) {
M[i][j] = '0';
}
}
for (int i = B; i < H; i++) {
for (int j = A; j < W; j++) {
M[i][j] = '0';
}
}
for (auto s : M)
cout << s << endl;
return res;
}
int main(void) {
cin.tie(0);
ios_base::sync_with_stdio(false);
solve(0);
// cout << fixed << setprecision(15) << solve<ll>() << endl;
return 0;
}
| #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
template <class S, class T>
ostream &operator<<(ostream &out, const pair<S, T> &o) {
out << "(" << o.first << "," << o.second << ")";
return out;
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &V) {
for (int i = 0; i < V.size(); i++) {
out << V[i];
if (i != V.size() - 1)
out << " ";
}
return out;
}
template <class T>
ostream &operator<<(ostream &out, const vector<vector<T>> &Mat) {
for (int i = 0; i < Mat.size(); i++) {
if (i != 0)
out << endl;
out << Mat[i];
}
return out;
}
template <class S, class T>
ostream &operator<<(ostream &out, const map<S, T> &mp) {
out << "{ ";
for (auto it = mp.begin(); it != mp.end(); it++) {
out << it->first << ":" << it->second;
if (mp.size() - 1 != distance(mp.begin(), it))
out << ", ";
}
out << " }";
return out;
}
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
t = v;
}
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
for (auto &e : t)
fill_v(e, v);
}
/*
<url:>
問題文============================================================
=================================================================
解説=============================================================
================================================================
*/
template <class Type> Type solve(Type res = Type()) {
int H, W, A, B;
cin >> H >> W >> A >> B;
vector<string> M(H, string(W, '1'));
for (int i = 0; i < B; i++) {
for (int j = 0; j < A; j++) {
M[i][j] = '0';
}
}
for (int i = B; i < H; i++) {
for (int j = A; j < W; j++) {
M[i][j] = '0';
}
}
for (auto s : M)
cout << s << endl;
return res;
}
int main(void) {
cin.tie(0);
ios_base::sync_with_stdio(false);
solve(0);
// cout << fixed << setprecision(15) << solve<ll>() << endl;
return 0;
}
| replace | 68 | 70 | 68 | 70 | 0 | |
p02903 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using uint = unsigned int;
using pcc = pair<char, char>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;
using tuplis = pair<ll, pll>;
using tuplis2 = pair<pll, ll>;
template <class T> using pq = priority_queue<T, vector<T>, greater<T>>;
const ll LINF = 0x1fffffffffffffff;
const ll MINF = 0x7fffffffffff;
const int INF = 0x3fffffff;
const ll MOD = 1000000007;
const ll MODD = 998244353;
const ld DINF = numeric_limits<ld>::infinity();
const ld EPS = 1e-9;
const ld PI = 3.141592653589793238462643383279;
const vector<ll> four{0, 1, 0, -1, 0};
#define _overload4(_1, _2, _3, _4, name, ...) name
#define _overload3(_1, _2, _3, name, ...) name
#define _rep1(n) for (ll i = 0; i < n; ++i)
#define _rep2(i, n) for (ll i = 0; i < n; ++i)
#define _rep3(i, a, b) for (ll i = a; i < b; ++i)
#define _rep4(i, a, b, c) for (ll i = a; i < b; i += c)
#define rep(...) \
_overload4(__VA_ARGS__, _rep4, _rep3, _rep2, _rep1)(__VA_ARGS__)
#define _rrep1(n) for (ll i = (n)-1; i >= 0; i--)
#define _rrep2(i, n) for (ll i = (n)-1; i >= 0; i--)
#define _rrep3(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define _rrep4(i, a, b, c) for (ll i = a + (b - a - 1) / c * c; i >= a; i -= c)
#define rrep(...) \
_overload4(__VA_ARGS__, _rrep4, _rrep3, _rrep2, _rrep1)(__VA_ARGS__)
#define each(i, a) for (auto &i : a)
#define sum(...) accumulate(range(__VA_ARGS__), 0LL)
#define dsum(...) accumulate(range(__VA_ARGS__), double(0))
#define _range(i) (i).begin(), (i).end()
#define _range2(i, k) (i).begin(), (i).begin() + k
#define _range3(i, a, b) (i).begin() + a, (i).begin() + b
#define range(...) \
_overload3(__VA_ARGS__, _range3, _range2, _range)(__VA_ARGS__)
#define _rrange(i) (i).rbegin(), (i).rend()
#define _rrange2(i, k) (i).rbegin(), (i).rbegin() + k
#define _rrange3(i, a, b) (i).rbegin() + a, (i).rbegin() + b
#define rrange(...) \
_overload3(__VA_ARGS__, _rrange3, _rrange2, _rrange)(__VA_ARGS__)
#define elif else if
#define unless(a) if (!(a))
#define mp make_pair
#define mt make_tuple
#define INT(...) \
int __VA_ARGS__; \
in(__VA_ARGS__)
#define LL(...) \
ll __VA_ARGS__; \
in(__VA_ARGS__)
#define ULL(...) \
ull __VA_ARGS__; \
in(__VA_ARGS__)
#define STR(...) \
string __VA_ARGS__; \
in(__VA_ARGS__)
#define CHR(...) \
char __VA_ARGS__; \
in(__VA_ARGS__)
#define DBL(...) \
double __VA_ARGS__; \
in(__VA_ARGS__)
#define vec(type, name, ...) vector<type> name(__VA_ARGS__)
#define VEC(type, name, size) \
vector<type> name(size); \
in(name)
#define vv(type, name, h, ...) \
vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define VV(type, name, h, ...) \
vector<vector<type>> name(h, vector<type>(__VA_ARGS__)); \
in(name)
#define vvv(type, name, h, w, ...) \
vector<vector<vector<type>>> name( \
h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
inline constexpr ll gcd(ll a, ll b) {
if (!a || !b)
return 0;
while (b) {
ll c = b;
b = a % b;
a = c;
}
return a;
}
inline constexpr ll lcm(ll a, ll b) {
if (!a || !b)
return 0;
return a * b / gcd(a, b);
}
template <class T> inline constexpr T min(vector<T> &v) {
return *min_element(range(v));
}
inline char min(string &v) { return *min_element(range(v)); }
template <class T> inline constexpr T max(vector<T> &v) {
return *max_element(range(v));
}
inline char max(string &v) { return *max_element(range(v)); }
inline constexpr ll intpow(ll a, ll b) {
ll ans = 1;
for (ll i = 1; b; i *= 2) {
if (b & i) {
b ^= i;
ans *= a;
}
a *= a;
}
return ans;
}
template <typename T> inline constexpr bool chmin(T &mn, const T &cnt) {
if (mn > cnt) {
mn = cnt;
return 1;
} else
return 0;
}
template <typename T> inline constexpr bool chmax(T &mx, const T &cnt) {
if (mx < cnt) {
mx = cnt;
return 1;
} else
return 0;
}
template <class T> unordered_map<T, ll> press(vector<T> &a) {
auto b = a;
sort(range(b));
b.erase(unique(range(b)), b.end());
unordered_map<T, ll> ans;
rep(b.size()) ans[b[i]] = i;
each(i, a) i = ans[i];
return ans;
}
template <class T> map<T, ll> press_map(vector<T> &a) {
auto b = a;
sort(range(b));
b.erase(unique(range(b)), b.end());
map<T, ll> ans;
rep(b.size()) ans[b[i]] = i;
each(i, a) i = ans[i];
return ans;
}
inline int scan() { return getchar(); }
inline void scan(int &a) { scanf("%d", &a); }
inline void scan(unsigned &a) { scanf("%u", &a); }
inline void scan(long &a) { scanf("%ld", &a); }
inline void scan(long long &a) { scanf("%lld", &a); }
inline void scan(unsigned long long &a) { scanf("%llu", &a); }
inline void scan(char &a) { cin >> a; }
inline void scan(float &a) { scanf("%f", &a); }
inline void scan(double &a) { scanf("%lf", &a); }
inline void scan(long double &a) { scanf("%Lf", &a); }
inline void scan(vector<bool> &vec) {
for (unsigned i = 0; i < vec.size(); i++) {
int a;
scan(a);
vec[i] = a;
}
}
inline void scan(char a[]) { scanf("%s", a); }
inline void scan(string &a) { cin >> a; }
template <class T> inline void scan(vector<T> &vec);
template <class T, size_t size> inline void scan(array<T, size> &vec);
template <class T, class L> inline void scan(pair<T, L> &p);
template <class T, size_t size> inline void scan(T (&vec)[size]);
template <class T> inline void scan(vector<T> &vec) {
for (auto &i : vec)
scan(i);
}
template <class T, size_t size> inline void scan(array<T, size> &vec) {
for (auto &i : vec)
scan(i);
}
template <class T, class L> inline void scan(pair<T, L> &p) {
scan(p.first);
scan(p.second);
}
template <class T, size_t size> inline void scan(T (&vec)[size]) {
for (auto &i : vec)
scan(i);
}
template <class T> inline void scan(T &a) { cin >> a; }
inline void in() {}
template <class Head, class... Tail> inline void in(Head &head, Tail &...tail) {
scan(head);
in(tail...);
}
inline void print() { putchar(' '); }
inline void print(const bool &a) { printf("%d", a); }
inline void print(const int &a) { printf("%d", a); }
inline void print(const unsigned &a) { printf("%u", a); }
inline void print(const long &a) { printf("%ld", a); }
inline void print(const long long &a) { printf("%lld", a); }
inline void print(const unsigned long long &a) { printf("%llu", a); }
inline void print(const char &a) { printf("%c", a); }
inline void print(const char a[]) { printf("%s", a); }
inline void print(const float &a) { printf("%.15f", a); }
inline void print(const double &a) { printf("%.15f", a); }
inline void print(const long double &a) { printf("%.15Lf", a); }
template <class T> void print(const vector<T> &vec);
template <class T, size_t size> void print(const array<T, size> &vec);
template <class T, class L> void print(const pair<T, L> &p);
template <class T, size_t size> inline void print(const T (&vec)[size]);
template <class T> void print(const vector<T> &vec) {
if (vec.empty())
return;
print(vec[0]);
for (auto i = vec.begin(); ++i != vec.end();) {
putchar(' ');
print(*i);
}
}
template <class T, size_t size> void print(const array<T, size> &vec) {
print(vec[0]);
for (auto i = vec.begin(); ++i != vec.end();) {
putchar(' ');
print(*i);
}
}
template <class T, class L> void print(const pair<T, L> &p) {
print(p.first);
putchar(' ');
print(p.second);
}
template <class T, size_t size> inline void print(const T (&vec)[size]) {
print(vec[0]);
for (auto i = vec; ++i != end(vec);) {
putchar(' ');
print(*i);
}
}
template <class T> inline void print(const T &a) { cout << a; }
inline int out() {
putchar('\n');
return 0;
}
template <class T> inline int out(const T &t) {
print(t);
putchar('\n');
return 0;
}
template <class Head, class... Tail>
inline int out(const Head &head, const Tail &...tail) {
print(head);
putchar(' ');
out(tail...);
return 0;
}
template <class T> inline void err(T t) { cerr << t << '\n'; }
template <class Head, class... Tail>
inline void err(const Head &head, const Tail &...tail) {
cerr << head << ' ';
err(tail...);
}
inline void err() { cerr << '\n'; }
inline int first(const bool &i) { return out(i ? "first" : "second"); }
inline int yes(const bool &i) { return out(i ? "yes" : "no"); }
inline int Yes(const bool &i) { return out(i ? "Yes" : "No"); }
inline int YES(const bool &i) { return out(i ? "YES" : "NO"); }
inline int Yay(const bool &i) { return out(i ? "Yay!" : ":("); }
inline int Possible(const bool &i) {
return out(i ? "Possible" : "Impossible");
}
inline int POSSIBLE(const bool &i) {
return out(i ? "POSSIBLE" : "IMPOSSIBLE");
}
inline void Case(ll i) { printf("Case #%lld: ", i); }
signed main() {
LL(h, w, a, b);
vector<pll> A, B;
rrep(h + 1) A.emplace_back(i * a + (h - i) * (w - a), i);
rrep(w + 1) B.emplace_back(i * b + (w - i) * (h - b), i);
ll at = 0, at2 = 0;
while (at < A.size() && at2 < B.size()) {
if (A[at].first < B[at2].first) {
at++;
}
elif (A[at].first > B[at2].first) { at2++; }
else {
vec(string, ans, h, string(w, '0'));
ll k = 0;
rep(A[at].second + 1) {
rep(_, a) {
ans[i][k] = '1';
k++;
if (k >= w)
k -= w;
}
}
rep(i, A[at].second, h) {
rep(_, w - a) {
ans[i][k] = '1';
k++;
if (k >= w)
k -= w;
}
}
each(i, ans) out(i);
return 0;
}
}
Yes(0);
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using uint = unsigned int;
using pcc = pair<char, char>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;
using tuplis = pair<ll, pll>;
using tuplis2 = pair<pll, ll>;
template <class T> using pq = priority_queue<T, vector<T>, greater<T>>;
const ll LINF = 0x1fffffffffffffff;
const ll MINF = 0x7fffffffffff;
const int INF = 0x3fffffff;
const ll MOD = 1000000007;
const ll MODD = 998244353;
const ld DINF = numeric_limits<ld>::infinity();
const ld EPS = 1e-9;
const ld PI = 3.141592653589793238462643383279;
const vector<ll> four{0, 1, 0, -1, 0};
#define _overload4(_1, _2, _3, _4, name, ...) name
#define _overload3(_1, _2, _3, name, ...) name
#define _rep1(n) for (ll i = 0; i < n; ++i)
#define _rep2(i, n) for (ll i = 0; i < n; ++i)
#define _rep3(i, a, b) for (ll i = a; i < b; ++i)
#define _rep4(i, a, b, c) for (ll i = a; i < b; i += c)
#define rep(...) \
_overload4(__VA_ARGS__, _rep4, _rep3, _rep2, _rep1)(__VA_ARGS__)
#define _rrep1(n) for (ll i = (n)-1; i >= 0; i--)
#define _rrep2(i, n) for (ll i = (n)-1; i >= 0; i--)
#define _rrep3(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define _rrep4(i, a, b, c) for (ll i = a + (b - a - 1) / c * c; i >= a; i -= c)
#define rrep(...) \
_overload4(__VA_ARGS__, _rrep4, _rrep3, _rrep2, _rrep1)(__VA_ARGS__)
#define each(i, a) for (auto &i : a)
#define sum(...) accumulate(range(__VA_ARGS__), 0LL)
#define dsum(...) accumulate(range(__VA_ARGS__), double(0))
#define _range(i) (i).begin(), (i).end()
#define _range2(i, k) (i).begin(), (i).begin() + k
#define _range3(i, a, b) (i).begin() + a, (i).begin() + b
#define range(...) \
_overload3(__VA_ARGS__, _range3, _range2, _range)(__VA_ARGS__)
#define _rrange(i) (i).rbegin(), (i).rend()
#define _rrange2(i, k) (i).rbegin(), (i).rbegin() + k
#define _rrange3(i, a, b) (i).rbegin() + a, (i).rbegin() + b
#define rrange(...) \
_overload3(__VA_ARGS__, _rrange3, _rrange2, _rrange)(__VA_ARGS__)
#define elif else if
#define unless(a) if (!(a))
#define mp make_pair
#define mt make_tuple
#define INT(...) \
int __VA_ARGS__; \
in(__VA_ARGS__)
#define LL(...) \
ll __VA_ARGS__; \
in(__VA_ARGS__)
#define ULL(...) \
ull __VA_ARGS__; \
in(__VA_ARGS__)
#define STR(...) \
string __VA_ARGS__; \
in(__VA_ARGS__)
#define CHR(...) \
char __VA_ARGS__; \
in(__VA_ARGS__)
#define DBL(...) \
double __VA_ARGS__; \
in(__VA_ARGS__)
#define vec(type, name, ...) vector<type> name(__VA_ARGS__)
#define VEC(type, name, size) \
vector<type> name(size); \
in(name)
#define vv(type, name, h, ...) \
vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define VV(type, name, h, ...) \
vector<vector<type>> name(h, vector<type>(__VA_ARGS__)); \
in(name)
#define vvv(type, name, h, w, ...) \
vector<vector<vector<type>>> name( \
h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
inline constexpr ll gcd(ll a, ll b) {
if (!a || !b)
return 0;
while (b) {
ll c = b;
b = a % b;
a = c;
}
return a;
}
inline constexpr ll lcm(ll a, ll b) {
if (!a || !b)
return 0;
return a * b / gcd(a, b);
}
template <class T> inline constexpr T min(vector<T> &v) {
return *min_element(range(v));
}
inline char min(string &v) { return *min_element(range(v)); }
template <class T> inline constexpr T max(vector<T> &v) {
return *max_element(range(v));
}
inline char max(string &v) { return *max_element(range(v)); }
inline constexpr ll intpow(ll a, ll b) {
ll ans = 1;
for (ll i = 1; b; i *= 2) {
if (b & i) {
b ^= i;
ans *= a;
}
a *= a;
}
return ans;
}
template <typename T> inline constexpr bool chmin(T &mn, const T &cnt) {
if (mn > cnt) {
mn = cnt;
return 1;
} else
return 0;
}
template <typename T> inline constexpr bool chmax(T &mx, const T &cnt) {
if (mx < cnt) {
mx = cnt;
return 1;
} else
return 0;
}
template <class T> unordered_map<T, ll> press(vector<T> &a) {
auto b = a;
sort(range(b));
b.erase(unique(range(b)), b.end());
unordered_map<T, ll> ans;
rep(b.size()) ans[b[i]] = i;
each(i, a) i = ans[i];
return ans;
}
template <class T> map<T, ll> press_map(vector<T> &a) {
auto b = a;
sort(range(b));
b.erase(unique(range(b)), b.end());
map<T, ll> ans;
rep(b.size()) ans[b[i]] = i;
each(i, a) i = ans[i];
return ans;
}
inline int scan() { return getchar(); }
inline void scan(int &a) { scanf("%d", &a); }
inline void scan(unsigned &a) { scanf("%u", &a); }
inline void scan(long &a) { scanf("%ld", &a); }
inline void scan(long long &a) { scanf("%lld", &a); }
inline void scan(unsigned long long &a) { scanf("%llu", &a); }
inline void scan(char &a) { cin >> a; }
inline void scan(float &a) { scanf("%f", &a); }
inline void scan(double &a) { scanf("%lf", &a); }
inline void scan(long double &a) { scanf("%Lf", &a); }
inline void scan(vector<bool> &vec) {
for (unsigned i = 0; i < vec.size(); i++) {
int a;
scan(a);
vec[i] = a;
}
}
inline void scan(char a[]) { scanf("%s", a); }
inline void scan(string &a) { cin >> a; }
template <class T> inline void scan(vector<T> &vec);
template <class T, size_t size> inline void scan(array<T, size> &vec);
template <class T, class L> inline void scan(pair<T, L> &p);
template <class T, size_t size> inline void scan(T (&vec)[size]);
template <class T> inline void scan(vector<T> &vec) {
for (auto &i : vec)
scan(i);
}
template <class T, size_t size> inline void scan(array<T, size> &vec) {
for (auto &i : vec)
scan(i);
}
template <class T, class L> inline void scan(pair<T, L> &p) {
scan(p.first);
scan(p.second);
}
template <class T, size_t size> inline void scan(T (&vec)[size]) {
for (auto &i : vec)
scan(i);
}
template <class T> inline void scan(T &a) { cin >> a; }
inline void in() {}
template <class Head, class... Tail> inline void in(Head &head, Tail &...tail) {
scan(head);
in(tail...);
}
inline void print() { putchar(' '); }
inline void print(const bool &a) { printf("%d", a); }
inline void print(const int &a) { printf("%d", a); }
inline void print(const unsigned &a) { printf("%u", a); }
inline void print(const long &a) { printf("%ld", a); }
inline void print(const long long &a) { printf("%lld", a); }
inline void print(const unsigned long long &a) { printf("%llu", a); }
inline void print(const char &a) { printf("%c", a); }
inline void print(const char a[]) { printf("%s", a); }
inline void print(const float &a) { printf("%.15f", a); }
inline void print(const double &a) { printf("%.15f", a); }
inline void print(const long double &a) { printf("%.15Lf", a); }
template <class T> void print(const vector<T> &vec);
template <class T, size_t size> void print(const array<T, size> &vec);
template <class T, class L> void print(const pair<T, L> &p);
template <class T, size_t size> inline void print(const T (&vec)[size]);
template <class T> void print(const vector<T> &vec) {
if (vec.empty())
return;
print(vec[0]);
for (auto i = vec.begin(); ++i != vec.end();) {
putchar(' ');
print(*i);
}
}
template <class T, size_t size> void print(const array<T, size> &vec) {
print(vec[0]);
for (auto i = vec.begin(); ++i != vec.end();) {
putchar(' ');
print(*i);
}
}
template <class T, class L> void print(const pair<T, L> &p) {
print(p.first);
putchar(' ');
print(p.second);
}
template <class T, size_t size> inline void print(const T (&vec)[size]) {
print(vec[0]);
for (auto i = vec; ++i != end(vec);) {
putchar(' ');
print(*i);
}
}
template <class T> inline void print(const T &a) { cout << a; }
inline int out() {
putchar('\n');
return 0;
}
template <class T> inline int out(const T &t) {
print(t);
putchar('\n');
return 0;
}
template <class Head, class... Tail>
inline int out(const Head &head, const Tail &...tail) {
print(head);
putchar(' ');
out(tail...);
return 0;
}
template <class T> inline void err(T t) { cerr << t << '\n'; }
template <class Head, class... Tail>
inline void err(const Head &head, const Tail &...tail) {
cerr << head << ' ';
err(tail...);
}
inline void err() { cerr << '\n'; }
inline int first(const bool &i) { return out(i ? "first" : "second"); }
inline int yes(const bool &i) { return out(i ? "yes" : "no"); }
inline int Yes(const bool &i) { return out(i ? "Yes" : "No"); }
inline int YES(const bool &i) { return out(i ? "YES" : "NO"); }
inline int Yay(const bool &i) { return out(i ? "Yay!" : ":("); }
inline int Possible(const bool &i) {
return out(i ? "Possible" : "Impossible");
}
inline int POSSIBLE(const bool &i) {
return out(i ? "POSSIBLE" : "IMPOSSIBLE");
}
inline void Case(ll i) { printf("Case #%lld: ", i); }
signed main() {
LL(h, w, a, b);
vec(string, ans, h, string(w, '0'));
rep(b) rep(j, a) ans[i][j] = '1';
rep(i, b, h) rep(j, a, w) ans[i][j] = '1';
each(i, ans) out(i);
}
| replace | 277 | 311 | 277 | 282 | -11 | |
p02903 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T> inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> P;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define SORT(c) sort((c).begin(), (c).end())
#define REVERSE(c) reverse((c).begin(), (c).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for (int i = (a)-1; i >= (b); --i)
#define RREP(i, n) RFOR(i, n, 0)
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INT_INF = 2147483647;
const long long LL_INF = 1LL << 60;
const long long MOD = 1000000007;
#define CLR(a) memset((a), 0, sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x) \
cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \
<< " " << __FILE__ << endl;
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;
}
int main(void) {
int H, W, A, B;
cin >> H >> W >> A >> B;
int ans[H][W];
REP(i, H) REP(j, W) ans[i][j] = 0;
REP(i, A) REP(j, B) ans[i][j] = 1;
FOR(i, A, H) FOR(j, B, W) ans[i][j] = 1;
REP(i, H) {
REP(j, W) cout << ans[i][j];
cout << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T> inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> P;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define SORT(c) sort((c).begin(), (c).end())
#define REVERSE(c) reverse((c).begin(), (c).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for (int i = (a)-1; i >= (b); --i)
#define RREP(i, n) RFOR(i, n, 0)
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INT_INF = 2147483647;
const long long LL_INF = 1LL << 60;
const long long MOD = 1000000007;
#define CLR(a) memset((a), 0, sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x) \
cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \
<< " " << __FILE__ << endl;
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;
}
int main(void) {
int H, W, A, B;
cin >> H >> W >> A >> B;
int ans[H][W];
REP(i, H) REP(j, W) ans[i][j] = 0;
REP(i, B) REP(j, A) ans[i][j] = 1;
FOR(i, B, H) FOR(j, A, W) ans[i][j] = 1;
REP(i, H) {
REP(j, W) cout << ans[i][j];
cout << endl;
}
return 0;
} | replace | 64 | 66 | 64 | 67 | 0 | |
p02903 | C++ | Runtime Error | #define _CRT_SECURE_NO_WARNINGS
#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define rrep(i, m, n) for (int i = (m); i >= (n); --i)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define mp make_pair
#define pb push_back
#define eb emplace_back
using namespace std;
using LL = long long;
using VB = vector<bool>;
using VI = vector<int>;
using VL = vector<LL>;
using VS = vector<string>;
using VD = vector<double>;
using VVI = vector<VI>;
using VVL = vector<VL>;
using PII = pair<int, int>;
using PLL = pair<LL, LL>;
using VP = vector<PII>;
using VPL = vector<PLL>;
const int inf = (int)1e9;
const LL inf_ll = (LL)1e18, MOD = 1000000007;
const double PI = acos(-1.0), EPS = 1e-12;
template <class T> void Sort(T &a) { sort(all(a)); }
template <class T> void RSort(T &a) { sort(rall(a)); }
template <class T> void Reverse(T &a) { reverse(all(a)); }
template <class T> void Unique(T &a) { a.erase(unique(all(a)), a.end()); }
template <class T> T Sorted(T a) {
Sort(a);
return a;
}
template <class T> T RSorted(T a) {
RSort(a);
return a;
}
template <class T> T Reversed(T a) {
Reverse(a);
return a;
}
template <class T> T Uniqued(T a) {
Unique(a);
return a;
}
template <class T> auto Max(const T &a) { return *max_element(all(a)); }
template <class T> auto Min(const T &a) { return *min_element(all(a)); }
template <class T> int MaxPos(const T &a) {
return max_element(all(a)) - a.begin();
}
template <class T> int MinPos(const T &a) {
return min_element(all(a)) - a.begin();
}
template <class T, class U> int Count(const T &a, const U &v) {
return count(all(a), v);
}
template <class T, class U> int Find(const T &a, const U &v) {
auto pos = find(all(a), v);
return pos == a.end() ? -1 : pos - a.begin();
}
template <class T, class U> U Sum(const T &a, const U &v) {
return accumulate(all(a), v);
}
template <class T, class U> int Lower(const T &a, const U &v) {
return lower_bound(all(a), v) - a.begin();
}
template <class T, class U> int Upper(const T &a, const U &v) {
return upper_bound(all(a), v) - a.begin();
}
template <class T, class P> void RemoveIf(T &a, P f) {
a.erase(remove_if(all(a), f), a.end());
}
template <class T> T Age(T n, T m) { return (n + m - 1) / m; }
template <class T> T Age2(T n, T m) { return Age(n, m) * m; }
template <class T> T Gcd(T n, T m) { return m ? Gcd(m, n % m) : n; }
template <class T> T Lcm(T n, T m) { return n / Gcd(n, m) * m; }
template <class T> T Pow(T a, T n) {
T r = 1;
while (n > 0) {
if (n & 1)
r *= a;
a *= a;
n /= 2;
}
return r;
}
template <class T> T Powmod(T a, T n, T m = MOD) {
T r = 1;
while (n > 0) {
if (n & 1)
r = r * a % m, n--;
else
a = a * a % m, n /= 2;
}
return r;
}
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
string operator*(string s, int n) {
string ret;
rep(i, n) ret += s;
return ret;
}
// --- input --- //
#if defined(_MSC_VER) || defined(ONLINE_JUDGE)
#define getchar_unlocked _getchar_nolock
#define putchar_unlocked _putchar_nolock
#endif
inline int gc() { return getchar_unlocked(); }
template <class T> inline void InputF(T &v) { cin >> v; }
inline void InputF(char &v) {
while (isspace(v = gc()))
;
}
inline void InputF(string &v) {
char c;
for (InputF(c); !isspace(c); c = gc())
v += c;
}
inline void InputF(int &v) {
int k = 1;
v = 0;
char c;
InputF(c);
if (c == '-') {
k = -1;
c = gc();
}
for (; isdigit(c); c = gc())
v = v * 10 + (c - '0');
v *= k;
}
inline void InputF(long long &v) {
int k = 1;
v = 0;
char c;
InputF(c);
if (c == '-') {
k = -1;
c = gc();
}
for (; isdigit(c); c = gc())
v = v * 10 + (c - '0');
v *= k;
}
inline void InputF(double &v) {
double dp = 1;
int k = 1;
bool adp = false;
v = 0;
char c;
InputF(c);
if (c == '-') {
k = -1;
c = gc();
}
for (; isdigit(c) || c == '.'; c = gc()) {
if (c == '.')
adp = true;
else if (adp)
v += (c - '0') * (dp *= 0.1);
else
v = v * 10 + (c - '0');
}
v *= k;
}
template <class T, class U> inline void InputF(pair<T, U> &v) {
InputF(v.first);
InputF(v.second);
}
template <class T> inline void InputF(vector<T> &v) {
for (auto &e : v)
InputF(e);
}
struct InputV {
int n, m;
InputV(int N) : n(N), m(0) {}
InputV(pair<int, int> N) : n(N.first), m(N.second) {}
template <class T> operator vector<T>() {
vector<T> v(n);
InputF(v);
return v;
}
template <class T> operator vector<vector<T>>() {
vector<vector<T>> v(n, vector<T>(m));
InputF(v);
return v;
}
};
struct Input {
template <class T> operator T() {
T v;
InputF(v);
return v;
}
InputV operator[](int n) { return InputV(n); }
InputV operator[](pair<int, int> n) { return InputV(n); }
} in;
// --- output --- //
struct BoolStr {
const char *t, *f;
BoolStr(const char *_t, const char *_f) : t(_t), f(_f) {}
} Yes("Yes", "No"), yes("yes", "no"), YES("YES", "NO"), Int("1", "0");
struct DivStr {
const char *d, *l;
DivStr(const char *_d, const char *_l) : d(_d), l(_l) {}
} spc(" ", "\n"), no_spc("", "\n"), end_line("\n", "\n"), comma(",", "\n"),
no_endl(" ", "");
class Output {
BoolStr B{Yes};
DivStr D{spc};
void p(double v) { printf("%.20f", v); }
void p(int v) { printf("%d", v); }
void p(LL v) { printf("%lld", v); }
void p(char v) { putchar(v); }
void p(bool v) { printf(v ? B.t : B.f); }
template <class T> void p(const T &v) { cout << v; }
template <class T, class U> void p(const pair<T, U> &v) {
p(v.first);
printf(D.d);
p(v.second);
}
template <class T> void p(const vector<T> &v) {
rep(i, sz(v)) {
if (i)
printf(D.d);
p(v[i]);
}
}
template <class T> void p(const vector<vector<T>> &v) {
rep(i, sz(v)) {
if (i)
printf(D.l);
p(v[i]);
}
}
void p(const BoolStr &v) { B = v; }
void p(const DivStr &v) { D = v; }
template <class T> bool isPrint(const T &v) {
return !is_same<BoolStr, T>::value && !is_same<DivStr, T>::value;
}
public:
void operator()() { printf(D.l); }
template <class H> void operator()(H &&h) {
p(h);
if (isPrint(h))
printf(D.l);
B = Yes;
D = spc;
}
template <class H, class... T> void operator()(H &&h, T &&...t) {
p(h);
if (isPrint(h))
printf(D.d);
operator()(forward<T>(t)...);
}
} out;
#if __has_include("dump.hpp")
#include "dump.hpp"
#else
#define dump(...) (void(0))
#endif
int main() {
int h = in, w = in, a = in, b = in;
VVI ans(h, VI(w));
rep(i, a) rep(j, b) ans[i][j] = 1;
FOR(i, b, h) FOR(j, a, w) ans[i][j] = 1;
out(no_spc, ans);
} | #define _CRT_SECURE_NO_WARNINGS
#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define rrep(i, m, n) for (int i = (m); i >= (n); --i)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define mp make_pair
#define pb push_back
#define eb emplace_back
using namespace std;
using LL = long long;
using VB = vector<bool>;
using VI = vector<int>;
using VL = vector<LL>;
using VS = vector<string>;
using VD = vector<double>;
using VVI = vector<VI>;
using VVL = vector<VL>;
using PII = pair<int, int>;
using PLL = pair<LL, LL>;
using VP = vector<PII>;
using VPL = vector<PLL>;
const int inf = (int)1e9;
const LL inf_ll = (LL)1e18, MOD = 1000000007;
const double PI = acos(-1.0), EPS = 1e-12;
template <class T> void Sort(T &a) { sort(all(a)); }
template <class T> void RSort(T &a) { sort(rall(a)); }
template <class T> void Reverse(T &a) { reverse(all(a)); }
template <class T> void Unique(T &a) { a.erase(unique(all(a)), a.end()); }
template <class T> T Sorted(T a) {
Sort(a);
return a;
}
template <class T> T RSorted(T a) {
RSort(a);
return a;
}
template <class T> T Reversed(T a) {
Reverse(a);
return a;
}
template <class T> T Uniqued(T a) {
Unique(a);
return a;
}
template <class T> auto Max(const T &a) { return *max_element(all(a)); }
template <class T> auto Min(const T &a) { return *min_element(all(a)); }
template <class T> int MaxPos(const T &a) {
return max_element(all(a)) - a.begin();
}
template <class T> int MinPos(const T &a) {
return min_element(all(a)) - a.begin();
}
template <class T, class U> int Count(const T &a, const U &v) {
return count(all(a), v);
}
template <class T, class U> int Find(const T &a, const U &v) {
auto pos = find(all(a), v);
return pos == a.end() ? -1 : pos - a.begin();
}
template <class T, class U> U Sum(const T &a, const U &v) {
return accumulate(all(a), v);
}
template <class T, class U> int Lower(const T &a, const U &v) {
return lower_bound(all(a), v) - a.begin();
}
template <class T, class U> int Upper(const T &a, const U &v) {
return upper_bound(all(a), v) - a.begin();
}
template <class T, class P> void RemoveIf(T &a, P f) {
a.erase(remove_if(all(a), f), a.end());
}
template <class T> T Age(T n, T m) { return (n + m - 1) / m; }
template <class T> T Age2(T n, T m) { return Age(n, m) * m; }
template <class T> T Gcd(T n, T m) { return m ? Gcd(m, n % m) : n; }
template <class T> T Lcm(T n, T m) { return n / Gcd(n, m) * m; }
template <class T> T Pow(T a, T n) {
T r = 1;
while (n > 0) {
if (n & 1)
r *= a;
a *= a;
n /= 2;
}
return r;
}
template <class T> T Powmod(T a, T n, T m = MOD) {
T r = 1;
while (n > 0) {
if (n & 1)
r = r * a % m, n--;
else
a = a * a % m, n /= 2;
}
return r;
}
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
string operator*(string s, int n) {
string ret;
rep(i, n) ret += s;
return ret;
}
// --- input --- //
#if defined(_MSC_VER) || defined(ONLINE_JUDGE)
#define getchar_unlocked _getchar_nolock
#define putchar_unlocked _putchar_nolock
#endif
inline int gc() { return getchar_unlocked(); }
template <class T> inline void InputF(T &v) { cin >> v; }
inline void InputF(char &v) {
while (isspace(v = gc()))
;
}
inline void InputF(string &v) {
char c;
for (InputF(c); !isspace(c); c = gc())
v += c;
}
inline void InputF(int &v) {
int k = 1;
v = 0;
char c;
InputF(c);
if (c == '-') {
k = -1;
c = gc();
}
for (; isdigit(c); c = gc())
v = v * 10 + (c - '0');
v *= k;
}
inline void InputF(long long &v) {
int k = 1;
v = 0;
char c;
InputF(c);
if (c == '-') {
k = -1;
c = gc();
}
for (; isdigit(c); c = gc())
v = v * 10 + (c - '0');
v *= k;
}
inline void InputF(double &v) {
double dp = 1;
int k = 1;
bool adp = false;
v = 0;
char c;
InputF(c);
if (c == '-') {
k = -1;
c = gc();
}
for (; isdigit(c) || c == '.'; c = gc()) {
if (c == '.')
adp = true;
else if (adp)
v += (c - '0') * (dp *= 0.1);
else
v = v * 10 + (c - '0');
}
v *= k;
}
template <class T, class U> inline void InputF(pair<T, U> &v) {
InputF(v.first);
InputF(v.second);
}
template <class T> inline void InputF(vector<T> &v) {
for (auto &e : v)
InputF(e);
}
struct InputV {
int n, m;
InputV(int N) : n(N), m(0) {}
InputV(pair<int, int> N) : n(N.first), m(N.second) {}
template <class T> operator vector<T>() {
vector<T> v(n);
InputF(v);
return v;
}
template <class T> operator vector<vector<T>>() {
vector<vector<T>> v(n, vector<T>(m));
InputF(v);
return v;
}
};
struct Input {
template <class T> operator T() {
T v;
InputF(v);
return v;
}
InputV operator[](int n) { return InputV(n); }
InputV operator[](pair<int, int> n) { return InputV(n); }
} in;
// --- output --- //
struct BoolStr {
const char *t, *f;
BoolStr(const char *_t, const char *_f) : t(_t), f(_f) {}
} Yes("Yes", "No"), yes("yes", "no"), YES("YES", "NO"), Int("1", "0");
struct DivStr {
const char *d, *l;
DivStr(const char *_d, const char *_l) : d(_d), l(_l) {}
} spc(" ", "\n"), no_spc("", "\n"), end_line("\n", "\n"), comma(",", "\n"),
no_endl(" ", "");
class Output {
BoolStr B{Yes};
DivStr D{spc};
void p(double v) { printf("%.20f", v); }
void p(int v) { printf("%d", v); }
void p(LL v) { printf("%lld", v); }
void p(char v) { putchar(v); }
void p(bool v) { printf(v ? B.t : B.f); }
template <class T> void p(const T &v) { cout << v; }
template <class T, class U> void p(const pair<T, U> &v) {
p(v.first);
printf(D.d);
p(v.second);
}
template <class T> void p(const vector<T> &v) {
rep(i, sz(v)) {
if (i)
printf(D.d);
p(v[i]);
}
}
template <class T> void p(const vector<vector<T>> &v) {
rep(i, sz(v)) {
if (i)
printf(D.l);
p(v[i]);
}
}
void p(const BoolStr &v) { B = v; }
void p(const DivStr &v) { D = v; }
template <class T> bool isPrint(const T &v) {
return !is_same<BoolStr, T>::value && !is_same<DivStr, T>::value;
}
public:
void operator()() { printf(D.l); }
template <class H> void operator()(H &&h) {
p(h);
if (isPrint(h))
printf(D.l);
B = Yes;
D = spc;
}
template <class H, class... T> void operator()(H &&h, T &&...t) {
p(h);
if (isPrint(h))
printf(D.d);
operator()(forward<T>(t)...);
}
} out;
#if __has_include("dump.hpp")
#include "dump.hpp"
#else
#define dump(...) (void(0))
#endif
int main() {
int h = in, w = in, a = in, b = in;
VVI ans(h, VI(w));
rep(i, b) rep(j, a) ans[i][j] = 1;
FOR(i, b, h) FOR(j, a, w) ans[i][j] = 1;
out(no_spc, ans);
} | replace | 286 | 287 | 286 | 287 | 0 | |
p02903 | C++ | Runtime Error | #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
int main() {
int H, W;
cin >> H >> W;
int A, B;
cin >> A >> B;
vector<vector<int>> ans(H, vector<int>(W, 0));
for (int i = 0; i < H - B; i++) {
for (int j = 0; j < W - A; j++) {
ans[i][j] = 1;
}
}
for (int i = 0; i < A; i++) {
for (int j = 0; j < B; j++) {
ans[H - 1 - i][W - 1 - j] = 1;
}
}
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++)
cout << ans[i][j];
cout << endl;
}
return 0;
}
| #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
int main() {
int H, W;
cin >> H >> W;
int A, B;
cin >> A >> B;
vector<vector<int>> ans(H, vector<int>(W, 0));
for (int i = 0; i < H - B; i++) {
for (int j = 0; j < W - A; j++) {
ans[i][j] = 1;
}
}
for (int i = 0; i < B; i++) {
for (int j = 0; j < A; j++) {
ans[H - 1 - i][W - 1 - j] = 1;
}
}
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++)
cout << ans[i][j];
cout << endl;
}
return 0;
}
| replace | 37 | 39 | 37 | 39 | 0 | |
p02903 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cctype>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
using namespace std;
using ll = __int64_t;
using lll = __int128_t;
typedef pair<ll, ll> P;
#define REP(i, b, e) for (ll i = (ll)b; i < (ll)e; i++)
#define rep0(i, n) REP(i, 0ll, n)
#define rep1(i, n) REP(i, 1ll, n + 1)
const ll longinf = 1ll << 60;
const int inf = (1 << 30) - 1;
const ll mod = 1e9 + 7;
int main(void) {
int h, w, a, b;
cin >> h >> w >> a >> b;
int f[h][w];
rep0(i, h) rep0(j, w) f[i][j] = 0;
if (h == 1) {
rep0(i, h) rep0(j, w) {
if (j < a)
f[i][j] = 1;
}
} else if (w == 1) {
rep0(i, h) rep0(j, w) {
if (i < b)
f[i][j] = 1;
}
} else if (h % a == 0 && w % b == 0 && h / a == w / b) {
int d = h / a;
rep0(p, d) {
rep0(j, b) rep0(k, a) { f[p * b + j][p * a + k] = 1; }
}
} else {
cout << "No" << endl;
return 0;
}
rep0(i, h) {
rep0(j, w) { cout << f[i][j]; }
cout << endl;
}
return 0;
} | #include <algorithm>
#include <bitset>
#include <cctype>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
using namespace std;
using ll = __int64_t;
using lll = __int128_t;
typedef pair<ll, ll> P;
#define REP(i, b, e) for (ll i = (ll)b; i < (ll)e; i++)
#define rep0(i, n) REP(i, 0ll, n)
#define rep1(i, n) REP(i, 1ll, n + 1)
const ll longinf = 1ll << 60;
const int inf = (1 << 30) - 1;
const ll mod = 1e9 + 7;
int main(void) {
int h, w, a, b;
cin >> h >> w >> a >> b;
int f[h][w];
rep0(i, h) rep0(j, w) {
if (i < b && j < a)
f[i][j] = 0;
else if (i < b)
f[i][j] = 1;
else if (j < a)
f[i][j] = 1;
else
f[i][j] = 0;
}
rep0(i, h) {
rep0(j, w) { cout << f[i][j]; }
cout << endl;
}
return 0;
} | replace | 28 | 47 | 28 | 37 | 0 | |
p02903 | C++ | Runtime Error | #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
// template
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (a); i > (b); i--)
#define ALL(v) (v).begin(), (v).end()
typedef long long int ll;
typedef pair<ll, ll> P;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
fill((T *)array, (T *)(array + N), val);
}
const int inf = INT_MAX / 2;
const ll INF = LLONG_MAX / 2;
// template end
int main() {
int h, w, a, b;
scanf("%d%d%d%d", &h, &w, &a, &b);
int x[101][101] = {};
rep(i, 0, b) rep(j, 0, a) x[i][j] = 1;
rep(i, b, h) rep(j, a, w) x[i][j] = 1;
rep(i, 0, h) {
rep(j, 0, w) printf("%d", x[i][j]);
puts("");
}
return 0;
} | #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
// template
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (a); i > (b); i--)
#define ALL(v) (v).begin(), (v).end()
typedef long long int ll;
typedef pair<ll, ll> P;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
fill((T *)array, (T *)(array + N), val);
}
const int inf = INT_MAX / 2;
const ll INF = LLONG_MAX / 2;
// template end
int main() {
int h, w, a, b;
scanf("%d%d%d%d", &h, &w, &a, &b);
int x[1010][1010] = {};
rep(i, 0, b) rep(j, 0, a) x[i][j] = 1;
rep(i, b, h) rep(j, a, w) x[i][j] = 1;
rep(i, 0, h) {
rep(j, 0, w) printf("%d", x[i][j]);
puts("");
}
return 0;
} | replace | 35 | 36 | 35 | 36 | 0 | |
p02903 | C++ | Runtime Error | #include <bits/stdc++.h>
#define ll long long
#define double long double
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
#define mod (ll)(1e9 + 7)
#define inf (ll)(3e18 + 7)
#define P pair<int, int>
#define PiP pair<int, pair<int, int>>
#define all(x) x.begin(), x.end()
using namespace std;
int main() {
ll h, w, a, b;
cin >> h >> w >> a >> b;
ll ans[h][w];
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (i + 1 <= b)
ans[i][j] = 1;
else
ans[i][j] = 0;
}
}
for (int i = 0; i < a; i++) {
for (int j = 0; j < h; j++) {
if (ans[j][i] == 1)
ans[i][j] = 0;
else
ans[j][i] = 1;
}
}
rep(i, h) {
rep(j, w) cout << ans[i][j];
cout << endl;
}
} | #include <bits/stdc++.h>
#define ll long long
#define double long double
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
#define mod (ll)(1e9 + 7)
#define inf (ll)(3e18 + 7)
#define P pair<int, int>
#define PiP pair<int, pair<int, int>>
#define all(x) x.begin(), x.end()
using namespace std;
int main() {
ll h, w, a, b;
cin >> h >> w >> a >> b;
ll ans[h][w];
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (i + 1 <= b)
ans[i][j] = 1;
else
ans[i][j] = 0;
}
}
for (int i = 0; i < a; i++) {
for (int j = 0; j < h; j++) {
if (ans[j][i] == 1)
ans[j][i] = 0;
else
ans[j][i] = 1;
}
}
rep(i, h) {
rep(j, w) cout << ans[i][j];
cout << endl;
}
} | replace | 27 | 28 | 27 | 28 | 0 | |
p02903 | C++ | Time Limit Exceeded | // A.
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sstream>
using namespace std;
typedef long long LL;
int main(int argc, char *argv[]) {
while (true) {
LL h, w, a, b;
cin >> h >> w >> a >> b;
if (h <= 0 || w <= 0)
break;
for (int i = 0; i < h; ++i) {
string s;
if (i < b) {
s = string(a, '0') + string(w - a, '1');
} else {
s = string(a, '1') + string(w - a, '0');
}
cout << s << endl;
}
}
return 0;
}
| // A.
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sstream>
using namespace std;
typedef long long LL;
int main(int argc, char *argv[]) {
while (true) {
LL h = -1, w = -1, a, b;
cin >> h >> w >> a >> b;
if (h <= 0 || w <= 0)
break;
for (int i = 0; i < h; ++i) {
string s;
if (i < b) {
s = string(a, '0') + string(w - a, '1');
} else {
s = string(a, '1') + string(w - a, '0');
}
cout << s << endl;
}
}
return 0;
}
| replace | 15 | 16 | 15 | 16 | TLE | |
p02903 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
bool judge(vector<string> &ans, int A, int B) {
int H = ans.size();
int W = ans[0].size();
for (int i = 0; i < H; i++) {
int cnt = 0;
for (int j = 0; j < W; j++) {
cnt += ans[i][j] - '0';
}
cnt = min(cnt, W - cnt);
if (cnt != A)
return false;
}
for (int j = 0; j < W; j++) {
int cnt = 0;
for (int i = 0; i < H; i++) {
cnt += ans[i][j] - '0';
}
cnt = min(cnt, H - cnt);
if (cnt != B)
return false;
}
return true;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int H, W, A, B;
cin >> H >> W >> A >> B;
vector<string> ans(H, string(W, '0'));
if (H == 2 * B) {
if (B * W == A * H) {
string s1 = string(A, '1') + string(A, '0');
string s2 = string(A, '0') + string(A, '1');
for (int i = 0; i < B; i++) {
ans[i] = s1;
}
for (int i = B; i < H; i++) {
ans[i] = s2;
}
} else {
cout << "No\n";
return 0;
}
} else {
int hoge = H - 2 * B;
int bar = -1;
int ai = -1;
for (int i = 0; i <= H; i++) {
int anum = A * i + (W - A) * (H - i);
if (anum < B * W)
continue;
int foo = anum - B * W;
if (foo % hoge != 0)
continue;
int next_bar = W - foo / hoge;
if (next_bar <= 0 || next_bar > W)
continue;
bar = next_bar;
ai = i;
break;
}
if (bar == -1) {
cout << "No\n";
return 0;
}
// cerr << bar << " " << ai << endl;
vector<int> rest(H, W - A);
for (int i = 0; i < ai; i++) {
rest[i] = A;
}
for (int j = 0; j < bar; j++) {
int cnt = 0;
for (int i = 0; i < H; i++) {
if (cnt == B)
break;
if (rest[i] > 0) {
ans[i][j] = '1';
rest[i]--;
cnt++;
}
}
}
for (int j = bar; j < W; j++) {
int cnt = 0;
for (int i = 0; i < H; i++) {
if (cnt == H - B)
break;
if (rest[i] > 0) {
ans[i][j] = '1';
rest[i]--;
cnt++;
}
}
}
}
assert(judge(ans, A, B));
for (int i = 0; i < H; i++) {
cout << ans[i] << "\n";
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
bool judge(vector<string> &ans, int A, int B) {
int H = ans.size();
int W = ans[0].size();
for (int i = 0; i < H; i++) {
int cnt = 0;
for (int j = 0; j < W; j++) {
cnt += ans[i][j] - '0';
}
cnt = min(cnt, W - cnt);
if (cnt != A)
return false;
}
for (int j = 0; j < W; j++) {
int cnt = 0;
for (int i = 0; i < H; i++) {
cnt += ans[i][j] - '0';
}
cnt = min(cnt, H - cnt);
if (cnt != B)
return false;
}
return true;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int H, W, A, B;
cin >> H >> W >> A >> B;
vector<string> ans(H, string(W, '0'));
if (H == 2 * B) {
if (B * W == A * H) {
string s1 = string(A, '1') + string(A, '0');
string s2 = string(A, '0') + string(A, '1');
for (int i = 0; i < B; i++) {
ans[i] = s1;
}
for (int i = B; i < H; i++) {
ans[i] = s2;
}
} else {
cout << "No\n";
return 0;
}
} else {
int hoge = H - 2 * B;
int bar = -1;
int ai = -1;
for (int i = 0; i <= H; i++) {
int anum = A * i + (W - A) * (H - i);
if (anum < B * W)
continue;
int foo = anum - B * W;
if (foo % hoge != 0)
continue;
int next_bar = W - foo / hoge;
if (next_bar < 0 || next_bar > W)
continue;
bar = next_bar;
ai = i;
break;
}
if (bar == -1) {
cout << "No\n";
return 0;
}
// cerr << bar << " " << ai << endl;
vector<int> rest(H, W - A);
for (int i = 0; i < ai; i++) {
rest[i] = A;
}
for (int j = 0; j < bar; j++) {
int cnt = 0;
for (int i = 0; i < H; i++) {
if (cnt == B)
break;
if (rest[i] > 0) {
ans[i][j] = '1';
rest[i]--;
cnt++;
}
}
}
for (int j = bar; j < W; j++) {
int cnt = 0;
for (int i = 0; i < H; i++) {
if (cnt == H - B)
break;
if (rest[i] > 0) {
ans[i][j] = '1';
rest[i]--;
cnt++;
}
}
}
}
assert(judge(ans, A, B));
for (int i = 0; i < H; i++) {
cout << ans[i] << "\n";
}
return 0;
} | replace | 64 | 65 | 64 | 65 | 0 | |
p02904 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdlib>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
#define pie 3.141592653589793238462643383279
#define mod 1000000007
#define INF 1145141919810364364
#define int long long
#define all(vec) vec.begin(), vec.end()
#define P pair<int, int>
#define S second
#define F first
int gcd(int x, int y) {
if (y == 0)
return x;
return gcd(y, x % y);
}
int lcm(int x, int y) { return x / gcd(x, y) * y; }
bool prime(int x) {
for (int i = 2; i <= sqrt(x); i++) {
if (x % i == 0)
return false;
}
return true;
}
int kai(int x, int y) {
int res = 1;
for (int i = x - y + 1; i <= x; i++) {
res *= i;
res %= mod;
}
return res;
}
int mod_pow(int x, int y, int m) {
int res = 1;
while (y > 0) {
if (y & 1) {
res = res * x % m;
}
x = x * x % m;
y >>= 1;
}
return res;
}
int comb(int x, int y) {
if (y > x)
return 0;
return kai(x, y) * mod_pow(kai(y, y), mod - 2, mod) % mod;
}
int n, k, p[100010];
int ans = 1;
set<int> st;
int cnt = 1;
bool bl = false;
signed main() {
cin >> n >> k;
for (int i = 0; i < n; i++)
cin >> p[i];
for (int i = 0; i < k; i++) {
st.insert(p[i]);
if (i) {
if (p[i - 1] < p[i])
cnt++;
else
cnt = 1;
}
}
if (cnt == k)
bl = true;
for (int i = k; i < n; i++) {
if (p[i - 1] < p[i])
cnt++;
else
cnt = 1;
if (*st.begin() != p[i - k] || *st.rbegin() > p[i]) {
if (!bl || cnt < k)
ans++;
}
st.erase(p[i - k]);
st.insert(p[i]);
if (cnt >= k)
bl = true;
}
cout << ans << endl;
} | #include <algorithm>
#include <cmath>
#include <cstdlib>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
#define pie 3.141592653589793238462643383279
#define mod 1000000007
#define INF 1145141919810364364
#define int long long
#define all(vec) vec.begin(), vec.end()
#define P pair<int, int>
#define S second
#define F first
int gcd(int x, int y) {
if (y == 0)
return x;
return gcd(y, x % y);
}
int lcm(int x, int y) { return x / gcd(x, y) * y; }
bool prime(int x) {
for (int i = 2; i <= sqrt(x); i++) {
if (x % i == 0)
return false;
}
return true;
}
int kai(int x, int y) {
int res = 1;
for (int i = x - y + 1; i <= x; i++) {
res *= i;
res %= mod;
}
return res;
}
int mod_pow(int x, int y, int m) {
int res = 1;
while (y > 0) {
if (y & 1) {
res = res * x % m;
}
x = x * x % m;
y >>= 1;
}
return res;
}
int comb(int x, int y) {
if (y > x)
return 0;
return kai(x, y) * mod_pow(kai(y, y), mod - 2, mod) % mod;
}
int n, k, p[200010];
int ans = 1;
set<int> st;
int cnt = 1;
bool bl = false;
signed main() {
cin >> n >> k;
for (int i = 0; i < n; i++)
cin >> p[i];
for (int i = 0; i < k; i++) {
st.insert(p[i]);
if (i) {
if (p[i - 1] < p[i])
cnt++;
else
cnt = 1;
}
}
if (cnt == k)
bl = true;
for (int i = k; i < n; i++) {
if (p[i - 1] < p[i])
cnt++;
else
cnt = 1;
if (*st.begin() != p[i - k] || *st.rbegin() > p[i]) {
if (!bl || cnt < k)
ans++;
}
st.erase(p[i - k]);
st.insert(p[i]);
if (cnt >= k)
bl = true;
}
cout << ans << endl;
} | replace | 63 | 64 | 63 | 64 | 0 | |
p02904 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef bool boolean;
const int N = 2e5 + 5;
int n, K;
int a[N], Q[N];
boolean ismx[N], ismi[N];
int main() {
scanf("%d%d", &n, &K);
for (int i = 1; i <= n; i++) {
scanf("%d", a + i);
}
int st = 1, ed = 0;
for (int i = n; i; i--) {
while (st <= ed && Q[st] >= i + K)
st++;
while (st <= ed && a[Q[ed]] > a[i])
ed--;
Q[++ed] = i;
ismi[i] = (st == ed);
}
st = 1, ed = 0;
for (int i = 1; i <= n; i++) {
while (st <= ed && Q[st] <= i - K)
st++;
while (st <= ed && a[Q[ed]] < a[i])
ed--;
Q[++ed] = i;
ismx[i] = (st == ed);
}
int qaq = 0;
for (int i = 1, j; i <= n; i = j) {
++j;
while (a[j] > a[j - 1])
j++;
qaq += (j - i >= K);
}
int ans = n - K + 1 - max(qaq - 1, 0);
for (int i = K + 1; i <= n; i++) {
ans -= ismi[i - K] && ismx[i];
}
printf("%d\n", ans);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef bool boolean;
const int N = 2e5 + 5;
int n, K;
int a[N], Q[N];
boolean ismx[N], ismi[N];
int main() {
scanf("%d%d", &n, &K);
for (int i = 1; i <= n; i++) {
scanf("%d", a + i);
}
int st = 1, ed = 0;
for (int i = n; i; i--) {
while (st <= ed && Q[st] >= i + K)
st++;
while (st <= ed && a[Q[ed]] > a[i])
ed--;
Q[++ed] = i;
ismi[i] = (st == ed);
}
st = 1, ed = 0;
for (int i = 1; i <= n; i++) {
while (st <= ed && Q[st] <= i - K)
st++;
while (st <= ed && a[Q[ed]] < a[i])
ed--;
Q[++ed] = i;
ismx[i] = (st == ed);
}
int qaq = 0;
for (int i = 1, j = 1; i <= n; i = j) {
++j;
while (a[j] > a[j - 1])
j++;
qaq += (j - i >= K);
}
int ans = n - K + 1 - max(qaq - 1, 0);
for (int i = K + 1; i <= n; i++) {
ans -= ismi[i - K] && ismx[i];
}
printf("%d\n", ans);
return 0;
}
| replace | 34 | 35 | 34 | 35 | 0 | |
p02904 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using lint = long long int;
struct UnionFind {
vector<int> data;
UnionFind(int n) { data.assign(n, -1); }
bool unite(int x, int y) {
x = root(x), y = root(y);
if (x == y)
return true;
if (data[x] > data[y])
swap(x, y);
data[x] += data[y];
data[y] = x;
return true;
}
int root(int x) { return (data[x] < 0 ? x : data[x] = root(data[x])); }
int size(int x) { return -data[root(x)]; }
};
struct SegmentTree {
using D = long long int;
using DD = function<D(D, D)>;
private:
int n;
vector<D> data;
D ie;
DD f;
public:
SegmentTree(int len, D ie, DD f) : ie(ie), f(f) {
n = pow(2, ceil(log2(len)));
data.assign(2 * n, ie);
}
void update(int idx, D val) {
idx += n;
data[idx] = val;
while (idx) {
idx >>= 1;
data[idx] = f(data[idx * 2], data[idx * 2 + 1]);
}
}
// [l, r)
D query(int l, int r) {
l = min(max(l, 0), n);
r = min(max(r, 0), n);
D res = ie;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l & 1)
res = f(res, data[l++]);
if (r & 1)
res = f(res, data[--r]);
}
return res;
}
D operator[](int i) { return data[i + n]; }
};
int main() {
lint n, k;
cin >> n >> k;
vector<lint> p(n);
for (auto &elem : p)
cin >> elem;
UnionFind uf(n - k + 1);
// i+k <= j
static vector<lint> inc(n, 1);
for (lint i = 0; i < n; i++) {
if (p[i] < p[i + 1])
inc[i + 1] = inc[i] + 1;
}
static vector<lint> po;
for (lint i = 0; i + k - 1 < n; i++) {
if (inc[i + k - 1] - inc[i] == k - 1)
po.push_back(i);
}
for (lint i = 0; i + 1 < po.size(); i++) {
uf.unite(po[i], po[i + 1]);
}
// i < j < i+k
// 隣接間だけチェックすればよい
SegmentTree mn(n, 1e18, [](lint a, lint b) { return min(a, b); });
SegmentTree mx(n, -1e18, [](lint a, lint b) { return max(a, b); });
for (lint i = 0; i < n; i++) {
mn.update(i, p[i]);
mx.update(i, p[i]);
}
for (lint i = 0; i + 1 + k <= n; i++) {
if (p[i] < mn.query(i + 1, i + k) && mx.query(i + 1, i + k) < p[i + k]) {
uf.unite(i, i + 1);
}
}
map<lint, lint> mp;
for (lint i = 0; i < n - k + 1; i++)
mp[uf.root(i)]++;
cout << mp.size() << endl;
} | #include <bits/stdc++.h>
using namespace std;
using lint = long long int;
struct UnionFind {
vector<int> data;
UnionFind(int n) { data.assign(n, -1); }
bool unite(int x, int y) {
x = root(x), y = root(y);
if (x == y)
return true;
if (data[x] > data[y])
swap(x, y);
data[x] += data[y];
data[y] = x;
return true;
}
int root(int x) { return (data[x] < 0 ? x : data[x] = root(data[x])); }
int size(int x) { return -data[root(x)]; }
};
struct SegmentTree {
using D = long long int;
using DD = function<D(D, D)>;
private:
int n;
vector<D> data;
D ie;
DD f;
public:
SegmentTree(int len, D ie, DD f) : ie(ie), f(f) {
n = pow(2, ceil(log2(len)));
data.assign(2 * n, ie);
}
void update(int idx, D val) {
idx += n;
data[idx] = val;
while (idx) {
idx >>= 1;
data[idx] = f(data[idx * 2], data[idx * 2 + 1]);
}
}
// [l, r)
D query(int l, int r) {
l = min(max(l, 0), n);
r = min(max(r, 0), n);
D res = ie;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l & 1)
res = f(res, data[l++]);
if (r & 1)
res = f(res, data[--r]);
}
return res;
}
D operator[](int i) { return data[i + n]; }
};
int main() {
lint n, k;
cin >> n >> k;
vector<lint> p(n);
for (auto &elem : p)
cin >> elem;
UnionFind uf(n - k + 1);
// i+k <= j
static vector<lint> inc(n, 1);
for (lint i = 0; i + 1 < n; i++) {
if (p[i] < p[i + 1])
inc[i + 1] = inc[i] + 1;
}
static vector<lint> po;
for (lint i = 0; i + k - 1 < n; i++) {
if (inc[i + k - 1] - inc[i] == k - 1)
po.push_back(i);
}
for (lint i = 0; i + 1 < po.size(); i++) {
uf.unite(po[i], po[i + 1]);
}
// i < j < i+k
// 隣接間だけチェックすればよい
SegmentTree mn(n, 1e18, [](lint a, lint b) { return min(a, b); });
SegmentTree mx(n, -1e18, [](lint a, lint b) { return max(a, b); });
for (lint i = 0; i < n; i++) {
mn.update(i, p[i]);
mx.update(i, p[i]);
}
for (lint i = 0; i + 1 + k <= n; i++) {
if (p[i] < mn.query(i + 1, i + k) && mx.query(i + 1, i + k) < p[i + k]) {
uf.unite(i, i + 1);
}
}
map<lint, lint> mp;
for (lint i = 0; i < n - k + 1; i++)
mp[uf.root(i)]++;
cout << mp.size() << endl;
} | replace | 78 | 79 | 78 | 79 | -6 | Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)
|
p02904 | C++ | Runtime Error | #include <bits/stdc++.h>
#define fi first
#define se second
const int N = 200200;
const long long m1 = 999998727899999ll;
const long long m2 = 777737777777777ll;
const long long p1 = 1e9 + 7;
const long long p2 = 1e9 + 9;
using namespace std;
struct fenwick {
long long mod;
long long t[N];
void upd(int x, long long y) {
y %= mod;
while (x < N) {
t[x] += y;
if (t[x] < 0) {
t[x] += mod;
} else if (t[x] >= mod) {
t[x] -= mod;
}
x += x & -x;
}
}
long long get(int x) {
long long res = 0;
while (x > 0) {
res += t[x];
if (res >= mod) {
res -= mod;
}
x -= x & -x;
}
return res;
}
};
fenwick t1, t2;
fenwick C;
int n;
int k;
int a[N];
long long f1[N];
long long f2[N];
long long safe_mul(long long x, long long y, long long mod) {
x %= mod;
long long res = 0;
while (y) {
if (y & 1) {
res += x;
if (res >= mod) {
res -= mod;
}
}
y /= 2;
x += x;
if (x >= mod) {
x -= mod;
}
}
return res;
}
void add(long long &x, long long y, const long long &mod) {
x += y;
if (x < 0) {
x += mod;
} else if (x >= mod) {
x -= mod;
}
}
int main() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(0);
t1.mod = m1;
t2.mod = m2;
C.mod = N;
cin >> n >> k;
f1[0] = f2[0] = 1;
for (int i = 1; i <= n; i++) {
cin >> a[i];
a[i] += 1;
f1[i] = safe_mul(f1[i - 1], p1, m1);
f2[i] = safe_mul(f2[i - 1], p2, m2);
}
set<pair<long long, long long>> res;
long long A = 0, B = 0;
for (int i = 1; i < k; i++) {
t1.upd(a[i], f1[i]);
t2.upd(a[i], f2[i]);
C.upd(a[i], 1);
add(A, t1.get(n), m1);
add(A, -t1.get(a[i]), m1);
add(B, t2.get(n), m2);
add(B, -t2.get(a[i]), m2);
}
for (int i = k; i <= n; i++) {
t1.upd(a[i], f1[i]);
t2.upd(a[i], f2[i]);
C.upd(a[i], 1);
add(A, t1.get(n), m1);
add(A, -t1.get(a[i]), m1);
add(B, t2.get(n), m2);
add(B, -t2.get(a[i]), m2);
// cout << A << " " << B << endl;
res.insert({A, B});
long long col = C.get(a[i - k + 1] - 1);
t1.upd(a[i - k + 1], -f1[i - k + 1]);
t2.upd(a[i - k + 1], -f2[i - k + 1]);
C.upd(a[i - k + 1], -1);
add(A, -safe_mul(col, f1[i - k + 1], m1), m1);
add(B, -safe_mul(col, f2[i - k + 1], m2), m2);
}
cout << res.size() << "\n";
}
| #include <bits/stdc++.h>
#define fi first
#define se second
const int N = 200200;
const long long m1 = 999998727899999ll;
const long long m2 = 777737777777777ll;
const long long p1 = 1e9 + 7;
const long long p2 = 1e9 + 9;
using namespace std;
struct fenwick {
long long mod;
long long t[N];
void upd(int x, long long y) {
y %= mod;
while (x < N) {
t[x] += y;
if (t[x] < 0) {
t[x] += mod;
} else if (t[x] >= mod) {
t[x] -= mod;
}
x += x & -x;
}
}
long long get(int x) {
long long res = 0;
while (x > 0) {
res += t[x];
if (res >= mod) {
res -= mod;
}
x -= x & -x;
}
return res;
}
};
fenwick t1, t2;
fenwick C;
int n;
int k;
int a[N];
long long f1[N];
long long f2[N];
long long safe_mul(long long x, long long y, long long mod) {
x %= mod;
long long res = 0;
while (y) {
if (y & 1) {
res += x;
if (res >= mod) {
res -= mod;
}
}
y /= 2;
x += x;
if (x >= mod) {
x -= mod;
}
}
return res;
}
void add(long long &x, long long y, const long long &mod) {
x += y;
if (x < 0) {
x += mod;
} else if (x >= mod) {
x -= mod;
}
}
int main() {
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(0);
t1.mod = m1;
t2.mod = m2;
C.mod = N;
cin >> n >> k;
f1[0] = f2[0] = 1;
for (int i = 1; i <= n; i++) {
cin >> a[i];
a[i] += 1;
f1[i] = safe_mul(f1[i - 1], p1, m1);
f2[i] = safe_mul(f2[i - 1], p2, m2);
}
set<pair<long long, long long>> res;
long long A = 0, B = 0;
for (int i = 1; i < k; i++) {
t1.upd(a[i], f1[i]);
t2.upd(a[i], f2[i]);
C.upd(a[i], 1);
add(A, t1.get(n), m1);
add(A, -t1.get(a[i]), m1);
add(B, t2.get(n), m2);
add(B, -t2.get(a[i]), m2);
}
for (int i = k; i <= n; i++) {
t1.upd(a[i], f1[i]);
t2.upd(a[i], f2[i]);
C.upd(a[i], 1);
add(A, t1.get(n), m1);
add(A, -t1.get(a[i]), m1);
add(B, t2.get(n), m2);
add(B, -t2.get(a[i]), m2);
// cout << A << " " << B << endl;
res.insert({A, B});
long long col = C.get(a[i - k + 1] - 1);
t1.upd(a[i - k + 1], -f1[i - k + 1]);
t2.upd(a[i - k + 1], -f2[i - k + 1]);
C.upd(a[i - k + 1], -1);
add(A, -safe_mul(col, f1[i - k + 1], m1), m1);
add(B, -safe_mul(col, f2[i - k + 1], m2), m2);
}
cout << res.size() << "\n";
}
| replace | 81 | 83 | 81 | 83 | TLE | |
p02904 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const int MOD2 = 1000000009;
const int MAXN = 100000;
int n, k;
int p[MAXN];
int pw[MAXN];
int pw2[MAXN];
int p1[MAXN];
int p2[MAXN];
pair<int, int> tree[4 * MAXN];
int len[4 * MAXN];
void upd(int i, int l, int r, int x, bool add) {
if (l > x || r < x)
return;
if (l == r) {
if (add) {
tree[i] = make_pair(x, x);
len[i] = 1;
} else {
tree[i] = {0, 0};
len[i] = 0;
}
return;
}
upd(i * 2 + 1, l, (l + r) / 2, x, add);
upd(i * 2 + 2, (l + r) / 2 + 1, r, x, add);
len[i] = len[i * 2 + 1] + len[i * 2 + 2];
tree[i].first = (tree[i * 2 + 1].first +
(pw[len[i * 2 + 1]] * 1ll * tree[i * 2 + 2].first)) %
MOD;
tree[i].second = (tree[i * 2 + 1].second +
(pw2[len[i * 2 + 1]] * 1ll * tree[i * 2 + 2].second)) %
MOD2;
}
set<pair<int, int>> perms;
void solve(int i) {
if (i + k > n)
return;
long long ans2 = p2[n - 1];
long long ans1 = p1[n - 1];
ans1 -= p1[i + k - 1];
ans2 -= p2[i + k - 1];
if (i) {
ans1 += p1[i - 1];
ans2 += p2[i - 1];
upd(0, 1, n, p[i + k - 1], true);
upd(0, 1, n, p[i - 1], false);
} else {
for (int j = 0; j < k; j++) {
upd(0, 1, n, p[j], true);
}
}
while (ans1 < 0)
ans1 += MOD;
while (ans2 < 0)
ans2 += MOD2;
pair<int, int> res;
res.first = (ans1 + tree[0].first * 1ll * pw[i]) % MOD;
res.second = (ans2 + tree[0].second * 1ll * pw2[i]) % MOD2;
perms.insert(res);
}
int main() {
pw[0] = 1;
pw2[0] = 1;
for (int i = 1; i < MAXN; i++) {
pw[i] = (MAXN * 1ll * pw[i - 1]) % MOD;
pw2[i] = (MAXN * 1ll * pw2[i - 1]) % MOD2;
}
scanf("%d %d", &n, &k);
for (int i = 0; i < n; i++) {
scanf("%d", p + i);
p[i]++;
p1[i] = (p[i] * 1ll * pw[i] + p1[i - 1]) % MOD;
p2[i] = (p[i] * 1ll * pw2[i] + p2[i - 1]) % MOD2;
}
for (int i = 0; i < n; i++)
solve(i);
printf("%d\n", perms.size());
}
| #include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const int MOD2 = 1000000009;
const int MAXN = 200005;
int n, k;
int p[MAXN];
int pw[MAXN];
int pw2[MAXN];
int p1[MAXN];
int p2[MAXN];
pair<int, int> tree[4 * MAXN];
int len[4 * MAXN];
void upd(int i, int l, int r, int x, bool add) {
if (l > x || r < x)
return;
if (l == r) {
if (add) {
tree[i] = make_pair(x, x);
len[i] = 1;
} else {
tree[i] = {0, 0};
len[i] = 0;
}
return;
}
upd(i * 2 + 1, l, (l + r) / 2, x, add);
upd(i * 2 + 2, (l + r) / 2 + 1, r, x, add);
len[i] = len[i * 2 + 1] + len[i * 2 + 2];
tree[i].first = (tree[i * 2 + 1].first +
(pw[len[i * 2 + 1]] * 1ll * tree[i * 2 + 2].first)) %
MOD;
tree[i].second = (tree[i * 2 + 1].second +
(pw2[len[i * 2 + 1]] * 1ll * tree[i * 2 + 2].second)) %
MOD2;
}
set<pair<int, int>> perms;
void solve(int i) {
if (i + k > n)
return;
long long ans2 = p2[n - 1];
long long ans1 = p1[n - 1];
ans1 -= p1[i + k - 1];
ans2 -= p2[i + k - 1];
if (i) {
ans1 += p1[i - 1];
ans2 += p2[i - 1];
upd(0, 1, n, p[i + k - 1], true);
upd(0, 1, n, p[i - 1], false);
} else {
for (int j = 0; j < k; j++) {
upd(0, 1, n, p[j], true);
}
}
while (ans1 < 0)
ans1 += MOD;
while (ans2 < 0)
ans2 += MOD2;
pair<int, int> res;
res.first = (ans1 + tree[0].first * 1ll * pw[i]) % MOD;
res.second = (ans2 + tree[0].second * 1ll * pw2[i]) % MOD2;
perms.insert(res);
}
int main() {
pw[0] = 1;
pw2[0] = 1;
for (int i = 1; i < MAXN; i++) {
pw[i] = (MAXN * 1ll * pw[i - 1]) % MOD;
pw2[i] = (MAXN * 1ll * pw2[i - 1]) % MOD2;
}
scanf("%d %d", &n, &k);
for (int i = 0; i < n; i++) {
scanf("%d", p + i);
p[i]++;
p1[i] = (p[i] * 1ll * pw[i] + p1[i - 1]) % MOD;
p2[i] = (p[i] * 1ll * pw2[i] + p2[i - 1]) % MOD2;
}
for (int i = 0; i < n; i++)
solve(i);
printf("%d\n", perms.size());
}
| replace | 5 | 6 | 5 | 6 | 0 | |
p02904 | C++ | Runtime Error | /* Aa^~ kokoro ga pyonpyon suru n jaa^~
// ZZZXXkXkkkZ!``` ``` ``` ``` ``` ``` ``` ``` ``` ``` ```
```?Wfpppbpbbpbbpbbbkbkk
// ppbbbpbbpVr`` `` ` ` ` ` ```` `` ` ` `` ` ` ` ` ` ` ` ` `
dppbbkkkkkkkkkkqkqkk
// HkqqqqqkkWr`` ` ` ``` ``` `?G, ` ` ``.JC!```` ` ` `` ``
````(Wpbkkkkkkkkqkkkkkqk
// mmmmmqqqqpr` `` `` ```````.+zT=`` `` 7TO-.```````` `` ``
```(yppbkkkkkkkkkkkkkkk
// ggmgmmqqqH$ ``````````....````` ` ````````.`````` ``
``````.yfpppbbbbkkkkqqqqqH
// gmmmmmqqqkW<```` `````...````` .,.` ````....````` ```````
(Wbqqmgmmgmggggggggg
// qmmmqqqqkkWk.``````````````````` ;:<``
`````.`````````````-_<-?WHHqmmmmmmgmmgggg
// @@@@@@@gggHH6- ``````````````` `` _ `` ````````````````
._~~_.`-?Wkqmmmmmmmggg@g
// @@@@g@gggHY~.-<_- ``````````````````````````````````
._~~(<-``.`.(WHqqqmmggggmmm
// @@g@gggHH=.`..._<-___..```````````````````````. .-_~~~_(!``-.``.``
OHHWUWHmqHWXW
// gggggmqK1.``..~.. _<<+-(____.. ```````` ..__~~_((<<!.`.``` .``.``
j0C1XUHmHIdW
// ggmmqH0!,_``.>`````` _<<;<v<<<++((((((((((<<<<<<~_. (-.``~``.>..```
jOuWHHqHIdH
// gmmqkW!`(_ J>` `` ` _~<`_~~~~<<<<<<<~~__````````` ?1. ._`(__``` zXWHg@HkXH
// gHHWS{``(lJ<!``.``.```(:+>`._`````.` <..`` - ``. ` _ ?&._.I`_``````
.XyVfppppW
// HHHSv``.(X:_..... _..(;+<!.(<..-.....-.-_..+_`..<.`.`..`_IJd}
.`..````jqg@@@@@@
// XHWZ{..<Jk~!.`..
(<.-(+>(_.(1.(_..`.`.`.<_.+<_..<<-..._..-zy>.`_`...```.WH@HHHHH
// bkWt~.-jCz(_..`.(+<.(;<
._-<=_(<_..-....(_.<1<..(<<.`._..-JUS-._.`...```dHmH9VUH
// WUUO..(f.(c...__+z<-(+~`
_-+<_(><..__.`.(<._.z_.(1;_..__.(C(zT-(..`...``(WHR<+Xk
//
kkkk._(_.->..._(z;:_><.._>_+_<(1>_._<...(v<<.(<.(+z<..-_(Z~_<_j+_..`...`(WHKz1ZW
//
@@gR._+_..~..-<+z<<?<>```_.<_.(+1><_;_..(1_:`.<<??1z--(+Z!..<_.j<....`..(bgHAAQX
// @@mR.(j:..~.._<z!`.(>~```
~(_.(+<1><><_.(((_`.<__`.<_.(X>...<_.(<.....`.JUWWWyWW
//
@gmH_(zl..(.._+>```<+_````.~>``(+.<?>>_._(<```(<<``(__<>....<.._<.......dXkkkHHH
//
mmqHl(dk_.(_.-=~`.`.1-..._~-1.``_:`(??<_~(`.--.&_.`.<(;<...._.._<..`..._Xg@@@@@@
//
qHkpk(dX<.(;..j_```.(((JJ&a&-~``````.1<_```-(((e+.-(/`(>...._..(<......(Wmggg@@g
// HVHbWcz><__+_.(_.(dWWHHH@HHc~````````.+~``
(jHMMMHHHm&.?..._<..(<_..._.(WqqHHmHg
// 0>vWWkzZwl~<o.__`__~X@@HM@Hb ```.`.``. ````
d@@HHH@@K?76...(<..(<_...(_(ppWWWWHq
// X0XWHKXXw$<(z<.( ``
WHHMHHHH_``````````````.WHHMNMHHH_`(...(<_.(z_..._<(fWVC174W
//
XuXWHHWWz>__+z+.!`..??CZYCOX_`````````````.`~.OvTUZUS_`~.._+?_.(_~_.._zjO=1+~+jy
// kkkkkkkkX:._<z=1(_`` << ``->``.``.``.``.```` ?<``
(v!`._..(??_.(1._.._=dUOOzzzwX
// @@@@@@@@H<...1O=v<_...__ -_````````````````.`` `` ~.``
:.~+=?~.(;_(...jdQQQQQkkk
//
H@@@@@@@H~...(==>.~~~~~....`.`````````.`````.`........->.(===~~<<.(...(dg@@@@@@@
//
@@@H@@HHH_.__(=l>~.~~~~~....``.``.``.```..`......~~~~~(<_+=l=~_<.->..~_dqggggg@g
//
@H@@@@MHH_._<(=l>...........```````````````.`...~~~~~~+<(=lz=~((j=z_..~jWqmmgggm
//
@@H@@HHWH_._<(lll-.......```.````.``.`..`````........_z<+llZz~(lOO=<...(VYUUUW9Y
//
@@HMMHWZf>~_=:=llw+.`````````.`.```__~~_``.`````.....(z+llOOz_zllOlz~..~<<1+dW>_
//
MMM#MHHWXl~_=>1ltwOl&.`.``.`````.``````````````.````.(llttwtz(OltwOz<..__zwOwwOz
//
HM#HMHUUI<._1z+ttOZttlt&....``.``.`.````.``...``...(zZtttOktzjttttwlz_._<(Xkkkkk
//
HHHmHSZu:(_~+OztttXtttOZZttO+-..............-(+ztOttwttttd0tOZttttwOl<~.(_dMMHHH
//
rvuuXuuI~~<~(uttttwvOwwwkQQHMMHHHHHHHHHMMMNmgey?OwwwrtttwXOtwttttttXtO-~.((wZyyy
// HHHHHHK>(~(-(dOrtrrl(QgMHMMMHHHHHHHHHHHHHHHH##HMNkX0rrrrXXrd%``
(Ctwwtz_~.<(Wg@H
// NNNNNHD(~(zo~zXrrrQdHHMMNMHHHHHHHHHHHHHHHHHHHHHH##HNmyrdKkwZ `
_``-zwrt1~~_<(MNM
// MMMMM#<<_jwr:(Z4QHHMMHMHHHHHHHHHHHHHHHHHHHHHHHHHHHH###NHSXZ>`
~````.OXtt>~._<?MM
*/
#include <bits/stdc++.h>
using namespace std;
using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using VS = vector<string>;
#define ALL(a) begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define MT make_tuple
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FF first
#define SS second
template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) {
return is >> p.FF >> p.SS;
}
template <class T> istream &operator>>(istream &is, vector<T> &xs) {
for (auto &x : xs)
is >> x;
return is;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
return os << p.FF << " " << p.SS;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &xs) {
for (unsigned int i = 0; i < xs.size(); ++i)
os << (i ? " " : "") << xs[i];
return os;
}
template <class T> void maxi(T &x, T y) {
if (x < y)
x = y;
}
template <class T> void mini(T &x, T y) {
if (x > y)
x = y;
}
void debug(istringstream &) {}
template <char sep = ',', class Head, class... Tail>
void debug(istringstream &iss, Head &&head, Tail &&...tail) {
string name;
getline(iss, name, ',');
cout << sep << name << "=" << head;
debug(iss, forward<Tail>(tail)...);
}
#ifdef PYONPOI
#define DEBUG(...) \
do { \
istringstream ss(#__VA_ARGS__); \
debug<' '>(ss, __VA_ARGS__); \
cout << endl; \
} while (0)
#else
#define DEBUG
#endif
const double EPS = 1e-10;
const double PI = acos(-1.0);
const LL MOD = 1e9 + 7;
const int MAX_LOG = 18;
VI mn[MAX_LOG];
VI mx[MAX_LOG];
class SparseTable {
private:
int n;
VI logTable;
public:
SparseTable(int n, const VI &xs) : n(n) {
logTable.resize(n + 1);
logTable[0] = logTable[1] = 0;
for (int i = 2; i <= n; ++i)
logTable[i] = logTable[i >> 1] + 1;
mn[0] = mx[0] = xs;
for (int i = 1; i < MAX_LOG; ++i) {
mn[i].resize(n);
mx[i].resize(n);
for (int j = 0; j < n; ++j) {
mn[i][j] = min(mn[i - 1][j], mn[i - 1][min(n - 1, j + (1 << (i - 1)))]);
mx[i][j] = max(mx[i - 1][j], mx[i - 1][min(n - 1, j + (1 << (i - 1)))]);
}
}
}
// RMQ [l,r)
int query_min(int l, int r) {
int d = logTable[r - l];
return min(mn[d][l], mn[d][r - (1 << d)]);
}
int query_max(int l, int r) {
int d = logTable[r - l];
return max(mx[d][l], mx[d][r - (1 << d)]);
}
};
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
int N, K;
cin >> N >> K;
VI xs(N);
cin >> xs;
REP(i, K + 1) xs.PB(N + i);
VI ord(N);
ord.back() = 1;
for (int i = N - 2; i >= 0; --i) {
ord[i] = 1;
if (xs[i] < xs[i + 1])
ord[i] += ord[i + 1];
}
SparseTable sp(SZ(xs), xs);
int ans = 0, nop = 0;
for (int i = 0; i + K <= N;) {
int k = 1;
for (; k < K; ++k) {
if (xs[i + k - 1] == sp.query_min(i + k - 1, i + k + K - 1) &&
sp.query_max(i, i + k + K) == xs[i + k + K - 1]) {
} else
break;
}
if (ord[i] >= K)
++nop;
else
++ans;
i += k;
}
mini(nop, 1);
DEBUG(nop);
ans += nop;
cout << ans << endl;
return 0;
} | /* Aa^~ kokoro ga pyonpyon suru n jaa^~
// ZZZXXkXkkkZ!``` ``` ``` ``` ``` ``` ``` ``` ``` ``` ```
```?Wfpppbpbbpbbpbbbkbkk
// ppbbbpbbpVr`` `` ` ` ` ` ```` `` ` ` `` ` ` ` ` ` ` ` ` `
dppbbkkkkkkkkkkqkqkk
// HkqqqqqkkWr`` ` ` ``` ``` `?G, ` ` ``.JC!```` ` ` `` ``
````(Wpbkkkkkkkkqkkkkkqk
// mmmmmqqqqpr` `` `` ```````.+zT=`` `` 7TO-.```````` `` ``
```(yppbkkkkkkkkkkkkkkk
// ggmgmmqqqH$ ``````````....````` ` ````````.`````` ``
``````.yfpppbbbbkkkkqqqqqH
// gmmmmmqqqkW<```` `````...````` .,.` ````....````` ```````
(Wbqqmgmmgmggggggggg
// qmmmqqqqkkWk.``````````````````` ;:<``
`````.`````````````-_<-?WHHqmmmmmmgmmgggg
// @@@@@@@gggHH6- ``````````````` `` _ `` ````````````````
._~~_.`-?Wkqmmmmmmmggg@g
// @@@@g@gggHY~.-<_- ``````````````````````````````````
._~~(<-``.`.(WHqqqmmggggmmm
// @@g@gggHH=.`..._<-___..```````````````````````. .-_~~~_(!``-.``.``
OHHWUWHmqHWXW
// gggggmqK1.``..~.. _<<+-(____.. ```````` ..__~~_((<<!.`.``` .``.``
j0C1XUHmHIdW
// ggmmqH0!,_``.>`````` _<<;<v<<<++((((((((((<<<<<<~_. (-.``~``.>..```
jOuWHHqHIdH
// gmmqkW!`(_ J>` `` ` _~<`_~~~~<<<<<<<~~__````````` ?1. ._`(__``` zXWHg@HkXH
// gHHWS{``(lJ<!``.``.```(:+>`._`````.` <..`` - ``. ` _ ?&._.I`_``````
.XyVfppppW
// HHHSv``.(X:_..... _..(;+<!.(<..-.....-.-_..+_`..<.`.`..`_IJd}
.`..````jqg@@@@@@
// XHWZ{..<Jk~!.`..
(<.-(+>(_.(1.(_..`.`.`.<_.+<_..<<-..._..-zy>.`_`...```.WH@HHHHH
// bkWt~.-jCz(_..`.(+<.(;<
._-<=_(<_..-....(_.<1<..(<<.`._..-JUS-._.`...```dHmH9VUH
// WUUO..(f.(c...__+z<-(+~`
_-+<_(><..__.`.(<._.z_.(1;_..__.(C(zT-(..`...``(WHR<+Xk
//
kkkk._(_.->..._(z;:_><.._>_+_<(1>_._<...(v<<.(<.(+z<..-_(Z~_<_j+_..`...`(WHKz1ZW
//
@@gR._+_..~..-<+z<<?<>```_.<_.(+1><_;_..(1_:`.<<??1z--(+Z!..<_.j<....`..(bgHAAQX
// @@mR.(j:..~.._<z!`.(>~```
~(_.(+<1><><_.(((_`.<__`.<_.(X>...<_.(<.....`.JUWWWyWW
//
@gmH_(zl..(.._+>```<+_````.~>``(+.<?>>_._(<```(<<``(__<>....<.._<.......dXkkkHHH
//
mmqHl(dk_.(_.-=~`.`.1-..._~-1.``_:`(??<_~(`.--.&_.`.<(;<...._.._<..`..._Xg@@@@@@
//
qHkpk(dX<.(;..j_```.(((JJ&a&-~``````.1<_```-(((e+.-(/`(>...._..(<......(Wmggg@@g
// HVHbWcz><__+_.(_.(dWWHHH@HHc~````````.+~``
(jHMMMHHHm&.?..._<..(<_..._.(WqqHHmHg
// 0>vWWkzZwl~<o.__`__~X@@HM@Hb ```.`.``. ````
d@@HHH@@K?76...(<..(<_...(_(ppWWWWHq
// X0XWHKXXw$<(z<.( ``
WHHMHHHH_``````````````.WHHMNMHHH_`(...(<_.(z_..._<(fWVC174W
//
XuXWHHWWz>__+z+.!`..??CZYCOX_`````````````.`~.OvTUZUS_`~.._+?_.(_~_.._zjO=1+~+jy
// kkkkkkkkX:._<z=1(_`` << ``->``.``.``.``.```` ?<``
(v!`._..(??_.(1._.._=dUOOzzzwX
// @@@@@@@@H<...1O=v<_...__ -_````````````````.`` `` ~.``
:.~+=?~.(;_(...jdQQQQQkkk
//
H@@@@@@@H~...(==>.~~~~~....`.`````````.`````.`........->.(===~~<<.(...(dg@@@@@@@
//
@@@H@@HHH_.__(=l>~.~~~~~....``.``.``.```..`......~~~~~(<_+=l=~_<.->..~_dqggggg@g
//
@H@@@@MHH_._<(=l>...........```````````````.`...~~~~~~+<(=lz=~((j=z_..~jWqmmgggm
//
@@H@@HHWH_._<(lll-.......```.````.``.`..`````........_z<+llZz~(lOO=<...(VYUUUW9Y
//
@@HMMHWZf>~_=:=llw+.`````````.`.```__~~_``.`````.....(z+llOOz_zllOlz~..~<<1+dW>_
//
MMM#MHHWXl~_=>1ltwOl&.`.``.`````.``````````````.````.(llttwtz(OltwOz<..__zwOwwOz
//
HM#HMHUUI<._1z+ttOZttlt&....``.``.`.````.``...``...(zZtttOktzjttttwlz_._<(Xkkkkk
//
HHHmHSZu:(_~+OztttXtttOZZttO+-..............-(+ztOttwttttd0tOZttttwOl<~.(_dMMHHH
//
rvuuXuuI~~<~(uttttwvOwwwkQQHMMHHHHHHHHHMMMNmgey?OwwwrtttwXOtwttttttXtO-~.((wZyyy
// HHHHHHK>(~(-(dOrtrrl(QgMHMMMHHHHHHHHHHHHHHHH##HMNkX0rrrrXXrd%``
(Ctwwtz_~.<(Wg@H
// NNNNNHD(~(zo~zXrrrQdHHMMNMHHHHHHHHHHHHHHHHHHHHHH##HNmyrdKkwZ `
_``-zwrt1~~_<(MNM
// MMMMM#<<_jwr:(Z4QHHMMHMHHHHHHHHHHHHHHHHHHHHHHHHHHHH###NHSXZ>`
~````.OXtt>~._<?MM
*/
#include <bits/stdc++.h>
using namespace std;
using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using VS = vector<string>;
#define ALL(a) begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define MT make_tuple
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FF first
#define SS second
template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) {
return is >> p.FF >> p.SS;
}
template <class T> istream &operator>>(istream &is, vector<T> &xs) {
for (auto &x : xs)
is >> x;
return is;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
return os << p.FF << " " << p.SS;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &xs) {
for (unsigned int i = 0; i < xs.size(); ++i)
os << (i ? " " : "") << xs[i];
return os;
}
template <class T> void maxi(T &x, T y) {
if (x < y)
x = y;
}
template <class T> void mini(T &x, T y) {
if (x > y)
x = y;
}
void debug(istringstream &) {}
template <char sep = ',', class Head, class... Tail>
void debug(istringstream &iss, Head &&head, Tail &&...tail) {
string name;
getline(iss, name, ',');
cout << sep << name << "=" << head;
debug(iss, forward<Tail>(tail)...);
}
#ifdef PYONPOI
#define DEBUG(...) \
do { \
istringstream ss(#__VA_ARGS__); \
debug<' '>(ss, __VA_ARGS__); \
cout << endl; \
} while (0)
#else
#define DEBUG
#endif
const double EPS = 1e-10;
const double PI = acos(-1.0);
const LL MOD = 1e9 + 7;
const int MAX_LOG = 20;
VI mn[MAX_LOG];
VI mx[MAX_LOG];
class SparseTable {
private:
int n;
VI logTable;
public:
SparseTable(int n, const VI &xs) : n(n) {
logTable.resize(n + 1);
logTable[0] = logTable[1] = 0;
for (int i = 2; i <= n; ++i)
logTable[i] = logTable[i >> 1] + 1;
mn[0] = mx[0] = xs;
for (int i = 1; i < MAX_LOG; ++i) {
mn[i].resize(n);
mx[i].resize(n);
for (int j = 0; j < n; ++j) {
mn[i][j] = min(mn[i - 1][j], mn[i - 1][min(n - 1, j + (1 << (i - 1)))]);
mx[i][j] = max(mx[i - 1][j], mx[i - 1][min(n - 1, j + (1 << (i - 1)))]);
}
}
}
// RMQ [l,r)
int query_min(int l, int r) {
int d = logTable[r - l];
return min(mn[d][l], mn[d][r - (1 << d)]);
}
int query_max(int l, int r) {
int d = logTable[r - l];
return max(mx[d][l], mx[d][r - (1 << d)]);
}
};
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
int N, K;
cin >> N >> K;
VI xs(N);
cin >> xs;
REP(i, K + 1) xs.PB(N + i);
VI ord(N);
ord.back() = 1;
for (int i = N - 2; i >= 0; --i) {
ord[i] = 1;
if (xs[i] < xs[i + 1])
ord[i] += ord[i + 1];
}
SparseTable sp(SZ(xs), xs);
int ans = 0, nop = 0;
for (int i = 0; i + K <= N;) {
int k = 1;
for (; k < K; ++k) {
if (xs[i + k - 1] == sp.query_min(i + k - 1, i + k + K - 1) &&
sp.query_max(i, i + k + K) == xs[i + k + K - 1]) {
} else
break;
}
if (ord[i] >= K)
++nop;
else
++ans;
i += k;
}
mini(nop, 1);
DEBUG(nop);
ans += nop;
cout << ans << endl;
return 0;
} | replace | 164 | 165 | 164 | 165 | 0 | |
p02904 | C++ | Runtime Error | /**
* Created by hiramekun at 20:53 on 2019-09-21.
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using P = pair<ll, ll>;
template <typename T> using pq = priority_queue<T>;
template <typename T> using minpq = priority_queue<T, vector<T>, greater<T>>;
template <typename T, typename K> using ump = unordered_map<T, K>;
const ll dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const ll mod = 1000000007;
const ll inf = ll(1e9);
const ll e5 = ll(1e5);
const ll ll_inf = ll(1e9) * ll(1e9);
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repr(i, n) for (ll i = ll(n - 1); i >= 0; i--)
#define each(i, mp) for (auto &i : mp)
#define eb emplace_back
#define F first
#define S second
#define all(obj) (obj).begin(), (obj).end()
template <class T> ostream &operator<<(ostream &out, const vector<T> &list) {
ll n = list.size();
rep(i, n) out << list[i] << ' ';
return out;
}
template <class T> istream &operator>>(istream &in, vector<T> &list) {
ll n = list.size();
rep(i, n) in >> list[i];
return in;
}
template <class T>
ostream &operator<<(ostream &out, const vector<vector<T>> &list) {
ll n = list.size();
rep(i, n) out << list[i] << '\n';
return out;
}
// sample: http://beet-aizu.hatenablog.com/entry/2017/12/01/225955
// verified with
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_2_A
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_2_F
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_2_G
template <typename Data, typename Lazy> struct SegmentTree {
public:
// mergeするときの操作
using MergeData = function<Data(Data, Data)>;
// lazyをxでupdate
using UpdateLazyFromX = function<Lazy(Lazy, Lazy)>;
// dataをlazyでupdate
using UpdateDataFromLazy = function<Data(Data, Lazy)>;
// lenが与えられた時にlazyを計算
using CalcLazyWithLen = function<Lazy(Lazy, ll)>;
// O(N)
SegmentTree(int n, const MergeData fm, const UpdateLazyFromX fl,
const UpdateDataFromLazy fa, const CalcLazyWithLen fw, Data M1,
Lazy A1)
: fm(fm), fl(fl), fa(fa), fw(fw), M1(M1), A1(A1) {
sz = 1;
while (sz < n)
sz *= 2;
seg.assign(2 * sz - 1, M1);
// lazy.assign(2 * sz - 1, A1);
}
void build(const vector<Data> &v) {
rep(i, sz) seg[i + sz - 1] = v[i];
for (ll i = sz - 2; i >= 0; --i) {
seg[i] = fm(seg[i * 2 + 1], seg[i * 2 + 2]);
}
}
// // O(logN)
// Data update(int a, int b, Data x) {
// return update(a, b, x, 0, 0, sz);
// }
// O(logN)
Data query(int a, int b) { return query(a, b, 0, 0, sz); }
int sz;
vector<Data> seg;
// vector<Lazy> lazy;
private:
const MergeData fm;
const UpdateLazyFromX fl;
const UpdateDataFromLazy fa;
const CalcLazyWithLen fw;
const Data M1;
const Lazy A1;
// void eval(int len, int k) {
// if (lazy[k] == A1) return;
// if (k * 2 + 1 < sz * 2 - 1) {
// lazy[2 * k + 1] = fl(lazy[2 * k + 1], lazy[k]);
// lazy[2 * k + 2] = fl(lazy[2 * k + 2], lazy[k]);
// }
// seg[k] = fa(seg[k], fw(lazy[k], len));
// lazy[k] = A1;
// }
// Data update(int a, int b, Data x, int k, int l, int r) {
//// eval(r - l, k);
// if (r <= a || b <= l) return seg[k];
// if (a <= l && r <= b) {
// lazy[k] = fl(lazy[k], x);
// return fa(seg[k], fw(lazy[k], r - l));
// }
// return seg[k] = fm(update(a, b, x, k * 2 + 1, l, (l + r) / 2),
// update(a, b, x, k * 2 + 2, (l + r) / 2, r));
// }
Data query(int a, int b, int k, int l, int r) {
// eval(r - l, k);
if (r <= a || b <= l)
return M1;
if (a <= l && r <= b)
return seg[k];
Data vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
Data vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
return fm(vl, vr);
}
};
/* ------------- ANSWER ------------- */
/* ---------------------------------- */
void solve() {
ll n, k;
cin >> n >> k;
vl p(n);
cin >> p;
ll same = 0, cont = 0;
rep(i, n) {
ll c = 0;
while (i + 1 < n && p[i] < p[i + 1]) {
c++;
i++;
}
ll tmp = max(0LL, (c - k + 2));
same += tmp;
cont += max(0LL, tmp - 1);
}
auto fm1 = [](ll a, ll b) { return min(a, b); };
auto fl1 = [](ll a, ll b) { return min(a, b); };
auto fa1 = [](ll a, ll b) { return min(a, b); };
auto fw1 = [](ll a, ll w) { return a; };
SegmentTree<ll, ll> minTree(n, fm1, fl1, fa1, fw1, INT_MAX, INT_MAX);
auto fm2 = [](ll a, ll b) { return max(a, b); };
auto fl2 = [](ll a, ll b) { return max(a, b); };
auto fa2 = [](ll a, ll b) { return max(a, b); };
auto fw2 = [](ll a, ll w) { return a; };
SegmentTree<ll, ll> maxTree(n, fm2, fl2, fa2, fw2, INT_MIN, INT_MIN);
minTree.build(p);
maxTree.build(p);
ll prev_min = ll_inf, prev_max = ll_inf;
ll ans = 0;
rep(i, n - k + 1) {
ll mi = minTree.query(i, i + k);
ll mx = maxTree.query(i, i + k);
if ((prev_min < mi && prev_max < mx))
;
else
ans++;
prev_min = mi, prev_max = mx;
}
if (same > 0) {
ans -= (same - cont);
ans++;
}
cout << max(1LL, ans) << '\n';
}
int main() {
#ifdef MY_DEBUG
while (true) {
#endif
solve();
#ifdef MY_DEBUG
}
#endif
return 0;
} | /**
* Created by hiramekun at 20:53 on 2019-09-21.
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using P = pair<ll, ll>;
template <typename T> using pq = priority_queue<T>;
template <typename T> using minpq = priority_queue<T, vector<T>, greater<T>>;
template <typename T, typename K> using ump = unordered_map<T, K>;
const ll dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const ll mod = 1000000007;
const ll inf = ll(1e9);
const ll e5 = ll(1e5);
const ll ll_inf = ll(1e9) * ll(1e9);
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repr(i, n) for (ll i = ll(n - 1); i >= 0; i--)
#define each(i, mp) for (auto &i : mp)
#define eb emplace_back
#define F first
#define S second
#define all(obj) (obj).begin(), (obj).end()
template <class T> ostream &operator<<(ostream &out, const vector<T> &list) {
ll n = list.size();
rep(i, n) out << list[i] << ' ';
return out;
}
template <class T> istream &operator>>(istream &in, vector<T> &list) {
ll n = list.size();
rep(i, n) in >> list[i];
return in;
}
template <class T>
ostream &operator<<(ostream &out, const vector<vector<T>> &list) {
ll n = list.size();
rep(i, n) out << list[i] << '\n';
return out;
}
// sample: http://beet-aizu.hatenablog.com/entry/2017/12/01/225955
// verified with
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_2_A
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_2_F
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_2_G
template <typename Data, typename Lazy> struct SegmentTree {
public:
// mergeするときの操作
using MergeData = function<Data(Data, Data)>;
// lazyをxでupdate
using UpdateLazyFromX = function<Lazy(Lazy, Lazy)>;
// dataをlazyでupdate
using UpdateDataFromLazy = function<Data(Data, Lazy)>;
// lenが与えられた時にlazyを計算
using CalcLazyWithLen = function<Lazy(Lazy, ll)>;
// O(N)
SegmentTree(int n, const MergeData fm, const UpdateLazyFromX fl,
const UpdateDataFromLazy fa, const CalcLazyWithLen fw, Data M1,
Lazy A1)
: fm(fm), fl(fl), fa(fa), fw(fw), M1(M1), A1(A1) {
sz = 1;
while (sz < n)
sz *= 2;
seg.assign(2 * sz - 1, M1);
// lazy.assign(2 * sz - 1, A1);
}
void build(const vector<Data> &v) {
ll n = v.size();
rep(i, n) seg[i + sz - 1] = v[i];
for (ll i = sz - 2; i >= 0; --i) {
seg[i] = fm(seg[i * 2 + 1], seg[i * 2 + 2]);
}
}
// // O(logN)
// Data update(int a, int b, Data x) {
// return update(a, b, x, 0, 0, sz);
// }
// O(logN)
Data query(int a, int b) { return query(a, b, 0, 0, sz); }
int sz;
vector<Data> seg;
// vector<Lazy> lazy;
private:
const MergeData fm;
const UpdateLazyFromX fl;
const UpdateDataFromLazy fa;
const CalcLazyWithLen fw;
const Data M1;
const Lazy A1;
// void eval(int len, int k) {
// if (lazy[k] == A1) return;
// if (k * 2 + 1 < sz * 2 - 1) {
// lazy[2 * k + 1] = fl(lazy[2 * k + 1], lazy[k]);
// lazy[2 * k + 2] = fl(lazy[2 * k + 2], lazy[k]);
// }
// seg[k] = fa(seg[k], fw(lazy[k], len));
// lazy[k] = A1;
// }
// Data update(int a, int b, Data x, int k, int l, int r) {
//// eval(r - l, k);
// if (r <= a || b <= l) return seg[k];
// if (a <= l && r <= b) {
// lazy[k] = fl(lazy[k], x);
// return fa(seg[k], fw(lazy[k], r - l));
// }
// return seg[k] = fm(update(a, b, x, k * 2 + 1, l, (l + r) / 2),
// update(a, b, x, k * 2 + 2, (l + r) / 2, r));
// }
Data query(int a, int b, int k, int l, int r) {
// eval(r - l, k);
if (r <= a || b <= l)
return M1;
if (a <= l && r <= b)
return seg[k];
Data vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
Data vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
return fm(vl, vr);
}
};
/* ------------- ANSWER ------------- */
/* ---------------------------------- */
void solve() {
ll n, k;
cin >> n >> k;
vl p(n);
cin >> p;
ll same = 0, cont = 0;
rep(i, n) {
ll c = 0;
while (i + 1 < n && p[i] < p[i + 1]) {
c++;
i++;
}
ll tmp = max(0LL, (c - k + 2));
same += tmp;
cont += max(0LL, tmp - 1);
}
auto fm1 = [](ll a, ll b) { return min(a, b); };
auto fl1 = [](ll a, ll b) { return min(a, b); };
auto fa1 = [](ll a, ll b) { return min(a, b); };
auto fw1 = [](ll a, ll w) { return a; };
SegmentTree<ll, ll> minTree(n, fm1, fl1, fa1, fw1, INT_MAX, INT_MAX);
auto fm2 = [](ll a, ll b) { return max(a, b); };
auto fl2 = [](ll a, ll b) { return max(a, b); };
auto fa2 = [](ll a, ll b) { return max(a, b); };
auto fw2 = [](ll a, ll w) { return a; };
SegmentTree<ll, ll> maxTree(n, fm2, fl2, fa2, fw2, INT_MIN, INT_MIN);
minTree.build(p);
maxTree.build(p);
ll prev_min = ll_inf, prev_max = ll_inf;
ll ans = 0;
rep(i, n - k + 1) {
ll mi = minTree.query(i, i + k);
ll mx = maxTree.query(i, i + k);
if ((prev_min < mi && prev_max < mx))
;
else
ans++;
prev_min = mi, prev_max = mx;
}
if (same > 0) {
ans -= (same - cont);
ans++;
}
cout << max(1LL, ans) << '\n';
}
int main() {
#ifdef MY_DEBUG
while (true) {
#endif
solve();
#ifdef MY_DEBUG
}
#endif
return 0;
} | replace | 77 | 78 | 77 | 79 | 0 | |
p02904 | C++ | Runtime Error | #include <bits/stdc++.h>
#define f first
#define s second
#define MOD 1000000007
#define PMOD 998244353
#define pb(x) push_back(x)
using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plii;
typedef pair<int, pii> piii;
const int INF = 1e9 + 10;
const ll LINF = 1LL * INF * INF;
const int MAXN = 2e5 + 10;
const int MAXM = 5e3 + 10;
priority_queue<int> pq;
vector<vector<int>> graph;
queue<int> que;
int A[MAXN];
int dp[MAXN];
bool chk[MAXN];
pii tree[MAXN];
char S[MAXN];
pii getv(pii a, pii b) {
int mn = min(a.f, b.f);
int mx = max(a.s, b.s);
return pii(mn, mx);
}
pii getans(int L, int R) {
pii res = pii(INF, 0);
while (L <= R) {
if (L & 1) {
res = getv(res, tree[L]);
L++;
}
if (!(R & 1)) {
res = getv(res, tree[R]);
R--;
}
L >>= 1;
R >>= 1;
}
return res;
}
int main() {
int n, m, k, a, b, x, y, q;
int sum = 0;
int cnt = 0;
int mx = 0;
int mn = INF;
int idx = -1;
int tc;
int base = 1;
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
for (int i = 0; i < n; i++)
cin >> A[i];
for (; base < n; base <<= 1)
;
for (int i = 0; i < n; i++)
tree[base + i] = pii(A[i], A[i]);
for (int i = n; i < base; i++)
tree[base + i] = pii(INF, 0);
for (int i = base - 1; i >= 1; i--)
tree[i] = getv(tree[i << 1], tree[(i << 1) | 1]);
pii cur;
int res = n - k + 1;
dp[0] = 1;
for (int i = 1; i < n; i++)
if (A[i] > A[i - 1])
dp[i] = dp[i - 1] + 1;
else
dp[i] = 1;
if (dp[k - 1] >= k)
chk[k - 1] = true;
for (int i = 0; i < n - k; i++) {
cur = getans(base + i, base + i + k);
if (chk[i])
cnt++;
if (dp[i + k] >= k)
chk[i + k] = true;
if (cur.f == A[i] && cur.s == A[i + k])
res--;
else if (cnt && dp[i + k] >= k)
res--;
}
cout << res << "\n";
return 0;
}
| #include <bits/stdc++.h>
#define f first
#define s second
#define MOD 1000000007
#define PMOD 998244353
#define pb(x) push_back(x)
using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plii;
typedef pair<int, pii> piii;
const int INF = 1e9 + 10;
const ll LINF = 1LL * INF * INF;
const int MAXN = 2e5 + 10;
const int MAXM = 5e3 + 10;
priority_queue<int> pq;
vector<vector<int>> graph;
queue<int> que;
int A[MAXN];
int dp[MAXN];
bool chk[MAXN];
pii tree[4 * MAXN];
pii getv(pii a, pii b) {
int mn = min(a.f, b.f);
int mx = max(a.s, b.s);
return pii(mn, mx);
}
pii getans(int L, int R) {
pii res = pii(INF, 0);
while (L <= R) {
if (L & 1) {
res = getv(res, tree[L]);
L++;
}
if (!(R & 1)) {
res = getv(res, tree[R]);
R--;
}
L >>= 1;
R >>= 1;
}
return res;
}
int main() {
int n, m, k, a, b, x, y, q;
int sum = 0;
int cnt = 0;
int mx = 0;
int mn = INF;
int idx = -1;
int tc;
int base = 1;
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
for (int i = 0; i < n; i++)
cin >> A[i];
for (; base < n; base <<= 1)
;
for (int i = 0; i < n; i++)
tree[base + i] = pii(A[i], A[i]);
for (int i = n; i < base; i++)
tree[base + i] = pii(INF, 0);
for (int i = base - 1; i >= 1; i--)
tree[i] = getv(tree[i << 1], tree[(i << 1) | 1]);
pii cur;
int res = n - k + 1;
dp[0] = 1;
for (int i = 1; i < n; i++)
if (A[i] > A[i - 1])
dp[i] = dp[i - 1] + 1;
else
dp[i] = 1;
if (dp[k - 1] >= k)
chk[k - 1] = true;
for (int i = 0; i < n - k; i++) {
cur = getans(base + i, base + i + k);
if (chk[i])
cnt++;
if (dp[i + k] >= k)
chk[i + k] = true;
if (cur.f == A[i] && cur.s == A[i + k])
res--;
else if (cnt && dp[i + k] >= k)
res--;
}
cout << res << "\n";
return 0;
}
| replace | 24 | 26 | 24 | 25 | 0 | |
p02904 | C++ | Runtime Error | #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pp pair<int, int>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ld long double
#define al(a) (a).begin(), (a).end()
#define mk make_pair
#define check cout << "?" << endl;
ll MOD = 1000000007;
ll mod = 998244353;
int inf = 1000001000;
ll INF = 1e18 + 5;
int main() {
int n, k;
cin >> n >> k;
int ans = n - k + 1, v = 0, cnt = 1;
vector<int> p(n, 0), l(n - k + 1, 0);
rep(i, n) cin >> p[i];
set<pp> s;
rep(i, n - 1) {
if (p[i] < p[i + 1])
cnt++;
else
cnt = 1;
if (cnt >= k) {
l[i - k + 1] = 1;
if (v)
ans--;
v = 1;
}
}
rep(i, k) s.insert(mk(p[i], i));
for (int i = 1; i < n - k + 1; i++) {
if (l[i] == 0) {
if ((*begin(s)).second == i - 1 && (*rbegin(s)).first < p[i + k - 1]) {
ans--;
}
}
s.insert(mk(p[i + k - 1], i + k - 1));
s.erase(mk(p[i - 1], i - 1));
}
cout << ans << endl;
}
| #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pp pair<int, int>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ld long double
#define al(a) (a).begin(), (a).end()
#define mk make_pair
#define check cout << "?" << endl;
ll MOD = 1000000007;
ll mod = 998244353;
int inf = 1000001000;
ll INF = 1e18 + 5;
int main() {
int n, k;
cin >> n >> k;
int ans = n - k + 1, v = 0, cnt = 1;
vector<int> p(n, 0), l(n - k + 1, 0);
rep(i, n) cin >> p[i];
set<pp> s;
rep(i, n - 1) {
if (p[i] < p[i + 1])
cnt++;
else
cnt = 1;
if (cnt >= k) {
l[i - k + 2] = 1;
if (v)
ans--;
v = 1;
}
}
rep(i, k) s.insert(mk(p[i], i));
for (int i = 1; i < n - k + 1; i++) {
if (l[i] == 0) {
if ((*begin(s)).second == i - 1 && (*rbegin(s)).first < p[i + k - 1]) {
ans--;
}
}
s.insert(mk(p[i + k - 1], i + k - 1));
s.erase(mk(p[i - 1], i - 1));
}
cout << ans << endl;
}
| replace | 29 | 30 | 29 | 30 | 0 | |
p02904 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e5;
deque<pii> qac, qde;
int p[N];
int main() {
int n, k;
cin >> n >> k;
for (int i = 0; i < n; i++) {
scanf("%d", &p[i]);
}
int len = 1;
int cnt2 = 0;
for (int i = 1; i < n; i++) {
if (p[i] > p[i - 1])
len++;
else
len = 1;
if (len == k)
cnt2++;
}
for (int i = 0; i < k; i++) {
while (!qac.empty() && qac.back().first > p[i])
qac.pop_back();
qac.push_back({p[i], i});
while (!qde.empty() && qde.back().first < p[i])
qde.pop_back();
qde.push_back({p[i], i});
}
int cnt = 0;
for (int i = k; i < n; i++) {
// cout << qac.front().first << " ";
// cout << qde.front().first << endl;
bool f1 = qac.front().first == p[i - k];
if (qac.front().second <= i - k)
qac.pop_front();
if (qde.front().second <= i - k)
qde.pop_front();
while (!qac.empty() && qac.back().first > p[i])
qac.pop_back();
qac.push_back({p[i], i});
while (!qde.empty() && qde.back().first < p[i])
qde.pop_back();
qde.push_back({p[i], i});
bool f2 = qde.front().first == p[i];
if (f1 && f2) {
// cout << "same" << endl;
cnt++;
}
}
cout << n - k + 1 - cnt - max(0, cnt2 - 1);
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 2e5 + 10;
deque<pii> qac, qde;
int p[N];
int main() {
int n, k;
cin >> n >> k;
for (int i = 0; i < n; i++) {
scanf("%d", &p[i]);
}
int len = 1;
int cnt2 = 0;
for (int i = 1; i < n; i++) {
if (p[i] > p[i - 1])
len++;
else
len = 1;
if (len == k)
cnt2++;
}
for (int i = 0; i < k; i++) {
while (!qac.empty() && qac.back().first > p[i])
qac.pop_back();
qac.push_back({p[i], i});
while (!qde.empty() && qde.back().first < p[i])
qde.pop_back();
qde.push_back({p[i], i});
}
int cnt = 0;
for (int i = k; i < n; i++) {
// cout << qac.front().first << " ";
// cout << qde.front().first << endl;
bool f1 = qac.front().first == p[i - k];
if (qac.front().second <= i - k)
qac.pop_front();
if (qde.front().second <= i - k)
qde.pop_front();
while (!qac.empty() && qac.back().first > p[i])
qac.pop_back();
qac.push_back({p[i], i});
while (!qde.empty() && qde.back().first < p[i])
qde.pop_back();
qde.push_back({p[i], i});
bool f2 = qde.front().first == p[i];
if (f1 && f2) {
// cout << "same" << endl;
cnt++;
}
}
cout << n - k + 1 - cnt - max(0, cnt2 - 1);
} | replace | 4 | 5 | 4 | 5 | 0 | |
p02904 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
typedef long long i64;
int n, k, p[N], t[N], ans, cnt;
i64 s;
void add(int x, int d) {
for (; x <= n; x += x & -x)
t[x] += d;
}
int ask(int x) {
int y = 0;
for (; x; x ^= x & -x)
y += t[x];
return y;
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; ++i)
scanf("%d", p + i), ++p[i];
for (int i = k; i; --i)
s += ask(p[i]), add(p[i], 1);
ans = s > 0, cnt = s == 0;
for (int i = k + 1, w = 0, v; i <= n; ++i) {
w = (v = ask(p[i - k] - 1)) > 0, add(p[i - k], -1), s -= v,
w |= (v = k - 1 - ask(p[i])) > 0, add(p[i], 1), s += v, ans += w && s > 0,
cnt += s == 0;
}
printf("%d\n", ans + (cnt > 0));
return 0;
} | #include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
typedef long long i64;
int n, k, p[N], t[N], ans, cnt;
i64 s;
void add(int x, int d) {
for (; x <= n; x += x & -x)
t[x] += d;
}
int ask(int x) {
int y = 0;
for (; x; x ^= x & -x)
y += t[x];
return y;
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; ++i)
scanf("%d", p + i), ++p[i];
for (int i = k; i; --i)
s += ask(p[i]), add(p[i], 1);
ans = s > 0, cnt = s == 0;
for (int i = k + 1, w = 0, v; i <= n; ++i) {
w = (v = ask(p[i - k] - 1)) > 0, add(p[i - k], -1), s -= v,
w |= (v = k - 1 - ask(p[i])) > 0, add(p[i], 1), s += v, ans += w && s > 0,
cnt += s == 0;
}
printf("%d\n", ans + (cnt > 0));
return 0;
} | replace | 2 | 3 | 2 | 3 | 0 | |
p02904 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int n, k;
const int maxn = 200005;
int a[maxn];
int ans;
set<int> st;
int b[maxn];
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
int now = 1;
int cnt = 0;
for (int i = 2; i <= n; i++) {
if (a[i] < a[i - 1])
now = i;
if (i - now + 1 >= k)
b[i] = 1, cnt = 1;
}
if (!b[k])
ans = 1;
for (int i = 1; i <= n; i++)
for (int i = 1; i <= k; i++) {
st.insert(a[i]);
}
for (int i = k + 1; i <= n; i++) {
ans++;
if (b[i] || (st.lower_bound(a[i]) == st.end() &&
st.lower_bound(a[i - k]) == st.begin())) {
ans--;
}
st.erase(a[i - k]);
st.insert(a[i]);
}
cout << ans + cnt << endl;
}
| #include <bits/stdc++.h>
using namespace std;
int n, k;
const int maxn = 200005;
int a[maxn];
int ans;
set<int> st;
int b[maxn];
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
int now = 1;
int cnt = 0;
for (int i = 2; i <= n; i++) {
if (a[i] < a[i - 1])
now = i;
if (i - now + 1 >= k)
b[i] = 1, cnt = 1;
}
if (!b[k])
ans = 1;
for (int i = 1; i <= k; i++) {
st.insert(a[i]);
}
for (int i = k + 1; i <= n; i++) {
ans++;
if (b[i] || (st.lower_bound(a[i]) == st.end() &&
st.lower_bound(a[i - k]) == st.begin())) {
ans--;
}
st.erase(a[i - k]);
st.insert(a[i]);
}
cout << ans + cnt << endl;
}
| replace | 22 | 26 | 22 | 25 | TLE | |
p02904 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define REP(i, m, n) for (int i = (m); i < (int)(n); i++)
#define REPS(i, m, n) for (int i = (m); i <= (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define reps(i, n) for (int i = 0; i <= (int)(n); i++)
#define rrep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define rreps(i, x) for (int i = (int)(x); i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back
typedef long long ll;
typedef pair<int, int> P;
const int inf = INT_MAX;
const ll INF = 1LL << 60;
const ll mod = 1e9 + 7;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
fill((T *)array, (T *)(array + N), val);
}
class UnionFind {
public:
vector<ll> par;
vector<ll> siz;
UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) {
for (ll i = 0; i < sz_; ++i)
par[i] = i;
}
void init(ll sz_) {
par.resize(sz_);
siz.assign(sz_, 1LL);
for (ll i = 0; i < sz_; ++i)
par[i] = i;
}
ll root(ll x) {
while (par[x] != x) {
x = par[x] = par[par[x]];
}
return x;
}
bool unite(ll x, ll y) {
x = root(x);
y = root(y);
if (x == y)
return false;
if (siz[x] < siz[y])
swap(x, y);
siz[x] += siz[y];
par[y] = x;
return true;
}
bool issame(ll x, ll y) { return root(x) == root(y); }
ll size(ll x) { return siz[root(x)]; }
};
struct SegmentTree {
private:
int n;
vector<int> node;
public:
SegmentTree(vector<int> v) {
int sz = v.size();
n = 1;
// 2の累乗の大きさにする
while (n < sz)
n *= 2;
// 余りをinfで埋める
node.resize(2 * n - 1, inf);
// 葉を構築
for (int i = 0; i < sz; i++) {
node[i + n - 1] = v[i];
}
// 親を構築
for (int i = n - 2; i >= 0; i--) {
node[i] = min(node[2 * i + 1], node[2 * i + 2]);
}
}
void update(int x, int val) {
x += (n - 1);
node[x] = val;
while (x > 0) {
x = (x - 1) / 2;
node[x] = min(node[2 * x + 1], node[2 * x + 2]);
}
}
int getmin(int a, int b, int k = 0, int l = 0, int r = -1) {
if (r < 0)
r = n;
if (r <= a || b <= l)
return inf;
if (a <= l && r <= b)
return node[k];
int vl = getmin(a, b, 2 * k + 1, l, (l + r) / 2);
int vr = getmin(a, b, 2 * k + 2, (l + r) / 2, r);
return min(vl, vr);
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, k;
cin >> n >> k;
SegmentTree sg1(vector<int>(2 * n, inf)), sg2(vector<int>(2 * n, inf));
vector<int> a(n);
rep(i, n) {
cin >> a[i];
sg1.update(i, a[i]);
sg2.update(i, -a[i]);
}
UnionFind uf(n + 1);
REP(i, 1, n - k + 1) {
if (sg1.getmin(i - 1, i + k - 1) == a[i - 1] &&
sg2.getmin(i, i + k) == -a[k + i - 1]) {
uf.unite(i - 1, i);
}
}
vector<int> sum(n);
rep(i, n - 1) { sum[i + 1] = (a[i] > a[i + 1]); }
rep(i, n) sum[i + 1] += sum[i];
rep(i, n - k + 1) {
if (sum[i] == sum[i + k - 1]) {
uf.unite(i, n);
}
}
int ans = 0;
rep(i, n - k + 1) {
if (uf.root(i) == i)
ans++;
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define REP(i, m, n) for (int i = (m); i < (int)(n); i++)
#define REPS(i, m, n) for (int i = (m); i <= (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define reps(i, n) for (int i = 0; i <= (int)(n); i++)
#define rrep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define rreps(i, x) for (int i = (int)(x); i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back
typedef long long ll;
typedef pair<int, int> P;
const int inf = INT_MAX;
const ll INF = 1LL << 60;
const ll mod = 1e9 + 7;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
fill((T *)array, (T *)(array + N), val);
}
class UnionFind {
public:
vector<ll> par;
vector<ll> siz;
UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) {
for (ll i = 0; i < sz_; ++i)
par[i] = i;
}
void init(ll sz_) {
par.resize(sz_);
siz.assign(sz_, 1LL);
for (ll i = 0; i < sz_; ++i)
par[i] = i;
}
ll root(ll x) {
while (par[x] != x) {
x = par[x] = par[par[x]];
}
return x;
}
bool unite(ll x, ll y) {
x = root(x);
y = root(y);
if (x == y)
return false;
if (siz[x] < siz[y])
swap(x, y);
siz[x] += siz[y];
par[y] = x;
return true;
}
bool issame(ll x, ll y) { return root(x) == root(y); }
ll size(ll x) { return siz[root(x)]; }
};
struct SegmentTree {
private:
int n;
vector<int> node;
public:
SegmentTree(vector<int> v) {
int sz = v.size();
n = 1;
// 2の累乗の大きさにする
while (n < sz)
n *= 2;
// 余りをinfで埋める
node.resize(2 * n - 1, inf);
// 葉を構築
for (int i = 0; i < sz; i++) {
node[i + n - 1] = v[i];
}
// 親を構築
for (int i = n - 2; i >= 0; i--) {
node[i] = min(node[2 * i + 1], node[2 * i + 2]);
}
}
void update(int x, int val) {
x += (n - 1);
node[x] = val;
while (x > 0) {
x = (x - 1) / 2;
node[x] = min(node[2 * x + 1], node[2 * x + 2]);
}
}
int getmin(int a, int b, int k = 0, int l = 0, int r = -1) {
if (r < 0)
r = n;
if (r <= a || b <= l)
return inf;
if (a <= l && r <= b)
return node[k];
int vl = getmin(a, b, 2 * k + 1, l, (l + r) / 2);
int vr = getmin(a, b, 2 * k + 2, (l + r) / 2, r);
return min(vl, vr);
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, k;
cin >> n >> k;
SegmentTree sg1(vector<int>(2 * n, inf)), sg2(vector<int>(2 * n, inf));
vector<int> a(n);
rep(i, n) {
cin >> a[i];
sg1.update(i, a[i]);
sg2.update(i, -a[i]);
}
UnionFind uf(n + 1);
REP(i, 1, n - k + 1) {
if (sg1.getmin(i - 1, i + k - 1) == a[i - 1] &&
sg2.getmin(i, i + k) == -a[k + i - 1]) {
uf.unite(i - 1, i);
}
}
vector<int> sum(n + 1);
rep(i, n - 1) { sum[i + 1] = (a[i] > a[i + 1]); }
rep(i, n) sum[i + 1] += sum[i];
rep(i, n - k + 1) {
if (sum[i] == sum[i + k - 1]) {
uf.unite(i, n);
}
}
int ans = 0;
rep(i, n - k + 1) {
if (uf.root(i) == i)
ans++;
}
cout << ans << endl;
return 0;
} | replace | 142 | 143 | 142 | 143 | 0 | |
p02904 | C++ | Time Limit Exceeded | #include <cstdio>
#include <iostream>
#include <set>
using namespace std;
int n, k, c, cnt, ans = 1, a[200005];
set<int> st;
int main() {
int i;
cin >> n >> k;
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
for (i = 0; i < n - 1; i++) {
if (a[i] < a[i + 1])
c++;
else
c = 0;
if (c == k - 1)
cnt++;
}
for (i = 0; i < k; i++)
st.insert(a[i]);
while (i < n) {
st.erase(a[i - k]);
if (a[i - k] > *st.begin() || a[i] < *st.rbegin())
ans++;
st.insert(a[i]);
}
cout << ans - max(0, cnt - 1);
return 0;
}
| #include <cstdio>
#include <iostream>
#include <set>
using namespace std;
int n, k, c, cnt, ans = 1, a[200005];
set<int> st;
int main() {
int i;
cin >> n >> k;
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
for (i = 0; i < n - 1; i++) {
if (a[i] < a[i + 1])
c++;
else
c = 0;
if (c == k - 1)
cnt++;
}
for (i = 0; i < k; i++)
st.insert(a[i]);
while (i < n) {
st.erase(a[i - k]);
if (a[i - k] > *st.begin() || a[i] < *st.rbegin())
ans++;
st.insert(a[i]);
i++;
}
cout << ans - max(0, cnt - 1);
return 0;
}
| insert | 28 | 28 | 28 | 29 | TLE | |
p02904 | C++ | Runtime Error | #include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vii vector<int>
#define vll vector<ll>
#define lb lower_bound
#define pb push_back
#define mp make_pair
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rep2(i, a, b) for (ll i = a; i < b; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define all(x) x.begin(), x.end()
#define int ll
using namespace std;
const int INF(1 << 30);
const ll LLINF(1LL << 55LL);
const int MOD = 1000000007;
const int MAX = 510000;
struct UnionFind {
vector<int> par;
UnionFind(int n) : par(n, -1) {}
void init(int n) { par.assign(n, -1); }
int find(int x) {
if (par[x] < 0)
return x;
else
return par[x] = find(par[x]);
}
bool same(int x, int y) { return find(x) == find(y); }
bool unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return false;
if (par[x] > par[y])
swap(x, y);
par[x] += par[y];
par[y] = x;
return true;
}
int size(int x) { return -par[find(x)]; }
void print(int x) {
rep(i, x) { cout << par[i] << endl; }
return;
}
};
struct SegmentTree {
static const int MAX_N = 1 << 8;
int n, dat[2 * MAX_N - 1];
SegmentTree(int n_) {
n = 1;
while (n < n_)
n *= 2;
for (int i = 0; i < 2 * n - 1; i++)
dat[i] = INT_MAX;
}
void update(int k, int a) {
k += n - 1;
dat[k] = a;
while (k > 0) {
k = (k - 1) / 2;
dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
}
}
int query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return INT_MAX;
if (a <= l && r <= b)
return dat[k];
else {
int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
return min(vr, vl);
}
}
int query(int a, int b) { return query(a, b, 0, 0, n); }
};
struct SegmentTree1 {
static const int MAX_N = 1 << 12;
int n, dat[2 * MAX_N - 1];
SegmentTree1(int n_) {
n = 1;
while (n < n_)
n *= 2;
for (int i = 0; i < 2 * n - 1; i++)
dat[i] = -INT_MAX;
}
void update(int k, int a) {
k += n - 1;
dat[k] = a;
while (k > 0) {
k = (k - 1) / 2;
dat[k] = max(dat[k * 2 + 1], dat[k * 2 + 2]);
}
}
int query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return -INT_MAX;
if (a <= l && r <= b)
return dat[k];
else {
int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
return max(vr, vl);
}
}
int query(int a, int b) { return query(a, b, 0, 0, n); }
};
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
ll n, p[200010], k, a[200010];
set<ll> st;
pair<ll, ll> pl[200010];
cin >> n >> k;
SegmentTree sg(n), sg2(n);
UnionFind uf(n);
rep(i, n) {
cin >> p[i];
sg.update(i, p[i]);
sg2.update(i, -p[i]);
}
for (ll i = 1; i < n - k + 1; i++) {
pl[i] = pll(sg.query(i, i + k - 1), sg2.query(i, i + k - 1));
}
ll cnt = 1, ans = 0, S;
a[0] = 1;
for (ll i = 1; i < n; i++) {
if (p[i - 1] < p[i])
cnt++;
else
cnt = 1;
a[i] = cnt;
if (a[i] >= k)
st.insert(i - k + 1);
}
if (st.size() > 0) {
auto itr = st.begin();
S = *itr;
for (auto i : st) {
uf.unite(S, i);
}
} /*
vector<int> sum(n);
rep(i,n-1){
sum[i+1]=(p[i]>p[i+1]);
}
rep(i,n)sum[i+1]+=sum[i];
rep(i,n-k+1){
if(sum[i]==sum[i+k-1])uf.unite(i,n);
}*/
/*
rep(i,n-k){
if(p[i]<pl[i+1].first && pl[i+1].second<p[i+k]){
uf.unite(i,i+1);
}
}*/
rep2(i, 1, n - k + 1) {
if (sg.query(i - 1, i + k - 1) == p[i - 1] &&
sg2.query(i, i + k) == -p[k + i - 1]) {
// cout<<i<<"yes"<<endl;
uf.unite(i - 1, i);
}
}
rep(i, n - k + 1) {
if (i == uf.find(i)) {
// cout<<i<<endl;
++ans;
}
}
// cout<<uf.find(0)<<endl;
cout << ans << endl;
return 0;
} | #include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vii vector<int>
#define vll vector<ll>
#define lb lower_bound
#define pb push_back
#define mp make_pair
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rep2(i, a, b) for (ll i = a; i < b; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define all(x) x.begin(), x.end()
#define int ll
using namespace std;
const int INF(1 << 30);
const ll LLINF(1LL << 55LL);
const int MOD = 1000000007;
const int MAX = 510000;
struct UnionFind {
vector<int> par;
UnionFind(int n) : par(n, -1) {}
void init(int n) { par.assign(n, -1); }
int find(int x) {
if (par[x] < 0)
return x;
else
return par[x] = find(par[x]);
}
bool same(int x, int y) { return find(x) == find(y); }
bool unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return false;
if (par[x] > par[y])
swap(x, y);
par[x] += par[y];
par[y] = x;
return true;
}
int size(int x) { return -par[find(x)]; }
void print(int x) {
rep(i, x) { cout << par[i] << endl; }
return;
}
};
struct SegmentTree {
static const int MAX_N = 1 << 18;
int n, dat[2 * MAX_N - 1];
SegmentTree(int n_) {
n = 1;
while (n < n_)
n *= 2;
for (int i = 0; i < 2 * n - 1; i++)
dat[i] = INT_MAX;
}
void update(int k, int a) {
k += n - 1;
dat[k] = a;
while (k > 0) {
k = (k - 1) / 2;
dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
}
}
int query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return INT_MAX;
if (a <= l && r <= b)
return dat[k];
else {
int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
return min(vr, vl);
}
}
int query(int a, int b) { return query(a, b, 0, 0, n); }
};
struct SegmentTree1 {
static const int MAX_N = 1 << 12;
int n, dat[2 * MAX_N - 1];
SegmentTree1(int n_) {
n = 1;
while (n < n_)
n *= 2;
for (int i = 0; i < 2 * n - 1; i++)
dat[i] = -INT_MAX;
}
void update(int k, int a) {
k += n - 1;
dat[k] = a;
while (k > 0) {
k = (k - 1) / 2;
dat[k] = max(dat[k * 2 + 1], dat[k * 2 + 2]);
}
}
int query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return -INT_MAX;
if (a <= l && r <= b)
return dat[k];
else {
int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
return max(vr, vl);
}
}
int query(int a, int b) { return query(a, b, 0, 0, n); }
};
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
ll n, p[200010], k, a[200010];
set<ll> st;
pair<ll, ll> pl[200010];
cin >> n >> k;
SegmentTree sg(n), sg2(n);
UnionFind uf(n);
rep(i, n) {
cin >> p[i];
sg.update(i, p[i]);
sg2.update(i, -p[i]);
}
for (ll i = 1; i < n - k + 1; i++) {
pl[i] = pll(sg.query(i, i + k - 1), sg2.query(i, i + k - 1));
}
ll cnt = 1, ans = 0, S;
a[0] = 1;
for (ll i = 1; i < n; i++) {
if (p[i - 1] < p[i])
cnt++;
else
cnt = 1;
a[i] = cnt;
if (a[i] >= k)
st.insert(i - k + 1);
}
if (st.size() > 0) {
auto itr = st.begin();
S = *itr;
for (auto i : st) {
uf.unite(S, i);
}
} /*
vector<int> sum(n);
rep(i,n-1){
sum[i+1]=(p[i]>p[i+1]);
}
rep(i,n)sum[i+1]+=sum[i];
rep(i,n-k+1){
if(sum[i]==sum[i+k-1])uf.unite(i,n);
}*/
/*
rep(i,n-k){
if(p[i]<pl[i+1].first && pl[i+1].second<p[i+k]){
uf.unite(i,i+1);
}
}*/
rep2(i, 1, n - k + 1) {
if (sg.query(i - 1, i + k - 1) == p[i - 1] &&
sg2.query(i, i + k) == -p[k + i - 1]) {
// cout<<i<<"yes"<<endl;
uf.unite(i - 1, i);
}
}
rep(i, n - k + 1) {
if (i == uf.find(i)) {
// cout<<i<<endl;
++ans;
}
}
// cout<<uf.find(0)<<endl;
cout << ans << endl;
return 0;
} | replace | 72 | 73 | 72 | 73 | 0 | |
p02904 | C++ | Runtime Error | // https://atcoder.jp/contests/agc038/tasks/agc038_b
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("tree-loop-im")
#pragma GCC optimize("tree-loop-ivcanon")
#pragma GCC target("sse,sse2,sse3,sse4,abm,avx,avx2,fma,mmx,popcnt,tune=native")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace std::placeholders;
using namespace __gnu_pbds;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define f first
#define s second
#define all(a) (a).begin(), (a).end()
#define For(i, a, b) for (auto i = (a); i < (b); i++)
#define FOR(i, b) For(i, 0, b)
#define Rev(i, a, b) for (auto i = (a); i > (b); i--)
#define REV(i, a) Rev(i, a, -1)
#define FORE(i, a) for (auto &&i : a)
template <class C> constexpr int sz(const C &c) { return int(c.size()); }
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pill = pair<int, ll>;
using plli = pair<ll, int>;
using pdd = pair<double, double>;
using pld = pair<ld, ld>;
constexpr const char nl = '\n', sp = ' ';
constexpr const int INT_INF = 0x3f3f3f3f;
constexpr const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
constexpr const double D_INF = numeric_limits<double>::infinity();
constexpr const ld LD_INF = numeric_limits<ld>::infinity();
template <class T> constexpr const T &_min(const T &x, const T &y) {
return x < y ? x : y;
}
template <class T> constexpr const T &_max(const T &x, const T &y) {
return x < y ? y : x;
}
template <class T, class... Ts>
constexpr const T &_min(const T &x, const Ts &...xs) {
return _min(x, _min(xs...));
}
template <class T, class... Ts>
constexpr const T &_max(const T &x, const Ts &...xs) {
return _max(x, _max(xs...));
}
template <class T, class... Ts> void MIN(T &x, const Ts &...xs) {
x = _min(x, xs...);
}
template <class T, class... Ts> void MAX(T &x, const Ts &...xs) {
x = _max(x, xs...);
}
template <class T>
constexpr const T &_clamp(const T &v, const T &lo, const T &hi) {
return v < lo ? lo : hi < v ? hi : v;
}
template <class T> void CLAMP(T &v, const T &lo, const T &hi) {
v = _clamp(v, lo, hi);
}
template <class T, class... Args> unique_ptr<T> _make_unique(Args &&...args) {
return unique_ptr<T>(new T(forward<Args>(args)...));
}
template <class T, class... Args> shared_ptr<T> _make_shared(Args &&...args) {
return shared_ptr<T>(new T(forward<Args>(args)...));
}
#define min(...) _min(__VA_ARGS__)
#define max(...) _max(__VA_ARGS__)
#define clamp(...) _clamp(__VA_ARGS__)
#define make_unique _make_unique
#define make_shared _make_shared
seed_seq seq{(uint64_t)chrono::duration_cast<chrono::nanoseconds>(
chrono::high_resolution_clock::now().time_since_epoch())
.count(),
(uint64_t)__builtin_ia32_rdtsc(),
(uint64_t)(uintptr_t)make_unique<char>().get()};
mt19937 rng(seq);
mt19937_64 rng64(seq);
const size_t RANDOM =
uniform_int_distribution<size_t>(0, (numeric_limits<size_t>::max)())(rng64);
template <class T, class H = hash<T>> struct rand_hash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(const T &x) const { return splitmix64(H{}(x) + RANDOM); }
};
template <class K, class H = rand_hash<K>, class... Ts>
using hashset = gp_hash_table<K, null_type, H, Ts...>;
template <class K, class V, class H = rand_hash<K>, class... Ts>
using hashmap = gp_hash_table<K, V, H, Ts...>;
template <class K, class C = less<K>, class... Ts>
using treeset = tree<K, null_type, C, rb_tree_tag,
tree_order_statistics_node_update, Ts...>;
template <class K, class V, class C = less<K>, class... Ts>
using treemap =
tree<K, V, C, rb_tree_tag, tree_order_statistics_node_update, Ts...>;
template <class K, class H = rand_hash<K>, class... Ts>
using uset = unordered_set<K, H, Ts...>;
template <class K, class V, class H = rand_hash<K>, class... Ts>
using umap = unordered_map<K, V, H, Ts...>;
template <class T> using minpq = std::priority_queue<T, vector<T>, greater<T>>;
template <class T> using maxpq = std::priority_queue<T, vector<T>, less<T>>;
template <class T>
using minpairingheap =
__gnu_pbds::priority_queue<T, greater<T>, pairing_heap_tag>;
template <class T>
using maxpairingheap = __gnu_pbds::priority_queue<T, less<T>, pairing_heap_tag>;
template <class T1, class T2, class H1 = rand_hash<T1>,
class H2 = rand_hash<T2>>
struct pair_hash {
size_t operator()(const pair<T1, T2> &p) const {
return 31 * H1{}(p.first) + H2{}(p.second);
}
};
template <class T> struct is_iterator {
template <class U, typename enable_if<!is_convertible<U, const char *>::value,
int>::type = 0>
constexpr static auto has_indirection(int)
-> decltype(*declval<U>(), bool()) {
return true;
}
template <class> constexpr static bool has_indirection(long) { return false; }
constexpr static bool value = has_indirection<T>(0);
};
#define INTERACTIVE_INPUT 0
constexpr const int _bufferSize = 1 << 16, _maxNumLength = 128;
char _inputBuffer[_bufferSize + 1],
*_inputPtr = _inputBuffer, _outputBuffer[_bufferSize], _c, _sign,
*_tempInputBuf = nullptr, _numBuf[_maxNumLength],
_tempOutputBuf[_maxNumLength], _fill = ' ';
FILE *_input = stdin, *_output = stdout, *_error = stderr;
const char *_delimiter = " ";
int _cur, _outputPtr = 0, _numPtr = 0, _precision = 6, _width = 0,
_tempOutputPtr = 0, _cnt;
ull _precisionBase = 1000000;
#ifdef _WIN32
#define getchar_unlocked getchar
#define fread_unlocked fread
#define fwrite_unlocked fwrite
#endif
#if INTERACTIVE_INPUT
#define _getchar() getchar_unlocked()
#else
#define _peekchar() \
(*_inputPtr ? *_inputPtr \
: (_inputBuffer[fread_unlocked(_inputPtr = _inputBuffer, 1, \
_bufferSize, _input)] = '\0', \
*_inputPtr))
#define _getchar() \
(*_inputPtr ? *_inputPtr++ \
: (_inputBuffer[fread_unlocked(_inputPtr = _inputBuffer, 1, \
_bufferSize, _input)] = '\0', \
*_inputPtr++))
#define _hasNext() (*_inputPtr || !feof(_input))
bool hasNext() {
while (_hasNext() && _peekchar() <= ' ')
_getchar();
return _hasNext();
}
bool hasNextLine() {
while (_hasNext() && _peekchar() == '\r')
_getchar();
return _hasNext();
}
#endif
#define _readSignAndNum(x) \
while (((x) = _getchar()) <= ' ') \
; \
_sign = (x) == '-'; \
if (_sign) \
(x) = _getchar(); \
for ((x) -= '0'; (_c = _getchar()) >= '0'; (x) = (x)*10 + _c - '0')
#define _readFloatingPoint(x, T) \
for (T _div = 1.0; (_c = _getchar()) >= '0'; (x) += (_c - '0') / (_div *= 10))
#define rc(x) \
do { \
while (((x) = _getchar()) <= ' ') \
; \
} while (0)
#define ri(x) \
do { \
_readSignAndNum(x); \
if (_sign) \
(x) = -(x); \
} while (0)
#define rd(x) \
do { \
_readSignAndNum(x); \
if (_c == '.') \
_readFloatingPoint(x, double); \
if (_sign) \
(x) = -(x); \
} while (0)
#define rld(x) \
do { \
_readSignAndNum(x); \
if (_c == '.') \
_readFloatingPoint(x, ld); \
if (_sign) \
(x) = -(x); \
} while (0)
#define rcs(x) \
do { \
_cur = 0; \
do { \
_c = _getchar(); \
} while (_c <= ' '); \
do { \
(x)[_cur++] = _c; \
} while ((_c = _getchar()) > ' '); \
(x)[_cur] = '\0'; \
} while (0)
#define rs(x) \
do { \
if (!_tempInputBuf) \
assert(0); \
rcs(_tempInputBuf); \
(x) = string(_tempInputBuf, _cur); \
} while (0)
#define rcln(x) \
do { \
while (_inputPtr != _inputBuffer && *(_inputPtr - 1) == '\r') \
_getchar(); \
_cur = 0; \
while ((_c = _getchar()) != '\n' && _c) { \
if (_c != '\r') \
(x)[_cur++] = _c; \
} \
(x)[_cur] = '\0'; \
} while (0)
#define rln(x) \
do { \
if (!_tempInputBuf) \
assert(0); \
rcln(_tempInputBuf); \
(x) = string(_tempInputBuf, _cur); \
} while (0)
void setLength(int x) {
if (_tempInputBuf)
delete[] (_tempInputBuf);
_tempInputBuf = new char[x + 1];
}
void read(int &x) { ri(x); }
void read(uint &x) { ri(x); }
void read(ll &x) { ri(x); }
void read(ull &x) { ri(x); }
void read(double &x) { rd(x); }
void read(ld &x) { rld(x); }
void read(char &x) { rc(x); }
void read(char *x) { rcs(x); }
void read(string &x) { rs(x); }
void readln(char *x) { rcln(x); }
void readln(string &x) { rln(x); }
template <class T1, class T2> void read(pair<T1, T2> &x) {
read(x.first);
read(x.second);
}
template <class T> void read(complex<T> &x) {
T _re, _im;
read(_re);
read(_im);
x.real(_re);
x.imag(_im);
}
template <class T> void read(T &x);
template <class T, class... Ts> void read(T &x, Ts &&...xs);
template <class It>
typename enable_if<is_iterator<It>::value>::type read(It st, It en) {
for (It _i = st; _i != en; _i++)
read(*_i);
}
template <class It, class... Ts>
typename enable_if<is_iterator<It>::value>::type read(It st, It en,
Ts &&...xs) {
read(st, en);
read(forward<Ts>(xs)...);
}
template <class T> void read(T &x) {
for (auto &&_i : x)
read(_i);
}
template <class T, class... Ts> void read(T &x, Ts &&...xs) {
read(x);
read(forward<Ts>(xs)...);
}
void setInput(FILE *file) {
*_inputPtr = '\0';
_input = file;
}
void setInput(const char *s) {
*_inputPtr = '\0';
_input = fopen(s, "r");
}
void setInput(const string &s) {
*_inputPtr = '\0';
_input = fopen(s.c_str(), "r");
}
#define _flush() \
do { \
_flushBuf(); \
fflush(_output); \
} while (0)
#define _flushBuf() \
(fwrite_unlocked(_outputBuffer, 1, _outputPtr, _output), _outputPtr = 0)
#define _putchar(x) \
(_outputBuffer[_outputPtr == _bufferSize ? _flushBuf() : _outputPtr] = (x), \
_outputPtr++)
#define _writeTempBuf(x) (_tempOutputBuf[_tempOutputPtr++] = (x))
#define _writeOutput() \
for (int _i = 0; _i < _tempOutputPtr; _putchar(_tempOutputBuf[_i++])) \
; \
_tempOutputPtr = 0
#define _writeNum(x, T, digits) \
_cnt = 0; \
for (T _y = (x); _y; _y /= 10, _cnt++) \
_numBuf[_numPtr++] = '0' + _y % 10; \
for (; _cnt < digits; _cnt++) \
_numBuf[_numPtr++] = '0'; \
_flushNumBuf();
#define _writeFloatingPoint(x, T) \
ull _I = (ull)(x); \
ull _F = ((x)-_I) * _precisionBase + (T)(0.5); \
if (_F >= _precisionBase) { \
_I++; \
_F = 0; \
} \
_writeNum(_I, ull, 1); \
_writeTempBuf('.'); \
_writeNum(_F, ull, _precision)
#define _checkFinite(x) \
if (std::isnan(x)) { \
wcs("NaN"); \
} else if (std::isinf(x)) { \
wcs("Inf"); \
}
#define _flushNumBuf() for (; _numPtr; _writeTempBuf(_numBuf[--_numPtr]))
#define _fillBuf(x) \
for (int _i = 0; _i < (x); _i++) \
_putchar(_fill)
#define _flushTempBuf() \
int _tempLen = _tempOutputPtr; \
_fillBuf(_width - _tempLen); \
_writeOutput(); \
_fillBuf(-_width - _tempLen)
#define wb(x) \
do { \
if (x) \
_writeTempBuf('1'); \
else \
_writeTempBuf('0'); \
_flushTempBuf(); \
} while (0)
#define wc(x) \
do { \
_writeTempBuf(x); \
_flushTempBuf(); \
} while (0)
#define wi(x) \
do { \
if ((x) < 0) { \
_writeTempBuf('-'); \
_writeNum(-(x), uint, 1); \
} else { \
_writeNum(x, uint, 1); \
} \
_flushTempBuf(); \
} while (0)
#define wll(x) \
do { \
if ((x) < 0) { \
_writeTempBuf('-'); \
_writeNum(-(x), ull, 1); \
} else { \
_writeNum(x, ull, 1); \
} \
_flushTempBuf(); \
} while (0)
#define wd(x) \
do { \
_checkFinite(x) else if ((x) < 0) { \
_writeTempBuf('-'); \
_writeFloatingPoint(-(x), double); \
} \
else { \
_writeFloatingPoint(x, double); \
} \
_flushTempBuf(); \
} while (0)
#define wld(x) \
do { \
_checkFinite(x) else if ((x) < 0) { \
_writeTempBuf('-'); \
_writeFloatingPoint(-(x), ld); \
} \
else { \
_writeFloatingPoint(x, ld); \
} \
_flushTempBuf(); \
} while (0)
#define wcs(x) \
do { \
int _slen = strlen(x); \
_fillBuf(_width - _slen); \
for (const char *_p = (x); *_p; _putchar(*_p++)) \
; \
_fillBuf(-_width - _slen); \
} while (0)
#define ws(x) \
do { \
_fillBuf(_width - int((x).length())); \
for (int _i = 0; _i < int((x).length()); _putchar(x[_i++])) \
; \
_fillBuf(-_width - int((x).length())); \
} while (0)
void setPrecision(int x) {
_precision = x;
_precisionBase = 1;
for (int _i = 0; _i < x; _i++, _precisionBase *= 10)
;
}
void setWidth(int x) { _width = x; }
void setFill(char x) { _fill = x; }
void setDelimiter(const char *x) { _delimiter = x; }
void setDelimiter(const string &x) { _delimiter = x.c_str(); }
void writeDelimiter() {
for (const char *_p = _delimiter; *_p; _putchar(*_p++))
;
}
void write(const bool &x) { wb(x); }
void write(const int &x) { wi(x); }
void write(const uint &x) { wi(x); }
void write(const ll &x) { wll(x); }
void write(const ull &x) { wll(x); }
void write(const double &x) { wd(x); }
void write(const ld &x) { wld(x); }
void write(const char &x) { wc(x); }
void write(const char *x) { wcs(x); }
void write(const string &x) { ws(x); }
template <class T1, class T2> void write(const pair<T1, T2> &x) {
write(x.first);
writeDelimiter();
write(x.second);
}
template <class T> void write(const complex<T> &x) {
write(x.real());
writeDelimiter();
write(x.imag());
}
template <class T> void write(const T &x);
template <class T, class... Ts> void write(const T &x, Ts &&...xs);
template <class It>
typename enable_if<is_iterator<It>::value>::type write(It st, It en) {
bool _first = 1;
for (It _i = st; _i != en; _i++) {
if (_first)
_first = 0;
else
writeDelimiter();
write(*_i);
}
}
template <class It, class... Ts>
typename enable_if<is_iterator<It>::value>::type write(It st, It en,
Ts &&...xs) {
write(st, en);
writeDelimiter();
write(forward<Ts>(xs)...);
}
template <class T> void write(const T &x) {
bool _first = 1;
for (auto &&_i : x) {
if (_first)
_first = 0;
else
writeDelimiter();
write(_i);
}
}
template <class T, class... Ts> void write(const T &x, Ts &&...xs) {
write(x);
writeDelimiter();
write(forward<Ts>(xs)...);
}
void writeln() { _putchar('\n'); }
template <class... Ts> void writeln(Ts &&...xs) {
write(forward<Ts>(xs)...);
_putchar('\n');
}
void flush() { _flush(); }
class IOManager {
public:
~IOManager() {
flush();
if (_tempInputBuf)
delete[] (_tempInputBuf);
}
};
unique_ptr<IOManager> iomanager = make_unique<IOManager>();
void setOutput(FILE *file) {
flush();
_output = file;
}
void setOutput(const char *s) {
flush();
_output = fopen(s, "w");
}
void setOutput(const string &s) {
flush();
_output = fopen(s.c_str(), "w");
}
template <class... Ts> void debug(const Ts &...xs) {
FILE *_temp = _output;
setOutput(_error);
write(xs...);
setOutput(_temp);
}
template <class... Ts> void debugln(const Ts &...xs) {
FILE *_temp = _output;
setOutput(_error);
writeln(xs...);
setOutput(_temp);
}
void setError(FILE *file) {
flush();
_error = file;
}
void setError(const char *s) {
flush();
_error = fopen(s, "w");
}
void setError(const string &s) {
flush();
_error = fopen(s.c_str(), "w");
}
const int MAXN = 2e5 + 5, MAXLGN = 22;
template <const int MAXN, const int MAXLGN, class T, const bool IS_MAX>
struct SparseTableRMQ {
T A[MAXN];
int ST[MAXLGN][MAXN];
int cmpInd(int l, int r) { return (A[l] <= A[r]) ^ IS_MAX ? l : r; }
void init(int N) {
int lg = 32 - __builtin_clz(N);
assert(lg < MAXLGN);
iota(ST[0], ST[0] + N, 0);
for (int i = 0; i < lg - 1; i++)
for (int j = 0; j < N; j++)
ST[i + 1][j] = cmpInd(ST[i][j], ST[i][min(j + (1 << i), N - 1)]);
}
// 0-indexed, inclusive
int query(int l, int r) {
int i = 31 - __builtin_clz(r - l + 1);
return cmpInd(ST[i][l], ST[i][r - (1 << i) + 1]);
}
};
int N, K, P[MAXN], D[MAXN];
SparseTableRMQ<MAXN, MAXLGN, int, 1> MX;
SparseTableRMQ<MAXN, MAXLGN, int, 0> MN;
int main() {
// setInput("in.txt");
// setOutput("out.txt");
// setError("err.txt");
read(N, K);
if (K == 1) {
writeln(N);
return 0;
} else if (K == N) {
writeln(1);
return 0;
}
read(P, P + N);
copy(P, P + N, MX.A);
MX.init(N);
copy(P, P + N, MN.A);
MN.init(N);
adjacent_difference(P, P + N, D);
int ans = 0, hasNoSort = 0, cur = 0;
For(i, 1, K) cur += D[i] > 0;
if (cur == K - 1)
hasNoSort = 1;
else
ans = 1;
FOR(i, N - K) {
cur -= D[i + 1] > 0;
cur += D[i + K] > 0;
if (cur == K - 1)
hasNoSort = 1;
else if (i != MN.query(i, i + K - 1) || i + K != MX.query(i + 1, i + K))
ans++;
}
writeln(ans + hasNoSort);
return 0;
}
| // https://atcoder.jp/contests/agc038/tasks/agc038_b
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace std::placeholders;
using namespace __gnu_pbds;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define f first
#define s second
#define all(a) (a).begin(), (a).end()
#define For(i, a, b) for (auto i = (a); i < (b); i++)
#define FOR(i, b) For(i, 0, b)
#define Rev(i, a, b) for (auto i = (a); i > (b); i--)
#define REV(i, a) Rev(i, a, -1)
#define FORE(i, a) for (auto &&i : a)
template <class C> constexpr int sz(const C &c) { return int(c.size()); }
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pill = pair<int, ll>;
using plli = pair<ll, int>;
using pdd = pair<double, double>;
using pld = pair<ld, ld>;
constexpr const char nl = '\n', sp = ' ';
constexpr const int INT_INF = 0x3f3f3f3f;
constexpr const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
constexpr const double D_INF = numeric_limits<double>::infinity();
constexpr const ld LD_INF = numeric_limits<ld>::infinity();
template <class T> constexpr const T &_min(const T &x, const T &y) {
return x < y ? x : y;
}
template <class T> constexpr const T &_max(const T &x, const T &y) {
return x < y ? y : x;
}
template <class T, class... Ts>
constexpr const T &_min(const T &x, const Ts &...xs) {
return _min(x, _min(xs...));
}
template <class T, class... Ts>
constexpr const T &_max(const T &x, const Ts &...xs) {
return _max(x, _max(xs...));
}
template <class T, class... Ts> void MIN(T &x, const Ts &...xs) {
x = _min(x, xs...);
}
template <class T, class... Ts> void MAX(T &x, const Ts &...xs) {
x = _max(x, xs...);
}
template <class T>
constexpr const T &_clamp(const T &v, const T &lo, const T &hi) {
return v < lo ? lo : hi < v ? hi : v;
}
template <class T> void CLAMP(T &v, const T &lo, const T &hi) {
v = _clamp(v, lo, hi);
}
template <class T, class... Args> unique_ptr<T> _make_unique(Args &&...args) {
return unique_ptr<T>(new T(forward<Args>(args)...));
}
template <class T, class... Args> shared_ptr<T> _make_shared(Args &&...args) {
return shared_ptr<T>(new T(forward<Args>(args)...));
}
#define min(...) _min(__VA_ARGS__)
#define max(...) _max(__VA_ARGS__)
#define clamp(...) _clamp(__VA_ARGS__)
#define make_unique _make_unique
#define make_shared _make_shared
seed_seq seq{(uint64_t)chrono::duration_cast<chrono::nanoseconds>(
chrono::high_resolution_clock::now().time_since_epoch())
.count(),
(uint64_t)__builtin_ia32_rdtsc(),
(uint64_t)(uintptr_t)make_unique<char>().get()};
mt19937 rng(seq);
mt19937_64 rng64(seq);
const size_t RANDOM =
uniform_int_distribution<size_t>(0, (numeric_limits<size_t>::max)())(rng64);
template <class T, class H = hash<T>> struct rand_hash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(const T &x) const { return splitmix64(H{}(x) + RANDOM); }
};
template <class K, class H = rand_hash<K>, class... Ts>
using hashset = gp_hash_table<K, null_type, H, Ts...>;
template <class K, class V, class H = rand_hash<K>, class... Ts>
using hashmap = gp_hash_table<K, V, H, Ts...>;
template <class K, class C = less<K>, class... Ts>
using treeset = tree<K, null_type, C, rb_tree_tag,
tree_order_statistics_node_update, Ts...>;
template <class K, class V, class C = less<K>, class... Ts>
using treemap =
tree<K, V, C, rb_tree_tag, tree_order_statistics_node_update, Ts...>;
template <class K, class H = rand_hash<K>, class... Ts>
using uset = unordered_set<K, H, Ts...>;
template <class K, class V, class H = rand_hash<K>, class... Ts>
using umap = unordered_map<K, V, H, Ts...>;
template <class T> using minpq = std::priority_queue<T, vector<T>, greater<T>>;
template <class T> using maxpq = std::priority_queue<T, vector<T>, less<T>>;
template <class T>
using minpairingheap =
__gnu_pbds::priority_queue<T, greater<T>, pairing_heap_tag>;
template <class T>
using maxpairingheap = __gnu_pbds::priority_queue<T, less<T>, pairing_heap_tag>;
template <class T1, class T2, class H1 = rand_hash<T1>,
class H2 = rand_hash<T2>>
struct pair_hash {
size_t operator()(const pair<T1, T2> &p) const {
return 31 * H1{}(p.first) + H2{}(p.second);
}
};
template <class T> struct is_iterator {
template <class U, typename enable_if<!is_convertible<U, const char *>::value,
int>::type = 0>
constexpr static auto has_indirection(int)
-> decltype(*declval<U>(), bool()) {
return true;
}
template <class> constexpr static bool has_indirection(long) { return false; }
constexpr static bool value = has_indirection<T>(0);
};
#define INTERACTIVE_INPUT 0
constexpr const int _bufferSize = 1 << 16, _maxNumLength = 128;
char _inputBuffer[_bufferSize + 1],
*_inputPtr = _inputBuffer, _outputBuffer[_bufferSize], _c, _sign,
*_tempInputBuf = nullptr, _numBuf[_maxNumLength],
_tempOutputBuf[_maxNumLength], _fill = ' ';
FILE *_input = stdin, *_output = stdout, *_error = stderr;
const char *_delimiter = " ";
int _cur, _outputPtr = 0, _numPtr = 0, _precision = 6, _width = 0,
_tempOutputPtr = 0, _cnt;
ull _precisionBase = 1000000;
#ifdef _WIN32
#define getchar_unlocked getchar
#define fread_unlocked fread
#define fwrite_unlocked fwrite
#endif
#if INTERACTIVE_INPUT
#define _getchar() getchar_unlocked()
#else
#define _peekchar() \
(*_inputPtr ? *_inputPtr \
: (_inputBuffer[fread_unlocked(_inputPtr = _inputBuffer, 1, \
_bufferSize, _input)] = '\0', \
*_inputPtr))
#define _getchar() \
(*_inputPtr ? *_inputPtr++ \
: (_inputBuffer[fread_unlocked(_inputPtr = _inputBuffer, 1, \
_bufferSize, _input)] = '\0', \
*_inputPtr++))
#define _hasNext() (*_inputPtr || !feof(_input))
bool hasNext() {
while (_hasNext() && _peekchar() <= ' ')
_getchar();
return _hasNext();
}
bool hasNextLine() {
while (_hasNext() && _peekchar() == '\r')
_getchar();
return _hasNext();
}
#endif
#define _readSignAndNum(x) \
while (((x) = _getchar()) <= ' ') \
; \
_sign = (x) == '-'; \
if (_sign) \
(x) = _getchar(); \
for ((x) -= '0'; (_c = _getchar()) >= '0'; (x) = (x)*10 + _c - '0')
#define _readFloatingPoint(x, T) \
for (T _div = 1.0; (_c = _getchar()) >= '0'; (x) += (_c - '0') / (_div *= 10))
#define rc(x) \
do { \
while (((x) = _getchar()) <= ' ') \
; \
} while (0)
#define ri(x) \
do { \
_readSignAndNum(x); \
if (_sign) \
(x) = -(x); \
} while (0)
#define rd(x) \
do { \
_readSignAndNum(x); \
if (_c == '.') \
_readFloatingPoint(x, double); \
if (_sign) \
(x) = -(x); \
} while (0)
#define rld(x) \
do { \
_readSignAndNum(x); \
if (_c == '.') \
_readFloatingPoint(x, ld); \
if (_sign) \
(x) = -(x); \
} while (0)
#define rcs(x) \
do { \
_cur = 0; \
do { \
_c = _getchar(); \
} while (_c <= ' '); \
do { \
(x)[_cur++] = _c; \
} while ((_c = _getchar()) > ' '); \
(x)[_cur] = '\0'; \
} while (0)
#define rs(x) \
do { \
if (!_tempInputBuf) \
assert(0); \
rcs(_tempInputBuf); \
(x) = string(_tempInputBuf, _cur); \
} while (0)
#define rcln(x) \
do { \
while (_inputPtr != _inputBuffer && *(_inputPtr - 1) == '\r') \
_getchar(); \
_cur = 0; \
while ((_c = _getchar()) != '\n' && _c) { \
if (_c != '\r') \
(x)[_cur++] = _c; \
} \
(x)[_cur] = '\0'; \
} while (0)
#define rln(x) \
do { \
if (!_tempInputBuf) \
assert(0); \
rcln(_tempInputBuf); \
(x) = string(_tempInputBuf, _cur); \
} while (0)
void setLength(int x) {
if (_tempInputBuf)
delete[] (_tempInputBuf);
_tempInputBuf = new char[x + 1];
}
void read(int &x) { ri(x); }
void read(uint &x) { ri(x); }
void read(ll &x) { ri(x); }
void read(ull &x) { ri(x); }
void read(double &x) { rd(x); }
void read(ld &x) { rld(x); }
void read(char &x) { rc(x); }
void read(char *x) { rcs(x); }
void read(string &x) { rs(x); }
void readln(char *x) { rcln(x); }
void readln(string &x) { rln(x); }
template <class T1, class T2> void read(pair<T1, T2> &x) {
read(x.first);
read(x.second);
}
template <class T> void read(complex<T> &x) {
T _re, _im;
read(_re);
read(_im);
x.real(_re);
x.imag(_im);
}
template <class T> void read(T &x);
template <class T, class... Ts> void read(T &x, Ts &&...xs);
template <class It>
typename enable_if<is_iterator<It>::value>::type read(It st, It en) {
for (It _i = st; _i != en; _i++)
read(*_i);
}
template <class It, class... Ts>
typename enable_if<is_iterator<It>::value>::type read(It st, It en,
Ts &&...xs) {
read(st, en);
read(forward<Ts>(xs)...);
}
template <class T> void read(T &x) {
for (auto &&_i : x)
read(_i);
}
template <class T, class... Ts> void read(T &x, Ts &&...xs) {
read(x);
read(forward<Ts>(xs)...);
}
void setInput(FILE *file) {
*_inputPtr = '\0';
_input = file;
}
void setInput(const char *s) {
*_inputPtr = '\0';
_input = fopen(s, "r");
}
void setInput(const string &s) {
*_inputPtr = '\0';
_input = fopen(s.c_str(), "r");
}
#define _flush() \
do { \
_flushBuf(); \
fflush(_output); \
} while (0)
#define _flushBuf() \
(fwrite_unlocked(_outputBuffer, 1, _outputPtr, _output), _outputPtr = 0)
#define _putchar(x) \
(_outputBuffer[_outputPtr == _bufferSize ? _flushBuf() : _outputPtr] = (x), \
_outputPtr++)
#define _writeTempBuf(x) (_tempOutputBuf[_tempOutputPtr++] = (x))
#define _writeOutput() \
for (int _i = 0; _i < _tempOutputPtr; _putchar(_tempOutputBuf[_i++])) \
; \
_tempOutputPtr = 0
#define _writeNum(x, T, digits) \
_cnt = 0; \
for (T _y = (x); _y; _y /= 10, _cnt++) \
_numBuf[_numPtr++] = '0' + _y % 10; \
for (; _cnt < digits; _cnt++) \
_numBuf[_numPtr++] = '0'; \
_flushNumBuf();
#define _writeFloatingPoint(x, T) \
ull _I = (ull)(x); \
ull _F = ((x)-_I) * _precisionBase + (T)(0.5); \
if (_F >= _precisionBase) { \
_I++; \
_F = 0; \
} \
_writeNum(_I, ull, 1); \
_writeTempBuf('.'); \
_writeNum(_F, ull, _precision)
#define _checkFinite(x) \
if (std::isnan(x)) { \
wcs("NaN"); \
} else if (std::isinf(x)) { \
wcs("Inf"); \
}
#define _flushNumBuf() for (; _numPtr; _writeTempBuf(_numBuf[--_numPtr]))
#define _fillBuf(x) \
for (int _i = 0; _i < (x); _i++) \
_putchar(_fill)
#define _flushTempBuf() \
int _tempLen = _tempOutputPtr; \
_fillBuf(_width - _tempLen); \
_writeOutput(); \
_fillBuf(-_width - _tempLen)
#define wb(x) \
do { \
if (x) \
_writeTempBuf('1'); \
else \
_writeTempBuf('0'); \
_flushTempBuf(); \
} while (0)
#define wc(x) \
do { \
_writeTempBuf(x); \
_flushTempBuf(); \
} while (0)
#define wi(x) \
do { \
if ((x) < 0) { \
_writeTempBuf('-'); \
_writeNum(-(x), uint, 1); \
} else { \
_writeNum(x, uint, 1); \
} \
_flushTempBuf(); \
} while (0)
#define wll(x) \
do { \
if ((x) < 0) { \
_writeTempBuf('-'); \
_writeNum(-(x), ull, 1); \
} else { \
_writeNum(x, ull, 1); \
} \
_flushTempBuf(); \
} while (0)
#define wd(x) \
do { \
_checkFinite(x) else if ((x) < 0) { \
_writeTempBuf('-'); \
_writeFloatingPoint(-(x), double); \
} \
else { \
_writeFloatingPoint(x, double); \
} \
_flushTempBuf(); \
} while (0)
#define wld(x) \
do { \
_checkFinite(x) else if ((x) < 0) { \
_writeTempBuf('-'); \
_writeFloatingPoint(-(x), ld); \
} \
else { \
_writeFloatingPoint(x, ld); \
} \
_flushTempBuf(); \
} while (0)
#define wcs(x) \
do { \
int _slen = strlen(x); \
_fillBuf(_width - _slen); \
for (const char *_p = (x); *_p; _putchar(*_p++)) \
; \
_fillBuf(-_width - _slen); \
} while (0)
#define ws(x) \
do { \
_fillBuf(_width - int((x).length())); \
for (int _i = 0; _i < int((x).length()); _putchar(x[_i++])) \
; \
_fillBuf(-_width - int((x).length())); \
} while (0)
void setPrecision(int x) {
_precision = x;
_precisionBase = 1;
for (int _i = 0; _i < x; _i++, _precisionBase *= 10)
;
}
void setWidth(int x) { _width = x; }
void setFill(char x) { _fill = x; }
void setDelimiter(const char *x) { _delimiter = x; }
void setDelimiter(const string &x) { _delimiter = x.c_str(); }
void writeDelimiter() {
for (const char *_p = _delimiter; *_p; _putchar(*_p++))
;
}
void write(const bool &x) { wb(x); }
void write(const int &x) { wi(x); }
void write(const uint &x) { wi(x); }
void write(const ll &x) { wll(x); }
void write(const ull &x) { wll(x); }
void write(const double &x) { wd(x); }
void write(const ld &x) { wld(x); }
void write(const char &x) { wc(x); }
void write(const char *x) { wcs(x); }
void write(const string &x) { ws(x); }
template <class T1, class T2> void write(const pair<T1, T2> &x) {
write(x.first);
writeDelimiter();
write(x.second);
}
template <class T> void write(const complex<T> &x) {
write(x.real());
writeDelimiter();
write(x.imag());
}
template <class T> void write(const T &x);
template <class T, class... Ts> void write(const T &x, Ts &&...xs);
template <class It>
typename enable_if<is_iterator<It>::value>::type write(It st, It en) {
bool _first = 1;
for (It _i = st; _i != en; _i++) {
if (_first)
_first = 0;
else
writeDelimiter();
write(*_i);
}
}
template <class It, class... Ts>
typename enable_if<is_iterator<It>::value>::type write(It st, It en,
Ts &&...xs) {
write(st, en);
writeDelimiter();
write(forward<Ts>(xs)...);
}
template <class T> void write(const T &x) {
bool _first = 1;
for (auto &&_i : x) {
if (_first)
_first = 0;
else
writeDelimiter();
write(_i);
}
}
template <class T, class... Ts> void write(const T &x, Ts &&...xs) {
write(x);
writeDelimiter();
write(forward<Ts>(xs)...);
}
void writeln() { _putchar('\n'); }
template <class... Ts> void writeln(Ts &&...xs) {
write(forward<Ts>(xs)...);
_putchar('\n');
}
void flush() { _flush(); }
class IOManager {
public:
~IOManager() {
flush();
if (_tempInputBuf)
delete[] (_tempInputBuf);
}
};
unique_ptr<IOManager> iomanager = make_unique<IOManager>();
void setOutput(FILE *file) {
flush();
_output = file;
}
void setOutput(const char *s) {
flush();
_output = fopen(s, "w");
}
void setOutput(const string &s) {
flush();
_output = fopen(s.c_str(), "w");
}
template <class... Ts> void debug(const Ts &...xs) {
FILE *_temp = _output;
setOutput(_error);
write(xs...);
setOutput(_temp);
}
template <class... Ts> void debugln(const Ts &...xs) {
FILE *_temp = _output;
setOutput(_error);
writeln(xs...);
setOutput(_temp);
}
void setError(FILE *file) {
flush();
_error = file;
}
void setError(const char *s) {
flush();
_error = fopen(s, "w");
}
void setError(const string &s) {
flush();
_error = fopen(s.c_str(), "w");
}
const int MAXN = 2e5 + 5, MAXLGN = 22;
template <const int MAXN, const int MAXLGN, class T, const bool IS_MAX>
struct SparseTableRMQ {
T A[MAXN];
int ST[MAXLGN][MAXN];
int cmpInd(int l, int r) { return (A[l] <= A[r]) ^ IS_MAX ? l : r; }
void init(int N) {
int lg = 32 - __builtin_clz(N);
assert(lg < MAXLGN);
iota(ST[0], ST[0] + N, 0);
for (int i = 0; i < lg - 1; i++)
for (int j = 0; j < N; j++)
ST[i + 1][j] = cmpInd(ST[i][j], ST[i][min(j + (1 << i), N - 1)]);
}
// 0-indexed, inclusive
int query(int l, int r) {
int i = 31 - __builtin_clz(r - l + 1);
return cmpInd(ST[i][l], ST[i][r - (1 << i) + 1]);
}
};
int N, K, P[MAXN], D[MAXN];
SparseTableRMQ<MAXN, MAXLGN, int, 1> MX;
SparseTableRMQ<MAXN, MAXLGN, int, 0> MN;
int main() {
// setInput("in.txt");
// setOutput("out.txt");
// setError("err.txt");
read(N, K);
if (K == 1) {
writeln(N);
return 0;
} else if (K == N) {
writeln(1);
return 0;
}
read(P, P + N);
copy(P, P + N, MX.A);
MX.init(N);
copy(P, P + N, MN.A);
MN.init(N);
adjacent_difference(P, P + N, D);
int ans = 0, hasNoSort = 0, cur = 0;
For(i, 1, K) cur += D[i] > 0;
if (cur == K - 1)
hasNoSort = 1;
else
ans = 1;
FOR(i, N - K) {
cur -= D[i + 1] > 0;
cur += D[i + K] > 0;
if (cur == K - 1)
hasNoSort = 1;
else if (i != MN.query(i, i + K - 1) || i + K != MX.query(i + 1, i + K))
ans++;
}
writeln(ans + hasNoSort);
return 0;
}
| delete | 1 | 6 | 1 | 1 | 0 | |
p02904 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = std::int_fast64_t;
using std::cin;
using std::cout;
using std::endl;
template <typename T> std::vector<T> make_v(size_t a) {
return std::vector<T>(a);
}
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
return std::vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
#include <functional>
#include <vector>
template <typename Monoid> class segment_tree {
using value_type = Monoid;
using size_type = size_t;
using binary_function = std::function<value_type(value_type, value_type)>;
using checker = std::function<bool(value_type)>;
const size_type size_;
size_type height_;
const value_type id;
const binary_function bi_func;
std::vector<value_type> data;
private:
const size_type get_height(const size_type &size) const {
size_type height = 1;
while (1 << height < size)
height++;
return height;
}
const size_type base_size() const { return 1 << height_; }
void meld(const size_type &index) {
data[index] = bi_func(data[index << 1 ^ 0], data[index << 1 ^ 1]);
}
public:
segment_tree(const size_type &size, const binary_function &bi_func,
const value_type &id)
: size_(size), bi_func(bi_func), id(id) {
height_ = get_height(size);
data.assign(base_size() << 1, id);
}
value_type fold(size_type left, size_type right) {
value_type l_value = id, r_value = id;
for (left += base_size(), right += base_size(); left < right;
left >>= 1, right >>= 1) {
if (left & 1)
l_value = bi_func(l_value, data[left++]);
if (right & 1)
r_value = bi_func(data[--right], r_value);
}
return bi_func(std::move(l_value), std::move(r_value));
}
void update(size_type index, const value_type &value) {
index += base_size();
data[index] = bi_func(data[index], value);
while (index >>= 1)
meld(index);
}
void change(size_type index, const value_type &value) {
index += base_size();
data[index] = value;
while (index >>= 1)
meld(index);
}
const size_type search(const size_type &left, const checker &check) {
value_type val = id;
size_t base_size_ = base_size();
auto find = [&](auto &&find, size_type k, size_type l, size_type r) -> int {
if (l + 1 == r) {
val = bi_func(val, data[k]);
return (check(val) ? k - base_size_ : -1);
}
const size_type mid = (l + r) >> 1;
if (mid <= left)
return find(find, k << 1 ^ 1, mid, r);
if (left <= l and !check(bi_func(val, data[k]))) {
val = bi_func(val, data[k]);
return -1;
}
const int left_ret = find(find, k << 1 ^ 0, l, mid);
if (left_ret == -1)
return find(find, k << 1 ^ 1, mid, r);
return left_ret;
};
return find(find, 1, 0, base_size_);
}
value_type operator[](const size_type &index) const {
return data[index + base_size()];
}
const size_type size() const { return size_; }
const bool empty() const { return data.empty(); }
};
int main() {
int n, k;
scanf("%d%d", &n, &k);
std::vector<int> p(n);
for (int i = 0; i < n; i++)
scanf("%d", &p[i]);
auto g = [](int a, int b) { return std::min(a, b); };
auto f = [](int a, int b) { return std::max(a, b); };
segment_tree<int> A(n, f, -1);
segment_tree<int> B(n, g, 1 << 30);
for (int i = 0; i < n; i++)
A.change(i, p[i]);
for (int i = 0; i < n; i++)
B.change(i, p[i]);
int tmp = 0;
for (int i = 0; i < k - 1; i++)
tmp += p[i] < p[i + 1];
int ans = (tmp == k - 1 ? 0 : 1), C = (tmp == k - 1);
for (int i = k; i < n; i++) {
int a = A.fold(i - k + 1, i + 1);
int b = B.fold(i - k, i);
std::vector<int> vec;
for (int j = i - k + 1; j <= i; j++)
vec.push_back(p[j]);
sort(begin(vec), end(vec));
tmp -= p[i - k] < p[i - k + 1];
tmp += p[i - 1] < p[i];
C += (tmp == k - 1);
if (a == p[i] and b == p[i - k] or (tmp == k - 1))
continue;
ans++;
}
if (C)
ans++;
printf("%d\n", ans);
return 0;
}
| #include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = std::int_fast64_t;
using std::cin;
using std::cout;
using std::endl;
template <typename T> std::vector<T> make_v(size_t a) {
return std::vector<T>(a);
}
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
return std::vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
#include <functional>
#include <vector>
template <typename Monoid> class segment_tree {
using value_type = Monoid;
using size_type = size_t;
using binary_function = std::function<value_type(value_type, value_type)>;
using checker = std::function<bool(value_type)>;
const size_type size_;
size_type height_;
const value_type id;
const binary_function bi_func;
std::vector<value_type> data;
private:
const size_type get_height(const size_type &size) const {
size_type height = 1;
while (1 << height < size)
height++;
return height;
}
const size_type base_size() const { return 1 << height_; }
void meld(const size_type &index) {
data[index] = bi_func(data[index << 1 ^ 0], data[index << 1 ^ 1]);
}
public:
segment_tree(const size_type &size, const binary_function &bi_func,
const value_type &id)
: size_(size), bi_func(bi_func), id(id) {
height_ = get_height(size);
data.assign(base_size() << 1, id);
}
value_type fold(size_type left, size_type right) {
value_type l_value = id, r_value = id;
for (left += base_size(), right += base_size(); left < right;
left >>= 1, right >>= 1) {
if (left & 1)
l_value = bi_func(l_value, data[left++]);
if (right & 1)
r_value = bi_func(data[--right], r_value);
}
return bi_func(std::move(l_value), std::move(r_value));
}
void update(size_type index, const value_type &value) {
index += base_size();
data[index] = bi_func(data[index], value);
while (index >>= 1)
meld(index);
}
void change(size_type index, const value_type &value) {
index += base_size();
data[index] = value;
while (index >>= 1)
meld(index);
}
const size_type search(const size_type &left, const checker &check) {
value_type val = id;
size_t base_size_ = base_size();
auto find = [&](auto &&find, size_type k, size_type l, size_type r) -> int {
if (l + 1 == r) {
val = bi_func(val, data[k]);
return (check(val) ? k - base_size_ : -1);
}
const size_type mid = (l + r) >> 1;
if (mid <= left)
return find(find, k << 1 ^ 1, mid, r);
if (left <= l and !check(bi_func(val, data[k]))) {
val = bi_func(val, data[k]);
return -1;
}
const int left_ret = find(find, k << 1 ^ 0, l, mid);
if (left_ret == -1)
return find(find, k << 1 ^ 1, mid, r);
return left_ret;
};
return find(find, 1, 0, base_size_);
}
value_type operator[](const size_type &index) const {
return data[index + base_size()];
}
const size_type size() const { return size_; }
const bool empty() const { return data.empty(); }
};
int main() {
int n, k;
scanf("%d%d", &n, &k);
std::vector<int> p(n);
for (int i = 0; i < n; i++)
scanf("%d", &p[i]);
auto g = [](int a, int b) { return std::min(a, b); };
auto f = [](int a, int b) { return std::max(a, b); };
segment_tree<int> A(n, f, -1);
segment_tree<int> B(n, g, 1 << 30);
for (int i = 0; i < n; i++)
A.change(i, p[i]);
for (int i = 0; i < n; i++)
B.change(i, p[i]);
int tmp = 0;
for (int i = 0; i < k - 1; i++)
tmp += p[i] < p[i + 1];
int ans = (tmp == k - 1 ? 0 : 1), C = (tmp == k - 1);
for (int i = k; i < n; i++) {
int a = A.fold(i - k + 1, i + 1);
int b = B.fold(i - k, i);
tmp -= p[i - k] < p[i - k + 1];
tmp += p[i - 1] < p[i];
C += (tmp == k - 1);
if (a == p[i] and b == p[i - k] or (tmp == k - 1))
continue;
ans++;
}
if (C)
ans++;
printf("%d\n", ans);
return 0;
}
| delete | 136 | 141 | 136 | 136 | TLE | |
p02904 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#include <unistd.h>
using namespace std;
#define DEBUG(x) cerr << #x << ": " << x << endl;
#define DEBUG_VEC(v) \
cerr << #v << ":"; \
for (int i = 0; i < v.size(); i++) \
cerr << " " << v[i]; \
cerr << endl
#define DEBUG_MAT(v) \
cerr << #v << endl; \
for (int i = 0; i < v.size(); i++) { \
for (int j = 0; j < v[i].size(); j++) { \
cerr << v[i][j] << " "; \
} \
cerr << endl; \
}
typedef long long ll;
#define vi vector<int>
#define vl vector<ll>
#define vii vector<vector<int>>
#define vll vector<vector<ll>>
#define vs vector<string>
#define pii pair<int, int>
#define pis pair<int, string>
#define psi pair<string, int>
#define pll pair<ll, ll>
template <class S, class T>
pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) {
return pair<S, T>(s.first + t.first, s.second + t.second);
}
template <class S, class T>
pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) {
return pair<S, T>(s.first - t.first, s.second - t.second);
}
template <class S, class T> ostream &operator<<(ostream &os, pair<S, T> p) {
os << "(" << p.first << ", " << p.second << ")";
return os;
}
#define X first
#define Y second
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rrep1(i, n) for (int i = (n); i > 0; i--)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define in(x, a, b) (a <= x && x < b)
#define all(c) c.begin(), c.end()
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
#define UNIQUE(v) v.erase(std::unique(v.begin(), v.end()), v.end());
const ll inf = 1000000001;
const ll INF = (ll)1e18 + 1;
const ll MOD = 1000000007;
// const ll MOD = 998244353;
const double pi = 3.14159265358979323846;
#define Sp(p) cout << setprecision(15) << fixed << p << endl;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int dx2[8] = {1, 1, 0, -1, -1, -1, 0, 1}, dy2[8] = {0, 1, 1, 1, 0, -1, -1, -1};
#define fio() \
cin.tie(0); \
ios::sync_with_stdio(false);
// #define mp make_pair
// #define endl '\n'
template <typename T> class SegmentTree {
public:
using F = function<T(T &, T &)>;
int n;
vector<T> dat;
T e; // 単位元
F query_func;
F update_func;
SegmentTree(vector<T> a, F query_func, F update_func, T e)
: n(a.size()), query_func(query_func), update_func(update_func), e(e) {
dat.resize(4 * n);
init(0, 0, n, a);
}
void init(int k, int l, int r, vector<T> &a) {
if (r - l == 1) {
dat[k] = a[l];
} else {
int lch = 2 * k + 1, rch = 2 * k + 2;
init(lch, l, (l + r) / 2, a);
init(rch, (l + r) / 2, r, a);
dat[k] = query_func(dat[lch], dat[rch]);
}
}
// k番目の値をaに変更
void update(int k, T a, int v, int l, int r) {
if (r - l == 1) {
dat[v] = update_func(dat[v], a);
} else {
if (k < (l + r) / 2)
update(k, a, 2 * v + 1, l, (l + r) / 2);
else {
update(k, a, 2 * v + 2, (l + r) / 2, r);
}
dat[v] = query_func(dat[v * 2 + 1], dat[v * 2 + 2]);
}
}
//[a,b)の最小値を求める
// 後ろのほうの引数は計算の簡単のための引数
// kは接点の番号,l,rはその接点が[l,r)に対応していることを表す
// 従って、外からはquery(a,b,0,0,n)としてよぶ
T query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l) {
return e;
}
if (a <= l && r <= b) {
return dat[k];
} else {
T ul = query(a, b, k * 2 + 1, l, (l + r) / 2);
T ur = query(a, b, k * 2 + 2, (l + r) / 2, r);
return query_func(ul, ur);
}
}
};
int min_f(int a, int b) { return min(a, b); }
int max_f(int a, int b) { return max(a, b); }
int main() {
int n, k;
cin >> n >> k;
vi p(n);
rep(i, n) cin >> p[i];
set<pii> lr;
int l = 0, r = 1;
int pre = p[0];
while (r <= n) {
if (r == n) {
lr.insert(pii(l, r));
break;
}
if (p[r] > pre) {
pre = p[r];
r++;
} else {
lr.insert(pii(l, r));
l = r;
r = r + 1;
pre = p[l];
}
}
SegmentTree<int> min_seg(p, min_f, min_f, inf);
SegmentTree<int> max_seg(p, max_f, max_f, -inf);
int ans = 1;
bool al = false;
auto itr = lr.begin();
r = itr->second;
if (r >= k) {
al = true;
}
for (int i = 0; i + k < n; i++) {
auto itr = lr.lower_bound(pii(i + 1, i + inf));
if (itr != lr.begin()) {
itr--;
int l = itr->first, r = itr->second;
if (l <= i + 1 and r >= i + 1 + k) {
if (not al) {
al = true;
ans++;
}
continue;
}
}
vi p2 = p;
sort(p2.begin() + i, p2.begin() + i + k);
vi p3 = p;
sort(p3.begin() + i + 1, p3.begin() + i + k + 1);
if (p[i] == min_seg.query(i, i + k, 0, 0, min_seg.n) and
p[i + k] == max_seg.query(i + 1, i + k + 1, 0, 0, max_seg.n)) {
continue;
} else {
ans++;
}
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
#include <unistd.h>
using namespace std;
#define DEBUG(x) cerr << #x << ": " << x << endl;
#define DEBUG_VEC(v) \
cerr << #v << ":"; \
for (int i = 0; i < v.size(); i++) \
cerr << " " << v[i]; \
cerr << endl
#define DEBUG_MAT(v) \
cerr << #v << endl; \
for (int i = 0; i < v.size(); i++) { \
for (int j = 0; j < v[i].size(); j++) { \
cerr << v[i][j] << " "; \
} \
cerr << endl; \
}
typedef long long ll;
#define vi vector<int>
#define vl vector<ll>
#define vii vector<vector<int>>
#define vll vector<vector<ll>>
#define vs vector<string>
#define pii pair<int, int>
#define pis pair<int, string>
#define psi pair<string, int>
#define pll pair<ll, ll>
template <class S, class T>
pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) {
return pair<S, T>(s.first + t.first, s.second + t.second);
}
template <class S, class T>
pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) {
return pair<S, T>(s.first - t.first, s.second - t.second);
}
template <class S, class T> ostream &operator<<(ostream &os, pair<S, T> p) {
os << "(" << p.first << ", " << p.second << ")";
return os;
}
#define X first
#define Y second
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rrep1(i, n) for (int i = (n); i > 0; i--)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define in(x, a, b) (a <= x && x < b)
#define all(c) c.begin(), c.end()
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
#define UNIQUE(v) v.erase(std::unique(v.begin(), v.end()), v.end());
const ll inf = 1000000001;
const ll INF = (ll)1e18 + 1;
const ll MOD = 1000000007;
// const ll MOD = 998244353;
const double pi = 3.14159265358979323846;
#define Sp(p) cout << setprecision(15) << fixed << p << endl;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int dx2[8] = {1, 1, 0, -1, -1, -1, 0, 1}, dy2[8] = {0, 1, 1, 1, 0, -1, -1, -1};
#define fio() \
cin.tie(0); \
ios::sync_with_stdio(false);
// #define mp make_pair
// #define endl '\n'
template <typename T> class SegmentTree {
public:
using F = function<T(T &, T &)>;
int n;
vector<T> dat;
T e; // 単位元
F query_func;
F update_func;
SegmentTree(vector<T> a, F query_func, F update_func, T e)
: n(a.size()), query_func(query_func), update_func(update_func), e(e) {
dat.resize(4 * n);
init(0, 0, n, a);
}
void init(int k, int l, int r, vector<T> &a) {
if (r - l == 1) {
dat[k] = a[l];
} else {
int lch = 2 * k + 1, rch = 2 * k + 2;
init(lch, l, (l + r) / 2, a);
init(rch, (l + r) / 2, r, a);
dat[k] = query_func(dat[lch], dat[rch]);
}
}
// k番目の値をaに変更
void update(int k, T a, int v, int l, int r) {
if (r - l == 1) {
dat[v] = update_func(dat[v], a);
} else {
if (k < (l + r) / 2)
update(k, a, 2 * v + 1, l, (l + r) / 2);
else {
update(k, a, 2 * v + 2, (l + r) / 2, r);
}
dat[v] = query_func(dat[v * 2 + 1], dat[v * 2 + 2]);
}
}
//[a,b)の最小値を求める
// 後ろのほうの引数は計算の簡単のための引数
// kは接点の番号,l,rはその接点が[l,r)に対応していることを表す
// 従って、外からはquery(a,b,0,0,n)としてよぶ
T query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l) {
return e;
}
if (a <= l && r <= b) {
return dat[k];
} else {
T ul = query(a, b, k * 2 + 1, l, (l + r) / 2);
T ur = query(a, b, k * 2 + 2, (l + r) / 2, r);
return query_func(ul, ur);
}
}
};
int min_f(int a, int b) { return min(a, b); }
int max_f(int a, int b) { return max(a, b); }
int main() {
int n, k;
cin >> n >> k;
vi p(n);
rep(i, n) cin >> p[i];
set<pii> lr;
int l = 0, r = 1;
int pre = p[0];
while (r <= n) {
if (r == n) {
lr.insert(pii(l, r));
break;
}
if (p[r] > pre) {
pre = p[r];
r++;
} else {
lr.insert(pii(l, r));
l = r;
r = r + 1;
pre = p[l];
}
}
SegmentTree<int> min_seg(p, min_f, min_f, inf);
SegmentTree<int> max_seg(p, max_f, max_f, -inf);
int ans = 1;
bool al = false;
auto itr = lr.begin();
r = itr->second;
if (r >= k) {
al = true;
}
for (int i = 0; i + k < n; i++) {
auto itr = lr.lower_bound(pii(i + 1, i + inf));
if (itr != lr.begin()) {
itr--;
int l = itr->first, r = itr->second;
if (l <= i + 1 and r >= i + 1 + k) {
if (not al) {
al = true;
ans++;
}
continue;
}
}
if (p[i] == min_seg.query(i, i + k, 0, 0, min_seg.n) and
p[i + k] == max_seg.query(i + 1, i + k + 1, 0, 0, max_seg.n)) {
continue;
} else {
ans++;
}
}
cout << ans << endl;
}
| delete | 193 | 197 | 193 | 193 | TLE | |
p02904 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rrep(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define reps(i, n) for (int i = 1; i <= int(n); i++)
#define rreps(i, n) for (int i = int(n); i >= 1; i--)
#define repc(i, n) for (int i = 0; i <= int(n); i++)
#define rrepc(i, n) for (int i = int(n); i >= 0; i--)
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define repic(i, a, b) for (int i = int(a); i <= int(b); i++)
#define each(x, y) for (auto &x : y)
#define all(a) (a).begin(), (a).end()
#define bit32(x) (1 << (x))
#define bit64(x) (1ll << (x))
using namespace std;
using i64 = long long;
using f80 = long double;
using vi32 = vector<int>;
using vi64 = vector<i64>;
using vf80 = vector<f80>;
using vstr = vector<string>;
inline void yes() {
cout << "Yes" << endl;
exit(0);
}
inline void no() {
cout << "No" << endl;
exit(0);
}
inline i64 gcd(i64 a, i64 b) {
if (min(a, b) == 0)
return max(a, b);
if (a % b == 0)
return b;
return gcd(b, a % b);
}
inline i64 lcm(i64 a, i64 b) {
if (min(a, b) == 0)
return max(a, b);
return a / gcd(a, b) * b;
}
template <typename T>
class pqasc : public priority_queue<T, vector<T>, greater<T>> {};
template <typename T>
class pqdesc : public priority_queue<T, vector<T>, less<T>> {};
template <typename T> inline void amax(T &x, T y) { x = max(x, y); }
template <typename T> inline void amin(T &x, T y) { x = min(x, y); }
template <typename T> inline T exp(T x, i64 n, T e = 1) {
T r = e;
while (n > 0) {
if (n & 1)
r *= x;
x *= x;
n >>= 1;
}
return r;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
each(x, v) is >> x;
return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &v) {
rep(i, v.size()) {
if (i)
os << ' ';
os << v[i];
}
return os;
}
void solve();
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(16);
solve();
return 0;
}
void solve() {
int N, K;
cin >> N >> K;
vi32 P(N);
cin >> P;
int ans = 0;
vi32 nochange(N);
{
vi32 acc(N);
rep(i, N - 1) {
if (P[i] < P[i + 1]) {
acc[i + 1] = acc[i] + 1;
} else {
acc[i + 1] = acc[i] - 1;
}
}
for (int i = 0; i + K <= N; i++) {
if (acc[i + K - 1] - acc[i] == K - 1) {
nochange[i] = 1;
if (ans == 0)
ans = 1;
}
}
}
set<int> sasc;
set<int, greater<int>> sdsc;
for (int i = 0; i < N; i++) {
sasc.insert(P[i]);
sdsc.insert(P[i]);
if (nochange[i - K + 1] == 0 && sasc.size() == K) {
ans++;
}
if (sasc.size() > K) {
if ((*sasc.begin() == P[i - K]) && (*sdsc.begin() == P[i])) {
} else if (nochange[i - K + 1]) {
} else {
ans++;
}
sasc.erase(P[i - K]);
sdsc.erase(P[i - K]);
}
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rrep(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define reps(i, n) for (int i = 1; i <= int(n); i++)
#define rreps(i, n) for (int i = int(n); i >= 1; i--)
#define repc(i, n) for (int i = 0; i <= int(n); i++)
#define rrepc(i, n) for (int i = int(n); i >= 0; i--)
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define repic(i, a, b) for (int i = int(a); i <= int(b); i++)
#define each(x, y) for (auto &x : y)
#define all(a) (a).begin(), (a).end()
#define bit32(x) (1 << (x))
#define bit64(x) (1ll << (x))
using namespace std;
using i64 = long long;
using f80 = long double;
using vi32 = vector<int>;
using vi64 = vector<i64>;
using vf80 = vector<f80>;
using vstr = vector<string>;
inline void yes() {
cout << "Yes" << endl;
exit(0);
}
inline void no() {
cout << "No" << endl;
exit(0);
}
inline i64 gcd(i64 a, i64 b) {
if (min(a, b) == 0)
return max(a, b);
if (a % b == 0)
return b;
return gcd(b, a % b);
}
inline i64 lcm(i64 a, i64 b) {
if (min(a, b) == 0)
return max(a, b);
return a / gcd(a, b) * b;
}
template <typename T>
class pqasc : public priority_queue<T, vector<T>, greater<T>> {};
template <typename T>
class pqdesc : public priority_queue<T, vector<T>, less<T>> {};
template <typename T> inline void amax(T &x, T y) { x = max(x, y); }
template <typename T> inline void amin(T &x, T y) { x = min(x, y); }
template <typename T> inline T exp(T x, i64 n, T e = 1) {
T r = e;
while (n > 0) {
if (n & 1)
r *= x;
x *= x;
n >>= 1;
}
return r;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
each(x, v) is >> x;
return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &v) {
rep(i, v.size()) {
if (i)
os << ' ';
os << v[i];
}
return os;
}
void solve();
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(16);
solve();
return 0;
}
void solve() {
int N, K;
cin >> N >> K;
vi32 P(N);
cin >> P;
int ans = 0;
vi32 nochange(N);
{
vi32 acc(N);
rep(i, N - 1) {
if (P[i] < P[i + 1]) {
acc[i + 1] = acc[i] + 1;
} else {
acc[i + 1] = acc[i] - 1;
}
}
for (int i = 0; i + K <= N; i++) {
if (acc[i + K - 1] - acc[i] == K - 1) {
nochange[i] = 1;
if (ans == 0)
ans = 1;
}
}
}
set<int> sasc;
set<int, greater<int>> sdsc;
for (int i = 0; i < N; i++) {
sasc.insert(P[i]);
sdsc.insert(P[i]);
if (sasc.size() == K && nochange[i - K + 1] == 0) {
ans++;
}
if (sasc.size() > K) {
if ((*sasc.begin() == P[i - K]) && (*sdsc.begin() == P[i])) {
} else if (nochange[i - K + 1]) {
} else {
ans++;
}
sasc.erase(P[i - K]);
sdsc.erase(P[i - K]);
}
}
cout << ans << endl;
}
| replace | 110 | 111 | 110 | 111 | 0 | |
p02904 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <iterator>
#include <set>
using namespace std;
// using namespace __gnu_pbds;
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(), x.end()
#define ll long long int
#define vi vector<int>
#define vii vector<pair<int, int>>
#define pii pair<int, int>
#define plii pair<pair<ll, int>, int>
#define piii pair<pii, int>
#define viii vector<pair<pii, int>>
#define vl vector<ll>
#define vll vector<pair<ll, ll>>
#define pll pair<ll, ll>
#define pli pair<ll, int>
#define mp make_pair
#define ms(x, v) memset(x, v, sizeof x)
#define pr_vec(v) \
for (int i = 0; i < v.size(); i++) \
cout << v[i] << " ";
#define f_in(st) freopen(st, "r", stdin)
#define f_out(st) freopen(st, "w", stdout)
#define INF (ll)1e17
#define matrix vector<vector<ll>>
#define fr(i, a, b) for (i = a; i <= b; i++)
#define fb(i, a, b) for (i = a; i >= b; i--)
#define ASST(x, l, r) assert(x <= r && x >= l)
#define maxbits 9
#define maxmask (1ll << maxbits)
// typedef tree<pll, null_type, less<pll>, rb_tree_tag,
// tree_order_statistics_node_update>
// new_data_set;
const int N = (ll)2e3 + 10, M = (ll)1e9 + 1;
const int mod = (ll)1e9 + 7, MAX = (ll)1e6 + 10;
string fir = "Monocarp", sec = "Bicarp";
void work() {
ll n, k;
cin >> n >> k;
vl a(n);
for (ll i = 0; i < n; i++)
cin >> a[i];
vl pref(n, 0);
for (ll i = 1; i < n; i++)
pref[i] = a[i - 1] <= a[i];
for (ll i = 1; i < n; i++)
pref[i] += pref[i - 1];
vl mini(n, 0), maxi(n, 0);
set<ll> s;
for (ll i = 0; i < k - 1; i++) {
s.insert(a[i]);
// s_maxi.insert(-a[i]);
}
for (ll i = k - 1; i < n; i++) {
if (i >= k)
s.erase(a[i - k]);
s.insert(a[i]);
mini[i - k + 1] = (a[i - k + 1] == *(s.begin()));
maxi[i] = (a[i] == *(s.rbegin()));
}
for (ll i = 0; i < n; i++) {
// cout << pref[i] << " ";
}
vl issorted(n, 0);
ll anysorted = 0;
for (ll i = 0; i < n; i++) {
if (i - k + 1 >= 0)
issorted[i] = (pref[i] - pref[i - k + 1] == k - 1);
anysorted |= issorted[i];
}
ll ans = anysorted;
if (issorted[k - 1] == 0)
ans++;
// cout << anysorted << "\n";
for (ll i = k; i < n; i++) {
if (issorted[i])
continue;
if (mini[i - k] && maxi[i])
continue;
// cout << i << " ";
ans++;
}
cout << ans << "\n";
return;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
// to avoid printing in Scientific Notation like 2.14748e+09 and instead
// print: 2147483648.000000
cout << fixed;
// precomp();
ll t, z;
// cin >> t;
t = 1;
// z=t;
while (t--) {
// cout << "Case #" << z-t << ": " << "\n";
work();
}
return 0;
}
| #include <bits/stdc++.h>
#include <iterator>
#include <set>
using namespace std;
// using namespace __gnu_pbds;
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(), x.end()
#define ll long long int
#define vi vector<int>
#define vii vector<pair<int, int>>
#define pii pair<int, int>
#define plii pair<pair<ll, int>, int>
#define piii pair<pii, int>
#define viii vector<pair<pii, int>>
#define vl vector<ll>
#define vll vector<pair<ll, ll>>
#define pll pair<ll, ll>
#define pli pair<ll, int>
#define mp make_pair
#define ms(x, v) memset(x, v, sizeof x)
#define pr_vec(v) \
for (int i = 0; i < v.size(); i++) \
cout << v[i] << " ";
#define f_in(st) freopen(st, "r", stdin)
#define f_out(st) freopen(st, "w", stdout)
#define INF (ll)1e17
#define matrix vector<vector<ll>>
#define fr(i, a, b) for (i = a; i <= b; i++)
#define fb(i, a, b) for (i = a; i >= b; i--)
#define ASST(x, l, r) assert(x <= r && x >= l)
#define maxbits 9
#define maxmask (1ll << maxbits)
// typedef tree<pll, null_type, less<pll>, rb_tree_tag,
// tree_order_statistics_node_update>
// new_data_set;
const int N = (ll)2e3 + 10, M = (ll)1e9 + 1;
const int mod = (ll)1e9 + 7, MAX = (ll)1e6 + 10;
string fir = "Monocarp", sec = "Bicarp";
void work() {
ll n, k;
cin >> n >> k;
vl a(n);
for (ll i = 0; i < n; i++)
cin >> a[i];
vl pref(n, 0);
for (ll i = 1; i < n; i++)
pref[i] = a[i - 1] <= a[i];
for (ll i = 1; i < n; i++)
pref[i] += pref[i - 1];
vl mini(n, 0), maxi(n, 0);
set<ll> s;
for (ll i = 0; i < k - 1; i++) {
s.insert(a[i]);
// s_maxi.insert(-a[i]);
}
for (ll i = k - 1; i < n; i++) {
if (i >= k)
s.erase(a[i - k]);
s.insert(a[i]);
mini[i - k + 1] = (a[i - k + 1] == *(s.begin()));
maxi[i] = (a[i] == *(s.rbegin()));
}
for (ll i = 0; i < n; i++) {
// cout << pref[i] << " ";
}
vl issorted(n, 0);
ll anysorted = 0;
for (ll i = 0; i < n; i++) {
if (i - k + 1 >= 0)
issorted[i] = (pref[i] - pref[i - k + 1] == k - 1);
anysorted |= issorted[i];
}
ll ans = anysorted;
if (issorted[k - 1] == 0)
ans++;
// cout << anysorted << "\n";
for (ll i = k; i < n; i++) {
if (issorted[i])
continue;
if (mini[i - k] && maxi[i])
continue;
// cout << i << " ";
ans++;
}
cout << ans << "\n";
return;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
// to avoid printing in Scientific Notation like 2.14748e+09 and instead
// print: 2147483648.000000
cout << fixed;
// precomp();
ll t, z;
// cin >> t;
t = 1;
// z=t;
while (t--) {
// cout << "Case #" << z-t << ": " << "\n";
work();
}
return 0;
}
| delete | 114 | 119 | 114 | 114 | -11 | |
p02904 | C++ | Runtime Error | #include <bits/stdc++.h>
#ifdef __LOCAL
#define DBG(X) cout << #X << " = " << (X) << endl;
#define SAY(X) cout << (X) << endl;
#else
#define DBG(X)
#define SAY(X)
#endif
#ifdef __LOCAL
#include <filesystem>
namespace fs = std::filesystem;
#endif
using namespace std;
using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
int dx[] = {1, 0, -1, 0, 0};
int dy[] = {0, 1, 0, -1, 0};
const int INT_INF = (int)(2e9);
const ll LL_INF = (ll)(2e18);
inline void fast_io() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
};
template <typename T, typename S>
inline ostream &operator<<(ostream &os, const pair<T, S> p) {
cout << "[" << p.first << ";" << p.second << "]";
return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const vector<T> &v) {
for (auto el : v)
cout << el << " ";
return os;
}
template <typename T> inline vector<T> fetch_vec(int sz) {
vector<T> ret(sz);
for (auto &elem : ret)
cin >> elem;
return ret;
}
template <typename Monoid> struct SegmentTree {
using F = function<Monoid(Monoid, Monoid)>;
int sz;
vector<Monoid> seg;
const F f;
const Monoid M1;
SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
sz = 1;
while (sz < n)
sz <<= 1;
seg.assign(2 * sz, M1);
}
void set(int k, const Monoid &x) { seg[k + sz] = x; }
void build() {
for (int k = sz - 1; k > 0; k--) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
void update(int k, const Monoid &x) {
k += sz;
seg[k] = x;
while (k >>= 1) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
Monoid query(int a, int b) {
Monoid L = M1, R = M1;
for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if (a & 1)
L = f(L, seg[a++]);
if (b & 1)
R = f(seg[--b], R);
}
return f(L, R);
}
Monoid operator[](const int &k) const { return seg[k + sz]; }
template <typename C>
int find_subtree(int a, const C &check, Monoid &M, bool type) {
while (a < sz) {
Monoid nxt = type ? f(seg[2 * a + type], M) : f(M, seg[2 * a + type]);
if (check(nxt))
a = 2 * a + type;
else
M = nxt, a = 2 * a + 1 - type;
}
return a - sz;
}
template <typename C> int find_first(int a, const C &check) {
Monoid L = M1;
if (a <= 0) {
if (check(f(L, seg[1])))
return find_subtree(1, check, L, false);
return -1;
}
int b = sz;
for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if (a & 1) {
Monoid nxt = f(L, seg[a]);
if (check(nxt))
return find_subtree(a, check, L, false);
L = nxt;
++a;
}
}
return -1;
}
template <typename C> int find_last(int b, const C &check) {
Monoid R = M1;
if (b >= sz) {
if (check(f(seg[1], R)))
return find_subtree(1, check, R, true);
return -1;
}
int a = sz;
for (b += sz; a < b; a >>= 1, b >>= 1) {
if (b & 1) {
Monoid nxt = f(seg[--b], R);
if (check(nxt))
return find_subtree(b, check, R, true);
R = nxt;
}
}
return -1;
}
};
int N, K;
vector<int> P;
void input() {
fast_io();
#ifdef __LOCAL
fs::path p = __FILE__;
fs::path input, output;
input = output = p.parent_path();
input += string("/input/") + string(p.stem()) + string(".txt");
output += string("/output/") + string(p.stem()) + string(".txt");
freopen(input.c_str(), "r", stdin);
freopen(output.c_str(), "w", stdout);
#endif
cin >> N >> K;
P = fetch_vec<int>(N);
}
int TLEsolve() {
if (K == 1) {
cout << 1 << endl;
return 0;
}
if (K == N) {
cout << 1 << endl;
return 0;
}
// 計算量を考慮せずに愚直にやる場合・・・
map<vector<int>, int> cnt;
vector<int> A;
for (int i = 0; i <= N - K; i++) {
A = P;
sort(A.begin() + i, A.begin() + i + K);
cnt[A]++;
A.clear();
}
int ans;
ans = cnt.size();
cout << ans << endl;
return ans;
}
map<int, vector<int>> G;
vector<bool> visited;
int dfs(int i) {
// if(visited[i]) return 0;
visited[i] = true;
for (auto to : G[i]) {
if (visited[to])
continue;
dfs(to);
}
return 0;
}
// RMQ だとO(NlogN)かかる
// スライド最小値だとO(N)
int RMQsolve() {
if (K == 1) {
cout << 1 << endl;
return 0;
}
if (K == N) {
cout << 1 << endl;
return 0;
}
// deque<int> A;
// しゃくとり法的にやる。
// a1...a'i...a'i+k-1...an-1
// a1...a'i+1...a'i+k...an-1
// が同じ列 <==>
// ai = min(ai,...,ai+k-1) and
// ai+k > max(ai,...,ai+k-1)
// セグ木使えばdequeは要らない。
// for (int i = 0; i < K; i++)
// {
// A.push_back(P[i]);
// }
[&]() {
vector<pii> ZL;
vector<int> sortedIdx;
int r = 0;
for (int i = 0; i < N - 1; i++) {
if (P[i] < P[i + 1])
r++;
// else if(i==N-1) {r++;ZL.push_back(r);r=0;}
else {
r++;
if (r >= K) {
ZL.push_back({i, r});
}
r = 0;
}
}
r++;
if (r >= K)
ZL.push_back({N - 1, r});
DBG(ZL)
for (auto z : ZL)
while (z.second >= K) {
sortedIdx.push_back(z.first - z.second + 1);
z.second--;
}
ZL.clear();
DBG(sortedIdx)
for (auto i : sortedIdx)
G[i] = sortedIdx;
sortedIdx.clear();
}();
[&]() {
SegmentTree<int> segMin(
N, [](int a, int b) { return min(a, b); }, N + 10);
SegmentTree<int> segMax(
N, [](int a, int b) { return max(a, b); }, 0);
for (int i = 0; i < N; i++) {
segMin.update(i, P[i]);
segMax.update(i, P[i]);
}
// max,minのクエリはセグ木を構築して高速化する。
int mi = segMin.query(0, K);
int ma = segMax.query(0, K);
for (int i = 0; i < N - K; i++) {
int out = P[i];
int in = P[i + K];
if (mi == out && ma < in) {
G[i].push_back(i + 1);
G[i + 1].push_back(i);
ma = in;
mi = segMin.query(i + 1, i + K + 1);
}
// else if(mi!=out && ma<in) {ma=in;}
// else if(mi==out && ma>=in){mi=segMin.query(i+1,i+K+1);}
// else if(mi!=out && ma>=in &&
// ma==out){mi=min(mi,in);ma=segMax.query(i+1,i+K+1);} else if(mi!=out &&
// ma>=in && ma!=out){mi=min(mi,in);}
else {
ma = segMax.query(i + 1, i + K + 1);
mi = segMin.query(i + 1, i + K + 1);
}
// seg.query(a,b) : [a,b)に対する操作なのでbは対象外であることに注意
}
}();
int ans = 0;
visited.assign(N, false);
for (int i = 0; i <= N - K; i++) {
if (visited[i])
continue;
dfs(i);
ans++;
}
cout << ans << endl;
return ans;
}
int solve() {
if (K == 1) {
cout << 1 << endl;
return 0;
}
if (K == N) {
cout << 1 << endl;
return 0;
}
// deque<int> A;
// しゃくとり法的にやる。
// a1...a'i...a'i+k-1...an-1
// a1...a'i+1...a'i+k...an-1
// が同じ列 <==>
// ai = min(ai,...,ai+k-1) and
// ai+k > max(ai,...,ai+k-1)
// セグ木使えばdequeは要らない。
// for (int i = 0; i < K; i++)
// {
// A.push_back(P[i]);
// }
[&]() {
vector<pii> ZL;
vector<int> sortedIdx;
int r = 0;
for (int i = 0; i < N - 1; i++) {
if (P[i] < P[i + 1])
r++;
// else if(i==N-1) {r++;ZL.push_back(r);r=0;}
else {
r++;
if (r >= K) {
ZL.push_back({i, r});
}
r = 0;
}
}
r++;
if (r >= K)
ZL.push_back({N - 1, r});
DBG(ZL)
for (auto z : ZL)
while (z.second >= K) {
sortedIdx.push_back(z.first - z.second + 1);
z.second--;
}
ZL.clear();
DBG(sortedIdx)
int j = sortedIdx[0];
for (auto &i : sortedIdx) {
G[i].push_back(j);
G[j].push_back(i);
}
sortedIdx.clear();
}();
// スライド最小値
deque<int> deq;
vector<int> slideMin(N);
for (int i = 0; i < N; i++) {
while (!deq.empty() && P[deq.back()] >= P[i])
deq.pop_back();
deq.push_back(i);
if (i - K + 1 >= 0) {
slideMin[i - K + 1] = P[deq.front()];
if (deq.front() == i - K + 1)
deq.pop_front();
}
}
DBG(slideMin)
deq.clear();
vector<int> slideMax(N);
for (int i = 0; i < N; i++) {
while (!deq.empty() && P[deq.back()] <= P[i])
deq.pop_back();
deq.push_back(i);
if (i - K + 1 >= 0) {
slideMax[i - K + 1] = P[deq.front()];
if (deq.front() == i - K + 1)
deq.pop_front();
}
}
DBG(slideMax)
deq.clear();
int mi = slideMin[0];
int ma = slideMax[0];
int in, out;
for (int i = 0; i < N - K; i++) {
out = P[i];
in = P[i + K];
if (mi == out && ma < in) {
G[i].push_back(i + 1);
G[i + 1].push_back(i);
}
ma = slideMax[i + 1];
mi = slideMin[i + 1];
}
int ans = 0;
visited.assign(N, false);
for (int i = 0; i <= N - K; i++) {
if (visited[i])
continue;
dfs(i);
ans++;
}
cout << ans << endl;
return ans;
}
int main() {
input();
solve();
return 0;
}
| #include <bits/stdc++.h>
#ifdef __LOCAL
#define DBG(X) cout << #X << " = " << (X) << endl;
#define SAY(X) cout << (X) << endl;
#else
#define DBG(X)
#define SAY(X)
#endif
#ifdef __LOCAL
#include <filesystem>
namespace fs = std::filesystem;
#endif
using namespace std;
using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
int dx[] = {1, 0, -1, 0, 0};
int dy[] = {0, 1, 0, -1, 0};
const int INT_INF = (int)(2e9);
const ll LL_INF = (ll)(2e18);
inline void fast_io() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
};
template <typename T, typename S>
inline ostream &operator<<(ostream &os, const pair<T, S> p) {
cout << "[" << p.first << ";" << p.second << "]";
return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const vector<T> &v) {
for (auto el : v)
cout << el << " ";
return os;
}
template <typename T> inline vector<T> fetch_vec(int sz) {
vector<T> ret(sz);
for (auto &elem : ret)
cin >> elem;
return ret;
}
template <typename Monoid> struct SegmentTree {
using F = function<Monoid(Monoid, Monoid)>;
int sz;
vector<Monoid> seg;
const F f;
const Monoid M1;
SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
sz = 1;
while (sz < n)
sz <<= 1;
seg.assign(2 * sz, M1);
}
void set(int k, const Monoid &x) { seg[k + sz] = x; }
void build() {
for (int k = sz - 1; k > 0; k--) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
void update(int k, const Monoid &x) {
k += sz;
seg[k] = x;
while (k >>= 1) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
Monoid query(int a, int b) {
Monoid L = M1, R = M1;
for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if (a & 1)
L = f(L, seg[a++]);
if (b & 1)
R = f(seg[--b], R);
}
return f(L, R);
}
Monoid operator[](const int &k) const { return seg[k + sz]; }
template <typename C>
int find_subtree(int a, const C &check, Monoid &M, bool type) {
while (a < sz) {
Monoid nxt = type ? f(seg[2 * a + type], M) : f(M, seg[2 * a + type]);
if (check(nxt))
a = 2 * a + type;
else
M = nxt, a = 2 * a + 1 - type;
}
return a - sz;
}
template <typename C> int find_first(int a, const C &check) {
Monoid L = M1;
if (a <= 0) {
if (check(f(L, seg[1])))
return find_subtree(1, check, L, false);
return -1;
}
int b = sz;
for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if (a & 1) {
Monoid nxt = f(L, seg[a]);
if (check(nxt))
return find_subtree(a, check, L, false);
L = nxt;
++a;
}
}
return -1;
}
template <typename C> int find_last(int b, const C &check) {
Monoid R = M1;
if (b >= sz) {
if (check(f(seg[1], R)))
return find_subtree(1, check, R, true);
return -1;
}
int a = sz;
for (b += sz; a < b; a >>= 1, b >>= 1) {
if (b & 1) {
Monoid nxt = f(seg[--b], R);
if (check(nxt))
return find_subtree(b, check, R, true);
R = nxt;
}
}
return -1;
}
};
int N, K;
vector<int> P;
void input() {
fast_io();
#ifdef __LOCAL
fs::path p = __FILE__;
fs::path input, output;
input = output = p.parent_path();
input += string("/input/") + string(p.stem()) + string(".txt");
output += string("/output/") + string(p.stem()) + string(".txt");
freopen(input.c_str(), "r", stdin);
freopen(output.c_str(), "w", stdout);
#endif
cin >> N >> K;
P = fetch_vec<int>(N);
}
int TLEsolve() {
if (K == 1) {
cout << 1 << endl;
return 0;
}
if (K == N) {
cout << 1 << endl;
return 0;
}
// 計算量を考慮せずに愚直にやる場合・・・
map<vector<int>, int> cnt;
vector<int> A;
for (int i = 0; i <= N - K; i++) {
A = P;
sort(A.begin() + i, A.begin() + i + K);
cnt[A]++;
A.clear();
}
int ans;
ans = cnt.size();
cout << ans << endl;
return ans;
}
map<int, vector<int>> G;
vector<bool> visited;
int dfs(int i) {
// if(visited[i]) return 0;
visited[i] = true;
for (auto to : G[i]) {
if (visited[to])
continue;
dfs(to);
}
return 0;
}
// RMQ だとO(NlogN)かかる
// スライド最小値だとO(N)
int RMQsolve() {
if (K == 1) {
cout << 1 << endl;
return 0;
}
if (K == N) {
cout << 1 << endl;
return 0;
}
// deque<int> A;
// しゃくとり法的にやる。
// a1...a'i...a'i+k-1...an-1
// a1...a'i+1...a'i+k...an-1
// が同じ列 <==>
// ai = min(ai,...,ai+k-1) and
// ai+k > max(ai,...,ai+k-1)
// セグ木使えばdequeは要らない。
// for (int i = 0; i < K; i++)
// {
// A.push_back(P[i]);
// }
[&]() {
vector<pii> ZL;
vector<int> sortedIdx;
int r = 0;
for (int i = 0; i < N - 1; i++) {
if (P[i] < P[i + 1])
r++;
// else if(i==N-1) {r++;ZL.push_back(r);r=0;}
else {
r++;
if (r >= K) {
ZL.push_back({i, r});
}
r = 0;
}
}
r++;
if (r >= K)
ZL.push_back({N - 1, r});
DBG(ZL)
for (auto z : ZL)
while (z.second >= K) {
sortedIdx.push_back(z.first - z.second + 1);
z.second--;
}
ZL.clear();
DBG(sortedIdx)
for (auto i : sortedIdx)
G[i] = sortedIdx;
sortedIdx.clear();
}();
[&]() {
SegmentTree<int> segMin(
N, [](int a, int b) { return min(a, b); }, N + 10);
SegmentTree<int> segMax(
N, [](int a, int b) { return max(a, b); }, 0);
for (int i = 0; i < N; i++) {
segMin.update(i, P[i]);
segMax.update(i, P[i]);
}
// max,minのクエリはセグ木を構築して高速化する。
int mi = segMin.query(0, K);
int ma = segMax.query(0, K);
for (int i = 0; i < N - K; i++) {
int out = P[i];
int in = P[i + K];
if (mi == out && ma < in) {
G[i].push_back(i + 1);
G[i + 1].push_back(i);
ma = in;
mi = segMin.query(i + 1, i + K + 1);
}
// else if(mi!=out && ma<in) {ma=in;}
// else if(mi==out && ma>=in){mi=segMin.query(i+1,i+K+1);}
// else if(mi!=out && ma>=in &&
// ma==out){mi=min(mi,in);ma=segMax.query(i+1,i+K+1);} else if(mi!=out &&
// ma>=in && ma!=out){mi=min(mi,in);}
else {
ma = segMax.query(i + 1, i + K + 1);
mi = segMin.query(i + 1, i + K + 1);
}
// seg.query(a,b) : [a,b)に対する操作なのでbは対象外であることに注意
}
}();
int ans = 0;
visited.assign(N, false);
for (int i = 0; i <= N - K; i++) {
if (visited[i])
continue;
dfs(i);
ans++;
}
cout << ans << endl;
return ans;
}
int solve() {
if (K == 1) {
cout << 1 << endl;
return 0;
}
if (K == N) {
cout << 1 << endl;
return 0;
}
// deque<int> A;
// しゃくとり法的にやる。
// a1...a'i...a'i+k-1...an-1
// a1...a'i+1...a'i+k...an-1
// が同じ列 <==>
// ai = min(ai,...,ai+k-1) and
// ai+k > max(ai,...,ai+k-1)
// セグ木使えばdequeは要らない。
// for (int i = 0; i < K; i++)
// {
// A.push_back(P[i]);
// }
[&]() {
vector<pii> ZL;
vector<int> sortedIdx;
int r = 0;
for (int i = 0; i < N - 1; i++) {
if (P[i] < P[i + 1])
r++;
// else if(i==N-1) {r++;ZL.push_back(r);r=0;}
else {
r++;
if (r >= K) {
ZL.push_back({i, r});
}
r = 0;
}
}
r++;
if (r >= K)
ZL.push_back({N - 1, r});
DBG(ZL)
for (auto z : ZL)
while (z.second >= K) {
sortedIdx.push_back(z.first - z.second + 1);
z.second--;
}
ZL.clear();
DBG(sortedIdx)
if (sortedIdx.size() > 0) {
int j = sortedIdx[0];
for (auto &i : sortedIdx) {
G[i].push_back(j);
G[j].push_back(i);
}
sortedIdx.clear();
}
}();
// スライド最小値
deque<int> deq;
vector<int> slideMin(N);
for (int i = 0; i < N; i++) {
while (!deq.empty() && P[deq.back()] >= P[i])
deq.pop_back();
deq.push_back(i);
if (i - K + 1 >= 0) {
slideMin[i - K + 1] = P[deq.front()];
if (deq.front() == i - K + 1)
deq.pop_front();
}
}
DBG(slideMin)
deq.clear();
vector<int> slideMax(N);
for (int i = 0; i < N; i++) {
while (!deq.empty() && P[deq.back()] <= P[i])
deq.pop_back();
deq.push_back(i);
if (i - K + 1 >= 0) {
slideMax[i - K + 1] = P[deq.front()];
if (deq.front() == i - K + 1)
deq.pop_front();
}
}
DBG(slideMax)
deq.clear();
int mi = slideMin[0];
int ma = slideMax[0];
int in, out;
for (int i = 0; i < N - K; i++) {
out = P[i];
in = P[i + K];
if (mi == out && ma < in) {
G[i].push_back(i + 1);
G[i + 1].push_back(i);
}
ma = slideMax[i + 1];
mi = slideMin[i + 1];
}
int ans = 0;
visited.assign(N, false);
for (int i = 0; i <= N - K; i++) {
if (visited[i])
continue;
dfs(i);
ans++;
}
cout << ans << endl;
return ans;
}
int main() {
input();
solve();
return 0;
}
| replace | 358 | 364 | 358 | 366 | -11 | |
p02904 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <cfloat>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using std::cerr;
using std::cin;
using std::cout;
using std::endl;
int main() {
cout << std::fixed << std::setprecision(16);
cin.tie(nullptr);
std::ios::sync_with_stdio(false);
int n, k;
cin >> n >> k;
std::vector<int> p(n);
for (int i = 0; i < n; i++) {
cin >> p[i];
}
std::set<int> range_value;
std::unordered_multiset<int> pos;
for (int i = 0; i < k; i++) {
range_value.insert(p[i]);
}
int result = 1;
pos.insert(k - 1);
for (int i = k; i < n; i++) {
int curr_min = *range_value.begin();
int curr_max = *(--range_value.end());
int next_value = p[i];
range_value.insert(next_value);
range_value.erase(p[i - k]);
if (curr_min < *range_value.begin() && curr_max < *(--range_value.end())) {
// nothing
} else {
pos.insert(i);
result++;
}
}
int increasing_pos_count = 0;
int max = -1;
int increasing_length = 0;
for (int i = 0; i < n; i++) {
if (max < p[i]) {
increasing_length++;
max = p[i];
if (pos.find(i) != pos.end() && increasing_length >= k) {
increasing_pos_count++;
}
} else {
increasing_length = 1;
max = p[i];
}
}
result -= (increasing_pos_count * (increasing_pos_count - 1)) / 2;
assert(result >= 1);
cout << result << endl;
return 0;
} | #include <algorithm>
#include <cassert>
#include <cfloat>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using std::cerr;
using std::cin;
using std::cout;
using std::endl;
int main() {
cout << std::fixed << std::setprecision(16);
cin.tie(nullptr);
std::ios::sync_with_stdio(false);
int n, k;
cin >> n >> k;
std::vector<int> p(n);
for (int i = 0; i < n; i++) {
cin >> p[i];
}
std::set<int> range_value;
std::unordered_multiset<int> pos;
for (int i = 0; i < k; i++) {
range_value.insert(p[i]);
}
int result = 1;
pos.insert(k - 1);
for (int i = k; i < n; i++) {
int curr_min = *range_value.begin();
int curr_max = *(--range_value.end());
int next_value = p[i];
range_value.insert(next_value);
range_value.erase(p[i - k]);
if (curr_min < *range_value.begin() && curr_max < *(--range_value.end())) {
// nothing
} else {
pos.insert(i);
result++;
}
}
int increasing_pos_count = 0;
int max = -1;
int increasing_length = 0;
for (int i = 0; i < n; i++) {
if (max < p[i]) {
increasing_length++;
max = p[i];
if (pos.find(i) != pos.end() && increasing_length >= k) {
increasing_pos_count++;
}
} else {
increasing_length = 1;
max = p[i];
}
}
if (increasing_pos_count >= 2) {
result -= (increasing_pos_count);
result++;
}
assert(result >= 1);
cout << result << endl;
return 0;
} | replace | 71 | 72 | 71 | 75 | 0 | |
p02904 | C++ | Runtime Error | #include <bits/stdc++.h>
#define pb push_back
#define int long long
#define mp make_pair
#define inf 998244353
#define ll long long
using namespace std;
ll leng[114514], used[114514], mi[114514], ma[114514];
signed main() {
int n, k;
cin >> n >> k;
int ans = n - k + 1;
vector<int> a;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
a.pb(x);
}
set<int> s;
for (int i = 0; i < k; i++) {
s.insert(a[i]);
}
leng[n - 1] = 1;
for (int i = n - 2; i >= 0; i--) {
if (a[i] < a[i + 1])
leng[i] = leng[i + 1] + 1;
else
leng[i] = 1;
}
for (int i = 1; i <= n + 1 - k; i++) {
s.erase(a[i - 1]);
s.insert(a[i + k - 2]);
set<int>::iterator it = s.begin();
mi[i] = *it;
it = s.end();
it--;
ma[i] = *it;
}
for (int i = 0; i < n - k; i++) {
int cmi = mi[i + 1];
int cma = ma[i + 1];
// cout<<a[i]<<" "<<cmi<<" "<<cma<<" "<<a[i+k]<<endl;
if (a[i] < cmi && cma < a[i + k]) {
ans--;
used[i] = 1;
}
}
int cnt = 0;
for (int i = 0; i < n; i++) {
// cout<<leng[i]<<endl;
if (leng[i] >= k && used[i] == 0)
cnt++;
}
if (cnt >= 2)
ans -= cnt - 1;
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
#define pb push_back
#define int long long
#define mp make_pair
#define inf 998244353
#define ll long long
using namespace std;
ll leng[214514], used[214514], mi[214514], ma[214514];
signed main() {
int n, k;
cin >> n >> k;
int ans = n - k + 1;
vector<int> a;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
a.pb(x);
}
set<int> s;
for (int i = 0; i < k; i++) {
s.insert(a[i]);
}
leng[n - 1] = 1;
for (int i = n - 2; i >= 0; i--) {
if (a[i] < a[i + 1])
leng[i] = leng[i + 1] + 1;
else
leng[i] = 1;
}
for (int i = 1; i <= n + 1 - k; i++) {
s.erase(a[i - 1]);
s.insert(a[i + k - 2]);
set<int>::iterator it = s.begin();
mi[i] = *it;
it = s.end();
it--;
ma[i] = *it;
}
for (int i = 0; i < n - k; i++) {
int cmi = mi[i + 1];
int cma = ma[i + 1];
// cout<<a[i]<<" "<<cmi<<" "<<cma<<" "<<a[i+k]<<endl;
if (a[i] < cmi && cma < a[i + k]) {
ans--;
used[i] = 1;
}
}
int cnt = 0;
for (int i = 0; i < n; i++) {
// cout<<leng[i]<<endl;
if (leng[i] >= k && used[i] == 0)
cnt++;
}
if (cnt >= 2)
ans -= cnt - 1;
cout << ans << endl;
return 0;
} | replace | 7 | 8 | 7 | 8 | 0 | |
p02904 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <set>
#include <stdio.h>
#include <string.h>
#define maxn 100005
using namespace std;
int n, k;
int p[maxn];
set<int> s1, s2;
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++)
scanf("%d", &p[i]);
p[0] = -1;
int cnt = 0, flag = 1;
for (int i = 1; i <= k; i++) {
s1.insert(p[i]);
s2.insert(-p[i]);
if (p[i] > p[i - 1])
cnt++;
else
cnt = 1;
}
if (cnt == k)
flag = 0;
int ans = 1;
for (int i = k + 1; i <= n; i++) {
if (p[i] > p[i - 1])
cnt++;
else
cnt = 1;
s1.erase(p[i - k]);
s2.erase(-p[i - k]);
int minn = (*s1.begin());
int maxx = (-(*s2.begin()));
if (maxx < p[i] && minn > p[i - k])
1;
else {
if (cnt >= k) {
if (flag == 1) {
flag = 0;
ans++;
} else {
flag = 0;
}
} else
ans++;
}
s1.insert(p[i]);
s2.insert(-p[i]);
}
printf("%d\n", ans);
} | #include <algorithm>
#include <iostream>
#include <set>
#include <stdio.h>
#include <string.h>
#define maxn 200005
using namespace std;
int n, k;
int p[maxn];
set<int> s1, s2;
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++)
scanf("%d", &p[i]);
p[0] = -1;
int cnt = 0, flag = 1;
for (int i = 1; i <= k; i++) {
s1.insert(p[i]);
s2.insert(-p[i]);
if (p[i] > p[i - 1])
cnt++;
else
cnt = 1;
}
if (cnt == k)
flag = 0;
int ans = 1;
for (int i = k + 1; i <= n; i++) {
if (p[i] > p[i - 1])
cnt++;
else
cnt = 1;
s1.erase(p[i - k]);
s2.erase(-p[i - k]);
int minn = (*s1.begin());
int maxx = (-(*s2.begin()));
if (maxx < p[i] && minn > p[i - k])
1;
else {
if (cnt >= k) {
if (flag == 1) {
flag = 0;
ans++;
} else {
flag = 0;
}
} else
ans++;
}
s1.insert(p[i]);
s2.insert(-p[i]);
}
printf("%d\n", ans);
} | replace | 5 | 6 | 5 | 6 | 0 | |
p02904 | C++ | Runtime Error | #include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
#define INF ((int)1e9)
#define INFLL ((ll)1e18)
#define MOD (1000000007LL)
int main(int argc, const char *argv[]) {
ios_base::sync_with_stdio(false);
int N, K;
cin >> N >> K;
vector<int> P(N);
for (int n = 0; n < N; ++n)
cin >> P[n];
if (N == K) {
cout << 1 << endl;
return 0;
}
vector<int> vnMin(N - K), vnMax(N - K);
deque<int> deqMin, deqMax;
for (int n = 0; n < N; ++n) {
while (!deqMin.empty()) {
if (P[deqMin.back()] > P[n]) {
deqMin.pop_back();
} else {
break;
}
}
deqMin.push_back(n);
if (n >= K) {
vnMin[n - K] = P[deqMin[0]];
if (deqMin.front() == n - K) {
deqMin.pop_front();
}
}
while (!deqMax.empty()) {
if (P[deqMax.back()] < P[n]) {
deqMax.pop_back();
} else {
break;
}
}
deqMax.push_back(n);
if (n >= K) {
vnMax[n - K] = P[deqMax[0]];
if (deqMax.front() == n - K) {
deqMax.pop_front();
}
}
}
int nCount = 0;
int nOrigInc = 0;
vector<int> vnOrigInc(N - K, 0);
for (int n = 0; n < N - 1; ++n) {
if (P[n] < P[n + 1]) {
++nCount;
}
if (n >= K - 1 && P[n - K + 1] < P[n - K + 2]) {
--nCount;
}
if (nCount == K - 1) {
++nOrigInc;
vnOrigInc[n - K + 2] = 1;
}
}
int nRes = N - K + 1;
for (int n = 0; n + K < N; ++n) {
if (!vnOrigInc[n] && P[n] == vnMin[n] && P[n + K] == vnMax[n]) {
--nRes;
}
}
if (nOrigInc > 1) {
nRes -= (nOrigInc - 1);
}
cout << nRes << endl;
return 0;
}
| #include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
#define INF ((int)1e9)
#define INFLL ((ll)1e18)
#define MOD (1000000007LL)
int main(int argc, const char *argv[]) {
ios_base::sync_with_stdio(false);
int N, K;
cin >> N >> K;
vector<int> P(N);
for (int n = 0; n < N; ++n)
cin >> P[n];
if (N == K) {
cout << 1 << endl;
return 0;
}
vector<int> vnMin(N - K), vnMax(N - K);
deque<int> deqMin, deqMax;
for (int n = 0; n < N; ++n) {
while (!deqMin.empty()) {
if (P[deqMin.back()] > P[n]) {
deqMin.pop_back();
} else {
break;
}
}
deqMin.push_back(n);
if (n >= K) {
vnMin[n - K] = P[deqMin[0]];
if (deqMin.front() == n - K) {
deqMin.pop_front();
}
}
while (!deqMax.empty()) {
if (P[deqMax.back()] < P[n]) {
deqMax.pop_back();
} else {
break;
}
}
deqMax.push_back(n);
if (n >= K) {
vnMax[n - K] = P[deqMax[0]];
if (deqMax.front() == n - K) {
deqMax.pop_front();
}
}
}
int nCount = 0;
int nOrigInc = 0;
vector<int> vnOrigInc(N - K + 1, 0);
for (int n = 0; n < N - 1; ++n) {
if (P[n] < P[n + 1]) {
++nCount;
}
if (n >= K - 1 && P[n - K + 1] < P[n - K + 2]) {
--nCount;
}
if (nCount == K - 1) {
++nOrigInc;
vnOrigInc[n - K + 2] = 1;
}
}
int nRes = N - K + 1;
for (int n = 0; n + K < N; ++n) {
if (!vnOrigInc[n] && P[n] == vnMin[n] && P[n + K] == vnMax[n]) {
--nRes;
}
}
if (nOrigInc > 1) {
nRes -= (nOrigInc - 1);
}
cout << nRes << endl;
return 0;
}
| replace | 72 | 73 | 72 | 73 | 0 | |
p02904 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int p[maxn];
set<int> st;
int main() {
int n, k;
cin >> n >> k;
for (int i = 0; i < n; i++)
cin >> p[i];
int inc = 1;
int tmp = 0;
for (int i = 0; i < k; i++) {
st.insert(p[i]);
if (i > 0)
inc = p[i] > p[i - 1] ? inc + 1 : 1;
}
int ans = inc >= k ? 0 : 1;
tmp = inc >= k ? 1 : tmp;
for (int i = k; i < n; i++) {
inc = p[i] > p[i - 1] ? inc + 1 : 1;
tmp = inc >= k ? 1 : tmp;
int j = i - k;
st.erase(p[j]);
if (p[i] > *st.rbegin() && p[j] < *st.begin()) {
} else
ans += inc >= k ? 0 : 1;
st.insert(p[i]);
}
cout << ans + tmp << endl;
} | #include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int p[maxn];
set<int> st;
int main() {
int n, k;
cin >> n >> k;
for (int i = 0; i < n; i++)
cin >> p[i];
int inc = 1;
int tmp = 0;
for (int i = 0; i < k; i++) {
st.insert(p[i]);
if (i > 0)
inc = p[i] > p[i - 1] ? inc + 1 : 1;
}
int ans = inc >= k ? 0 : 1;
tmp = inc >= k ? 1 : tmp;
for (int i = k; i < n; i++) {
inc = p[i] > p[i - 1] ? inc + 1 : 1;
tmp = inc >= k ? 1 : tmp;
int j = i - k;
st.erase(p[j]);
if (p[i] > *st.rbegin() && p[j] < *st.begin()) {
} else
ans += inc >= k ? 0 : 1;
st.insert(p[i]);
}
cout << ans + tmp << endl;
} | replace | 3 | 4 | 3 | 4 | 0 | |
p02904 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// #include <boost/multiprecision/cpp_int.hpp>
// typedef boost::multiprecision::cpp_int ll;
typedef long double dd;
#define i_7 (ll)(1E9 + 7)
// #define i_7 998244353
#define i_5 i_7 - 2
ll mod(ll a) {
ll c = a % i_7;
if (c >= 0)
return c;
return c + i_7;
}
typedef pair<ll, ll> l_l;
ll inf = (ll)1E17;
#define rep(i, l, r) for (ll i = l; i <= r; i++)
#define pb push_back
ll max(ll a, ll b) {
if (a < b)
return b;
else
return a;
}
ll min(ll a, ll b) {
if (a > b)
return b;
else
return a;
}
void Max(ll &pos, ll val) { pos = max(pos, val); } // Max(dp[n],dp[n-1]);
void Min(ll &pos, ll val) { pos = min(pos, val); }
void Add(ll &pos, ll val) { pos = mod(pos + val); }
dd EPS = 1E-9;
#define fastio \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
///////////////////////////////////////
// 区間最大区間加算
struct RBQRAQ {
ll n;
vector<ll> data, lazy;
RBQRAQ(ll vs) {
n = 1;
while (n < vs)
n *= 2;
data.resize(2 * n - 1, 0);
lazy.resize(2 * n - 1, 0);
}
void eval(ll k, ll l, ll r) {
if (lazy[k] != 0) {
data[k] += lazy[k];
if (r - l > 1) {
lazy[2 * k + 1] += lazy[k];
lazy[2 * k + 2] += lazy[k];
}
lazy[k] = 0;
}
}
void add(ll a, ll b, ll x, ll k, ll l, ll r) {
eval(k, l, r);
if (r <= a || b <= l)
return;
if (a <= l && r <= b) {
lazy[k] += x;
eval(k, l, r);
} else {
add(a, b, x, 2 * k + 1, l, (l + r) / 2);
add(a, b, x, 2 * k + 2, (l + r) / 2, r);
data[k] = max(data[2 * k + 1], data[2 * k + 2]);
}
}
void add(ll a, ll b, ll x) { add(a, b, x, 0, 0, n); } //[a,b)にxを加える
ll query(ll a, ll b, ll k, ll l, ll r) {
eval(k, l, r);
if (r <= a || b <= l)
return -inf;
if (a <= l && r <= b) {
return data[k];
} else {
ll vl = query(a, b, 2 * k + 1, l, (l + r) / 2);
ll vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
return max(vl, vr);
}
}
ll query(ll a, ll b) { return query(a, b, 0, 0, n); } //[a,b)の最大値
};
//////////////////////////////////////////////////////
const ll MAX_N = 1 << 20;
// セグメント木を持つグローバル配列
ll n, dat[2 * MAX_N - 1];
// 初期化
void init(ll n_) {
// 簡単のため要素数を2のべき乗に
n = 1;
while (n < n_)
n *= 2;
// 全ての値をinfに
for (ll i = 0; i < 2 * n - 1; i++)
dat[i] = inf;
}
// k番目の値(0-indexed)をaに変更
void update(ll k, ll a) {
// 葉の節点
k += n - 1;
dat[k] = a;
// 登りながら更新
while (k > 0) {
k = (k - 1) / 2;
dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
}
}
//[a,b)の最小値を求める
// 後ろの方の引数は、計算の簡単のための引数
// kは節点の番号、l,rはその節点が[l,r)に対応づいていることを表す。
ll query(ll a, ll b, ll k, ll l, ll r) {
//[a,b)と[l,r)が交差しなければinf
if (r <= a || b <= l)
return inf;
//[a,b)が[l,r)を完全に含んでいれば、この節点の値
if (a <= l && r <= b)
return dat[k];
else {
// そうでなければ、2つの子の最小値
ll vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
ll vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
return min(vl, vr);
}
}
//[a,b)の最小値
ll que(ll a, ll b) { return query(a, b, 0, 0, n); }
/////////////////////////////////////////////////////
int main() {
fastio ll n, k;
cin >> n >> k;
ll p[n];
rep(i, 0, n - 1) cin >> p[i];
RBQRAQ u(n);
init(n);
rep(i, 0, n - 1) { update(i, p[i]); }
rep(i, 0, n - 1) { u.add(i, i + 1, p[i]); }
vector<ll> v;
ll pos = n - 1;
ll cou = 1;
ll res[10];
res[n - 1] = 1;
while (pos >= 0) {
while (pos - 1 >= 0) {
if (p[pos - 1] < p[pos]) {
cou++;
pos--;
res[pos] = cou;
} else {
cou = 1;
break;
}
}
pos--;
res[pos] = cou;
cou = 1;
}
/*rep(i,0,n-1){
cout<<res[i]<<" ";
}cout<<endl;
*/
ll count = 0;
rep(i, 0, n - 1) {
if (res[i] >= k)
count++;
}
ll ans = n - k + 1 - max(0, count - 1);
rep(i, 0, n - k - 1) {
// cout<<endl<<i<<" ";
if (res[i] >= k)
continue;
ll c = u.query(i + 1, i + k);
ll a = u.query(i, i + 1);
ll b = u.query(i + k, i + k + 1);
ll d = que(i + 1, i + k);
if (a < d && c < b)
ans--;
// cout<<a<<" "<<d<<" "<<c<<" "<<b;
}
// cout<<endl;
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// #include <boost/multiprecision/cpp_int.hpp>
// typedef boost::multiprecision::cpp_int ll;
typedef long double dd;
#define i_7 (ll)(1E9 + 7)
// #define i_7 998244353
#define i_5 i_7 - 2
ll mod(ll a) {
ll c = a % i_7;
if (c >= 0)
return c;
return c + i_7;
}
typedef pair<ll, ll> l_l;
ll inf = (ll)1E17;
#define rep(i, l, r) for (ll i = l; i <= r; i++)
#define pb push_back
ll max(ll a, ll b) {
if (a < b)
return b;
else
return a;
}
ll min(ll a, ll b) {
if (a > b)
return b;
else
return a;
}
void Max(ll &pos, ll val) { pos = max(pos, val); } // Max(dp[n],dp[n-1]);
void Min(ll &pos, ll val) { pos = min(pos, val); }
void Add(ll &pos, ll val) { pos = mod(pos + val); }
dd EPS = 1E-9;
#define fastio \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
///////////////////////////////////////
// 区間最大区間加算
struct RBQRAQ {
ll n;
vector<ll> data, lazy;
RBQRAQ(ll vs) {
n = 1;
while (n < vs)
n *= 2;
data.resize(2 * n - 1, 0);
lazy.resize(2 * n - 1, 0);
}
void eval(ll k, ll l, ll r) {
if (lazy[k] != 0) {
data[k] += lazy[k];
if (r - l > 1) {
lazy[2 * k + 1] += lazy[k];
lazy[2 * k + 2] += lazy[k];
}
lazy[k] = 0;
}
}
void add(ll a, ll b, ll x, ll k, ll l, ll r) {
eval(k, l, r);
if (r <= a || b <= l)
return;
if (a <= l && r <= b) {
lazy[k] += x;
eval(k, l, r);
} else {
add(a, b, x, 2 * k + 1, l, (l + r) / 2);
add(a, b, x, 2 * k + 2, (l + r) / 2, r);
data[k] = max(data[2 * k + 1], data[2 * k + 2]);
}
}
void add(ll a, ll b, ll x) { add(a, b, x, 0, 0, n); } //[a,b)にxを加える
ll query(ll a, ll b, ll k, ll l, ll r) {
eval(k, l, r);
if (r <= a || b <= l)
return -inf;
if (a <= l && r <= b) {
return data[k];
} else {
ll vl = query(a, b, 2 * k + 1, l, (l + r) / 2);
ll vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
return max(vl, vr);
}
}
ll query(ll a, ll b) { return query(a, b, 0, 0, n); } //[a,b)の最大値
};
//////////////////////////////////////////////////////
const ll MAX_N = 1 << 20;
// セグメント木を持つグローバル配列
ll n, dat[2 * MAX_N - 1];
// 初期化
void init(ll n_) {
// 簡単のため要素数を2のべき乗に
n = 1;
while (n < n_)
n *= 2;
// 全ての値をinfに
for (ll i = 0; i < 2 * n - 1; i++)
dat[i] = inf;
}
// k番目の値(0-indexed)をaに変更
void update(ll k, ll a) {
// 葉の節点
k += n - 1;
dat[k] = a;
// 登りながら更新
while (k > 0) {
k = (k - 1) / 2;
dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
}
}
//[a,b)の最小値を求める
// 後ろの方の引数は、計算の簡単のための引数
// kは節点の番号、l,rはその節点が[l,r)に対応づいていることを表す。
ll query(ll a, ll b, ll k, ll l, ll r) {
//[a,b)と[l,r)が交差しなければinf
if (r <= a || b <= l)
return inf;
//[a,b)が[l,r)を完全に含んでいれば、この節点の値
if (a <= l && r <= b)
return dat[k];
else {
// そうでなければ、2つの子の最小値
ll vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
ll vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
return min(vl, vr);
}
}
//[a,b)の最小値
ll que(ll a, ll b) { return query(a, b, 0, 0, n); }
/////////////////////////////////////////////////////
int main() {
fastio ll n, k;
cin >> n >> k;
ll p[n];
rep(i, 0, n - 1) cin >> p[i];
RBQRAQ u(n);
init(n);
rep(i, 0, n - 1) { update(i, p[i]); }
rep(i, 0, n - 1) { u.add(i, i + 1, p[i]); }
vector<ll> v;
ll pos = n - 1;
ll cou = 1;
ll res[n];
res[n - 1] = 1;
while (pos >= 0) {
while (pos - 1 >= 0) {
if (p[pos - 1] < p[pos]) {
cou++;
pos--;
res[pos] = cou;
} else {
cou = 1;
break;
}
}
pos--;
res[pos] = cou;
cou = 1;
}
/*rep(i,0,n-1){
cout<<res[i]<<" ";
}cout<<endl;
*/
ll count = 0;
rep(i, 0, n - 1) {
if (res[i] >= k)
count++;
}
ll ans = n - k + 1 - max(0, count - 1);
rep(i, 0, n - k - 1) {
// cout<<endl<<i<<" ";
if (res[i] >= k)
continue;
ll c = u.query(i + 1, i + k);
ll a = u.query(i, i + 1);
ll b = u.query(i + k, i + k + 1);
ll d = que(i + 1, i + k);
if (a < d && c < b)
ans--;
// cout<<a<<" "<<d<<" "<<c<<" "<<b;
}
// cout<<endl;
cout << ans << endl;
return 0;
}
| replace | 150 | 151 | 150 | 151 | 0 | |
p02904 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int solve(const vector<int> &a, int k) {
const int n = a.size();
int count = 1;
set<int> seg;
vector<int> s(n);
s[0] = 1;
for (int i = 1; i < n; ++i) {
if (a[i] > a[i - 1])
s[i] = s[i - 1] + 1;
else
s[i] = 1;
}
if (*max_element(s.begin(), s.end()) >= k && s[k - 1] < k)
++count;
for (int i = 0; i < k; ++i)
seg.insert(a[i]);
for (int i = k; i < n; ++i) {
if (a[i - k] == *seg.begin() && a[i] > *(seg.rbegin()))
;
else if (s[i] < k) {
++count;
}
seg.erase(a[i - k]);
seg.insert(a[i]);
}
return count;
}
int solve_naive(const vector<int> &a, int k) {
const int n = a.size();
set<vector<int>> s;
for (int i = k; i <= n; ++i) {
vector<int> b = a;
sort(b.begin() + i - k, b.begin() + i);
s.insert(b);
}
return s.size();
}
int main() {
// int n = 7;
// vector<int> a(n);
// iota(a.begin(), a.end(), 0);
// do {
// for (int k = 2; k <= n; ++k) {
// if (solve(a, k) != solve_naive(a, k)) {
// cout << n << " " << k << "\n";
// cout << solve(a, k) << " " << solve_naive(a, k) << "\n";
// for (int v: a)
// cout << v << " ";
// cout << "\n";
// }
// }
// } while (next_permutation(a.begin(), a.end()));
// cout << "done\n";
// return 0;
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
vector<int> a(n);
for (int i = 0; i < n; ++i)
cin >> a[i];
cout << solve_naive(a, k) << "\n";
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int solve(const vector<int> &a, int k) {
const int n = a.size();
int count = 1;
set<int> seg;
vector<int> s(n);
s[0] = 1;
for (int i = 1; i < n; ++i) {
if (a[i] > a[i - 1])
s[i] = s[i - 1] + 1;
else
s[i] = 1;
}
if (*max_element(s.begin(), s.end()) >= k && s[k - 1] < k)
++count;
for (int i = 0; i < k; ++i)
seg.insert(a[i]);
for (int i = k; i < n; ++i) {
if (a[i - k] == *seg.begin() && a[i] > *(seg.rbegin()))
;
else if (s[i] < k) {
++count;
}
seg.erase(a[i - k]);
seg.insert(a[i]);
}
return count;
}
int solve_naive(const vector<int> &a, int k) {
const int n = a.size();
set<vector<int>> s;
for (int i = k; i <= n; ++i) {
vector<int> b = a;
sort(b.begin() + i - k, b.begin() + i);
s.insert(b);
}
return s.size();
}
int main() {
// int n = 7;
// vector<int> a(n);
// iota(a.begin(), a.end(), 0);
// do {
// for (int k = 2; k <= n; ++k) {
// if (solve(a, k) != solve_naive(a, k)) {
// cout << n << " " << k << "\n";
// cout << solve(a, k) << " " << solve_naive(a, k) << "\n";
// for (int v: a)
// cout << v << " ";
// cout << "\n";
// }
// }
// } while (next_permutation(a.begin(), a.end()));
// cout << "done\n";
// return 0;
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
vector<int> a(n);
for (int i = 0; i < n; ++i)
cin >> a[i];
cout << solve(a, k) << "\n";
return 0;
}
| replace | 65 | 66 | 65 | 66 | TLE | |
p02904 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
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;
}
int main() {
int N, K;
cin >> N >> K;
vector<int> P(N);
rep(i, N) cin >> P[i];
set<int> st, unite;
rep(i, K) st.insert(P[i]);
for (int i = K; i < N; i++) {
if (P[i - K] == *(st.begin()) && P[i] > *(st.rbegin()))
unite.insert(i);
st.erase(P[i]);
st.insert(P[i + K]);
}
int cnt = 1;
bool flag = 0;
for (int i = 1; i < N; i++) {
if (P[i] > P[i - 1]) {
cnt++;
if (cnt >= K) {
flag = 1;
unite.insert(i);
}
} else
cnt = 1;
}
cout << N - K + 1 - unite.size() + flag << endl;
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
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;
}
int main() {
int N, K;
cin >> N >> K;
vector<int> P(N);
rep(i, N) cin >> P[i];
set<int> st, unite;
rep(i, K) st.insert(P[i]);
for (int i = K; i < N; i++) {
if (P[i - K] == *(st.begin()) && P[i] > *(st.rbegin()))
unite.insert(i);
st.erase(P[i - K]);
st.insert(P[i]);
}
int cnt = 1;
bool flag = 0;
for (int i = 1; i < N; i++) {
if (P[i] > P[i - 1]) {
cnt++;
if (cnt >= K) {
flag = 1;
unite.insert(i);
}
} else
cnt = 1;
}
cout << N - K + 1 - unite.size() + flag << endl;
return 0;
} | replace | 43 | 45 | 43 | 45 | 0 | |
p02904 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using i64 = long long;
const i64 MOD = 1e9 + 7;
const i64 INF = i64(1e18) + 7;
template <typename T> bool chmin(T &x, T y) {
if (x > y) {
x = y;
return true;
}
return false;
}
template <typename T> bool chmax(T &x, T y) {
if (x < y) {
x = y;
return true;
}
return false;
}
template <typename T> struct DisjointSparseTable {
function<T(T, T)> f;
vector<vector<T>> v;
DisjointSparseTable(vector<T> &inp, function<T(T, T)> f) : f(f) {
int n = inp.size();
int b;
for (b = 0; (1 << b) <= inp.size(); ++b)
;
v.assign(b, vector<T>(n));
for (int i = 0; i < n; ++i)
v[0][i] = inp[i];
for (int i = 1; i < b; ++i) {
int siz = 1 << i;
for (int j = 0; j < n; j += siz << 1) {
int t = min(j + siz, n);
v[i][t - 1] = inp[t - 1];
for (int k = t - 2; k >= j; --k)
v[i][k] = f(inp[k], v[i][k + 1]);
if (t >= n)
break;
v[i][t] = inp[t];
int r = min(t + siz, n);
for (int k = t + 1; k < r; ++k)
v[i][k] = f(v[i][k - 1], inp[k]);
}
}
}
T get(int l, int r) {
if (l >= --r)
return v[0][l];
int p = 31 - __builtin_clz(l ^ r);
return f(v[p][l], v[p][r]);
}
};
signed main() {
int n, k;
cin >> n >> k;
vector<int> a(n);
for (int i = 0; i < n; ++i)
cin >> a[i];
DisjointSparseTable<int> d_max(a, [](auto x, auto y) { return max(x, y); });
DisjointSparseTable<int> d_min(a, [](auto x, auto y) { return min(x, y); });
vector<int> ma(n), mi(n);
iota(ma.begin(), ma.end(), 1);
iota(mi.begin(), mi.end(), 0);
for (int i = 0; i < n - 1; ++i)
if (a[i] < a[i + 1])
mi[i + 1] = mi[i];
for (int i = n - 2; i >= 0; --i)
if (a[i] < a[i + 1])
ma[i] = ma[i + 1];
set<pair<int, int>> v;
bool fl = false;
for (int i = 0; i < n - k + 1; ++i) {
int l = i, r = i + k;
int ma_p = d_max.get(l, r);
int mi_p = d_min.get(l, r);
if (r <= ma[l]) {
fl = true;
continue;
}
auto b = a;
sort(next(b.begin(), i), next(b.begin(), i + k));
if (l && a[l - 1] < mi_p)
l = mi[l - 1];
if (r != n && ma_p < a[r])
r = ma[r];
v.emplace(l, r);
}
cout << v.size() + fl << endl;
}
| #include <bits/stdc++.h>
using namespace std;
using i64 = long long;
const i64 MOD = 1e9 + 7;
const i64 INF = i64(1e18) + 7;
template <typename T> bool chmin(T &x, T y) {
if (x > y) {
x = y;
return true;
}
return false;
}
template <typename T> bool chmax(T &x, T y) {
if (x < y) {
x = y;
return true;
}
return false;
}
template <typename T> struct DisjointSparseTable {
function<T(T, T)> f;
vector<vector<T>> v;
DisjointSparseTable(vector<T> &inp, function<T(T, T)> f) : f(f) {
int n = inp.size();
int b;
for (b = 0; (1 << b) <= inp.size(); ++b)
;
v.assign(b, vector<T>(n));
for (int i = 0; i < n; ++i)
v[0][i] = inp[i];
for (int i = 1; i < b; ++i) {
int siz = 1 << i;
for (int j = 0; j < n; j += siz << 1) {
int t = min(j + siz, n);
v[i][t - 1] = inp[t - 1];
for (int k = t - 2; k >= j; --k)
v[i][k] = f(inp[k], v[i][k + 1]);
if (t >= n)
break;
v[i][t] = inp[t];
int r = min(t + siz, n);
for (int k = t + 1; k < r; ++k)
v[i][k] = f(v[i][k - 1], inp[k]);
}
}
}
T get(int l, int r) {
if (l >= --r)
return v[0][l];
int p = 31 - __builtin_clz(l ^ r);
return f(v[p][l], v[p][r]);
}
};
signed main() {
int n, k;
cin >> n >> k;
vector<int> a(n);
for (int i = 0; i < n; ++i)
cin >> a[i];
DisjointSparseTable<int> d_max(a, [](auto x, auto y) { return max(x, y); });
DisjointSparseTable<int> d_min(a, [](auto x, auto y) { return min(x, y); });
vector<int> ma(n), mi(n);
iota(ma.begin(), ma.end(), 1);
iota(mi.begin(), mi.end(), 0);
for (int i = 0; i < n - 1; ++i)
if (a[i] < a[i + 1])
mi[i + 1] = mi[i];
for (int i = n - 2; i >= 0; --i)
if (a[i] < a[i + 1])
ma[i] = ma[i + 1];
set<pair<int, int>> v;
bool fl = false;
for (int i = 0; i < n - k + 1; ++i) {
int l = i, r = i + k;
int ma_p = d_max.get(l, r);
int mi_p = d_min.get(l, r);
if (r <= ma[l]) {
fl = true;
continue;
}
if (l && a[l - 1] < mi_p)
l = mi[l - 1];
if (r != n && ma_p < a[r])
r = ma[r];
v.emplace(l, r);
}
cout << v.size() + fl << endl;
}
| delete | 92 | 94 | 92 | 92 | TLE | |
p02905 | C++ | Time Limit Exceeded | #include <iostream>
#include <vector>
using namespace std;
using ll = long long int;
const ll MOD = 998244353;
vector<vector<int>> divs(1010101, vector<int>{});
vector<vector<int>> A_div(1010101, vector<int>{});
ll sum_AiAj[1010101];
vector<int> a(202020);
ll mod_pow(ll x, ll n) {
ll ret = 1;
while (n != 0) {
if (n & 1) {
ret = ret * x % MOD;
}
x = x * x % MOD;
n = n >> 1;
}
return ret;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 1; i < 1000001; i++) {
for (int j = 1; i * j < 1000001; j++) {
divs[i * j].push_back(i);
}
}
for (auto &v : a) {
for (auto &w : divs[v]) {
A_div[w].push_back(v);
}
}
ll inv_mod2 = mod_pow(2, MOD - 2);
for (int i = 0; i < 1000001; i++) {
ll square_sum = 0;
ll sum = 0;
for (auto &v : A_div[i]) {
square_sum += 1LL * v * v;
sum += v;
}
square_sum %= MOD;
sum %= MOD;
sum_AiAj[i] = (sum * sum - square_sum + MOD) % MOD;
sum_AiAj[i] *= inv_mod2;
sum_AiAj[i] %= MOD;
}
ll ans = 0;
for (int i = 1000000; i > 0; i--) {
ll val = sum_AiAj[i];
for (int j = 2; i * j < 1000001; j++) {
val = (val - sum_AiAj[i * j] + MOD) % MOD;
}
sum_AiAj[i] = val;
ans += val * mod_pow(i, MOD - 2);
ans %= MOD;
}
cout << (ans + MOD) % MOD << endl;
return 0;
} | #include <iostream>
#include <vector>
using namespace std;
using ll = long long int;
const ll MOD = 998244353;
vector<vector<int>> divs(1010101, vector<int>{});
vector<vector<int>> A_div(1010101, vector<int>{});
ll sum_AiAj[1010101];
vector<int> a(202020);
ll mod_pow(ll x, ll n) {
ll ret = 1;
while (n != 0) {
if (n & 1) {
ret = ret * x % MOD;
}
x = x * x % MOD;
n = n >> 1;
}
return ret;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 1; i < 1000001; i++) {
for (int j = 1; i * j < 1000001; j++) {
divs[i * j].push_back(i);
}
}
for (auto &v : a) {
for (auto &w : divs[v]) {
A_div[w].push_back(v);
}
}
ll inv_mod2 = mod_pow(2, MOD - 2);
for (int i = 0; i < 1000001; i++) {
ll square_sum = 0;
ll sum = 0;
for (auto &v : A_div[i]) {
square_sum += 1LL * v * v;
sum += v;
}
square_sum %= MOD;
sum %= MOD;
sum_AiAj[i] = (sum * sum - square_sum + MOD) % MOD;
sum_AiAj[i] *= inv_mod2;
sum_AiAj[i] %= MOD;
}
ll ans = 0;
for (int i = 1000000; i > 0; i--) {
ll val = sum_AiAj[i];
for (int j = 2; i * j < 1000001; j++) {
val = (val - sum_AiAj[i * j] + MOD) % MOD;
}
sum_AiAj[i] = val;
if (val == 0) {
continue;
}
ans += val * mod_pow(i, MOD - 2);
ans %= MOD;
}
cout << (ans + MOD) % MOD << endl;
return 0;
} | insert | 64 | 64 | 64 | 67 | TLE | |
p02905 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define m_p make_pair
#define sz(x) (int)x.size()
#define out(x) cerr << #x << " = " << x << " "
#define outln(x) cerr << #x << " = " << x << endl
#define outarr(x, l, r) \
cerr << #x "[" << l << "-" << r << "] = "; \
for (int _i = l; _i <= r; ++_i) \
cerr << x[_i] << " "; \
cerr << endl;
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
#define gc() getchar()
// char buf[1<<23],*p1=buf,*p2=buf;
// #define gc()
// (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
template <class T> void read(T &x) {
x = 0;
char c = gc();
int flag = 0;
while (c < '0' || c > '9')
flag |= (c == '-'), c = gc();
while (c >= '0' && c <= '9')
x = (x << 3) + (x << 1) + (c ^ 48), c = gc();
if (flag)
x = -x;
}
template <class T> T _max(T a, T b) { return a > b ? a : b; }
template <class T> T _min(T a, T b) { return a < b ? a : b; }
template <class T> bool checkmax(T &a, T b) { return b > a ? a = b, 1 : 0; }
template <class T> bool checkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
const int mod = 998244353;
int Add(int a, int b) {
a += b;
return a >= mod ? a - mod : a;
}
int Sub(int a, int b) {
a -= b;
return a >= 0 ? a : a + mod;
}
int Mul(int a, int b) { return 1LL * a * b % mod; }
void add(int &a, int b) { a = Add(a, b); }
void sub(int &a, int b) { a = Sub(a, b); }
void mul(int &a, int b) { a = Mul(a, b); }
int qpow(int a, int b) {
int ans = 1;
while (b) {
if (b & 1)
mul(ans, a);
a = Mul(a, a);
b >>= 1;
}
return ans;
}
const int N = 200005, M = 1000000;
int n, k, cnt[N], mx = 0;
int prime[M + 5], flag[M + 5], inv[M + 5], mu[M + 5], f[M + 5], g[M + 5];
void init() {
read(n);
for (int i = 1, x; i <= n; ++i) {
read(x);
add(cnt[x], x);
checkmax(mx, x);
}
int tot = 0;
mu[1] = 1;
for (int i = 2; i <= mx; ++i) {
if (!flag[i]) {
prime[++tot] = i;
mu[i] = mod - 1;
}
for (int j = 1; j <= tot && i * prime[j] <= mx; ++j) {
flag[i * prime[j]] = 1;
if (i % prime[j] == 0) {
mu[i * prime[j]] = 0;
break;
}
mu[i * prime[j]] = mod - mu[i];
}
}
inv[1] = 1;
for (int i = 2; i <= mx; ++i) {
inv[i] = Mul(inv[mod % i], mod - mod / i);
}
for (int i = 1; i <= mx; ++i) {
for (int j = i; j <= mx; j += i) {
add(f[i], cnt[j]);
add(g[j], Mul(inv[i], mu[j / i]));
}
}
}
void solve() {
int ans = 0;
for (int i = 1; i <= mx; ++i) {
add(ans, Mul(Mul(f[i], f[i]), g[i]));
}
sub(ans, f[1]);
mul(ans, inv[2]);
printf("%d\n", ans);
}
int main() {
init();
solve();
return 0;
}
| #include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define m_p make_pair
#define sz(x) (int)x.size()
#define out(x) cerr << #x << " = " << x << " "
#define outln(x) cerr << #x << " = " << x << endl
#define outarr(x, l, r) \
cerr << #x "[" << l << "-" << r << "] = "; \
for (int _i = l; _i <= r; ++_i) \
cerr << x[_i] << " "; \
cerr << endl;
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
#define gc() getchar()
// char buf[1<<23],*p1=buf,*p2=buf;
// #define gc()
// (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
template <class T> void read(T &x) {
x = 0;
char c = gc();
int flag = 0;
while (c < '0' || c > '9')
flag |= (c == '-'), c = gc();
while (c >= '0' && c <= '9')
x = (x << 3) + (x << 1) + (c ^ 48), c = gc();
if (flag)
x = -x;
}
template <class T> T _max(T a, T b) { return a > b ? a : b; }
template <class T> T _min(T a, T b) { return a < b ? a : b; }
template <class T> bool checkmax(T &a, T b) { return b > a ? a = b, 1 : 0; }
template <class T> bool checkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
const int mod = 998244353;
int Add(int a, int b) {
a += b;
return a >= mod ? a - mod : a;
}
int Sub(int a, int b) {
a -= b;
return a >= 0 ? a : a + mod;
}
int Mul(int a, int b) { return 1LL * a * b % mod; }
void add(int &a, int b) { a = Add(a, b); }
void sub(int &a, int b) { a = Sub(a, b); }
void mul(int &a, int b) { a = Mul(a, b); }
int qpow(int a, int b) {
int ans = 1;
while (b) {
if (b & 1)
mul(ans, a);
a = Mul(a, a);
b >>= 1;
}
return ans;
}
const int N = 200005, M = 1000000;
int n, k, cnt[M + 5], mx = 0;
int prime[M + 5], flag[M + 5], inv[M + 5], mu[M + 5], f[M + 5], g[M + 5];
void init() {
read(n);
for (int i = 1, x; i <= n; ++i) {
read(x);
add(cnt[x], x);
checkmax(mx, x);
}
int tot = 0;
mu[1] = 1;
for (int i = 2; i <= mx; ++i) {
if (!flag[i]) {
prime[++tot] = i;
mu[i] = mod - 1;
}
for (int j = 1; j <= tot && i * prime[j] <= mx; ++j) {
flag[i * prime[j]] = 1;
if (i % prime[j] == 0) {
mu[i * prime[j]] = 0;
break;
}
mu[i * prime[j]] = mod - mu[i];
}
}
inv[1] = 1;
for (int i = 2; i <= mx; ++i) {
inv[i] = Mul(inv[mod % i], mod - mod / i);
}
for (int i = 1; i <= mx; ++i) {
for (int j = i; j <= mx; j += i) {
add(f[i], cnt[j]);
add(g[j], Mul(inv[i], mu[j / i]));
}
}
}
void solve() {
int ans = 0;
for (int i = 1; i <= mx; ++i) {
add(ans, Mul(Mul(f[i], f[i]), g[i]));
}
sub(ans, f[1]);
mul(ans, inv[2]);
printf("%d\n", ans);
}
int main() {
init();
solve();
return 0;
}
| replace | 69 | 70 | 69 | 70 | 0 | |
p02905 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
#define scan(x) \
do { \
while ((x = getchar()) < '0') \
; \
for (x -= '0'; '0' <= (_ = getchar()); x = (x << 3) + (x << 1) + _ - '0') \
; \
} while (0)
char _;
#define complete_unique(a) a.erase(unique(a.begin(), a.end()), a.end())
#define all(a) a.begin(), a.end()
#define println printf("\n");
#define readln(x) getline(cin, x);
#define pb push_back
#define endl "\n"
#define INT_INF 0x3f3f3f3f
#define LL_INF 0x3f3f3f3f3f3f3f3f
#define MOD 998244353
#define MOD2 1494318097
#define SEED 131
#define mp make_pair
#define fastio \
cin.tie(0); \
cin.sync_with_stdio(0);
#define MAXN 200005
#define MAXV 1000005
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef unordered_map<int, int> umii;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef pair<int, pii> triple;
typedef int8_t byte;
mt19937 g1(time(0));
int randint(int a, int b) { return uniform_int_distribution<int>(a, b)(g1); }
ll randlong(ll a, ll b) {
return uniform_int_distribution<long long>(a, b)(g1);
}
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
ll fpow(ll b, ll exp, ll mod) {
if (exp == 0)
return 1;
ll t = fpow(b, exp / 2, mod);
if (exp & 1)
return t * t % mod * b % mod;
return t * t % mod;
}
ll divmod(ll i, ll j, ll mod) {
i %= mod, j %= mod;
return i * fpow(j, mod - 2, mod) % mod;
}
int len, arr[MAXN];
vector<int> nums[MAXV];
ll dp[MAXV], res;
int main() {
scanf("%d", &len);
for (int i = 1; i <= len; i++) {
scanf(" %d", &arr[i]);
for (int k = 1; k <= sqrt(arr[i]); k++) {
if (arr[i] % k == 0) {
nums[k].pb(arr[i]);
if (k * k != arr[i])
nums[arr[i] / k].pb(arr[i]);
}
}
}
for (int n = 1000000; n >= 1; n--) {
ll sum = 0, tot = 0;
for (int check : nums[n])
sum = (sum + check) % MOD;
for (int i = 0; i < nums[n].size(); i++) {
sum = (sum - nums[n][i] + MOD) % MOD;
tot = (tot + nums[n][i] * sum) % MOD;
}
for (int v = n + n; v <= 1000000; v += n)
tot = (tot - dp[v] + MOD) % MOD;
res = (res + divmod(tot, n, MOD)) % MOD;
dp[n] = tot;
}
printf("%lld\n", res);
} | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
#define scan(x) \
do { \
while ((x = getchar()) < '0') \
; \
for (x -= '0'; '0' <= (_ = getchar()); x = (x << 3) + (x << 1) + _ - '0') \
; \
} while (0)
char _;
#define complete_unique(a) a.erase(unique(a.begin(), a.end()), a.end())
#define all(a) a.begin(), a.end()
#define println printf("\n");
#define readln(x) getline(cin, x);
#define pb push_back
#define endl "\n"
#define INT_INF 0x3f3f3f3f
#define LL_INF 0x3f3f3f3f3f3f3f3f
#define MOD 998244353
#define MOD2 1494318097
#define SEED 131
#define mp make_pair
#define fastio \
cin.tie(0); \
cin.sync_with_stdio(0);
#define MAXN 200005
#define MAXV 1000005
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef unordered_map<int, int> umii;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef pair<int, pii> triple;
typedef int8_t byte;
mt19937 g1(time(0));
int randint(int a, int b) { return uniform_int_distribution<int>(a, b)(g1); }
ll randlong(ll a, ll b) {
return uniform_int_distribution<long long>(a, b)(g1);
}
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
ll fpow(ll b, ll exp, ll mod) {
if (exp == 0)
return 1;
ll t = fpow(b, exp / 2, mod);
if (exp & 1)
return t * t % mod * b % mod;
return t * t % mod;
}
ll divmod(ll i, ll j, ll mod) {
i %= mod, j %= mod;
return i * fpow(j, mod - 2, mod) % mod;
}
int len, arr[MAXN];
vector<int> nums[MAXV];
ll dp[MAXV], res;
int main() {
scanf("%d", &len);
for (int i = 1; i <= len; i++) {
scanf(" %d", &arr[i]);
for (int k = 1; k <= sqrt(arr[i]); k++) {
if (arr[i] % k == 0) {
nums[k].pb(arr[i]);
if (k * k != arr[i])
nums[arr[i] / k].pb(arr[i]);
}
}
}
for (int n = 1000000; n >= 1; n--) {
if (!nums[n].size())
continue;
ll sum = 0, tot = 0;
for (int check : nums[n])
sum = (sum + check) % MOD;
for (int i = 0; i < nums[n].size(); i++) {
sum = (sum - nums[n][i] + MOD) % MOD;
tot = (tot + nums[n][i] * sum) % MOD;
}
for (int v = n + n; v <= 1000000; v += n)
tot = (tot - dp[v] + MOD) % MOD;
res = (res + divmod(tot, n, MOD)) % MOD;
dp[n] = tot;
}
printf("%lld\n", res);
} | insert | 86 | 86 | 86 | 88 | TLE | |
p02905 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 2, mod = 998244353;
int n, x, a[N], inv[N], w[N];
int main() {
inv[1] = 1;
for (int i = 2; i < N; i++)
inv[i] = (-1ll * inv[mod % i] * (mod / i) % mod + mod) % mod;
scanf("%d", &n);
int ans = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", &x);
(a[x] += x) %= mod;
(ans += mod - x) %= mod;
} // w[1]=1;
for (int i = 1; i < N; i++) {
int cnt = 0;
(w[i] += inv[i]) %= mod;
for (int j = i + i; j < N; j += i)
(w[j] += mod - w[i]) %= mod, (cnt += a[j]) %= mod;
(cnt += a[i]) %= mod;
// cout<<cnt<<" "<<i<<" "<<w[i]<<endl;
(ans += 1ll * cnt * cnt % mod * w[i] % mod) %= mod;
}
ans = (1ll * ans * inv[2] % mod + mod) % mod;
cout << ans;
}
| #include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 2, mod = 998244353;
int n, x, a[N], inv[N], w[N];
int main() {
inv[1] = 1;
for (int i = 2; i < N; i++)
inv[i] = (-1ll * inv[mod % i] * (mod / i) % mod + mod) % mod;
scanf("%d", &n);
int ans = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", &x);
(a[x] += x) %= mod;
(ans += mod - x) %= mod;
} // w[1]=1;
for (int i = 1; i < N; i++) {
int cnt = 0;
(w[i] += inv[i]) %= mod;
for (int j = i + i; j < N; j += i)
(w[j] += mod - w[i]) %= mod, (cnt += a[j]) %= mod;
(cnt += a[i]) %= mod;
// cout<<cnt<<" "<<i<<" "<<w[i]<<endl;
(ans += 1ll * cnt * cnt % mod * w[i] % mod) %= mod;
}
ans = (1ll * ans * inv[2] % mod + mod) % mod;
cout << ans;
}
| replace | 2 | 3 | 2 | 3 | 0 | |
p02905 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define fo(i, l, r) for (int i = l; i <= r; i++)
#define of(i, l, r) for (int i = l; i >= r; i--)
#define fe(i, u) for (int i = head[u]; i; i = e[i].next)
#define el putchar('\n')
#define ta putchar(' ')
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
inline void open(const char *s) {
#ifndef ONLINE_JUDGE
char str[20];
sprintf(str, "%s.in", s);
freopen(str, "r", stdin);
// sprintf(str,"%s.out",s);
// freopen(str,"w",stdout);
#endif
}
inline int rd() {
static int x, f;
x = 0;
f = 1;
char ch = getchar();
for (; ch < '0' || ch > '9'; ch = getchar())
if (ch == '-')
f = -1;
for (; ch >= '0' && ch <= '9'; ch = getchar())
x = x * 10 + ch - '0';
return f > 0 ? x : -x;
}
const int N = 1000010, mod = 998244353;
int n, a[N];
bool vis[N];
int pri[N], cnt = 0, miu[N], inv[N], f[N];
vector<int> fac[N];
inline int pls(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }
inline int upd(int &a, int b) { return a = pls(a, b); }
inline int mns(int a, int b) { return a < b ? a - b + mod : a - b; }
inline int mul(int a, int b) { return (ll)a * b % mod; }
inline void prelude(int n) {
miu[1] = 1;
inv[0] = inv[1] = 1;
fo(i, 2, n) {
inv[i] = mns(0, mul(mod / i, inv[mod % i]));
if (!vis[i])
miu[i] = mod - 1, pri[++cnt] = i;
for (int j = 1; j <= cnt && pri[j] * i <= n; ++j) {
int x = pri[j] * i;
vis[x] = 1;
if (i % pri[j] == 0) {
miu[x] = 0;
break;
}
miu[x] = mns(0, miu[i]);
}
}
fo(i, 1, n) {
for (int j = i; j <= n; j += i) {
upd(f[j], mul(miu[i], inv[j / i]));
if (i ^ j)
upd(a[i], a[j]);
}
}
fo(i, 1, cnt) for (int j = pri[i]; j <= n; j += pri[i]) fac[j].push_back(
pri[i]);
}
int main() {
open("hh");
n = rd();
int ans = 0;
fo(i, 1, n) {
int x = rd();
upd(a[x], x);
ans = mns(ans, x);
}
prelude(N - 10);
fo(i, 1, N - 10) if (f[i]) {
int tmp = mul(f[i], mul(a[i], a[i]));
upd(ans, tmp);
}
ans = mul(ans, (mod + 1) / 2);
cout << ans;
return 0;
}
| #include <bits/stdc++.h>
#define fo(i, l, r) for (int i = l; i <= r; i++)
#define of(i, l, r) for (int i = l; i >= r; i--)
#define fe(i, u) for (int i = head[u]; i; i = e[i].next)
#define el putchar('\n')
#define ta putchar(' ')
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
inline void open(const char *s) {
#ifndef ONLINE_JUDGE
char str[20];
sprintf(str, "%s.in", s);
freopen(str, "r", stdin);
// sprintf(str,"%s.out",s);
// freopen(str,"w",stdout);
#endif
}
inline int rd() {
static int x, f;
x = 0;
f = 1;
char ch = getchar();
for (; ch < '0' || ch > '9'; ch = getchar())
if (ch == '-')
f = -1;
for (; ch >= '0' && ch <= '9'; ch = getchar())
x = x * 10 + ch - '0';
return f > 0 ? x : -x;
}
const int N = 1000010, mod = 998244353;
int n, a[N];
bool vis[N];
int pri[N], cnt = 0, miu[N], inv[N], f[N];
vector<int> fac[N];
inline int pls(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }
inline int upd(int &a, int b) { return a = pls(a, b); }
inline int mns(int a, int b) { return a < b ? a - b + mod : a - b; }
inline int mul(int a, int b) { return (ll)a * b % mod; }
inline void prelude(int n) {
miu[1] = 1;
inv[0] = inv[1] = 1;
fo(i, 2, n) {
inv[i] = mns(0, mul(mod / i, inv[mod % i]));
if (!vis[i])
miu[i] = mod - 1, pri[++cnt] = i;
for (int j = 1; j <= cnt && pri[j] * i <= n; ++j) {
int x = pri[j] * i;
vis[x] = 1;
if (i % pri[j] == 0) {
miu[x] = 0;
break;
}
miu[x] = mns(0, miu[i]);
}
}
fo(i, 1, n) {
for (int j = i; j <= n; j += i) {
upd(f[j], mul(miu[i], inv[j / i]));
if (i ^ j)
upd(a[i], a[j]);
}
}
fo(i, 1, cnt) for (int j = pri[i]; j <= n; j += pri[i]) fac[j].push_back(
pri[i]);
}
int main() {
n = rd();
int ans = 0;
fo(i, 1, n) {
int x = rd();
upd(a[x], x);
ans = mns(ans, x);
}
prelude(N - 10);
fo(i, 1, N - 10) if (f[i]) {
int tmp = mul(f[i], mul(a[i], a[i]));
upd(ans, tmp);
}
ans = mul(ans, (mod + 1) / 2);
cout << ans;
return 0;
}
| delete | 70 | 71 | 70 | 70 | TLE | |
p02905 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#define INF 1000000009
#define LINF 1000000000000000009
#define double long double
#define all(a) a.begin(), a.end()
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
ll gcd(ll n, ll m) { return (m ? gcd(m, n % m) : n); }
ll lcm(ll n, ll m) { return n / gcd(n, m) * m; }
template <class T> class segtree {
int n;
vector<T> data;
T def;
function<T(T, T)> operation;
function<T(T, T)> update;
T _query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return def;
if (a <= l && r <= b)
return data[k];
T c1 = _query(a, b, 2 * k + 1, l, (l + r) / 2);
T c2 = _query(a, b, 2 * k + 2, (l + r) / 2, r);
return operation(c1, c2);
}
public:
segtree(size_t _n, T _def, function<T(T, T)> _operation,
function<T(T, T)> _update)
: def(_def), operation(_operation), update(_update) {
n = 1;
while (n < _n) {
n *= 2;
}
data = vector<T>(2 * n - 1, def);
}
void change(int i, T x) {
i += n - 1;
data[i] = update(data[i], x);
while (i > 0) {
i = (i - 1) / 2;
data[i] = operation(data[i * 2 + 1], data[i * 2 + 2]);
}
}
T query(int a, int b) { return _query(a, b, 0, 0, n); }
T operator[](int i) { return data[i + n - 1]; }
};
ll mod = 998244353;
ll modpow(ll a, ll b) {
ll ans = 1;
a %= mod;
while (b) {
if (b & 1)
ans = ans * a % mod;
a = a * a % mod;
b >>= 1;
}
return ans;
}
ll modinv(ll n) { return modpow(n, mod - 2); }
ll sz = 1000000;
int main() {
ll n;
cin >> n;
vector<ll> a(n);
vector<ll> cnt(sz + 3);
for (int i = 0; i < n; i++) {
cin >> a[i];
cnt[a[i]]++;
}
vector<ll> c(sz + 3);
for (ll i = 1; i <= sz; i++) {
c[i] += modinv(i);
c[i] %= mod;
for (ll j = 2 * i; j <= sz; j += i) {
c[j] -= c[i];
if (c[j] < 0)
c[j] += mod;
}
}
ll ans = 0;
for (ll i = 1; i <= sz; i++) {
ll x = 0, y = 0;
for (ll j = i; j <= sz; j += i) {
x += cnt[j] * j % mod;
x %= mod;
y += cnt[j] * j % mod * j % mod;
y %= mod;
}
x = x * x;
x %= mod;
x -= y;
x += mod;
x %= mod;
x *= modinv(2);
x %= mod;
x *= c[i];
x %= mod;
ans += x;
ans %= mod;
}
cout << ans << endl;
} | #include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#define INF 1000000009
#define LINF 1000000000000000009
#define double long double
#define all(a) a.begin(), a.end()
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
ll gcd(ll n, ll m) { return (m ? gcd(m, n % m) : n); }
ll lcm(ll n, ll m) { return n / gcd(n, m) * m; }
template <class T> class segtree {
int n;
vector<T> data;
T def;
function<T(T, T)> operation;
function<T(T, T)> update;
T _query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return def;
if (a <= l && r <= b)
return data[k];
T c1 = _query(a, b, 2 * k + 1, l, (l + r) / 2);
T c2 = _query(a, b, 2 * k + 2, (l + r) / 2, r);
return operation(c1, c2);
}
public:
segtree(size_t _n, T _def, function<T(T, T)> _operation,
function<T(T, T)> _update)
: def(_def), operation(_operation), update(_update) {
n = 1;
while (n < _n) {
n *= 2;
}
data = vector<T>(2 * n - 1, def);
}
void change(int i, T x) {
i += n - 1;
data[i] = update(data[i], x);
while (i > 0) {
i = (i - 1) / 2;
data[i] = operation(data[i * 2 + 1], data[i * 2 + 2]);
}
}
T query(int a, int b) { return _query(a, b, 0, 0, n); }
T operator[](int i) { return data[i + n - 1]; }
};
ll mod = 998244353;
ll modpow(ll a, ll b) {
ll ans = 1;
a %= mod;
while (b) {
if (b & 1)
ans = ans * a % mod;
a = a * a % mod;
b >>= 1;
}
return ans;
}
ll modinv(ll n) { return modpow(n, mod - 2); }
ll sz = 1000000;
int main() {
ll n;
cin >> n;
vector<ll> a(n);
vector<ll> cnt(sz + 3);
for (int i = 0; i < n; i++) {
cin >> a[i];
cnt[a[i]]++;
}
vector<ll> c(sz + 3);
for (ll i = 1; i <= sz; i++) {
c[i] += modinv(i);
c[i] %= mod;
for (ll j = 2 * i; j <= sz; j += i) {
c[j] -= c[i];
if (c[j] < 0)
c[j] += mod;
}
}
ll ans = 0;
for (ll i = 1; i <= sz; i++) {
ll x = 0, y = 0;
for (ll j = i; j <= sz; j += i) {
x += cnt[j] * j % mod;
x %= mod;
y += cnt[j] * j % mod * j % mod;
y %= mod;
}
x = x * x;
x %= mod;
x -= y;
if (x < 0)
x += mod;
x *= (mod + 1) / 2;
x %= mod;
x *= c[i];
x %= mod;
ans += x;
ans %= mod;
}
cout << ans << endl;
} | replace | 122 | 125 | 122 | 125 | TLE | |
p02905 | C++ | Runtime Error | // পূর্ণিমা সন্ধ্যায় তোমার রজনী গন্ধায়
// রূপসাগরের পাড়ের পানে উদাসী মন ধায়
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
typedef long long ll;
const long long M = 1e5 + 20, MX = 1e6 + 20, mod = 998244353;
ll ara[M], inv[MX];
vi dvs[MX];
void invrs(ll m) {
inv[1] = 1;
for (ll i = 2; i <= m; i++) {
inv[i] = (mod - ((mod / i) * inv[mod % i]) % mod) % mod;
// if (i == 2) cout << "inv " << inv[i] << endl;
}
}
ll mx = 0, sm = 0, ans = 0, csm[MX], cnt[MX];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> ara[i];
cnt[ara[i]]++;
sm = (sm + ara[i]) % mod;
mx = max(mx, ara[i]);
}
invrs(mx);
for (int i = 1; i <= mx; i++) {
for (int j = i; j <= mx; j += i) {
dvs[j].pb(i);
if (cnt[j] > 0)
csm[i] = (csm[i] + (cnt[j] * j)) % mod;
}
}
for (int i = 1; i <= mx; i++) {
csm[i] = (csm[i] * csm[i]) % mod;
// cout << i << " " << csm[i] << endl;
}
for (int i = mx; i >= 1; i--) {
ans = (ans + csm[i] * inv[i]) % mod;
// cout << i << " " << csm[i] << " " << inv[i] << endl;
// cout << ans << endl;
for (int j : dvs[i]) {
csm[j] = (csm[j] + (mod - csm[i])) % mod;
}
}
ans = ((ans + mod - sm % mod) * inv[2]) % mod;
cout << ans << endl;
return 0;
}
| // পূর্ণিমা সন্ধ্যায় তোমার রজনী গন্ধায়
// রূপসাগরের পাড়ের পানে উদাসী মন ধায়
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
typedef long long ll;
const long long M = 1e5 + 20, MX = 1e6 + 20, mod = 998244353;
ll ara[2 * M], inv[MX];
vi dvs[MX];
void invrs(ll m) {
inv[1] = 1;
for (ll i = 2; i <= m; i++) {
inv[i] = (mod - ((mod / i) * inv[mod % i]) % mod) % mod;
// if (i == 2) cout << "inv " << inv[i] << endl;
}
}
ll mx = 0, sm = 0, ans = 0, csm[MX], cnt[MX];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> ara[i];
cnt[ara[i]]++;
sm = (sm + ara[i]) % mod;
mx = max(mx, ara[i]);
}
invrs(mx);
for (int i = 1; i <= mx; i++) {
for (int j = i; j <= mx; j += i) {
dvs[j].pb(i);
if (cnt[j] > 0)
csm[i] = (csm[i] + (cnt[j] * j)) % mod;
}
}
for (int i = 1; i <= mx; i++) {
csm[i] = (csm[i] * csm[i]) % mod;
// cout << i << " " << csm[i] << endl;
}
for (int i = mx; i >= 1; i--) {
ans = (ans + csm[i] * inv[i]) % mod;
// cout << i << " " << csm[i] << " " << inv[i] << endl;
// cout << ans << endl;
for (int j : dvs[i]) {
csm[j] = (csm[j] + (mod - csm[i])) % mod;
}
}
ans = ((ans + mod - sm % mod) * inv[2]) % mod;
cout << ans << endl;
return 0;
}
| replace | 9 | 10 | 9 | 10 | 0 | |
p02905 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
const int M = 1000000;
int N;
int A[100000];
long C[M + 1];
void sub() {
for (int i = 1; i <= M; ++i) {
long sum = 0;
for (int k = i; k <= M; k += i) {
sum += C[k];
}
C[i] = sum % mod;
}
for (int i = 1; i <= M; ++i) {
C[i] = C[i] * C[i] % mod;
}
for (int i = M; i >= 1; --i) {
for (int k = 2 * i; k <= M; k += i) {
C[i] += mod - C[k];
}
C[i] %= mod;
}
}
long powmod(long b, long e) {
if (e == 0)
return 1;
if (e % 2 == 1)
return b * powmod(b, e - 1) % mod;
long t = powmod(b, e / 2);
return t * t % mod;
}
long inv(long n) { return powmod(n, mod - 2); }
int main() {
cin >> N;
for (int i = 0; i < N; ++i) {
int a;
cin >> a;
A[i] = a;
C[a] += a;
if (C[a] >= mod)
C[a] -= mod;
}
sub();
long ans = 0;
for (int i = 1; i <= M; ++i) {
ans += inv(i) * C[i] % mod;
}
ans %= mod;
long sum = accumulate(A, A + N, 0l) % mod;
ans = (ans + mod - sum) * inv(2) % mod;
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
const int M = 1000000;
int N;
int A[200000];
long C[M + 1];
void sub() {
for (int i = 1; i <= M; ++i) {
long sum = 0;
for (int k = i; k <= M; k += i) {
sum += C[k];
}
C[i] = sum % mod;
}
for (int i = 1; i <= M; ++i) {
C[i] = C[i] * C[i] % mod;
}
for (int i = M; i >= 1; --i) {
for (int k = 2 * i; k <= M; k += i) {
C[i] += mod - C[k];
}
C[i] %= mod;
}
}
long powmod(long b, long e) {
if (e == 0)
return 1;
if (e % 2 == 1)
return b * powmod(b, e - 1) % mod;
long t = powmod(b, e / 2);
return t * t % mod;
}
long inv(long n) { return powmod(n, mod - 2); }
int main() {
cin >> N;
for (int i = 0; i < N; ++i) {
int a;
cin >> a;
A[i] = a;
C[a] += a;
if (C[a] >= mod)
C[a] -= mod;
}
sub();
long ans = 0;
for (int i = 1; i <= M; ++i) {
ans += inv(i) * C[i] % mod;
}
ans %= mod;
long sum = accumulate(A, A + N, 0l) % mod;
ans = (ans + mod - sum) * inv(2) % mod;
cout << ans << endl;
} | replace | 7 | 8 | 7 | 8 | 0 | |
p02905 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define pf printf
#define PF(a) printf("%d\n", (a))
#define PF2(a, b) printf("%d %d\n", (a), (b))
#define PF3(a, b, c) printf("%d %d %d\n", (a), (b), (c))
#define PFL(a) printf("%lld\n", (a))
#define PFL2(a, b) printf("%lld %lld\n", (a), (b))
#define PFL3(a, b, c) printf("%lld %lld %lld\n", (a), (b), (c))
#define get(a) cin >> a
#define sf scanf
#define SF(a) scanf("%d", &a)
#define SF2(a, b) scanf("%d %d", &a, &b)
#define SF3(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define SFL(a) scanf("%lld", &a)
#define SFL2(a, b) scanf("%lld %lld", &a, &b)
#define SFL3(a, b, c) scanf("%lld %lld %lld", &a, &b, &c)
#define gl(a) getline(cin, (a))
#define gc() getchar()
#define pb push_back
#define m_p make_pair
#define pc() printf("Case %d: ", tt++)
#define tc() cout << "Case " << tt++ << ": "
#define SZ(a) (int)(a).size()
#define all(a) a.begin(), a.end()
#define FF first
#define SS second
#define MAX 2134567891
#define MOD 1000000007
#define MM 100005
#define PI 2 * acos(0.0)
#define cond(n, m) (xx >= 0 && xx < (n) && yy >= 0 && yy < (m))
#define mem(a) memset((a), 0, sizeof(a))
#define SET(a) memset((a), -1, sizeof(a))
#define output freopen("output.txt", "w", stdout);
#define input freopen("input.txt", "r", stdin);
#define I_O \
ios_base::sync_with_stdio(0); \
cin.tie(0)
#define rep(a) for (int i = 0; i < (a); i++)
#define FOR(a, b) for (int i = (a); i < (b); i++)
#define REP(a) for (int j = 0; j < (a); j++)
#define rev(a) for (int i = (a); i >= 0; i--)
#define fr(i, n) for (i = 0; i < n; i++)
#define fr1(i, n, m) for (i = n; i < m; i++)
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy8[] = {1, -1, -1, 0, 1, -1, 0, 1};
int kx8[] = {1, 1, 2, 2, -1, -1, -2, -2};
int ky8[] = {2, -2, 1, -1, 2, -2, 1, -1};
typedef long long ll;
typedef unsigned long long llu;
typedef priority_queue<int> PQ;
typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<string> vs;
ll bigmod(ll a, ll b, ll c) {
if (b == 0)
return 1 % c;
ll x = bigmod(a, b / 2, c);
x = x * x % c;
if (b % 2 == 1)
x = x * a % c;
return x;
}
ll mod_inverse(ll a, ll mod) { return bigmod(a, mod - 2, mod); }
/*
//FUNCTION//
ll poww(ll a, ll b)
{
if(b==0)return 1;ll x=poww(a,b/2);x=x*x;if(b%2==1)x=x*a;return x;
}
ll LCM(ll a, ll b){ return a*b/ __gcd(a,b);}
int pr = 1000000;
vi primelist;
bool a[MM*10];
void seive( )
{
int i,j,k=sqrt(pr);
a[1]=1;
primelist.pb(2);
for(i=4;i<=pr;i+=2)a[i]=1;
for(i=3;i<=k;i+=2)if(!a[i])for(j=i*i;j<=pr;j+=2*i)a[j]=1;
for(i=3;i<=pr;i+=2)if(!a[i])primelist.pb(i);
}
ll fact_divs( ll n, ll p)
{
ll cnt=0;while(p<=n){cnt += n/p;n /= p;}return cnt;
}
struct point{double x, y;};
double area( point A, point B, point C){
return 0.5*(A.x*(B.y-C.y) - A.y*(B.x-C.x) + B.x*C.y - C.x*B.y);
}
ll Set(ll N,ll pos){return N=N | (1LL<<pos);}
ll reset(ll N,ll pos){return N= N & ~(1LL<<pos);}
bool check(ll N,ll pos){return (bool)(N & (1LL<<pos));}
//*/
const int maxn = 1000005;
vector<int> F[maxn];
ll p[maxn];
ll Count[maxn];
void sieve() {
for (int i = 1; i < maxn; i++) {
for (int j = i; j < maxn; j += i) {
F[j].push_back(i);
}
}
}
int a[maxn];
ll mod = 998244353;
int main() {
sieve();
int n;
scanf("%d", &n);
ll tot = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
tot += a[i];
tot %= mod;
for (int x : F[a[i]]) {
p[x] += a[i];
if (p[x] >= mod)
p[x] -= mod;
}
}
for (int i = maxn; i >= 1; i--) {
Count[i] = (p[i] * p[i]) % mod;
for (ll j = i + i; j <= maxn; j += i)
Count[i] -= (Count[j] * j) % mod;
Count[i] %= mod;
if (Count[i] < 0)
Count[i] += mod;
ll md_inv = mod_inverse(i, mod);
Count[i] = (Count[i] * md_inv) % mod;
}
ll sum = 0;
for (int i = 1; i < maxn; i++) {
sum += Count[i];
sum %= mod;
}
sum -= tot;
if (sum < 0)
sum += mod;
ll md_inv = mod_inverse(2, mod);
sum *= md_inv;
sum %= mod;
PFL(sum);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define pf printf
#define PF(a) printf("%d\n", (a))
#define PF2(a, b) printf("%d %d\n", (a), (b))
#define PF3(a, b, c) printf("%d %d %d\n", (a), (b), (c))
#define PFL(a) printf("%lld\n", (a))
#define PFL2(a, b) printf("%lld %lld\n", (a), (b))
#define PFL3(a, b, c) printf("%lld %lld %lld\n", (a), (b), (c))
#define get(a) cin >> a
#define sf scanf
#define SF(a) scanf("%d", &a)
#define SF2(a, b) scanf("%d %d", &a, &b)
#define SF3(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define SFL(a) scanf("%lld", &a)
#define SFL2(a, b) scanf("%lld %lld", &a, &b)
#define SFL3(a, b, c) scanf("%lld %lld %lld", &a, &b, &c)
#define gl(a) getline(cin, (a))
#define gc() getchar()
#define pb push_back
#define m_p make_pair
#define pc() printf("Case %d: ", tt++)
#define tc() cout << "Case " << tt++ << ": "
#define SZ(a) (int)(a).size()
#define all(a) a.begin(), a.end()
#define FF first
#define SS second
#define MAX 2134567891
#define MOD 1000000007
#define MM 100005
#define PI 2 * acos(0.0)
#define cond(n, m) (xx >= 0 && xx < (n) && yy >= 0 && yy < (m))
#define mem(a) memset((a), 0, sizeof(a))
#define SET(a) memset((a), -1, sizeof(a))
#define output freopen("output.txt", "w", stdout);
#define input freopen("input.txt", "r", stdin);
#define I_O \
ios_base::sync_with_stdio(0); \
cin.tie(0)
#define rep(a) for (int i = 0; i < (a); i++)
#define FOR(a, b) for (int i = (a); i < (b); i++)
#define REP(a) for (int j = 0; j < (a); j++)
#define rev(a) for (int i = (a); i >= 0; i--)
#define fr(i, n) for (i = 0; i < n; i++)
#define fr1(i, n, m) for (i = n; i < m; i++)
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy8[] = {1, -1, -1, 0, 1, -1, 0, 1};
int kx8[] = {1, 1, 2, 2, -1, -1, -2, -2};
int ky8[] = {2, -2, 1, -1, 2, -2, 1, -1};
typedef long long ll;
typedef unsigned long long llu;
typedef priority_queue<int> PQ;
typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<string> vs;
ll bigmod(ll a, ll b, ll c) {
if (b == 0)
return 1 % c;
ll x = bigmod(a, b / 2, c);
x = x * x % c;
if (b % 2 == 1)
x = x * a % c;
return x;
}
ll mod_inverse(ll a, ll mod) { return bigmod(a, mod - 2, mod); }
/*
//FUNCTION//
ll poww(ll a, ll b)
{
if(b==0)return 1;ll x=poww(a,b/2);x=x*x;if(b%2==1)x=x*a;return x;
}
ll LCM(ll a, ll b){ return a*b/ __gcd(a,b);}
int pr = 1000000;
vi primelist;
bool a[MM*10];
void seive( )
{
int i,j,k=sqrt(pr);
a[1]=1;
primelist.pb(2);
for(i=4;i<=pr;i+=2)a[i]=1;
for(i=3;i<=k;i+=2)if(!a[i])for(j=i*i;j<=pr;j+=2*i)a[j]=1;
for(i=3;i<=pr;i+=2)if(!a[i])primelist.pb(i);
}
ll fact_divs( ll n, ll p)
{
ll cnt=0;while(p<=n){cnt += n/p;n /= p;}return cnt;
}
struct point{double x, y;};
double area( point A, point B, point C){
return 0.5*(A.x*(B.y-C.y) - A.y*(B.x-C.x) + B.x*C.y - C.x*B.y);
}
ll Set(ll N,ll pos){return N=N | (1LL<<pos);}
ll reset(ll N,ll pos){return N= N & ~(1LL<<pos);}
bool check(ll N,ll pos){return (bool)(N & (1LL<<pos));}
//*/
const int maxn = 1000005;
vector<int> F[maxn];
ll p[maxn];
ll Count[maxn];
void sieve() {
for (int i = 1; i < maxn; i++) {
for (int j = i; j < maxn; j += i) {
F[j].push_back(i);
}
}
}
int a[maxn];
ll mod = 998244353;
int main() {
sieve();
int n;
scanf("%d", &n);
ll tot = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
tot += a[i];
tot %= mod;
for (int x : F[a[i]]) {
p[x] += a[i];
if (p[x] >= mod)
p[x] -= mod;
}
}
for (int i = maxn; i >= 1; i--) {
if (p[i] == 0)
continue;
Count[i] = (p[i] * p[i]) % mod;
for (ll j = i + i; j <= maxn; j += i)
Count[i] -= (Count[j] * j) % mod;
Count[i] %= mod;
if (Count[i] < 0)
Count[i] += mod;
ll md_inv = mod_inverse(i, mod);
Count[i] = (Count[i] * md_inv) % mod;
}
ll sum = 0;
for (int i = 1; i < maxn; i++) {
sum += Count[i];
sum %= mod;
}
sum -= tot;
if (sum < 0)
sum += mod;
ll md_inv = mod_inverse(2, mod);
sum *= md_inv;
sum %= mod;
PFL(sum);
return 0;
}
| insert | 148 | 148 | 148 | 150 | TLE | |
p02905 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using uint = unsigned int;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using ull = unsigned long long;
using ld = long double;
template <typename T> void _(const char *s, T h) {
cerr << s << " = " << h << "\n";
}
template <typename T, typename... Ts> void _(const char *s, T h, Ts... t) {
int b = 0;
while (((b += *s == '(') -= *s == ')') != 0 || *s != ',')
cerr << *s++;
cerr << " = " << h << ",";
_(s + 1, t...);
} // break continue pop_back 998244353
#define int ll
#define pii pll
#define f first
#define s second
#define pb emplace_back
#define forn(i, n) for (int i = 0; i < (n); ++i)
#define sz(a) ((int)(a).size())
#define sqr(x) ((x) * (x))
struct init {
init() {
cin.tie(0);
iostream::sync_with_stdio(0);
cout << fixed << setprecision(10);
cerr << fixed << setprecision(5);
}
~init() {
#ifdef LOCAL
#define dbg(...) _(#__VA_ARGS__, __VA_ARGS__)
cerr << "Time elapsed: " << (double)clock() / CLOCKS_PER_SEC << "s.\n";
#else
#define dbg(...)
#endif
}
} init;
template <typename T, typename U> void upx(T &x, U y) {
if (x < y)
x = y;
}
template <typename T, typename U> void upn(T &x, U y) {
if (x > y)
x = y;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int D = 4, dx[] = {+1, 0, -1, 0}, dy[] = {0, +1, 0, -1};
const int N = 1e5 + 5;
const int32_t M = 998244353;
int32_t rev(int32_t x, int32_t m) {
if (x == 1)
return 1;
return (1 - (ll)rev(m % x, x) * m) / x + m;
}
struct mint {
int32_t x;
mint() : x(0) {}
mint(int32_t xx) : x(xx % M) {
if (x < 0)
x += M;
}
mint(ll xx) : x(xx % M) {
if (x < 0)
x += M;
}
mint &operator+=(mint b) {
if ((x += b.x) >= M)
x -= M;
return *this;
}
mint operator+(mint b) {
mint res = *this;
res += b;
return res;
}
mint &operator-=(mint b) {
if ((x -= b.x) < 0)
x += M;
return *this;
}
mint operator-(mint b) {
mint res = *this;
res -= b;
return res;
}
mint &operator*=(mint b) {
x = (ll)x * b.x % M;
return *this;
}
mint operator*(mint b) {
mint res = *this;
res *= b;
return res;
}
mint inverse() { return rev(x, M); }
mint &operator/=(mint b) {
x = (ll)x * rev(b.x, M) % M;
return *this;
}
mint operator/(mint b) {
mint res = *this;
res /= b;
return res;
}
mint operator^(ll w) {
mint r = 1, a = *this;
while (w) {
if (w & 1)
r *= a;
a *= a;
w /= 2;
}
return r;
}
mint operator^=(ll w) { return *this = (*this) ^ w; }
mint operator^(mint w) { return *this ^ w.x; }
mint operator^=(mint w) { return *this = (*this) ^ w; }
mint operator-() const { return mint() - *this; }
explicit operator bool() const { return x; }
};
istream &operator>>(istream &is, mint &p) {
istream &res = is >> p.x;
p.x %= M;
return res;
}
ostream &operator<<(ostream &os, mint p) { return os << p.x; }
template <typename T> mint operator+(T x, mint y) { return mint(x) + y; }
template <typename T> mint operator-(T x, mint y) { return mint(x) - y; }
template <typename T> mint operator*(T x, mint y) { return mint(x) * y; }
template <typename T> mint operator/(T x, mint y) { return mint(x) / y; }
template <typename T> mint operator^(T x, mint y) { return mint(x) ^ y; }
int a[N];
const int C = 1e6 + 1;
mint c[C], kek[C];
int lp[C], pr[C], cpr = 0, pw[C];
mint g[C];
int32_t main() {
int n;
cin >> n;
forn(i, n) {
cin >> a[i];
c[a[i]] += a[i];
}
for (int i = 1; i < C; ++i)
for (int j = i; j < C; j += i)
kek[i] += c[j];
for (int i = 2; i < C; ++i) {
if (!lp[i]) {
pr[cpr++] = lp[i] = i;
pw[i] = 1;
}
for (int j = 0; j < cpr && pr[j] <= lp[i] && i * pr[j] < C; ++j)
lp[i * pr[j]] = pr[j];
if (pw[i] && i * lp[i] < C)
pw[i * lp[i]] = pw[i] + 1;
}
g[1] = 1;
for (int i = 2; i < C; ++i) {
if (pw[i]) {
g[i] = (mint(1) - lp[i]) / i;
} else {
int x = 1, y = i, p = lp[i];
while (lp[y] == p)
x *= p, y /= p;
g[i] = g[x] * g[y];
}
}
mint ans = 0;
for (int i = 1; i < C; ++i) {
ans += (g[i] * kek[i]) * kek[i];
}
forn(i, n) ans -= a[i];
ans /= 2;
cout << ans << '\n';
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using uint = unsigned int;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using ull = unsigned long long;
using ld = long double;
template <typename T> void _(const char *s, T h) {
cerr << s << " = " << h << "\n";
}
template <typename T, typename... Ts> void _(const char *s, T h, Ts... t) {
int b = 0;
while (((b += *s == '(') -= *s == ')') != 0 || *s != ',')
cerr << *s++;
cerr << " = " << h << ",";
_(s + 1, t...);
} // break continue pop_back 998244353
#define int ll
#define pii pll
#define f first
#define s second
#define pb emplace_back
#define forn(i, n) for (int i = 0; i < (n); ++i)
#define sz(a) ((int)(a).size())
#define sqr(x) ((x) * (x))
struct init {
init() {
cin.tie(0);
iostream::sync_with_stdio(0);
cout << fixed << setprecision(10);
cerr << fixed << setprecision(5);
}
~init() {
#ifdef LOCAL
#define dbg(...) _(#__VA_ARGS__, __VA_ARGS__)
cerr << "Time elapsed: " << (double)clock() / CLOCKS_PER_SEC << "s.\n";
#else
#define dbg(...)
#endif
}
} init;
template <typename T, typename U> void upx(T &x, U y) {
if (x < y)
x = y;
}
template <typename T, typename U> void upn(T &x, U y) {
if (x > y)
x = y;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int D = 4, dx[] = {+1, 0, -1, 0}, dy[] = {0, +1, 0, -1};
const int N = 2e5 + 5;
const int32_t M = 998244353;
int32_t rev(int32_t x, int32_t m) {
if (x == 1)
return 1;
return (1 - (ll)rev(m % x, x) * m) / x + m;
}
struct mint {
int32_t x;
mint() : x(0) {}
mint(int32_t xx) : x(xx % M) {
if (x < 0)
x += M;
}
mint(ll xx) : x(xx % M) {
if (x < 0)
x += M;
}
mint &operator+=(mint b) {
if ((x += b.x) >= M)
x -= M;
return *this;
}
mint operator+(mint b) {
mint res = *this;
res += b;
return res;
}
mint &operator-=(mint b) {
if ((x -= b.x) < 0)
x += M;
return *this;
}
mint operator-(mint b) {
mint res = *this;
res -= b;
return res;
}
mint &operator*=(mint b) {
x = (ll)x * b.x % M;
return *this;
}
mint operator*(mint b) {
mint res = *this;
res *= b;
return res;
}
mint inverse() { return rev(x, M); }
mint &operator/=(mint b) {
x = (ll)x * rev(b.x, M) % M;
return *this;
}
mint operator/(mint b) {
mint res = *this;
res /= b;
return res;
}
mint operator^(ll w) {
mint r = 1, a = *this;
while (w) {
if (w & 1)
r *= a;
a *= a;
w /= 2;
}
return r;
}
mint operator^=(ll w) { return *this = (*this) ^ w; }
mint operator^(mint w) { return *this ^ w.x; }
mint operator^=(mint w) { return *this = (*this) ^ w; }
mint operator-() const { return mint() - *this; }
explicit operator bool() const { return x; }
};
istream &operator>>(istream &is, mint &p) {
istream &res = is >> p.x;
p.x %= M;
return res;
}
ostream &operator<<(ostream &os, mint p) { return os << p.x; }
template <typename T> mint operator+(T x, mint y) { return mint(x) + y; }
template <typename T> mint operator-(T x, mint y) { return mint(x) - y; }
template <typename T> mint operator*(T x, mint y) { return mint(x) * y; }
template <typename T> mint operator/(T x, mint y) { return mint(x) / y; }
template <typename T> mint operator^(T x, mint y) { return mint(x) ^ y; }
int a[N];
const int C = 1e6 + 1;
mint c[C], kek[C];
int lp[C], pr[C], cpr = 0, pw[C];
mint g[C];
int32_t main() {
int n;
cin >> n;
forn(i, n) {
cin >> a[i];
c[a[i]] += a[i];
}
for (int i = 1; i < C; ++i)
for (int j = i; j < C; j += i)
kek[i] += c[j];
for (int i = 2; i < C; ++i) {
if (!lp[i]) {
pr[cpr++] = lp[i] = i;
pw[i] = 1;
}
for (int j = 0; j < cpr && pr[j] <= lp[i] && i * pr[j] < C; ++j)
lp[i * pr[j]] = pr[j];
if (pw[i] && i * lp[i] < C)
pw[i * lp[i]] = pw[i] + 1;
}
g[1] = 1;
for (int i = 2; i < C; ++i) {
if (pw[i]) {
g[i] = (mint(1) - lp[i]) / i;
} else {
int x = 1, y = i, p = lp[i];
while (lp[y] == p)
x *= p, y /= p;
g[i] = g[x] * g[y];
}
}
mint ans = 0;
for (int i = 1; i < C; ++i) {
ans += (g[i] * kek[i]) * kek[i];
}
forn(i, n) ans -= a[i];
ans /= 2;
cout << ans << '\n';
return 0;
} | replace | 52 | 53 | 52 | 53 | 0 | |
p02905 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1000 + 1, MOD = 998244353;
int cnt[N], S[N], _S[N];
vector<int> Div[N];
int sum(int a, int b) { return a + b - (a + b >= MOD ? MOD : 0); }
int mul(ll a, ll b) { return a * b % MOD; }
int main() {
for (int i = N - 1; i >= 1; i--)
for (int j = i; j < N; j += i)
Div[j].push_back(i);
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n;
cin >> n;
for (int i = 0, x; i < n; i++)
cin >> x, cnt[x]++;
int ans = 0;
for (int i = 1; i < N; i++)
if (cnt[i]) {
ans = sum(ans, mul(i, 1ll * cnt[i] * (cnt[i] - 1) / 2));
int res = 0;
for (int d : Div[i])
_S[d] = S[d];
for (int d : Div[i]) {
res = sum(res, mul(S[d], i / d));
for (int _d : Div[d])
if (_d != d)
S[_d] = sum(S[_d], MOD - S[d]);
}
ans = sum(ans, mul(res, cnt[i]));
for (int d : Div[i])
S[d] = sum(_S[d], mul(i, cnt[i]));
}
cout << ans << "\n";
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e6 + 1, MOD = 998244353;
int cnt[N], S[N], _S[N];
vector<int> Div[N];
int sum(int a, int b) { return a + b - (a + b >= MOD ? MOD : 0); }
int mul(ll a, ll b) { return a * b % MOD; }
int main() {
for (int i = N - 1; i >= 1; i--)
for (int j = i; j < N; j += i)
Div[j].push_back(i);
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n;
cin >> n;
for (int i = 0, x; i < n; i++)
cin >> x, cnt[x]++;
int ans = 0;
for (int i = 1; i < N; i++)
if (cnt[i]) {
ans = sum(ans, mul(i, 1ll * cnt[i] * (cnt[i] - 1) / 2));
int res = 0;
for (int d : Div[i])
_S[d] = S[d];
for (int d : Div[i]) {
res = sum(res, mul(S[d], i / d));
for (int _d : Div[d])
if (_d != d)
S[_d] = sum(S[_d], MOD - S[d]);
}
ans = sum(ans, mul(res, cnt[i]));
for (int d : Div[i])
S[d] = sum(_S[d], mul(i, cnt[i]));
}
cout << ans << "\n";
return 0;
}
| replace | 4 | 5 | 4 | 5 | 0 | |
p02905 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>
#define ll long long
#define itn int
#define endl '\n'
#define co(ans) cout << ans << endl
#define COYE cout << "YES" << endl
#define COYe cout << "Yes" << endl
#define COye cout << "yes" << endl
#define CONO cout << "NO" << endl
#define CONo cout << "No" << endl
#define COno cout << "no" << endl
#define FORE(i, a) for (auto &i : a)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FFOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) FFOR(i, 1, n)
#define PB push_back
#define MP make_pair
#define ALL(V) (V).begin(), (V).end()
#define SORT(V) sort((V).begin(), (V).end())
#define REVERSE(V) reverse((V).begin(), (V).end())
#define EACH(V, i) \
for (typeof((V).begin()) i = (V).begin(); i != (V).end(); ++i)
#define equals(a, b) (fabs((a) - (b)) < EPS)
#define INF ((1LL << 62) - (1LL << 31))
#define EPS 1e-10
#define PI 3.141592653589793238
#define MOD 998244353
#define MAX 2010101
using namespace std;
using Edge = pair<ll, ll>;
using Graph = vector<vector<int>>;
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T> inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
////////// 二項係数(N<=10^7) //////////
ll fac[MAX], finv[MAX], inv[MAX];
// fac[i] i!
// finv[i] i!の逆元
// inv[i] iの逆元
// テーブルを作る前処理
void COM_init() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (ll i = 2; i < MAX; ++i) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
// 二項係数計算
ll COM(ll n, ll k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
//////////////////////////////////////////////////
////////// 約数列挙 //////////
vector<ll> divs[MAX];
// divs[i]=iの約数の集合
void DIVS_init() {
for (int i = 1; i < MAX; ++i) {
for (int j = i; j < MAX; j += i) {
divs[j].push_back(i);
}
}
}
//////////////////////////////////////////////////
int main() {
ll N, ans = 0;
cin >> N;
COM_init();
DIVS_init();
ll F[MAX]; // F[d] d=gcd(A[i],A[j])となるA[i]*A[j]の総和
ll G[MAX]; // G[d] d|A[i],A[j]となるA[i]*A[j]の総和
ll sum1[MAX]; // sum1[d] d|A[i]となるA[i]の総和
ll sum2[MAX]; // sum2[d] d|A[i]となるA[i]^2の総和
REP(i, MAX) F[i] = 0, G[i] = 0, sum1[i] = 0, sum2[i] = 0;
ll A[N];
REP(i, N) {
cin >> A[i];
FORE(v, divs[A[i]]) {
sum1[v] += A[i];
sum1[v] %= MOD;
sum2[v] += A[i] * A[i];
sum2[v] %= MOD;
}
}
// 2*ΣA[i]*A[j]=(ΣA[i])^2-Σ(A[i]^2)
for (int i = 1; i < MAX; ++i) {
G[i] += sum1[i] * sum1[i] % MOD;
G[i] -= sum2[i];
G[i] *= inv[2];
G[i] %= MOD;
if (G[i] < 0)
G[i] += MOD;
}
for (int i = 1010101; i >= 1; --i) {
ll cnt = G[i];
for (int j = 2; j * i < 1010101; ++j) {
cnt -= F[j * i];
cnt %= MOD;
if (cnt < 0)
cnt += MOD;
}
F[i] = cnt;
ans += F[i] * inv[i];
ans %= MOD;
if (ans < 0)
ans += MOD;
}
co(ans);
return 0;
} | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>
#define ll long long
#define itn int
#define endl '\n'
#define co(ans) cout << ans << endl
#define COYE cout << "YES" << endl
#define COYe cout << "Yes" << endl
#define COye cout << "yes" << endl
#define CONO cout << "NO" << endl
#define CONo cout << "No" << endl
#define COno cout << "no" << endl
#define FORE(i, a) for (auto &i : a)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FFOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) FFOR(i, 1, n)
#define PB push_back
#define MP make_pair
#define ALL(V) (V).begin(), (V).end()
#define SORT(V) sort((V).begin(), (V).end())
#define REVERSE(V) reverse((V).begin(), (V).end())
#define EACH(V, i) \
for (typeof((V).begin()) i = (V).begin(); i != (V).end(); ++i)
#define equals(a, b) (fabs((a) - (b)) < EPS)
#define INF ((1LL << 62) - (1LL << 31))
#define EPS 1e-10
#define PI 3.141592653589793238
#define MOD 998244353
#define MAX 1010101
using namespace std;
using Edge = pair<ll, ll>;
using Graph = vector<vector<int>>;
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T> inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
////////// 二項係数(N<=10^7) //////////
ll fac[MAX], finv[MAX], inv[MAX];
// fac[i] i!
// finv[i] i!の逆元
// inv[i] iの逆元
// テーブルを作る前処理
void COM_init() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (ll i = 2; i < MAX; ++i) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
// 二項係数計算
ll COM(ll n, ll k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
//////////////////////////////////////////////////
////////// 約数列挙 //////////
vector<ll> divs[MAX];
// divs[i]=iの約数の集合
void DIVS_init() {
for (int i = 1; i < MAX; ++i) {
for (int j = i; j < MAX; j += i) {
divs[j].push_back(i);
}
}
}
//////////////////////////////////////////////////
int main() {
ll N, ans = 0;
cin >> N;
COM_init();
DIVS_init();
ll F[MAX]; // F[d] d=gcd(A[i],A[j])となるA[i]*A[j]の総和
ll G[MAX]; // G[d] d|A[i],A[j]となるA[i]*A[j]の総和
ll sum1[MAX]; // sum1[d] d|A[i]となるA[i]の総和
ll sum2[MAX]; // sum2[d] d|A[i]となるA[i]^2の総和
REP(i, MAX) F[i] = 0, G[i] = 0, sum1[i] = 0, sum2[i] = 0;
ll A[N];
REP(i, N) {
cin >> A[i];
FORE(v, divs[A[i]]) {
sum1[v] += A[i];
sum1[v] %= MOD;
sum2[v] += A[i] * A[i];
sum2[v] %= MOD;
}
}
// 2*ΣA[i]*A[j]=(ΣA[i])^2-Σ(A[i]^2)
for (int i = 1; i < MAX; ++i) {
G[i] += sum1[i] * sum1[i] % MOD;
G[i] -= sum2[i];
G[i] *= inv[2];
G[i] %= MOD;
if (G[i] < 0)
G[i] += MOD;
}
for (int i = 1010101; i >= 1; --i) {
ll cnt = G[i];
for (int j = 2; j * i < 1010101; ++j) {
cnt -= F[j * i];
cnt %= MOD;
if (cnt < 0)
cnt += MOD;
}
F[i] = cnt;
ans += F[i] * inv[i];
ans %= MOD;
if (ans < 0)
ans += MOD;
}
co(ans);
return 0;
} | replace | 53 | 54 | 53 | 54 | TLE | |
p02905 | C++ | Runtime Error | // +-- -- --++-- +-In the name of ALLAH-+ --++-- -- --+ \\
#include <bits/stdc++.h>
#define F first
#define S second
#define _sz(x) (int)x.size()
#define pb push_back
#define error(x) cout << #x << " = " << x << endl
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
const int N = 1e6 + 20, mod = 998244353;
int n, a[N], ans;
int sum[N], val[N];
vector<int> dv[N];
inline int pw(int a, int b) {
int res = 1;
while (b) {
if (b & 1)
res = 1ll * res * a % mod;
a = 1ll * a * a % mod;
b >>= 1;
}
return res;
}
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
for (int i = 1; i < N; i++)
for (int j = i; j < N; j += i)
dv[j].pb(i);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
for (int x : dv[a[i]]) {
val[x] = (val[x] + 1ll * sum[x] * a[i]) % mod;
sum[x] = (sum[x] + a[i]) % mod;
}
}
for (int i = N; i >= 0; i--) {
ans = (ans + 1ll * val[i] * pw(i, mod - 2)) % mod;
for (int j : dv[i]) {
if (i == j)
continue;
val[j] = (val[j] - val[i] + mod) % mod;
}
}
cout << ans << '\n';
}
| // +-- -- --++-- +-In the name of ALLAH-+ --++-- -- --+ \\
#include <bits/stdc++.h>
#define F first
#define S second
#define _sz(x) (int)x.size()
#define pb push_back
#define error(x) cout << #x << " = " << x << endl
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
const int N = 1e6 + 20, mod = 998244353;
int n, a[N], ans;
int sum[N], val[N];
vector<int> dv[N];
inline int pw(int a, int b) {
int res = 1;
while (b) {
if (b & 1)
res = 1ll * res * a % mod;
a = 1ll * a * a % mod;
b >>= 1;
}
return res;
}
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
for (int i = 1; i < N; i++)
for (int j = i; j < N; j += i)
dv[j].pb(i);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
for (int x : dv[a[i]]) {
val[x] = (val[x] + 1ll * sum[x] * a[i]) % mod;
sum[x] = (sum[x] + a[i]) % mod;
}
}
for (int i = N - 1; i > 0; i--) {
ans = (ans + 1ll * val[i] * pw(i, mod - 2)) % mod;
for (int j : dv[i]) {
if (i == j)
continue;
val[j] = (val[j] - val[i] + mod) % mod;
}
}
cout << ans << '\n';
}
| replace | 50 | 51 | 50 | 51 | TLE | |
p02905 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
using namespace std;
using ll = long long;
int const nmax = 1000000;
int const modulo = 998244353;
int v[1 + nmax];
ll sum[1 + nmax], sol[1 + nmax];
void gcd(int a, int b, int &x, int &y) {
if (b == 0) {
x = 1;
y = 0;
} else {
gcd(b, a % b, x, y);
int aux = x;
x = y;
y = aux - a / b * y;
}
}
int inv(int number) {
int x, y;
gcd(number, modulo, x, y);
x %= modulo;
if (x < 0)
x += modulo;
return x;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> v[i];
sum[v[i]] += v[i];
}
for (int i = 1; i <= nmax; i++)
for (int j = 2 * i; j <= nmax; j += i)
sum[i] += sum[j];
ll result = 0, result2 = 0;
for (int i = 1; i < n; i++)
for (int j = i + 1; j <= n; j++) {
result2 += (1LL * v[i] * v[j] / __gcd(v[i], v[j])) % modulo;
if (modulo <= result2)
result2 -= modulo;
}
for (int i = nmax; 1 <= i; i--) {
sol[i] = 1LL * (sum[i] / i) % modulo * (sum[i] % modulo) % modulo;
for (int j = 2 * i; j <= nmax; j += i) {
sol[i] -= 1LL * sol[j] * (j / i) % modulo;
if (sol[i] < 0)
sol[i] += modulo;
}
result += sol[i];
if (modulo <= result)
result -= modulo;
}
cout << 1LL * ((modulo + result - sum[1] % modulo) * inv(2)) % modulo;
return 0;
}
| #include <algorithm>
#include <iostream>
using namespace std;
using ll = long long;
int const nmax = 1000000;
int const modulo = 998244353;
int v[1 + nmax];
ll sum[1 + nmax], sol[1 + nmax];
void gcd(int a, int b, int &x, int &y) {
if (b == 0) {
x = 1;
y = 0;
} else {
gcd(b, a % b, x, y);
int aux = x;
x = y;
y = aux - a / b * y;
}
}
int inv(int number) {
int x, y;
gcd(number, modulo, x, y);
x %= modulo;
if (x < 0)
x += modulo;
return x;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> v[i];
sum[v[i]] += v[i];
}
for (int i = 1; i <= nmax; i++)
for (int j = 2 * i; j <= nmax; j += i)
sum[i] += sum[j];
ll result = 0;
for (int i = nmax; 1 <= i; i--) {
sol[i] = 1LL * (sum[i] / i) % modulo * (sum[i] % modulo) % modulo;
for (int j = 2 * i; j <= nmax; j += i) {
sol[i] -= 1LL * sol[j] * (j / i) % modulo;
if (sol[i] < 0)
sol[i] += modulo;
}
result += sol[i];
if (modulo <= result)
result -= modulo;
}
cout << 1LL * ((modulo + result - sum[1] % modulo) * inv(2)) % modulo;
return 0;
}
| replace | 49 | 57 | 49 | 50 | TLE | |
p02905 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define rrep(i, a, b) for (int i = (a); i >= (b); --i)
#define PB push_back
#define ar2 array<int, 2>
typedef long long LL;
const int P = 998244353;
const int N = 2e5, M = 1e6;
const double pi = acos(-1);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
LL n, a[N + 5], sum[M + 5], sum2[M + 5];
int cnt[M + 5];
vector<int> fac[M + 5];
LL pw(LL x, int n) {
LL res = 1;
for (; n; n >>= 1, x = x * x % P)
if (n & 1)
res = res * x % P;
return res;
}
void add(LL &x, LL y) {
x += y;
if (x >= P)
x -= P;
}
void sub(LL &x, LL y) {
x -= y;
if (x < 0)
x += P;
}
int main() {
freopen("test.in", "r", stdin);
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
rep(i, 1, n) cin >> a[i];
rep(i, 1, M) for (int j = i + i; j <= M; j += i) fac[j].push_back(i);
rep(i, 1, n) {
for (auto x : fac[a[i]])
add(sum[x], a[i]), add(sum2[x], a[i] * a[i] % P), ++cnt[x];
add(sum[a[i]], a[i]), add(sum2[a[i]], a[i] * a[i] % P), ++cnt[a[i]];
}
LL ans = 0, inv2 = pw(2, P - 2);
rrep(i, M, 1) if (cnt[i]) {
LL x = sum[i], y = sum2[i];
x = x * x % P, sub(x, y), x = x * inv2 % P;
sum2[i] = x;
}
rrep(i, M, 1) if (cnt[i]) {
add(ans, sum2[i] * pw(i, P - 2) % P);
for (auto x : fac[i])
sub(sum2[x], sum2[i]);
// cout<<i<<" "<<sum2[i]<<" "<<cnt[i]<<endl;
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define rrep(i, a, b) for (int i = (a); i >= (b); --i)
#define PB push_back
#define ar2 array<int, 2>
typedef long long LL;
const int P = 998244353;
const int N = 2e5, M = 1e6;
const double pi = acos(-1);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
LL n, a[N + 5], sum[M + 5], sum2[M + 5];
int cnt[M + 5];
vector<int> fac[M + 5];
LL pw(LL x, int n) {
LL res = 1;
for (; n; n >>= 1, x = x * x % P)
if (n & 1)
res = res * x % P;
return res;
}
void add(LL &x, LL y) {
x += y;
if (x >= P)
x -= P;
}
void sub(LL &x, LL y) {
x -= y;
if (x < 0)
x += P;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
rep(i, 1, n) cin >> a[i];
rep(i, 1, M) for (int j = i + i; j <= M; j += i) fac[j].push_back(i);
rep(i, 1, n) {
for (auto x : fac[a[i]])
add(sum[x], a[i]), add(sum2[x], a[i] * a[i] % P), ++cnt[x];
add(sum[a[i]], a[i]), add(sum2[a[i]], a[i] * a[i] % P), ++cnt[a[i]];
}
LL ans = 0, inv2 = pw(2, P - 2);
rrep(i, M, 1) if (cnt[i]) {
LL x = sum[i], y = sum2[i];
x = x * x % P, sub(x, y), x = x * inv2 % P;
sum2[i] = x;
}
rrep(i, M, 1) if (cnt[i]) {
add(ans, sum2[i] * pw(i, P - 2) % P);
for (auto x : fac[i])
sub(sum2[x], sum2[i]);
// cout<<i<<" "<<sum2[i]<<" "<<cnt[i]<<endl;
}
cout << ans << endl;
} | delete | 35 | 36 | 35 | 35 | -6 | terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
|
p02905 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define rep(i, s, e) for (i64(i) = (s); (i) < (e); (i)++)
#define rev(i, s, e) for (i64(i) = (s); (i)-- > (e);)
#define all(x) x.begin(), x.end()
template <class T>
static inline std::vector<T> ndvec(size_t &&n, T val) noexcept {
return std::vector<T>(n, std::forward<T>(val));
}
template <class... Tail>
static inline auto ndvec(size_t &&n, Tail &&...tail) noexcept {
return std::vector<decltype(ndvec(std::forward<Tail>(tail)...))>(
n, ndvec(std::forward<Tail>(tail)...));
}
#pragma GCC target("avx")
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
template <i64 M> struct modint {
i64 a;
constexpr modint(const i64 x = 0) noexcept : a((x % M + M) % M) {}
constexpr i64 value() const noexcept { return a; }
constexpr modint pow(i64 r) const noexcept {
modint ans(1);
modint aa = *this;
while (r) {
if (r & 1) {
ans *= aa;
}
aa *= aa;
r >>= 1;
}
return ans;
}
constexpr modint &operator+=(const modint r) noexcept {
a += r.a;
if (a >= M)
a -= M;
return *this;
}
constexpr modint &operator=(const i64 r) {
a = (r % M + M) % M;
return *this;
}
constexpr modint &operator-=(const modint r) noexcept {
a -= r.a;
if (a < 0)
a += M;
return *this;
}
constexpr modint &operator*=(const modint r) noexcept {
a = a * r.a % M;
return *this;
}
constexpr modint &operator/=(modint r) noexcept {
i64 ex = M - 2;
while (ex) {
if (ex & 1) {
*this *= r;
}
r *= r;
ex >>= 1;
}
return *this;
}
constexpr modint operator+(const modint r) const {
return modint(*this) += r;
}
constexpr modint operator-(const modint r) const {
return modint(*this) -= r;
}
constexpr modint operator*(const modint r) const {
return modint(*this) *= r;
}
constexpr modint operator/(const modint r) const {
return modint(*this) /= r;
}
};
template <const i64 M>
std::ostream &operator<<(std::ostream &os, const modint<M> &m) {
os << m.value();
return os;
}
const i64 MOD = 998244353;
using fp = modint<MOD>;
constexpr i64 A = 1e6 + 1;
i64 gcd(i64 a, i64 b) {
while (b != 0) {
a %= b;
swap(a, b);
}
return a;
}
int main() {
vector<vector<int>> divs(A);
rep(i, 1, A) {
for (i64 x = i; x <= A; x += i) {
divs[x].emplace_back(i);
}
}
std::vector<int> mu(A);
mu[1] = 1;
rep(x, 2, A) {
for (auto d : divs[x]) {
if (d < x)
mu[x] -= mu[d];
}
}
std::vector<fp> g(A);
rep(i, 1, A) { g[i] = fp(i).pow(MOD - 2); }
std::vector<fp> f(A);
rep(x, 1, A) {
for (auto d : divs[x]) {
if (mu[d] == 1)
f[x] += g[x / d];
if (mu[d] == -1)
f[x] -= g[x / d];
}
}
cin.tie(nullptr);
std::ios::sync_with_stdio(false);
i64 N;
cin >> N;
vector<fp> sum(A);
fp ans = 0;
rep(i, 0, N) {
i64 A;
cin >> A;
fp res = 0;
for (auto d : divs[A]) {
res += f[d] * sum[d];
sum[d] += fp(A);
}
ans += res * fp(A);
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, s, e) for (i64(i) = (s); (i) < (e); (i)++)
#define rev(i, s, e) for (i64(i) = (s); (i)-- > (e);)
#define all(x) x.begin(), x.end()
template <class T>
static inline std::vector<T> ndvec(size_t &&n, T val) noexcept {
return std::vector<T>(n, std::forward<T>(val));
}
template <class... Tail>
static inline auto ndvec(size_t &&n, Tail &&...tail) noexcept {
return std::vector<decltype(ndvec(std::forward<Tail>(tail)...))>(
n, ndvec(std::forward<Tail>(tail)...));
}
#pragma GCC target("avx")
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
template <i64 M> struct modint {
i64 a;
constexpr modint(const i64 x = 0) noexcept : a(x) {}
constexpr i64 value() const noexcept { return a; }
constexpr modint pow(i64 r) const noexcept {
modint ans(1);
modint aa = *this;
while (r) {
if (r & 1) {
ans *= aa;
}
aa *= aa;
r >>= 1;
}
return ans;
}
constexpr modint &operator+=(const modint r) noexcept {
a += r.a;
if (a >= M)
a -= M;
return *this;
}
constexpr modint &operator=(const i64 r) {
a = (r % M + M) % M;
return *this;
}
constexpr modint &operator-=(const modint r) noexcept {
a -= r.a;
if (a < 0)
a += M;
return *this;
}
constexpr modint &operator*=(const modint r) noexcept {
a = a * r.a % M;
return *this;
}
constexpr modint &operator/=(modint r) noexcept {
i64 ex = M - 2;
while (ex) {
if (ex & 1) {
*this *= r;
}
r *= r;
ex >>= 1;
}
return *this;
}
constexpr modint operator+(const modint r) const {
return modint(*this) += r;
}
constexpr modint operator-(const modint r) const {
return modint(*this) -= r;
}
constexpr modint operator*(const modint r) const {
return modint(*this) *= r;
}
constexpr modint operator/(const modint r) const {
return modint(*this) /= r;
}
};
template <const i64 M>
std::ostream &operator<<(std::ostream &os, const modint<M> &m) {
os << m.value();
return os;
}
const i64 MOD = 998244353;
using fp = modint<MOD>;
constexpr i64 A = 1e6 + 1;
i64 gcd(i64 a, i64 b) {
while (b != 0) {
a %= b;
swap(a, b);
}
return a;
}
int main() {
vector<vector<int>> divs(A);
rep(i, 1, A) {
for (i64 x = i; x <= A; x += i) {
divs[x].emplace_back(i);
}
}
std::vector<int> mu(A);
mu[1] = 1;
rep(x, 2, A) {
for (auto d : divs[x]) {
if (d < x)
mu[x] -= mu[d];
}
}
std::vector<fp> g(A);
rep(i, 1, A) { g[i] = fp(i).pow(MOD - 2); }
std::vector<fp> f(A);
rep(x, 1, A) {
for (auto d : divs[x]) {
if (mu[d] == 1)
f[x] += g[x / d];
if (mu[d] == -1)
f[x] -= g[x / d];
}
}
cin.tie(nullptr);
std::ios::sync_with_stdio(false);
i64 N;
cin >> N;
vector<fp> sum(A);
fp ans = 0;
rep(i, 0, N) {
i64 A;
cin >> A;
fp res = 0;
for (auto d : divs[A]) {
res += f[d] * sum[d];
sum[d] += fp(A);
}
ans += res * fp(A);
}
cout << ans << endl;
}
| replace | 25 | 26 | 25 | 26 | TLE | |
p02905 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, s, e) for (i64(i) = (s); (i) < (e); (i)++)
#define rev(i, s, e) for (i64(i) = (s); (i)-- > (e);)
#define all(x) x.begin(), x.end()
template <class T>
static inline std::vector<T> ndvec(size_t &&n, T val) noexcept {
return std::vector<T>(n, std::forward<T>(val));
}
template <class... Tail>
static inline auto ndvec(size_t &&n, Tail &&...tail) noexcept {
return std::vector<decltype(ndvec(std::forward<Tail>(tail)...))>(
n, ndvec(std::forward<Tail>(tail)...));
}
#pragma GCC target("avx")
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
template <i64 M> struct modint {
i64 a;
constexpr modint(const i64 x = 0) noexcept : a(x) {}
constexpr i64 value() const noexcept { return a; }
constexpr modint pow(i64 r) const noexcept {
modint ans(1);
modint aa = *this;
while (r) {
if (r & 1) {
ans *= aa;
}
aa *= aa;
r >>= 1;
}
return ans;
}
constexpr modint &operator+=(const modint r) noexcept {
a += r.a;
if (a >= M)
a -= M;
return *this;
}
constexpr modint &operator=(const i64 r) {
a = (r % M + M) % M;
return *this;
}
constexpr modint &operator-=(const modint r) noexcept {
a -= r.a;
if (a < 0)
a += M;
return *this;
}
constexpr modint &operator*=(const modint r) noexcept {
a = a * r.a % M;
return *this;
}
constexpr modint &operator/=(modint r) noexcept {
i64 ex = M - 2;
while (ex) {
if (ex & 1) {
*this *= r;
}
r *= r;
ex >>= 1;
}
return *this;
}
constexpr modint operator+(const modint r) const {
return modint(*this) += r;
}
constexpr modint operator-(const modint r) const {
return modint(*this) -= r;
}
constexpr modint operator*(const modint r) const {
return modint(*this) *= r;
}
constexpr modint operator/(const modint r) const {
return modint(*this) /= r;
}
};
template <const i64 M>
std::ostream &operator<<(std::ostream &os, const modint<M> &m) {
os << m.value();
return os;
}
const i64 MOD = 998244353;
using fp = modint<MOD>;
i64 gcd(i64 a, i64 b) {
while (b != 0) {
a %= b;
swap(a, b);
}
return a;
}
#include <cstdio>
namespace niu {
char cur;
struct FIN {
static inline bool is_blank(char c) { return c <= ' '; }
inline char next() { return cur = getc_unlocked(stdin); }
inline char peek() { return cur; }
inline void skip() {
while (is_blank(next())) {
}
}
#define intin(inttype) \
FIN &operator>>(inttype &n) { \
bool sign = 0; \
n = 0; \
skip(); \
while (!is_blank(peek())) { \
if (peek() == '-') \
sign = 1; \
else \
n = (n << 1) + (n << 3) + (peek() & 0b1111); \
next(); \
} \
if (sign) \
n = -n; \
return *this; \
}
intin(int) intin(long long)
} fin;
char tmp[128];
struct FOUT {
static inline bool is_blank(char c) { return c <= ' '; }
inline void push(char c) { putc_unlocked(c, stdout); }
FOUT &operator<<(char c) {
push(c);
return *this;
}
FOUT &operator<<(const char *s) {
while (*s)
push(*s++);
return *this;
}
#define intout(inttype) \
FOUT &operator<<(inttype n) { \
if (n) { \
char *p = tmp + 127; \
bool neg = 0; \
if (n < 0) \
neg = 1, n = -n; \
while (n) \
*--p = (n % 10) | 0b00110000, n /= 10; \
if (neg) \
*--p = '-'; \
return (*this) << p; \
} else { \
push('0'); \
return *this; \
} \
}
intout(int) intout(long long)
} fout;
} // namespace niu
constexpr i64 A = 11;
int divs[A][240];
int dcnt[A];
int main() {
rep(i, 1, A) {
for (i64 x = i; x < A; x += i) {
divs[x][dcnt[x]] = i;
dcnt[x]++;
}
}
std::vector<int> mu(A);
mu[1] = 1;
rep(x, 2, A) {
for (int i = 0; i < dcnt[x]; i++) {
int d = divs[x][i];
if (d < x)
mu[x] -= mu[d];
}
}
std::vector<fp> g(A);
rep(i, 1, A) { g[i] = fp(i).pow(MOD - 2); }
std::vector<fp> f(A);
rep(x, 1, A) {
for (int i = 0; i < dcnt[x]; i++) {
int d = divs[x][i];
if (mu[d] == 1)
f[x] += g[x / d];
if (mu[d] == -1)
f[x] -= g[x / d];
}
}
i64 N;
niu::fin >> N;
vector<fp> sum(A);
fp ans = 0;
rep(i, 0, N) {
int x;
niu::fin >> x;
fp res = 0;
for (int j = 0; j < dcnt[x]; j++) {
int d = divs[x][j];
res += f[d] * sum[d];
sum[d] += fp(x);
}
ans += res * fp(x);
}
niu::fout << ans.value() << "\n";
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, s, e) for (i64(i) = (s); (i) < (e); (i)++)
#define rev(i, s, e) for (i64(i) = (s); (i)-- > (e);)
#define all(x) x.begin(), x.end()
template <class T>
static inline std::vector<T> ndvec(size_t &&n, T val) noexcept {
return std::vector<T>(n, std::forward<T>(val));
}
template <class... Tail>
static inline auto ndvec(size_t &&n, Tail &&...tail) noexcept {
return std::vector<decltype(ndvec(std::forward<Tail>(tail)...))>(
n, ndvec(std::forward<Tail>(tail)...));
}
#pragma GCC target("avx")
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
template <i64 M> struct modint {
i64 a;
constexpr modint(const i64 x = 0) noexcept : a(x) {}
constexpr i64 value() const noexcept { return a; }
constexpr modint pow(i64 r) const noexcept {
modint ans(1);
modint aa = *this;
while (r) {
if (r & 1) {
ans *= aa;
}
aa *= aa;
r >>= 1;
}
return ans;
}
constexpr modint &operator+=(const modint r) noexcept {
a += r.a;
if (a >= M)
a -= M;
return *this;
}
constexpr modint &operator=(const i64 r) {
a = (r % M + M) % M;
return *this;
}
constexpr modint &operator-=(const modint r) noexcept {
a -= r.a;
if (a < 0)
a += M;
return *this;
}
constexpr modint &operator*=(const modint r) noexcept {
a = a * r.a % M;
return *this;
}
constexpr modint &operator/=(modint r) noexcept {
i64 ex = M - 2;
while (ex) {
if (ex & 1) {
*this *= r;
}
r *= r;
ex >>= 1;
}
return *this;
}
constexpr modint operator+(const modint r) const {
return modint(*this) += r;
}
constexpr modint operator-(const modint r) const {
return modint(*this) -= r;
}
constexpr modint operator*(const modint r) const {
return modint(*this) *= r;
}
constexpr modint operator/(const modint r) const {
return modint(*this) /= r;
}
};
template <const i64 M>
std::ostream &operator<<(std::ostream &os, const modint<M> &m) {
os << m.value();
return os;
}
const i64 MOD = 998244353;
using fp = modint<MOD>;
i64 gcd(i64 a, i64 b) {
while (b != 0) {
a %= b;
swap(a, b);
}
return a;
}
#include <cstdio>
namespace niu {
char cur;
struct FIN {
static inline bool is_blank(char c) { return c <= ' '; }
inline char next() { return cur = getc_unlocked(stdin); }
inline char peek() { return cur; }
inline void skip() {
while (is_blank(next())) {
}
}
#define intin(inttype) \
FIN &operator>>(inttype &n) { \
bool sign = 0; \
n = 0; \
skip(); \
while (!is_blank(peek())) { \
if (peek() == '-') \
sign = 1; \
else \
n = (n << 1) + (n << 3) + (peek() & 0b1111); \
next(); \
} \
if (sign) \
n = -n; \
return *this; \
}
intin(int) intin(long long)
} fin;
char tmp[128];
struct FOUT {
static inline bool is_blank(char c) { return c <= ' '; }
inline void push(char c) { putc_unlocked(c, stdout); }
FOUT &operator<<(char c) {
push(c);
return *this;
}
FOUT &operator<<(const char *s) {
while (*s)
push(*s++);
return *this;
}
#define intout(inttype) \
FOUT &operator<<(inttype n) { \
if (n) { \
char *p = tmp + 127; \
bool neg = 0; \
if (n < 0) \
neg = 1, n = -n; \
while (n) \
*--p = (n % 10) | 0b00110000, n /= 10; \
if (neg) \
*--p = '-'; \
return (*this) << p; \
} else { \
push('0'); \
return *this; \
} \
}
intout(int) intout(long long)
} fout;
} // namespace niu
constexpr i64 A = 1e6 + 1;
int divs[A][240];
int dcnt[A];
int main() {
rep(i, 1, A) {
for (i64 x = i; x < A; x += i) {
divs[x][dcnt[x]] = i;
dcnt[x]++;
}
}
std::vector<int> mu(A);
mu[1] = 1;
rep(x, 2, A) {
for (int i = 0; i < dcnt[x]; i++) {
int d = divs[x][i];
if (d < x)
mu[x] -= mu[d];
}
}
std::vector<fp> g(A);
rep(i, 1, A) { g[i] = fp(i).pow(MOD - 2); }
std::vector<fp> f(A);
rep(x, 1, A) {
for (int i = 0; i < dcnt[x]; i++) {
int d = divs[x][i];
if (mu[d] == 1)
f[x] += g[x / d];
if (mu[d] == -1)
f[x] -= g[x / d];
}
}
i64 N;
niu::fin >> N;
vector<fp> sum(A);
fp ans = 0;
rep(i, 0, N) {
int x;
niu::fin >> x;
fp res = 0;
for (int j = 0; j < dcnt[x]; j++) {
int d = divs[x][j];
res += f[d] * sum[d];
sum[d] += fp(x);
}
ans += res * fp(x);
}
niu::fout << ans.value() << "\n";
}
| replace | 167 | 168 | 167 | 168 | 0 | |
p02905 | C++ | Time Limit Exceeded | /**
* author: otera
**/
#include <algorithm>
#include <bitset>
#include <cassert>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
// #define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef long double ld;
const int inf = 1e9 + 7;
const ll INF = 1LL << 60;
const ll mod = 1e9 + 7;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<int, int> P;
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(), c.end()
#define pb push_back
#define debug(x) cerr << #x << " = " << (x) << endl;
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;
}
// modint: mod 計算を int を扱うように扱える構造体
template <int MOD> struct Fp {
long long val;
constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
if (val < 0)
val += MOD;
}
constexpr int getmod() { return MOD; }
constexpr Fp operator-() const noexcept { return val ? MOD - val : 0; }
constexpr Fp operator+(const Fp &r) const noexcept { return Fp(*this) += r; }
constexpr Fp operator-(const Fp &r) const noexcept { return Fp(*this) -= r; }
constexpr Fp operator*(const Fp &r) const noexcept { return Fp(*this) *= r; }
constexpr Fp operator/(const Fp &r) const noexcept { return Fp(*this) /= r; }
constexpr Fp &operator+=(const Fp &r) noexcept {
val += r.val;
if (val >= MOD)
val -= MOD;
return *this;
}
constexpr Fp &operator-=(const Fp &r) noexcept {
val -= r.val;
if (val < 0)
val += MOD;
return *this;
}
constexpr Fp &operator*=(const Fp &r) noexcept {
val = val * r.val % MOD;
return *this;
}
constexpr Fp &operator/=(const Fp &r) noexcept {
long long a = r.val, b = MOD, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
val = val * u % MOD;
if (val < 0)
val += MOD;
return *this;
}
constexpr bool operator==(const Fp &r) const noexcept {
return this->val == r.val;
}
constexpr bool operator!=(const Fp &r) const noexcept {
return this->val != r.val;
}
friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept {
return os << x.val;
}
friend constexpr istream &operator>>(istream &is, Fp<MOD> &x) noexcept {
return is >> x.val;
}
friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
if (n == 0)
return 1;
auto t = modpow(a, n / 2);
t = t * t;
if (n & 1)
t = t * a;
return t;
}
};
// 二項係数ライブラリ
template <class T> struct BiCoef {
vector<T> fact_, inv_, finv_;
constexpr BiCoef() {}
constexpr BiCoef(int n) noexcept : fact_(n, 1), inv_(n, 1), finv_(n, 1) {
init(n);
}
constexpr void init(int n) noexcept {
fact_.assign(n, 1), inv_.assign(n, 1), finv_.assign(n, 1);
int MOD = fact_[0].getmod();
for (int i = 2; i < n; i++) {
fact_[i] = fact_[i - 1] * i;
inv_[i] = -inv_[MOD % i] * (MOD / i);
finv_[i] = finv_[i - 1] * inv_[i];
}
}
constexpr T com(int n, int k) const noexcept {
if (n < k || n < 0 || k < 0)
return 0;
return fact_[n] * finv_[k] * finv_[n - k];
}
constexpr T fact(int n) const noexcept {
if (n < 0)
return 0;
return fact_[n];
}
constexpr T inv(int n) const noexcept {
if (n < 0)
return 0;
return inv_[n];
}
constexpr T finv(int n) const noexcept {
if (n < 0)
return 0;
return finv_[n];
}
};
// const int MOD = 1000000007;
const int MOD = 998244353;
using mint = Fp<MOD>;
BiCoef<mint> bc;
const int MAX = 10001000;
void solve() {
int n;
cin >> n;
vector<int> a(n);
vector<int> cnt(MAX, 0);
rep(i, n) {
cin >> a[i];
cnt[a[i]]++;
}
vector<mint> w(MAX, 0);
for (int d = 1; d < MAX; ++d) {
w[d] += (mint)1 / (mint)d;
for (int j = 2 * d; j < MAX; j += d) {
w[j] -= w[d];
}
}
mint ans = 0;
for (int d = 1; d < MAX; ++d) {
mint X = 0, Y = 0;
for (int j = d; j < MAX; j += d) {
X += (mint)j * (mint)cnt[j];
Y += (mint)j * (mint)j * (mint)cnt[j];
}
mint res = (X * X - Y) / (mint)2;
ans += w[d] * res;
}
cout << ans << endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
// cout << fixed << setprecision(10);
// int t; cin >> t; rep(i, t)solve();
solve();
return 0;
} | /**
* author: otera
**/
#include <algorithm>
#include <bitset>
#include <cassert>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
// #define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef long double ld;
const int inf = 1e9 + 7;
const ll INF = 1LL << 60;
const ll mod = 1e9 + 7;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<int, int> P;
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(), c.end()
#define pb push_back
#define debug(x) cerr << #x << " = " << (x) << endl;
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;
}
// modint: mod 計算を int を扱うように扱える構造体
template <int MOD> struct Fp {
long long val;
constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
if (val < 0)
val += MOD;
}
constexpr int getmod() { return MOD; }
constexpr Fp operator-() const noexcept { return val ? MOD - val : 0; }
constexpr Fp operator+(const Fp &r) const noexcept { return Fp(*this) += r; }
constexpr Fp operator-(const Fp &r) const noexcept { return Fp(*this) -= r; }
constexpr Fp operator*(const Fp &r) const noexcept { return Fp(*this) *= r; }
constexpr Fp operator/(const Fp &r) const noexcept { return Fp(*this) /= r; }
constexpr Fp &operator+=(const Fp &r) noexcept {
val += r.val;
if (val >= MOD)
val -= MOD;
return *this;
}
constexpr Fp &operator-=(const Fp &r) noexcept {
val -= r.val;
if (val < 0)
val += MOD;
return *this;
}
constexpr Fp &operator*=(const Fp &r) noexcept {
val = val * r.val % MOD;
return *this;
}
constexpr Fp &operator/=(const Fp &r) noexcept {
long long a = r.val, b = MOD, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
val = val * u % MOD;
if (val < 0)
val += MOD;
return *this;
}
constexpr bool operator==(const Fp &r) const noexcept {
return this->val == r.val;
}
constexpr bool operator!=(const Fp &r) const noexcept {
return this->val != r.val;
}
friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept {
return os << x.val;
}
friend constexpr istream &operator>>(istream &is, Fp<MOD> &x) noexcept {
return is >> x.val;
}
friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
if (n == 0)
return 1;
auto t = modpow(a, n / 2);
t = t * t;
if (n & 1)
t = t * a;
return t;
}
};
// 二項係数ライブラリ
template <class T> struct BiCoef {
vector<T> fact_, inv_, finv_;
constexpr BiCoef() {}
constexpr BiCoef(int n) noexcept : fact_(n, 1), inv_(n, 1), finv_(n, 1) {
init(n);
}
constexpr void init(int n) noexcept {
fact_.assign(n, 1), inv_.assign(n, 1), finv_.assign(n, 1);
int MOD = fact_[0].getmod();
for (int i = 2; i < n; i++) {
fact_[i] = fact_[i - 1] * i;
inv_[i] = -inv_[MOD % i] * (MOD / i);
finv_[i] = finv_[i - 1] * inv_[i];
}
}
constexpr T com(int n, int k) const noexcept {
if (n < k || n < 0 || k < 0)
return 0;
return fact_[n] * finv_[k] * finv_[n - k];
}
constexpr T fact(int n) const noexcept {
if (n < 0)
return 0;
return fact_[n];
}
constexpr T inv(int n) const noexcept {
if (n < 0)
return 0;
return inv_[n];
}
constexpr T finv(int n) const noexcept {
if (n < 0)
return 0;
return finv_[n];
}
};
// const int MOD = 1000000007;
const int MOD = 998244353;
using mint = Fp<MOD>;
BiCoef<mint> bc;
const int MAX = 1000100;
void solve() {
int n;
cin >> n;
vector<int> a(n);
vector<int> cnt(MAX, 0);
rep(i, n) {
cin >> a[i];
cnt[a[i]]++;
}
vector<mint> w(MAX, 0);
for (int d = 1; d < MAX; ++d) {
w[d] += (mint)1 / (mint)d;
for (int j = 2 * d; j < MAX; j += d) {
w[j] -= w[d];
}
}
mint ans = 0;
for (int d = 1; d < MAX; ++d) {
mint X = 0, Y = 0;
for (int j = d; j < MAX; j += d) {
X += (mint)j * (mint)cnt[j];
Y += (mint)j * (mint)j * (mint)cnt[j];
}
mint res = (X * X - Y) / (mint)2;
ans += w[d] * res;
}
cout << ans << endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
// cout << fixed << setprecision(10);
// int t; cin >> t; rep(i, t)solve();
solve();
return 0;
} | replace | 175 | 176 | 175 | 176 | TLE | |
p02905 | C++ | Time Limit Exceeded | #include "bits/stdc++.h"
using namespace std;
using ll = long long;
using vll = std::vector<ll>;
using vvll = std::vector<vll>;
using vvvll = std::vector<vvll>;
using dd = double;
using vdd = std::vector<dd>;
using vvdd = std::vector<vdd>;
using vvvdd = std::vector<vvdd>;
constexpr ll INF = 1LL << 60;
constexpr dd EPS = 1e-11;
// cin,cout高速化のおまじない+桁数指定
struct Fast {
Fast() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(std::numeric_limits<double>::max_digits10);
}
} fast;
#define REPS(i, S, E) for (ll i = (S); i <= (E); i++)
#define REP(i, N) REPS(i, 0, N - 1)
#define DEPS(i, S, E) for (ll i = (E); i >= (S); i--)
#define DEP(i, N) DEPS(i, 0, N - 1)
#define EACH(e, v) for (auto &&e : v)
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline T MaxE(vector<T> &v, ll S, ll E) {
T m = v[S];
REPS(i, S, E) chmax(m, v[i]);
return m;
} // v[S]~v[E]の最大値
template <class T> inline T MinE(vector<T> &v, ll S, ll E) {
T m = v[S];
REPS(i, S, E) chmin(m, v[i]);
return m;
} // v[S]~v[E]の最小値
template <class T> inline T MaxE(vector<T> &v, ll N) {
return MaxE(v, 0, N - 1);
} // 先頭N個中の最大値
template <class T> inline T MinE(vector<T> &v, ll N) {
return MinE(v, 0, N - 1);
}
template <class T> inline T MaxE(vector<T> &v) { return MaxE(v, (ll)v.size()); }
template <class T> inline T MinE(vector<T> &v) { return MinE(v, (ll)v.size()); }
template <class T> inline ll MaxI(vector<T> &v, ll S, ll E) {
ll m = S;
REPS(i, S, E) {
if (v[i] > v[m])
m = i;
}
return m;
}
template <class T> inline ll MinI(vector<T> &v, ll S, ll E) {
ll m = S;
REPS(i, S, E) {
if (v[i] < v[m])
m = i;
}
return m;
}
template <class T> inline ll MaxI(vector<T> &v, ll N) {
return MaxI(v, 0, N - 1);
}
template <class T> inline ll MinI(vector<T> &v, ll N) {
return MinI(v, 0, N - 1);
}
template <class T> inline ll MaxI(vector<T> &v) {
return MaxI(v, (ll)v.size());
}
template <class T> inline ll MinI(vector<T> &v) {
return MinI(v, (ll)v.size());
}
template <class T> inline T Sum(vector<T> &v, ll S, ll E) {
T s = v[S];
REPS(i, S + 1, E) s += v[i];
return s;
}
template <class T> inline T Sum(vector<T> &v, ll N) { return Sum(v, 0, N - 1); }
template <class T> inline T Sum(vector<T> &v) { return Sum(v, v.size()); }
template <class T> inline T POW(T a, ll n) {
T r = 1;
for (; n > 0; n >>= 1, a *= a) {
if (n & 1)
r *= a;
}
return r;
}
inline ll POW(int a, ll n) { return POW((ll)a, n); }
inline ll MSB(ll a) {
for (ll o = 63, x = -1;;) {
ll m = (o + x) / 2;
if (a < (1LL << m))
o = m;
else
x = m;
if (o - x == 1)
return x;
}
}
inline ll CEIL(ll a, ll b) { return (a + b - 1) / b; }
template <class T = ll> inline vector<T> cinv(ll N) {
vector<T> v(N);
REP(i, N) cin >> v[i];
return move(v);
}
template <class T = ll, class S = ll> inline vector<pair<T, S>> cinv2(ll N) {
vector<pair<T, S>> v(N);
REP(i, N) { cin >> v[i].first >> v[i].second; }
return move(v);
}
template <class T = ll, class S = ll, class R = ll>
inline vector<tuple<T, S, R>> cinv3(ll N) {
vector<tuple<T, S, R>> v(N);
REP(i, N) { cin >> get<0>(v[i]) >> get<1>(v[i]) >> get<2>(v[i]); }
return move(v);
}
template <class T = ll, class S = ll, class R = ll, class Q = ll>
inline vector<tuple<T, S, R, Q>> cinv4(ll N) {
vector<tuple<T, S, R, Q>> v(N);
REP(i, N) {
cin >> get<0>(v[i]) >> get<1>(v[i]) >> get<2>(v[i]) >> get<3>(v[i]);
}
return move(v);
}
template <class T> inline void coutv(vector<T> &v, char deli = ' ') {
ll N = (ll)v.size();
REP(i, N) { cout << v[i] << ((i == N - 1) ? '\n' : deli); }
}
template <class T> void bye(T a) {
cout << a << '\n';
exit(0);
}
struct mll {
static ll MOD;
ll val;
mll(ll v = 0) : val(v % MOD) {
if (val < 0)
val += MOD;
}
mll operator-() const { return -val; }
mll operator+(const mll &b) const { return val + b.val; }
mll operator-(const mll &b) const { return val - b.val; }
mll operator*(const mll &b) const { return val * b.val; }
mll operator/(const mll &b) const { return mll(*this) /= b; }
mll operator+(ll b) const { return *this + mll(b); }
mll operator-(ll b) const { return *this - mll(b); }
mll operator*(ll b) const { return *this * mll(b); }
friend mll operator+(ll a, const mll &b) { return b + a; }
friend mll operator-(ll a, const mll &b) { return -b + a; }
friend mll operator*(ll a, const mll &b) { return b * a; }
mll &operator+=(const mll &b) {
val = (val + b.val) % MOD;
return *this;
}
mll &operator-=(const mll &b) {
val = (val + MOD - b.val) % MOD;
return *this;
}
mll &operator*=(const mll &b) {
val = (val * b.val) % MOD;
return *this;
}
mll &operator/=(const mll &b) {
ll c = b.val, d = MOD, u = 1, v = 0;
while (d) {
ll t = c / d;
c -= t * d;
swap(c, d);
u -= t * v;
swap(u, v);
}
val = val * u % MOD;
if (val < 0)
val += MOD;
return *this;
}
mll &operator+=(ll b) { return *this += mll(b); }
mll &operator-=(ll b) { return *this -= mll(b); }
mll &operator*=(ll b) { return *this *= mll(b); }
mll &operator/=(ll b) { return *this /= mll(b); }
bool operator==(const mll &b) { return val == b.val; }
bool operator!=(const mll &b) { return val != b.val; }
bool operator==(ll b) { return *this == mll(b); }
bool operator!=(ll b) { return *this != mll(b); }
friend bool operator==(ll a, const mll &b) { return mll(a) == b.val; }
friend bool operator!=(ll a, const mll &b) { return mll(a) != b.val; }
friend ostream &operator<<(ostream &os, const mll &a) { return os << a.val; }
friend istream &operator>>(istream &is, mll &a) { return is >> a.val; }
static mll Combination(ll a, ll b) {
chmin(b, a - b);
if (b < 0)
return mll(0);
mll c = 1;
REP(i, b) c *= a - i;
REP(i, b) c /= i + 1;
return c;
}
};
ll mll::MOD = 998244353LL; // 998244353LL;//(ll)(1e9 + 7);
using vmll = std::vector<mll>;
using vvmll = std::vector<vmll>;
using vvvmll = std::vector<vvmll>;
using vvvvmll = std::vector<vvvmll>;
ll M = 1000000;
void solve() {
ll N;
cin >> N;
vll A = cinv(N);
// 予め1~100万の約数を求めておく
vvll ykVec(M + 1);
EACH(v, ykVec) v.reserve(24);
REPS(i, 1, M) {
REPS(k, 1, M) {
if (i * k > M)
break;
ykVec[i * k].push_back(i);
}
}
vmll sum(M + 1, 0); // sum[i] iの倍数のみの和
vmll sum2(M + 1, 0); // sum2[i] iの倍数のみの2乗和
EACH(a, A) {
EACH(yk, ykVec[a]) {
sum[yk] += a;
sum2[yk] += (a * a);
}
}
vmll sekiSum(M + 1);
REPS(i, 1, M) { sekiSum[i] = sum[i] * sum[i] - sum2[i]; }
vmll lcmSum(M + 1, 0); // lcmSum[i] gcdがiの組のlcmの和
DEPS(i, 1, M) {
lcmSum[i] = sekiSum[i];
REPS(k, 2, M) {
if (i * k > M)
break;
lcmSum[i] -= lcmSum[i * k];
}
}
REPS(i, 1, M) lcmSum[i] /= i;
mll ans = Sum(lcmSum, 1, M);
cout << ans / 2 << '\n';
}
int main() {
solve();
return 0;
}
| #include "bits/stdc++.h"
using namespace std;
using ll = long long;
using vll = std::vector<ll>;
using vvll = std::vector<vll>;
using vvvll = std::vector<vvll>;
using dd = double;
using vdd = std::vector<dd>;
using vvdd = std::vector<vdd>;
using vvvdd = std::vector<vvdd>;
constexpr ll INF = 1LL << 60;
constexpr dd EPS = 1e-11;
// cin,cout高速化のおまじない+桁数指定
struct Fast {
Fast() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(std::numeric_limits<double>::max_digits10);
}
} fast;
#define REPS(i, S, E) for (ll i = (S); i <= (E); i++)
#define REP(i, N) REPS(i, 0, N - 1)
#define DEPS(i, S, E) for (ll i = (E); i >= (S); i--)
#define DEP(i, N) DEPS(i, 0, N - 1)
#define EACH(e, v) for (auto &&e : v)
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline T MaxE(vector<T> &v, ll S, ll E) {
T m = v[S];
REPS(i, S, E) chmax(m, v[i]);
return m;
} // v[S]~v[E]の最大値
template <class T> inline T MinE(vector<T> &v, ll S, ll E) {
T m = v[S];
REPS(i, S, E) chmin(m, v[i]);
return m;
} // v[S]~v[E]の最小値
template <class T> inline T MaxE(vector<T> &v, ll N) {
return MaxE(v, 0, N - 1);
} // 先頭N個中の最大値
template <class T> inline T MinE(vector<T> &v, ll N) {
return MinE(v, 0, N - 1);
}
template <class T> inline T MaxE(vector<T> &v) { return MaxE(v, (ll)v.size()); }
template <class T> inline T MinE(vector<T> &v) { return MinE(v, (ll)v.size()); }
template <class T> inline ll MaxI(vector<T> &v, ll S, ll E) {
ll m = S;
REPS(i, S, E) {
if (v[i] > v[m])
m = i;
}
return m;
}
template <class T> inline ll MinI(vector<T> &v, ll S, ll E) {
ll m = S;
REPS(i, S, E) {
if (v[i] < v[m])
m = i;
}
return m;
}
template <class T> inline ll MaxI(vector<T> &v, ll N) {
return MaxI(v, 0, N - 1);
}
template <class T> inline ll MinI(vector<T> &v, ll N) {
return MinI(v, 0, N - 1);
}
template <class T> inline ll MaxI(vector<T> &v) {
return MaxI(v, (ll)v.size());
}
template <class T> inline ll MinI(vector<T> &v) {
return MinI(v, (ll)v.size());
}
template <class T> inline T Sum(vector<T> &v, ll S, ll E) {
T s = v[S];
REPS(i, S + 1, E) s += v[i];
return s;
}
template <class T> inline T Sum(vector<T> &v, ll N) { return Sum(v, 0, N - 1); }
template <class T> inline T Sum(vector<T> &v) { return Sum(v, v.size()); }
template <class T> inline T POW(T a, ll n) {
T r = 1;
for (; n > 0; n >>= 1, a *= a) {
if (n & 1)
r *= a;
}
return r;
}
inline ll POW(int a, ll n) { return POW((ll)a, n); }
inline ll MSB(ll a) {
for (ll o = 63, x = -1;;) {
ll m = (o + x) / 2;
if (a < (1LL << m))
o = m;
else
x = m;
if (o - x == 1)
return x;
}
}
inline ll CEIL(ll a, ll b) { return (a + b - 1) / b; }
template <class T = ll> inline vector<T> cinv(ll N) {
vector<T> v(N);
REP(i, N) cin >> v[i];
return move(v);
}
template <class T = ll, class S = ll> inline vector<pair<T, S>> cinv2(ll N) {
vector<pair<T, S>> v(N);
REP(i, N) { cin >> v[i].first >> v[i].second; }
return move(v);
}
template <class T = ll, class S = ll, class R = ll>
inline vector<tuple<T, S, R>> cinv3(ll N) {
vector<tuple<T, S, R>> v(N);
REP(i, N) { cin >> get<0>(v[i]) >> get<1>(v[i]) >> get<2>(v[i]); }
return move(v);
}
template <class T = ll, class S = ll, class R = ll, class Q = ll>
inline vector<tuple<T, S, R, Q>> cinv4(ll N) {
vector<tuple<T, S, R, Q>> v(N);
REP(i, N) {
cin >> get<0>(v[i]) >> get<1>(v[i]) >> get<2>(v[i]) >> get<3>(v[i]);
}
return move(v);
}
template <class T> inline void coutv(vector<T> &v, char deli = ' ') {
ll N = (ll)v.size();
REP(i, N) { cout << v[i] << ((i == N - 1) ? '\n' : deli); }
}
template <class T> void bye(T a) {
cout << a << '\n';
exit(0);
}
struct mll {
static ll MOD;
ll val;
mll(ll v = 0) : val(v % MOD) {
if (val < 0)
val += MOD;
}
mll operator-() const { return -val; }
mll operator+(const mll &b) const { return val + b.val; }
mll operator-(const mll &b) const { return val - b.val; }
mll operator*(const mll &b) const { return val * b.val; }
mll operator/(const mll &b) const { return mll(*this) /= b; }
mll operator+(ll b) const { return *this + mll(b); }
mll operator-(ll b) const { return *this - mll(b); }
mll operator*(ll b) const { return *this * mll(b); }
friend mll operator+(ll a, const mll &b) { return b + a; }
friend mll operator-(ll a, const mll &b) { return -b + a; }
friend mll operator*(ll a, const mll &b) { return b * a; }
mll &operator+=(const mll &b) {
val = (val + b.val) % MOD;
return *this;
}
mll &operator-=(const mll &b) {
val = (val + MOD - b.val) % MOD;
return *this;
}
mll &operator*=(const mll &b) {
val = (val * b.val) % MOD;
return *this;
}
mll &operator/=(const mll &b) {
ll c = b.val, d = MOD, u = 1, v = 0;
while (d) {
ll t = c / d;
c -= t * d;
swap(c, d);
u -= t * v;
swap(u, v);
}
val = val * u % MOD;
if (val < 0)
val += MOD;
return *this;
}
mll &operator+=(ll b) { return *this += mll(b); }
mll &operator-=(ll b) { return *this -= mll(b); }
mll &operator*=(ll b) { return *this *= mll(b); }
mll &operator/=(ll b) { return *this /= mll(b); }
bool operator==(const mll &b) { return val == b.val; }
bool operator!=(const mll &b) { return val != b.val; }
bool operator==(ll b) { return *this == mll(b); }
bool operator!=(ll b) { return *this != mll(b); }
friend bool operator==(ll a, const mll &b) { return mll(a) == b.val; }
friend bool operator!=(ll a, const mll &b) { return mll(a) != b.val; }
friend ostream &operator<<(ostream &os, const mll &a) { return os << a.val; }
friend istream &operator>>(istream &is, mll &a) { return is >> a.val; }
static mll Combination(ll a, ll b) {
chmin(b, a - b);
if (b < 0)
return mll(0);
mll c = 1;
REP(i, b) c *= a - i;
REP(i, b) c /= i + 1;
return c;
}
};
ll mll::MOD = 998244353LL; // 998244353LL;//(ll)(1e9 + 7);
using vmll = std::vector<mll>;
using vvmll = std::vector<vmll>;
using vvvmll = std::vector<vvmll>;
using vvvvmll = std::vector<vvvmll>;
ll M = 1000000;
void solve() {
ll N;
cin >> N;
vll A = cinv(N);
// 予め1~100万の約数を求めておく
vvll ykVec(M + 1);
EACH(v, ykVec) v.reserve(24);
REPS(i, 1, M) {
REPS(k, 1, M) {
if (i * k > M)
break;
ykVec[i * k].push_back(i);
}
}
vmll sum(M + 1, 0); // sum[i] iの倍数のみの和
vmll sum2(M + 1, 0); // sum2[i] iの倍数のみの2乗和
EACH(a, A) {
EACH(yk, ykVec[a]) {
sum[yk] += a;
sum2[yk] += (a * a);
}
}
vmll sekiSum(M + 1);
REPS(i, 1, M) { sekiSum[i] = sum[i] * sum[i] - sum2[i]; }
vmll lcmSum(M + 1, 0); // lcmSum[i] gcdがiの組のlcmの和
DEPS(i, 1, M) {
lcmSum[i] = sekiSum[i];
REPS(k, 2, M) {
if (i * k > M)
break;
lcmSum[i] -= lcmSum[i * k];
}
}
REPS(i, 1, M) {
if (lcmSum[i] != 0)
lcmSum[i] /= i;
}
mll ans = Sum(lcmSum, 1, M);
cout << ans / 2 << '\n';
}
int main() {
solve();
return 0;
}
| replace | 264 | 266 | 264 | 268 | TLE | |
p02905 | Python | Runtime Error | import sys
def prepare(n, MOD):
f = 1
factorials = [1]
for m in range(1, n + 1):
f *= m
f %= MOD
factorials.append(f)
inv = pow(f, MOD - 2, MOD)
invs = [1] * (n + 1)
invs[n] = inv
for m in range(n, 1, -1):
inv *= m
inv %= MOD
invs[m - 1] = inv
solo_invs = [0] + [f * i % MOD for f, i in zip(factorials, invs[1:])]
return factorials, invs, solo_invs
def decompose_inverses(solo_invs, MOD):
# 各整数 g に対して、g の約数である各 i について dcm[i] を全て足すと 1/g になるような数列を作成
n = len(solo_invs)
dcm = solo_invs.copy()
for i in range(1, n):
d = dcm[i]
for j in range(2 * i, n, i):
dcm[j] -= d
for i in range(1, n):
dcm[i] %= MOD
return dcm
n, *aaa = map(int, sys.stdin.buffer.read().split())
MOD = 998244353
LIMIT = max(aaa)
count = [0] * (LIMIT + 1)
double = [0] * (LIMIT + 1)
for a in aaa:
count[a] += a
double[a] += a * a
_, _, solo_invs = prepare(LIMIT, MOD)
dcm = decompose_inverses(solo_invs, MOD)
ans = 0
inv2 = solo_invs[2]
for d in range(1, LIMIT + 1):
ans = (ans + dcm[d] * (sum(count[d::d]) ** 2 - sum(double[d::d])) * inv2) % MOD
print(ans)
| import sys
def prepare(n, MOD):
f = 1
factorials = [1]
for m in range(1, n + 1):
f *= m
f %= MOD
factorials.append(f)
inv = pow(f, MOD - 2, MOD)
invs = [1] * (n + 1)
invs[n] = inv
for m in range(n, 1, -1):
inv *= m
inv %= MOD
invs[m - 1] = inv
solo_invs = [0] + [f * i % MOD for f, i in zip(factorials, invs[1:])]
return factorials, invs, solo_invs
def decompose_inverses(solo_invs, MOD):
# 各整数 g に対して、g の約数である各 i について dcm[i] を全て足すと 1/g になるような数列を作成
n = len(solo_invs)
dcm = solo_invs[:]
for i in range(1, n):
d = dcm[i]
for j in range(2 * i, n, i):
dcm[j] -= d
for i in range(1, n):
dcm[i] %= MOD
return dcm
n, *aaa = map(int, sys.stdin.buffer.read().split())
MOD = 998244353
LIMIT = max(aaa)
count = [0] * (LIMIT + 1)
double = [0] * (LIMIT + 1)
for a in aaa:
count[a] += a
double[a] += a * a
_, _, solo_invs = prepare(LIMIT, MOD)
dcm = decompose_inverses(solo_invs, MOD)
ans = 0
inv2 = solo_invs[2]
for d in range(1, LIMIT + 1):
ans = (ans + dcm[d] * (sum(count[d::d]) ** 2 - sum(double[d::d])) * inv2) % MOD
print(ans)
| replace | 26 | 27 | 26 | 27 | 0 | |
p02906 | C++ | Runtime Error | #include <cassert>
#include <cmath>
#include <iostream>
#include <vector>
using ll = long long;
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))
class Dsu {
public:
std::vector<int> mult;
std::vector<int> sz;
Dsu(int n) {
mult.resize(1 + n);
sz.resize(1 + n);
for (int i = 1; i <= n; i++) {
mult[i] = i;
sz[i] = 1;
}
}
int jump(int gala) {
if (gala != mult[gala])
mult[gala] = jump(mult[gala]);
return mult[gala];
}
void unite(int a, int b) {
a = jump(a);
b = jump(b);
if (a == b)
return;
if (sz[b] < sz[a])
std::swap(a, b);
sz[b] += sz[a];
mult[a] = b;
}
int _count(int n) {
int result = 0;
for (int i = 1; i <= n; i++)
if (mult[i] == i)
result++;
return result;
}
};
int main() {
int n, m, q;
std::cin >> n >> m >> q;
std::vector<std::pair<int, int>> cond[2];
for (int i = 1; i <= q; i++) {
int x, y, k;
std::cin >> x >> y >> k;
++x;
++y;
cond[k].push_back({x, y});
}
Dsu dsu(n);
for (int i = 0; i < cond[0].size(); i++) {
int x = cond[0][i].first;
int y = cond[0][i].second;
dsu.unite(x, y);
}
int comp = dsu._count(n);
m -= (n - 1);
if (m <= 1LL * comp * (comp - 1) / 2 - (comp - 1)) {
if (0 < cond[1].size()) {
if (0 == m || comp <= 2) {
std::cout << "No";
return 0;
} else {
for (int i = 0; i < cond[1].size(); i++) {
int x = cond[1][i].first, y = cond[1][i].second;
if (dsu.jump(x) == dsu.jump(y)) {
std::cout << "No";
return 0;
}
}
std::cout << "Yes";
}
} else
std::cout << "Yes";
} else
std::cout << "No";
return 0;
}
| #include <cassert>
#include <cmath>
#include <iostream>
#include <vector>
using ll = long long;
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))
class Dsu {
public:
std::vector<int> mult;
std::vector<int> sz;
Dsu(int n) {
mult.resize(1 + n);
sz.resize(1 + n);
for (int i = 1; i <= n; i++) {
mult[i] = i;
sz[i] = 1;
}
}
int jump(int gala) {
if (gala != mult[gala])
mult[gala] = jump(mult[gala]);
return mult[gala];
}
void unite(int a, int b) {
a = jump(a);
b = jump(b);
if (a == b)
return;
if (sz[b] < sz[a])
std::swap(a, b);
sz[b] += sz[a];
mult[a] = b;
}
int _count(int n) {
int result = 0;
for (int i = 1; i <= n; i++)
if (mult[i] == i)
result++;
return result;
}
};
int main() {
ll n, m, q;
std::cin >> n >> m >> q;
std::vector<std::pair<int, int>> cond[2];
for (int i = 1; i <= q; i++) {
int x, y, k;
std::cin >> x >> y >> k;
++x;
++y;
cond[k].push_back({x, y});
}
Dsu dsu(n);
for (int i = 0; i < cond[0].size(); i++) {
int x = cond[0][i].first;
int y = cond[0][i].second;
dsu.unite(x, y);
}
int comp = dsu._count(n);
m -= (n - 1);
if (m <= 1LL * comp * (comp - 1) / 2 - (comp - 1)) {
if (0 < cond[1].size()) {
if (0 == m || comp <= 2) {
std::cout << "No";
return 0;
} else {
for (int i = 0; i < cond[1].size(); i++) {
int x = cond[1][i].first, y = cond[1][i].second;
if (dsu.jump(x) == dsu.jump(y)) {
std::cout << "No";
return 0;
}
}
std::cout << "Yes";
}
} else
std::cout << "Yes";
} else
std::cout << "No";
return 0;
}
| replace | 46 | 47 | 46 | 47 | 0 | |
p02906 | C++ | Runtime Error | #include "bits/stdc++.h"
using namespace std;
#define int long long
#define rep(i, n) for (short i = 0; i < n; ++i)
typedef pair<int, int> P;
int par[100006], siz[100006];
void init(int n) {
rep(i, n) {
par[i] = i;
siz[i] = 1;
}
}
int find(int x) {
if (par[x] == x)
return x;
return par[x] = find(par[x]);
}
void unite(int x, int y) {
x = find(x), y = find(y);
if (x == y)
return;
if (siz[x] < siz[y])
swap(x, y);
par[y] = x;
siz[x] += siz[y];
}
vector<P> v1;
signed main() {
int n, m, q;
cin >> n >> m >> q;
init(n);
rep(i, q) {
int a, b, c;
cin >> a >> b >> c;
if (c)
v1.push_back(P(a, b));
else
unite(a, b);
}
if (v1.size()) {
set<int> S;
rep(i, n) S.insert(find(i));
if (S.size() <= 2) {
puts("No");
return 0;
}
rep(i, v1.size()) {
if (find(v1[i].first) == find(v1[i].second)) {
puts("No");
return 0;
}
}
int M = 0;
for (auto itr = S.begin(); itr != S.end(); itr++) {
M += siz[find(*itr)] - 1;
}
int z = S.size();
M += z * (z - 1) / 2;
if (n - 1 < m && m <= M)
puts("Yes");
else
puts("No");
} else {
if (m == n - 1)
puts("Yes");
else
puts("No");
}
} | #include "bits/stdc++.h"
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; ++i)
typedef pair<int, int> P;
int par[100006], siz[100006];
void init(int n) {
rep(i, n) {
par[i] = i;
siz[i] = 1;
}
}
int find(int x) {
if (par[x] == x)
return x;
return par[x] = find(par[x]);
}
void unite(int x, int y) {
x = find(x), y = find(y);
if (x == y)
return;
if (siz[x] < siz[y])
swap(x, y);
par[y] = x;
siz[x] += siz[y];
}
vector<P> v1;
signed main() {
int n, m, q;
cin >> n >> m >> q;
init(n);
rep(i, q) {
int a, b, c;
cin >> a >> b >> c;
if (c)
v1.push_back(P(a, b));
else
unite(a, b);
}
if (v1.size()) {
set<int> S;
rep(i, n) S.insert(find(i));
if (S.size() <= 2) {
puts("No");
return 0;
}
rep(i, v1.size()) {
if (find(v1[i].first) == find(v1[i].second)) {
puts("No");
return 0;
}
}
int M = 0;
for (auto itr = S.begin(); itr != S.end(); itr++) {
M += siz[find(*itr)] - 1;
}
int z = S.size();
M += z * (z - 1) / 2;
if (n - 1 < m && m <= M)
puts("Yes");
else
puts("No");
} else {
if (m == n - 1)
puts("Yes");
else
puts("No");
}
} | replace | 3 | 4 | 3 | 4 | 0 | |
p02906 | C++ | Runtime Error | #include <bits/stdc++.h>
#define int long long
#define pii pair<int, int>
#define x1 x1228
#define y1 y1228
#define left left228
#define right right228
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define ff first
#define ss second
#define matr vector<vector<int>>
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
typedef long double ld;
const int maxn = 3e5 + 7, mod = 998244353, MAXN = 1e6 + 7;
const double eps = 1e-9;
const ll inf = 1e18;
mt19937 rnd(time(0));
int n, m, q;
struct edge {
int a, b, c;
};
vector<edge> e;
int pr[maxn];
int siz[maxn];
int get(int a) {
if (pr[a] == a)
return a;
return pr[a] = get(pr[a]);
}
int ctr;
bool unite(int a, int b) {
a = get(a);
b = get(b);
if (a == b)
return 0;
if (siz[a] > siz[b])
swap(a, b);
pr[a] = b;
siz[b] += siz[a];
return 1;
}
void solve() {
cin >> n >> m >> q;
ctr = n;
for (int i = 0; i < n; ++i) {
pr[i] = i;
siz[i] = 1;
}
set<pair<pii, int>> kek;
for (int i = 0; i < q; ++i) {
int a, b, c;
cin >> a >> b >> c;
if (a > b)
swap(a, b);
kek.insert(mp(mp(a, b), c));
}
for (auto v : kek) {
e.pb({v.ff.ff, v.ff.ss, v.ss});
}
for (auto v : e) {
if (v.c == 0) {
int t = unite(v.a, v.b);
ctr -= t;
}
}
set<pii> ms;
int ptr = 0;
for (auto v : e) {
if (v.c == 1) {
ptr++;
int a = get(v.a);
int b = get(v.b);
if (a == b) {
cout << "No";
return;
}
if (a > b)
swap(a, b);
ms.insert(mp(a, b));
}
}
if (ms.size() != ptr) {
cout << "No";
return;
}
exit(1);
if (ctr == 2) {
if (ptr)
cout << "No";
else
cout << "Yes";
return;
}
if (m == n - 1) {
if (ptr)
cout << "No";
else
cout << "Yes";
return;
}
if (m <= ctr * (ctr - 1) / 2 + n - ctr) {
cout << "Yes";
return;
}
cout << "No";
}
signed main() {
#ifdef LOCAL
freopen("TASK.in", "r", stdin);
freopen("TASK.out", "w", stdout);
#else
#endif // LOCAL
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.precision(20);
cout << fixed;
int t = 1;
for (int i = 0; i < t; ++i) {
solve();
}
return 0;
} | #include <bits/stdc++.h>
#define int long long
#define pii pair<int, int>
#define x1 x1228
#define y1 y1228
#define left left228
#define right right228
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define ff first
#define ss second
#define matr vector<vector<int>>
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
typedef long double ld;
const int maxn = 3e5 + 7, mod = 998244353, MAXN = 1e6 + 7;
const double eps = 1e-9;
const ll inf = 1e18;
mt19937 rnd(time(0));
int n, m, q;
struct edge {
int a, b, c;
};
vector<edge> e;
int pr[maxn];
int siz[maxn];
int get(int a) {
if (pr[a] == a)
return a;
return pr[a] = get(pr[a]);
}
int ctr;
bool unite(int a, int b) {
a = get(a);
b = get(b);
if (a == b)
return 0;
if (siz[a] > siz[b])
swap(a, b);
pr[a] = b;
siz[b] += siz[a];
return 1;
}
void solve() {
cin >> n >> m >> q;
ctr = n;
for (int i = 0; i < n; ++i) {
pr[i] = i;
siz[i] = 1;
}
set<pair<pii, int>> kek;
for (int i = 0; i < q; ++i) {
int a, b, c;
cin >> a >> b >> c;
if (a > b)
swap(a, b);
kek.insert(mp(mp(a, b), c));
}
for (auto v : kek) {
e.pb({v.ff.ff, v.ff.ss, v.ss});
}
for (auto v : e) {
if (v.c == 0) {
int t = unite(v.a, v.b);
ctr -= t;
}
}
set<pii> ms;
int ptr = 0;
for (auto v : e) {
if (v.c == 1) {
ptr++;
int a = get(v.a);
int b = get(v.b);
if (a == b) {
cout << "No";
return;
}
if (a > b)
swap(a, b);
ms.insert(mp(a, b));
}
}
if (ctr == 2) {
if (ptr)
cout << "No";
else
cout << "Yes";
return;
}
if (m == n - 1) {
if (ptr)
cout << "No";
else
cout << "Yes";
return;
}
if (m <= ctr * (ctr - 1) / 2 + n - ctr) {
cout << "Yes";
return;
}
cout << "No";
}
signed main() {
#ifdef LOCAL
freopen("TASK.in", "r", stdin);
freopen("TASK.out", "w", stdout);
#else
#endif // LOCAL
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.precision(20);
cout << fixed;
int t = 1;
for (int i = 0; i < t; ++i) {
solve();
}
return 0;
} | delete | 100 | 105 | 100 | 100 | 1 | |
p02906 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using int64 = long long;
const int mod = 998244353;
const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;
struct IoSetup {
IoSetup() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
cerr << fixed << setprecision(10);
}
} iosetup;
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
os << p.first << " " << p.second;
return os;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
is >> p.first >> p.second;
return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
for (int i = 0; i < (int)v.size(); i++) {
os << v[i] << (i + 1 != v.size() ? " " : "");
}
return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
for (T &in : v)
is >> in;
return is;
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
return a > b && (a = b, true);
}
template <typename T = int64> vector<T> make_v(size_t a) {
return vector<T>(a);
}
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
t = v;
}
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
for (auto &e : t)
fill_v(e, v);
}
template <typename F> struct FixPoint : F {
FixPoint(F &&f) : F(forward<F>(f)) {}
template <typename... Args> decltype(auto) operator()(Args &&...args) const {
return F::operator()(*this, forward<Args>(args)...);
}
};
template <typename F> inline decltype(auto) MFP(F &&f) {
return FixPoint<F>{forward<F>(f)};
}
struct UnionFind {
vector<int> data;
UnionFind(int sz) { data.assign(sz, -1); }
bool unite(int x, int y) {
x = find(x), y = find(y);
if (x == y)
return (false);
if (data[x] > data[y])
swap(x, y);
data[x] += data[y];
data[y] = x;
return (true);
}
int find(int k) {
if (data[k] < 0)
return (k);
return (data[k] = find(data[k]));
}
int size(int k) { return (-data[find(k)]); }
};
int main() {
int64 N, M, Q;
cin >> N >> M >> Q;
UnionFind uf(N);
vector<int> A(N), B(N), C(N);
for (int i = 0; i < Q; i++) {
cin >> A[i] >> B[i] >> C[i];
}
int used = 0;
for (int i = 0; i < Q; i++) {
if (C[i] == 0)
if (uf.unite(A[i], B[i]))
++used;
}
auto Yes = []() {
cout << "Yes" << endl;
exit(0);
};
auto No = []() {
cout << "No" << endl;
exit(0);
};
for (int i = 0; i < Q; i++) {
if (C[i] == 1) {
if (uf.find(A[i]) == uf.find(B[i]))
No();
}
}
vector<int> rot;
for (int i = 0; i < N; i++) {
if (uf.find(i) == i)
rot.emplace_back(i);
}
if (rot.size() == 1) {
if (used == M)
Yes();
else
No();
} else if (rot.size() == 2) {
for (int i = 0; i < Q; i++) {
if (C[i] == 1)
No();
}
++used;
if (used == M)
Yes();
else
No();
} else {
UnionFind uf2(N);
int64 low = used, high = used;
set<int> appear;
for (int i = 0; i < Q; i++) {
if (C[i] == 1) {
appear.emplace(uf.find(A[i]));
appear.emplace(uf.find(B[i]));
}
}
int64 v = rot.size();
high += v * (v - 1) / 2;
low += appear.size();
if (low <= M && M <= high)
Yes();
else
No();
}
}
| #include <bits/stdc++.h>
using namespace std;
using int64 = long long;
const int mod = 998244353;
const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;
struct IoSetup {
IoSetup() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
cerr << fixed << setprecision(10);
}
} iosetup;
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
os << p.first << " " << p.second;
return os;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
is >> p.first >> p.second;
return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
for (int i = 0; i < (int)v.size(); i++) {
os << v[i] << (i + 1 != v.size() ? " " : "");
}
return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
for (T &in : v)
is >> in;
return is;
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
return a > b && (a = b, true);
}
template <typename T = int64> vector<T> make_v(size_t a) {
return vector<T>(a);
}
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
t = v;
}
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
for (auto &e : t)
fill_v(e, v);
}
template <typename F> struct FixPoint : F {
FixPoint(F &&f) : F(forward<F>(f)) {}
template <typename... Args> decltype(auto) operator()(Args &&...args) const {
return F::operator()(*this, forward<Args>(args)...);
}
};
template <typename F> inline decltype(auto) MFP(F &&f) {
return FixPoint<F>{forward<F>(f)};
}
struct UnionFind {
vector<int> data;
UnionFind(int sz) { data.assign(sz, -1); }
bool unite(int x, int y) {
x = find(x), y = find(y);
if (x == y)
return (false);
if (data[x] > data[y])
swap(x, y);
data[x] += data[y];
data[y] = x;
return (true);
}
int find(int k) {
if (data[k] < 0)
return (k);
return (data[k] = find(data[k]));
}
int size(int k) { return (-data[find(k)]); }
};
int main() {
int64 N, M, Q;
cin >> N >> M >> Q;
UnionFind uf(N);
vector<int> A(Q), B(Q), C(Q);
for (int i = 0; i < Q; i++) {
cin >> A[i] >> B[i] >> C[i];
}
int used = 0;
for (int i = 0; i < Q; i++) {
if (C[i] == 0)
if (uf.unite(A[i], B[i]))
++used;
}
auto Yes = []() {
cout << "Yes" << endl;
exit(0);
};
auto No = []() {
cout << "No" << endl;
exit(0);
};
for (int i = 0; i < Q; i++) {
if (C[i] == 1) {
if (uf.find(A[i]) == uf.find(B[i]))
No();
}
}
vector<int> rot;
for (int i = 0; i < N; i++) {
if (uf.find(i) == i)
rot.emplace_back(i);
}
if (rot.size() == 1) {
if (used == M)
Yes();
else
No();
} else if (rot.size() == 2) {
for (int i = 0; i < Q; i++) {
if (C[i] == 1)
No();
}
++used;
if (used == M)
Yes();
else
No();
} else {
UnionFind uf2(N);
int64 low = used, high = used;
set<int> appear;
for (int i = 0; i < Q; i++) {
if (C[i] == 1) {
appear.emplace(uf.find(A[i]));
appear.emplace(uf.find(B[i]));
}
}
int64 v = rot.size();
high += v * (v - 1) / 2;
low += appear.size();
if (low <= M && M <= high)
Yes();
else
No();
}
}
| replace | 112 | 113 | 112 | 113 | 0 | |
p02906 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<typename T> using gpp_set = tree<T, null_type, less<T>, rb_tree_tag,
tree_order_statistics_node_update>; template<typename T, typename L> using
gpp_map = tree<T, L, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T> using gpp_multiset = tree<T, null_type, less_equal<T>,
rb_tree_tag, tree_order_statistics_node_update>;*/
struct fast_ios {
fast_ios() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
};
} fast_ios_;
#define FOR(i, begin, end) for (int i = (begin); i < (end); i++)
#define REP(i, n) FOR(i, 0, n)
#define IFOR(i, begin, end) for (int i = (end)-1; i >= (begin); i--)
#define IREP(i, n) IFOR(i, 0, n)
#define Sort(v) sort(v.begin(), v.end())
#define Reverse(v) reverse(v.begin(), v.end())
#define all(v) v.begin(), v.end()
#define SZ(v) ((int)v.size())
#define Lower_bound(v, x) \
distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x) \
distance(v.begin(), upper_bound(v.begin(), v.end(), x))
#define Max(a, b) a = max(a, b)
#define Min(a, b) a = min(a, b)
#define bit(n) (1LL << (n))
#define bit_exist(x, n) ((x >> n) & 1)
#define debug(x) cout << #x << "=" << x << endl;
#define vdebug(v) \
cout << #v << "=" << endl; \
REP(i_debug, v.size()) { cout << v[i_debug] << ","; } \
cout << endl;
#define mdebug(m) \
cout << #m << "=" << endl; \
REP(i_debug, m.size()) { \
REP(j_debug, m[i_debug].size()) { cout << m[i_debug][j_debug] << ","; } \
cout << endl; \
}
#define pb push_back
#define f first
#define s second
#define int long long
#define INF 1000000000000000000
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
for (auto &x : v)
is >> x;
return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &v) {
for (int i = 0; i < v.size(); i++) {
cout << v[i];
if (i != v.size() - 1)
cout << endl;
};
return os;
}
template <typename T> void Out(T x) { cout << x << endl; }
template <typename T1, typename T2> void Ans(bool f, T1 y, T2 n) {
if (f)
Out(y);
else
Out(n);
}
using vec = vector<int>;
using mat = vector<vec>;
using Pii = pair<int, int>;
using PiP = pair<int, Pii>;
using PPi = pair<Pii, int>;
using bools = vector<bool>;
using pairs = vector<Pii>;
// int dx[4] = {1,0,-1,0};
// int dy[4] = {0,1,0,-1};
// char d[4] = {'D','R','U','L'};
const int mod = 1000000007;
// const int mod = 998244353;
// #define Add(x, y) x = (x + (y)) % mod
// #define Mult(x, y) x = (x * (y)) % mod
struct UnionFind {
vector<int> par, sz;
int n_cc;
UnionFind(int N) {
par = vector<int>(N);
REP(i, N) par[i] = i;
sz = vector<int>(N, 1);
n_cc = N;
}
int find(int x) {
if (par[x] == x)
return x;
else
return par[x] = find(par[x]);
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return;
if (sz[x] < sz[y])
swap(x, y);
par[y] = x;
sz[x] += sz[y];
n_cc--;
}
bool is_union(int x, int y) {
x = find(x);
y = find(y);
return x == y;
}
int union_size(int x) { return sz[find(x)]; }
};
signed main() {
int N, M, Q;
cin >> N >> M >> Q;
vec A(Q), B(Q), C(Q);
UnionFind UF(N);
REP(i, Q) {
cin >> A[i] >> B[i] >> C[i];
if (C[i] == 0) {
/*
if(UF.is_union(A[i], B[i])){
Out("No");
return 0;
}else{
UF.unite(A[i], B[i]);
}*/
UF.unite(A[i], B[i]);
}
}
// debug(UF.n_cc);
bool f1 = false;
REP(i, Q) if (C[i] == 1) {
f1 = true;
if (UF.is_union(A[i], B[i])) {
Out("No");
return 0;
}
}
if (f1 && UF.n_cc == 2) {
Out("No");
} else {
assert(false);
Out("Yes");
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<typename T> using gpp_set = tree<T, null_type, less<T>, rb_tree_tag,
tree_order_statistics_node_update>; template<typename T, typename L> using
gpp_map = tree<T, L, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T> using gpp_multiset = tree<T, null_type, less_equal<T>,
rb_tree_tag, tree_order_statistics_node_update>;*/
struct fast_ios {
fast_ios() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
};
} fast_ios_;
#define FOR(i, begin, end) for (int i = (begin); i < (end); i++)
#define REP(i, n) FOR(i, 0, n)
#define IFOR(i, begin, end) for (int i = (end)-1; i >= (begin); i--)
#define IREP(i, n) IFOR(i, 0, n)
#define Sort(v) sort(v.begin(), v.end())
#define Reverse(v) reverse(v.begin(), v.end())
#define all(v) v.begin(), v.end()
#define SZ(v) ((int)v.size())
#define Lower_bound(v, x) \
distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x) \
distance(v.begin(), upper_bound(v.begin(), v.end(), x))
#define Max(a, b) a = max(a, b)
#define Min(a, b) a = min(a, b)
#define bit(n) (1LL << (n))
#define bit_exist(x, n) ((x >> n) & 1)
#define debug(x) cout << #x << "=" << x << endl;
#define vdebug(v) \
cout << #v << "=" << endl; \
REP(i_debug, v.size()) { cout << v[i_debug] << ","; } \
cout << endl;
#define mdebug(m) \
cout << #m << "=" << endl; \
REP(i_debug, m.size()) { \
REP(j_debug, m[i_debug].size()) { cout << m[i_debug][j_debug] << ","; } \
cout << endl; \
}
#define pb push_back
#define f first
#define s second
#define int long long
#define INF 1000000000000000000
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
for (auto &x : v)
is >> x;
return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &v) {
for (int i = 0; i < v.size(); i++) {
cout << v[i];
if (i != v.size() - 1)
cout << endl;
};
return os;
}
template <typename T> void Out(T x) { cout << x << endl; }
template <typename T1, typename T2> void Ans(bool f, T1 y, T2 n) {
if (f)
Out(y);
else
Out(n);
}
using vec = vector<int>;
using mat = vector<vec>;
using Pii = pair<int, int>;
using PiP = pair<int, Pii>;
using PPi = pair<Pii, int>;
using bools = vector<bool>;
using pairs = vector<Pii>;
// int dx[4] = {1,0,-1,0};
// int dy[4] = {0,1,0,-1};
// char d[4] = {'D','R','U','L'};
const int mod = 1000000007;
// const int mod = 998244353;
// #define Add(x, y) x = (x + (y)) % mod
// #define Mult(x, y) x = (x * (y)) % mod
struct UnionFind {
vector<int> par, sz;
int n_cc;
UnionFind(int N) {
par = vector<int>(N);
REP(i, N) par[i] = i;
sz = vector<int>(N, 1);
n_cc = N;
}
int find(int x) {
if (par[x] == x)
return x;
else
return par[x] = find(par[x]);
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return;
if (sz[x] < sz[y])
swap(x, y);
par[y] = x;
sz[x] += sz[y];
n_cc--;
}
bool is_union(int x, int y) {
x = find(x);
y = find(y);
return x == y;
}
int union_size(int x) { return sz[find(x)]; }
};
signed main() {
int N, M, Q;
cin >> N >> M >> Q;
vec A(Q), B(Q), C(Q);
UnionFind UF(N);
REP(i, Q) {
cin >> A[i] >> B[i] >> C[i];
if (C[i] == 0) {
/*
if(UF.is_union(A[i], B[i])){
Out("No");
return 0;
}else{
UF.unite(A[i], B[i]);
}*/
UF.unite(A[i], B[i]);
}
}
// debug(UF.n_cc);
bool f1 = false;
REP(i, Q) if (C[i] == 1) {
f1 = true;
if (UF.is_union(A[i], B[i])) {
Out("No");
return 0;
}
}
if (f1 && UF.n_cc == 2) {
Out("No");
} else {
int n_tree = UF.n_cc;
int Mmin = N - 1;
if (f1)
Mmin++;
int Mmax = N - n_tree + n_tree * (n_tree - 1) / 2;
if (Mmin <= M && M <= Mmax)
Out("Yes");
else
Out("No");
}
return 0;
}
| replace | 161 | 163 | 161 | 170 | -6 | 252fe9e3-ba2e-48a4-ab7d-7e8046f30eaf.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02906/C++/s591088407.cpp:128: int main(): Assertion `false' failed.
|
p02906 | C++ | Runtime Error | #include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
struct UnionFind {
vector<int> par;
UnionFind(int n) : par(n, -1) {}
int find(int x) {
if (par[x] < 0)
return x;
return par[x] = find(par[x]);
}
bool unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return false;
if (par[x] > par[y]) {
par[y] += par[x];
par[x] = y;
} else {
par[x] += par[y];
par[y] = x;
}
return true;
}
bool same(int x, int y) { return find(x) == find(y); }
int size(int x) { return -par[find(x)]; }
};
int main() {
ll n, m, q;
cin >> n >> m >> q;
vector<pair<int, int>> edge;
UnionFind uf(n);
ll nn = n;
while (q--) {
int x, y, t;
cin >> x >> y >> t;
--x;
--y;
if (t) {
edge.emplace_back(x, y);
} else {
if (uf.unite(x, y)) {
--m;
--nn;
}
}
}
vector<int> used(n);
ll cnt = 0;
for (auto p : edge) {
int x = uf.find(p.first), y = uf.find(p.second);
if (x == y) {
cout << "No" << endl;
return 0;
}
if (!used[x])
++cnt;
if (!used[y])
++cnt;
used[x] = used[y] = 1;
}
if (cnt == 0) {
ll mi = nn - 1, ma = nn * (nn - 1) / 2;
if (mi <= m && m <= ma) {
cout << "Yes" << endl;
} else
cout << "No" << endl;
} else {
ll mi = nn, ma = nn * (nn - 1) / 2;
if (mi <= m && m <= ma) {
cout << "Yes" << endl;
} else
cout << "No" << endl;
}
return 0;
} | #include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
struct UnionFind {
vector<int> par;
UnionFind(int n) : par(n, -1) {}
int find(int x) {
if (par[x] < 0)
return x;
return par[x] = find(par[x]);
}
bool unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return false;
if (par[x] > par[y]) {
par[y] += par[x];
par[x] = y;
} else {
par[x] += par[y];
par[y] = x;
}
return true;
}
bool same(int x, int y) { return find(x) == find(y); }
int size(int x) { return -par[find(x)]; }
};
int main() {
ll n, m, q;
cin >> n >> m >> q;
vector<pair<int, int>> edge;
UnionFind uf(n);
ll nn = n;
while (q--) {
int x, y, t;
cin >> x >> y >> t;
if (t) {
edge.emplace_back(x, y);
} else {
if (uf.unite(x, y)) {
--m;
--nn;
}
}
}
vector<int> used(n);
ll cnt = 0;
for (auto p : edge) {
int x = uf.find(p.first), y = uf.find(p.second);
if (x == y) {
cout << "No" << endl;
return 0;
}
if (!used[x])
++cnt;
if (!used[y])
++cnt;
used[x] = used[y] = 1;
}
if (cnt == 0) {
ll mi = nn - 1, ma = nn * (nn - 1) / 2;
if (mi <= m && m <= ma) {
cout << "Yes" << endl;
} else
cout << "No" << endl;
} else {
ll mi = nn, ma = nn * (nn - 1) / 2;
if (mi <= m && m <= ma) {
cout << "Yes" << endl;
} else
cout << "No" << endl;
}
return 0;
} | delete | 59 | 61 | 59 | 59 | 0 | |
p02906 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using UL = unsigned int;
using ULL = unsigned long long;
using LL = long long;
#define rep(i, n) for (UL i = 0; i < (n); i++)
struct UF {
UL N;
vector<UL> V;
UL Z;
void init(UL n) {
N = n;
V.resize(N);
Z = N;
rep(i, N) V[i] = i;
}
UL root(UL a) {
if (V[a] == a)
return a;
return root(V[a]);
}
void unite(UL a, UL b) {
a = root(a), b = root(b);
if (a != b)
Z--;
V[a] = b;
}
};
struct Problem {
public:
LL N, M, Q;
UF F;
vector<pair<UL, UL>> Q1;
vector<pair<UL, UL>> Q2;
void Solve() {
scanf("%lld%lld%lld", &N, &M, &Q);
F.init(N);
rep(i, Q) {
UL a, b, c;
scanf("%u%u%u", &a, &b, &c);
if (c == 0)
Q1.push_back({a, b});
else
Q2.push_back({a, b});
}
for (auto &p : Q1)
F.unite(p.first, p.second);
for (auto &e : Q2)
if (F.root(e.first) == F.root(e.second)) {
printf("No");
return;
}
LL X = F.Z;
if (M > (N - 1) + (X - 2) * (X - 1) / 2) {
printf("No");
return;
}
if (M < (N - 1)) {
printf("No");
return;
}
if (Q2.size()) {
if (M < (N)) {
printf("No");
return;
}
if (F.Z <= 2) {
printf("No");
return;
}
}
printf("Yes");
}
};
int main() {
unique_ptr<Problem> p(new Problem());
p->Solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using UL = unsigned int;
using ULL = unsigned long long;
using LL = long long;
#define rep(i, n) for (UL i = 0; i < (n); i++)
struct UF {
UL N;
vector<UL> V;
UL Z;
void init(UL n) {
N = n;
V.resize(N);
Z = N;
rep(i, N) V[i] = i;
}
UL root(UL a) {
if (V[a] == a)
return a;
return V[a] = root(V[a]);
}
void unite(UL a, UL b) {
a = root(a), b = root(b);
if (a != b)
Z--;
V[a] = b;
}
};
struct Problem {
public:
LL N, M, Q;
UF F;
vector<pair<UL, UL>> Q1;
vector<pair<UL, UL>> Q2;
void Solve() {
scanf("%lld%lld%lld", &N, &M, &Q);
F.init(N);
rep(i, Q) {
UL a, b, c;
scanf("%u%u%u", &a, &b, &c);
if (c == 0)
Q1.push_back({a, b});
else
Q2.push_back({a, b});
}
for (auto &p : Q1)
F.unite(p.first, p.second);
for (auto &e : Q2)
if (F.root(e.first) == F.root(e.second)) {
printf("No");
return;
}
LL X = F.Z;
if (M > (N - 1) + (X - 2) * (X - 1) / 2) {
printf("No");
return;
}
if (M < (N - 1)) {
printf("No");
return;
}
if (Q2.size()) {
if (M < (N)) {
printf("No");
return;
}
if (F.Z <= 2) {
printf("No");
return;
}
}
printf("Yes");
}
};
int main() {
unique_ptr<Problem> p(new Problem());
p->Solve();
return 0;
}
| replace | 20 | 21 | 20 | 21 | TLE | |
p02906 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define popCnt(x) (__builtin_popcountll(x))
typedef long long Long;
const int N = 1e5 + 5;
vector<int> adj[2][N];
int comp[N];
void dfs(int node, int id) {
if (comp[node] == id)
return;
comp[node] = id;
for (int x : adj[0][node]) {
dfs(x, id);
}
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#ifdef Local
freopen("test.in", "r", stdin);
#else
#define endl '\n'
#endif
int n, m, q;
cin >> n >> m >> q;
bool type_1_appeared = false;
while (q--) {
int u, v, c;
cin >> u >> v >> c;
++u, ++v;
adj[c][u].push_back(v);
adj[c][v].push_back(u);
type_1_appeared |= (c == 1);
}
int cnt_comps = 0;
for (int i = 1; i <= n; ++i) {
if (comp[i] == 0) {
dfs(i, i);
++cnt_comps;
}
}
for (int i = 1; i <= n; ++i) {
for (int x : adj[1][i]) {
if (comp[i] == comp[x]) {
cout << "No" << endl;
return 0;
}
}
}
int mn = n;
Long mx = n - cnt_comps + 1LL * cnt_comps * (cnt_comps - 1) / 2;
if (!type_1_appeared) {
mn = n - 1;
}
if (cnt_comps == 2 && type_1_appeared) {
cout << "No" << endl;
return 0;
}
if (mn <= m && m <= mx) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
}
| #include <bits/stdc++.h>
using namespace std;
#define popCnt(x) (__builtin_popcountll(x))
typedef long long Long;
const int N = 1e5 + 5;
vector<int> adj[2][N];
int comp[N];
void dfs(int node, int id) {
if (comp[node] == id)
return;
comp[node] = id;
for (int x : adj[0][node]) {
dfs(x, id);
}
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#ifdef Local
freopen("test.in", "r", stdin);
#else
#define endl '\n'
#endif
int n, q;
Long m;
cin >> n >> m >> q;
bool type_1_appeared = false;
while (q--) {
int u, v, c;
cin >> u >> v >> c;
++u, ++v;
adj[c][u].push_back(v);
adj[c][v].push_back(u);
type_1_appeared |= (c == 1);
}
int cnt_comps = 0;
for (int i = 1; i <= n; ++i) {
if (comp[i] == 0) {
dfs(i, i);
++cnt_comps;
}
}
for (int i = 1; i <= n; ++i) {
for (int x : adj[1][i]) {
if (comp[i] == comp[x]) {
cout << "No" << endl;
return 0;
}
}
}
int mn = n;
Long mx = n - cnt_comps + 1LL * cnt_comps * (cnt_comps - 1) / 2;
if (!type_1_appeared) {
mn = n - 1;
}
if (cnt_comps == 2 && type_1_appeared) {
cout << "No" << endl;
return 0;
}
if (mn <= m && m <= mx) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
}
| replace | 28 | 29 | 28 | 30 | 0 | |
p02906 | C++ | Runtime Error | #include <cstdio>
typedef long long LL;
const int MN = 100005, MQ = 100005;
int N, Q;
LL M;
int pa[MN];
int fp(int x) { return pa[x] ? pa[x] = fp(pa[x]) : x; }
int qu[MQ], qv[MQ], K;
int E;
int main() {
scanf("%d%lld%d", &N, &M, &Q);
for (int i = 1; i <= Q; ++i) {
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
++x, ++y;
if (z == 0) {
if ((x = fp(x)) != (y = fp(y)))
pa[y] = x, ++E;
} else
qu[++K] = x, qv[K] = y;
}
if (!K)
return puts("Yes"), 0;
if (M == N - 1)
return puts("No"), 1;
for (int i = 1; i <= K; ++i)
if ((qu[i] = fp(qu[i])) == (qv[i] = fp(qv[i])))
return puts("No"), 0;
int C = N - E;
if (C == 2)
return puts("No"), 0;
if (M > E + (LL)C * (C - 1) / 2)
return puts("No"), 0;
return puts("Yes"), 0;
return 0;
} | #include <cstdio>
typedef long long LL;
const int MN = 100005, MQ = 100005;
int N, Q;
LL M;
int pa[MN];
int fp(int x) { return pa[x] ? pa[x] = fp(pa[x]) : x; }
int qu[MQ], qv[MQ], K;
int E;
int main() {
scanf("%d%lld%d", &N, &M, &Q);
for (int i = 1; i <= Q; ++i) {
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
++x, ++y;
if (z == 0) {
if ((x = fp(x)) != (y = fp(y)))
pa[y] = x, ++E;
} else
qu[++K] = x, qv[K] = y;
}
if (K && M == N - 1)
return puts("No"), 0;
for (int i = 1; i <= K; ++i)
if ((qu[i] = fp(qu[i])) == (qv[i] = fp(qv[i])))
return puts("No"), 0;
int C = N - E;
if (C == 2)
return puts("No"), 0;
if (M > E + (LL)C * (C - 1) / 2)
return puts("No"), 0;
return puts("Yes"), 0;
return 0;
} | replace | 24 | 28 | 24 | 26 | 0 | |
p02906 | C++ | Runtime Error | #include <bits/stdc++.h>
#define clr(x) memset(x, 0, sizeof x)
#define For(i, a, b) for (int i = (a); i <= (b); i++)
#define Fod(i, b, a) for (int i = (b); i >= (a); i--)
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define fi first
#define se second
#define next Next
#define y1 _y1
#define outval(x) cerr << #x " = " << x << endl
#define outtag(x) cerr << "-----------------" #x "-----------------\n"
#define outarr(a, L, R) \
cerr << #a "[" << L << ".." << R << "] = "; \
For(_x, L, R) cerr << a[_x] << " "; \
cerr << endl;
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> pii;
LL read() {
LL x = 0, f = 0;
char ch = getchar();
while (!isdigit(ch))
f = ch == '-', ch = getchar();
while (isdigit(ch))
x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
return f ? -x : x;
}
const int N = 100005;
int n, q;
LL m;
struct nd {
int a, b, c;
} v[N];
int fa[N];
int getf(int x) { return fa[x] == x ? x : fa[x] = getf(fa[x]); }
int id[N], c[N];
int cnt[N];
int main() {
n = read(), m = read(), q = read();
For(i, 1, q) v[i].a = read() + 1, v[i].b = read() + 1, v[i].c = read();
For(i, 1, n) fa[i] = i;
For(i, 1, q) {
if (v[i].c == 0)
fa[getf(v[i].a)] = getf(v[i].b);
}
For(i, 1,
q) if (v[i].c == 1 && getf(v[i].a) == getf(v[i].b)) return puts("No"),
0;
int c1 = 0;
For(i, 1, n) {
id[i] = getf(i);
c[id[i]]++;
if (id[i] != i)
c1++;
}
if (m < c1)
return puts("No"), 0;
For(i, 1, n) fa[i] = i;
For(i, 1, q) if (v[i].c == 1) fa[getf(id[v[i].a])] = getf(id[v[i].b]);
int s = 0;
For(i, 1, n) if (id[i] == i) cnt[getf(i)]++, s++;
vector<int> v;
For(i, 1, n) if (id[i] == i && getf(i) == i) v.pb(cnt[i]);
sort(v.begin(), v.end());
int f = 0;
if (s == 1)
return puts(m == n - 1 ? "Yes" : "No");
if (s == 2)
return puts("No"), 0;
if (m < n || m > (LL)s * (s - 1) / 2 + c1)
return puts("No"), 0;
puts("Yes");
return 0;
} | #include <bits/stdc++.h>
#define clr(x) memset(x, 0, sizeof x)
#define For(i, a, b) for (int i = (a); i <= (b); i++)
#define Fod(i, b, a) for (int i = (b); i >= (a); i--)
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define fi first
#define se second
#define next Next
#define y1 _y1
#define outval(x) cerr << #x " = " << x << endl
#define outtag(x) cerr << "-----------------" #x "-----------------\n"
#define outarr(a, L, R) \
cerr << #a "[" << L << ".." << R << "] = "; \
For(_x, L, R) cerr << a[_x] << " "; \
cerr << endl;
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> pii;
LL read() {
LL x = 0, f = 0;
char ch = getchar();
while (!isdigit(ch))
f = ch == '-', ch = getchar();
while (isdigit(ch))
x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
return f ? -x : x;
}
const int N = 100005;
int n, q;
LL m;
struct nd {
int a, b, c;
} v[N];
int fa[N];
int getf(int x) { return fa[x] == x ? x : fa[x] = getf(fa[x]); }
int id[N], c[N];
int cnt[N];
int main() {
n = read(), m = read(), q = read();
For(i, 1, q) v[i].a = read() + 1, v[i].b = read() + 1, v[i].c = read();
For(i, 1, n) fa[i] = i;
For(i, 1, q) {
if (v[i].c == 0)
fa[getf(v[i].a)] = getf(v[i].b);
}
For(i, 1,
q) if (v[i].c == 1 && getf(v[i].a) == getf(v[i].b)) return puts("No"),
0;
int c1 = 0;
For(i, 1, n) {
id[i] = getf(i);
c[id[i]]++;
if (id[i] != i)
c1++;
}
if (m < c1)
return puts("No"), 0;
For(i, 1, n) fa[i] = i;
For(i, 1, q) if (v[i].c == 1) fa[getf(id[v[i].a])] = getf(id[v[i].b]);
int s = 0;
For(i, 1, n) if (id[i] == i) cnt[getf(i)]++, s++;
vector<int> v;
For(i, 1, n) if (id[i] == i && getf(i) == i) v.pb(cnt[i]);
sort(v.begin(), v.end());
if (v.back() == 1)
return puts(m == n - 1 ? "Yes" : "No"), 0;
if (s <= 2)
return puts("No"), 0;
if (m < n || m > (LL)s * (s - 1) / 2 + c1)
return puts("No"), 0;
puts("Yes");
return 0;
} | replace | 66 | 70 | 66 | 69 | 0 | |
p02906 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int N = 1e5 + 7;
int p[N], sz[N];
int get(int a) { return (a == p[a] ? a : p[a] = get(p[a])); }
int cmp;
void join(int a, int b) {
a = get(a);
b = get(b);
if (a != b) {
--cmp;
p[a] = b;
sz[b] += sz[a];
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.setf(ios::fixed);
cout.precision(20);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int n, m, q;
cin >> n >> m >> q;
for (int i = 0; i < n; ++i) {
p[i] = i;
sz[i] = 1;
}
cmp = n;
vector<int> a(q), b(q), c(q);
bool fl = false;
for (int i = 0; i < q; ++i) {
cin >> a[i] >> b[i] >> c[i];
if (c[i] == 0) {
join(a[i], b[i]);
} else {
fl = true;
}
}
for (int i = 0; i < q; ++i) {
if (c[i] == 1 && get(a[i]) == get(b[i])) {
cout << "No\n";
return 0;
}
}
ll mn, mx;
if (!fl) {
mn = n - 1;
} else {
mn = n;
}
mx = 0;
vector<int> cm;
for (int i = 0; i < n; ++i)
cm.push_back(get(i));
sort(cm.begin(), cm.end());
cm.resize(unique(cm.begin(), cm.end()) - cm.begin());
for (auto x : cm) {
mx += 2 * (sz[x] - 1);
mx += cmp - 1;
}
mx /= 2;
if (mn <= m && m <= mx) {
cout << "Yes\n";
} else {
cout << "No\n";
}
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int N = 1e5 + 7;
int p[N], sz[N];
int get(int a) { return (a == p[a] ? a : p[a] = get(p[a])); }
int cmp;
void join(int a, int b) {
a = get(a);
b = get(b);
if (a != b) {
--cmp;
p[a] = b;
sz[b] += sz[a];
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.setf(ios::fixed);
cout.precision(20);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int n, q;
ll m;
cin >> n >> m >> q;
for (int i = 0; i < n; ++i) {
p[i] = i;
sz[i] = 1;
}
cmp = n;
vector<int> a(q), b(q), c(q);
bool fl = false;
for (int i = 0; i < q; ++i) {
cin >> a[i] >> b[i] >> c[i];
if (c[i] == 0) {
join(a[i], b[i]);
} else {
fl = true;
}
}
for (int i = 0; i < q; ++i) {
if (c[i] == 1 && get(a[i]) == get(b[i])) {
cout << "No\n";
return 0;
}
}
ll mn, mx;
if (!fl) {
mn = n - 1;
} else {
mn = n;
}
mx = 0;
vector<int> cm;
for (int i = 0; i < n; ++i)
cm.push_back(get(i));
sort(cm.begin(), cm.end());
cm.resize(unique(cm.begin(), cm.end()) - cm.begin());
for (auto x : cm) {
mx += 2 * (sz[x] - 1);
mx += cmp - 1;
}
mx /= 2;
if (mn <= m && m <= mx) {
cout << "Yes\n";
} else {
cout << "No\n";
}
} | replace | 34 | 35 | 34 | 36 | 0 | |
p02907 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define fo(i, l, r) for (int i = l; i <= r; i++)
#define of(i, l, r) for (int i = l; i >= r; i--)
#define fe(i, u) for (int i = head[u]; i; i = e[i].next)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
inline void open(const char *s) {
#ifndef ONLINE_JUDGE
char str[20];
sprintf(str, "%s.in", s);
freopen(str, "r", stdin);
// sprintf(str,"%s.out",s);
// freopen(str,"w",stdout);
#endif
}
inline int rd() {
static int x, f;
x = 0;
f = 1;
char ch = getchar();
for (; ch < '0' || ch > '9'; ch = getchar())
if (ch == '-')
f = -1;
for (; ch >= '0' && ch <= '9'; ch = getchar())
x = x * 10 + ch - '0';
return f > 0 ? x : -x;
}
const int N = 410, mod = 998244353;
int n, a[N], b[N], SA, SB;
int f[2][N][N], rfac[N];
int pw[N][N];
inline int pls(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }
inline int upd(int &a, int b) { return a = pls(a, b); }
inline int mns(int a, int b) { return a < b ? a - b + mod : a - b; }
inline int mul(int a, int b) { return (ll)a * b % mod; }
inline int pwr(int x, int k) {
int res = 1;
for (; k; k >>= 1, x = mul(x, x))
if (k & 1)
res = mul(res, x);
return res;
}
int main() {
open("hh");
n = rd();
fo(i, 1, n) {
a[i] = rd(), b[i] = rd();
SA += a[i];
SB += b[i];
}
rfac[0] = rfac[1] = 1;
fo(i, 2, 400) rfac[i] = mns(0, mul(mod / i, rfac[mod % i]));
fo(i, 2, 400) rfac[i] = mul(rfac[i - 1], rfac[i]);
fo(i, 0, 400) fo(j, 0, 401) pw[i][j] = pwr(i, j);
int u = 0, v = 1;
f[0][0][0] = mod - 1;
fo(i, 1, n) {
fo(j, 0, SA) fo(k, 0, SB) if (f[u][j][k]) {
int tmp = f[u][j][k];
upd(f[v][j][k], tmp);
tmp = mns(0, tmp);
fo(l, 0, b[i] - 1) {
int hh = mul(tmp, mul(pw[a[i]][l], rfac[l]));
upd(f[v][j + a[i]][k + l], hh);
}
}
swap(u, v);
memset(f[v], 0, sizeof f[v]);
}
int ans = 0;
fo(i, 1, SA) fo(j, 0, SB) {
int tmp = f[u][i][j];
// if(tmp)cout<<i<<' '<<j<<' '<<tmp<<'\n';
tmp = mul(tmp, pwr(i, mod - 2));
tmp = mul(tmp, SA);
tmp = mul(tmp, pwr(rfac[j], mod - 2));
tmp = mul(tmp, pwr(pw[i][j], mod - 2));
upd(ans, tmp);
}
cout << ans;
return 0;
}
| #include <bits/stdc++.h>
#define fo(i, l, r) for (int i = l; i <= r; i++)
#define of(i, l, r) for (int i = l; i >= r; i--)
#define fe(i, u) for (int i = head[u]; i; i = e[i].next)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
inline void open(const char *s) {
#ifndef ONLINE_JUDGE
char str[20];
sprintf(str, "%s.in", s);
freopen(str, "r", stdin);
// sprintf(str,"%s.out",s);
// freopen(str,"w",stdout);
#endif
}
inline int rd() {
static int x, f;
x = 0;
f = 1;
char ch = getchar();
for (; ch < '0' || ch > '9'; ch = getchar())
if (ch == '-')
f = -1;
for (; ch >= '0' && ch <= '9'; ch = getchar())
x = x * 10 + ch - '0';
return f > 0 ? x : -x;
}
const int N = 410, mod = 998244353;
int n, a[N], b[N], SA, SB;
int f[2][N][N], rfac[N];
int pw[N][N];
inline int pls(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }
inline int upd(int &a, int b) { return a = pls(a, b); }
inline int mns(int a, int b) { return a < b ? a - b + mod : a - b; }
inline int mul(int a, int b) { return (ll)a * b % mod; }
inline int pwr(int x, int k) {
int res = 1;
for (; k; k >>= 1, x = mul(x, x))
if (k & 1)
res = mul(res, x);
return res;
}
int main() {
n = rd();
fo(i, 1, n) {
a[i] = rd(), b[i] = rd();
SA += a[i];
SB += b[i];
}
rfac[0] = rfac[1] = 1;
fo(i, 2, 400) rfac[i] = mns(0, mul(mod / i, rfac[mod % i]));
fo(i, 2, 400) rfac[i] = mul(rfac[i - 1], rfac[i]);
fo(i, 0, 400) fo(j, 0, 401) pw[i][j] = pwr(i, j);
int u = 0, v = 1;
f[0][0][0] = mod - 1;
fo(i, 1, n) {
fo(j, 0, SA) fo(k, 0, SB) if (f[u][j][k]) {
int tmp = f[u][j][k];
upd(f[v][j][k], tmp);
tmp = mns(0, tmp);
fo(l, 0, b[i] - 1) {
int hh = mul(tmp, mul(pw[a[i]][l], rfac[l]));
upd(f[v][j + a[i]][k + l], hh);
}
}
swap(u, v);
memset(f[v], 0, sizeof f[v]);
}
int ans = 0;
fo(i, 1, SA) fo(j, 0, SB) {
int tmp = f[u][i][j];
// if(tmp)cout<<i<<' '<<j<<' '<<tmp<<'\n';
tmp = mul(tmp, pwr(i, mod - 2));
tmp = mul(tmp, SA);
tmp = mul(tmp, pwr(rfac[j], mod - 2));
tmp = mul(tmp, pwr(pw[i][j], mod - 2));
upd(ans, tmp);
}
cout << ans;
return 0;
}
| delete | 46 | 47 | 46 | 46 | TLE | |
p02907 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define MOD2 1000000007
#define MOD 998244353
#define INF ((1 << 30) - 1)
#define LINF (1LL << 60)
#define EPS (1e-10)
typedef long long Int;
typedef pair<Int, Int> P;
Int fact[1100000];
Int revfact[1100000];
Int rev[1100000];
void init() {
Int m = MOD;
fact[0] = 1;
revfact[0] = 1;
rev[0] = 0;
rev[1] = 1;
for (int i = 1; i < 1100000; i++) {
fact[i] = fact[i - 1] * i % m;
if (i > 1)
rev[i] = MOD / i * (MOD - rev[MOD % i]) % MOD;
revfact[i] = revfact[i - 1] * rev[i] % MOD;
}
}
Int comb[1100][1100];
Int nCk(Int n, Int k) {
if (n < k)
return 0;
if (comb[n][k] != 0)
return comb[n][k];
Int &res = comb[n][k];
return res = fact[n] * revfact[n - k] % MOD * revfact[k] % MOD;
}
Int mod_pow(Int x, Int a, Int m = MOD) {
if (a == 0)
return 1;
Int res = mod_pow(x, a / 2, m);
res = res * res % m;
if (a % 2)
res *= x;
return res % m;
}
Int inv(Int x, Int m = MOD) { return mod_pow(x, m - 2, m); }
Int aall, ball;
Int n, ans;
Int a[440], b[440];
Int dp[440][440][2][2];
int main() {
init();
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i] >> b[i];
aall += a[i];
ball += b[i];
}
dp[0][0][0][0] = 1;
for (int i = 1; i <= n; i++) {
int id = i % 2;
for (int asum = aall; asum >= 0; asum--) {
for (int cnt = 0; cnt <= ball; cnt++) {
for (int p = 0; p < 2; p++) {
if (asum - a[i - 1] < 0)
continue;
Int pw = 1;
for (int use = 0; use < b[i - 1]; use++) {
if (cnt - use >= 0) {
Int tmp = nCk(cnt, use) % MOD * pw % MOD;
dp[asum][cnt][p][0] +=
dp[asum - a[i - 1]][cnt - use][p ^ 1][0] * tmp % MOD;
dp[asum][cnt][p][1] +=
dp[asum - a[i - 1]][cnt - use][p ^ 1][1] * tmp % MOD;
}
pw = pw * a[i - 1] % MOD;
}
if (cnt - b[i - 1] + 1 >= 0)
dp[asum][cnt][p][1] +=
dp[asum - a[i - 1]][cnt - b[i - 1] + 1][p ^ 1][0] *
nCk(cnt, b[i - 1] - 1) % MOD * pw % MOD;
dp[asum][cnt][p][0] %= MOD;
dp[asum][cnt][p][1] %= MOD;
}
}
}
}
for (int asum = 0; asum <= aall; asum++) {
Int rev_asum_pow = 1;
for (int cnt = 0; cnt <= ball; cnt++) {
rev_asum_pow = rev_asum_pow * rev[asum] % MOD;
for (int p = 0; p < 2; p++) {
Int x = dp[asum][cnt][p][1] * rev_asum_pow % MOD * (cnt + 1) % MOD *
aall % MOD * rev[asum] % MOD;
if (p == 1)
ans += x;
else
ans -= x;
ans %= MOD;
}
}
}
if (ans < 0)
ans += MOD;
cout << ans << endl;
return 0;
} | #include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define MOD2 1000000007
#define MOD 998244353
#define INF ((1 << 30) - 1)
#define LINF (1LL << 60)
#define EPS (1e-10)
typedef long long Int;
typedef pair<Int, Int> P;
Int fact[1100000];
Int revfact[1100000];
Int rev[1100000];
void init() {
Int m = MOD;
fact[0] = 1;
revfact[0] = 1;
rev[0] = 0;
rev[1] = 1;
for (int i = 1; i < 1100000; i++) {
fact[i] = fact[i - 1] * i % m;
if (i > 1)
rev[i] = MOD / i * (MOD - rev[MOD % i]) % MOD;
revfact[i] = revfact[i - 1] * rev[i] % MOD;
}
}
Int comb[1100][1100];
Int nCk(Int n, Int k) {
if (n < k)
return 0;
if (comb[n][k] != 0)
return comb[n][k];
Int &res = comb[n][k];
return res = fact[n] * revfact[n - k] % MOD * revfact[k] % MOD;
}
Int mod_pow(Int x, Int a, Int m = MOD) {
if (a == 0)
return 1;
Int res = mod_pow(x, a / 2, m);
res = res * res % m;
if (a % 2)
res *= x;
return res % m;
}
Int inv(Int x, Int m = MOD) { return mod_pow(x, m - 2, m); }
Int aall, ball;
Int n, ans;
Int a[440], b[440];
Int dp[440][440][2][2];
int main() {
init();
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i] >> b[i];
aall += a[i];
ball += b[i];
}
dp[0][0][0][0] = 1;
for (int i = 1; i <= n; i++) {
int id = i % 2;
for (int asum = aall; asum >= 0; asum--) {
for (int cnt = 0; cnt <= ball; cnt++) {
for (int p = 0; p < 2; p++) {
if (asum - a[i - 1] < 0)
continue;
Int pw = 1;
for (int use = 0; use < b[i - 1]; use++) {
if (cnt - use >= 0) {
Int tmp = nCk(cnt, use) % MOD * pw % MOD;
dp[asum][cnt][p][0] +=
dp[asum - a[i - 1]][cnt - use][p ^ 1][0] * tmp % MOD;
dp[asum][cnt][p][1] +=
dp[asum - a[i - 1]][cnt - use][p ^ 1][1] * tmp % MOD;
} else
break;
pw = pw * a[i - 1] % MOD;
}
if (cnt - b[i - 1] + 1 >= 0)
dp[asum][cnt][p][1] +=
dp[asum - a[i - 1]][cnt - b[i - 1] + 1][p ^ 1][0] *
nCk(cnt, b[i - 1] - 1) % MOD * pw % MOD;
dp[asum][cnt][p][0] %= MOD;
dp[asum][cnt][p][1] %= MOD;
}
}
}
}
for (int asum = 0; asum <= aall; asum++) {
Int rev_asum_pow = 1;
for (int cnt = 0; cnt <= ball; cnt++) {
rev_asum_pow = rev_asum_pow * rev[asum] % MOD;
for (int p = 0; p < 2; p++) {
Int x = dp[asum][cnt][p][1] * rev_asum_pow % MOD * (cnt + 1) % MOD *
aall % MOD * rev[asum] % MOD;
if (p == 1)
ans += x;
else
ans -= x;
ans %= MOD;
}
}
}
if (ans < 0)
ans += MOD;
cout << ans << endl;
return 0;
} | replace | 91 | 92 | 91 | 93 | TLE | |
p02907 | C++ | Runtime Error | #include <algorithm>
#include <cstring>
#include <iostream>
#include <numeric>
#include <vector>
constexpr int P = 998'244'353, N = 400;
int n;
int a[N], b[N], factorial[N], invFactorial[N], pw[N];
int dp[2][N][N];
int power(int a, int b) {
int res = 1;
while (b > 0) {
if (b & 1)
res = 1LL * res * a % P;
a = 1LL * a * a % P;
b >>= 1;
}
return res;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> n;
for (int i = 0; i < n; ++i)
std::cin >> a[i] >> b[i];
int s = std::accumulate(a, a + n, 0);
int invS = power(s, P - 2);
int r = std::accumulate(b, b + n, 0) - n;
factorial[0] = 1;
for (int i = 1; i <= r; ++i)
factorial[i] = 1LL * factorial[i - 1] * i % P;
invFactorial[r] = power(factorial[r], P - 2);
for (int i = r; i >= 1; --i)
invFactorial[i - 1] = 1LL * invFactorial[i] * i % P;
int cur = 0;
dp[0][0][0] = P - 1;
for (int i = 0; i < n; ++i) {
cur ^= 1;
std::memset(dp[cur], 0, sizeof(dp[cur]));
pw[0] = 1;
int p = 1LL * a[i] * invS % P;
for (int j = 1; j < b[i]; ++j)
pw[j] = 1LL * pw[j - 1] * p % P;
for (int j = 0; j <= r; ++j) {
for (int k = 0; k <= s; ++k) {
if (dp[cur ^ 1][j][k] == 0)
continue;
dp[cur][j][k] = (dp[cur][j][k] + dp[cur ^ 1][j][k]) % P;
for (int l = 0; l < b[i]; ++l)
dp[cur][j + l][k + a[i]] =
(dp[cur][j + l][k + a[i]] +
1LL * (P - dp[cur ^ 1][j][k]) * invFactorial[l] % P * pw[l]) %
P;
}
}
}
int ans = 0;
for (int i = 0; i <= r; ++i) {
for (int j = 1; j <= s; ++j) {
int inv = power(1LL * j * invS % P, P - 2 - i);
ans = (ans + 1LL * factorial[i] * dp[cur][i][j] % P * inv) % P;
}
}
std::cout << ans << "\n";
return 0;
} | #include <algorithm>
#include <cstring>
#include <iostream>
#include <numeric>
#include <vector>
constexpr int P = 998'244'353, N = 400;
int n;
int a[N], b[N], factorial[N], invFactorial[N], pw[N];
int dp[2][N][N + 1];
int power(int a, int b) {
int res = 1;
while (b > 0) {
if (b & 1)
res = 1LL * res * a % P;
a = 1LL * a * a % P;
b >>= 1;
}
return res;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> n;
for (int i = 0; i < n; ++i)
std::cin >> a[i] >> b[i];
int s = std::accumulate(a, a + n, 0);
int invS = power(s, P - 2);
int r = std::accumulate(b, b + n, 0) - n;
factorial[0] = 1;
for (int i = 1; i <= r; ++i)
factorial[i] = 1LL * factorial[i - 1] * i % P;
invFactorial[r] = power(factorial[r], P - 2);
for (int i = r; i >= 1; --i)
invFactorial[i - 1] = 1LL * invFactorial[i] * i % P;
int cur = 0;
dp[0][0][0] = P - 1;
for (int i = 0; i < n; ++i) {
cur ^= 1;
std::memset(dp[cur], 0, sizeof(dp[cur]));
pw[0] = 1;
int p = 1LL * a[i] * invS % P;
for (int j = 1; j < b[i]; ++j)
pw[j] = 1LL * pw[j - 1] * p % P;
for (int j = 0; j <= r; ++j) {
for (int k = 0; k <= s; ++k) {
if (dp[cur ^ 1][j][k] == 0)
continue;
dp[cur][j][k] = (dp[cur][j][k] + dp[cur ^ 1][j][k]) % P;
for (int l = 0; l < b[i]; ++l)
dp[cur][j + l][k + a[i]] =
(dp[cur][j + l][k + a[i]] +
1LL * (P - dp[cur ^ 1][j][k]) * invFactorial[l] % P * pw[l]) %
P;
}
}
}
int ans = 0;
for (int i = 0; i <= r; ++i) {
for (int j = 1; j <= s; ++j) {
int inv = power(1LL * j * invS % P, P - 2 - i);
ans = (ans + 1LL * factorial[i] * dp[cur][i][j] % P * inv) % P;
}
}
std::cout << ans << "\n";
return 0;
} | replace | 8 | 9 | 8 | 9 | 0 | |
p02908 | C++ | Runtime Error | #include <bits/stdc++.h>
#define For(i, x, y) for (register int i = (x); i <= (y); i++)
#define FOR(i, x, y) for (register int i = (x); i < (y); i++)
#define Dow(i, x, y) for (register int i = (x); i >= (y); i--)
#define Debug(v) \
for (auto i : v) \
printf("%lld ", i); \
puts("")
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define ep emplace_back
#define siz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define fil(a, b) memset((a), (b), sizeof(a))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pa;
typedef pair<ll, ll> PA;
typedef vector<int> poly;
inline ll read() {
ll x = 0, f = 1;
char c = getchar();
while ((c < '0' || c > '9') && (c != '-'))
c = getchar();
if (c == '-')
f = -1, c = getchar();
while (c >= '0' && c <= '9')
x = x * 10 + c - '0', c = getchar();
return x * f;
}
const int N = 5e5 + 10;
int n, S, T, a[N], b[N], cnt, id[2][N];
poly v1[N], v2[N];
map<int, int> f, g;
namespace Flow {
int n, tot = -1, first[N], to[N << 1], flow[N << 1], last[N << 1];
inline void add(int x, int y, int z) {
to[++tot] = y, flow[tot] = z, last[tot] = first[x], first[x] = tot;
}
inline void Add(int x, int y, int z) { add(x, y, z), add(y, x, 0); }
int hd, tl, q[N], deep[N];
inline bool bfs(int s, int t) {
For(i, 1, n) deep[i] = 0;
hd = 0, q[tl = 1] = s, deep[s] = 1;
while (hd < tl) {
int u = q[++hd];
for (int i = first[u]; i != -1; i = last[i])
if (!deep[to[i]] && flow[i])
deep[q[++tl] = to[i]] = deep[u] + 1;
}
return deep[t];
}
int cur[N];
inline void init() { For(i, 1, n) cur[i] = first[i]; }
inline int dfs(int u, int t, int dist) {
if (u == t || !dist)
return dist;
int Flow = 0, f;
for (int i = cur[u]; i != -1; i = last[i]) {
cur[u] = i;
if (deep[to[i]] == deep[u] + 1 && (f = dfs(to[i], t, min(dist, flow[i])))) {
dist -= f, Flow += f, flow[i] -= f, flow[i ^ 1] += f;
if (!dist)
break;
}
}
return Flow;
}
const int inf = 1e9;
inline int Dinic(int s, int t) {
int ans = 0;
while (bfs(s, t))
init(), ans += dfs(s, t, inf);
return ans;
}
} // namespace Flow
int main() {
n = read();
For(i, 1, n) a[i] = read() + 1;
For(i, 1, n) b[i] = read() + 1;
int ans = n;
For(i, 1, n) if (a[i] == b[i] && a[i] == i)-- ans, id[i][0] = id[i][1] = -1;
fil(Flow::first, -1), S = 1, T = 2, cnt = 2;
For(i, 1, n) if (!id[0][i]) {
++cnt;
int tot = 0;
for (int j = i; !id[0][j]; j = a[j]) {
id[0][j] = cnt;
if (b[j] == j)
++tot;
v1[cnt].pb(j);
}
Flow::Add(S, cnt, tot);
Flow::Add(cnt, T, 0);
}
int cnt1 = cnt;
For(i, 1, n) if (!id[1][i]) {
++cnt;
int tot = 0;
for (int j = i; !id[1][j]; j = b[j]) {
id[1][j] = cnt;
if (a[j] == j)
++tot;
v2[cnt].pb(j);
}
Flow::Add(cnt, T, tot);
Flow::Add(S, cnt, 0);
}
Flow::n = cnt;
For(i, 1, cnt1) {
f.clear(), g.clear();
for (auto j : v1[i]) {
if (a[j] == b[j])
f[id[1][j]]++;
g[id[1][j]]++;
}
for (auto j : f)
Flow::Add(i, j.fi, j.se);
for (auto j : g)
Flow::Add(j.fi, i, j.se);
}
printf("%d\n", ans - Flow::Dinic(S, T));
} | #include <bits/stdc++.h>
#define For(i, x, y) for (register int i = (x); i <= (y); i++)
#define FOR(i, x, y) for (register int i = (x); i < (y); i++)
#define Dow(i, x, y) for (register int i = (x); i >= (y); i--)
#define Debug(v) \
for (auto i : v) \
printf("%lld ", i); \
puts("")
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define ep emplace_back
#define siz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define fil(a, b) memset((a), (b), sizeof(a))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pa;
typedef pair<ll, ll> PA;
typedef vector<int> poly;
inline ll read() {
ll x = 0, f = 1;
char c = getchar();
while ((c < '0' || c > '9') && (c != '-'))
c = getchar();
if (c == '-')
f = -1, c = getchar();
while (c >= '0' && c <= '9')
x = x * 10 + c - '0', c = getchar();
return x * f;
}
const int N = 5e5 + 10;
int n, S, T, a[N], b[N], cnt, id[2][N];
poly v1[N], v2[N];
map<int, int> f, g;
namespace Flow {
int n, tot = -1, first[N], to[N << 1], flow[N << 1], last[N << 1];
inline void add(int x, int y, int z) {
to[++tot] = y, flow[tot] = z, last[tot] = first[x], first[x] = tot;
}
inline void Add(int x, int y, int z) { add(x, y, z), add(y, x, 0); }
int hd, tl, q[N], deep[N];
inline bool bfs(int s, int t) {
For(i, 1, n) deep[i] = 0;
hd = 0, q[tl = 1] = s, deep[s] = 1;
while (hd < tl) {
int u = q[++hd];
for (int i = first[u]; i != -1; i = last[i])
if (!deep[to[i]] && flow[i])
deep[q[++tl] = to[i]] = deep[u] + 1;
}
return deep[t];
}
int cur[N];
inline void init() { For(i, 1, n) cur[i] = first[i]; }
inline int dfs(int u, int t, int dist) {
if (u == t || !dist)
return dist;
int Flow = 0, f;
for (int i = cur[u]; i != -1; i = last[i]) {
cur[u] = i;
if (deep[to[i]] == deep[u] + 1 && (f = dfs(to[i], t, min(dist, flow[i])))) {
dist -= f, Flow += f, flow[i] -= f, flow[i ^ 1] += f;
if (!dist)
break;
}
}
return Flow;
}
const int inf = 1e9;
inline int Dinic(int s, int t) {
int ans = 0;
while (bfs(s, t))
init(), ans += dfs(s, t, inf);
return ans;
}
} // namespace Flow
int main() {
n = read();
For(i, 1, n) a[i] = read() + 1;
For(i, 1, n) b[i] = read() + 1;
int ans = n;
For(i, 1, n) {
if (a[i] == b[i] && a[i] == i)
--ans, id[0][i] = id[1][i] = -1;
}
fil(Flow::first, -1), S = 1, T = 2, cnt = 2;
For(i, 1, n) if (!id[0][i]) {
++cnt;
int tot = 0;
for (int j = i; !id[0][j]; j = a[j]) {
id[0][j] = cnt;
if (b[j] == j)
++tot;
v1[cnt].pb(j);
}
Flow::Add(S, cnt, tot);
Flow::Add(cnt, T, 0);
}
int cnt1 = cnt;
For(i, 1, n) if (!id[1][i]) {
++cnt;
int tot = 0;
for (int j = i; !id[1][j]; j = b[j]) {
id[1][j] = cnt;
if (a[j] == j)
++tot;
v2[cnt].pb(j);
}
Flow::Add(cnt, T, tot);
Flow::Add(S, cnt, 0);
}
Flow::n = cnt;
For(i, 1, cnt1) {
f.clear(), g.clear();
for (auto j : v1[i]) {
if (a[j] == b[j])
f[id[1][j]]++;
g[id[1][j]]++;
}
for (auto j : f)
Flow::Add(i, j.fi, j.se);
for (auto j : g)
Flow::Add(j.fi, i, j.se);
}
printf("%d\n", ans - Flow::Dinic(S, T));
} | replace | 87 | 88 | 87 | 91 | 0 | |
p02908 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
namespace mine {
typedef long long ll;
#define pr pair<int, int>
#define FR first
#define SE second
#define MP make_pair
#define PB push_back
#define vc vector
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define bin(x) (1ll << (x))
#define GG(x) \
if (x) \
puts("error")
#define rep(i, l, r) for (int i = (l); i <= (r); i++)
ll qread() {
ll ans = 0, f = 1;
char c = getchar();
while (c < '0' or c > '9') {
if (c == '-')
f = -1;
c = getchar();
}
while ('0' <= c and c <= '9')
ans = ans * 10 + c - '0', c = getchar();
return ans * f;
}
void write(ll num) {
if (num < 0)
putchar('-'), num = -num;
if (num >= 10)
write(num / 10);
putchar('0' + num % 10);
}
void write1(ll num) {
write(num);
putchar(' ');
}
void write2(ll num) {
write(num);
putchar('\n');
}
template <typename T> void chmax(T &x, const T y) { x = (x > y ? x : y); }
template <typename T> void chmin(T &x, const T y) { x = (x < y ? x : y); }
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
const int INF = 0x3f3f3f3f;
const int MOD = 998244353;
inline int mm(const int x) { return x >= MOD ? x - MOD : x; }
template <typename T> inline void add(T &x, const T y) { x = mm(x + y); }
inline ll qpower(ll x, ll e) {
ll ans = 1;
GG(e < 0);
while (e) {
if (e & 1)
ans = ans * x % MOD;
x = x * x % MOD;
e >>= 1;
}
return ans;
}
ll invm(ll x) { return qpower(x, MOD - 2); }
const int N = 1e6 + 10;
namespace MM {
int hou[N];
struct Edge {
int y, c, g;
} e[N];
int ln = 0;
int oth(int x) { return x & 1 ? x + 1 : x - 1; }
void insert(int x, int y, int c) {
e[++ln] = (Edge){y, c, hou[x]};
hou[x] = ln;
e[++ln] = (Edge){x, 0, hou[y]};
hou[y] = ln;
}
int st, ed;
int h[N];
queue<int> qq;
int cur[N];
bool bfs() {
for (int i = st; i <= ed; i++)
h[i] = 0, cur[i] = hou[i];
qq.push(st);
h[st] = 1;
while (qq.size()) {
int x = qq.front();
qq.pop();
for (int k = hou[x]; k > 0; k = e[k].g) {
int y = e[k].y;
if (e[k].c and !h[y])
h[y] = h[x] + 1, qq.push(y);
}
}
return h[ed] > 0;
}
int dfs(int x, int flow) {
if (x == ed)
return flow;
int tt = 0;
for (int k = cur[x]; k > 0; k = e[k].g) {
int y = e[k].y;
if (h[y] == h[x] + 1 and e[k].c and tt < flow) {
int out = dfs(y, min(flow - tt, e[k].c));
e[k].c -= out;
e[oth(k)].c += out;
tt += out;
}
if (tt == flow)
break;
if (e[k].c == 0 and cur[x] == k)
cur[x] = e[k].g;
}
if (!tt)
h[x] = 0;
return tt;
}
int solve() {
int ans = 0;
while (bfs())
ans += dfs(st, INF);
return ans;
}
}; // namespace MM
struct DSU {
int fa[N];
DSU() {
for (int i = 0; i < N; i++)
fa[i] = i;
}
int findfa(int x) { return x == fa[x] ? x : fa[x] = findfa(fa[x]); }
void merg(int x, int y) {
x = findfa(x), y = findfa(y);
fa[x] = y;
}
} dsu[2];
int p[N], q[N];
void main() {
int n = qread();
for (int i = 1; i <= n; i++)
p[i] = qread() + 1, dsu[0].merg(i, p[i]);
for (int i = 1; i <= n; i++)
q[i] = qread() + 1, dsu[1].merg(i, q[i]);
using namespace MM;
st = 0, ed = n + n + 1;
int cnt = 0;
for (int i = 1; i <= n; i++) {
cnt += (p[i] != q[i]);
int fx = dsu[0].findfa(p[i]), fy = n + dsu[1].findfa(q[i]);
if (p[i] == i and q[i] != i)
insert(st, fy, 1);
if (p[i] != i and q[i] == i)
insert(fx, ed, 1);
if (p[i] == q[i] and p[i] != i)
insert(fx, fy, 1), insert(fy, fx, 1), cnt++;
if (p[i] != q[i] and p[i] != i and q[i] != i)
insert(fx, fy, 1);
}
write(cnt - solve());
}
}; // namespace mine
int main() {
srand(time(0));
mine::main();
}
| #include <bits/stdc++.h>
using namespace std;
namespace mine {
typedef long long ll;
#define pr pair<int, int>
#define FR first
#define SE second
#define MP make_pair
#define PB push_back
#define vc vector
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define bin(x) (1ll << (x))
#define GG(x) \
if (x) \
puts("error")
#define rep(i, l, r) for (int i = (l); i <= (r); i++)
ll qread() {
ll ans = 0, f = 1;
char c = getchar();
while (c < '0' or c > '9') {
if (c == '-')
f = -1;
c = getchar();
}
while ('0' <= c and c <= '9')
ans = ans * 10 + c - '0', c = getchar();
return ans * f;
}
void write(ll num) {
if (num < 0)
putchar('-'), num = -num;
if (num >= 10)
write(num / 10);
putchar('0' + num % 10);
}
void write1(ll num) {
write(num);
putchar(' ');
}
void write2(ll num) {
write(num);
putchar('\n');
}
template <typename T> void chmax(T &x, const T y) { x = (x > y ? x : y); }
template <typename T> void chmin(T &x, const T y) { x = (x < y ? x : y); }
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
const int INF = 0x3f3f3f3f;
const int MOD = 998244353;
inline int mm(const int x) { return x >= MOD ? x - MOD : x; }
template <typename T> inline void add(T &x, const T y) { x = mm(x + y); }
inline ll qpower(ll x, ll e) {
ll ans = 1;
GG(e < 0);
while (e) {
if (e & 1)
ans = ans * x % MOD;
x = x * x % MOD;
e >>= 1;
}
return ans;
}
ll invm(ll x) { return qpower(x, MOD - 2); }
const int N = 1e6 + 10;
namespace MM {
int hou[N];
struct Edge {
int y, c, g;
} e[N];
int ln = 0;
int oth(int x) { return x & 1 ? x + 1 : x - 1; }
void insert(int x, int y, int c) {
e[++ln] = (Edge){y, c, hou[x]};
hou[x] = ln;
e[++ln] = (Edge){x, 0, hou[y]};
hou[y] = ln;
}
int st, ed;
int h[N];
queue<int> qq;
int cur[N];
bool bfs() {
for (int i = st; i <= ed; i++)
h[i] = 0, cur[i] = hou[i];
qq.push(st);
h[st] = 1;
while (qq.size()) {
int x = qq.front();
qq.pop();
for (int k = hou[x]; k > 0; k = e[k].g) {
int y = e[k].y;
if (e[k].c and !h[y])
h[y] = h[x] + 1, qq.push(y);
}
}
return h[ed] > 0;
}
int dfs(int x, int flow) {
if (x == ed)
return flow;
int tt = 0;
for (int k = cur[x]; k > 0; k = e[k].g) {
int y = e[k].y;
if (h[y] == h[x] + 1 and e[k].c and tt < flow) {
int out = dfs(y, min(flow - tt, e[k].c));
e[k].c -= out;
e[oth(k)].c += out;
tt += out;
}
if (tt == flow)
break;
if (e[k].c == 0)
cur[x] = e[k].g;
}
if (!tt)
h[x] = 0;
return tt;
}
int solve() {
int ans = 0;
while (bfs())
ans += dfs(st, INF);
return ans;
}
}; // namespace MM
struct DSU {
int fa[N];
DSU() {
for (int i = 0; i < N; i++)
fa[i] = i;
}
int findfa(int x) { return x == fa[x] ? x : fa[x] = findfa(fa[x]); }
void merg(int x, int y) {
x = findfa(x), y = findfa(y);
fa[x] = y;
}
} dsu[2];
int p[N], q[N];
void main() {
int n = qread();
for (int i = 1; i <= n; i++)
p[i] = qread() + 1, dsu[0].merg(i, p[i]);
for (int i = 1; i <= n; i++)
q[i] = qread() + 1, dsu[1].merg(i, q[i]);
using namespace MM;
st = 0, ed = n + n + 1;
int cnt = 0;
for (int i = 1; i <= n; i++) {
cnt += (p[i] != q[i]);
int fx = dsu[0].findfa(p[i]), fy = n + dsu[1].findfa(q[i]);
if (p[i] == i and q[i] != i)
insert(st, fy, 1);
if (p[i] != i and q[i] == i)
insert(fx, ed, 1);
if (p[i] == q[i] and p[i] != i)
insert(fx, fy, 1), insert(fy, fx, 1), cnt++;
if (p[i] != q[i] and p[i] != i and q[i] != i)
insert(fx, fy, 1);
}
write(cnt - solve());
}
}; // namespace mine
int main() {
srand(time(0));
mine::main();
}
| replace | 114 | 115 | 114 | 115 | TLE | |
p02908 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (rint i = 1; i <= (n); i++)
#define re0(i, n) for (rint i = 0; i < (int)n; i++)
#define travel(i, u) for (rint i = head[u]; i; i = e[i].nxt)
#define rint register int
using namespace std;
typedef long long lo;
template <typename tp> inline void read(tp &x) {
x = 0;
char c = getchar();
int f = 0;
for (; c < '0' || c > '9'; f |= c == '-', c = getchar())
;
for (; c >= '0' && c <= '9'; x = (x << 3) + (x << 1) + c - '0', c = getchar())
;
if (f)
x = -x;
}
namespace mf {
static const int N = 5e5 + 233;
static const int M = 2e6 + 233;
static const int inf = 1e8;
struct E {
int nxt, to, fl;
} e[M << 1];
int d[N], head[N], cur[N], e_cnt = 1;
int n, s, t;
inline void init(int _n, int _s, int _t) {
n = _n;
s = _s;
t = _t;
fill(head, head + n + 1, 0);
e_cnt = 1;
}
inline void add(int u, int v, int fl) {
e[++e_cnt] = (E){head[u], v, fl};
head[u] = e_cnt;
}
inline void link(int u, int v, int fl) {
add(u, v, fl);
add(v, u, 0);
}
inline bool bfs(void) {
queue<int> q;
fill(d, d + n + 1, -1);
d[s] = 0;
q.push(s);
while (!q.empty()) {
int u = q.front();
q.pop();
for (int i = head[u]; i; i = e[i].nxt)
if (e[i].fl) {
int v = e[i].to;
if (!~d[v])
d[v] = d[u] + 1, q.push(v);
}
}
return d[t] != -1;
}
inline int dfs(int u, int f) {
if (u == t || !f)
return f;
int used = 0, t;
for (int i = head[u]; i; i = e[i].nxt)
if (e[i].fl) {
int v = e[i].to;
if (d[v] != d[u] + 1)
continue;
t = dfs(v, min(f - used, e[i].fl));
used += t;
e[i].fl -= t;
e[i ^ 1].fl += t;
if (used == f)
break;
}
if (!used)
d[u] = -1;
return used;
}
inline int doit(void) {
int fl = 0;
while (bfs()) {
memcpy(cur, head, sizeof(int) * (n + 2));
fl += dfs(s, inf);
}
return fl;
}
} // namespace mf
const int N = 5e5 + 233;
int n, cnt, a[N], b[N], p[N], q[N], x[N][2], y[N][2], vis[N];
inline void doit(int p[], int a[]) {
memset(vis, 0, sizeof vis);
rep(i, n) if (!vis[i]) {
++cnt;
for (int u = i; !vis[u]; u = p[u])
a[u] = cnt, vis[u] = true;
}
}
using mf::link;
int main(void) {
read(n);
rep(i, n) read(p[i]), ++p[i];
rep(i, n) read(q[i]), ++q[i];
doit(p, a);
doit(q, b);
int S = cnt + 1, T = S + 1;
mf::init(T, S, T);
int ans = 0;
rep(i, n) {
if (p[i] == q[i]) {
if (q[i] == i)
++ans;
else {
link(a[i], b[i], 1);
link(b[i], a[i], 1);
}
} else {
if (q[i] == i)
link(a[i], T, 1);
else if (p[i] == i)
link(S, b[i], 1);
else
link(a[i], b[i], 1);
}
}
ans += mf::doit();
cout << n - ans << "\n";
}
| #include <bits/stdc++.h>
#define rep(i, n) for (rint i = 1; i <= (n); i++)
#define re0(i, n) for (rint i = 0; i < (int)n; i++)
#define travel(i, u) for (rint i = head[u]; i; i = e[i].nxt)
#define rint register int
using namespace std;
typedef long long lo;
template <typename tp> inline void read(tp &x) {
x = 0;
char c = getchar();
int f = 0;
for (; c < '0' || c > '9'; f |= c == '-', c = getchar())
;
for (; c >= '0' && c <= '9'; x = (x << 3) + (x << 1) + c - '0', c = getchar())
;
if (f)
x = -x;
}
namespace mf {
static const int N = 5e5 + 233;
static const int M = 2e6 + 233;
static const int inf = 1e8;
struct E {
int nxt, to, fl;
} e[M << 1];
int d[N], head[N], cur[N], e_cnt = 1;
int n, s, t;
inline void init(int _n, int _s, int _t) {
n = _n;
s = _s;
t = _t;
fill(head, head + n + 1, 0);
e_cnt = 1;
}
inline void add(int u, int v, int fl) {
e[++e_cnt] = (E){head[u], v, fl};
head[u] = e_cnt;
}
inline void link(int u, int v, int fl) {
add(u, v, fl);
add(v, u, 0);
}
inline bool bfs(void) {
queue<int> q;
fill(d, d + n + 1, -1);
d[s] = 0;
q.push(s);
while (!q.empty()) {
int u = q.front();
q.pop();
for (int i = head[u]; i; i = e[i].nxt)
if (e[i].fl) {
int v = e[i].to;
if (!~d[v])
d[v] = d[u] + 1, q.push(v);
}
}
return d[t] != -1;
}
inline int dfs(int u, int f) {
if (u == t || !f)
return f;
int used = 0, t;
for (int &i = cur[u]; i; i = e[i].nxt)
if (e[i].fl) {
int v = e[i].to;
if (d[v] != d[u] + 1)
continue;
t = dfs(v, min(f - used, e[i].fl));
used += t;
e[i].fl -= t;
e[i ^ 1].fl += t;
if (used == f)
break;
}
if (!used)
d[u] = -1;
return used;
}
inline int doit(void) {
int fl = 0;
while (bfs()) {
memcpy(cur, head, sizeof(int) * (n + 2));
fl += dfs(s, inf);
}
return fl;
}
} // namespace mf
const int N = 5e5 + 233;
int n, cnt, a[N], b[N], p[N], q[N], x[N][2], y[N][2], vis[N];
inline void doit(int p[], int a[]) {
memset(vis, 0, sizeof vis);
rep(i, n) if (!vis[i]) {
++cnt;
for (int u = i; !vis[u]; u = p[u])
a[u] = cnt, vis[u] = true;
}
}
using mf::link;
int main(void) {
read(n);
rep(i, n) read(p[i]), ++p[i];
rep(i, n) read(q[i]), ++q[i];
doit(p, a);
doit(q, b);
int S = cnt + 1, T = S + 1;
mf::init(T, S, T);
int ans = 0;
rep(i, n) {
if (p[i] == q[i]) {
if (q[i] == i)
++ans;
else {
link(a[i], b[i], 1);
link(b[i], a[i], 1);
}
} else {
if (q[i] == i)
link(a[i], T, 1);
else if (p[i] == i)
link(S, b[i], 1);
else
link(a[i], b[i], 1);
}
}
ans += mf::doit();
cout << n - ans << "\n";
}
| replace | 65 | 66 | 65 | 66 | TLE | |
p02908 | C++ | Time Limit Exceeded | #include <cstdio>
#include <iostream>
#include <queue>
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define REP1(u) for (int i = hd[u], v, c; v = e[i].v, c = e[i].c, i; i = e[i].n)
#define REP2(u) \
for (int &i = cr[u], v, c; v = e[i].v, c = e[i].c, i; i = e[i].n)
using namespace std;
const int N = 200200, INF = 1000010000;
int n, x, y, ans, S, T, fl = 1;
int a[N], b[N], ca[N], cb[N], hd[N], cr[N], dep[N];
queue<int> q;
struct edge {
int n, v, c;
} e[N];
void add_(int u, int v, int c) {
e[++fl] = (edge){hd[u], v, c};
hd[u] = fl;
}
void adde(int u, int v, int c) {
add_(u, v, c);
add_(v, u, 0);
}
bool bfs() {
FOR(i, S, T) dep[i] = -1, cr[i] = hd[i];
q.push(S);
dep[S] = 0;
while (q.size()) {
int u = q.front();
q.pop();
REP1(u) if (c && dep[v] < 0) dep[v] = dep[u] + 1, q.push(v);
}
return ~dep[T];
}
int dfs(int u, int E) {
if (u == T)
return E;
int F = 0;
REP2(u) if (c && dep[v] == dep[u] + 1) {
int t = dfs(v, min(c, E));
F += t;
e[i ^ 1].c += t;
E -= t;
e[i ^ 0].c -= t;
if (!E)
break;
}
return F;
}
int main() {
// freopen("perm.in","r",stdin);
// freopen("perm.out","w",stdout);
scanf("%d", &n);
S = T = 1;
FOR(i, 1, n) scanf("%d", &a[i]);
FOR(i, 1, n) scanf("%d", &b[i]);
FOR(i, 1, n) if (!ca[i]) for (x = a[i], ++T; ca[x] = T, x != i; x = a[x]);
FOR(i, 1, n) if (!cb[i]) for (x = b[i], ++T; cb[x] = T, x != i; x = b[x]);
++T;
FOR(i, 1, n) {
x = ca[i];
y = cb[i];
if (a[i] == b[i]) {
if (i == a[i])
ans++;
else
adde(x, y, 1), e[fl].c = 1;
} else {
if (i != a[i] && i != b[i])
adde(y, x, 1);
if (i == a[i])
adde(y, T, 1);
if (i == b[i])
adde(S, x, 1);
}
}
// FOR(u,1,T){cout<<u<<':';REP(u) cout<<v<<'-'<<c<<' ';cout<<'\n';}
while (bfs())
ans += dfs(S, INF);
cout << n - ans << '\n';
// FOR(i,1,n) cout<<(~dep[ca[i]]?a[i]:i)<<' ';cout<<'\n';
// FOR(i,1,n) cout<<(~dep[cb[i]]?i:b[i])<<' ';cout<<'\n';
} | #include <cstdio>
#include <iostream>
#include <queue>
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define REP1(u) for (int i = hd[u], v, c; v = e[i].v, c = e[i].c, i; i = e[i].n)
#define REP2(u) \
for (int &i = cr[u], v, c; v = e[i].v, c = e[i].c, i; i = e[i].n)
using namespace std;
const int N = 200200, INF = 1000010000;
int n, x, y, ans, S, T, fl = 1;
int a[N], b[N], ca[N], cb[N], hd[N], cr[N], dep[N];
queue<int> q;
struct edge {
int n, v, c;
} e[N];
void add_(int u, int v, int c) {
e[++fl] = (edge){hd[u], v, c};
hd[u] = fl;
}
void adde(int u, int v, int c) {
add_(u, v, c);
add_(v, u, 0);
}
bool bfs() {
FOR(i, S, T) dep[i] = -1, cr[i] = hd[i];
q.push(S);
dep[S] = 0;
while (q.size()) {
int u = q.front();
q.pop();
REP1(u) if (c && dep[v] < 0) dep[v] = dep[u] + 1, q.push(v);
}
return ~dep[T];
}
int dfs(int u, int E) {
if (u == T)
return E;
int F = 0;
REP2(u) if (c && dep[v] == dep[u] + 1) {
int t = dfs(v, min(c, E));
F += t;
e[i ^ 1].c += t;
E -= t;
e[i ^ 0].c -= t;
if (!E)
break;
}
return F;
}
int main() {
// freopen("perm.in","r",stdin);
// freopen("perm.out","w",stdout);
scanf("%d", &n);
S = T = 1;
FOR(i, 1, n) scanf("%d", &a[i]), a[i]++;
FOR(i, 1, n) scanf("%d", &b[i]), b[i]++;
FOR(i, 1, n) if (!ca[i]) for (x = a[i], ++T; ca[x] = T, x != i; x = a[x]);
FOR(i, 1, n) if (!cb[i]) for (x = b[i], ++T; cb[x] = T, x != i; x = b[x]);
++T;
FOR(i, 1, n) {
x = ca[i];
y = cb[i];
if (a[i] == b[i]) {
if (i == a[i])
ans++;
else
adde(x, y, 1), e[fl].c = 1;
} else {
if (i != a[i] && i != b[i])
adde(y, x, 1);
if (i == a[i])
adde(y, T, 1);
if (i == b[i])
adde(S, x, 1);
}
}
// FOR(u,1,T){cout<<u<<':';REP(u) cout<<v<<'-'<<c<<' ';cout<<'\n';}
while (bfs())
ans += dfs(S, INF);
cout << n - ans << '\n';
// FOR(i,1,n) cout<<(~dep[ca[i]]?a[i]:i)<<' ';cout<<'\n';
// FOR(i,1,n) cout<<(~dep[cb[i]]?i:b[i])<<' ';cout<<'\n';
} | replace | 54 | 56 | 54 | 56 | TLE | |
p02908 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
const int N = 100005, inf = 0x3f3f3f3f;
int n, s, t, p[N], q[N], L[N], R[N], head[N], dis[N], cur[N];
int left[N], right[N], tot = 1, idx;
bool vis[N];
struct edge {
int to, nxt, rem;
} e[N * 10];
void link(int x, int y, int z, int w = 0) {
e[++tot] = (edge){y, head[x], z}, head[x] = tot;
e[++tot] = (edge){x, head[y], w}, head[y] = tot;
}
bool bfs() {
std::memset(dis, 0, idx + 1 << 2);
std::queue<int> q;
q.push(s), dis[s] = 1;
while (q.size()) {
int tmp = q.front();
q.pop();
for (int i = head[tmp]; i; i = e[i].nxt)
if (e[i].rem && !dis[e[i].to])
dis[e[i].to] = dis[tmp] + 1, q.push(e[i].to);
}
return dis[t] > 0;
}
int dinic(int x, int low) {
if (x == t || !low)
return low;
int ans = 0, t;
for (int i = head[x]; i; i = e[i].nxt)
if (e[i].rem && dis[e[i].to] == dis[x] + 1 &&
(t = dinic(e[i].to, std::min(low, e[i].rem)))) {
e[i].rem -= t, e[i ^ 1].rem += t, ans += t, low -= t;
if (!low)
break;
}
return ans;
}
int main() {
std::ios::sync_with_stdio(0), std::cin.tie(0);
std::cin >> n;
for (int i = 0; i < n; ++i)
std::cin >> p[i];
for (int i = 0; i < n; ++i)
std::cin >> q[i];
for (int i = 0; i < n; ++i)
if (!vis[i]) {
++idx;
for (int j = i; !vis[j]; vis[j] = 1, j = p[j])
L[j] = idx;
}
std::memset(vis, 0, sizeof vis);
for (int i = 0; i < n; ++i)
if (!vis[i]) {
++idx;
for (int j = i; !vis[j]; vis[j] = 1, j = q[j])
R[j] = idx;
}
t = ++idx;
int ans = n;
for (int i = 0; i < n; ++i)
if (p[i] != i || q[i] != i) {
if (p[i] == i) {
++left[R[i]];
} else if (q[i] == i) {
++right[L[i]];
} else if (p[i] == q[i]) {
link(L[i], R[i], 1, 1);
} else {
link(L[i], R[i], 1);
}
} else
--ans;
for (int i = 1; i < idx; ++i) {
if (left[i])
link(s, i, left[i]);
if (right[i])
link(i, t, right[i]);
}
while (bfs())
std::memcpy(cur, head, idx + 1 << 2), ans -= dinic(s, inf);
std::cout << ans << '\n';
return 0;
} | #include <bits/stdc++.h>
const int N = 100005, inf = 0x3f3f3f3f;
int n, s, t, p[N], q[N], L[N], R[N], head[N], dis[N], cur[N];
int left[N], right[N], tot = 1, idx;
bool vis[N];
struct edge {
int to, nxt, rem;
} e[N * 10];
void link(int x, int y, int z, int w = 0) {
e[++tot] = (edge){y, head[x], z}, head[x] = tot;
e[++tot] = (edge){x, head[y], w}, head[y] = tot;
}
bool bfs() {
std::memset(dis, 0, idx + 1 << 2);
std::queue<int> q;
q.push(s), dis[s] = 1;
while (q.size()) {
int tmp = q.front();
q.pop();
for (int i = head[tmp]; i; i = e[i].nxt)
if (e[i].rem && !dis[e[i].to])
dis[e[i].to] = dis[tmp] + 1, q.push(e[i].to);
}
return dis[t] > 0;
}
int dinic(int x, int low) {
if (x == t || !low)
return low;
int ans = 0, t;
for (int &i = cur[x]; i; i = e[i].nxt)
if (e[i].rem && dis[e[i].to] == dis[x] + 1 &&
(t = dinic(e[i].to, std::min(low, e[i].rem)))) {
e[i].rem -= t, e[i ^ 1].rem += t, ans += t, low -= t;
if (!low)
break;
}
return ans;
}
int main() {
std::ios::sync_with_stdio(0), std::cin.tie(0);
std::cin >> n;
for (int i = 0; i < n; ++i)
std::cin >> p[i];
for (int i = 0; i < n; ++i)
std::cin >> q[i];
for (int i = 0; i < n; ++i)
if (!vis[i]) {
++idx;
for (int j = i; !vis[j]; vis[j] = 1, j = p[j])
L[j] = idx;
}
std::memset(vis, 0, sizeof vis);
for (int i = 0; i < n; ++i)
if (!vis[i]) {
++idx;
for (int j = i; !vis[j]; vis[j] = 1, j = q[j])
R[j] = idx;
}
t = ++idx;
int ans = n;
for (int i = 0; i < n; ++i)
if (p[i] != i || q[i] != i) {
if (p[i] == i) {
++left[R[i]];
} else if (q[i] == i) {
++right[L[i]];
} else if (p[i] == q[i]) {
link(L[i], R[i], 1, 1);
} else {
link(L[i], R[i], 1);
}
} else
--ans;
for (int i = 1; i < idx; ++i) {
if (left[i])
link(s, i, left[i]);
if (right[i])
link(i, t, right[i]);
}
while (bfs())
std::memcpy(cur, head, idx + 1 << 2), ans -= dinic(s, inf);
std::cout << ans << '\n';
return 0;
} | replace | 34 | 35 | 34 | 35 | TLE | |
p02908 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define clr(x) memset(x, 0, sizeof x)
#define For(i, a, b) for (int i = (a); i <= (b); i++)
#define Fod(i, b, a) for (int i = (b); i >= (a); i--)
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define fi first
#define se second
#define outval(x) cerr << #x " = " << x << endl
#define outtag(x) cerr << "-----------------" #x "-----------------\n"
#define outarr(a, L, R) \
cerr << #a "[" << L << ".." << R << "] = "; \
For(_x, L, R) cerr << a[_x] << " "; \
cerr << endl;
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> pii;
LL read() {
LL x = 0, f = 0;
char ch = getchar();
while (!isdigit(ch))
f = ch == '-', ch = getchar();
while (isdigit(ch))
x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
return f ? -x : x;
}
const int N = 100005, INF = 1e9;
namespace flows {
const int N = 100005 * 2, M = N * 2;
struct Edge {
int x, y, nxt, cap;
Edge() {}
Edge(int _x, int _y, int _nxt, int _cap) {
x = _x, y = _y, nxt = _nxt, cap = _cap;
}
} e[M];
int n, S, T, cnt;
int fst[N], cur[N], dis[N];
void clear(int _n, int _S, int _T) {
n = _n, S = _S, T = _T, cnt = 1;
clr(fst);
}
void Add(int x, int y, int cap) {
e[++cnt] = Edge(x, y, fst[x], cap), fst[x] = cnt;
e[++cnt] = Edge(y, x, fst[y], 0), fst[y] = cnt;
}
int bfs() {
static queue<int> q;
For(i, 1, n) dis[i] = 0;
dis[S] = 1;
q.push(S);
while (!q.empty()) {
int x = q.front();
q.pop();
for (int i = fst[x]; i; i = e[i].nxt) {
int y = e[i].y;
if (e[i].cap && !dis[y]) {
dis[y] = dis[x] + 1;
q.push(y);
}
}
}
return dis[T] != 0;
}
int dfs(int x, int Lim) {
if (x == T || !Lim)
return Lim;
int rem = Lim;
for (int &i = cur[x]; i; i = e[i].nxt) {
int y = e[i].y;
if (dis[x] + 1 == dis[y] && e[i].cap) {
int d = dfs(y, min(rem, e[i].cap));
rem -= d, e[i].cap -= d, e[i ^ 1].cap += d;
}
}
return Lim - rem;
}
int Dinic() {
int res = 0;
while (bfs()) {
For(i, 1, n) cur[i] = fst[i];
res += dfs(S, INF);
}
return res;
}
} // namespace flows
int n, c = 0;
int p[N], q[N];
int x[N], y[N];
int main() {
n = read();
For(i, 1, n) p[i] = read() + 1;
For(i, 1, n) q[i] = read() + 1;
For(i, 1, n) {
if (x[i])
continue;
c++;
for (int j = i; !x[j]; j = p[j])
x[j] = c;
}
For(i, 1, n) {
if (y[i])
continue;
c++;
for (int j = i; !y[j]; j = q[j])
y[j] = c;
}
int S = c + 1, T = S + 1;
flows::clear(T, S, T);
int ans = 0;
For(i, 1, n) {
if (i == p[i] && i == q[i])
ans++;
else if (i == p[i] && i != q[i])
flows::Add(S, y[i], 1);
else if (i == q[i] && i != p[i])
flows::Add(x[i], T, 1);
else if (p[i] != q[i])
flows::Add(x[i], y[i], 1);
else {
flows::Add(x[i], y[i], 1);
flows::Add(y[i], x[i], 1);
}
}
ans += flows::Dinic();
cout << n - ans << endl;
return 0;
} | #include <bits/stdc++.h>
#define clr(x) memset(x, 0, sizeof x)
#define For(i, a, b) for (int i = (a); i <= (b); i++)
#define Fod(i, b, a) for (int i = (b); i >= (a); i--)
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define fi first
#define se second
#define outval(x) cerr << #x " = " << x << endl
#define outtag(x) cerr << "-----------------" #x "-----------------\n"
#define outarr(a, L, R) \
cerr << #a "[" << L << ".." << R << "] = "; \
For(_x, L, R) cerr << a[_x] << " "; \
cerr << endl;
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> pii;
LL read() {
LL x = 0, f = 0;
char ch = getchar();
while (!isdigit(ch))
f = ch == '-', ch = getchar();
while (isdigit(ch))
x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
return f ? -x : x;
}
const int N = 100005, INF = 1e9;
namespace flows {
const int N = 100005 * 2, M = N * 2;
struct Edge {
int x, y, nxt, cap;
Edge() {}
Edge(int _x, int _y, int _nxt, int _cap) {
x = _x, y = _y, nxt = _nxt, cap = _cap;
}
} e[M];
int n, S, T, cnt;
int fst[N], cur[N], dis[N];
void clear(int _n, int _S, int _T) {
n = _n, S = _S, T = _T, cnt = 1;
clr(fst);
}
void Add(int x, int y, int cap) {
e[++cnt] = Edge(x, y, fst[x], cap), fst[x] = cnt;
e[++cnt] = Edge(y, x, fst[y], 0), fst[y] = cnt;
}
int bfs() {
static queue<int> q;
For(i, 1, n) dis[i] = 0;
dis[S] = 1;
q.push(S);
while (!q.empty()) {
int x = q.front();
q.pop();
for (int i = fst[x]; i; i = e[i].nxt) {
int y = e[i].y;
if (e[i].cap && !dis[y]) {
dis[y] = dis[x] + 1;
q.push(y);
}
}
}
return dis[T] != 0;
}
int dfs(int x, int Lim) {
if (x == T || !Lim)
return Lim;
int rem = Lim;
for (int &i = cur[x]; i; i = e[i].nxt) {
int y = e[i].y;
if (dis[x] + 1 == dis[y] && e[i].cap) {
int d = dfs(y, min(rem, e[i].cap));
rem -= d, e[i].cap -= d, e[i ^ 1].cap += d;
if (!rem)
break;
}
}
return Lim - rem;
}
int Dinic() {
int res = 0;
while (bfs()) {
For(i, 1, n) cur[i] = fst[i];
res += dfs(S, INF);
}
return res;
}
} // namespace flows
int n, c = 0;
int p[N], q[N];
int x[N], y[N];
int main() {
n = read();
For(i, 1, n) p[i] = read() + 1;
For(i, 1, n) q[i] = read() + 1;
For(i, 1, n) {
if (x[i])
continue;
c++;
for (int j = i; !x[j]; j = p[j])
x[j] = c;
}
For(i, 1, n) {
if (y[i])
continue;
c++;
for (int j = i; !y[j]; j = q[j])
y[j] = c;
}
int S = c + 1, T = S + 1;
flows::clear(T, S, T);
int ans = 0;
For(i, 1, n) {
if (i == p[i] && i == q[i])
ans++;
else if (i == p[i] && i != q[i])
flows::Add(S, y[i], 1);
else if (i == q[i] && i != p[i])
flows::Add(x[i], T, 1);
else if (p[i] != q[i])
flows::Add(x[i], y[i], 1);
else {
flows::Add(x[i], y[i], 1);
flows::Add(y[i], x[i], 1);
}
}
ans += flows::Dinic();
cout << n - ans << endl;
return 0;
} | insert | 74 | 74 | 74 | 76 | TLE | |
p02908 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define fo(i, l, r) for (int i = l; i <= r; i++)
#define of(i, l, r) for (int i = l; i >= r; i--)
#define fe(i, u) for (int i = head[u]; i; i = e[i].next)
#define el putchar('\n')
#define ta putchar(' ')
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
inline void open(const char *s) {
#ifndef ONLINE_JUDGE
char str[20];
sprintf(str, "%s.in", s);
freopen(str, "r", stdin);
// sprintf(str,"%s.out",s);
// freopen(str,"w",stdout);
#endif
}
inline int rd() {
static int x, f;
x = 0;
f = 1;
char ch = getchar();
for (; ch < '0' || ch > '9'; ch = getchar())
if (ch == '-')
f = -1;
for (; ch >= '0' && ch <= '9'; ch = getchar())
x = x * 10 + ch - '0';
return f > 0 ? x : -x;
}
const int N = 100010;
int n, a[N], b[N], t1, t2, bel1[N], bel2[N], c1[N], c2[N], vs, vt;
bool vis[N];
int head[N << 1], tot = 1;
struct edge {
int v, f, next;
} e[N * 20];
inline void add(int u, int v, int f) {
e[++tot] = edge{v, f, head[u]};
head[u] = tot;
e[++tot] = edge{u, 0, head[v]};
head[v] = tot;
}
int d[N << 1], cur[N << 1];
inline bool bfs() {
fo(i, 1, vt) d[i] = -1, cur[i] = 0;
static queue<int> q;
while (!q.empty())
q.pop();
q.push(vs);
d[vs] = 0;
cur[vs] = head[vs];
while (!q.empty()) {
int u = q.front();
q.pop();
fe(i, u) {
int v = e[i].v, f = e[i].f;
if (f && !~d[v]) {
d[v] = d[u] + 1;
cur[v] = head[v];
if (v == vt)
return 1;
q.push(v);
}
}
}
return 0;
}
int dfs(int u, int a) {
if (!a || u == vt)
return a;
int ans = 0;
fe(i, u) {
int v = e[i].v, f = e[i].f;
if (f && d[v] == d[u] + 1) {
f = dfs(v, min(a, f));
if (!f)
continue;
e[i].f -= f;
e[i ^ 1].f += f;
ans += f;
a -= f;
if (!a)
break;
}
}
return ans;
}
int main() {
n = rd();
fo(i, 1, n) a[i] = rd() + 1;
fo(i, 1, n) b[i] = rd() + 1;
fo(i, 1, n) if (!vis[i]) {
if (a[i] == i)
continue;
++t1;
for (int x = i; !vis[x]; x = a[x]) {
vis[x] = 1, bel1[x] = t1;
if (b[x] == x)
++c1[t1];
}
}
memset(vis, 0, N);
fo(i, 1, n) if (!vis[i]) {
if (b[i] == i)
continue;
++t2;
for (int x = i; !vis[x]; x = b[x]) {
vis[x] = 1, bel2[x] = t2;
if (a[x] == x)
++c2[t2];
}
}
vs = t1 + t2 + 1;
vt = vs + 1;
fo(i, 1, t1) if (c1[i]) add(vs, i, c1[i]);
fo(i, 1, t2) if (c2[i]) add(i + t1, vt, c2[i]);
fo(i, 1, n) if (a[i] != i && b[i] != i) add(bel2[i] + t1, bel1[i], 1);
fo(i, 1, n) if (a[i] != i && b[i] != i && a[i] == b[i])
add(bel1[i], bel2[i] + t1, 1);
int ans = n;
fo(i, 1, n) if (a[i] == i && b[i] == i)-- ans;
while (bfs())
ans -= dfs(vs, 1e9);
cout << ans;
return 0;
}
| #include <bits/stdc++.h>
#define fo(i, l, r) for (int i = l; i <= r; i++)
#define of(i, l, r) for (int i = l; i >= r; i--)
#define fe(i, u) for (int i = head[u]; i; i = e[i].next)
#define el putchar('\n')
#define ta putchar(' ')
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
inline void open(const char *s) {
#ifndef ONLINE_JUDGE
char str[20];
sprintf(str, "%s.in", s);
freopen(str, "r", stdin);
// sprintf(str,"%s.out",s);
// freopen(str,"w",stdout);
#endif
}
inline int rd() {
static int x, f;
x = 0;
f = 1;
char ch = getchar();
for (; ch < '0' || ch > '9'; ch = getchar())
if (ch == '-')
f = -1;
for (; ch >= '0' && ch <= '9'; ch = getchar())
x = x * 10 + ch - '0';
return f > 0 ? x : -x;
}
const int N = 100010;
int n, a[N], b[N], t1, t2, bel1[N], bel2[N], c1[N], c2[N], vs, vt;
bool vis[N];
int head[N << 1], tot = 1;
struct edge {
int v, f, next;
} e[N * 20];
inline void add(int u, int v, int f) {
e[++tot] = edge{v, f, head[u]};
head[u] = tot;
e[++tot] = edge{u, 0, head[v]};
head[v] = tot;
}
int d[N << 1], cur[N << 1];
inline bool bfs() {
fo(i, 1, vt) d[i] = -1, cur[i] = 0;
static queue<int> q;
while (!q.empty())
q.pop();
q.push(vs);
d[vs] = 0;
cur[vs] = head[vs];
while (!q.empty()) {
int u = q.front();
q.pop();
fe(i, u) {
int v = e[i].v, f = e[i].f;
if (f && !~d[v]) {
d[v] = d[u] + 1;
cur[v] = head[v];
if (v == vt)
return 1;
q.push(v);
}
}
}
return 0;
}
int dfs(int u, int a) {
if (!a || u == vt)
return a;
int ans = 0;
for (int &i = cur[u]; i; i = e[i].next) {
int v = e[i].v, f = e[i].f;
if (f && d[v] == d[u] + 1) {
f = dfs(v, min(a, f));
if (!f)
continue;
e[i].f -= f;
e[i ^ 1].f += f;
ans += f;
a -= f;
if (!a)
break;
}
}
return ans;
}
int main() {
n = rd();
fo(i, 1, n) a[i] = rd() + 1;
fo(i, 1, n) b[i] = rd() + 1;
fo(i, 1, n) if (!vis[i]) {
if (a[i] == i)
continue;
++t1;
for (int x = i; !vis[x]; x = a[x]) {
vis[x] = 1, bel1[x] = t1;
if (b[x] == x)
++c1[t1];
}
}
memset(vis, 0, N);
fo(i, 1, n) if (!vis[i]) {
if (b[i] == i)
continue;
++t2;
for (int x = i; !vis[x]; x = b[x]) {
vis[x] = 1, bel2[x] = t2;
if (a[x] == x)
++c2[t2];
}
}
vs = t1 + t2 + 1;
vt = vs + 1;
fo(i, 1, t1) if (c1[i]) add(vs, i, c1[i]);
fo(i, 1, t2) if (c2[i]) add(i + t1, vt, c2[i]);
fo(i, 1, n) if (a[i] != i && b[i] != i) add(bel2[i] + t1, bel1[i], 1);
fo(i, 1, n) if (a[i] != i && b[i] != i && a[i] == b[i])
add(bel1[i], bel2[i] + t1, 1);
int ans = n;
fo(i, 1, n) if (a[i] == i && b[i] == i)-- ans;
while (bfs())
ans -= dfs(vs, 1e9);
cout << ans;
return 0;
}
| replace | 74 | 75 | 74 | 75 | TLE | |
p02908 | C++ | Time Limit Exceeded | #include <iostream>
#include <queue>
#include <vector>
#define llint long long
#define inf 1e18
using namespace std;
struct edge {
llint to, cap, rev;
edge(llint a, llint b, llint c) { to = a, cap = b, rev = c; }
};
int n;
llint p[100005], q[100005];
llint a[100005], b[100005];
vector<edge> G[100005];
int S, T;
llint level[100005], iter[100005];
void bfs(llint s) {
for (int i = 1; i <= T; i++)
level[i] = inf;
level[s] = 0;
queue<int> Q;
Q.push(s);
while (Q.size()) {
int v = Q.front();
Q.pop();
for (int i = 0; i < G[v].size(); i++) {
int u = G[v][i].to;
if (G[v][i].cap <= 0 || level[u] < inf)
continue;
level[u] = level[v] + 1;
Q.push(u);
}
}
}
llint dfs(int v, llint f) {
if (v == T)
return f;
llint ret;
for (int i = iter[v]; i < G[v].size(); i++) {
if (level[v] >= level[G[v][i].to] || G[v][i].cap <= 0)
continue;
ret = dfs(G[v][i].to, min(f, G[v][i].cap));
if (ret > 0) {
G[v][i].cap -= ret;
G[G[v][i].to][G[v][i].rev].cap += ret;
return ret;
}
}
return 0;
}
void add_edge(int s, int t, llint cap) {
G[s].push_back(edge(t, cap, G[t].size()));
G[t].push_back(edge(s, 0, G[s].size() - 1));
}
llint calc(llint p[], llint a[]) {
llint ret = 1;
for (int i = 1; i <= n; i++) {
if (a[i])
continue;
int v = i;
do {
a[v] = ret;
v = p[v];
} while (!a[v]);
ret++;
}
return ret - 1;
}
int main(void) {
cin >> n;
for (int i = 1; i <= n; i++)
cin >> p[i], p[i]++;
for (int i = 1; i <= n; i++)
cin >> q[i], q[i]++;
llint P = calc(p, a), Q = calc(q, b);
S = P + Q + 1, T = P + Q + 2;
int ans = 0;
for (int i = 1; i <= n; i++) {
if (p[i] == i && q[i] == i)
ans++;
else if (p[i] == i)
add_edge(P + b[i], T, 1);
else if (q[i] == i)
add_edge(S, a[i], 1);
else {
add_edge(P + b[i], a[i], 1);
if (p[i] == q[i])
add_edge(a[i], P + b[i], 1);
}
}
llint flow;
while (1) {
bfs(S);
if (level[T] >= inf)
break;
for (int i = 1; i <= T; i++)
iter[i] = 0;
while (1) {
flow = dfs(S, inf);
if (flow <= 0)
break;
ans += flow;
}
}
cout << n - ans << endl;
return 0;
} | #include <iostream>
#include <queue>
#include <vector>
#define llint long long
#define inf 1e18
using namespace std;
struct edge {
llint to, cap, rev;
edge(llint a, llint b, llint c) { to = a, cap = b, rev = c; }
};
int n;
llint p[100005], q[100005];
llint a[100005], b[100005];
vector<edge> G[100005];
int S, T;
llint level[100005], iter[100005];
void bfs(llint s) {
for (int i = 1; i <= T; i++)
level[i] = inf;
level[s] = 0;
queue<int> Q;
Q.push(s);
while (Q.size()) {
int v = Q.front();
Q.pop();
for (int i = 0; i < G[v].size(); i++) {
int u = G[v][i].to;
if (G[v][i].cap <= 0 || level[u] < inf)
continue;
level[u] = level[v] + 1;
Q.push(u);
}
}
}
llint dfs(int v, llint f) {
if (v == T)
return f;
llint ret;
for (llint &i = iter[v]; i < G[v].size(); i++) {
if (level[v] >= level[G[v][i].to] || G[v][i].cap <= 0)
continue;
ret = dfs(G[v][i].to, min(f, G[v][i].cap));
if (ret > 0) {
G[v][i].cap -= ret;
G[G[v][i].to][G[v][i].rev].cap += ret;
return ret;
}
}
return 0;
}
void add_edge(int s, int t, llint cap) {
G[s].push_back(edge(t, cap, G[t].size()));
G[t].push_back(edge(s, 0, G[s].size() - 1));
}
llint calc(llint p[], llint a[]) {
llint ret = 1;
for (int i = 1; i <= n; i++) {
if (a[i])
continue;
int v = i;
do {
a[v] = ret;
v = p[v];
} while (!a[v]);
ret++;
}
return ret - 1;
}
int main(void) {
cin >> n;
for (int i = 1; i <= n; i++)
cin >> p[i], p[i]++;
for (int i = 1; i <= n; i++)
cin >> q[i], q[i]++;
llint P = calc(p, a), Q = calc(q, b);
S = P + Q + 1, T = P + Q + 2;
int ans = 0;
for (int i = 1; i <= n; i++) {
if (p[i] == i && q[i] == i)
ans++;
else if (p[i] == i)
add_edge(P + b[i], T, 1);
else if (q[i] == i)
add_edge(S, a[i], 1);
else {
add_edge(P + b[i], a[i], 1);
if (p[i] == q[i])
add_edge(a[i], P + b[i], 1);
}
}
llint flow;
while (1) {
bfs(S);
if (level[T] >= inf)
break;
for (int i = 1; i <= T; i++)
iter[i] = 0;
while (1) {
flow = dfs(S, inf);
if (flow <= 0)
break;
ans += flow;
}
}
cout << n - ans << endl;
return 0;
} | replace | 46 | 47 | 46 | 47 | TLE | |
p02909 | C++ | Runtime Error | /* -> Written by <-
-----------
|K_A_Z_A_M_A|
|___________|
| ___ |
| (^_^) |
| /( | )\ |
|____|_|____|
H O A N G
*/
// i hope i get better
// Task name:
#define Task ""
using namespace std;
// Include
#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
// Define
#define reset(x, value) memset((x), (value), sizeof((x)))
#define fr(i, a, b) for (int i = (a); i <= (b); i++)
#define FR(i, a, b) for (int i = (a); i < (b); i++)
#define frr(i, a, b) for (int i = (a); i >= (b); i--)
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define nl "\n"
#define bit(x, i) (x >> i) & 1
#define all(x) (x).begin(), (x).end()
#define optimize(x) (x).resize(unique(all(x)) - (x).begin())
#define Inpf freopen(Task ".inp", "r", stdin)
#define Outf freopen(Task ".out", "w", stdout)
#define fasterIO \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
// typedef
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vii;
typedef vector<ll> vll;
const int base = 131;
const int MOD = 1e9 + 7;
const ll MM = 1ll * MOD * MOD;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
void AddMOD(int &x, int y) {
x += y;
if (x >= MOD)
x -= MOD;
}
int Gcd(int x, int y) {
if (y == 0)
return x;
return Gcd(y, x % y);
}
template <class T> inline void Gmax(T &x, T y) { x = max((x), (y)); }
template <class T> inline void Gmin(T &x, T y) { x = min((x), (y)); }
void exit_with_runtime() {
db x = 1.0 * clock() / CLOCKS_PER_SEC;
cerr << "\n\n---------------------\nTime elapsed: " << x << " s.\n\n";
if (x > 1)
cerr << "TIME LIMIT EXCEEDED!\n\n";
exit(0);
}
// --------------------------------------------------------------------
const int maxn = 2e5 + 5;
// --------------------------------------------------------------------
string str[] = {"Sunny", "Cloudy", "Rainy"};
void Solve() {
string s;
cin >> s;
for (int i = 0; i <= 2; i++)
if (s == str[i])
cout << str[(i + 1) % 3];
}
int main() {
#ifndef ONLINE_JUDGE
Inpf;
Outf;
#endif // ONLINE_JUDGE
fasterIO;
int test = 1;
// cin >> test;
while (test--)
Solve();
// Did you check with the special test case n = 0, n = 1??
exit_with_runtime();
}
// Goodbye, see u again
| /* -> Written by <-
-----------
|K_A_Z_A_M_A|
|___________|
| ___ |
| (^_^) |
| /( | )\ |
|____|_|____|
H O A N G
*/
// i hope i get better
// Task name:
#define Task ""
using namespace std;
// Include
#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
// Define
#define reset(x, value) memset((x), (value), sizeof((x)))
#define fr(i, a, b) for (int i = (a); i <= (b); i++)
#define FR(i, a, b) for (int i = (a); i < (b); i++)
#define frr(i, a, b) for (int i = (a); i >= (b); i--)
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define nl "\n"
#define bit(x, i) (x >> i) & 1
#define all(x) (x).begin(), (x).end()
#define optimize(x) (x).resize(unique(all(x)) - (x).begin())
#define Inpf freopen(Task ".inp", "r", stdin)
#define Outf freopen(Task ".out", "w", stdout)
#define fasterIO \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
// typedef
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vii;
typedef vector<ll> vll;
const int base = 131;
const int MOD = 1e9 + 7;
const ll MM = 1ll * MOD * MOD;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
void AddMOD(int &x, int y) {
x += y;
if (x >= MOD)
x -= MOD;
}
int Gcd(int x, int y) {
if (y == 0)
return x;
return Gcd(y, x % y);
}
template <class T> inline void Gmax(T &x, T y) { x = max((x), (y)); }
template <class T> inline void Gmin(T &x, T y) { x = min((x), (y)); }
void exit_with_runtime() {
db x = 1.0 * clock() / CLOCKS_PER_SEC;
cerr << "\n\n---------------------\nTime elapsed: " << x << " s.\n\n";
if (x > 1)
cerr << "TIME LIMIT EXCEEDED!\n\n";
exit(0);
}
// --------------------------------------------------------------------
const int maxn = 2e5 + 5;
// --------------------------------------------------------------------
string str[] = {"Sunny", "Cloudy", "Rainy"};
void Solve() {
string s;
cin >> s;
for (int i = 0; i <= 2; i++)
if (s == str[i])
cout << str[(i + 1) % 3];
}
int main() {
#ifndef ONLINE_JUDGE
// Inpf;
// Outf;
#endif // ONLINE_JUDGE
fasterIO;
int test = 1;
// cin >> test;
while (test--)
Solve();
// Did you check with the special test case n = 0, n = 1??
exit_with_runtime();
}
// Goodbye, see u again
| replace | 105 | 107 | 105 | 107 | 0 |
---------------------
Time elapsed: 0.043947 s.
|
p02909 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
vector<string> w = {"Sunny", "Cloudy", "Rainy", "Sunny"};
for (int i = 0; i < 4; i++) {
if (w[i] == s) {
cout << w[i + 1] << endl;
}
}
} | #include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
vector<string> w = {"Sunny", "Cloudy", "Rainy", "Sunny"};
for (int i = 0; i < 4; i++) {
if (w[i] == s) {
cout << w[i + 1] << endl;
break;
}
}
}
| insert | 11 | 11 | 11 | 12 | -11 | |
p02909 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
string S;
cin >> S;
vector<string> wea;
wea = {"Sunny", "Cloudy", "Rainy"};
for (int i = 0; i < 3; i++) {
if (S == wea[i])
cout << wea[i + 1] << endl;
}
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
string S;
cin >> S;
vector<string> wea;
wea = {"Sunny", "Cloudy", "Rainy"};
for (int i = 0; i < 3; i++) {
if (S == wea[i])
cout << wea[(i + 1) % 3] << endl;
}
}
| replace | 10 | 11 | 10 | 11 | 0 | |
p02909 | C++ | Runtime Error | /*
ID: varunra2
LANG: C++
TASK: codechef
*/
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits.h>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
using namespace std;
#define EPS 1e-9
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert(B <= A && A <= C)
#define INF (int)1e9
#define MEM(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define MP make_pair
#define PB push_back
#define PI 3.1415926535897932384626433832795
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
typedef pair<int, int> PII;
typedef vector<int> VI;
const double pi = acos(-1.0);
typedef long int int32;
typedef long long int int64;
typedef map<int, int> MPII;
typedef multiset<int> MSETI;
typedef set<int> SETI;
typedef set<string> SETS;
typedef unsigned long int uint32;
typedef unsigned long long int uint64;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef vector<string> VS;
#define watch(x) cerr << (#x) << " is " << (x) << endl
#define printvii(v) \
cerr << (#v) << "="; \
for_each(v.begin(), v.end(), [](PII i) -> void { \
cerr << "(" << i.first << "," << i.second << "),"; \
}); \
cerr << endl;
#define printvi(v) \
cerr << (#v) << "="; \
for_each(v.begin(), v.end(), [](int i) -> void { cerr << i << ","; }); \
cerr << endl;
int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
freopen("codechef.in", "r", stdin);
freopen("codechef.out", "w", stdout);
#endif
cin.sync_with_stdio(0);
cin.tie(0);
string s;
cin >> s;
if (s == "Sunny") {
cout << "Cloudy" << endl;
} else if (s == "Rainy") {
cout << "Sunny" << endl;
} else {
cout << "Rainy" << endl;
}
return 0;
}
| /*
ID: varunra2
LANG: C++
TASK: codechef
*/
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits.h>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
using namespace std;
#define EPS 1e-9
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert(B <= A && A <= C)
#define INF (int)1e9
#define MEM(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define MP make_pair
#define PB push_back
#define PI 3.1415926535897932384626433832795
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
typedef pair<int, int> PII;
typedef vector<int> VI;
const double pi = acos(-1.0);
typedef long int int32;
typedef long long int int64;
typedef map<int, int> MPII;
typedef multiset<int> MSETI;
typedef set<int> SETI;
typedef set<string> SETS;
typedef unsigned long int uint32;
typedef unsigned long long int uint64;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef vector<string> VS;
#define watch(x) cerr << (#x) << " is " << (x) << endl
#define printvii(v) \
cerr << (#v) << "="; \
for_each(v.begin(), v.end(), [](PII i) -> void { \
cerr << "(" << i.first << "," << i.second << "),"; \
}); \
cerr << endl;
#define printvi(v) \
cerr << (#v) << "="; \
for_each(v.begin(), v.end(), [](int i) -> void { cerr << i << ","; }); \
cerr << endl;
int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
// freopen("codechef.in", "r", stdin);
// freopen("codechef.out", "w", stdout);
#endif
cin.sync_with_stdio(0);
cin.tie(0);
string s;
cin >> s;
if (s == "Sunny") {
cout << "Cloudy" << endl;
} else if (s == "Rainy") {
cout << "Sunny" << endl;
} else {
cout << "Rainy" << endl;
}
return 0;
}
| replace | 74 | 76 | 74 | 76 | 0 | |
p02909 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
vector<string> weather = {"Sunny", "Cloudy", "Rainy"};
for (int i = 0; i < 3; i++) {
if (s == weather.at(i)) {
cout << weather.at(i + 1) << endl;
}
}
} | #include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
if (s == "Sunny") {
cout << "Cloudy" << endl;
}
if (s == "Cloudy") {
cout << "Rainy" << endl;
}
if (s == "Rainy") {
cout << "Sunny" << endl;
}
} | replace | 5 | 10 | 5 | 13 | 0 | |
p02909 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define fi first
#define se second
#define PI acos(-1.0)
#define pb push_back
#define mp make_pair
#define ll long long
#define pii pair<int, int>
#define lowbit(x) x & (-x)
using namespace std;
const int maxx = 1e6 + 5;
const int mod = 998244353;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
int n, m, T;
char s[100];
int main() {
int i, j, k;
string ans;
while (scanf(" %s", s)) {
if (s[0] == 'S')
ans = "Cloudy";
else if (s[0] == 'C')
ans = "Rainy";
else
ans = "Sunny";
cout << ans << endl;
}
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define fi first
#define se second
#define PI acos(-1.0)
#define pb push_back
#define mp make_pair
#define ll long long
#define pii pair<int, int>
#define lowbit(x) x & (-x)
using namespace std;
const int maxx = 1e6 + 5;
const int mod = 998244353;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
int n, m, T;
char s[100];
int main() {
int i, j, k;
string ans;
scanf(" %s", s);
if (s[0] == 'S')
ans = "Cloudy";
else if (s[0] == 'C')
ans = "Rainy";
else
ans = "Sunny";
cout << ans << endl;
return 0;
} | replace | 32 | 41 | 32 | 40 | TLE | |
p02909 | C++ | Runtime Error | /** Author: Dwai
* Omae wa mou shindeiru!
**/
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define ff first
#define ss second
#define all(a) (a).begin(), (a).end()
#define fr(i, a, b) for (i = a; i <= b; i++)
#define frr(i, a, b) for (i = a; i >= b; i--)
#define rep(i, n) for (i = 0; i < n; i++)
#define repr(i, n) for (i = n - 1; i >= 0; i--)
#define fast \
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); \
srand(time(NULL));
#define flushinput cin.ignore(numeric_limits<streamsize>::max(), '\n');
void debug_out() { cerr << endl; }
void clock_out() {
cerr << "\nTime Elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " s\n";
}
void fileio() {
freopen("/home/dwai/Desktop/cp/input.txt", "r", stdin);
freopen("/home/dwai/Desktop/cp/output.txt", "w", stdout);
freopen("/home/dwai/Desktop/cp/debug.txt", "w", stderr);
}
void ofileio() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
cerr << " " << H;
debug_out(T...);
}
#ifndef ONLINE_JUDGE
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define fio() fileio()
#define ofio() fileio()
#define clock() clock_out()
#else
#define debug(...) 42
#define fio() 42
#define ofio() ofileio()
#define clock() 42
#endif
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;
typedef vector<pll> vll;
typedef map<ll, ll> mll;
int IT_MAX = 1 << 20;
const ll MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const db PI = acos(-1);
const db ERR = 1e-10;
void solve() {
string s;
cin >> s;
string arr[3] = {"Sunny", "Cloudy", "Rainy"};
int in = find(arr, arr + 3, s) - arr;
in = (in + 1) % 3;
cout << arr[in];
}
int main() {
fio();
fast
solve();
return 0;
} | /** Author: Dwai
* Omae wa mou shindeiru!
**/
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define ff first
#define ss second
#define all(a) (a).begin(), (a).end()
#define fr(i, a, b) for (i = a; i <= b; i++)
#define frr(i, a, b) for (i = a; i >= b; i--)
#define rep(i, n) for (i = 0; i < n; i++)
#define repr(i, n) for (i = n - 1; i >= 0; i--)
#define fast \
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); \
srand(time(NULL));
#define flushinput cin.ignore(numeric_limits<streamsize>::max(), '\n');
void debug_out() { cerr << endl; }
void clock_out() {
cerr << "\nTime Elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " s\n";
}
void fileio() {
freopen("/home/dwai/Desktop/cp/input.txt", "r", stdin);
freopen("/home/dwai/Desktop/cp/output.txt", "w", stdout);
freopen("/home/dwai/Desktop/cp/debug.txt", "w", stderr);
}
void ofileio() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
cerr << " " << H;
debug_out(T...);
}
#ifndef ONLINE_JUDGE
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define fio() fileio()
#define ofio() fileio()
#define clock() clock_out()
#else
#define debug(...) 42
#define fio() 42
#define ofio() ofileio()
#define clock() 42
#endif
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;
typedef vector<pll> vll;
typedef map<ll, ll> mll;
int IT_MAX = 1 << 20;
const ll MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const db PI = acos(-1);
const db ERR = 1e-10;
void solve() {
string s;
cin >> s;
string arr[3] = {"Sunny", "Cloudy", "Rainy"};
int in = find(arr, arr + 3, s) - arr;
in = (in + 1) % 3;
cout << arr[in];
}
int main() {
// fio();
fast
solve();
return 0;
} | replace | 83 | 84 | 83 | 84 | 0 | |
p02909 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
string s;
string weather[4];
weather[0] = "Sunny";
weather[1] = "Cloudy";
weather[2] = "Rainy";
weather[3] = weather[0];
cin >> s;
for (int i = 0; i < 4; i++) {
if (s == weather[i]) {
cout << weather[i + 1] << endl;
continue;
}
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
string s;
string weather[4];
weather[0] = "Sunny";
weather[1] = "Cloudy";
weather[2] = "Rainy";
weather[3] = weather[0];
cin >> s;
for (int i = 0; i < 3; i++) {
if (s == weather[i]) {
cout << weather[i + 1] << endl;
continue;
}
}
return 0;
}
| replace | 10 | 11 | 10 | 11 | 0 | |
p02909 | C++ | Runtime Error | #include <stdio.h>
#define ll long long int
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main() {
string *k;
getline(cin, k[0]);
k[0] = "Sunny";
k[1] = "Cloudy";
k[2] = "Rainy";
string j;
cin >> j;
if (j == k[0]) {
cout << k[1];
} else if (j == k[1]) {
cout << k[2];
} else if (j == k[2]) {
cout << k[0];
}
} | #include <stdio.h>
#define ll long long int
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main() {
string k[4] = {"Sunny", "Cloudy", "Rainy"};
string j;
cin >> j;
if (j == k[0]) {
cout << k[1];
} else if (j == k[1]) {
cout << k[2];
} else if (j == k[2]) {
cout << k[0];
}
} | replace | 8 | 13 | 8 | 9 | -11 | |
p02909 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
typedef pair<int, int> P;
const int INF = 1001001001;
const long double PI = (acos(-1));
const int mod = 1e9 + 7;
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
string s[3] = {"Sunny", "Cloudy", "Rainy"};
string a;
cin >> a;
rep(i, 3) {
if (a == s[i]) {
cout << s[i + 1] << endl;
return 0;
}
}
cout << endl;
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
typedef pair<int, int> P;
const int INF = 1001001001;
const long double PI = (acos(-1));
const int mod = 1e9 + 7;
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
string s[3] = {"Sunny", "Cloudy", "Rainy"};
string a;
cin >> a;
rep(i, 3) {
if (a == s[i]) {
if (i == 2) {
cout << s[0] << endl;
return 0;
}
cout << s[i + 1] << endl;
return 0;
}
}
cout << endl;
return 0;
}
| insert | 24 | 24 | 24 | 28 | 0 | |
p02909 | C++ | Runtime Error | #define __USE_MINGW_ANSI_STDIO
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
typedef long long ll;
typedef long long ull;
typedef pair<ll, ll> ii;
#define all(v) ((v).begin()), ((v).end())
#define sz(v) ((int)((v).size()))
#define endl "\n"
#define fx(n) fixed << setprecision(n)
#define mk make_pair
void fast() {
ios::sync_with_stdio(NULL);
cout.tie(NULL);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("output.out", "w", stdout);
freopen("input.in", "r", stdin);
#endif
/*#ifdef ONLINE_JUDGE
//freopen("output.txt", "w", stdout);
freopen("path.in", "r", stdin);
#endif*/
}
const double pi = 2 * acos(0.0);
const ll oo = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int nn = 1e3 + 15;
int dx[8] = {1, -1, 0, 0, 1, -1, 1, -1};
int dy[8] = {0, 0, -1, 1, -1, 1, 1, -1};
int main() {
fast();
string s;
cin >> s;
string arr[] = {"Sunny", "Cloudy", "Rainy"};
int idx = find(arr, arr + 2, s) - arr;
cout << arr[(idx + 1) % 3] << endl;
return 0;
} | #define __USE_MINGW_ANSI_STDIO
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
typedef long long ll;
typedef long long ull;
typedef pair<ll, ll> ii;
#define all(v) ((v).begin()), ((v).end())
#define sz(v) ((int)((v).size()))
#define endl "\n"
#define fx(n) fixed << setprecision(n)
#define mk make_pair
void fast() {
ios::sync_with_stdio(NULL);
cout.tie(NULL);
cin.tie(NULL);
/*#ifndef ONLINE_JUDGE
freopen("output.out", "w", stdout);
freopen("input.in", "r", stdin);
#endif
#ifdef ONLINE_JUDGE
//freopen("output.txt", "w", stdout);
freopen("path.in", "r", stdin);
#endif*/
}
const double pi = 2 * acos(0.0);
const ll oo = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int nn = 1e3 + 15;
int dx[8] = {1, -1, 0, 0, 1, -1, 1, -1};
int dy[8] = {0, 0, -1, 1, -1, 1, 1, -1};
int main() {
fast();
string s;
cin >> s;
string arr[] = {"Sunny", "Cloudy", "Rainy"};
int idx = find(arr, arr + 2, s) - arr;
cout << arr[(idx + 1) % 3] << endl;
return 0;
} | replace | 22 | 30 | 22 | 30 | 0 | |
p02909 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
string w;
cin >> w;
if (w == "Sunny")
cout << "Cloudy";
else if (w == "Cloudy")
cout << "Rainy";
else if (w == "Rainy")
cout << "Sunny";
} | #include <bits/stdc++.h>
using namespace std;
int main() {
// #ifndef ONLINE_JUDGE
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// #endif
// ios_base::sync_with_stdio(0);
// cin.tie(0);
string w;
cin >> w;
if (w == "Sunny")
cout << "Cloudy";
else if (w == "Cloudy")
cout << "Rainy";
else if (w == "Rainy")
cout << "Sunny";
} | replace | 3 | 9 | 3 | 9 | 0 | |
p02909 | C++ | Runtime Error | /// AtCoder Beginner Contest 141
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
string value[3] = {"Sunny", "Cloudy", "Rainy"};
string str;
cin >> str;
for (int i = 0; i < str.size(); i++) {
if (str == value[i]) {
cout << value[(i + 1) % 3] << "\n";
}
}
return 0;
}
| /// AtCoder Beginner Contest 141
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
string value[3] = {"Sunny", "Cloudy", "Rainy"};
string str;
cin >> str;
for (int i = 0; i < 3; i++) {
if (str == value[i]) {
cout << value[(i + 1) % 3] << "\n";
}
}
return 0;
}
| replace | 14 | 15 | 14 | 15 | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.