题解:P15298 [ROI 2012 Day 2] mosaic 马赛克
lanchenhao2010 · · 题解
鲜花
这题曾经出现在几百年前我们的校内模拟赛中,然而数据量比这里狂野,各种
分析
首先,一个很自然的想法是求出所有不和谐对,然而这样的对数显然可以被卡到
然后,我们发现题目只要求出一个所求的不和谐对,所以我们可以不用保留那么多对。考虑不和谐对中右端点相同的某些对,为了其能够套在询问的
那怎么求呢?设
别急,我们先画个示意:
如此所示,首先容易知道此时不可能
处理出所有有用的对后,怎么处理询问呢?由于是个静态问题,没必要上什么 DS。我们只需要用求前缀最大值的方法预处理一个数组
总复杂度
代码
由于我若只了,硬生生把询问写成了离线下来扫描线,不过总复杂度还是
#include <bits/stdc++.h>
#define GET (c = getchar_unlocked())
using namespace std;
inline int read(){
int lty = 0;bool flag = false;
char c;GET;
while((c > '9' || c < '0') && c != '-') GET;
if(c == '-') flag = true,GET;
while('0' <= c && c <= '9') lty = (lty << 1) + (lty << 3) + (c ^ '0'),GET;
if(flag) lty = -lty;
return lty;
}
inline void wr(int x,char c = '\n'){ // no minus
if(!x){
putchar('0');putchar(c);
return;
}
int cnt = 0;
static char buf[105];
while(x) buf[++cnt] = (x % 10) ^ '0',x /= 10;
while(cnt) putchar(buf[cnt--]);
putchar(c);
}
int a[100005];
int b[100005];
int o[100005];
vector<pair<int,int> > q[100005];
int al[100005],ar[100005];
int n,k;
int main(){
n = read();
int la = 0,lb = 0;
for(int i = 1;i <= n;i++){
a[i] = read(),b[i] = read();
(a[i] ^ a[i - 1]) && (la = i - 1);
(b[i] ^ b[i - 1]) && (lb = i - 1);
int&oo = o[i];
oo = min(la,lb);
((a[oo] ^ a[i]) && (b[oo] ^ b[i])) || (oo = 0);
}
k = read();
for(int i = 1;i <= k;i++){
int l = read(),r = read();
q[r].emplace_back(i,l);
}
int mx = 0,dr = 0;
for(int i = 1;i <= n;i++){
(o[i] > mx) && (mx = o[i],dr = i);
for(auto&p : q[i]){
(mx >= p.second) && (al[p.first] = mx,ar[p.first] = dr);
}
}
for(int i = 1;i <= k;i++) wr(al[i],' '),wr(ar[i]);
return 0;
}