#include <bits/stdc++.h>
#define F first
#define S second
#define ll long long
#define nd '\n'
using namespace std;
vector <long long> plants;
vector <ll> finale;
ll k;
ll n;
ll invalido = false;
struct nodo {
ll limIz, limDe;
ll lazy;
ll v;
ll medio;
ll minimo;
ll posMinimo;
nodo *left, *right;
nodo(ll iz, ll de){ limIz = iz, limDe = de;}
void build (){
if (limIz == limDe){
v = plants[limIz];
minimo = v;
posMinimo = limIz;
return;
}
medio = (limDe+limIz)/2;
left = new nodo(limIz, medio);
right = new nodo(medio+1, limDe);
left->build();
right->build();
v = left->v + right-> v;
if (left->minimo <= right->minimo)
minimo = left->minimo, posMinimo = left->posMinimo;
else minimo = right->minimo, posMinimo = right->posMinimo;
//cout << "nodo: " << limIz << " " << limDe << nd;
//cout << "v: " << v << " minimo: " << minimo << " pos: " << posMinimo<< nd;
}
void updateLazy(){
if (lazy == 0) return;
v+=(limDe-limIz+1)*lazy;
minimo+=lazy;
//cout << "lazy " << lazy << " min " << minimo << nd;
if (limIz != limDe) left->lazy+=lazy, right->lazy+=lazy;
lazy=0;
}
void update (ll upL, ll upR, ll v){
//cout << limIz << " entrandou " << limDe << nd;
updateLazy();
if (upL <= limIz && limDe <= upR){
lazy+=v;
//cout << limIz << " laziando " << limDe << nd;
updateLazy();
return;
}
if (limIz > upR || limDe < upL) return;
left->update(upL, upR, v);
right->update(upL, upR, v);
v = left->v + right->v;
if (left->minimo <= right->minimo)
minimo = left->minimo, posMinimo = left->posMinimo;
else minimo = right->minimo, posMinimo = right->posMinimo;
//cout << "nodo: " << limIz << " " << limDe << nd;
//cout << "v: " << v << " minimo: " << minimo << nd;
}
pair <ll,ll> query (ll qL, ll qR){
//cout << limIz << " entrando " << limDe << nd;
updateLazy();
if (qL <= limIz && limDe <= qR){
//cout << "query: " << limIz << " " << limDe << nd;
//cout << minimo << " x " << posMinimo << nd;
return {minimo, posMinimo};
}
if (limIz > qR || limDe < qL) return {(long long) INT_MAX, -1};
pair <ll,ll> a = left->query(qL, qR);
pair <ll,ll> b = right->query(qL, qR);
//cout << "query: " << limIz << " " << limDe << nd;
//cout << a.F << " " << a.S << " " << b.F << " " << b.S << nd;
if (a.F <= b.F) return a;
else return b;
}
};
pair<ll,ll> procesarConsulta(long long l, long long r, nodo *st){
//cout << l << " " << r << nd;
if (l <= r) return st->query(l, r-1);
pair <ll,ll> a = st->query(l, n-1);
if (r == 0) return a;
pair <ll,ll> b = st->query(0, r-1);
if (a.F <= b.F) return a;
else return b;
}
void procesarUpdate(long long l, long long r, nodo *st){
//cout << "actualizando" << l << " " << r << nd;
if (l <= r){ st->update(l, r-1, -1); return;}
st->update(l, n-1, -1);
if (r == 0) return;
st->update(0, r-1, -1);
}
nodo *st;
void init (int K, vector <int> r){
for (auto x: r) plants.push_back(x);
k=K;
n = r.size();
st = new nodo(0, n-1);
st->build();
finale.assign(n,1);
ll orden=0;
pair <ll,ll> cero = st->query(0, n-1);
//cout << 1 << nd;
auto posible = procesarConsulta((cero.S-k+1+n)%n, cero.S, st);
//cout << "posible " << posible.F << " " << posible.S << nd;
if (posible.F == 0) cero = posible;
for (ll i=0; i<n; i++){
//cout << i << nd;
procesarUpdate((cero.S-k+1+n)%n, cero.S, st);
//cout << "aaa" << nd;
st->update(cero.S, cero.S, (long long)INT_MAX);
finale[cero.S] = orden--;
vector <pair <ll,ll>> s;
for (ll i=0; i<n; i++) {
s.push_back(st->query(i,i));
}
//for (ll i=0; i<n; i++) {
//cout << s[i].F << " ";
//}
//cout << nd;
//for (ll i=0; i<n; i++) cout << finale[i] << " ";
//cout << nd;
if (i == n-1) break;
cero = procesarConsulta((cero.S-k+1+n)%n, cero.S, st);
if (cero.F != 0) cero = st->query(0, n-1);
auto posible = procesarConsulta((cero.S-k+1+n)%n, cero.S, st);
//cout << cero.S << " ceros " << posible.S << nd;
if (posible.F == 0) cero = posible;
if (cero.F != 0) invalido = true;
}
}
long long compare_plants(int x, int y){
if (invalido) return 0;
if (finale[x] > finale[y]) return 1;
else return -1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |