当前位置:网站首页>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());
}
}
边栏推荐
- Functions should not specify operation types through variables
- 洛谷P5994 [PA2014]Kuglarz(异或思维+MST)
- 【Unexpected token o in JSON at position 1出错原因及解决方法】
- 【日常训练】207. 课程表
- Authority design of SaaS system based on RBAC
- 环网冗余式CAN/光纤转换器的CAN光端机在消防火灾联网报警系统中的应用
- Application of can optical transceiver of ring network redundant can/ optical fiber converter in fire alarm system
- Opencv minimum filtering (not limited to images)
- Three Siemens fire-fighting hosts fc18 are equipped with can optical transceiver for optical fiber redundant ring network networking test
- TCP的那点玩意儿
猜你喜欢

Use the frame statistics function of the message and waveform recording analyzer royalscope to troubleshoot the accidental faults of the CAN bus

电子学:第009课——实验 7:研究继电器

Sword finger offer II 027 Palindrome linked list

How to resize an image in C #

一文了解 | 革兰氏阳性和阴性菌区别,致病差异,针对用药

Application of can optical transceiver of ring network redundant can/ optical fiber converter in fire alarm system

FM信号、调制信号和载波

饮食干预减轻癌症治疗相关症状和毒性

c#ColorDialog更改文本颜色和FontDialog更改文本字体的使用示例

使用Adobe Acrobat Pro调整PDF页面为统一大小
随机推荐
【莫比乌斯反演】
用函数的递归来解决几道有趣的题
WebSocket的理解以及应用场景
C#控件刷新
电子学:第013课——实验 14:可穿戴的脉冲发光体
To understand the difference between Gram-positive and Gram-negative bacteria and the difference in pathogenicity
Sword finger offer II 027 Palindrome linked list
剑指offer刷题(简单等级)
使用Adobe Acrobat Pro调整PDF页面为统一大小
FFT【模板】
ffmpeg+SDL2实现音频播放
將數據導入到MATLAB
C control refresh
产品经理专业知识50篇(四)-从问题到能力提升:AMDGF模型工具
27. remove elements
How much do you know about electronic components on PCB?
共话云原生数据库的未来
Buckle 78: subset
@The difference between resource and @autowired annotation, why recommend @resource?
洛谷P2048 [NOI2010] 超级钢琴(RMQ+优先队列)