BZOJ1058: [ZJOI2007]报表统计(洛谷P1110)

本文介绍了一种使用STL中的set和map解决特定问题的方法。通过维护两个set来跟踪全局差值和局部差值的变化,并利用map进行元素存在性的检查,结合优先队列快速获取最小值。适用于算法竞赛中涉及区间更新和查询的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

STL

BZOJ题目传送门
洛谷题目传送门

学了一发set。

用两个set,一个维护全局的差值,一个维护每个位置的差值。
对每个位置记录起始元素和最终元素,就可以用set维护了。
map用来判断元素是否存在,堆快速得出答案。

代码:

#include<map>
#include<set>
#include<queue>
#include<cctype>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 500005
#define F inline
using namespace std;
const int inf=1e9;
int n,m,st[N],ed[N];
char s[15];
multiset<int>a,b;
map<int,int>mp;
priority_queue<int,vector<int>,greater<int> >q;
F int _read(){
    int x=0,f=1; char ch=getchar();
    while(!isdigit(ch)) { if (ch=='-') f=-1; ch=getchar(); }
    while(isdigit(ch)) x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
    return x*f;
}
F void writec(int x){ if (x>9) writec(x/10); putchar(x%10+48); }
F void _write(int x){ writec(x),putchar('\n'); }
void nsrt(int x){ if (!mp[x]) a.insert(x); mp[x]++; }
void dlt(int x){ mp[x]--; if (!mp[x]) a.erase(x); }
void push(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(){
    n=_read(),m=_read(),b.insert(inf),b.insert(-inf);
    for (int i=1;i<=n;i++) st[i]=ed[i]=_read(),push(ed[i]);
    for (int i=2;i<=n;i++) nsrt(abs(st[i]-st[i-1]));
    while (m--){
        char s[15]; scanf("%s",s); int x,w;
        switch (s[4]){
            case 'R':
                x=_read(),w=_read();
                if (x!=n) dlt(abs(ed[x]-st[x+1]));
                nsrt(abs(ed[x]-w)),nsrt(abs(w-st[x+1]));
                ed[x]=w,push(w); break;
            case 'S': _write(q.top()); break;
            case 'G': _write(*a.begin()); break;
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值