Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 42082 Accepted Submission(s): 20247
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
1
10
2
1 5 2
5 9 3
Sample Output
Case 1: The total value of the hook is 24.
解析:基本的线段树更改区间查询。套用模板(区间更改确实是比较难理解,加了个懒惰标记,按照我的理解,是先标记该树节点已经更新了,但是它的左右子节点还是没更新,这里就是有懒惰的意思了————因为我们现在用到当前的区间,往下的区间用不到,所以我们只是标记到这里就可以,它的子节点我们还没用到,我们就暂且标记到这里也更新到这里,然后接下来,要是会用到它的子节点,我们就往下推,把它的子节点更新了,当前节点就标记0,表示已经更新过。所以叫懒惰:(个人理解:就是懒得把全部的节点更新),注意,query的时候也要pushDown,因为有可能会用到当前已经更新的节点的子节点,否则会影响结果)
#include
using namespace std;
#define e exp(1)
#define pi acos(-1)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define ll long long
#define ull unsigned long long
#define mem(a,b) memset(a,b,sizeof(a))
int gcd(int a,int b){return b?gcd(b,a%b):a;}
const int maxn=100000+10;
int n,q;
struct node{
int l,r;
ll sum,lazy;
}t[maxn<<2];
void pushUp(int m)
{
t[m].sum=t[m<<1].sum+t[m<<1|1].sum;
}
void pushDown(int m,int ln,int rn)
{
if(t[m].lazy)
{
t[m<<1].lazy=t[m].lazy;
t[m<<1|1].lazy=t[m].lazy;
t[m<<1].sum=t[m].lazy*ln;
t[m<<1|1].sum=t[m].lazy*rn;
t[m].lazy=0;
}
}
void bulid(int l,int r,int m)
{
t[m].l=l;
t[m].r=r;
if(l==r)
{
t[m].sum=1;
return ;
}
int mid=l+r>>1;
bulid(l,mid,m<<1);
bulid(mid+1,r,m<<1|1);
pushUp(m);
}
void update(int L,int R,int v,int l,int r,int m)
{
if(L<=l&&r<=R)
{
t[m].sum=(ll)v*(r-l+1);
t[m].lazy=v;
return ;
}
int mid=l+r>>1;
pushDown(m,mid-l+1,r-mid);
if(L<=mid)update(L,R,v,l,mid,m<<1);
if(R>mid)update(L,R,v,mid+1,r,m<<1|1);
pushUp(m);
}
ll query(int L,int R,int l,int r,int m)
{
if(L<=l&&r<=R)
{
return t[m].sum;
}
int mid=l+r>>1;
pushDown(m,mid-l+1,r-mid);
ll ans=0;
if(L<=mid)ans+=query(L,R,l,mid,m<<1);
if(R>mid)ans+=query(L,R,mid+1,r,m<<1|1);
return ans;
}
int main()
{
int T;scanf("%d",&T);
int cas=1;
while(T--)
{
scanf("%d",&n);
for(int i=0; i