Just a Hook

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 24474    Accepted Submission(s): 12194

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.
 
Source
 
线段树 区间更新 区间求和
理解这个博客
要多刷
http://www.douban.com/note/273509745/
4.14重刷 更新模板  注意 pushdown的使用 有些题目 懒散标记不能堆积,得向下更新
 
 #include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<stack>
#define ll __int64
#define maxn 100000
using namespace std;
struct node
{
int l,r;
int add;
int sum;
}tree[*maxn];
int t;
int x,y,z;
int n,q;
int ans=;
void buildtree(int root,int left,int right)
{
tree[root].l=left;
tree[root].r=right;
tree[root].add=;
if(left==right)
{
tree[root].sum=;
return ;
}
int mid=(left+right)>>;
buildtree(root<<,left,mid);
buildtree(root<<|,mid+,right);
tree[root].sum=tree[root<<].sum+tree[root<<|].sum;
}
void pushdown(int root,int m)
{
tree[root<<].add=tree[root].add;
tree[root<<|].add=tree[root].add;
tree[root<<].sum=tree[root].add*(m-(m>>));
tree[root<<|].sum=tree[root].add*(m>>);
tree[root].add=;
}
void updata(int root,int left,int right,int c)
{
if(tree[root].l==left&&tree[root].r==right)
{
tree[root].add=c;
tree[root].sum=c*(right-left+);
return ;
}
if(tree[root].l==tree[root].r)
return ;
if(tree[root].add)
pushdown(root,tree[root].r-tree[root].l+);
int mid=(tree[root].l+tree[root].r)>>;
if(right<=mid)
updata(root<<,left,right,c);
else
{
if(left>mid)
updata(root<<|,left,right,c);
else
{
updata(root<<,left,mid,c);
updata(root<<|,mid+,right,c);
}
}
tree[root].sum=tree[root<<].sum+tree[root<<|].sum;
}
int query(int root,int left,int right)
{
if(tree[root].l==left&&tree[root].r==right)
{
return tree[root].sum;
}
if(tree[root].add)
pushdown(root,tree[root].r-tree[root].l+);
int mid=(tree[root].l+tree[root].r)>>;
if(right<=mid)
ans+=query(root<<,left,right);
else
{
if(left>mid)
ans+=query(root<<|,left,right);
else
{
ans+=query(root<<,left,mid);
ans+=query(root<<|,mid+,right);
}
}
return ans;
}
int main()
{
while(scanf("%d",&t)!=EOF)
{
for(int i=;i<=t;i++)
{
scanf("%d",&n);
buildtree(,,n);
scanf("%d",&q);
for(int j=;j<=q;j++)
{
scanf("%d %d %d",&x,&y,&z);
updata(,x,y,z);
}
ans=;
printf("Case %d: The total value of the hook is %d.\n",i,query(,,n));
}
} return ;
}

旧板子

 
#include<bits/stdc++.h>
using namespace std;
#define maxn 100005
struct ss
{
int l,r;
int sum;
int tag;
}tr[4*maxn];
void build (int k,int s,int t)
{
tr[k].l=s;
tr[k].r=t;
tr[k].tag=0;
if(s==t)
{
tr[k].sum=1;
return ;
}
int mid=(s+t)>>1;
build(k<<1,s,mid);
build(k<<1|1,mid+1,t);
tr[k].sum=tr[k<<1].sum+tr[k<<1|1].sum;
//cout<<tr[k].sum<<endl;
}
void pushdown(int k,int m)//向下更新节点
{
tr[k<<1].tag=tr[k].tag;
tr[k<<1|1].tag=tr[k].tag;
tr[k<<1].sum=tr[k].tag*(m-(m>>1));
tr[k<<1|1].sum=tr[k].tag*(m>>1);
tr[k].tag=0;
}
void update (int k,int s,int t,int exm)
{
if(tr[k].l==s&&tr[k].r==t)
{
tr[k].tag=exm;
tr[k].sum=exm*(t-s+1);
return ;
}
if(tr[k].l==tr[k].r)
return ;
if(tr[k].tag)
pushdown(k,tr[k].r-tr[k].l+1);
int mid=(tr[k].l+tr[k].r)>>1;
if(t<=mid)
update(k<<1,s,t,exm);
else if(s>mid)
update(k<<1|1,s,t,exm);
else
{
update(k<<1,s,mid,exm);
update(k<<1|1,mid+1,t,exm);
}
tr[k].sum=tr[k<<1].sum+tr[k<<1|1].sum;
}
int ask(int k,int s,int t)
{
if(tr[k].l==s&&tr[k].r==t)
{
return tr[k].sum;
}
if(tr[k].tag) pushdown(k,tr[k].r-tr[k].l+1);
int mid=(tr[k].l+tr[k].r)>>1;
int ans=0;
if(t<=mid)
ans+= ask(k<<1,s,t);
else
if(s>mid)
ans+=ask(k<<1|1,s,t);
else
{
ans+=ask(k<<1,s,mid);
ans+=ask(k<<1|1,mid+1,t);
}
return ans;
}
int t;
int n;
int m;
int qq,ww,ee;
//int a[maxn];
int main()
{
while(scanf("%d",&t)!=EOF)
{
for(int i=1;i<=t;i++)
{ scanf("%d",&n);
build(1,1,n);
scanf("%d",&m);
for(int j=1;j<=m;j++)
{
scanf("%d%d%d",&qq,&ww,&ee);
update(1,qq,ww,ee);
}
printf("Case %d: The total value of the hook is %d.\n",i,ask(1,1,n));
}
}
return 0;
}

  

hdu 1698 线段树 区间更新 区间求和的更多相关文章

  1. hdu 1166线段树 单点更新 区间求和

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  2. HDU 3308 线段树单点更新+区间查找最长连续子序列

    LCIS                                                              Time Limit: 6000/2000 MS (Java/Oth ...

  3. HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)

    HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...

  4. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  5. POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)

    POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...

  6. hdu 4267 线段树间隔更新

    A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  7. hdu1166(线段树单点更新&区间求和模板)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 题意:中文题诶- 思路:线段树单点更新,区间求和模板 代码: #include <iost ...

  8. hdu2795(线段树单点更新&区间最值)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 题意:有一个 h * w 的板子,要在上面贴 n 条 1 * x 的广告,在贴第 i 条广告时要 ...

  9. J - Super Mario HDU - 4417 线段树 离线处理 区间排序

    J - Super Mario HDU - 4417 这个题目我开始直接暴力,然后就超时了,不知道该怎么做,直接看了题解,这个习惯其实不太好. 不过网上的思路真的很厉害,看完之后有点伤心,感觉自己应该 ...

随机推荐

  1. vim python自动补全插件:pydiction

    vim python自动补全插件:pydiction 可以实现下面python代码的自动补全: 1.简单python关键词补全 2.python 函数补全带括号 3.python 模块补全 4.pyt ...

  2. NOIP2012 普及组真题 4.13校模拟

    考试状态: 我今天抽签看了洛谷的… 这我能怂???凶中带吉,我怕考试??我!不!怕! 看着整个机房的男同学们,我明白我是不会触发我的忌了.很好,开刷. A. [NOIP2012普及组真题] 质因数分解 ...

  3. tomcat下载、安装

    下载 官网地址:https://tomcat.apache.org/download-80.cgi 安装 直接安装即可.安装完毕后Tomcat的目录结构如下: bin:脚本目录 ​ 启动脚本:star ...

  4. LogisticRegression Algorithm——机器学习(西瓜书)读书笔记

    import numpy as np from sklearn.datasets import load_breast_cancer import sklearn.linear_model from ...

  5. 实战小项目之ffmpeg推流yolo视频实时检测

    之前实现了yolo图像的在线检测,这次主要完成远程视频的检测.主要包括推流--収流--检测显示三大部分 首先说一下推流,主要使用ffmpeg命令进行本地摄像头的推流,为了实现首屏秒开使用-g设置gop ...

  6. CDH问题集

    1.在CM中添加主机报JDK错误 手动在机器上安装oracle-jdk1.7+update64.然后在CM中选择不安装oracle-jdk即可. 2.HostMoinitor无法与server联系 查 ...

  7. HDU 2492 Ping pong(数学+树状数组)(2008 Asia Regional Beijing)

    Description N(3<=N<=20000) ping pong players live along a west-east street(consider the street ...

  8. Python-期末练习

    1.骑车与走路:我们的校园很大很大很大大大大大……,骑个自行车去办事会很快,比如取个快递了,到其他宿舍楼找个同(nv)学(you)了.但实际上,并非去办任何事情都是骑车快,因为骑车总要找车.开锁.停车 ...

  9. 基于TensorFlow解决手写数字识别的Softmax方法、多层卷积网络方法和前馈神经网络方法

    一.基于TensorFlow的softmax回归模型解决手写字母识别问题 详细步骤如下: 1.加载MNIST数据: input_data.read_data_sets('MNIST_data',one ...

  10. python 项目配置虚拟环境

    # Windows 环境1, 安装 Visual C++ 2015 Build Tools, 依赖.Net Framework 4.6, 安装包位置 ./tools/windows/visualcpp ...