求助ODT
  • 板块学术版
  • 楼主AsoltA
  • 当前回复1
  • 已保存回复1
  • 发布时间2023/7/6 16:40
  • 上次更新2023/11/3 11:18:55
查看原帖
求助ODT
475419
AsoltA楼主2023/7/6 16:40

RT.

本人想实现一个最简单的odt,但是在区间赋值的时候出了问题。

操作如下:

  1. 将[2,4] 赋值为1;
  2. 将[6,7]赋值为2;
  3. 将[9,9]赋值为4;
  4. 将[4,5]赋值为3;
  5. 求整个ODT的和。

操作完的区间应该是[0,1,1,3,3,2,2,4]。 但是[6,7]那一段被吞掉了。导致求和从16变为12。

odt代码:

struct Node_t
{
  int l, r;
  mutable int v;

  Node_t(const int &il, const int &ir, const int &iv) : l(il), r(ir), v(iv) {}

  bool operator<(const Node_t &o) const { return l < o.l; }
};
std::set<Node_t> odt;
typedef std::set<Node_t>::iterator iter;
auto splt(int x)
{
	if(x>n) return odt.end();
	auto it=--odt.upper_bound(Node_t{x,0,0});
 	if (it->l==x) return it;
	int l=it->l,r=it->r,v=it->v;
	odt.erase(it);
	odt.insert(Node_t(l,x-1,v));
	return odt.insert(Node_t(x,r,v)).first;
}
void assign(int l, int r, int v)
{
	auto itr=splt(r+1),itl=splt(l);
	odt.erase(itl,itr);
	odt.insert(Node_t(l,r,v));
}
int getsum()
{
	int ans=0;
	auto itl=odt.begin(),itr=odt.end();
	for(;itl!=itr;++itl)
	{
		ans+=(itl->r-itl->l+1)*itl->v;
	}
	return ans;
}
2023/7/6 16:40
加载中...