This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
# include <bits/stdc++.h>
using namespace std;
using ll = long long;
using db = long double; // or double, if TL is tight
using str = string; // yay python!
// pairs
using pii = pair<ll,int>;
using pl = pair<ll,ll>;
using pd = pair<db,db>;
#define mp make_pair
#define f first
#define s second
#define tcT template<class T
#define tcTU tcT, class U
// ^ lol this makes everything look weird but I'll try it
tcT> using V = vector<T>;
tcT, size_t SZ> using AR = array<T,SZ>;
using vi = V<int>;
using vb = V<bool>;
using vd = V<db>;
using vs = V<str>;
using vpi = V<pii>;
using vpl = V<pl>;
using vpd = V<pd>;
// vectors
// oops size(x), rbegin(x), rend(x) need C++17
#define sz(x) int((x).size())
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define rall(x) x.rbegin(), x.rend()
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert
#define pb push_back
#define eb emplace_back
#define ft front()
#define bk back()
#define lb lower_bound
#define ub upper_bound
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define rep(a) F0R(_,a)
#define each(a,x) for (auto& a: x)
const int MOD = 998244353;
const int N = 3e5 + 5;
const ll inf = 1e9; // not too close to LLONG_MAX
const db PI = acos((db)-1);
const int dx[4]{1,0,-1,0}, dy[4]{0,1,0,-1}; // for every grid problem!!
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
template<class T> using pqg = priority_queue<T,vector<T>,greater<T>>;
struct DSU {
vi e; void init(int N) { e = vi(N,-1); }
int get(int x) { return e[x] < 0 ? x : e[x] = get(e[x]); }
bool sameSet(int a, int b) { return get(a) == get(b); }
int size(int x) { return -e[get(x)]; }
bool unite(int x, int y) { // union by size
x = get(x), y = get(y); if (x == y) return 0;
if (e[x] > e[y]) swap(x,y);
e[x] += e[y]; e[y] = x; return 1;
}
};
/*
inline namespace Helpers {
//////////// is_iterable
// https://stackoverflow.com/questions/13830158/check-if-a-variable-type-is-iterable
// this gets used only when we can call begin() and end() on that type
tcT, class = void> struct is_iterable : false_type {};
tcT> struct is_iterable<T, void_t<decltype(begin(declval<T>())),
decltype(end(declval<T>()))
>
> : true_type {};
tcT> constexpr bool is_iterable_v = is_iterable<T>::value;
//////////// is_readable
tcT, class = void> struct is_readable : false_type {};
tcT> struct is_readable<T,
typename std::enable_if_t<
is_same_v<decltype(cin >> declval<T&>()), istream&>
>
> : true_type {};
tcT> constexpr bool is_readable_v = is_readable<T>::value;
//////////// is_printable
// // https://nafe.es/posts/2020-02-29-is-printable/
tcT, class = void> struct is_printable : false_type {};
tcT> struct is_printable<T,
typename std::enable_if_t<
is_same_v<decltype(cout << declval<T>()), ostream&>
>
> : true_type {};
tcT> constexpr bool is_printable_v = is_printable<T>::value;
}*/
using ll = long long;
using db = long double; // or double, if TL is tight
using str = string; // yay python!
// pairs
using pii = pair<ll,int>;
using pl = pair<ll,ll>;
using pd = pair<db,db>;
#define mp make_pair
#define f first
#define s second
#define tcT template<class T
#define tcTU tcT, class U
// ^ lol this makes everything look weird but I'll try it
tcT> using V = vector<T>;
tcT, size_t SZ> using AR = array<T,SZ>;
using vi = V<int>;
using vb = V<bool>;
using vd = V<db>;
using vs = V<str>;
using vpi = V<pii>;
using vpl = V<pl>;
using vpd = V<pd>;
// vectors
// oops size(x), rbegin(x), rend(x) need C++17
#define sz(x) int((x).size())
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define rall(x) x.rbegin(), x.rend()
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert
#define pb push_back
#define eb emplace_back
#define ft front()
#define bk back()
#define lb lower_bound
#define ub upper_bound
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define rep(a) F0R(_,a)
#define each(a,x) for (auto& a: x)
template<class T> using pqg = priority_queue<T,vector<T>,greater<T>>;
/*
inline namespace Helpers {
//////////// is_iterable
// https://stackoverflow.com/questions/13830158/check-if-a-variable-type-is-iterable
// this gets used only when we can call begin() and end() on that type
tcT, class = void> struct is_iterable : false_type {};
tcT> struct is_iterable<T, void_t<decltype(begin(declval<T>())),
decltype(end(declval<T>()))
>
> : true_type {};
tcT> constexpr bool is_iterable_v = is_iterable<T>::value;
//////////// is_readable
tcT, class = void> struct is_readable : false_type {};
tcT> struct is_readable<T,
typename std::enable_if_t<
is_same_v<decltype(cin >> declval<T&>()), istream&>
>
> : true_type {};
tcT> constexpr bool is_readable_v = is_readable<T>::value;
//////////// is_printable
// // https://nafe.es/posts/2020-02-29-is-printable/
tcT, class = void> struct is_printable : false_type {};
tcT> struct is_printable<T,
typename std::enable_if_t<
is_same_v<decltype(cout << declval<T>()), ostream&>
>
> : true_type {};
tcT> constexpr bool is_printable_v = is_printable<T>::value;
}*/
int t, n, a[N], k;
struct tournament_
{
vector<pii> pr;
vector<pii> sf;
int ans;
};
tournament_ tree[4 *N];
void merge(int node)
{
tree[node].pr = tree[2 *node].pr;
tree[node].sf = tree[2 *node + 1].sf;
tree[node].ans = min(tree[2 *node].ans, tree[2 *node + 1].ans);
for (pii x: tree[2 *node + 1].pr)
{
long long mask = x.f;
int idx = x.s;
if (tree[node].pr.size())
{
long long curmask = tree[node].pr.back().f;
if ((curmask & mask) == mask) continue;
tree[node].pr.pb({ curmask | mask, idx });
}
else
{
tree[node].pr.pb({ mask, idx });
}
}
for (pii x: tree[2 *node].sf)
{
long long mask = x.f;
int idx = x.s;
if (tree[node].sf.size())
{
long long curmask = tree[node].sf.back().f;
if ((curmask & mask) == mask) continue;
tree[node].sf.pb({ curmask | mask, idx });
}
else
{
tree[node].sf.pb({ mask, idx });
}
}
if (tree[node].pr.size() > k) assert(false);
if (tree[node].pr.back().f != ((1LL << k) - 1))
{
tree[node].ans = inf;
return;
}
if (tree[node].pr.size() < k) assert(false);
int cur = 0;
reverse(tree[2 *node].sf.begin(), tree[2 *node].sf.end());
for (pii x: tree[2 *node + 1].pr)
{
long long mask = x.f;
int idx = x.s;
while ((tree[2 *node].sf[cur].f | mask) == ((1LL << k) - 1) && cur < tree[2 *node].sf.size())
{
cur++;
}
if (cur < tree[2 *node].sf.size() && (tree[2 *node].sf[cur].f | mask) == ((1LL << k) - 1)) tree[node].ans = min(tree[node].ans, idx - tree[2 *node].sf[cur].s + 1);
if (cur && (tree[2 *node].sf[cur - 1].f | mask) == ((1LL << k) - 1)) tree[node].ans = min(tree[node].ans, idx - tree[2 *node].sf[cur - 1].s + 1);
}
reverse(tree[2 *node].sf.begin(), tree[2 *node].sf.end());
return;
}
void build(int node, int le, int ri)
{
if (le == ri)
{
tree[node].pr.pb({ (1LL << a[le]), le });
tree[node].sf.pb({ (1LL << a[le]), le });
if (k == 1) tree[node].ans = 1;
else tree[node].ans = inf;
return;
}
int mid = (le + ri) / 2;
build(2 *node, le, mid);
build(2 *node + 1, mid + 1, ri);
merge(node);
}
void update(int node, int le, int ri, int idx, int val)
{
if (le > idx || ri < idx) return;
if (le == ri)
{
tree[node].pr.clear();
tree[node].sf.clear();
tree[node].pr.pb({ (1LL << a[le]), le });
tree[node].sf.pb({ (1LL << a[le]), le });
if (k == 1) tree[node].ans = 1;
else tree[node].ans = inf;
return;
}
int mid = (le + ri) / 2;
update(2 *node, le, mid, idx, val);
update(2 *node + 1, mid + 1, ri, idx, val);
merge(node);
}
main()
{
ios_base::sync_with_stdio(false),
cin.tie(NULL),
cout.tie(NULL);
int q;
cin >> n >> k >> q;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
a[i]--;
}
build(1, 1, n);
for (int i = 1; i <= q; i++)
{
int type_;
cin >> type_;
if (type_ == 2) cout << (tree[1].ans == inf ? -1 : tree[1].ans) << "\n";
else
{
int ID, VAL;
cin >> ID >> VAL;
VAL--;
a[ID] = VAL;
update(1, 1, n, ID, VAL);
}
}
}
Compilation message (stderr)
nekameleoni.cpp: In function 'void merge(int)':
nekameleoni.cpp:224:27: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<long long int, int>, std::allocator<std::pair<long long int, int> > >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
224 | if (tree[node].pr.size() > k) assert(false);
| ~~~~~~~~~~~~~~~~~~~~~^~~
nekameleoni.cpp:231:27: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<long long int, int>, std::allocator<std::pair<long long int, int> > >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
231 | if (tree[node].pr.size() < k) assert(false);
| ~~~~~~~~~~~~~~~~~~~~~^~~
nekameleoni.cpp:238:70: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int>, std::allocator<std::pair<long long int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
238 | while ((tree[2 *node].sf[cur].f | mask) == ((1LL << k) - 1) && cur < tree[2 *node].sf.size())
| ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
nekameleoni.cpp:243:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int>, std::allocator<std::pair<long long int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
243 | if (cur < tree[2 *node].sf.size() && (tree[2 *node].sf[cur].f | mask) == ((1LL << k) - 1)) tree[node].ans = min(tree[node].ans, idx - tree[2 *node].sf[cur].s + 1);
| ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
nekameleoni.cpp: At global scope:
nekameleoni.cpp:288:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
288 | main()
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |