3 条题解

  • 0
    @ 2026-8-4 11:28:57

    allall 维护 aa 序列中的所有元素,由于没有删除元素的操作,去重后对最小值没影响,所以我用的是 setset 类型。diffdiff 维护的 aa 序列中所有相邻元素的差。

    #include<bits/stdc++.h>
    using namespace std;
    const int N=5e5+10;
    struct nd{int st,ed;}a[N];
    multiset<int>diff;
    int n,m,mn;set<int>all;
    void INSERT(int x)
    {
        if(mn==0)return;
        if(all.count(x)){mn=0;return;}
        auto it=all.lower_bound(x);
        if(it!=all.end())mn=min(mn,abs(*it-x));
        if(it!=all.begin())mn=min(mn,abs(*(--it)-x));
        all.insert(x);
    }
    signed main()
    {
    	ios::sync_with_stdio(0);
    	cin.tie(0);cout.tie(0);
    	mn=5e8;cin>>n>>m;
        for(int i=1,x;i<=n;i++)
        {
            cin>>x;a[i].st=a[i].ed=x;INSERT(x);
            if(i>1)diff.insert(abs(a[i-1].ed-x));
        }
        while(m--)
        {
            string op;cin>>op;
            if(op[0]=='I')
            {
                int pos,k;cin>>pos>>k;
                if(pos<n)
                {
                    diff.erase(diff.find(abs(a[pos].ed-a[pos+1].st)));
                    diff.insert(abs(k-a[pos+1].st));
                }
    			diff.insert(abs(a[pos].ed-k));
                a[pos].ed=k;INSERT(k);
            }
            else
            {
                if(op[4]=='G')cout<<*diff.begin()<<'\n';
                else cout<<mn<<'\n';
            }
        }
    	return 0;
    }
    
    • 0
      @ 2025-10-8 17:02:31
      #include<cstdio>
      #include<set>
      #include<cstdlib>
      #include<cctype>
      using namespace std;
      const int maxn=5e5+1e2;
      const int inf=0x3f3f3f3f;
      
      multiset<int> delta,full;
      int st[maxn],ed[maxn];
      int srt=inf;
      int n,m;
      
      inline void update_srt(int x)
      {
          multiset<int>::iterator it = full.lower_bound(x);
          int nw = *it - x;
          --it;
          nw = min( nw , x - *it );
          srt = min( srt , nw );
          full.insert(x);
      }
      
      inline void replac(int pos,int x)
      {
          delta.insert( abs( x - ed[pos] ) );
          if( pos != n )
              delta.erase( delta.find( abs( st[pos+1] - ed[pos] ) ) ),
              delta.insert( abs( st[pos+1] - x ) );
          ed[pos] = x;
      }
      
      inline int getint()
      {
          int ret = 0 , fix = 1;
          char ch = getchar();
          while( !isdigit(ch) )
          {
              if( ch == '-' )
                  fix = -1;
              ch = getchar();
          }
          while( isdigit(ch) )
              ret = ret * 10 + ( ch - '0' ),
              ch = getchar();
          return ret * fix;
      }
      
      int main()
      {
          static char str[1<<5];
          n = getint() , m = getint();
          for(int i=1;i<=n;i++)
              st[i] = ed[i] = getint();
          
          full.insert(inf),
          full.insert(-inf);
          for(int i=1;i<n;i++)
              delta.insert( abs( st[i+1] - ed[i] ) );
          for(int i=1;i<=n;i++)
              update_srt(st[i]);
          for(int i=1,pos,x;i<=m;i++)
          {
              scanf("%s",str);
              if( *str == 'I' )
              {
                  pos = getint() , x = getint();
                  update_srt(x);
                  replac(pos,x);
              }
              else if( str[4] == 'S' )
                  printf("%d\n",srt);
              else
                  printf("%d\n",*delta.begin());
          }
          return 0;
      }
      
      • 1

      *【STL:multiset】[ZJOI2007] 报表统计

      信息

      ID
      2711
      时间
      2500ms
      内存
      256MiB
      难度
      7
      标签
      递交数
      90
      已通过
      19
      上传者