跳到正文

题解

题解:AT_abc411_c AT_abc411_c - [ABC411C] Black Intervals

anke2017约 4 分钟
原文
文章信息与版本

文章信息

发布
2025年6月22日
收录
2025年12月12日
更新
2026年11月16日
篇幅
约 4 分钟
获赞
2

版本

  1. v1当前

提供一种比较逆天的想法,估计是 ODT 学魔怔了弄出来的。

考虑把值相同的数并在一起,丢到 set 里存储。对于反转,直接把包含这个位置的区间取出来操作,同时维护值为 11 的段数量即可。

赛时代码(缩进有点奇怪,能看就行):

C++
#include<bits/stdc++.h>

using namespace std;
struct sth
{
  int l,r,col;//左右端点、颜色
  bool operator <(const sth tmp)const
  {
    return l<tmp.l;
  }
};
set<sth> st;
int main()
{
  int n,m;cin>>n>>m;
  int ans=0;
  st.insert((sth){1,n,0});//初始状态:全 0
    if(n==1)
    {
        for(int i=1;i<=m;i++)
            cout<<(i&1)<<'\n';
        return 0;
    }
  for(int i=1;i<=m;i++)
    {
    int x;cin>>x;
    auto t=st.lower_bound((sth){x,x,0});
    if(t->l!=x)t--;
    int l=t->l,r=t->r,col=t->col;
    st.erase(t);
    if(l!=x)st.insert((sth){l,x-1,col});
    if(r!=x)st.insert((sth){x+1,r,col});
    t=st.insert((sth){x,x,!col}).first;
    if(!col)ans++;
    if(l==r&&col)ans--;
    if(l!=x&&r!=x&&col)ans++;
//下面这段是保证相邻区间的值不相等,以保证答案正确用的
//特判:没有左/右区间,防止越界
    if(x==1)
    {
        auto r1=++t;t--;
if(r1->col==t->col)
    {
        l=x,r=r1->r;
        st.erase(r1),st.erase(t);
        st.insert((sth){l,r,!col});
        ans-=!col;
    }
        cout<<ans<<'\n';continue;
    }
    else if(x==n)
    {
        auto l1=--t;t++;
        if(l1->col==t->col)
      {
        l=l1->l,r=x;
        st.erase(t),st.erase(l1);
        st.insert((sth){l,r,!col});
          ans-=!col;
      }
        cout<<ans<<'\n';
        continue;
    }
//正常情况下,判定一下左/右区间是否和这个区间颜色相同,如果相同就合并1
    auto l1=--t;t++;
    auto r1=++t;t--;
    if(l1->col==t->col)
    {
      if(r1->col==t->col)
      {
        l=l1->l,r=r1->r;
        st.erase(r1),st.erase(t),st.erase(l1);
        st.insert((sth){l,r,!col});
        ans-=2*!col;
      }
      else
      {
        l=l1->l,r=x;
        st.erase(t),st.erase(l1);
        st.insert((sth){l,r,!col});
          ans-=!col;
      }
    }
    else if(r1->col==t->col)
    {
        l=x,r=r1->r;
        st.erase(r1),st.erase(t);
        st.insert((sth){l,r,!col});
        ans-=!col;
    }
    cout<<ans<<'\n';
  }
}
原文