1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
| #include <bits/stdc++.h> using namespace std; typedef long long ll; #define MOD 1000000007 #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back const double pi = acos(-1.0);
struct SEGT{ int cnt; int vs[2]; }segt[12][800010]; #define UNSETINF -1 int a[200010]; int n,q;
void mergeS(int *v1,int *v2,int *outv){ unsigned int sa[] = { (unsigned int)v1[0], (unsigned int)v1[1], (unsigned int)v2[0], (unsigned int)v2[1] }; rep(i,0,2){ rep(j,i+1,4){ if(sa[j] < sa[i]){ swap(sa[i],sa[j]); } } } outv[0] = sa[0]; outv[1] = sa[1]; } void insert(int off,int o,int l,int r,int idx,int v){ segt[off][o].cnt++; if(l == r){ segt[off][o].vs[0] = v; return ; } int mid = (l+r)/2; if(idx <= mid){ insert(off,o<<1,l,mid,idx,v); }else{ insert(off,o<<1 | 1,mid+1,r,idx,v); } mergeS(segt[off][o<<1].vs,segt[off][o<<1|1].vs,segt[off][o].vs); } static int ten[]={1,10,100,1'000,10'000,100'000,1'000'000,10'000'000,100'000'000,1'000'000'000}; void build(int off,int o,int l,int r){ if(l == r){ if((a[l] / ten[off])%10 != 0){ segt[off][o].vs[0] = a[l]; segt[off][o].cnt++; } return ; } int mid = (l+r)/2; build(off,o<<1,l,mid); build(off,o<<1|1,mid+1,r); mergeS(segt[off][o<<1].vs,segt[off][o<<1|1].vs,segt[off][o].vs); segt[off][o].cnt = segt[off][o<<1].cnt + segt[off][o<<1|1].cnt; } void del(int off,int o,int l,int r,int idx,int v){ segt[off][o].cnt--; if(l == r){ segt[off][o].vs[0] = UNSETINF; return ; } int mid = (l+r)/2; if(idx <= mid){ del(off,o<<1,l,mid,idx,v); }else{ del(off,o<<1 | 1,mid+1,r,idx,v); } mergeS(segt[off][o<<1].vs,segt[off][o<<1|1].vs,segt[off][o].vs); } void change(int idx,int v){ int oldv = a[idx]; if(oldv == v)return; rep(j,0,10){ if(oldv%10 != 0){ del(j,1,0,n-1,idx,a[idx]); } oldv/=10; } a[idx] = v; rep(j,0,10){ if(v%10 != 0){ insert(j,1,0,n-1,idx,a[idx]); } v/=10; } } void qtree(int off,int o,int l,int r,int ql,int qr,int *ret){ if(segt[off][o].cnt == 0){ ret[0] = UNSETINF; ret[1] = UNSETINF; return ; } if(l == ql && r == qr){ ret[0] = segt[off][o].vs[0]; ret[1] = segt[off][o].vs[1]; return ; } int mid = (l+r)/2; if(qr <= mid){ qtree(off,o<<1,l,mid,ql,qr,ret); return ; }else if(ql > mid){ qtree(off,o<<1 | 1,mid+1,r,ql,qr,ret); return ; } int retl[2]; qtree(off,o<<1 ,l ,mid,ql ,mid,retl); int retr[2]; qtree(off,o<<1 | 1,mid+1,r ,mid+1,qr,retr); mergeS(retl,retr,ret); } void query(int l,int r){ int ans = -1; rep(j,0,10){ int ret[2]; qtree(j,1,0,n-1,l,r,ret); if(ret[1] == -1){ continue; } if(ans == -1 || ans > ret[0]+ret[1]){ ans = ret[0]+ret[1]; } } printf("%d\n",ans); } void init(){ rep(i,0,11){ rep(j,0,800001){ segt[i][j].vs[0] = UNSETINF; segt[i][j].vs[1] = UNSETINF; } } } int main(){ init(); scanf("%d %d",&n,&q); rep(i,0,n){ scanf("%d",a+i); } rep(off,0,10){ build(off,1,0,n-1); } rep(i,0,q){ int op; scanf("%d",&op); if(op == 1){ int idx,x; scanf("%d %d",&idx,&x); change(idx-1,x); }else{ int l,r; scanf("%d %d",&l,&r); query(l-1,r-1); } } return 0; }
|