当前位置:网站首页>Niuke challenge 48 e speed instant forwarding (tree over tree)
Niuke challenge 48 e speed instant forwarding (tree over tree)
2022-06-26 13:57:00 【__ LazyCat__】
link :E- Speed Forwarding _ Cattle challenge 48 (nowcoder.com)
The question : Given length is n n n Sequence a a a , Together with m m m operations , There are two operations :
1. Given l , r , k l,r,k l,r,k, The query interval satisfies S ( x ) > = k S(x)>=k S(x)>=k Maximum x ( x ∈ [ 0 , 1 0 5 ] ) x(x \in [0,10^5]) x(x∈[0,105]) ,S The function is defined as S ( x ) = ∑ i = l r max ( a i − x , 0 ) S(x)=\sum_{i=l}^{r} \max(a_{i}-x,0) S(x)=∑i=lrmax(ai−x,0).
2. Given p , k p,k p,k, take a p a_{p} ap It is amended as follows k k k.
Pre knowledge : Chairman tree , Tree array , Tree cover tree , Dynamic open point line segment tree .
Answer key : First , Observe S S S The definition of the function can be found S S S The value and interval of a function [ l , r ] [l,r] [l,r] The value range of . What we need to maintain is that the interval is greater than x x x( Two points to get x x x ) The sum of all the Numbers s u m sum sum And number n u m num num , And then the result S ( x ) = s u m − x ∗ n u m S(x)=sum-x*num S(x)=sum−x∗num, contrast S ( x ) S(x) S(x) Whether the function value is greater than k k k Can continue to two points . It is obviously a good choice to maintain the chairman tree of the interval value range . But there is a second modification operation , Then we need to dynamically modify our chairman tree .
Dynamic modification is also easy . Let's start with a question , Given a sequence , Ask the interval several times [ l , r ] [l,r] [l,r] And . Obviously, this can be done directly , However, if the modification operation is added at this time , We can use tree arrays to maintain prefixes and . In fact, it also means "tree over tree" . The static chairman tree maintains prefixes and , Now there is a modification operation , We use the tree array to maintain the prefix and , The dynamic chairman tree can be achieved . Because a tree array is set on the basis of the chairman tree , So the complexity of a single query modification is O ( log 2 n ) O(\log^2{n}) O(log2n). Therefore, the complexity of the whole process is O ( q log 2 n ) O(q \log^2{n}) O(qlog2n), But the query operation is divided into two parts x x x , This will result in a query complexity band 3 3 3 individual log \log log, It is obviously difficult to 1 0 5 10^5 105 Under the data of 3 s 3s 3s Run inside .
Observe carefully and we find that , In fact, we are at two points each time x x x The next search is the interval [ l , r ] [l,r] [l,r] Greater than or equal to x x x Function value of S ( x ) S(x) S(x) To judge , But since the segment tree ( The chairman tree is actually a lot of segment trees ) In essence, it is also in dichotomy , Then we don't need to split in the outer layer x x x , You only need to calculate whether the maximum function value on the right is greater than k Until you want to jump that way , Finally jump to l = = r l==r l==r The position of is the answer . from S ( x ) = ∑ i = l r a i ∗ [ a i > m i d ] − x ∗ ∑ i = l r [ a i > m i d ] > = k S(x)=\sum_{i=l}^{r} a_{i}*[a_{i}>mid]-x*\sum_{i=l}^{r}[a_{i}>mid]>=k S(x)=∑i=lrai∗[ai>mid]−x∗∑i=lr[ai>mid]>=k know x x x Take the minimum function value to the maximum ( among m i d mid mid Is the right section greater than the midpoint , Or right range ), that x x x take m i d + 1 mid+1 mid+1 Determine whether it is greater than or equal to k k k that will do . Then the query complexity is compressed to O ( n ∗ log 2 n ) O(n*\log^2{n}) O(n∗log2n). So I can pass this question happily .
details : Note that when jumping the left subtree during query, the sum of values and points in the right value field must be preserved , Because once you jump to the left subtree, the value of the right value field will not be included in the next calculation , It needs to be reserved as a parameter .
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
const int N=1e5;
ll sum[maxn<<7],a[maxn],n,m,op,u,v,w;
int ls[maxn<<7],rs[maxn<<7],sz[maxn<<7],rt[maxn],now;
int qx[100],tx,qy[100],ty;
template<class T>inline
bool read(T&x)
{
x=0; char ch=getchar();
while(!isdigit(ch))ch=getchar();
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return ch!=EOF;
}
template<class A,class...B>inline
bool read(A&x,B&...y)
{
return read(x)&&read(y...);
}
template<class T>inline
void print(T x)
{
if(x<0)putchar('-'),x=-x;
int t[30]={
0},pos=0;
while(x)t[++pos]=x%10,x/=10;
if(!pos)putchar('0');
else while(pos)putchar(t[pos--]+'0');
return;
}
template<class T>inline
void print(T x,char ch)
{
print(x),putchar(ch);
}
inline int lowbit(int x)
{
return x&(-x);
}
int update(int&t,int l,int r,ll pos,ll up)
{
if(!t)t=++now;
sum[t]+=pos*up,sz[t]+=up;
if(l==r)return t;
int mid=l+r>>1;
if(pos<=mid)update(ls[t],l,mid,pos,up);
else update(rs[t],mid+1,r,pos,up);
return t;
}
ll query(int l,int r,ll as,ll az,ll k)
{
if(l==r)return l;
int mid=l+r>>1;
ll sml=0,nml=0,smr=0,nmr=0,sm,nm;
for(int i=1;i<=tx;i++)sml+=sum[rs[qx[i]]],nml+=sz[rs[qx[i]]];
for(int i=1;i<=ty;i++)smr+=sum[rs[qy[i]]],nmr+=sz[rs[qy[i]]];
sm=smr-sml,nm=nmr-nml;
if(as+sm-(nm+az)*(mid+1)<k)
{
for(int i=1;i<=tx;i++)qx[i]=ls[qx[i]];
for(int i=1;i<=ty;i++)qy[i]=ls[qy[i]];
return query(l,mid,as+sm,az+nm,k);
}
else
{
for(int i=1;i<=tx;i++)qx[i]=rs[qx[i]];
for(int i=1;i<=ty;i++)qy[i]=rs[qy[i]];
return query(mid+1,r,as,az,k);
}
return -1;
}
void modify(int x,ll pos,ll up)
{
for(int i=x;i<=n;i+=lowbit(i))
{
rt[i]=update(rt[i],-1,N,pos,up);
}
return;
}
int main()
{
// freopen("LCA.in","r",stdin);
read(n,m);
for(int i=1;i<=n;i++)
{
read(a[i]);
modify(i,a[i],1);
}
for(int i=1;i<=m;i++)
{
read(op,u,v);
if(!op)
{
read(w); tx=ty=0;
for(int j=u-1;j>=1;j-=lowbit(j))qx[++tx]=rt[j];
for(int j=v;j>=1;j-=lowbit(j))qy[++ty]=rt[j];
print(query(-1,N,0,0,w),'\n');
}
else
{
modify(u,a[u],-1);
modify(u,v,1);
a[u]=v;
}
}
return 0;
}
边栏推荐
- The most critical elements of team management
- Mongodb series window environment deployment configuration
- Jenkins build prompt error: eacces: permission denied
- Mathematical design D12 according to string function
- ICML 2022 | LIMO: 一种快速生成靶向分子的新方法
- Ring queue PHP
- Thinking caused by the error < note: candidate expectations 1 argument, 0 provided >
- [MySQL from introduction to mastery] [advanced part] (II) representation of MySQL directory structure and tables in the file system
- 爱可可AI前沿推介(6.26)
- Here document interaction free and expect automatic interaction
猜你喜欢

