#include<bits/stdc++.h>usingnamespacestd;#define int long longconstexprintsi=1e6+10;intn,q;inta[si];inttag[si];inlineintlowbit(intx){returnx&-x;}structBIT{intn,c[si],ans=0;inlinevoidmodify(intx,inty){for(registerinti=x;i<=n;i+=lowbit(i))c[i]+=y;}inlinevoidadd(intl,intr,intv){modify(l,v),modify(r+1,-v);}inlineintquery(intx){ans=0;for(registerinti=x;i;i-=lowbit(i))ans+=c[i];returnans;}}bitr;structnode{intl,r;mutableintval;node(constint&il,constint&ir,constint&iv):l(il),r(ir),val(iv){}inlinebooloperator<(constnode&b)const{returnl<b.l;}};std::set<node>odt;inlinestd::set<node>::iteratorsplit(intpos){if(pos>n)returnodt.end();std::set<node>::iteratorit=--odt.upper_bound((node){pos,0,0});if(it->l==pos)returnit;intl=it->l,r=it->r,v=it->val;odt.erase(it),odt.insert(node{l,pos-1,v});returnodt.insert((node){pos,r,v}).first;}inlinevoidassign(intl,intr,intv){std::set<node>::iteratoritr=split(r+1),itl=split(l);for(std::set<node>::iteratorit=itl;it!=itr;++it){bitr.add(it->l,it->r,tag[it->val]);}odt.erase(itl,itr),odt.insert((node){l,r,v});bitr.add(l,r,-tag[v]);}inlineintTag(intpos){std::set<node>::iteratorit=odt.lower_bound(node{pos,0,0});if(it!=odt.end()&&it->l==pos)returntag[it->val];--it;returntag[it->val];}signedmain(){scanf("%lld%lld",&n,&q),odt.insert((node){1,n,1});bitr.n=n;stringop;intl,r,c;while(q--){cin>>op;if(op=="Color")scanf("%lld%lld%lld",&l,&r,&c),assign(l,r,c);if(op=="Add")scanf("%lld%lld",&l,&c),tag[l]+=c;if(op=="Query")scanf("%lld",&c),printf("%lld\n",Tag(c)+bitr.query(c));}return0;}
#include<bits/stdc++.h>usingnamespacestd;#define int long longconstexprintsi=2e5+10;intn,m;std::vector<int>GO[si],G[si];intcol[si],ord[si],x[si];boolvis[si],have_sol=true;structnode{intty,u,v;}a[si];inlinevoiddfs(intu,intcur){col[u]=cur,vis[u]=true;for(int&v:GO[u]){if(vis[v]&&col[v]==cur)have_sol=false;if(vis[v])continue;dfs(v,cur==1?2:1);}}signedmain(){scanf("%lld%lld",&n,&m);for(registerinti=1;i<=m;++i){scanf("%lld%lld%lld",&a[i].ty,&a[i].u,&a[i].v);GO[a[i].u].push_back(a[i].v),GO[a[i].v].push_back(a[i].u);}for(registerinti=1;i<=n;++i)if(!vis[i])dfs(i,1);if(!have_sol)returnputs("NO"),0;for(registerinti=1;i<=m;++i){int&u=a[i].u,&v=a[i].v;if(a[i].ty==1){if(col[u]==1)G[u].push_back(v),++ord[v];elseG[v].push_back(u),++ord[u];}else{if(col[u]==1)G[v].push_back(u),++ord[u];elseG[u].push_back(v),++ord[v];}}std::queue<int>q;for(registerinti=1;i<=n;++i)if(ord[i]==0)q.push(i);inttot=0;while(!q.empty()){intu=q.front();q.pop(),x[u]=++tot;for(int&v:G[u])if(!--ord[v])q.push(v);}if(tot!=n)returnputs("NO"),0;puts("YES");for(registerinti=1;i<=n;++i){if(col[i]==1)putchar('L');elseputchar('R');printf(" %lld\n",x[i]);}return0;}
#include<bits/stdc++.h>usingnamespacestd;#define int long longconstexprintsi=3e5+10;constexprintinf=4e18+1;intn,q;structFenwick{inta[si];Fenwick(){memset(a,0x7f,sizeofa);}inlineintlowbit(intx){returnx&-x;}inlinevoidmodify(intx,intv){x=n-x+1;while(x<=n)a[x]=min(a[x],v),x+=lowbit(x);}inlineintsum(intx){intans=inf;x=n-x+1;while(x)ans=min(ans,a[x]),x-=lowbit(x);returnans;}}tr;intx[si],w[si];intStack[si],Top;std::vector<std::pair<int,int>>a[si];intans[si];signedmain(){cin>>n>>q;for(registerinti=1;i<=n;++i)cin>>x[i]>>w[i];for(registerinti=1;i<=q;++i){intl,r;cin>>l>>r;a[r].push_back(make_pair(l,i));}for(registerinti=1;i<=n;++i){while(Top&&w[i]<=w[Stack[Top]])tr.modify(Stack[Top],(x[i]-x[Stack[Top]])*(w[i]+w[Stack[Top]])),--Top;if(Top)tr.modify(Stack[Top],(x[i]-x[Stack[Top]])*(w[i]+w[Stack[Top]]));Stack[++Top]=i;for(autox:a[i])ans[x.second]=tr.sum(x.first);}for(registerinti=1;i<=q;++i)cout<<ans[i]<<endl;return0;}
#include<bits/stdc++.h>usingnamespacestd;#define int long longconstexprintsi=2e5+10;intn,m,sum=0;intdeg[si];std::vector<int>f[si];structMycmp{inlinebooloperator()(intx,inty)const{returnf[x].size()<f[y].size();}};structDsu{intpa[si];inlineintroot(intx){if(pa[x]!=x)returnpa[x]=root(pa[x]);returnpa[x];}inlineboolsame(intx,inty){returnroot(x)==root(y);}inlinevoidUnion(intx,inty){intrx=root(x),ry=root(y);if(rx==ry)return;pa[rx]=ry;}}dsu;std::multiset<int,Mycmp>s;std::vector<pair<int,int>>ans;signedmain(){cin>>n>>m;for(registerinti=1;i<=n;++i)cin>>deg[i],sum+=deg[i],dsu.pa[i]=i;if(sum!=2*n-2)returnputs("-1"),0;f[0].push_back(0);for(registerinti=1;i<=m;++i){intu,v;cin>>u>>v;--deg[u],--deg[v];if(dsu.same(u,v))returnputs("-1"),0;dsu.Union(u,v);}for(registerinti=1;i<=n;++i)if(deg[i]<0)returnputs("-1"),0;elsewhile(deg[i]--)f[dsu.root(i)].push_back(i);for(registerinti=1;i<=n;++i)if(f[i].size()>0)s.insert(i);while(!s.empty()){std::multiset<int,Mycmp>::iteratorit=s.begin(),itt=s.upper_bound(0);intrmp=*it;if(f[*it].size()>1)returnputs("-1"),0;s.erase(it);if(!f[rmp].size())continue;if(itt==s.end()){std::multiset<int,Mycmp>::iteratoryt=std::prev(s.end());intls=*yt;ans.push_back({f[rmp][0],*prev(f[ls].end())}),s.erase(yt);}else{intls=*itt;ans.push_back({f[rmp][0],*prev(f[ls].end())});s.erase(itt),f[ls].pop_back(),s.insert(ls);}}for(std::pair<int,int>x:ans)cout<<x.first<<" "<<x.second<<endl;return0;}
#include<bits/stdc++.h>usingnamespacestd;#define int long longintn,q;signedmain(){cin>>n>>q;std::set<int>s;for(registerinti=-1;i<=n;++i)s.insert(i);std::map<int,int>rec;while(q--){intop;cin>>op;if(op==0){intl,r,x;cin>>l>>r>>x;--l;if(!x){std::set<int>::iteratorit=s.lower_bound(l);while(*it<r)it=s.erase(it);}else{std::map<int,int>::iteratorit=rec.lower_bound(l);if(it!=rec.end()&&it->second<=r)continue;rec[l]=r,it=rec.find(l);while(it!=rec.begin()&&r<=std::prev(it)->second)rec.erase(std::prev(it));}}else{intx;cin>>x;--x;if(!s.count(x))puts("NO");else{autoqwq=s.find(x);intl=*std::prev(qwq),r=*std::next(qwq);autoit=rec.upper_bound(l);if(it!=rec.end()&&it->second<=r)puts("YES");elseputs("N/A");}}}return0;}
#include<bits/stdc++.h>usingnamespacestd;#define int long longconstexprintsi=1e6+10;std::map<int,std::set<int>>row,col;std::map<std::pair<int,int>,int>rec;std::vector<int>G[si];intdis[si];intsx,sy,gx,gy;inth,w,n,tot=0;inlinevoidnode(intx,inty){if(!(x<1||x>h||y<1||y>w)&&!rec.count({x,y}))rec[{x,y}]=++tot;}inlinevoidadd(intx,inty){G[x].push_back(y);}inlinevoidbfs(ints){for(registerinti=1;i<=tot;++i)dis[i]=-1;std::queue<int>q;q.push(s),dis[s]=0;while(!q.empty()){intu=q.front();q.pop();for(intv:G[u]){if(!~dis[v])dis[v]=dis[u]+1,q.push(v);}}}signedmain(){cin>>h>>w>>n>>sx>>sy>>gx>>gy;for(registerinti=1,u,v;i<=n;++i)cin>>u>>v,row[u].insert(v),col[v].insert(u),node(u-1,v),node(u+1,v),node(u,v+1),node(u,v-1);node(sx,sy),node(gx,gy);for(std::pair<pair<int,int>,int>iter:rec){intx=iter.first.first,y=iter.first.second;std::_Rb_tree_const_iterator<int>it;it=row[x].lower_bound(y);if(it!=row[x].begin())add(rec[{x,y}],rec[{x,*std::prev(it)+1}]);it=row[x].upper_bound(y);if(it!=row[x].end())add(rec[{x,y}],rec[{x,*it-1}]);it=col[y].lower_bound(x);if(it!=col[y].begin())add(rec[{x,y}],rec[{*std::prev(it)+1,y}]);it=col[y].upper_bound(x);if(it!=col[y].end())add(rec[{x,y}],rec[{*it-1,y}]);}bfs(rec[{sx,sy}]);returnprintf("%lld\n",dis[rec[{gx,gy}]]),0;}