首页 > 其他分享 >[CF1824D] LuoTianyi and the Function

[CF1824D] LuoTianyi and the Function

时间:2023-08-24 22:57:24浏览次数:42  
标签:Function CF1824D ch int LL LuoTianyi nx while le

题目描述

LuoTianyi gives you an array $ a $ of $ n $ integers and the index begins from $ 1 $ .

Define $ g(i,j) $ as follows:

  • $ g(i,j) $ is the largest integer $ x $ that satisfies $ {a_p:i\le p\le j}\subseteq{a_q:x\le q\le j} $ while $ i \le j $ ;
  • and $ g(i,j)=0 $ while $ i>j $ .

There are $ q $ queries. For each query you are given four integers $ l,r,x,y $ , you need to calculate $ \sum\limits_{i=l}^{r} \sum\limits_{j=x}^{y} g(i,j) $ .

$ 1\le n,q\le 10^6,1\le a_i\le n, 1\le l\le r\le n, 1\le x\le y\le n $

考虑把答案差分后,扫描线,然后用历史版本和维护答案。

我们现在要尝试在线段树中维护出 \(g(i,r)\),当 \(r->r+1\) 时维护出 \(g\) 的变换,但是这个不好做。

换个方向,从后往前扫,维护 \(g\),此时将 \(g(l,i)\) 转到 \(g(l-1,i)\) 时,设 \(a_{l-1}\) 下一次出现的地方为 \(j\),那么 \([l-1,j]\) 这段区间内的 \(g\) 要覆盖成 \(l-1\)。然后用历史版本和维护答案即可。

题目卡常,要将多个 tag 分别加入。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1e6+5;
int n,T,l[N],r[N],x[N],y[N],nx[N],a[N];
LL ans[N];
vector<pair<int,int> >g[N];
inline int read()
{
	int s=0;
	char ch=getchar();
	while(ch<'0'||ch>'9')
		ch=getchar();
	while(ch>='0'&&ch<='9')
		s=s*10+ch-48,ch=getchar();
	return s;
}
struct node{
	int c,tg;
	LL s,hs,htg;
}tr[N<<2];
void merge(int o,int l,int r,node z)
{
	int c=tr[o].c+z.c,tg=tr[o].tg;
	LL s=tr[o].s,hs=tr[o].hs+z.c*tr[o].s+z.htg*(r-l+1),htg=tr[o].htg+z.htg;
	if(z.tg)
	{
		s=z.tg*(r-l+1LL);
		tg=z.tg;
	}
	if(tr[o].tg)
	{
		htg+=1LL*z.c*tr[o].tg;
		c-=z.c;
	}
	tr[o]=(node){c,tg,s,hs,htg};
}
void addc(int o,int l,int r,int c)
{
	if(tr[o].tg)
		tr[o].htg+=1LL*c*tr[o].tg;
	else
		tr[o].c+=c;
	tr[o].hs+=c*tr[o].s;
}
void addtg(int o,int l,int r,int tg)
{
	tr[o].s=tg*(r-l+1LL);
	tr[o].tg=tg;
}
void pushdown(int o,int l,int r)
{
	int md=l+r>>1;
	if(tr[o].c)
	{
		addc(o<<1,l,md,tr[o].c);
		addc(o<<1|1,md+1,r,tr[o].c);
		tr[o].c=0;
	}
	if(tr[o].tg)
	{
		addtg(o<<1,l,md,tr[o].tg);
		addtg(o<<1|1,md+1,r,tr[o].tg);
		tr[o].tg=0;
	}
	if(tr[o].htg)
	{
		tr[o<<1].htg+=tr[o].htg;
		tr[o<<1].hs+=(md-l+1LL)*tr[o].htg;
		tr[o<<1|1].htg+=tr[o].htg;
		tr[o<<1|1].hs+=1LL*(r-md)*tr[o].htg;
		tr[o].htg=0;
	}
}
void update(int o,int l,int r,int x,int y,int tg)
{
	if(x<=l&&r<=y)
	{
		addtg(o,l,r,tg);
		return;
	}
	pushdown(o,l,r);
	int md=l+r>>1;
	if(md>=x)
		update(o<<1,l,md,x,y,tg);
	if(md<y)
		update(o<<1|1,md+1,r,x,y,tg);
	tr[o].s=tr[o<<1].s+tr[o<<1|1].s;
	tr[o].hs=tr[o<<1].hs+tr[o<<1|1].hs;
}
LL query(int o,int l,int r,int x,int y)
{
	if(x<=l&&r<=y)
		return tr[o].hs;
	pushdown(o,l,r);
	int md=l+r>>1;
	LL ret=0;
	if(md>=x)
		ret+=query(o<<1,l,md,x,y);
	if(md<y)
		ret+=query(o<<1|1,md+1,r,x,y);
	return ret;
}
int main()
{
	n=read(),T=read();
	for(int i=1;i<=n;i++)
		a[i]=read(),nx[i]=n+1;
	for(int i=1;i<=T;i++)
	{
		l[i]=read(),r[i]=read(),x[i]=read(),y[i]=read();
		g[l[i]].push_back(make_pair(i,1));
		if(r[i]^n)
			g[r[i]+1].push_back(make_pair(i,-1));
	}
	for(int i=n;i>=1;i--)
	{
		update(1,1,n,i,nx[a[i]]-1,i);
		nx[a[i]]=i;
		addc(1,1,n,1);
		for(int j=0;j<g[i].size();j++)
			ans[g[i][j].first]+=g[i][j].second*query(1,1,n,x[g[i][j].first],y[g[i][j].first]);
	}
	for(int i=1;i<=T;i++)
		printf("%lld\n",ans[i]);
}

标签:Function,CF1824D,ch,int,LL,LuoTianyi,nx,while,le
From: https://www.cnblogs.com/mekoszc/p/17655408.html

相关文章

  • 如何使用 ABAP Function Module SEO_CLASS_CREATE_COMPLETE 创建 ABAP class
    SEO_CLASS_CREATE_COMPLETE函数模块用于在SAP系统中创建一个完整的SAP类。在SAPABAP中,类是面向对象编程的基本构建块,它允许开发者将数据和行为组织到一个单一的实体中。SAP的类通常用于描述业务对象、数据结构和业务逻辑,以实现灵活性和可维护性。SEO_CLASS_CREATE_COMPLETE函数......
  • Nodejs Function遇见WorkerProcessExitException : node exited with code -107374079
    问题描述NodejsFunction,使用BlobTrigger用于处理上传到StorageBlob的文件,但是最近发现偶发报错:Exceptionwhileexecutingfunction:Functions.AzureBlobTrigger--->Microsoft.Azure.WebJobs.Script.Workers.WorkerProcessExitException:nodeexitedwithcode-1073740791......
  • 【Azure Function App】Nodejs Function遇见WorkerProcessExitException : node exite
    问题描述NodejsFunction,使用BlobTrigger用于处理上传到StorageBlob的文件,但是最近发现偶发报错:Exceptionwhileexecutingfunction:Functions.AzureBlobTrigger--->Microsoft.Azure.WebJobs.Script.Workers.WorkerProcessExitException:nodeexitedwithcode-10737407......
  • [React Typescript] Function overload in React hook
    import{useState}from"react";import{Equal,Expect}from"../helpers/type-utils";typeUseStateReturnValue<T>={value:T;set:React.Dispatch<React.SetStateAction<T>>;};exportfunctionuseStateAsObjec......
  • Callback Function Essence
    IncludeExampleInput:Iama.routeexecutefinish.Iamb.routeexecutefinish.WhatisCallbackCallbackfunctiondefine:Ifafunctionisthreatedasafunctionparameter,thenthefunctionnamedaCallbackfunction.Callbackfunctionisaverycom......
  • KubeSphere 社区双周报 | Java functions framework 支持 SkyWalking | 2023.8.4-8.17
    KubeSphere社区双周报主要整理展示新增的贡献者名单和证书、新增的讲师证书以及两周内提交过commit的贡献者,并对近期重要的PR进行解析,同时还包含了线上/线下活动和布道推广等一系列社区动态。本次双周报涵盖时间为:2023.08.04-2023.08.17。贡献者名单新晋KubeSphereCon......
  • 关于 SAP ABAP Enqueue Function Module 的输入参数 _wait
    我们查看ABAP系统根据LockObject自动生成的EnqueueFunctionModule,可以发现它有一个名叫_wait的输入参数,默认值为space:该参数决定了发生锁冲突时的锁行为。开发人员有以下选择:初始值:如果由于存在竞争锁而导致锁定尝试失败,则会触发异常FOREIGN_LOCK。X:如果由......
  • python中function使用class调用和使用对象调用的区别
    问题在python中,class中函数的定义中有一个特殊的self指针,如果一个函数有一个self参数,通常意味着这是一个非静态函数,也就是调用的时候第一个参数是对象指针,只是这个指针是调用这个函数时由python来自动填充。tsecer@harry:catcls_mth.pyclasstsecer():defharry(self):......
  • FunctionalInterface解析
    FunctionalInterface注解FunctionalInterface`是Java8新增的一个注解,使用`FunctionalInterface`注解的接口被称为`函数式接口@FunctionalInterface`注解的作用是告知编译器进行检查,可加可不加,但是如果加上了,那么接口必须为`函数式接口特点从FunctionalInterface的Doc注释可知,......
  • [转]c++ function使用方法
    原帖:https://blog.csdn.net/myRealization/article/details/111189651 cppreference https://en.cppreference.com/w/cpp/utility/functional/functionboost源码剖析之:泛型函数指针类boost::functionhttps://blog.csdn.net/pongba/article/details/1560773c++模板偏特化 h......