Educational Codeforces Round 72 (Rated for Div. 2)E. Sum Queries?

链接

点击跳转

题解

沙雕题
对每一位,查询区间中这位不为 0 0 0的最小值和次小值

卡常数

代码

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 200010
#define cl(x) memset(x,0,sizeof(x))
#define rep(_,__) for(_=1;_<=(__);_++)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
    ll c, f(1);
    for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
    for(;isdigit(c);c=getchar())x=x*10+c-0x30;
    return f*x;
}
ll n, m;
pii operator+(pii a, pii b)
{
    int v1[]{a.first,a.second}, v2[]{b.first,b.second}, v3[2], tot(0), i(0), j(0);
    while(tot<2)
        if(v1[i]<v2[j])v3[tot++]=v1[i++];
        else v3[tot++]=v2[j++];
    return pii(v3[0],v3[1]);
}
struct SegmentTree
{
    #define inf 0x3f3f3f3f
    int L[maxn<<2], R[maxn<<2];
    pii mn[maxn<<2];
    void pushup(int o)
    {
        mn[o]=mn[o<<1]+mn[o<<1|1];
    }
    void build(int o, int l, int r)
    {
        int mid(l+r>>1);
        L[o]=l, R[o]=r;
        mn[o]=pii(iinf,iinf);
        if(l==r)return;
        build(o<<1,l,mid);
        build(o<<1|1,mid+1,r);
    }
    void chg(int o, int pos, int v)
    {
        int mid(L[o]+R[o]>>1);
        if(L[o]==R[o]){mn[o]=pii(v,iinf);return;}
        if(pos<=mid)chg(o<<1,pos,v);
        if(pos>mid)chg(o<<1|1,pos,v);
        mn[o]=mn[o<<1]+mn[o<<1|1];
    }
    pii q(int o, int l, int r)
    {
        int mid(L[o]+R[o]>>1);
        pii ans(pii(iinf,iinf));
        if(l<=L[o] and r>=R[o])return mn[o];
        if(l<=mid)ans=ans+q(o<<1,l,r);
        if(r>mid)ans=ans+q(o<<1|1,l,r);
        return ans;
    }
    #undef inf
}segtree[10];
ll x[maxn];
int main()
{
    int ans, i, j, type, a, b, t;
    n=read(), m=read();
    rep(i,9)segtree[i].build(1,1,n);
    rep(i,n)
    {
        x[i]=read();
        t=x[i];
        rep(j,9)
        {
            if(t%10)
            {
                segtree[j].chg(1,i,x[i]);
            }
            t/=10;
        }
    }
    while(m--)
    {
        type=read(), a=read(), b=read();
        if(type==1)
        {
            x[a]=b;
            rep(j,9)
            {
                if(b%10)segtree[j].chg(1,a,x[a]);
                else segtree[j].chg(1,a,iinf);
                b/=10;
            }
        }
        else
        {
            int ans=-1;
            rep(j,9)
            {
                auto p = segtree[j].q(1,a,b);
                if(p.second<iinf)
                {
                    if(ans==-1)ans=p.first+p.second;
                    else ans=min(ans,p.first+p.second);
                }
            }
            printf("%d\n",ans);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值