HDU-OJ-1698 Just a Hook

来源:互联网 发布:明天教室网络课怎么样 编辑:程序博客网 时间:2024/05/01 17:57

Just a Hook

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)


Problem Description
In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length.



Now Pudge wants to do some operations on the hook.

Let us number the consecutive metallic sticks of the hook from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver sticks or golden sticks.
The total value of the hook is calculated as the sum of values of N metallic sticks. More precisely, the value for each kind of stick is calculated as follows:

For each cupreous stick, the value is 1.
For each silver stick, the value is 2.
For each golden stick, the value is 3.

Pudge wants to know the total value of the hook after performing the operations.
You may consider the original hook is made up of cupreous sticks.
 

Input
The input consists of several test cases. The first line of the input is the number of the cases. There are no more than 10 cases.
For each case, the first line contains an integer N, 1<=N<=100,000, which is the number of the sticks of Pudge’s meat hook and the second line contains an integer Q, 0<=Q<=100,000, which is the number of the operations.
Next Q lines, each line contains three integers X, Y, 1<=X<=Y<=N, Z, 1<=Z<=3, which defines an operation: change the sticks numbered from X to Y into the metal kind Z, where Z=1 represents the cupreous kind, Z=2 represents the silver kind and Z=3 represents the golden kind.
 

Output
For each case, print a number in a line representing the total value of the hook after the operations. Use the format in the example.
 

Sample Input
11021 5 25 9 3
 

Sample Output
Case 1: The total value of the hook is 24.
 

————————————————————进阶的分割线————————————————————
思路:顺便我还看了看Dota里的这个英雄……简直无情啊。参见:百度百科
线段树的成段更新,依然是模仿(纯粹模仿)NotOnlySuccess的代码。为什么要成段更新呢?避免重复劳动
两次更新之间的区间如果有大部分是重合的,何必更新两次?那么思想就是,倘若要更新某一段,我就暂且更新这个段结点,并在这个区间上写下备忘:此段内所有子结点都是val。然后再更新某一段的时候,又一次访问了这个段结点,有新事情要做,但是看到有备忘。有备忘说明这层下面有事儿上次留着没做,这事儿不做,就不能更改备忘。那么先把备忘交给左儿子、右儿子,让他们知道是什么事儿,之后这个段结点的备忘就可以重新写而不被覆盖了。
就这样一次一次写备忘,下放备忘,到最后做过的事情都是必要的。
所谓的懒惰标记就是指:符合目标区间之内的时候,只更新段结点,设置一个标记,就return了。
代码如下:
/****************************************/ #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <cmath> #include <stack> #include <queue> #include <vector> #include <map> #include <string> #include <iostream> using namespace std;/****************************************/#define lson l, m, rt<<1#define rson m+1, r, rt<<1|1const int N = 100010;int tree_sum[N<<2], lazy[N<<2];int A, B, val;void Up(int rt) {tree_sum[rt] = tree_sum[rt<<1] + tree_sum[rt<<1|1];}void Down(int rt, int len) {if(lazy[rt]) {//本层有懒惰标记说明有事儿还没完成lazy[rt<<1|1] = lazy[rt<<1] = lazy[rt];tree_sum[rt<<1] = (len-(len>>1)) * lazy[rt];//下放标记之后,更新左子树、右子树tree_sum[rt<<1|1] = (len>>1) * lazy[rt];lazy[rt] = 0;//本层没事儿了,反正有事儿也是下一层的事儿,不用我管}}void build(int l, int r, int rt) {tree_sum[rt] = 1;lazy[rt] = 0;if(l == r)return ;int m = (l+r) >> 1;build(lson);build(rson);Up(rt);}void update(int l, int r, int rt) {if(A <= l&&r <= B) {//倘若当前区间在目标区间之内,则更新懒惰标记即可,不会进入下面的懒惰下放及更新lazy[rt] = val;//设置懒惰标记,表示该段下面所有结点共享的val值tree_sum[rt] = val * (r-l+1);return ;}//不在目标区间之内,继续向下找Down(rt, r-l+1);//倘若该点拥有懒惰标记,下放懒惰标记并且更新儿子,不然的话正确数据会被覆盖int m = (l+r) >> 1;if(A <= m)update(lson);if(B > m)update(rson);Up(rt);//向上更新是必须的}int main() {int cas;scanf("%d", &cas);for(int i = 1; i <= cas; i++) {int n, op;scanf("%d%d", &n, &op);build(1, n, 1);while(op--) {scanf("%d%d%d", &A, &B, &val);update(1, n, 1);}printf("Case %d: The total value of the hook is %d.\n", i, tree_sum[1]);}return 0;}


0 0
原创粉丝点击