Educational Codeforces Round 36 (Rated for Div. 2) E. Physical Education Lessons (动态开点线段树)

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

 

 题目大意:

一个长度N最多为1e9的区间,3e5次操作询问,每次把一个区间覆盖成0或1,并输出修改后整体的1个数

解法:

这题的N是1e9,显然不能用常规的区间覆盖来写。于是就有了动态开点,3e5次操作,假如每次都加一条链,相当于多开了log(N)的空间,3e5次就是3e5*log(N)。动态开点和常规写法的区间在于,构建线段树的函数变成了动态申请,左孩子和右孩子也得用数组来替代

Accepted code

#pragma GCC optimize(3)
#include
#include
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 
#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  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记得改宏定义!!!
}

 

你可能感兴趣的:(Codeforces,线段树)