首页 > 其他分享 >hdu 1698(线段树区间更新)

hdu 1698(线段树区间更新)

时间:2023-05-29 19:34:36浏览次数:37  
标签:lazy int 线段 tree hdu mid ans sum 1698


解题思路:线段树区间更新水题。


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

const int maxn = 100005;
struct seg
{
	int l,r,sum,lazy;
}tree[maxn<<2];

void build(int l,int r,int u)
{
	tree[u].l = l;
	tree[u].r = r;
	tree[u].lazy = 0;
	tree[u].sum = 0;
	if(l == r) return;
	int mid = (l + r) >> 1;
	build(l,mid,2*u);
	build(mid+1,r,2*u+1);
}

void PushDown(int u)
{
	tree[2*u].sum = (tree[2*u].r - tree[2*u].l + 1) * tree[u].lazy;
	tree[2*u+1].sum = (tree[2*u+1].r - tree[2*u+1].l + 1) * tree[u].lazy;
	tree[2*u].lazy = tree[u].lazy;
	tree[2*u+1].lazy = tree[u].lazy;
	tree[u].lazy = 0;
}

void PushUp(int u)
{
	tree[u].sum = tree[2*u].sum + tree[2*u+1].sum;
}

void update(int l,int r,int u,int v)
{
	if(tree[u].l >= l && tree[u].r <= r){
		tree[u].sum = (tree[u].r - tree[u].l + 1) * v;
		tree[u].lazy = v;
		return;
	}
	if(tree[u].lazy) PushDown(u);
	int mid = (tree[u].l + tree[u].r) >> 1;
	if(l <= mid) update(l,r,u*2,v);
	if(r > mid) update(l,r,u*2+1,v);
	PushUp(u);
}

int query(int l,int r,int u)
{
	if(tree[u].l >= l && tree[u].r <= r){
		return tree[u].sum;
	}
	int mid = (tree[u].l + tree[u].r) >> 1;
	int ans = 0;
	if(l <= mid) ans += query(l,r,2*u);
	if(r > mid) ans += query(l,r,2*u+1);
	return ans;
}

int main()
{
	int t,cas = 1;
	scanf("%d",&t);
	while(t--){
		int n;
		scanf("%d",&n);
		build(1,n,1);
		for(int i = 1; i <= n; i++)
			update(i,i,1,1);
		int q,x,y,z;
		scanf("%d",&q);
		while(q--){
			scanf("%d%d%d",&x,&y,&z);
			update(x,y,1,z);
		}
		printf("Case %d: The total value of the hook is %d.\n",cas++,query(1,n,1));
	}
	return 0;
}



标签:lazy,int,线段,tree,hdu,mid,ans,sum,1698
From: https://blog.51cto.com/u_16143128/6373666

相关文章

  • hdu 1511(dp)
    解题思路:两次dp确实很巧妙,我只想到了一次dp算出最后那个数最小,其实题目要求还要保证第一个数尽可能大,一开始也没有注意到。。AC:#include<stdio.h>#include<string.h>#include<algorithm>#include<iostream>#include<math.h>#include<stdlib.h>#include<time.h>usingnamesp......
  • hdu 5102(队列+节点扩展)
    TheK-thDistanceTimeLimit:8000/4000MS(Java/Others)    MemoryLimit:65536/65536K(Java/Others)ProblemDescriptionGivenatree,whichhasnnodeintotal.Definethedistancebetweentwonodeuandvisthenumberofedgeontheirunique......
  • hdu 5367(线段树+区间合并)
    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5367官方题解:对于求“高山脉”长度,可以利用线段树。树节点中保存左高度连续长度,左高度连续区间的高度,左高度连续区间的状态(即是否高于第一个高度不同的山),右高度连续长度,右高度连续区间的高度,右高度连续区间的状态,每段区间内的“......
  • hdu 5201(隔板法+容斥原理)
    TheMonkeyKingTimeLimit:8000/4000MS(Java/Others)    MemoryLimit:65536/65536K(Java/Others)ProblemDescriptionnpeaches.Andtherearemmonkeys(includingGoKu),theyarenumberedfrom1tom,GoKu’snumberis1.GoKuwa......
  • hdu 5086(dp)
    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5086题目大意:给出长度为n的数组,然后要求累计里面的每个子串的和。解题思路:这道题直接枚举肯定不行,所以要找递推关系。假设:{1,2,3,4}为某一个序列,假设我们已经找到了{1,2,3},接下来把{4}加入进去;由于{1,2,3}已经有{1},{2},{3},{1,......
  • hdu 5188(限制01背包)
    zhxandcontestTimeLimit:2000/1000MS(Java/Others)    MemoryLimit:65536/65536K(Java/Others)ProblemDescriptionAsoneofthemostpowerfulbrushesintheworld,zhxusuallytakespartinallkindsofcontests.Oneday,zhxtakespar......
  • hdu 5171(矩阵快速幂)
    GTY'sbirthdaygiftTimeLimit:2000/1000MS(Java/Others)    MemoryLimit:65536/65536K(Java/Others)ProblemDescriptiona,b∈S),andadda+b Input2≤n≤100000,1≤k≤1000000000).Thesecondlinecontainsnelementsai......
  • hdu 5157(manacher+前缀和+树状数组)
    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5157解题思路:我们可以先用mancher算法对字符串进行处理,把以每个点为中心的回文串半径求出来,然后进行处理。加入对以p为中心的点,从p-r[i]+1~p都是回文串的开头,那么对于每个回文串(开头是j)只要记录结尾从1~j-1的回文串个数,我们可......
  • hdu:第K小的数(构造二分)
    ProblemDescription给定\(n\)个正整数\(a_1,a_2,\dots,a_n\)和\(m\)个正整数\(b_1,b_2,\dots,b_m\)。请在\(n\timesm\)个\(a_i+b_j(1\leqi\leqn,1\leqj\leqm)\)中,找到第\(k\)小的数(不去重)。Input第一行包含一个正整数\(T(1\leqT\leq10)\),表示测试数据的组数。每组......
  • hdu:Ice Cream Tower(构造二分)
    一座高度为k的塔\(b1,b_2,\dots,b_k\)满足\(2b_1\leqb_2,2b_2\leqb_3,2b_3\leqb_4,\dots,2b{k-1}\leqb_k\)你要从中选择一些数来叠很多座高度为\(k\)的塔,问最多能叠多少座塔。Input第一行包含一个正整数T(1≤T≤10),表示测试数据的组数。每组数据第一行包含两个正整数n,k(2......