ZOJ 3324Machine

来源:互联网 发布:网络招聘平台有哪些 编辑:程序博客网 时间:2024/06/14 09:21

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

26 6p 2 2r 2 2p 3 3p 2 3p 2 2r 2 310 3p 2 7p 3 6p 4 5

Sample Output

Case #1:212222Case #2:22

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
原创粉丝点击