結果

問題 No.1673 Lamps on a line
ユーザー kyon2326kyon2326
提出日時 2021-09-10 22:51:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 67 ms / 2,000 ms
コード長 16,847 bytes
コンパイル時間 2,959 ms
コンパイル使用メモリ 267,276 KB
実行使用メモリ 35,968 KB
最終ジャッジ日時 2024-06-12 02:34:55
合計ジャッジ時間 3,697 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 13 ms
5,376 KB
testcase_03 AC 12 ms
5,376 KB
testcase_04 AC 20 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 19 ms
5,376 KB
testcase_07 AC 56 ms
35,968 KB
testcase_08 AC 15 ms
35,968 KB
testcase_09 AC 57 ms
11,308 KB
testcase_10 AC 67 ms
19,584 KB
testcase_11 AC 14 ms
19,584 KB
testcase_12 AC 63 ms
35,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace std;
/*
#include <atcoder/all>
using namespace atcoder;
*/
/*
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
using bll = boost::multiprecision::cpp_int;
using bdouble = boost::multiprecision::number<boost::multiprecision::cpp_dec_float<100>>;
using namespace boost::multiprecision;
*/
#if defined(LOCAL_TEST) || defined(LOCAL_DEV)
	#define BOOST_STACKTRACE_USE_ADDR2LINE
	#define BOOST_STACKTRACE_ADDR2LINE_LOCATION /usr/local/opt/binutils/bin/addr2line
	#define _GNU_SOURCE 1
	#include <boost/stacktrace.hpp>
#endif
#ifdef LOCAL_TEST
	namespace std {
		template <typename T> class dvector : public std::vector<T> {
		public:
			using std::vector<T>::vector;
			template <typename T_ = T, typename std::enable_if_t<std::is_same_v<T_, bool>, std::nullptr_t> = nullptr>
			std::vector<bool>::reference operator[](std::size_t n) {
				if (this->size() <= n) { std::cerr << boost::stacktrace::stacktrace() << '\n' << "vector::_M_range_check: __n (which is " << n << ") >= this->size() (which is " << this->size() << ")" << '\n'; } return this->at(n);
			}
			template <typename T_ = T, typename std::enable_if_t<std::is_same_v<T_, bool>, std::nullptr_t> = nullptr>
			const T_ operator[](std::size_t n) const {
				if (this->size() <= n) { std::cerr << boost::stacktrace::stacktrace() << '\n' << "vector::_M_range_check: __n (which is " << n << ") >= this->size() (which is " << this->size() << ")" << '\n'; } return this->at(n);
			}
			template <typename T_ = T, typename std::enable_if_t<!std::is_same_v<T_, bool>, std::nullptr_t> = nullptr>
			T_& operator[](std::size_t n) {
				if (this->size() <= n) { std::cerr << boost::stacktrace::stacktrace() << '\n' << "vector::_M_range_check: __n (which is " << n << ") >= this->size() (which is " << this->size() << ")" << '\n'; } return this->at(n);
			}
			template <typename T_ = T, typename std::enable_if_t<!std::is_same_v<T_, bool>, std::nullptr_t> = nullptr>
			const T_& operator[](std::size_t n) const {
				if (this->size() <= n) { std::cerr << boost::stacktrace::stacktrace() << '\n' << "vector::_M_range_check: __n (which is " << n << ") >= this->size() (which is " << this->size() << ")" << '\n'; } return this->at(n);
			}
		};
		template <typename T, typename Compare = std::less<T>, typename Allocator = std::allocator<T>> class dmultiset : public std::multiset<T,Compare,Allocator> {
		public:
			using std::multiset<T,Compare,Allocator>::multiset;
			const typename std::multiset<T,Compare,Allocator>::iterator erase(const typename std::multiset<T,Compare,Allocator>::iterator it) {
				return std::multiset<T,Compare,Allocator>::erase(it);
			}
			std::size_t erase([[maybe_unused]] const T& x) {
				std::cerr << boost::stacktrace::stacktrace() << '\n'; assert(false);
			}
			std::size_t erase_all_elements(const T& x) {
				return std::multiset<T,Compare,Allocator>::erase(x);
			}
		};
	}
	#define vector dvector
	#define multiset dmultiset
	class SIGFPE_exception : std::exception {};
	class SIGSEGV_exception : std::exception {};
	void catch_SIGFPE([[maybe_unused]] int e) { std::cerr << boost::stacktrace::stacktrace() << '\n'; throw SIGFPE_exception(); }
	void catch_SIGSEGV([[maybe_unused]] int e) { std::cerr << boost::stacktrace::stacktrace() << '\n'; throw SIGSEGV_exception(); }
	signed convertedmain();
	signed main() { signal(SIGFPE, catch_SIGFPE); signal(SIGSEGV, catch_SIGSEGV); return convertedmain(); }
	#define main() convertedmain()
