当前位置:网站首页>Luogu p2839 [national training team]middle (two points + chairman tree + interval merging)
Luogu p2839 [national training team]middle (two points + chairman tree + interval merging)
2022-06-25 08:02:00 【Mfy's little brother 1】
Luogu P2839 [ National Team ]middle( Two points + Chairman tree )
The question :
A length of n n n Sequence a a a, Let it be followed by b b b, Where the number of digits is defined as b n / 2 b_{n/2} bn/2 , among a , b a,b a,b from 0 0 0 Start tagging , Division takes the whole . Give you a length of n n n Sequence s s s.
answer Q Q Q A question like this : s s s The left-hand end of [ a , b ] [a,b] [a,b] Between , The right endpoint is [ c , d ] [c,d] [c,d] In the subinterval between , The largest median .
among a < b < c < d a<b<c<d a<b<c<d
The position also changes from 0 Start tagging
Ideas :
Sort the initial array from small to large , The subscript of the median binary x , Determine whether the element at this position conforms to : Greedy thought , stay x stay [a,d] Than a[x] All small numbers become -1, Big numbers all become 1, And then sum up : stay [a,b] Find an interval from the right endpoint b The largest continuous interval starting to the left and + [b+1,c-1] and + stay [c,d] Find an interval from the left endpoint c Start the largest continuous interval to the right and .
root[i] Express , Than a No i Large numbers and small numbers have all been assigned to -1, All large numbers are assigned to 1.root[i] Of i Satisfy continuity , It can be divided into two parts .
Use the chairman tree , First assign all the points to 1, Then press the weight from small to large , In the corresponding position (a The initial position of the array )-1. Every time violence logn To modify the tree .
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int>pii;
const int INF=0x3f3f3f3f;
const int maxn=1e5+6;
int n,m,tot,a[maxn],id[maxn],root[maxn],p[6],pre;
struct NODE{
int sum,ml,mr,l,r;
void init(){
sum=0,ml=mr=-INF;
}
}tr[maxn*400],ANS;
bool cmp(int x,int y){
return a[x]<a[y];
}
void build(int &t,int l,int r){
// Dynamic opening point
t=++tot;
tr[t].ml=tr[t].mr=tr[t].sum=r-l+1;
if(l==r)return;
int mid=(l+r)>>1;
build(tr[t].l,l,mid);
build(tr[t].r,mid+1,r);
}
NODE merge(NODE h,NODE x,NODE y){
//
NODE z;
z.l=h.l,z.r=h.r;// Initial subscript
z.sum=x.sum+y.sum;//sum Direct addition
z.mr=max(y.mr,y.sum+x.mr);// The maximum continuous sum of right intervals
z.ml=max(x.ml,x.sum+y.ml);// The maximum continuous sum of the left interval
return z;
}
void update(int &t,int l,int r,int x){
tr[++tot]=tr[t],t=tot;
if(l==r){
tr[t].ml=tr[t].mr=tr[t].sum=-1;
return;
}
int mid=(l+r)>>1;
if(x<=mid)update(tr[t].l,l,mid,x);
else update(tr[t].r,mid+1,r,x);
tr[t]=merge(tr[t],tr[tr[t].l],tr[tr[t].r]);
}
void query(int t,int l,int r,int x,int y){
if(x<=l&&r<=y){
ANS=merge(ANS,ANS,tr[t]);// Merge the results of each query
return;
}
int mid=(l+r)>>1;
if(x<=mid)query(tr[t].l,l,mid,x,y);
if(y>mid)query(tr[t].r,mid+1,r,x,y);
}
bool check(int pos){
int he=0;
if(p[2]+1<=p[3]-1){
ANS.init();// Remember to initialize
query(root[pos],1,n,p[2]+1,p[3]-1);
he+=ANS.sum;
}
ANS.init();
query(root[pos],1,n,p[1],p[2]);
he+=ANS.mr;
ANS.init();
query(root[pos],1,n,p[3],p[4]);
he+=ANS.ml;
return he>=0;
}
int solve(){
for(int i=1;i<=4;i++)
p[i]=(p[i]+pre)%n+1;// Subscript from 0 Start , want +1
sort(p+1,p+1+4);
int l=1,r=n,ans=0;// stay id Array subscripts are bisected
while(l<=r){
int mid=(l+r)>>1;
if(check(mid))ans=mid,l=mid+1;
else r=mid-1;
}
pre=a[id[ans]];
return pre;
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
id[i]=i;
}
sort(id+1,id+1+n,cmp);// discretization , take id Array by a The size order of , Easy to handle
build(root[1],1,n);
for(int i=2;i<=n;i++){
root[i]=root[i-1];
update(root[i],1,n,id[i-1]);
}
scanf("%d",&m);
while(m--){
scanf("%d%d%d%d",&p[1],&p[2],&p[3],&p[4]);
printf("%d\n",solve());
}
}
边栏推荐
- 2021ICPC网络赛第一场
- 饮食干预减轻癌症治疗相关症状和毒性
- 2265. number of nodes with statistical value equal to the average value of subtree
- Application of can optical transceiver of ring network redundant can/ optical fiber converter in fire alarm system
- Functions should not specify operation types through variables
- FFT【模板】
- 現在通過開戶經理發的開戶鏈接股票開戶安全嗎?
- 洛谷P5994 [PA2014]Kuglarz(异或思维+MST)
- navicat定时任务无效
- 1464. maximum product of two elements in an array
猜你喜欢
![[little knowledge] PCB proofing process](/img/bf/f66677294a14baf08cc35d1e8c1e31.jpg)
[little knowledge] PCB proofing process

消息中间件之ActiveMQ的基本使用

Technology blog | how to communicate using SSE
![洛谷P1073 [NOIP2009 提高组] 最优贸易(分层图+最短路)](/img/cb/046fe4b47898fd6db86edc8a267c34.png)
洛谷P1073 [NOIP2009 提高组] 最优贸易(分层图+最短路)

环网冗余式CAN/光纤转换器的CAN光端机在消防火灾联网报警系统中的应用

电子学:第010课——实验 9:时间与电容器

socket问题记录

取消word文档中某些页面的页眉

Can transparent cloud gateway caniot and candtu record can messages and send and receive can data remotely

Ph中和过程建模
随机推荐
Electronics: Lesson 011 - experiment 10: transistor switches
c#搭建ftp服务器并实现文件上传和下载
navicat定时任务无效
一文了解 | 革兰氏阳性和阴性菌区别,致病差异,针对用药
现在通过开户经理发的开户链接股票开户安全吗?
Can bus working condition and signal quality "physical examination"
@The difference between resource and @autowired annotation, why recommend @resource?
线程+线程问题记录
Three Siemens fire-fighting hosts fc18 are equipped with can optical transceiver for optical fiber redundant ring network networking test
Opencv minimum filtering (not limited to images)
1742. maximum number of small balls in the box
Sword finger offer II 027 Palindrome linked list
WebSocket的理解以及应用场景
The fourth floor is originally the fourth floor. Let's have a look
Drawing of clock dial
DNS协议及其DNS完整的查询过程
Anaconda navigator启动慢的一个解决方法
函数尽量不要通过变量指定操作类型
电子学:第011课——实验 10:晶体管开关
CAN总线工作状况和信号质量“体检”