线段树区间更新 Just a Hook
来源:互联网 发布:一建试题软件 编辑:程序博客网 时间:2024/05/22 06:47
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.
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.
11021 5 25 9 3
Case 1: The total value of the hook is 24.
最基础的区间更新题
#include <iostream>#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=100005;
int sum[maxn*4];
struct node
{
int l,r,val,mark;
}tree[maxn*4];
void build(int i,int l,int r)
{
tree[i].mark=0;
tree[i].val=1;
tree[i].l=l,tree[i].r=r;
//cout<<l<<" "<<r<<endl;
if(l==r)return;
int m=(l+r)/2;
build(i*2,l,m);
build(i*2+1,m+1,r);
}
void pushdown(int i,int v)
{
if(tree[i].mark!=0)
{
tree[i*2].mark=tree[i].mark;
tree[i*2+1].mark=tree[i].mark;
tree[i*2+1].val=tree[i].mark*(v/2); //区间中的个数
tree[i*2].val=tree[i].mark*(v-v/2);
tree[i].mark=0;
}
}
void update(int i,int l,int r,int add)
{
if(tree[i].l==l&&r==tree[i].r) //要更新的区间比i所含的区间大 全部包含
{
tree[i].mark=add;
tree[i].val=add*(tree[i].r-tree[i].l+1);
//cout<<tree[i].l<<" "<<tree[i].r<<" "<<tree[i].mark<<" "<<tree[i].val<<endl;
return;
}
pushdown(i,tree[i].r-tree[i].l+1);
//cout<<tree[i].l<<" "<<tree[i].r<<" "<<tree[i].mark<<" "<<tree[i].val<<endl;
int m=(tree[i].l+tree[i].r)/2;
if(r<=m)update(i*2,l,r,add);
else if(l>m)update(i*2+1,l,r,add);
else
{
update(i*2,l,m,add);
update(i*2+1,m+1,r,add);
}
tree[i].val=tree[i*2].val+tree[i*2+1].val;
}
/*int query(int i,int l,int r)
{
cout<<tree[i].l<<" "<<tree[i].r<<" "<<tree[i].mark<<" "<<tree[i].val<<endl;
if(tree[i].l>=l&&tree[i].r<=r)
return tree[i].val;
pushdown(i,tree[i].r-tree[i].l+1);
int ans=0;
int m=(tree[i].l+tree[i].r)/2;
if(l<=m)ans+=query(i*2,l,r);
if(r>m)ans+=query(i*2+1,l,r);
return ans;
}*/
int main()
{
int t;
cin>>t;
for(int i=1;i<=t;i++)
{
int n;
cin>>n;
build(1,1,n);
int q;
cin>>q;
int x,y,z;
while(q--)
{
scanf("%d%d%d",&x,&y,&z);
update(1,x,y,z);
}
//for(int i=1;i<100;i++)
//cout<<tree[i].l<<" "<<tree[i].r<<" "<<tree[i].val<<endl;
printf("Case %d: The total value of the hook is %d.\n",i,tree[1].val);
}
return 0;
}
阅读全文
0 0
- Just a Hook(线段树区间更新)
- 线段树区间更新 Just a Hook
- Just a Hook ---线段树区间更新
- Just a Hook ,线段树,区间更新 ,区间求和
- HDU 1698 Just a Hook 线段树区间更新
- HDU1698--Just a hook--线段树区间更新
- 线段树(区间更新) hdu-1698-Just a Hook
- hdu 1698(区间更新线段树)Just a Hook
- 线段树 1698 Just a Hook 区间set更新
- hdu 1698 Just a Hook(线段树-区间更新)
- HDU1698:Just a Hook(线段树区间更新)
- 1698 Just a Hook 线段树区间更新
- hdu 1698 Just a Hook 线段树区间更新
- Just a Hook +线段树区间更新模板题
- hdoj 1698 Just a Hook 【线段树 区间更新】
- HDU 1698 Just a Hook (线段树区间更新)
- HDU-1698-Just a Hook(线段树 区间更新)
- HDU 1698 Just a Hook (线段树区间更新)
- SpringMVC流程架构图
- Android实现二级联动
- 最简单最全的RabbitMQ安装教程
- eclipse使用gitHub
- 《JavaScript DOM编程艺术》第十章 用javascript实现动画效果
- 线段树区间更新 Just a Hook
- 【脚本语言系列】关于Python持久化dbhash,你需要知道的事
- ssh登录
- 正则使用注意事项
- Go游戏服务器开发的一些思考(二):综合考察(上)
- PHP PDO::execute()执行insert into失败返回错误无错误消息的解决
- String、StringBuffer与StringBuilder之间区别
- CRC32
- IE11的一些小问题