#else
	#define erase_all_elements erase
#endif
#ifdef LOCAL_DEV
	template <typename T1, typename T2> std::ostream& operator<<(std::ostream& s, const std::pair<T1, T2>& p) {
		return s << "(" << p.first << ", " << p.second << ")"; }
	template <typename T, std::size_t N> std::ostream& operator<<(std::ostream& s, const std::array<T, N>& a) {
		s << "{ "; for (std::size_t i = 0; i < N; ++i){ s << a[i] << "\t"; } s << "}"; return s; }
	template <typename T> std::ostream& operator<<(std::ostream& s, const std::set<T>& se) {
		s << "{ "; for (auto itr = se.begin(); itr != se.end(); ++itr){ s << (*itr) << "\t"; } s << "}"; return s; }
	template <typename T> std::ostream& operator<<(std::ostream& s, const std::multiset<T>& se) {
		s << "{ "; for (auto itr = se.begin(); itr != se.end(); ++itr){ s << (*itr) << "\t"; } s << "}"; return s; }
	template <typename T1, typename T2> std::ostream& operator<<(std::ostream& s, const std::map<T1, T2>& m) {
		s << "{\n"; for (auto itr = m.begin(); itr != m.end(); ++itr){ s << "\t" << (*itr).first << " : " << (*itr).second << "\n"; } s << "}"; return s; }
	template <typename T> std::ostream& operator<<(std::ostream& s, const std::deque<T>& v) {
		for (std::size_t i = 0; i < v.size(); ++i){ s << v[i]; if (i < v.size() - 1) s << "\t"; } return s; }
	template <typename T> std::ostream& operator<<(std::ostream& s, const std::vector<T>& v) {
		for (std::size_t i = 0; i < v.size(); ++i){ s << v[i]; if (i < v.size() - 1) s << "\t"; } return s; }
	template <typename T> std::ostream& operator<<(std::ostream& s, const std::vector<std::vector<T>>& vv) {
		s << "\\\n"; for (std::size_t i = 0; i < vv.size(); ++i){ s << vv[i] << "\n"; } return s; }
	template <typename T, std::size_t N, typename std::enable_if_t<!std::is_same_v<T, char>, std::nullptr_t> = nullptr> std::ostream& operator<<(std::ostream& s, const T (&v)[N]) {
		for (std::size_t i = 0; i < N; ++i){ s << v[i]; if (i < N - 1) s << "\t"; } return s; }
	#if __has_include(<ext/pb_ds/assoc_container.hpp>)
		template <typename Key, typename Compare> std::ostream& operator<<(std::ostream& s, const __gnu_pbds::tree<Key, __gnu_pbds::null_type, Compare, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>& se) {
			s << "{ "; for (auto itr = se.begin(); itr != se.end(); ++itr){ s << (*itr) << "\t"; } s << "}"; return s; }
		template <typename Key, typename T, typename Hash> std::ostream& operator<<(std::ostream& s, const __gnu_pbds::gp_hash_table<Key, T, Hash>& m) {
			s << "{\n"; for (auto itr = m.begin(); itr != m.end(); ++itr){ s << "\t" << (*itr).first << " : " << (*itr).second << "\n"; } s << "}"; return s; }
	#endif
	void debug_impl() { std::cerr << '\n'; }
	template <typename Head, typename... Tail> void debug_impl(const Head& head, const Tail&... tail) { std::cerr << " " << head << (sizeof...(tail) ? "," : ""); debug_impl(tail...); }
	#define debug(...) do { std::cerr << ":" << __LINE__ << " (" << #__VA_ARGS__ << ") ="; debug_impl(__VA_ARGS__); } while (false)
	constexpr inline long long prodlocal([[maybe_unused]] long long prod, [[maybe_unused]] long long local) { return local; }
