#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
#define int long long
struct SegmentTree{
private:
int sz=1,skip=0;
vector<int>seg,lazy;
void propegate(int l,int r,int node)
{
if(lazy[node]==0)return;
if(l!=r)
{
lazy[2*node+1]+=lazy[node];
lazy[2*node+2]+=lazy[node];
}
int len=r-l+1;
seg[node]+=len*lazy[node];
lazy[node]=0;
}
void update(int l,int r,int node,int lx,int rx,int val)
{
propegate(l,r,node);
if(l>rx||r<lx)
{
return;
}
if(l>=lx&&r<=rx)
{
lazy[node]+=val;
propegate(l,r,node);
return;
}
int mid=l+r>>1;
update(l,mid,2*node+1,lx,rx,val);
update(mid+1,r,2*node+2,lx,rx,val);
seg[node]=seg[2*node+1]+seg[2*node+2];
}
int query(int l,int r,int node,int lx,int rx)
{
propegate(l,r,node);
if(l>=lx&&r<=rx)
{
return seg[node];
}
if(l>rx||r<lx)
{
return skip;
}
int mid=l+r>>1;
return query(l,mid,2*node+1,lx,rx)+query(mid+1,r,2*node+2,lx,rx);
}
public:
void update(int l,int r,int val)
{
update(0,sz-1,0,l,r,val);
}
int query(int l,int r)
{
return query(0,sz-1,0,l,r);
}
SegmentTree(int n){
while(sz<=n)sz*=2;
seg=vector<int>(sz*2,0);
lazy=vector<int>(sz*2,0);
}
};
struct SegmentTreeBeats{
private:
#define L 2*node+1
#define R 2*node+2
#define mid (l+r>>1)
#define inf 1e15+5
struct Node{
int sum,mx,secMx,mxCnt,mn,mnCnt,secMn,lazyAdd,lazySet;
Node(int summ,int mxx,int secMxx,int mxCntt,int mnn,int secMnn,int mnCntt,int add=0,int set=-1)
:sum(summ),mx(mxx),secMx(secMxx),mxCnt(mxCntt),
mn(mnn),secMn(secMnn),mnCnt(mnCntt),
lazyAdd(add),lazySet(set){}
};
int sz;vector<Node>seg;
Node merge(Node&a,Node&b)
{
Node ret(0,-inf,-inf,0,inf,inf,0,0,-1);
ret.sum=a.sum+b.sum;
ret.mx=max(a.mx,b.mx);// max
ret.secMx=max(a.secMx,b.secMx);
if(ret.mx==a.mx) ret.mxCnt+=a.mxCnt;
else ret.secMx=max(ret.secMx,a.mx);
if(ret.mx==b.mx) ret.mxCnt+=b.mxCnt;
else ret.secMx=max(ret.secMx,b.mx);
ret.mn=min(a.mn,b.mn);// min
ret.secMn=min(a.secMn,b.secMn);
if(ret.mn==a.mn) ret.mnCnt+=a.mnCnt;
else ret.secMn=min(ret.secMn,a.mn);
if(ret.mn==b.mn) ret.mnCnt+=b.mnCnt;
else ret.secMn=min(ret.secMn,b.mn);
return ret;
}
void pushSet(int l,int r,int node,int val)
{
seg[node]=Node(val*(r-l+1),val,-inf,r-l+1,val,inf,r-l+1,0,val);
}
void pushMin(int l,int r,int node,int mn)
{
if(seg[node].mn>=mn)
{
pushSet(l,r,node,mn);
return;
}
if(seg[node].mx>mn)
{
if(seg[node].secMn==seg[node].mx) seg[node].secMn=mn;
int val=seg[node].mx-mn;
seg[node].sum-=val*seg[node].mxCnt;
seg[node].mx=mn;
}
}
void pushMax(int l,int r,int node,int mx)
{
if(seg[node].mx<=mx)
{
pushSet(l,r,node,mx);
return;
}
if(seg[node].mn<mx)
{
if(seg[node].secMx==seg[node].mn) seg[node].secMx=mx;
int val=mx-seg[node].mn;
seg[node].sum+=val*seg[node].mnCnt;
seg[node].mn=mx;
}
}
void pushAdd(int l,int r,int node,int val)
{
if(seg[node].mx==seg[node].mn)
{
pushSet(l,r,node,seg[node].mn+val);
return;
}
seg[node].mx+=val;
if(seg[node].secMx!=-inf) seg[node].secMx+=val;
seg[node].mn+=val;
if(seg[node].secMn!=inf) seg[node].secMn+=val;
seg[node].sum+=(r-l+1)*val;
seg[node].lazyAdd+=val;
}
void propegate(int l,int r,int node)
{
if(l==r) return;
if(seg[node].lazySet!=-1)
{
pushSet(l,mid,L,seg[node].lazySet);
pushSet(mid+1,r,R,seg[node].lazySet);
seg[node].lazySet=-1;
}
else
{
pushAdd(l,mid,L,seg[node].lazyAdd);
pushAdd(mid+1,r,R,seg[node].lazyAdd);
seg[node].lazyAdd=0;
pushMin(l,mid,L,seg[node].mx);
pushMin(mid+1,r,R,seg[node].mx);
pushMax(l,mid,L,seg[node].mn);
pushMax(mid+1,r,R,seg[node].mn);
}
}
void build(int l,int r,int node,vector<int>&v)
{
if(l==r)
{
if(l<v.size())
{
seg[node]=Node(v[l],v[l],-inf,1,v[l],inf,1);
}
return;
}
build(l,mid,L,v);
build(mid+1,r,R,v);
seg[node]= merge(seg[L],seg[R]);
}
void updateAdd(int l,int r,int node,int lx,int rx,int val)
{
if(l>rx||r<lx)return;
if(l>=lx&&r<=rx)
{
pushAdd(l,r,node,val);
return;
}
propegate(l,r,node);
updateAdd(l,mid,L,lx,rx,val);
updateAdd(mid+1,r,R,lx,rx,val);
seg[node]=merge(seg[L],seg[R]);
}
void updateSet(int l,int r,int node,int lx,int rx,int val)
{
if(l>rx||r<lx)return;
if(l>=lx&&r<=rx)
{
pushSet(l,r,node,val);
return;
}
propegate(l,r,node);
updateSet(l,mid,L,lx,rx,val);
updateSet(mid+1,r,R,lx,rx,val);
seg[node]=merge(seg[L],seg[R]);
}
void updateMin(int l,int r,int node,int lx,int rx,int mn)
{
if(r<lx||l>rx||seg[node].mx<=mn) return;
if(l>=lx&&r<=rx&&mn>seg[node].secMx)
{
pushMin(l,r,node,mn);
return;
}
propegate(l,r,node);
updateMin(l,mid,L,lx,rx,mn);
updateMin(mid+1,r,R,lx,rx,mn);
seg[node]=merge(seg[L],seg[R]);
}
void updateMax(int l,int r,int node,int lx,int rx,int mx)
{
if(l>rx||r<lx||mx<=seg[node].mn) return;
if(l>=lx&&r<=rx&&mx<seg[node].secMn)
{
pushMax(l,r,node,mx);
return;
}
propegate(l,r,node);
updateMax(l,mid,L,lx,rx,mx);
updateMax(mid+1,r,R,lx,rx,mx);
seg[node]=merge(seg[L],seg[R]);
}
int querySum(int l,int r,int node,int lx,int rx)
{
if(l>rx||r<lx)return 0;
if(l>=lx&&r<=rx)return seg[node].sum;
propegate(l,r,node);
return querySum(l,mid,L,lx,rx)+querySum(mid+1,r,R,lx,rx);
}
int queryMin(int l,int r,int node,int lx,int rx)
{
if(l>rx||r<lx)return inf;
if(l>=lx&&r<=rx)return seg[node].mn;
propegate(l,r,node);
return min(queryMin(l,mid,L,lx,rx),queryMin(mid+1,r,R,lx,rx));
}
int queryMax(int l,int r,int node,int lx,int rx)
{
if(l>rx||r<lx)return -inf;
if(l>=lx&&r<=rx)return seg[node].mx;
propegate(l,r,node);
return max(queryMax(l,mid,L,lx,rx),queryMax(mid+1,r,R,lx,rx));
}
#undef L
#undef R
#undef mid
public:
SegmentTreeBeats(vector<int>&v)
{
int n=v.size()+5;
sz=1;
while(sz<=n)sz<<=1;
seg=vector<Node>(sz<<1,Node(0,-inf,-inf,0,inf,inf,0,0,-1));
build(0,sz-1,0,v);
}
void updateAdd(int l,int r,int val)
{
updateAdd(0,sz-1,0,l,r,val);
}
void updateSet(int l,int r,int val)
{
updateSet(0,sz-1,0,l,r,val);
}
void updateMin(int l,int r,int mn)
{
updateMin(0,sz-1,0,l,r,mn);
}
void updateMax(int l,int r,int mx)
{
updateMax(0,sz-1,0,l,r,mx);
}
int queryMax(int l,int r)
{
return queryMax(0,sz-1,0,l,r);
}
int queryMin(int l,int r)
{
return queryMin(0,sz-1,0,l,r);
}
int querySum(int l,int r)
{
return querySum(0,sz-1,0,l,r);
}
#undef inf
};
signed main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n,q;cin>>n>>q;
vector<int>s(n+1);
SegmentTree tree(n+5);
for(int i=1;i<=n;i++)
{
cin>>s[i];
tree.update(i,i,s[i]);
}
vector<int>v(n+1);
SegmentTreeBeats beats(v);
while(q--)
{
int op;cin>>op;
if(op==1)
{
int l,r,x;cin>>l>>r>>x;
tree.update(l,r,x);
beats.updateAdd(l,r,-x);
beats.updateMax(l,r,0);
}
else
{
int l,r;cin>>l>>r;
cout<<tree.query(l,r)+beats.querySum(l,r)<<'\n';
}
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSBsbCBsb25nIGxvbmcKI2RlZmluZSBpbnQgbG9uZyBsb25nCiNkZWZpbmUgaW50IGxvbmcgbG9uZwpzdHJ1Y3QgU2VnbWVudFRyZWV7CnByaXZhdGU6CiAgICBpbnQgc3o9MSxza2lwPTA7CiAgICB2ZWN0b3I8aW50PnNlZyxsYXp5OwogICAgdm9pZCBwcm9wZWdhdGUoaW50IGwsaW50IHIsaW50IG5vZGUpCiAgICB7CiAgICAgICAgaWYobGF6eVtub2RlXT09MClyZXR1cm47CiAgICAgICAgaWYobCE9cikKICAgICAgICB7CiAgICAgICAgICAgIGxhenlbMipub2RlKzFdKz1sYXp5W25vZGVdOwogICAgICAgICAgICBsYXp5WzIqbm9kZSsyXSs9bGF6eVtub2RlXTsKICAgICAgICB9CiAgICAgICAgaW50IGxlbj1yLWwrMTsKICAgICAgICBzZWdbbm9kZV0rPWxlbipsYXp5W25vZGVdOwogICAgICAgIGxhenlbbm9kZV09MDsKICAgIH0KICAgIHZvaWQgdXBkYXRlKGludCBsLGludCByLGludCBub2RlLGludCBseCxpbnQgcngsaW50IHZhbCkKICAgIHsKICAgICAgICBwcm9wZWdhdGUobCxyLG5vZGUpOwogICAgICAgIGlmKGw+cnh8fHI8bHgpCiAgICAgICAgewogICAgICAgICAgICByZXR1cm47CiAgICAgICAgfQogICAgICAgIGlmKGw+PWx4JiZyPD1yeCkKICAgICAgICB7CiAgICAgICAgICAgIGxhenlbbm9kZV0rPXZhbDsKICAgICAgICAgICAgcHJvcGVnYXRlKGwscixub2RlKTsKICAgICAgICAgICAgcmV0dXJuOwogICAgICAgIH0KICAgICAgICBpbnQgbWlkPWwrcj4+MTsKICAgICAgICB1cGRhdGUobCxtaWQsMipub2RlKzEsbHgscngsdmFsKTsKICAgICAgICB1cGRhdGUobWlkKzEsciwyKm5vZGUrMixseCxyeCx2YWwpOwogICAgICAgIHNlZ1tub2RlXT1zZWdbMipub2RlKzFdK3NlZ1syKm5vZGUrMl07CiAgICB9CiAgICBpbnQgcXVlcnkoaW50IGwsaW50IHIsaW50IG5vZGUsaW50IGx4LGludCByeCkKICAgIHsKICAgICAgICBwcm9wZWdhdGUobCxyLG5vZGUpOwogICAgICAgIGlmKGw+PWx4JiZyPD1yeCkKICAgICAgICB7CiAgICAgICAgICAgIHJldHVybiBzZWdbbm9kZV07CiAgICAgICAgfQogICAgICAgIGlmKGw+cnh8fHI8bHgpCiAgICAgICAgewogICAgICAgICAgICByZXR1cm4gc2tpcDsKICAgICAgICB9CiAgICAgICAgaW50IG1pZD1sK3I+PjE7CiAgICAgICAgcmV0dXJuIHF1ZXJ5KGwsbWlkLDIqbm9kZSsxLGx4LHJ4KStxdWVyeShtaWQrMSxyLDIqbm9kZSsyLGx4LHJ4KTsKICAgIH0KcHVibGljOgogICAgdm9pZCB1cGRhdGUoaW50IGwsaW50IHIsaW50IHZhbCkKICAgIHsKICAgICAgICB1cGRhdGUoMCxzei0xLDAsbCxyLHZhbCk7CiAgICB9CiAgICBpbnQgcXVlcnkoaW50IGwsaW50IHIpCiAgICB7CiAgICAgICAgcmV0dXJuIHF1ZXJ5KDAsc3otMSwwLGwscik7CiAgICB9CiAgICBTZWdtZW50VHJlZShpbnQgbil7CiAgICAgICAgd2hpbGUoc3o8PW4pc3oqPTI7CiAgICAgICAgc2VnPXZlY3RvcjxpbnQ+KHN6KjIsMCk7CiAgICAgICAgbGF6eT12ZWN0b3I8aW50PihzeioyLDApOwogICAgfQp9OwpzdHJ1Y3QgU2VnbWVudFRyZWVCZWF0c3sKcHJpdmF0ZToKI2RlZmluZSBMIDIqbm9kZSsxCiNkZWZpbmUgUiAyKm5vZGUrMgojZGVmaW5lIG1pZCAobCtyPj4xKQojZGVmaW5lIGluZiAxZTE1KzUKICAgIHN0cnVjdCBOb2RlewogICAgICAgIGludCBzdW0sbXgsc2VjTXgsbXhDbnQsbW4sbW5DbnQsc2VjTW4sbGF6eUFkZCxsYXp5U2V0OwogICAgICAgIE5vZGUoaW50IHN1bW0saW50IG14eCxpbnQgc2VjTXh4LGludCBteENudHQsaW50IG1ubixpbnQgc2VjTW5uLGludCBtbkNudHQsaW50IGFkZD0wLGludCBzZXQ9LTEpCiAgICAgICAgICAgICAgICA6c3VtKHN1bW0pLG14KG14eCksc2VjTXgoc2VjTXh4KSxteENudChteENudHQpLAogICAgICAgICAgICAgICAgIG1uKG1ubiksc2VjTW4oc2VjTW5uKSxtbkNudChtbkNudHQpLAogICAgICAgICAgICAgICAgIGxhenlBZGQoYWRkKSxsYXp5U2V0KHNldCl7fQogICAgfTsKICAgIGludCBzejt2ZWN0b3I8Tm9kZT5zZWc7CiAgICBOb2RlIG1lcmdlKE5vZGUmYSxOb2RlJmIpCiAgICB7CiAgICAgICAgTm9kZSByZXQoMCwtaW5mLC1pbmYsMCxpbmYsaW5mLDAsMCwtMSk7CiAgICAgICAgcmV0LnN1bT1hLnN1bStiLnN1bTsKICAgICAgICByZXQubXg9bWF4KGEubXgsYi5teCk7Ly8gbWF4CiAgICAgICAgcmV0LnNlY014PW1heChhLnNlY014LGIuc2VjTXgpOwogICAgICAgIGlmKHJldC5teD09YS5teCkgcmV0Lm14Q250Kz1hLm14Q250OwogICAgICAgIGVsc2UgcmV0LnNlY014PW1heChyZXQuc2VjTXgsYS5teCk7CiAgICAgICAgaWYocmV0Lm14PT1iLm14KSByZXQubXhDbnQrPWIubXhDbnQ7CiAgICAgICAgZWxzZSByZXQuc2VjTXg9bWF4KHJldC5zZWNNeCxiLm14KTsKICAgICAgICByZXQubW49bWluKGEubW4sYi5tbik7Ly8gbWluCiAgICAgICAgcmV0LnNlY01uPW1pbihhLnNlY01uLGIuc2VjTW4pOwogICAgICAgIGlmKHJldC5tbj09YS5tbikgcmV0Lm1uQ250Kz1hLm1uQ250OwogICAgICAgIGVsc2UgcmV0LnNlY01uPW1pbihyZXQuc2VjTW4sYS5tbik7CiAgICAgICAgaWYocmV0Lm1uPT1iLm1uKSByZXQubW5DbnQrPWIubW5DbnQ7CiAgICAgICAgZWxzZSByZXQuc2VjTW49bWluKHJldC5zZWNNbixiLm1uKTsKICAgICAgICByZXR1cm4gcmV0OwogICAgfQogICAgdm9pZCBwdXNoU2V0KGludCBsLGludCByLGludCBub2RlLGludCB2YWwpCiAgICB7CiAgICAgICAgc2VnW25vZGVdPU5vZGUodmFsKihyLWwrMSksdmFsLC1pbmYsci1sKzEsdmFsLGluZixyLWwrMSwwLHZhbCk7CiAgICB9CiAgICB2b2lkIHB1c2hNaW4oaW50IGwsaW50IHIsaW50IG5vZGUsaW50IG1uKQogICAgewogICAgICAgIGlmKHNlZ1tub2RlXS5tbj49bW4pCiAgICAgICAgewogICAgICAgICAgICBwdXNoU2V0KGwscixub2RlLG1uKTsKICAgICAgICAgICAgcmV0dXJuOwogICAgICAgIH0KICAgICAgICBpZihzZWdbbm9kZV0ubXg+bW4pCiAgICAgICAgewogICAgICAgICAgICBpZihzZWdbbm9kZV0uc2VjTW49PXNlZ1tub2RlXS5teCkgc2VnW25vZGVdLnNlY01uPW1uOwogICAgICAgICAgICBpbnQgdmFsPXNlZ1tub2RlXS5teC1tbjsKICAgICAgICAgICAgc2VnW25vZGVdLnN1bS09dmFsKnNlZ1tub2RlXS5teENudDsKICAgICAgICAgICAgc2VnW25vZGVdLm14PW1uOwogICAgICAgIH0KICAgIH0KICAgIHZvaWQgcHVzaE1heChpbnQgbCxpbnQgcixpbnQgbm9kZSxpbnQgbXgpCiAgICB7CiAgICAgICAgaWYoc2VnW25vZGVdLm14PD1teCkKICAgICAgICB7CiAgICAgICAgICAgIHB1c2hTZXQobCxyLG5vZGUsbXgpOwogICAgICAgICAgICByZXR1cm47CiAgICAgICAgfQogICAgICAgIGlmKHNlZ1tub2RlXS5tbjxteCkKICAgICAgICB7CiAgICAgICAgICAgIGlmKHNlZ1tub2RlXS5zZWNNeD09c2VnW25vZGVdLm1uKSBzZWdbbm9kZV0uc2VjTXg9bXg7CiAgICAgICAgICAgIGludCB2YWw9bXgtc2VnW25vZGVdLm1uOwogICAgICAgICAgICBzZWdbbm9kZV0uc3VtKz12YWwqc2VnW25vZGVdLm1uQ250OwogICAgICAgICAgICBzZWdbbm9kZV0ubW49bXg7CiAgICAgICAgfQogICAgfQogICAgdm9pZCBwdXNoQWRkKGludCBsLGludCByLGludCBub2RlLGludCB2YWwpCiAgICB7CiAgICAgICAgaWYoc2VnW25vZGVdLm14PT1zZWdbbm9kZV0ubW4pCiAgICAgICAgewogICAgICAgICAgICBwdXNoU2V0KGwscixub2RlLHNlZ1tub2RlXS5tbit2YWwpOwogICAgICAgICAgICByZXR1cm47CiAgICAgICAgfQogICAgICAgIHNlZ1tub2RlXS5teCs9dmFsOwogICAgICAgIGlmKHNlZ1tub2RlXS5zZWNNeCE9LWluZikgIHNlZ1tub2RlXS5zZWNNeCs9dmFsOwogICAgICAgIHNlZ1tub2RlXS5tbis9dmFsOwogICAgICAgIGlmKHNlZ1tub2RlXS5zZWNNbiE9aW5mKSAgc2VnW25vZGVdLnNlY01uKz12YWw7CiAgICAgICAgc2VnW25vZGVdLnN1bSs9KHItbCsxKSp2YWw7CiAgICAgICAgc2VnW25vZGVdLmxhenlBZGQrPXZhbDsKICAgIH0KICAgIHZvaWQgcHJvcGVnYXRlKGludCBsLGludCByLGludCBub2RlKQogICAgewogICAgICAgIGlmKGw9PXIpIHJldHVybjsKICAgICAgICBpZihzZWdbbm9kZV0ubGF6eVNldCE9LTEpCiAgICAgICAgewogICAgICAgICAgICBwdXNoU2V0KGwsbWlkLEwsc2VnW25vZGVdLmxhenlTZXQpOwogICAgICAgICAgICBwdXNoU2V0KG1pZCsxLHIsUixzZWdbbm9kZV0ubGF6eVNldCk7CiAgICAgICAgICAgIHNlZ1tub2RlXS5sYXp5U2V0PS0xOwogICAgICAgIH0KICAgICAgICBlbHNlCiAgICAgICAgewogICAgICAgICAgICBwdXNoQWRkKGwsbWlkLEwsc2VnW25vZGVdLmxhenlBZGQpOwogICAgICAgICAgICBwdXNoQWRkKG1pZCsxLHIsUixzZWdbbm9kZV0ubGF6eUFkZCk7CiAgICAgICAgICAgIHNlZ1tub2RlXS5sYXp5QWRkPTA7CiAgICAgICAgICAgIHB1c2hNaW4obCxtaWQsTCxzZWdbbm9kZV0ubXgpOwogICAgICAgICAgICBwdXNoTWluKG1pZCsxLHIsUixzZWdbbm9kZV0ubXgpOwogICAgICAgICAgICBwdXNoTWF4KGwsbWlkLEwsc2VnW25vZGVdLm1uKTsKICAgICAgICAgICAgcHVzaE1heChtaWQrMSxyLFIsc2VnW25vZGVdLm1uKTsKICAgICAgICB9CiAgICB9CiAgICB2b2lkIGJ1aWxkKGludCBsLGludCByLGludCBub2RlLHZlY3RvcjxpbnQ+JnYpCiAgICB7CiAgICAgICAgaWYobD09cikKICAgICAgICB7CiAgICAgICAgICAgIGlmKGw8di5zaXplKCkpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIHNlZ1tub2RlXT1Ob2RlKHZbbF0sdltsXSwtaW5mLDEsdltsXSxpbmYsMSk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgcmV0dXJuOwogICAgICAgIH0KICAgICAgICBidWlsZChsLG1pZCxMLHYpOwogICAgICAgIGJ1aWxkKG1pZCsxLHIsUix2KTsKICAgICAgICBzZWdbbm9kZV09IG1lcmdlKHNlZ1tMXSxzZWdbUl0pOwogICAgfQogICAgdm9pZCB1cGRhdGVBZGQoaW50IGwsaW50IHIsaW50IG5vZGUsaW50IGx4LGludCByeCxpbnQgdmFsKQogICAgewogICAgICAgIGlmKGw+cnh8fHI8bHgpcmV0dXJuOwogICAgICAgIGlmKGw+PWx4JiZyPD1yeCkKICAgICAgICB7CiAgICAgICAgICAgIHB1c2hBZGQobCxyLG5vZGUsdmFsKTsKICAgICAgICAgICAgcmV0dXJuOwogICAgICAgIH0KICAgICAgICBwcm9wZWdhdGUobCxyLG5vZGUpOwogICAgICAgIHVwZGF0ZUFkZChsLG1pZCxMLGx4LHJ4LHZhbCk7CiAgICAgICAgdXBkYXRlQWRkKG1pZCsxLHIsUixseCxyeCx2YWwpOwogICAgICAgIHNlZ1tub2RlXT1tZXJnZShzZWdbTF0sc2VnW1JdKTsKICAgIH0KICAgIHZvaWQgdXBkYXRlU2V0KGludCBsLGludCByLGludCBub2RlLGludCBseCxpbnQgcngsaW50IHZhbCkKICAgIHsKICAgICAgICBpZihsPnJ4fHxyPGx4KXJldHVybjsKICAgICAgICBpZihsPj1seCYmcjw9cngpCiAgICAgICAgewogICAgICAgICAgICBwdXNoU2V0KGwscixub2RlLHZhbCk7CiAgICAgICAgICAgIHJldHVybjsKICAgICAgICB9CiAgICAgICAgcHJvcGVnYXRlKGwscixub2RlKTsKICAgICAgICB1cGRhdGVTZXQobCxtaWQsTCxseCxyeCx2YWwpOwogICAgICAgIHVwZGF0ZVNldChtaWQrMSxyLFIsbHgscngsdmFsKTsKICAgICAgICBzZWdbbm9kZV09bWVyZ2Uoc2VnW0xdLHNlZ1tSXSk7CiAgICB9CiAgICB2b2lkIHVwZGF0ZU1pbihpbnQgbCxpbnQgcixpbnQgbm9kZSxpbnQgbHgsaW50IHJ4LGludCBtbikKICAgIHsKICAgICAgICBpZihyPGx4fHxsPnJ4fHxzZWdbbm9kZV0ubXg8PW1uKSByZXR1cm47CiAgICAgICAgaWYobD49bHgmJnI8PXJ4JiZtbj5zZWdbbm9kZV0uc2VjTXgpCiAgICAgICAgewogICAgICAgICAgICBwdXNoTWluKGwscixub2RlLG1uKTsKICAgICAgICAgICAgcmV0dXJuOwogICAgICAgIH0KICAgICAgICBwcm9wZWdhdGUobCxyLG5vZGUpOwogICAgICAgIHVwZGF0ZU1pbihsLG1pZCxMLGx4LHJ4LG1uKTsKICAgICAgICB1cGRhdGVNaW4obWlkKzEscixSLGx4LHJ4LG1uKTsKICAgICAgICBzZWdbbm9kZV09bWVyZ2Uoc2VnW0xdLHNlZ1tSXSk7CiAgICB9CiAgICB2b2lkIHVwZGF0ZU1heChpbnQgbCxpbnQgcixpbnQgbm9kZSxpbnQgbHgsaW50IHJ4LGludCBteCkKICAgIHsKICAgICAgICBpZihsPnJ4fHxyPGx4fHxteDw9c2VnW25vZGVdLm1uKSByZXR1cm47CiAgICAgICAgaWYobD49bHgmJnI8PXJ4JiZteDxzZWdbbm9kZV0uc2VjTW4pCiAgICAgICAgewogICAgICAgICAgICBwdXNoTWF4KGwscixub2RlLG14KTsKICAgICAgICAgICAgcmV0dXJuOwogICAgICAgIH0KICAgICAgICBwcm9wZWdhdGUobCxyLG5vZGUpOwogICAgICAgIHVwZGF0ZU1heChsLG1pZCxMLGx4LHJ4LG14KTsKICAgICAgICB1cGRhdGVNYXgobWlkKzEscixSLGx4LHJ4LG14KTsKICAgICAgICBzZWdbbm9kZV09bWVyZ2Uoc2VnW0xdLHNlZ1tSXSk7CiAgICB9CiAgICBpbnQgcXVlcnlTdW0oaW50IGwsaW50IHIsaW50IG5vZGUsaW50IGx4LGludCByeCkKICAgIHsKICAgICAgICBpZihsPnJ4fHxyPGx4KXJldHVybiAwOwogICAgICAgIGlmKGw+PWx4JiZyPD1yeClyZXR1cm4gc2VnW25vZGVdLnN1bTsKICAgICAgICBwcm9wZWdhdGUobCxyLG5vZGUpOwogICAgICAgIHJldHVybiBxdWVyeVN1bShsLG1pZCxMLGx4LHJ4KStxdWVyeVN1bShtaWQrMSxyLFIsbHgscngpOwogICAgfQogICAgaW50IHF1ZXJ5TWluKGludCBsLGludCByLGludCBub2RlLGludCBseCxpbnQgcngpCiAgICB7CiAgICAgICAgaWYobD5yeHx8cjxseClyZXR1cm4gaW5mOwogICAgICAgIGlmKGw+PWx4JiZyPD1yeClyZXR1cm4gc2VnW25vZGVdLm1uOwogICAgICAgIHByb3BlZ2F0ZShsLHIsbm9kZSk7CiAgICAgICAgcmV0dXJuIG1pbihxdWVyeU1pbihsLG1pZCxMLGx4LHJ4KSxxdWVyeU1pbihtaWQrMSxyLFIsbHgscngpKTsKICAgIH0KICAgIGludCBxdWVyeU1heChpbnQgbCxpbnQgcixpbnQgbm9kZSxpbnQgbHgsaW50IHJ4KQogICAgewogICAgICAgIGlmKGw+cnh8fHI8bHgpcmV0dXJuIC1pbmY7CiAgICAgICAgaWYobD49bHgmJnI8PXJ4KXJldHVybiBzZWdbbm9kZV0ubXg7CiAgICAgICAgcHJvcGVnYXRlKGwscixub2RlKTsKICAgICAgICByZXR1cm4gbWF4KHF1ZXJ5TWF4KGwsbWlkLEwsbHgscngpLHF1ZXJ5TWF4KG1pZCsxLHIsUixseCxyeCkpOwogICAgfQojdW5kZWYgTAojdW5kZWYgUgojdW5kZWYgbWlkCnB1YmxpYzoKICAgIFNlZ21lbnRUcmVlQmVhdHModmVjdG9yPGludD4mdikKICAgIHsKICAgICAgICBpbnQgbj12LnNpemUoKSs1OwogICAgICAgIHN6PTE7CiAgICAgICAgd2hpbGUoc3o8PW4pc3o8PD0xOwogICAgICAgIHNlZz12ZWN0b3I8Tm9kZT4oc3o8PDEsTm9kZSgwLC1pbmYsLWluZiwwLGluZixpbmYsMCwwLC0xKSk7CiAgICAgICAgYnVpbGQoMCxzei0xLDAsdik7CiAgICB9CiAgICB2b2lkIHVwZGF0ZUFkZChpbnQgbCxpbnQgcixpbnQgdmFsKQogICAgewogICAgICAgIHVwZGF0ZUFkZCgwLHN6LTEsMCxsLHIsdmFsKTsKICAgIH0KICAgIHZvaWQgdXBkYXRlU2V0KGludCBsLGludCByLGludCB2YWwpCiAgICB7CiAgICAgICAgdXBkYXRlU2V0KDAsc3otMSwwLGwscix2YWwpOwogICAgfQogICAgdm9pZCB1cGRhdGVNaW4oaW50IGwsaW50IHIsaW50IG1uKQogICAgewogICAgICAgIHVwZGF0ZU1pbigwLHN6LTEsMCxsLHIsbW4pOwogICAgfQogICAgdm9pZCB1cGRhdGVNYXgoaW50IGwsaW50IHIsaW50IG14KQogICAgewogICAgICAgIHVwZGF0ZU1heCgwLHN6LTEsMCxsLHIsbXgpOwogICAgfQogICAgaW50IHF1ZXJ5TWF4KGludCBsLGludCByKQogICAgewogICAgICAgIHJldHVybiBxdWVyeU1heCgwLHN6LTEsMCxsLHIpOwogICAgfQogICAgaW50IHF1ZXJ5TWluKGludCBsLGludCByKQogICAgewogICAgICAgIHJldHVybiBxdWVyeU1pbigwLHN6LTEsMCxsLHIpOwogICAgfQogICAgaW50IHF1ZXJ5U3VtKGludCBsLGludCByKQogICAgewogICAgICAgIHJldHVybiBxdWVyeVN1bSgwLHN6LTEsMCxsLHIpOwogICAgfQojdW5kZWYgaW5mCn07CnNpZ25lZCBtYWluKCkKewogICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbygwKTtjaW4udGllKDApO2NvdXQudGllKDApOwogICAgaW50IG4scTtjaW4+Pm4+PnE7CiAgICB2ZWN0b3I8aW50PnMobisxKTsKICAgIFNlZ21lbnRUcmVlIHRyZWUobis1KTsKICAgIGZvcihpbnQgaT0xO2k8PW47aSsrKQogICAgewogICAgICAgIGNpbj4+c1tpXTsKICAgICAgICB0cmVlLnVwZGF0ZShpLGksc1tpXSk7CiAgICB9CiAgICB2ZWN0b3I8aW50PnYobisxKTsKICAgIFNlZ21lbnRUcmVlQmVhdHMgYmVhdHModik7CiAgICB3aGlsZShxLS0pCiAgICB7CiAgICAgICAgaW50IG9wO2Npbj4+b3A7CiAgICAgICAgaWYob3A9PTEpCiAgICAgICAgewogICAgICAgICAgICBpbnQgbCxyLHg7Y2luPj5sPj5yPj54OwogICAgICAgICAgICB0cmVlLnVwZGF0ZShsLHIseCk7CiAgICAgICAgICAgIGJlYXRzLnVwZGF0ZUFkZChsLHIsLXgpOwogICAgICAgICAgICBiZWF0cy51cGRhdGVNYXgobCxyLDApOwogICAgICAgIH0KICAgICAgICBlbHNlCiAgICAgICAgewogICAgICAgICAgICBpbnQgbCxyO2Npbj4+bD4+cjsKICAgICAgICAgICAgY291dDw8dHJlZS5xdWVyeShsLHIpK2JlYXRzLnF1ZXJ5U3VtKGwscik8PCdcbic7CiAgICAgICAgfQogICAgfQogICAgcmV0dXJuIDA7Cn0=