http://acm.hdu.edu.cn/showproblem.php?pid=1698
Just a HookTime Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 54263 Accepted Submission(s): 25289 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.
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 1 10 2 1 5 2 5 9 3
Sample Output Case 1: The total value of the hook is 24.
|
线段树区间更新, 不用加速器会超时, 开了加速器不能同时用cin 和 scanf,,,,
#include<bits/stdc++.h>
using namespace std;
const int N = 100005;
struct node
{
int l, r, lazy, sum;
}t[N<<2];
void pushdown(int x)
{
if(t[x].lazy)
{
t[x<<1].sum = (t[x<<1].r - t[x<<1].l + 1) * t[x].lazy;
t[x<<1].lazy = t[x].lazy;
t[x<<1|1].sum = (t[x<<1|1].r - t[x<<1|1].l + 1) * t[x].lazy;
t[x<<1|1].lazy = t[x].lazy;
t[x].lazy = 0;
}
}
void build(int x, int l, int r)
{
t[x].l = l, t[x].r = r, t[x].lazy = 0;
if(t[x].l == t[x].r)
{
t[x].sum = 1;
return ;
}
int mid = (t[x].l + t[x].r) >> 1;
build(x<<1, l, mid);
build(x<<1|1, mid+1, r);
t[x].sum = t[x<<1].sum + t[x<<1|1].sum;
}
void update(int x, int l, int r, int k)
{
if(l <= t[x].l && t[x].r <= r)
{
t[x].sum = (t[x].r - t[x].l + 1) * k;
t[x].lazy = k;
return ;
}
pushdown(x);
int mid = (t[x].l + t[x].r) >> 1;
if(l <= mid) update(x<<1, l, r, k);
if(r > mid) update(x<<1|1, l, r, k);
t[x].sum = t[x<<1].sum + t[x<<1|1].sum;
}
int main()
{
ios::sync_with_stdio(false);
int T;
cin >> T;
for(int tt = 1; tt <= T; tt++)
{
int n, m;
cin >> n >> m;
build(1, 1, n);
while(m--)
{
int l, r, k;
cin >> l >> r >> k;
update(1, l, r, k);
}
printf("Case %d: The total value of the hook is %d.\n", tt, t[1].sum);
}
return 0;
}