CF1290E Cartesian Tree

Posted C202044zxy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF1290E Cartesian Tree相关的知识,希望对你有一定的参考价值。

一、题目

点此看题

二、解法

保留 \\(\\leq k\\) 的数之后,考虑第 \\(i\\) 个数是作为极长段 \\((l_i,r_i)\\) 的最大值,那么答案是:

\\[\\sum_{i=1}^{k}r_i-l_i-1 \\]

我们先考虑计算 \\(A=\\sum_{i=1}^kr_i\\),考虑在增大 \\(k\\) 时动态插入一个数,设其位置是 \\(p\\)

  • 对于位置在它后面的位置 \\(j\\)\\(r_j\\) 会增加 \\(1\\)
  • \\(r_p=k+1\\)
  • 对于在它前面的位置 \\(j\\)\\(r_j\\) 需要和 \\(p\\)\\(\\min\\)

所以我们的数据结构要支持区间加法、全局查询、单点修改、区间取 \\(\\min\\);显然可以用势能线段树,主要是涉及区间加法有点难,可以先下传加法再下传 \\(max\\),这样加法是对 \\(max\\) 没有影响的。

可以把原序列翻转,按上面的操作再做一次,那么得到的是 \\(B=\\sum_{i=1}^k k-l_i+1\\)(因为全都翻转了),不难发现 \\(\\sum_{i=1}^kr_i-l_i-1=A+B-k(2+k)\\),那么我们得到了答案,时间复杂度 \\(O(n\\log^2n)\\)