Guruiwat rushed to the Hong Kong stock exchange for listing: set "multiple firsts" and obtained an investment of 900million yuan from IDG capital

Variable declaration of typescript

windows版MySQL软件的安装与卸载

李航老师新作《机器学习方法》上市了!附购买链接

ICML 2022 | LIMO: 一种快速生成靶向分子的新方法

Detailed introduction to shell script (4)

古瑞瓦特沖刺港交所上市:創下“多個第一”,獲IDG資本9億元投資

Jenkins build prompt error: eacces: permission denied

Es6: iterator

MediaPipe手势(Hands)
随机推荐
I met the problem of concurrent programming in an interview: how to safely interrupt a running thread
Ring queue PHP
网络远程访问的方式使用树莓派
[hcsd application development training camp] one line of code second cloud evaluation article - experience from the experiment process
Generate JDE dot train
KITTI Detection dataset whose format is letf_ top_ right_ bottom to JDE normalied xc_ yc_ w_ h
[node.js] MySQL module
古瑞瓦特沖刺港交所上市:創下“多個第一”,獲IDG資本9億元投資
虫子 类和对象 中
Bug STL string
程序员必备,一款让你提高工作效率N倍的神器uTools
Generation and rendering of VTK cylinder
GO语言-管道channel
Is expression of D
33、使用RGBD相机进行目标检测和深度信息输出
Nlp-d60-nlp competition D29
泰山OFFICE技术讲座:使用字体粗体的四种情形
虫子 运算符重载的一个好玩的
What is the use of index aliases in ES
Luogu p3426 [poi2005]sza-template solution