#else
	#define debug(...) do {} while (false)
	constexpr inline long long prodlocal([[maybe_unused]] long long prod, [[maybe_unused]] long long local) { return prod; }
#endif
//#define int long long
using ll = long long;
//INT_MAX = (1<<31)-1 = 2147483647, INT64_MAX = (1LL<<63)-1 = 9223372036854775807
constexpr ll INF = std::numeric_limits<ll>::max() == INT_MAX ? (ll)1e9 + 7 : (ll)1e18;
constexpr ll MOD = (ll)1e9 + 7; //primitive root = 5
//constexpr ll MOD = 998244353; //primitive root = 3
constexpr double EPS = 1e-9;
constexpr ll dx[4] = {1, 0, -1, 0};
constexpr ll dy[4] = {0, 1, 0, -1};
constexpr ll dx8[8] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr ll dy8[8] = {0, 1, 0, -1, 1, -1, 1, -1};
#define rep(i, n)   for(ll i=0, i##_length=(n); i< i##_length; ++i)
#define repeq(i, n) for(ll i=1, i##_length=(n); i<=i##_length; ++i)
#define rrep(i, n)   for(ll i=(n)-1; i>=0; --i)
#define rrepeq(i, n) for(ll i=(n)  ; i>=1; --i)
#define all(v) std::begin(v), std::end(v)
#define rall(v) std::rbegin(v), std::rend(v)
void p() { std::cout << '\n'; }
template <typename Head, typename... Tail> void p(const Head& head, const Tail&... tail) { std::cout << head << (sizeof...(tail) ? " " : ""); p(tail...); }
template <typename T> inline void pv(const std::vector<T>& v) { for(ll i=0, N=v.size(); i<N; i++) std::cout << v[i] << " \n"[i==N-1]; }
template <typename T> inline bool chmax(T& a, T b) { return a < b && (a = b, true); }
template <typename T> inline bool chmin(T& a, T b) { return a > b && (a = b, true); }
template <typename T> inline void uniq(std::vector<T>& v) { std::sort(v.begin(), v.end()); v.erase(std::unique(v.begin(), v.end()), v.end()); }
template <typename T> inline ll sz(const T& v) { return std::size(v); }
template <typename T, std::size_t N> std::vector<T> make_vector_impl(std::vector<ll>& sizes, typename std::enable_if<(N==1), const T&>::type x) { return std::vector<T>(sizes.front(),x); }
template <typename T, std::size_t N> auto make_vector_impl(std::vector<ll>& sizes, typename std::enable_if<(N>1), const T&>::type x) { ll size=sizes.back(); sizes.pop_back(); return std::vector<decltype(make_vector_impl<T,N-1>(sizes,x))>(size,make_vector_impl<T,N-1>(sizes,x)); }
template <typename T, std::size_t N> auto make_vector(const ll (&sizes)[N], const T& x=T()) { std::vector<ll> s(N); for(std::size_t i=0; i<N; ++i)s[i]=sizes[N-1-i]; return make_vector_impl<T,N>(s,x); }
template <typename T> struct Indexer {
	struct iterator {
		ll _pos;
		typename T::iterator _it;
		iterator(typename T::iterator it): _pos(0), _it(it) {}
		std::pair<ll, typename std::iterator_traits<typename T::iterator>::reference> operator*() const { return {_pos, *_it}; }
		iterator& operator++() { ++_pos; ++_it; return *this; }
		iterator operator++(int) { iterator tmp(*this); ++*this; return tmp; }
		bool operator==(iterator const& it) const { return _it == it._it; }
		bool operator!=(iterator const& it) const { return !(*this == it); }
	};
	T& _container;
	Indexer(T& t): _container(t) {}
	iterator begin() const { return iterator(_container.begin()); }
	iterator end() const { return iterator(_container.end()); }
};
template <typename T> Indexer<T> indexer(const T& t) { return Indexer<T>(t); }
#define repv(i, val, v) for(auto&& [i, val] : indexer(v))
#if __has_include(<ext/pb_ds/assoc_container.hpp>)
	template <typename Key, typename Mapped, typename Hash = std::hash<Key>, typename std::enable_if_t<std::is_integral_v<Key>, std::nullptr_t> = nullptr> struct fmap : public __gnu_pbds::gp_hash_table<Key, Mapped, Hash> {
		using __gnu_pbds::gp_hash_table<Key, Mapped, Hash>::gp_hash_table;
		template <typename T> fmap(std::initializer_list<std::initializer_list<T>> il) : __gnu_pbds::gp_hash_table<Key, Mapped, Hash>() {
			for (auto&& x : il) __gnu_pbds::gp_hash_table<Key, Mapped, Hash>::insert(std::pair<Key, Mapped>(*x.begin(), *(x.begin() + 1)));
		}
		template <typename T> ll count(const T& x) const {
			return __gnu_pbds::gp_hash_table<Key, Mapped, Hash>::find(x) != __gnu_pbds::gp_hash_table<Key, Mapped, Hash>::end();
		}
	};
