structnode{intl,r;mutableintval;// if we need change a node's value which already in the set by using iterator, we have to use mutable.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();// the position doesn't exist.std::set<node>::iteratorit=--odt.upper_bound((node){pos,0,0});// find the node that pos in;if(it->l==pos)returnit;// if pos is the begin of the node, return;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;// erase the original node, insert two node and return the left one's iterator.}// split the node [l,r] to two smaller node [l,pos),[pos,r];
inlinevoidassign(intl,intr,intv){std::set<node>::iteratoritr=split(r+1),itl=split(l);// because of [), so r+1. and **Remember, split(r+1) first. then split(l)**odt.erase(itl,itr),odt.insert((node){l,r,v});}// change all element in the interval [l,r] to v;