ZOJ3324-Machine

Machine

Time Limit: 3 Seconds       Memory Limit: 32768 KB

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

Author:  ZHUANG, Junyuan
Source:  The 7th Zhejiang Provincial Collegiate Programming Contest


题意:给出n个连续按键,给定操作有2种,一种是让[X,Y]下压1格,一种是让[X,Y]向上恢复1格,每种恢复操作必定对应之前的下降下降。初始状态在LEVEL=0处,对每个操作回答有几段LEVEL=0的连续方块。

解题思路:因为按键数量较大,采用动态建点或者离散化,数组f用来记录每段被下压多少,数组a用来记录每段有多少LEVEL=0的连续方块,数组ls和数组rs来记录每段左右端点是否被下压


离散化:


#include <iostream>  
#include <cstdio>  
#include <string>  
#include <cstring>  
#include <cmath> 
#include <algorithm>
#include <queue>  
#include <vector>  
#include <set>  
#include <stack>
#include <map>  
#include <climits>  

using namespace std;

#define LL long long  
const int INF = 0x3f3f3f3f;

int t, n, m;
int l[300009], r[300009], x[3 * 300009];
char ch[300009][5];
int a[4 * 300009], f[4 * 300009], lx[4 * 300009], rx[4 * 300009];

void build(int k, int l, int r)
{
	a[k] = 1, f[k] = lx[k] = rx[k] = 0;
	if (l == r) return;
	int mid = (l + r) >> 1;
	build(k << 1, l, mid);
	build(k << 1 | 1, mid + 1, r);
}

void Merge(int k, int l, int r)
{
	if (f[k]) { a[k] = 0; return; }
	if (l == r) a[k] = 1;
	else a[k] = a[k << 1] + a[k << 1 | 1] - ((!lx[k << 1 | 1] && !rx[k << 1]) ? 1 : 0);
}

void update(int k, int l, int r, int ll, int rr, int val)
{
	if (l >= ll&&r <= rr)
	{
		f[k] += val;
		lx[k] += val, rx[k] += val;
		Merge(k, l, r);
		return;
	}
	int mid = (l + r) >> 1;
	if (ll <= mid) update(k << 1, l, mid, ll, rr, val);
	if (rr > mid) update(k << 1 | 1, mid + 1, r, ll, rr, val);
	lx[k] = lx[k << 1] + f[k], rx[k] = rx[k << 1 | 1] + f[k];
	Merge(k, l, r);
}

int main()
{
	int cas = 1;
	scanf("%d", &t);
	while (t--)
	{
		printf("Case #%d:\n", cas++);
		scanf("%d %d", &n, &m);
		int cnt = 1;
		for (int i = 1; i <= m; i++)
		{
			scanf("%s%d%d", ch[i], &l[i], &r[i]);
			x[cnt++] = l[i], x[cnt++] = r[i];
			x[cnt++] = max(0, l[i] - 1), x[cnt++] = min(n - 1, l[i] + 1);
			x[cnt++] = max(0, r[i] - 1), x[cnt++] = min(n - 1, r[i] + 1);
		}
		sort(x + 1, x + cnt);
		cnt = unique(x + 1, x + cnt) - x;
		build(1, 1, cnt - 1);
		for (int i = 1; i <= m; i++)
		{
			int k = lower_bound(x + 1, x + cnt, l[i]) - x;
			int kk = lower_bound(x + 1, x + cnt, r[i]) - x;
			update(1, 1, cnt - 1, k, kk, ch[i][0] == 'p' ? 1 : -1);
			printf("%d\n", a[1]);
		}
	}
	return 0;
}


动态建点:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>

using namespace std;

#define LL long long
const int INF=0x3f3f3f3f;
const int N=1e6;
int t,n,m,l,r;
int L[N],R[N],a[N],f[N],ls[N],rs[N],tot;
char s[N];

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

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;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d %d",&n,&m);
		int rt=0;
		tot=1;
		printf("Case #%d:\n",cas++);
		while(m--)
		{
			scanf("%s",s);
			scanf("%d %d",&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、付费专栏及课程。

余额充值