#else
	template <typename Key, typename Mapped> using fmap = std::map<Key, Mapped>;
#endif

/*-----8<-----template-----8<-----*/

//[lib]lazy.cpp
/*
実装では木を 1-indexed の配列で表現している。
ノード k について, 親ノードは k/2, 子ノードは 2k, 2k+1 である。

LazySegmentTree(n, f, g, h, M1, OM0):= サイズ n の初期化。
	f は2つの区間の要素をマージする二項演算, 
	g は要素と作用素をマージする二項演算(第三引数は対応する区間の長さ), 
	h は作用素同士をマージする二項演算, M1 はモノイドの単位元, 
	M1 はモノイドの単位元, 
	OM0 は作用素の単位元である。
set(k,x):= k 番目の要素に x を代入する。
build():= セグメント木を構築する。
query(a,b):= 区間 [a,b) に対して二項演算した結果を返す。
update(a, b, x) := 区間 [a,b) に作用素 x を適用する。
operator[k] := k 番目の要素を返す。
find_first(a, check) := [a,x) が check を満たす最初の要素位置 x を返す。見つからない場合は -1 を返す。
find_last(b, check) := [x,b) が check を満たす最後の要素位置 x を返す。見つからない場合は -1 を返す。
*/
template< typename Monoid, typename OperatorMonoid, typename F , typename G , typename H >
class LazySegmentTree {
public:
	ll sz, height;
	vector< Monoid > data;
	vector< OperatorMonoid > lazy;
	const F f;
	const G g;
	const H h;
	const Monoid M1;
	const OperatorMonoid OM0;

	LazySegmentTree(ll n, const F &f, const G &g, const H &h, const Monoid &M1, const OperatorMonoid &OM0)
			: f(f), g(g), h(h), M1(M1), OM0(OM0) {
		sz = 1;
		height = 0;
		while(sz < n) sz <<= 1, height++;
		data.assign(2 * sz, M1);
		lazy.assign(2 * sz, OM0);
	}

	void set(ll k, const Monoid &x) {
		data[k + sz] = x;
	}

	void build() {
		for(ll k = sz - 1; k > 0; k--) {
			data[k] = f(data[2 * k + 0], data[2 * k + 1]);
		}
	}

