[ZJOI2007]报表统计(链表法+set)

problem

洛谷链接

solution

纯纯不理解要搬用平衡树的那些做法,使我们可可爱爱的链表不香不好写吗??

众所周知,链表法是可以进行删除和增加的,只需要维护每个点的前驱和后继。

相邻两个的差绝对值的最小值,用 set \text{set} set 维护这个绝对值差即可,每次增点时把原来的先删掉再加上新的。

任选两个数的差的绝对值更简单,直接扔进一个 set \text{set} set 然后每次新加点的时候,就 lower_bound() 求前后继,全局取 min \text{min} min 即可。

由于值可能相同,所以我们用 multiset \text{multiset} multiset 就行了。

非常小清新啊!

code

#include <bits/stdc++.h>
using namespace std;
#define maxn 1000005
multiset < int > s1, s2;
int n, m, ans = 0x3f3f3f3f;
int a[maxn], g[maxn], lst[maxn], nxt[maxn];

int main() {
    scanf( "%d %d", &n, &m );
    for( int i = 1;i <= n;i ++ ) {
        scanf( "%d", &a[i] );
        g[i] = i;
        if( i > 1 ) lst[i] = i - 1;
        if( i < n ) nxt[i] = i + 1;
        if( i > 1 ) s1.insert( fabs( a[i] - a[i - 1] ) );
        s2.insert( a[i] );
        auto it = s2.lower_bound( a[i] );
        auto l = it, r = it;
        if( it != s2.begin() ) ans = min( ans, a[i] - *(--l) );
        if( (++r) != s2.end() ) ans = min( ans, *r - a[i] );
    }
    int cnt = n;
    for( int i = 1;i <= m;i ++ ) {
        char op[10]; int x, k;
        scanf( "%s", op );
        if( op[0] == 'I' ) {
            scanf( "%d %d", &x, &k );
            a[++ cnt] = k;
            s1.erase( s1.find( fabs( a[g[x]] - a[nxt[g[x]]] ) ) );
            lst[nxt[g[x]]] = cnt;
            lst[cnt] = g[x];
            nxt[cnt] = nxt[g[x]];
            nxt[g[x]] = cnt;
            g[x] = cnt;
            s1.insert( fabs( k - a[lst[cnt]] ) );
            s1.insert( fabs( k - a[nxt[cnt]] ) );
            s2.insert( k );
            auto it = s2.lower_bound( k );
            auto l = it, r = it;
            if( it != s2.begin() ) ans = min( ans, k - *(--l) );
            if( (++r) != s2.end() ) ans = min( ans, *r - k );
        }
        else if( op[4] == 'G' ) printf( "%d\n", *s1.begin() );
        else printf( "%d\n", ans );
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值