HDU1698 Just a Hook 线段树成段替换
2017-02-05 19:32
71 查看
传送门:HDU1698
题意:给定n和q个操作,每个操作为:i j k 令num[t]=k(i<=t<=j),初始化num[t]=1(1<=t<=n)
思路:数据量较大,暴力肯定不行。考虑用线段树进行区间替换,这里询问只有一次,且是询问总区间,所以可以直接输出一号节点的信息。
代码:#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<map>
#define ll long long
#define pi acos(-1)
#define inf 0x3f3f3f3f
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std;
typedef pair<int,int>P;
const int MAXN=100010;
struct node{
int key,lazy;//lazy为延时标记
}num[MAXN<<2];
void pushup(int rt)
{
num[rt].key=num[rt<<1].key+num[rt<<1|1].key;
}
void pushdown(int rt,int k)
{
if(num[rt].lazy)
{
num[rt<<1].lazy=num[rt].lazy;
num[rt<<1|1].lazy=num[rt].lazy;
num[rt<<1].key=num[rt].lazy*(k-(k>>1));
num[rt<<1|1].key=num[rt].lazy*(k>>1);
num[rt].lazy=0;
}
}
void build(int l,int r,int rt)
{
num[rt].lazy=0;
if(l==r)
{
num[rt].key=1;
return ;
}
int mid=(l+r)>>1;
build(lson);
build(rson);
pushup(rt);
}
void update(int L,int R,int x,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
num[rt].lazy=x;
num[rt].key=x*(r-l+1);
return ;
}
pushdown(rt,r-l+1);
int mid=(l+r)>>1;
if(L<=mid)update(L,R,x,lson);
if(R>mid)update(L,R,x,rson);
pushup(rt);
}
int main()
{
int t,n,a,b,c,q;
scanf("%d",&t);
for(int Case=1;Case<=t;Case++)
{
scanf("%d%d",&n,&q);
build(1,n,1);
while(q--)
{
scanf("%d%d%d",&a,&b,&c);
update(a,b,c,1,n,1);
}
printf("Case %d: The total value of the hook is %d.\n",Case,num[1].key);
}
return 0;
}
题意:给定n和q个操作,每个操作为:i j k 令num[t]=k(i<=t<=j),初始化num[t]=1(1<=t<=n)
思路:数据量较大,暴力肯定不行。考虑用线段树进行区间替换,这里询问只有一次,且是询问总区间,所以可以直接输出一号节点的信息。
代码:#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<map>
#define ll long long
#define pi acos(-1)
#define inf 0x3f3f3f3f
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std;
typedef pair<int,int>P;
const int MAXN=100010;
struct node{
int key,lazy;//lazy为延时标记
}num[MAXN<<2];
void pushup(int rt)
{
num[rt].key=num[rt<<1].key+num[rt<<1|1].key;
}
void pushdown(int rt,int k)
{
if(num[rt].lazy)
{
num[rt<<1].lazy=num[rt].lazy;
num[rt<<1|1].lazy=num[rt].lazy;
num[rt<<1].key=num[rt].lazy*(k-(k>>1));
num[rt<<1|1].key=num[rt].lazy*(k>>1);
num[rt].lazy=0;
}
}
void build(int l,int r,int rt)
{
num[rt].lazy=0;
if(l==r)
{
num[rt].key=1;
return ;
}
int mid=(l+r)>>1;
build(lson);
build(rson);
pushup(rt);
}
void update(int L,int R,int x,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
num[rt].lazy=x;
num[rt].key=x*(r-l+1);
return ;
}
pushdown(rt,r-l+1);
int mid=(l+r)>>1;
if(L<=mid)update(L,R,x,lson);
if(R>mid)update(L,R,x,rson);
pushup(rt);
}
int main()
{
int t,n,a,b,c,q;
scanf("%d",&t);
for(int Case=1;Case<=t;Case++)
{
scanf("%d%d",&n,&q);
build(1,n,1);
while(q--)
{
scanf("%d%d%d",&a,&b,&c);
update(a,b,c,1,n,1);
}
printf("Case %d: The total value of the hook is %d.\n",Case,num[1].key);
}
return 0;
}
相关文章推荐
- hdu1698(线段树区间替换模板)
- HDU1698 Just a Hook(线段树成段替换)
- hdu1698 Just a Hook 线段树成段替换
- HDU1698 Just a Hook(线段树成段替换、区间求和,延迟标记的应用)
- HDU1698 - Just a Hook(成段替换+懒惰标记)
- 水晶报表中用公式替换交叉表列名
- Python代码热替换
- Java关于Properties用法(二)——替换配置文件中的参数
- App logo图标或内部icon的动态替换
- 使用查找替换批量处理手工编号
- [原]C语言字符串替换函数(strrpl)
- JavaScript DOM编程 学习笔记-替换节点
- Oracle 操作字符串:拼接、替换、截取、查找
- vi 替换命令
- 一句替换bbcode
- js替换指定字符串
- 【github notepad】QT 自动备份,(正则表达式)搜索替换
- 第六章 全局替换 附加
- emlog文章内容批量替换
- hdu1698 Just a Hook(线段树区间修改)