Just a Hook
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 24102 Accepted Submission(s): 12063
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.
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.
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
const int Max=+;
int segTree[Max<<];
int change[Max<<];
int T,q,r,l,z,n;
int PushUp(int node) //回溯,节点更新
{
segTree[node]=(segTree[node<<]+segTree[node<<|]);
return ;
}
int PushDown(int node,int arange)
{
if(change[node]){
change[node<<]=change[node<<|]=change[node];
segTree[node<<]=change[node]*(arange-(arange>>));
segTree[node<<|]=change[node]*(arange>>);
change[node]=;
}
return ;
}
void build(int node,int begin,int end)
{
segTree[node]=;
change[node]=;
if(begin==end)
return;
int m=(begin+end)>>;
build(node<<,begin,m);
build(node<<|,m+,end);
PushUp(node);
return;
}
int updata(int node,int begin,int end)
{
if(l<=begin&&end<=r)
{
segTree[node]=z*(end-begin+);
change[node]=z;
return ;
}
PushDown(node,end-begin+);
int mid=(begin+end)>>;
if(mid>=l) updata(node<<,begin,mid);
if(mid<r) updata(node<<|,mid+,end);
PushUp(node);
}
int main()
{
int i,j;
freopen("in.txt","r",stdin);
cin>>T;
int k=T;
while(T--)
{
scanf("%d%d",&n,&q);
build(,,n-);
for(i=;i<q;i++)
{
scanf("%d%d%d",&l,&r,&z);
l--,r--;
updata(,,n-);
}
printf("Case %d: The total value of the hook is %d.\n",k-T,segTree[]);
}
}
更简单的代码:
#include <cstdio>
using namespace std;
#define maxN 100005
#define m ((L+R)>>1)
int T, N, Q, X, Y, Z, i;
int val[ * maxN];
void push_down(int o){
val[o * ] = val[o * + ] = val[o];
val[o] = ;
}
void update(int o, int L, int R){
if (X <= L&&R <= Y) val[o] = Z;
else{
if (val[o]) push_down(o);
if (X <= m) update(o * , L, m);
if (Y > m) update(o * + , m + , R);
}
}
int sum(int o,int L,int R){
if (val[o]) return (R-L+)*val[o];
return sum(o * , L, m) + sum(o * + , m + , R);
}
int main(){
scanf("%d", &T);
for (int Case = ; Case <= T; Case++){
val[] = ;
scanf("%d%d", &N, &Q);
for (i = ; i < Q; i++){
scanf("%d%d%d", &X, &Y, &Z);
update(, , N);
}
printf("Case %d: The total value of the hook is %d.\n",Case,sum(,,N));
}
return ;
}