Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #define LDR 1
- #define LST 200000
- using namespace std;
- struct anode
- {
- int nr=0,val=0;
- };
- anode findBest(anode a,anode b)
- {
- if(a.val>b.val)
- return a;
- else
- if(a.val<b.val)
- return b;
- else
- if(a.nr<b.nr)
- return a;
- return b;
- }
- int n=0;
- int v[200001]={0};
- anode arb[600001];
- int lazy[600001]={0};
- void build(int nod,int a,int b)
- {
- if(a>=b)
- {
- arb[nod].nr=a;
- return;
- }
- int mij=(a+b)/2;
- build(nod*2,a,mij);
- build(nod*2+1,mij+1,b);
- }
- void propag(int nod,int a,int b)
- {
- arb[nod].val+=lazy[nod];
- if(arb[nod].nr==0)
- arb[nod].nr=a;
- if(a!=b)
- {
- lazy[nod*2]+=lazy[nod];
- lazy[nod*2+1]+=lazy[nod];
- }
- lazy[nod]=0;
- }
- void update(int nod,int st,int dr,int a,int b,int val)
- {
- if(a>=st&&b<=dr)
- lazy[nod]+=val;
- else
- {
- int mij=(a+b)/2;
- if(st<=mij)
- update(nod*2,st,dr,a,mij,val);
- propag(nod*2,a,mij);
- if(dr>mij)
- update(nod*2+1,st,dr,mij+1,b,val);
- propag(nod*2+1,mij+1,b);
- arb[nod]=findBest(arb[nod*2],arb[nod*2+1]);
- }
- }
- int main()
- {
- int x=0,y=0,q=0;
- build(1,LDR,LST);
- cin>>n>>v[1];
- for(int i=2;i<=n;++i)
- {
- cin>>v[i];
- update(1,min(v[i-1],v[i]),max(v[i-1],v[i]),LDR,LST,1);
- if(i>1&&i<n)
- update(1,v[i],v[i],LDR,LST,-1);
- }
- propag(1,LDR,LST);
- cin>>q;
- for(int i=1;i<=q;++i)
- {
- cin>>x>>y;
- if(x<n)
- update(1,min(v[x],v[x+1]),max(v[x],v[x+1]),LDR,LST,-1);
- if(x>1)
- update(1,min(v[x],v[x-1]),max(v[x],v[x-1]),LDR,LST,-1);
- if(x>1&&x<n)
- update(1,v[x],v[x],LDR,LST,1);
- v[x]=y;
- if(x<n)
- update(1,min(v[x],v[x+1]),max(v[x],v[x+1]),LDR,LST,1);
- if(x>1)
- update(1,min(v[x],v[x-1]),max(v[x],v[x-1]),LDR,LST,1);
- if(x>1&&x<n)
- update(1,v[x],v[x],LDR,LST,-1);
- propag(1,LDR,LST);
- cout<<arb[1].nr<<' '<<arb[1].val<<'\n';
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment