E-just a Hook
Time limit:2000ms Memory limit:32768kb 64bit IO format:%i64d &%i64u
Submit Status Practice
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 to several consecutive metallic sticks which is of the same length.
Now Pudge wants to do some operations on the hook.
Let us number the consecutive metallic sticks of the hooks from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver s Ticks 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 of the 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.
Consider the original hook is a 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 is no more than cases.
For each case, the first line contains a integer N, 1<=n<=100,000, which is the number of the sticks of Pudge ' s MEA T Hook and the second line contains a 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:c Hange the sticks numbered from X to Y into the metal kind Z, where z=1 represents the Cupreous kind, z=2 represents the SI Lver Kind and z=3 represents the golden kind.
Output
For each case, print a number with 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.
Similar to the delay mark the same idea
#include <bits/stdc++.h> #include <cstdio> #include <algorithm>using namespace Std;const int maxn= 100011;const int inf=999999999; #define Lson (rt<<1), L,m#define Rson (rt<<1|1), M+1,r#define M ((L+R) > >1) #define for (i,n) for (int i=0;i< (n); i++) Template<class t>inline T Read (t&x) {char C; while ((C=getchar ()) <=32); BOOL Ok=false; if (c== '-') Ok=true,c=getchar (); for (x=0; c>32; C=getchar ()) x=x*10+c-' 0 '; if (OK) x=-x; return x;} Template<class t> inline void Read_ (t&x,t&y) {read (x); Read (y);} Template<class t> inline void Write (T x) {if (x<0) Putchar ('-'), x=-x; if (x<10) putchar (x+ ' 0 '); else write (X/10), Putchar (x%10+ ' 0 ');} Template<class t>inline void Writeln (T x) {write (x); Putchar (' \ n ');} -------IO template------typedef long LONG ll;struct node{int sum; int Val;} p[maxn<<3];void Build (int rt,int l,int R) {p[rt].val=0; if (l==r) {p[rt].sum=1; p[rt].val=1; Return } build (Lson); Build (Rson); P[rt].sum=p[rt<<1].sum+p[rt<<1|1].sum;} void update (int rt,int l,int r,int x,int y,int z) {//printf ("%d%d%d%d%d\n", rt,l,r,x,y,z); if (x==l&&y==r) {p[rt].sum= (r-l+1) *z; P[rt].val=z; return; } if (p[rt].val>0)//down update, the judgment condition is written well, {p[rt<<1].sum= (m-l+1) *p[rt].val; P[rt<<1|1].sum= (r-m) *p[rt].val;//right son interval is r-m namely R (m+1) +1 p[rt<<1].val=p[rt<<1|1].val=p[rt].val; p[rt].val=0; } if (y<=m) update (LSON,X,Y,Z); else if (x>m) update (RSON,X,Y,Z); else {update (LSON,X,M,Z); Update (RSON,M+1,Y,Z); } P[rt].sum=p[rt<<1].sum+p[rt<<1|1].sum;} int main () {//#ifndef Online_judge//Freopen ("In.txt", "R", stdin);//#endif//Online_judge int T; int n,m,i,j,k,t; Read (T); int Cas=1; while (t--) {read_ (n,m); Build (1,1,n); int x,Y,z; while (m--) {read_ (x, y); Read (z); Update (1,1,N,X,Y,Z); } printf ("Case%d:the total value of the hook is%d.\n", cas++,p[1].sum); } return 0;}
E-just a Hook HDU 1698 (segment tree + similar delay tag)