题目大意:有$n$个点,原来都是$1$,$m$次区间修改,将$[l,r]$中所有元素改为一个值,问每次操作后$n$个点的和。$n\leqslant10^9,m\leqslant3\times10^5$

题解:动态开点线段树,但是因为刚学习$ODT$,就拿这道题练手,直接相同元素直接修改,在修改的同时更新答案。

卡点:

 

C++ Code:

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <set>
#include <iostream>

int n, q, ans;

namespace ODT {
	struct node {
		int l, r;
		mutable int v;
		inline bool operator < (const node &rhs) const {
			return l < rhs.l;
		}
	} ;
	std::set<node> s;
	typedef std::set<node>::iterator SIT;
	SIT split(int pos) {
		SIT it = s.lower_bound((node) { pos, 0, 0 });
		if (it != s.end() && it -> l == pos) return it;
		--it; const int l = it -> l, r = it -> r, v = it -> v;
		s.erase(it), s.insert((node) { l, pos - 1, v });
		return s.insert((node) { pos, r, v}).first;
	}
	void assign(int l, int r, int v) {
		SIT R = split(r + 1), L = split(l);
		for (SIT it = L; it != R; ++it)
			ans -= it -> v * (it -> r - it -> l + 1);
		s.erase(L, R), s.insert((node) { l, r, v });
		ans += v * (r - l + 1);
	}
}

int main() {
	std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0);
	std::cin >> n >> q; ans = n;
	ODT::s.insert((ODT::node) { 1, n, 1 });
	while (q --> 0) {
		static int l, r, x;
		std::cin >> l >> r >> x; --x;
		ODT::assign(l, r, x);
		std::cout << ans << '\n';
	}
	return 0;
}

  

相关文章:

  • 2021-10-31
  • 2022-01-09
  • 2021-08-17
  • 2022-12-23
  • 2022-12-23
  • 2021-09-23
  • 2021-12-25
  • 2022-01-12
猜你喜欢
  • 2021-05-21
  • 2022-02-20
  • 2021-11-13
  • 2022-01-04
  • 2022-12-23
  • 2021-07-19
相关资源
相似解决方案