답안 #782278

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
782278 2023-07-13T17:51:14 Z FEDIKUS 송신탑 (IOI22_towers) C++17
14 / 100
996 ms 85784 KB
#include "towers.h"

#include <bits/stdc++.h>

using namespace std;

using pii = pair<int,int>;
using mim = pair<pii,pii>;

const int maxn=1e5+10;
const int logg=18;

int n;
int h[maxn];
multiset<pii > res;
mim sparse[logg][maxn];
int klk;
int uniq[maxn];
pair<int,int> najs[2][logg][maxn];

mim comb(mim &a,mim &b){
  mim ret=a;
  ret.first=max(ret.first,b.first);
  ret.second=min(ret.second,b.second);
  return ret;
}

mim qry(int l,int r){
  mim ret={{h[l],l},{h[l],l}};
  for(int i=logg-1;i>=0;i--){
    if(r-(1<<i)+1>=l){
      ret=comb(ret,sparse[i][r]);
      r-=(1<<i);
    }
  }
  return ret;
}

void resi(int l,int r,multiset<pii > &ret,int naj=2e9+10){
  if(r<l) return;
  najs[0][0][l]=max(najs[0][0][l],{naj,l});
  najs[1][0][r]=max(najs[1][0][r],{naj,r});
  auto temp=qry(l,r);
  pii maxi=temp.first;
  pii mini=temp.second;
  multiset<pii> levi; multiset<pii> desni;
  resi(l,maxi.second-1,levi,maxi.first);
  resi(maxi.second+1,r,desni,maxi.first);
  if(levi.size()>desni.size()) swap(levi,desni);
  for(auto i:desni) levi.emplace(i);
  if(!levi.empty()){
    auto najv=--levi.end();
    int d=naj-mini.first;
    if((*najv).first<d){
        levi.erase(najv);
        levi.emplace(pii(d,mini.second));
    }
  }else{
    levi.emplace(pii(naj-mini.first,mini.second));
  }
  swap(ret,levi);
}

void init(int _n, vector<int> _h) {
  n=_n;
  for(int i=0;i<n;i++) najs[0][0][i]=najs[1][0][i]={-1,-1};
  for(int i=0;i<n;i++) h[i]=_h[i];
  for(int i=0;i<logg;i++){
    for(int j=0;j<n;j++){
      if(i==0) sparse[i][j]={{h[j],j},{h[j],j}};
      else if(j-(1<<(i-1))>=0) sparse[i][j]=comb(sparse[i-1][j],sparse[i-1][j-(1<<(i-1))]);
      else sparse[i][j]=sparse[i-1][j];
  	}
  }
  resi(0,n-1,res);
  for(int i=1;i<logg;i++){
    for(int j=0;j<n;j++){
      if(j-(1<<(i-1))) najs[0][i][j]=max(najs[0][i][j],najs[0][i-1][j-(1<<(i-1))]);
      else najs[0][i][j]=najs[0][i-1][j];

      if(j-(1<<(i-1))) najs[1][i][j]=max(najs[1][i][j],najs[1][i-1][j-(1<<(i-1))]);
      else najs[1][i][j]=najs[1][i-1][j];
    }
  }
}

pii najupit(int l,int r,int koji){
  pii ret={-1,-1};
  for(int i=logg-1;i>=0;i--){
    if(r-(1<<i)+1>=l){
      ret=max(ret,najs[koji][i][r]);
      r-=(1<<i);
    }
  }
  return ret;
}

int D=-1;

pair<int,pii> segt[4*maxn+10];

pair<int,pii> combseg(const pair<int,pii> a,const pair<int,pii> b){
	pair<int,pii> ret;
	ret.first=a.first+b.first;
	ret.second.first=max(a.second.first,b.second.first);
	ret.second.second=min(a.second.second,b.second.second);
	return ret;
}

void update(int t,int v,int ind=1,int l=0,int r=n-1){
  if(l==r){
		segt[ind]={1,{v,v}};
		return;
	}
	int mid=l+(r-l)/2;
	if(t<=mid) update(t,v,2*ind,l,mid);
	else update(t,v,2*ind+1,mid+1,r);
	segt[ind]=combseg(segt[2*ind],segt[2*ind+1]);
}

