码迷,mamicode.com
首页 > 其他好文 > 详细

蒟蒻的数列

时间:2019-11-11 12:37:06      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:pre   root   inf   个数   inpu   特殊情况   pos   操作   情况   

DCrusher有一个数列,初始值均为0,他进行N次操作,每次将数列[a,b)这个区间中所有比k小的数改为k,他想知
道N次操作后数列中所有元素的和。他还要玩其他游戏,所以这个问题留给你解决。
Input
第一行一个整数N,然后有N行,每行三个正整数a、b、k。
N<=40000 , a、b、k<=10^9
Output
一个数,数列中所有元素的和
Sample Input
4
2 5 1
9 10 4
6 8 2
4 6 3
Sample Output
16

 

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n,xx,yy,zz,root,cnt,inf=1000000000,tree[22222222],lson[22222222],rson[22222222];
long long ans;
void insert(int l,int r,int &pos,int L,int R,int Wei)
{
    if(!pos)
	pos=++cnt;
    if(l>=L&&r<=R)
	{
		tree[pos]=max(tree[pos],Wei);
		return;
	}
    int mid=(l+r)>>1;
    if(mid<L)
	   insert(mid+1,r,rson[pos],L,R,Wei);
    else 
	     if(mid>=R)
		      insert(l,mid,lson[pos],L,R,Wei);
         else insert(l,mid,lson[pos],L,R,Wei),insert(mid+1,r,rson[pos],L,R,Wei);
}
void dfs(int l,int r,int x,int Wei)
{    
    if(!x)
	{
		ans+=(r-l+1)*Wei;
		return;
	}
    int mid=(l+r)>>1;
    dfs(l,mid,lson[x],max(Wei,tree[lson[x]]));
    dfs(mid+1,r,rson[x],max(Wei,tree[rson[x]]));
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
	{
        scanf("%d%d%d",&xx,&yy,&zz);
        if(xx==yy)continue;//特殊情况,讨论一下 
        insert(1,inf,root,xx,yy-1,zz);
    }
    dfs(1,inf,root,0);
    printf("%lld\n",ans);
}

  

蒟蒻的数列

标签:pre   root   inf   个数   inpu   特殊情况   pos   操作   情况   

原文地址:https://www.cnblogs.com/cutemush/p/11833988.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!