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 "minerals.h"
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstdlib>
#define X first
#define Y second
#define PB push_back
using namespace std;
typedef long long ll;
typedef pair < int, int > pii;
const int N = 1e5 + 500;
int kod[N], tp[N], P[N], ans[N], izb[N];
int un[N], lst = 0, mks = 0, n;
vector < int > kol[20][N];
int pitaj(int x){
int nw = Query(x);
int ret = abs(lst - nw);
lst = nw; un[x] = !un[x];
return ret;
}
void pripremi(int l, int r, int raz, int zad){
if(l == r) return;
mks = max(mks, raz);
int rez = (l + r) / 2;
if(zad){
while(rez < r && ((rez - l + 1) & (rez - l)) != 0)
rez++;
}
else{
while(rez >= l && ((r - rez) & (r - rez - 1)) != 0)
rez--;
}
for(int i = l;i <= rez;i++)
kod[i] += (1 << raz);
pripremi(l, rez, raz + 1, 0);
pripremi(rez + 1, r, raz + 1, 1);
}
void odradi(int red){
for(int i = 1;i <= 2 * n;i++){
if(!izb[i] && !tp[i] && (un[i] ^ (!!(P[i] & (1 << red)))))
pitaj(i);
}
for(int i = 1;i <= 2 * n;i++)
if(tp[i] && !ans[i]){
int odg = !pitaj(i);
P[i] += odg * (1 << red);
}
}
inline void mozda(int red){
for(int i = 2 * n;i >= 1;i--){
if(!tp[i] || ans[i]) continue;
while(izb[kol[red][P[i]].back()]) kol[red][P[i]].pop_back();
if(kol[red][P[i]].size() == 1 || (kol[red][P[i]][(int)kol[red][P[i]].size() - 2] < i)){
ans[i] = *lower_bound(kol[red][P[i]].begin(), kol[red][P[i]].end(), i);
izb[ans[i]] = 1;
}
}
for(int i = 1;i <= 2 * n;i++){
if(!tp[i] || ans[i]) continue;
while(izb[kol[red][P[i]].back()]) kol[red][P[i]].pop_back();
if(kol[red][P[i]].size() == 1 || (kol[red][P[i]][(int)kol[red][P[i]].size() - 2] < i)){
ans[i] = kol[red][P[i]].back();
izb[ans[i]] = 1;
}
}
}
bool cmp(int x, int y){
return P[x] < P[y];
}
void Solve(int nn) {
n = nn;
pripremi(0, n - 1, 0, 1);
int cur = 0;
for(int i = 1;i <= 2 * n;i++){
tp[i] = pitaj(i);
if(!tp[i]){
P[i] = kod[cur++];
for(int j = 0;j <= mks + 1;j++)
kol[j][P[i] & ((1 << j) - 1)].PB(i);
}
}
mozda(0); mozda(0); mozda(0);
for(int i = 0;i <= mks;i++){
odradi(i);
mozda(i + 1); mozda(i + 1); mozda(i + 1);
}
for(int i = 1;i <= 2 * n;i++){
//if(tp[i]) printf("%d %d\n", i, ans[i]);
if(tp[i]) Answer(i, ans[i]);
}
}
# | 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... |