解题报告 P4898 [IOI2018] seats 排座位
题目链接:P4898 [IOI2018] seats 排座位。
题目大意
给定一个
定义矩阵美妙度为其中满足“大小为
要求在每次操作后立即输出当前矩阵的美妙度(交互题,所以强制在线)。
数据规模
AC 代码
评测记录。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
namespace Qza{
/*
一个点(i点)产生贡献的生命历程:
(高超思路:以点的编号顺序为时间轴)
时间轴: |-----白点-----| begin[i] |-----白点-----| i |-----黑点-----| end[i] |-----黑点-----|
状态描述: 未产生贡献 开始产生贡献 产生贡献ing 白点变黑点 产生贡献ing 贡献结束 再没贡献了
begin[],end[]记录的均为左闭右开区间,
直接令 begin[i] 为 i周围四点中第二小的点编号,end[i] 为 i左和上中较小的点的编号,
则可通过判断 begin[i]、i、end[i] 三者的大小关系来判断 i的贡献情况。
这种思路很不错。
*/
#define N 1000002
#define _N 4000002
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
int Min[_N],num[_N],col[_N],nowl,nowr;
int n,m,val[N],beg[N],end[N],tot,pending[15];// val[] 用以维护 “左和上均不为黑格的黑格数目 ”与 “周围四连通有不少于两个黑格的白格数目 ”之和。
struct Competitor { // 参赛者。
int r,c;
Competitor(int x=0,int y=0):r(x),c(y) {}
}comp[N];
int who[N]; // who[] 与 comp[] 互反。
int dx[4]={ 0, 1, 0,-1}; // 右、下、左、上。 (顺时针)
int dy[4]={ 1, 0,-1, 0}; // 右、下、左、上。 (顺时针)
template<typename T>
void swap(T &x,T &y) {T t=x;x=y;y=t;}
int max(int x,int y) {return x>=y?x:y;}
int min(int x,int y) {return x<=y?x:y;}
void update(int rt)
{
if(Min[rt<<1]==Min[rt<<1|1]) Min[rt]=Min[rt<<1],num[rt]=num[rt<<1]+num[rt<<1|1];
else if(Min[rt<<1]<Min[rt<<1|1]) Min[rt]=Min[rt<<1],num[rt]=num[rt<<1];
else Min[rt]=Min[rt<<1|1],num[rt]=num[rt<<1|1];
}
void build(int l,int r,int rt) {
if(l==r) {
Min[rt]=val[l]; num[rt]=1; return ;
}
int mid=(l+r)>>1;
build(lson);
build(rson);
update(rt);
}
void push_col(int rt)
{
if(col[rt]) {
Min[rt<<1]+=col[rt]; col[rt<<1]+=col[rt];
Min[rt<<1|1]+=col[rt]; col[rt<<1|1]+=col[rt];
col[rt]=0;
}
}
void modify(int v,int l,int r,int rt) {
if(nowl<=l && r<=nowr) {
Min[rt]+=v; col[rt]+=v; return ;
}
push_col(rt);
int mid=(l+r)>>1;
if(nowl<=mid) modify(v,lson);
if(mid <nowr) modify(v,rson);
update(rt);
}
int id(int x,int y) {return (x-1)*m+y;}
int white_beg(int node) {
int x,y,j,min1=node,min2=node;
for(int i=0;i<4;++i) {
x=comp[node].r+dx[i]; y=comp[node].c+dy[i];
if( 1<=x && x<=n && 1<=y && y<=m ) {
j=who[id(x,y)];
if(j<min1) min2=min1,min1=j;
else if(j<min2) min2=j;
}
}
return min2;
}
int black_end(int node) {
int x,y,min1=tot+1;
for(int i=2;i<4;++i) {
x=comp[node].r+dx[i]; y=comp[node].c+dy[i];
if( 1<=x && x<=n && 1<=y && y<=m )
min1=min(min1,who[id(x,y)]);
}
return min1;
}
int read()
{
char ch=getchar(); int x=0;
while(ch<'0' || ch>'9') ch=getchar();
while(ch>='0'&&ch<='9') x=(x*10)+(ch^48),ch=getchar();
return x;
}
void main()
{
int q,x,y,s,t,node,cnt;
n=read(); m=read(); q=read();
tot=n*m;
for(int i=1;i<=tot;++i) {
comp[i].r=read()+1; comp[i].c=read()+1;
who[id(comp[i].r,comp[i].c)]=i;
}
for(int i=1;i<=tot;++i) {
// 调整该格子的贡献:
val[i]=val[i-1]; // val先暂定为上一次的值,下面再做一些调整。val初始值为 0。
if((beg[i]=white_beg(i))<i) --val[i]; // 原来对 val产生贡献的一个白点变黑了。
if((end[i]=black_end(i))>i) ++val[i]; // 这个黑点是顶点,可对 val产生贡献。
// 调整该格子周围四连通格子的贡献:
for(int j=0;j<2;++j) { // 只检测当前格子下和右连通的格子是不是黑格子。
x=comp[i].r+dx[j]; y=comp[i].c+dy[j];
if( 1<=x && x<=n && 1<=y && y<=m ) {
node=who[id(x,y)];
if(node<i && black_end(node)==i) --val[i]; // 这个黑点曾经是顶点,现在它的贡献永远的结束了。
if(node>i && white_beg(node)==i) ++val[i]; // 这个白点从现在开始有了贡献。
}
}
for(int j=2;j<4;++j) { // 上左下右四个连通的格子分别检测一遍。
x=comp[i].r+dx[j]; y=comp[i].c+dy[j];
if( 1<=x && x<=n && 1<=y && y<=m ) {
node=who[id(x,y)];
if(node>i && white_beg(node)==i) ++val[i];
}
}
}
build(1,tot,1);
for(int i=1;i<=q;++i) {
cnt=0;
x=read()+1; y=read()+1;
if(x==y) {
printf("%d\n",num[1]);
continue;
}
if(x>y) swap(x,y);
for(int j=0;j<4;++j) {
s=comp[x].r+dx[j]; t=comp[x].c+dy[j];
if( 1<=s && s<=n && 1<=t && t<=m )
pending[++cnt]=who[id(s,t)];
s=comp[y].r+dx[j]; t=comp[y].c+dy[j];
if( 1<=s && s<=n && 1<=t && t<=m )
pending[++cnt]=who[id(s,t)];
}
pending[++cnt]=x;
pending[++cnt]=y;
sort(pending+1,pending+cnt+1); // 排个序便于去重。
// 删掉原来的贡献。
for(int j=1;j<=cnt;++j)
if(pending[j]!=pending[j-1]) {
node=pending[j];
if(beg[node]<node) { // 白点期间有贡献。
nowl=max(beg[node],x); // 若 begin[node]<x 则在 [begin[node],x-1] 内,x,y均为白点,不影响 node 的贡献。
nowr=min(node,y)-1; // 若 node>y 则在 [y+1,node] 内,x,y均为黑点,不影响 node 的贡献。
if(nowl<=nowr) modify(-1,1,tot,1);
}
if(node<end[node]) { // 黑点期间有贡献。
nowl=max(node,x); // 若 node<x 则在 [node,x-1] 内,x,y均为白点,不影响 node 的贡献。
nowr=min(end[node],y)-1; // 若 end[node]>y 则在 [y+1,end[node]] 内,x,y均为黑点,不影响 node 的贡献。
if(nowl<=nowr) modify(-1,1,tot,1);
}
}
// 交换后补上新的贡献。
who[id(comp[x].r,comp[x].c)]=y;
who[id(comp[y].r,comp[y].c)]=x;
swap(comp[x],comp[y]);
for(int j=1;j<=cnt;++j)
if(pending[j]!=pending[j-1]) {
node=pending[j];
beg[node]=white_beg(node);
end[node]=black_end(node);
if(beg[node]<node) { // 白点期间有贡献。
nowl=max(beg[node],x);
nowr=min(node,y)-1;
if(nowl<=nowr) modify(1,1,tot,1);
}
if(node<end[node]) { // 黑点期间有贡献。
nowl=max(node,x);
nowr=min(end[node],y)-1;
if(nowl<=nowr) modify(1,1,tot,1);
}
}
printf("%d\n",num[1]);
// Min[1] 定然为 1 。
}
return ;
}
}
int main()
{
Qza::main();
return 0;
}
后记
这样的神仙