# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
98848 | 2019-02-26T14:39:05 Z | janchomath | Xylophone (JOI18_xylophone) | C++14 | 0 ms | 0 KB |
#include "grader.cpp" #include<bits/stdc++.h> #define ll long long using namespace std; bool fix[200005]; ll ans[200005],ind; void solve(int n) { ll l = 2,r = n,mid = 0; while(l <= r){ mid = (l + r) / 2; ll curans = query(1,mid); // cout << 1 << " " << mid << " " << curans << endl; if(curans != n - 1){ l = mid + 1; } else { ind = mid; r = mid - 1; } } ans[ind] = n; fix[n] = 1; if(ind != n){ ans[ind + 1] = n - query(ind,ind + 1); } for(int i=ind+1; i<=n; i++){ fix[ans[i]] = 1; if(i == n){ break; } ll k1 = query(i,i + 1); if(ans[i] + k1 >= n || fix[ans[i] + k1]){ ans[i + 1] = ans[i] - k1; continue; } if(ans[i] - k1 <= 0 || fix[ans[i] - k1]){ ans[i + 1] = ans[i] + k1; continue; } ll k2 = query(i - 1,i + 1); if(ans[i - 1] < ans[i]){ if(k2 == k1 + ans[i] - ans[i - 1]){ ans[i + 1] = ans[i] + k1; } else { ans[i + 1] = ans[i] - k1; } } else { if(k2 == k1 + ans[i - 1] - ans[i]){ ans[i + 1] = ans[i] - k1; } else { ans[i + 1] = ans[i] + k1; } } } if(ind > 1){ ans[ind - 1] = n - query(ind-1,ind); } for(int i=ind - 1; i>=1; i--){ fix[ans[i]] = 1; // cout << i << " " << ans[i] << endl; if(i == 1){ break; } ll k1 = query(i - 1,i); if(ans[i] + k1 >= n || fix[ans[i] + k1]){ ans[i - 1] = ans[i] - k1; continue; } if(ans[i] - k1 <= 0 || (fix[ans[i] - k1] && ans[i] - k1 >= 0)){ ans[i - 1] = ans[i] + k1; continue; } // cout << "ET\n"; ll k2 = query(i - 1,i + 1); if(ans[i + 1] < ans[i]){ if(k2 == k1 + ans[i] - ans[i + 1]){ ans[i - 1] = ans[i] + k1; } else { ans[i - 1] = ans[i] - k1; } } else { if(k2 == k1 + ans[i + 1] - ans[i]){ ans[i - 1] = ans[i] - k1; } else { ans[i - 1] = ans[i] + k1; } } } for(int i=1; i<=n; i++){ // cout << ans[i] << endl; answer(i,ans[i]); } }