bzoj 1058: [ZJOI2007]报表统计

题意:

维护一个数列,支持加数,查询相邻最小差,全局最小差。

题解:

STL练习题。
我STL太菜了。
思路大概想了出来,然而不会写。
对于每一个原位置,记录第一个数与最后一个数,然后可以瞎维护。
orz各种用法,orz hzwer,终于敲(chao)了一份代码。
重复情况可以用map解决。
15s题16s过2333
code:

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<cmath>
#include<map>
#include<algorithm>
using namespace std;
const int inf=1000000000;
map<int,int> mp;
set<int> a,b;//a:相邻,b:全局 
int st[500010],ed[500010];
priority_queue<int,vector<int>,greater<int> > q;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
void add_a(int x)
{
    mp[x]++;
    if(mp[x]==1) a.insert(x);
}
void add_b(int x)
{
    int l=*(--b.lower_bound(x)),r=*b.lower_bound(x);
    q.push(min(x-l,r-x));
    b.insert(x);
}
int main()
{
    int n=read(),m=read();
    b.insert(inf);b.insert(-inf);
    for(int i=1;i<=n;i++)
    {
        int x=read();
        st[i]=ed[i]=x;
        add_b(x);
    }
    for(int i=2;i<=n;i++) add_a(abs(st[i]-st[i-1]));
    char s[15];
    while(m--)
    {
        scanf("%s",s+1);
        if(s[1]=='I')
        {
            int p=read(),x=read();
            if(p!=n)
            {
                int tmp=abs(ed[p]-st[p+1]);
                mp[tmp]--;if(!mp[tmp]) a.erase(tmp);
            }
            add_a(abs(ed[p]-x));
            add_a(abs(x-st[p+1]));
            ed[p]=x;add_b(x);
        }
        else if(s[5]=='S') printf("%d\n",q.top());
        else printf("%d\n",*a.begin());
    }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值