1013ms G++代码
#include
#include
#include
#include
#include
#include
using namespace std;
const int NUM = 100010;
typedef struct node
{
int left,mid,right,key;
}node;
node T[ NUM<<2 ];
int flag[ NUM<<2 ];
int a[NUM];
void create(int u,int l,int r)
{
T[u].left = l;
T[u].right = r;
T[u].mid = (l+r)>>1;
flag[ u ] = 0;
if(l == r)
{
T[u].key = 1;
return;
}
int mid = (l+r)>>1;
create(u+u,l,mid);
create(u+u+1,mid+1,r);
T[u].key = T[u+u].key+T[u+u+1].key;
}
void PushDown(int u,int m)//把当前结点的信息更新给儿子结点
{
if (flag[u] !=0 )
{
flag[u+u] = flag[u];
flag[u+u+1] = flag[u];
T[u+u].key = flag[u] * (m - (m >> 1));
T[u+u+1].key = flag[u] * (m >> 1);
flag[u] = 0;
}
}
void change(int from,int to,int newKey ,int i)//延迟更新
{
if(from <= T[i].left && to >= T[i].right)
{
T[i].key = newKey*(T[i].right - T[i].left +1);
flag[i] = newKey;
return ;
}
PushDown(i,T[i].right - T[i].left + 1);
if(from <= T[i].mid)
change( from , to ,newKey, i+i );
if(to > T[i].mid)
change( from , to ,newKey, i+i+1 );
T[i].key = T[i+i].key + T[i+i+1].key;
}
int main()
{
int n,t,q,x,y,ca=1,nn,z;
while(scanf("%d",&t) !=EOF)
{
while(t--)
{
scanf("%d",&n);
scanf("%d",&q);
create(1,1,n);
for(int i = 1; i <= q; i++)
{
scanf("%d %d %d",&x,&y,&z);
change(x,y,z,1);
}
printf("Case %d: The total value of the hook is %d.\n",ca++,T[1].key);
}
}
return 0;
}