Just a Hook
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 25630 Accepted Submission(s): 12798
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
Sample Output
Case 1: The total value of the hook is 24.
题目大意:
屠夫的钩子,长度为n,刚开始都是铜的,后来可以变为铜,银,金三种,然后是以最后一种为主。
思路:
很明显是区间的修改,然后就只要用sett来表示一下,当他是-1的时候就表示没有修改过,0表示这个点往下面传过值,然后2和3就是表示银和金了。(虽然没看别人的题解,自己按照自己的想法敲了出来,但是感觉和书上的还是有点区别,为什么书里面update函数中还有那么多的维护,感觉应该是用来维护sum,min,max的吧,如果只是单纯的修改可能就不需要维护了吧)
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n, q; const int maxn = 100000 + 5; int sett[maxn << 2]; int ans, cnt; void init(){ scanf("%d%d", &n, &q); fill(sett, sett + maxn * 4, -1); ans = 0; } void pushdown(int o){ if (sett[o] == 0) return ; int l = o << 1, r = o << 1 | 1; sett[l] = sett[r] = sett[o]; sett[o] = 0; } void update(int o, int ql, int qr, int l, int r, int val){ if (ql <= l && qr >= r){ sett[o] = val; return ; } else { pushdown(o); int mid = l + (r - l) / 2; if (ql <= mid) update(o << 1, ql, qr, l, mid, val); //else maintain(o << 1, l, mid); if (qr > mid) update(o << 1 | 1, ql, qr, mid + 1, r, val); //else maintain(o << 1 | 1, mid + 1, r); } //maintain(o, l, r); } void query(int o, int l, int r){ if (sett[o] == -1 || sett[o] == 1){ ans += r - l + 1; // printf("%d\n", ans); return ; } else if (sett[o] == 2){ ans += sett[o] * (r - l + 1); // printf("%d\n", ans); return ; } else if (sett[o] == 3){ ans += sett[o] * (r - l + 1); // printf("%d\n", ans); return ; } int mid = l + (r - l) / 2; if (l <= mid){ query(o << 1, l, mid); } if (r > mid){ query(o << 1 | 1, mid + 1, r); } } void solve(){ int ty; while (q--){ int ql, qr; scanf("%d%d%d", &ql, &qr, &ty); update(1, ql, qr, 1, n, ty); } /*for (int i = 1; i <= 25; i++){ printf("o = %d sett[o] = %d\n", i, sett[i]); }*/ query(1, 1, n); printf("Case %d: The total value of the hook is %d.\n", ++cnt, ans); } int main(){ int t; scanf("%d", &t); while (t--){ init(); solve(); } return 0; } </algorithm></cstring></cstdio>