hdu 1698 Just a Hook
来源:互联网 发布:js让多个功能延迟执行 编辑:程序博客网 时间:2024/06/02 07:30
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.
【分析】
线段树
区间打懒标记,注意初始化的memset
【代码】
#include<iostream>#include<cstring>#include<algorithm>#include<cstdio> #define ll long long#define fo(i,j,k) for(i=j;i<=k;i++)using namespace std;const int mxn=100005;struct node {int l,r,sum,col;} t[mxn*4];int T,n,m,L,R,w;inline void build(int num,int l,int r){ t[num].l=l,t[num].r=r; t[num].col=0; if(l==r) { t[num].sum=1; return; } int mid=(l+r)>>1; build(num<<1,l,mid); build((num<<1)+1,mid+1,r); t[num].sum=t[num<<1].sum+t[(num<<1)+1].sum;}inline void pushdown(int num){ if(t[num].col) { t[num<<1].col=t[(num<<1)+1].col=t[num].col; t[num<<1].sum=(t[num<<1].r-t[num<<1].l+1)*t[num].col; t[(num<<1)+1].sum=(t[(num<<1)+1].r-t[(num<<1)+1].l+1)*t[num].col; t[num].col=0; }}inline void change(int num){ if(L<=t[num].l && t[num].r<=R) { t[num].col=w; t[num].sum=(t[num].r-t[num].l+1)*w; return; } pushdown(num); int mid=(t[num].l+t[num].r)>>1; if(L<=mid) change(num<<1); if(mid<R) change((num<<1)+1); t[num].sum=t[num<<1].sum+t[(num<<1)+1].sum;}int main(){ int i,j,k; scanf("%d",&T); fo(k,1,T) { scanf("%d%d",&n,&m); build(1,1,n); while(m--) { scanf("%d%d%d",&L,&R,&w); change(1); } printf("Case %d: The total value of the hook is %d.\n",k,t[1].sum); } return 0;}
- hdu 1698 Just a Hook
- hdu 1698 Just a Hook
- Hdu 1698 Just a Hook
- hdu 1698 Just a Hook
- hdu 1698 just a hook
- hdu 1698 Just a Hook
- HDU 1698 Just a Hook
- hdu 1698 Just a Hook
- hdu 1698 Just a Hook
- hdu 1698 Just a Hook
- hdu 1698 Just a Hook
- HDU-1698-Just a Hook
- hdu 1698 Just a Hook
- hdu 1698 Just a Hook
- HDU 1698 Just a Hook
- HDU 1698 - Just a Hook
- hdu 1698 Just a Hook
- HDU 1698 Just a Hook
- ThreadLocal--线程独自共享变量
- Hexo 百度提交链接
- HTTP学习计划(web的基础)--HTTP概述(1/2)
- 创建DataFrame遇到的问题
- RecyclerView专家
- hdu 1698 Just a Hook
- sap hana backup issue
- ubuntu 14.04 挂载 nfs
- 大风歌
- #424 – 使ListBox 中每一项保持一致大小(Getting Data-Bound Items in a ListBox to Be a Consistent Size)
- java ArrayList与LinkedList的普通for循环遍历
- JSON 字符串 与 java 对象的转换
- 异常笔记
- c#--获取数据库指定表的所有列名