有人数据结构学疯了
查看原帖
有人数据结构学疯了
750240
Hellsing_Alucard楼主2023/2/4 09:56

有人数据结构学疯了,黄题用珂朵莉树

#include <bits/stdc++.h>
using namespace std;
#define int long long 
inline int read(){
	int x=0,f=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
struct node{
	int l, r;
	mutable int v;
	//node(int l, int r, int v) : l(l), r(r), v(v) {}
	bool operator<(const node &o) const { return l < o.l; }
};
set<node> odt;
int n;
auto split(int pos){
	auto it = odt.lower_bound({pos,0,0});
	if (it != odt.end() && it->l == pos)return it;
	it--;
	int l = it->l, r = it->r, v = it->v;
	odt.erase(it);
	odt.insert({l,pos-1,v});
	return odt.insert({pos,r,v}).first;
}
void assign(int l, int r, int v){
	auto end = split(r + 1), begin = split(l);
	odt.erase(begin, end);
	odt.insert({l,r,v});
}
int sum(int l, int r){
	int tot = 0;
	auto end = split(r + 1);
	for (auto it = split(l); it != end; it++)
		tot=(tot +(it->v)* (it->r - it->l + 1));
	return tot;
}
signed main(){
	n = read();
	odt.insert({-2147483647,2147483647,0});
	int l,r;
	while(n--){
		l=read();r=read()-1;
		assign(l,r,1);
	}
	printf("%lld\n", sum(-2147483647,2147483647));
	return 0;
}
2023/2/4 09:56
加载中...