Educational Codeforces Round 36 (Rated for Div. 2) E. Physical Education Lessons (Dynamic Open Point Line Segment Tree)

E. Physical Education Lessons

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

This year Alex has finished school, and now he is a first-year student of Berland State University. For him it was a total surprise that even though he studies programming, he still has to attend physical education lessons. The end of the term is very soon, but, unfortunately, Alex still hasn't attended a single lesson!

Since Alex doesn't want to get expelled, he wants to know the number of working days left until the end of the term, so he can attend physical education lessons during these days. But in BSU calculating the number of working days is a complicated matter:

There are n days left before the end of the term (numbered from 1 to n), and initially all of them are working days. Then the university staff sequentially publishes q orders, one after another. Each order is characterised by three numbers lr and k:

  • If k = 1, then all days from l to r (inclusive) become non-working days. If some of these days are made working days by some previous order, then these days still become non-working days;
  • If k = 2, then all days from l to r (inclusive) become working days. If some of these days are made non-working days by some previous order, then these days still become working days.

Help Alex to determine the number of working days left after each order!

Input

The first line contains one integer n, and the second line — one integer q (1 ≤ n ≤ 109, 1 ≤ q ≤ 3·105) — the number of days left before the end of the term, and the number of orders, respectively.

Then q lines follow, i-th line containing three integers l ir i and k i representing i-th order (1 ≤ l i ≤ r i ≤ n, 1 ≤ k i ≤ 2).

Output

Print q integers. i-th of them must be equal to the number of working days left until the end of the term after the first i orders are published.

Example

input

Copy

4
6
1 2 1
3 4 1
2 3 2
1 3 2
2 4 1
1 4 2

output

Copy

2
0
2
3
1
4

 

 Main idea:

An interval with a length of N up to 1e9, 3e5 operation queries, each time covering an interval to 0 or 1, and output the modified overall number of 1

solution:

The N in this question is 1e9, which obviously cannot be written with regular interval coverage. So there is a dynamic opening point, 3e5 operations, if you add a chain each time, it is equivalent to opening more log(N) space, 3e5 times is 3e5*log(N). The difference between the dynamic opening point and the conventional writing method is that the function of constructing the line segment tree becomes a dynamic application, and the left child and the right child have to be replaced by arrays .

Accepted code

#pragma GCC optimize(3)
#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;

#define sc scanf
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define pir pair <int, int>
#define MK(x, y) make_pair(x, y)
#define MEM(x, b) memset(x, b, sizeof(x))
#define MPY(x, b) memcpy(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))

typedef long long ll;
const int Mod = 1e9 + 7;
const int N = 3e5 + 100;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
inline ll dpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % Mod; b >>= 1; t = (t*t) % Mod; }return r; }
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t); b >>= 1; t = (t*t); }return r; }

int bt[N * 50];
int ls[N * 50], rs[N * 50], lzy[N * 50];
unordered_map <int, int> rt;
int cnt;

void Pushdown(int o, int len) {
	int Llen = len - (len >> 1);
	int Rlen = len >> 1;

	if (!ls[o])    // 申请左孩子
		ls[o] = ++cnt;
	if (!rs[o])    // 申请右孩子
		rs[o] = ++cnt;
	lzy[ls[o]] = lzy[rs[o]] = lzy[o];
	bt[ls[o]] = Llen * lzy[o];
	bt[rs[o]] = Rlen * lzy[o];

	lzy[o] = -1;
}
void Update(int &o, int L, int R, int l, int r, int k) {
	int len = R - L + 1;
	if (!o)
		o = ++cnt, lzy[o] = -1;   // 动态开区间

	if (L >= l && R <= r) {
		bt[o] = len * k;
		lzy[o] = k;
		return;
	}
	if (lzy[o] != -1)
	    Pushdown(o, len);

	int mid = (L + R) >> 1;
	if (mid >= l)
		Update(ls[o], L, mid, l, r, k);
	if (mid < r)
		Update(rs[o], mid + 1, R, l, r, k);
	bt[o] = bt[ls[o]] + bt[rs[o]];
}
int Ask(int o, int L, int R, int l, int r) {
	int len = R - L + 1;
	if (L >= l && R <= r)
		return bt[o];

	if (lzy[o] != -1)
		Pushdown(o, len);

	int mid = (L + R) >> 1, tot = 0;
	if (mid >= l)
		tot = Ask(ls[o], L, mid, l, r);
	if (mid < r)
		tot += Ask(rs[o], mid + 1, R, l, r);
	return tot;
}

int main()
{
	int n, q;
	cin >> n >> q;

	while (q--) {
		int l, r, op;
		sc("%d %d %d", &l, &r, &op);

		if (op == 2)
			op = 0;    // 区间置0
		Update(rt[1], 1, n, l, r, op);

		printf("%d\n", n - Ask(1, 1, n, 1, n));
	}
	return 0;  // 改数组大小!!!用pair记得改宏定义!!!
}

 

Guess you like

Origin blog.csdn.net/weixin_43851525/article/details/107755967