Just a Hook ,線段樹,區間更新 ,區間求和
阿新 • • 發佈:2018-12-30
Just a Hook
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 35004 Accepted Submission(s): 17109
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.
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.
Sample Input 1 10 2 1 5 2 5 9 3 Sample Output Case 1: The total value of the hook is 24. Source Recommend wangye 線段樹區間更新 #include <iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
typedef long long ll;
const int N=1000005;
struct node
{
ll x,mark;
} t[N<<2];
void creat(int i,int l,int r)
{
t[i].mark=0;
t[i].x=1;
if(l==r) return;
int mid=(l+r)>>1;
creat(i<<1,l,mid);
creat(i<<1|1,mid+1,r);
t[i].x=t[i<<1].x+t[i<<1|1].x;
}
void pushdown(int i,int m)
{
if(t[i].mark)
{
t[i<<1].mark=t[i<<1|1].mark=t[i].mark;
t[i<<1].x=(m-(m>>1))*t[i].mark;
t[i<<1|1].x=(m>>1)*t[i].mark;
t[i].mark=0;
}
}
void update(int i,int l,int r,int x,int y, int z)
{
if(r<x||y<l) return ;
if(x<=l&&r<=y)
{
t[i].mark=z;
t[i].x=z*(r-l+1);
return ;
}
pushdown(i,r-l+1);
int mid=(l+r)>>1;
if(l<=mid)
update(i<<1,l,mid,x,y,z);
if(r>mid)
update(i<<1|1,mid+1,r,x,y,z);
t[i].x=t[i<<1].x+t[i<<1|1].x;
}
int q,n,m;
int main()
{
int k=1;
scanf("%d",&q);
while(q--)
{
scanf("%d%d",&n,&m);
creat(1,1,n);
while(m--)
{
int a,b,c;
ll d;
scanf("%d%d%d",&a,&b,&c);
update(1,1,n,a,b,c);
}
printf("Case %d: The total value of the hook is %d.\n",k++,t[1].x);
}
return 0;
}
人一我百!人十我萬!永不放棄~~~懷著自信的心,去追逐夢想。