ZOJ 3324 Machine

Mee is the only genius engineer in country Nii who is fond of making machines. One day, when he was working with his research, an amazing idea thrilled through his head and he decided to make a powerful machine which seemed to be able to solve an important problem in his research.

As the picture shown above, the machine is made up of n blocks in a line numbered from 0 to n - 1. Each moment there is one of the two possible actions.

  • A pressure is generated and it makes a certain consecutive blocks lower than where they had been.
  • One of the still existing pressures is released and the effect the pressure made cancels.

The following picture is the situation after a pressure from 0 to 2 and a pressure from 2 to 3.

The following picture is the situation after releasing the first pressure.

During the progress, the blocks in the original position are considered to promise something and are called H-blocks. The consecutive one or more H-blocks even form an H-group. Mee knew that recording the number of H-groups would be extremely helpful. So after each moment, the number of H-groups should be recorded.

But soon he was facing a serious problem. n could be large and there may be no enough material to finish the machine. What's more, it was impossible to manually calculate the number of H-groups. So he wants a program to simulate the machine and record the number of H-groups for him.

Input

The first line is the number of test cases T (T <= 10).

In each case the first line is n and m (1 <= n <= 108, 0 <= m <= 20000), where n is the number of blocks and m is the number of actions. The following m lines are the m actions. Two kinds of expressions exist.

  • p i j means a pressure acted on blocks from number i to j (inclusive, 0 <= i <= j < n).
  • r i j means the pressure acting on blocks from number i to j is released. i and j is always the effective range of an existing pressure (inclusive, 0 <= i <= j < n).

Output

For each case, first print a line "Case #?:" where ? is the case number starting from 1.

Then print the number of H-groups after each action.

Sample Input

2
6 6
p 2 2
r 2 2
p 3 3
p 2 3
p 2 2
r 2 3
10 3
p 2 7
p 3 6
p 4 5

Sample Output

Case #1:
2
1
2
2
2
2
Case #2:
2
2

2

线段树,类似矩阵面积并,为了省去离散化,我用了动态节点的线段树

#include<map>
#include<cmath>    
#include<queue>    
#include<vector>
#include<cstdio>    
#include<cstring>    
#include<algorithm>    
using namespace std;
#define ms(x,y) memset(x,y,sizeof(x))    
#define rep(i,j,k) for(int i=j;i<=k;i++)    
#define per(i,j,k) for(int i=j;i>=k;i--)    
#define loop(i,j,k) for (int i=j;i!=-1;i=k[i])    
#define inone(x) scanf("%d",&x)    
#define intwo(x,y) scanf("%d%d",&x,&y)    
#define inthr(x,y,z) scanf("%d%d%d",&x,&y,&z)    
typedef long long LL;
const int low(int x) { return x&-x; }
const int INF = 0x7FFFFFFF;
const int mod = 1e9 + 7;
const int N = 1e6 + 10;
int T, n, m, l, r;
int L[N], R[N], a[N], f[N], ls[N], rs[N], t;
char s[N];

int node()
{
	f[t] = L[t] = R[t] = ls[t] = rs[t] = 0; a[t] = 1; return t++;
}

void merge(int x, int l, int r)
{
	ls[x] = ls[l]; rs[x] = rs[r];
	a[x] = a[l] + a[r] - (rs[l] == 0 && rs[l] == ls[r]);
}

void add(int &x, int l, int r, int ll, int rr, int v)
{
	if (!x) x = node();
	if (ll <= l && r <= rr)
	{
		if (f[x] += v) ls[x] = 1, rs[x] = 1, a[x] = 0;
		else if (L[x] || R[x]) merge(x, L[x], R[x]);
		else x = 0;
	}
	else
	{
		int mid = l + r >> 1;
		if (ll <= mid) add(L[x], l, mid, ll, rr, v);
		if (rr > mid) add(R[x], mid + 1, r, ll, rr, v);
		if (!f[x])
		{
			if (L[x] || R[x]) merge(x, L[x], R[x]);
			else x = 0;
		}
	}
}

int main()
{
	int cas = a[0] = 1;
	for (inone(T); T--;)
	{
		intwo(n, m);
		int rt = 0; t = 1;
		printf("Case #%d:\n", cas++);
		rep(i, 1, m)
		{
			scanf("%s", s); intwo(l, r);
			add(rt, 0, n - 1, l, r, s[0] == 'p' ? 1 : -1);
			printf("%d\n", a[rt]);
		}
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值