#include <cstdio>
#include <iostream>
using namespace std;
const int M = 600005;
#define int long long
int read()
{
	int x=0,f=1;char c;
	while((c=getchar())<\'0\' || c>\'9\') {if(c==\'-\') f=-1;}
	while(c>=\'0\' && c<=\'9\') {x=(x<<3)+(x<<1)+(c^48);c=getchar();}
	return x*f;
}
int n,p[M],res[M],mx[M],cx[M];
int nx[M],num[M],sum[M],fl[M];
void fuck(int i,int c)
{
	fl[i]+=c;sum[i]+=num[i]*c;
	mx[i]+=c;cx[i]+=c;
}
void lim(int i,int c)
{
	if(mx[i]<=c) return ;
	sum[i]-=(mx[i]-c)*nx[i];
	mx[i]=c;
}
void down(int i)
{
	if(fl[i])
	{
		fuck(i<<1,fl[i]);
		fuck(i<<1|1,fl[i]);
		fl[i]=0;
	}
	lim(i<<1,mx[i]);
	lim(i<<1|1,mx[i]);
}
void up(int i)
{
	nx[i]=0;
	num[i]=num[i<<1]+num[i<<1|1];
	sum[i]=sum[i<<1]+sum[i<<1|1];
	mx[i]=max(mx[i<<1],mx[i<<1|1]);
	cx[i]=max(cx[i<<1],cx[i<<1|1]);
	if(mx[i]==mx[i<<1]) nx[i]+=nx[i<<1];
	if(mx[i]==mx[i<<1|1]) nx[i]+=nx[i<<1|1];
	if(mx[i<<1]<mx[i]) cx[i]=max(cx[i],mx[i<<1]);
	if(mx[i<<1|1]<mx[i]) cx[i]=max(cx[i],mx[i<<1|1]);
}
int add(int i,int l,int r,int L,int R)
{
	if(L>r || l>R) return 0;
	if(L<=l && r<=R)
	{
		fuck(i,1);
		return num[i];
	}
	int mid=(l+r)>>1,res=0;down(i);
	res+=add(i<<1,l,mid,L,R);
	res+=add(i<<1|1,mid+1,r,L,R);
	up(i);return res;
}
void ins(int i,int l,int r,int id,int c)
{
	if(l==r)
	{
		nx[i]=num[i]=1;
		mx[i]=sum[i]=c;
		return ;
	}
	int mid=(l+r)>>1;down(i);
	if(mid>=id) ins(i<<1,l,mid,id,c);
	else ins(i<<1|1,mid+1,r,id,c);
	up(i);
}
void zxy(int i,int l,int r,int L,int R,int c)
{
	if(L>r || l>R) return ;
	if(L<=l && r<=R)
	{
		if(mx[i]<=c) return ;
		if(cx[i]<c)
		{
			lim(i,c);
			return ;
		}
	}
	int mid=(l+r)>>1;down(i);
	zxy(i<<1,l,mid,L,R,c);
	zxy(i<<1|1,mid+1,r,L,R,c);
	up(i);
}
signed main()
{
	n=read();
	for(int i=1;i<=n;i++)
		p[read()]=i;
	for(int t=1;t<=2;t++)
	{
		for(int i=1;i<=4*n;i++)
			mx[i]=cx[i]=num[i]=sum[i]=fl[i]=0;
		for(int i=1;i<=n;i++)
		{
			int x=add(1,1,n,p[i]+1,n);
			ins(1,1,n,p[i],i+1);
			zxy(1,1,n,1,p[i]-1,i-x);
			res[i]+=sum[1];
		}
		for(int i=1;i<=n;i++)
			p[i]=n-p[i]+1;
	}
	for(int i=1;i<=n;i++)
		printf("%lld\\n",res[i]-i*(i+2));
}

POJ-2201-Cartesian Tree(笛卡尔树)

Description

Let us consider a special type of a binary search tree, called a cartesian tree. Recall that a binary search tree is a rooted ordered binary tree, such that for its every node x the following condition is satisfied: each node in its left subtree has the key less then the key of x, and each node in its right subtree has the key greater then the key of x. 
That is, if we denote left subtree of the node x by L(x), its right subtree by R(x) and its key by kx then for each node x we have 
  • if y ∈ L(x) then ky < kx 
  • if z ∈ R(x) then kz > kx

The binary search tree is called cartesian if its every node x in addition to the main key kx also has an auxiliary key that we will denote by ax, and for these keys the heap condition is satisfied, that is 
  • if y is the parent of x then ay < ax

Thus a cartesian tree is a binary rooted ordered tree, such that each of its nodes has a pair of two keys (k, a) and three conditions described are satisfied. 
Given a set of pairs, construct a cartesian tree out of them, or detect that it is not possible.

Input

The first line of the input file contains an integer number N -- the number of pairs you should build cartesian tree out of (1 <= N <= 50 000). The following N lines contain two numbers each -- given pairs (ki, ai). For each pair |ki|, |ai| <= 30 000. All main keys and all auxiliary keys are different, i.e. ki != kj and ai != aj for each i != j.

Output

On the first line of the output file print YES if it is possible to build a cartesian tree out of given pairs or NO if it is not. If the answer is positive, on the following N lines output the tree. Let nodes be numbered from 1 to N corresponding to pairs they contain as they are given in the input file. For each node output three numbers -- its parent, its left child and its right child. If the node has no parent or no corresponding child, output 0 instead. 
The input ensure these is only one possible tree.

Sample Input

7
5 4
2 2
3 9
0 5
1 3
6 6
4 11

Sample Output

YES
2 3 6
0 5 1
1 0 7
5 0 0
2 4 0
1 0 0
3 0 0

Source

Northeastern Europe 2002, Northern Subregion


思路:题目中说全部的key值都不一样,因此不会存在NO的情况。

直接建树再输出就可以。


#include <cstdio>
#include <algorithm>
using namespace std;

struct S{
int id,key,val,parent,l,r;
}node[50005];

bool cmp(struct S a,struct S b)
{
    return a.key<b.key;
}

int stk[50005],top,p[50005],l[50005],r[50005];

void build(int n)//由于已经按key值升序排序了,所以后面增加的节点要么成为某个节点的右儿子。要么让根节点成为自己的左儿子
{
    int i;

    top=0;

    stk[top]=1;

    for(i=2;i<=n;i++)
    {
        while(top>=0 && node[stk[top]].val>node[i].val) top--;

        if(top>-1)//假设找到一个不大于自己的节点。就成为该节点的右儿子,还要注意让该节点原来的右儿子变成自己的左儿子(key比其大而且小于其val)
        {
            node[i].parent=stk[top];
            node[node[stk[top]].r].parent=i;
            node[i].l=node[stk[top]].r;
            node[stk[top]].r=i;
        }
        else//假设没找到不大于自己的节点。就变成新的根
        {
            node[stk[0]].parent=i;
            node[i].l=stk[0];
        }

        stk[++top]=i;
    }
}

int main()
{
    int n,i;

    while(~scanf("%d",&n))
    {
        for(i=1;i<=n;i++)
        {
            scanf("%d%d",&node[i].key,&node[i].val);

            node[i].id=i;

            node[i].parent=node[i].l=node[i].r=0;//初始化
        }

        sort(node+1,node+n+1,cmp);

        build(n);

        for(i=1;i<=n;i++)
        {
            p[node[i].id]=node[node[i].parent].id;
            l[node[i].id]=node[node[i].l].id;
            r[node[i].id]=node[node[i].r].id;
        }

        printf("YES\n");

        for(i=1;i<=n;i++) printf("%d %d %d\n",p[i],l[i],r[i]);
    }
}


以上是关于CF1290E Cartesian Tree的主要内容,如果未能解决你的问题,请参考以下文章

PAT(甲级)2019年冬季考试 7-4 Cartesian Tree

POJ-2201-Cartesian Tree(笛卡尔树)

[SGU 155] Cartesian Tree

POJ 2201 Cartesian Tree

CF570D:Tree Requests

CF 570 D. Tree Requests