pair<int,pii> query(int tl,int tr,int ind=1,int l=0,int r=n-1){
	if(tl<=l && r<=tr) return segt[ind];
	int mid=l+(r-l)/2;
	if(tr<=mid) return query(tl,tr,2*ind,l,mid);
	if(tl>mid) return query(tl,tr,2*ind+1,mid+1,r);
	return combseg(query(tl,tr,2*ind,l,mid),query(tl,tr,2*ind+1,mid+1,r));
}

int max_towers(int l, int r, int d) {
  if(D==-1){
			for(int i=0;i<4*maxn+10;i++) segt[i]={0,{-1,INT_MAX}};
      for(auto i:res){
          if(i.first>=d) update(i.second,i.second);
      }
      D=d;
  }
	if(l==r) return 1;
	auto temp=query(l,r);
  int ret=temp.first;
	int prvi=temp.second.second;
	int posl=temp.second.first;
	if(prvi==INT_MAX){
		prvi=r+1;
		posl=l-1;
	}

	if(l+1<=r-1) ret=query(l+1,r-1).first;
	else ret=0;
	if(h[l]<h[l+1]) ret++;
	if(h[r]<h[r-1]) ret++;
	return ret;
	
	// za levo
	if(prvi-1>=l){
		auto tmp=najupit(l,prvi-1,1);
		int koji=tmp.second;
		int naj=tmp.first;
		if(qry(l,koji).second.first<=naj-d) ret++;
	}
	// za desno
	if(posl+1<=r){
		auto tmp=najupit(posl+1,r,0);
		int koji=tmp.second;
		int naj=tmp.first;
		if(qry(koji,r).second.first<=naj-d) ret++;
	}
  return ret;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 429 ms 50780 KB 2nd lines differ - on the 1st token, expected: '1', found: '2'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5456 KB Output is correct
2 Correct 4 ms 6480 KB Output is correct
3 Incorrect 4 ms 6480 KB 1st lines differ - on the 1st token, expected: '91', found: '93'
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5456 KB Output is correct
2 Correct 4 ms 6480 KB Output is correct
3 Incorrect 4 ms 6480 KB 1st lines differ - on the 1st token, expected: '91', found: '93'
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 612 ms 64256 KB Output is correct
2 Correct 792 ms 64780 KB Output is correct
3 Correct 646 ms 64596 KB Output is correct
4 Correct 969 ms 66412 KB Output is correct
5 Correct 735 ms 65728 KB Output is correct
6 Correct 996 ms 66724 KB Output is correct
7 Correct 976 ms 65888 KB Output is correct
8 Correct 908 ms 85520 KB Output is correct
9 Correct 838 ms 85784 KB Output is correct
10 Correct 799 ms 79552 KB Output is correct
11 Correct 811 ms 79648 KB Output is correct
12 Correct 848 ms 85304 KB Output is correct
13 Correct 888 ms 85536 KB Output is correct
14 Correct 2 ms 5328 KB Output is correct
15 Correct 4 ms 6736 KB Output is correct
16 Correct 4 ms 6864 KB Output is correct
17 Correct 110 ms 64888 KB Output is correct
18 Correct 132 ms 66700 KB Output is correct
19 Correct 128 ms 66148 KB Output is correct
20 Correct 68 ms 85580 KB Output is correct
21 Correct 74 ms 85116 KB Output is correct
22 Correct 118 ms 64956 KB Output is correct
23 Correct 135 ms 66228 KB Output is correct
24 Correct 129 ms 65756 KB Output is correct
25 Correct 66 ms 85540 KB Output is correct
26 Correct 69 ms 83364 KB Output is correct
27 Correct 4 ms 6480 KB Output is correct
28 Correct 4 ms 6488 KB Output is correct
29 Correct 4 ms 6480 KB Output is correct
30 Correct 4 ms 6864 KB Output is correct
31 Correct 4 ms 6608 KB Output is correct
32 Correct 4 ms 6480 KB Output is correct
33 Correct 4 ms 6480 KB Output is correct
34 Correct 4 ms 6480 KB Output is correct
35 Correct 4 ms 6864 KB Output is correct
36 Correct 4 ms 6736 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 295 ms 19736 KB 2nd lines differ - on the 1st token, expected: '7063', found: '7197'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5456 KB Output is correct
2 Correct 4 ms 6480 KB Output is correct
3 Incorrect 4 ms 6480 KB 1st lines differ - on the 1st token, expected: '91', found: '93'
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 429 ms 50780 KB 2nd lines differ - on the 1st token, expected: '1', found: '2'
2 Halted 0 ms 0 KB -