「JOISC 2016 Day 3」回转寿司
这题我无力吐槽了...
强烈谴责出题人用脚造数据
解法
其实这题主要还是部分分启发正解吧。看到有个\(s_i = 1, t_i= n\)的做法就是维护一个堆就可以了,所以扩展下就是分块,然后每个块维护一个堆。散块暴力,大块直接查。但是有个很坑爹的问题在于,对于整块的部分我们没办法去修改,这个就很难受了。如果不去修改,在查询这个块为零散块的时候就没法暴力了。这也是我考试的时候的瓶颈。所以我这里觉得题解还是很妙的。把每一次修改的标记打在块上,对每个块开个\(vector\)维护这个块上的标记。在需要对这个块暴力的时候就进行下传。下传的时候也是从左到右从小到大更新就做完了。本来我看前方大佬被卡常数,我就写了带删除的堆,没想到这个地方要构造小根堆又把我玩炸了。于是看看标程,发现这个写法还挺香的
代码
#include <cstdio>
#include <cmath>
#include <queue>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
#define R register
#define LL long long
const int inf = 0x3f3f3f3f;
const int N = 4e5 + 10;
const int B = 800 + 5;
inline int read() {
char a = getchar(); int x = 0,f = 1;
for(; a > '9' || a < '0'; a = getchar()) if(a == '-') f = -1;
for(; a >= '0' && a <= '9' ;a = getchar()) x = x * 10 + a - '0';
return x * f;
}
int n, q, Siz;
int whi[N], a[N];
priority_queue<int> Q[B];
vector<int> Tag[B];
inline void build(int id) {
int l = (id - 1) * Siz + 1, r = min( id * Siz, n);
Q[id] = priority_queue<int>(a + l, a + r + 1);
}
inline void DEBUG() {
for(R int i = 1; i <= 4; i ++) {
while(Q[i].size()) printf("%d\n",Q[i].top()), Q[i].pop();
}
}
inline void Init() {
n = read(); q = read(); Siz = sqrt(n);
for(R int i = 1; i <= n; i ++) whi[i] = (i - 1) / Siz + 1;
for(R int i = 1; i <= n; i ++) a[i] = read();
for(R int i = 1; i <= whi[n]; i ++) build(i);
}
inline void rebuild(int id) {
if(Tag[id].empty()) return ;
int l = (id - 1) * Siz + 1, r = min(id * Siz, n);
priority_queue<int, vector<int>, greater<int> > QT(Tag[id].begin(), Tag[id].end());
for(R int i = l; i <= r; i ++) {
int y = QT.top();
if(y < a[i]) {
QT.pop();
swap(a[i], y);
QT.push(y);
}
}
build(id);
Tag[id].clear();
}
inline int oper(int id, int p) {
int x = Q[id].top();
if(p >= x) return p;
Tag[id].push_back(p);
Q[id].pop();
Q[id].push(p);
return x;
}
inline int ask(int s, int t, int p) {
int l = whi[s], r = whi[t];
rebuild(l); rebuild(r);
for(R int i = s; i <= l * Siz && i <= t; i ++) if(p < a[i]) swap(p, a[i]);
build(l);
for(R int i = l + 1; i <= r - 1; i ++) p = oper(i, p);
if(r > l) {
for(R int i = (r - 1) * Siz + 1; i <= t; i ++) if(p < a[i]) swap(p, a[i]);
build(r);
}
return p;
}
inline void Solve() {
while(q --) {
int l = read(), r = read(), p = read();
if(l <= r) printf("%d\n", ask(l, r, p));
else {
int tp = ask(l, n, p);
printf("%d\n", ask(1, r, tp));
}
}
}
int main() {
freopen("3.in","r",stdin);
//freopen("sushi.out","w",stdout);
Init();
Solve();
return 0;
}