	inline void propagate(ll k) {
		if(lazy[k] != OM0) {
			lazy[2 * k + 0] = h(lazy[2 * k + 0], lazy[k]);
			lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]);
			data[k] = reflect(k);
			lazy[k] = OM0;
		}
	}

	inline Monoid reflect(ll k) {
		return lazy[k] == OM0 ? data[k] : g(data[k], lazy[k]);
	}

	inline void recalc(ll k) {
		while(k >>= 1) data[k] = f(reflect(2 * k + 0), reflect(2 * k + 1));
	}

	inline void thrust(ll k) {
		for(ll i = height; i > 0; i--) propagate(k >> i);
	}

	void update(ll a, ll b, const OperatorMonoid &x) {
		thrust(a += sz);
		thrust(b += sz - 1);
		for(ll l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
			if(l & 1) lazy[l] = h(lazy[l], x), ++l;
			if(r & 1) --r, lazy[r] = h(lazy[r], x);
		}
		recalc(a);
		recalc(b);
	}

	Monoid query(ll a, ll b) {
		thrust(a += sz);
		thrust(b += sz - 1);
		Monoid L = M1, R = M1;
		for(ll l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
			if(l & 1) L = f(L, reflect(l++));
			if(r & 1) R = f(reflect(--r), R);
		}
		return f(L, R);
	}

	Monoid operator[](const ll &k) {
		return query(k, k + 1);
	}

	template< typename C >
	ll find_subtree(ll a, const C &check, Monoid &M, bool type) {
		while(a < sz) {
			propagate(a);
			Monoid nxt = type ? f(reflect(2 * a + type), M) : f(M, reflect(2 * a + type));
			if(check(nxt)) a = 2 * a + type;
			else M = nxt, a = 2 * a + 1 - type;
		}
		return a - sz;
	}

	template< typename C >
	ll find_first(ll a, const C &check) {
		Monoid L = M1;
		if(a <= 0) {
			if(check(f(L, reflect(1)))) return find_subtree(1, check, L, false);
			return -1;
		}
		thrust(a + sz);
		ll b = sz;
		for(a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
			if(a & 1) {
				Monoid nxt = f(L, reflect(a));
				if(check(nxt)) return find_subtree(a, check, L, false);
				L = nxt;
				++a;
			}
		}
		return -1;
	}

	template< typename C >
	ll find_last(ll b, const C &check) {
		Monoid R = M1;
		if(b >= sz) {
			if(check(f(reflect(1), R))) return find_subtree(1, check, R, true);
			return -1;
		}
		thrust(b + sz - 1);
		ll a = sz;
		for(b += sz; a < b; a >>= 1, b >>= 1) {
			if(b & 1) {
				Monoid nxt = f(reflect(--b), R);
				if(check(nxt)) return find_subtree(b, check, R, true);
				R = nxt;
			}
		}
		return -1;
	}

	// [L, R)の範囲で関数check を満たす/満たさない境目のindexを返す
	// auto check = [](Monoid data){ 何かしら };
	// 返却値 = 満たさない最初のindex, 返却値-L = 満たす要素数
	// L が固定, たぶん R が機能していない
	template<typename Func>
	ll partitionpoint(ll L, ll R, const Func &check){
		ll x = find_first(L, [&](const Monoid &data){return !check(data);});
		return x==-1||x>R ? R : x;
	}
	// R が固定, たぶん L が機能していない
	// 返却値 = 満たさない最初のindex, R-返却値 = 満たさない要素数
	template<typename Func>
	ll partitionpoint_rev(ll L, ll R, const Func &check){
		ll x = find_last(R, [&](const Monoid &data){return check(data);});
		return x==-1||x<L ? L : x+1;
	}
};
template< typename Monoid, typename OperatorMonoid, typename F , typename G , typename H >
std::ostream& operator<<(std::ostream& s, LazySegmentTree<Monoid,OperatorMonoid,F,G,H> seg) { 
	for(ll i=0; i<seg.sz; i++) s << seg[i] << "\t\n"[i==seg.sz-1];
	return s;
}

/*-----8<-----library-----8<-----*/

void solve() {
	ll N,Q;
	cin >> N >> Q;
	struct Data { ll value; ll l,r; };
	using Lazy = ll;
	auto fF = [](const Data &x, const Data &y) -> Data {
		return {x.value+y.value, min(x.l,y.l), max(x.r,y.r)};
	};
	auto fG = [](const Data& x, Lazy& y) -> Data {
		if (y == 0) return x;
		ll val = (x.r - x.l) - x.value;
		return {val, x.l, x.r};
	};
	auto fH = [](const Lazy &x, const Lazy &y){ return x ^ y; };
	const ll segsize = N;
	LazySegmentTree<Data,Lazy,decltype(fF),decltype(fG),decltype(fH)> seg(segsize, fF, fG, fH, {0,INF,-1}, 0);
	rep(i, segsize) seg.set(i, {0, i, i+1});
	seg.build();

    while(Q--){
		ll l, r;
		cin >> l >> r;
		l--;
		seg.update(l, r, 1);
		ll t = seg.query(0, N).value;
		p(t);
	}
}

signed main() {
#ifndef LOCAL_DEV
	std::cin.tie(nullptr);
	std::ios::sync_with_stdio(false);
#endif
	//ll Q; cin >> Q; while(Q--)solve();
	solve();
	return 0;
}
0