이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//#include "grader.cpp"
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n, c, last, k[N], s[N], e[N];
vector<pair<int,int>> ranges;
vector<int> pref;
vector<vector<int>> edges;
struct seg_tree{
vector<int>tree;
int s;
void mtree(int n){
s=1;
while(s<n)s*=2;
tree.assign(s*2, 0);
for(int i=s;i<s+n;i++) tree[i] = 1;
for(int i=s-1;i>0;i--) tree[i] = tree[i*2] + tree[i*2+1];
}
int get1(int l,int lx,int rx,int x){
if(lx==rx) return lx;
int m=(lx+rx)/2;
if(l <= tree[x*2]) return get1(l,lx,m,x*2);
else return get1(l,m+1,rx,x*2+1);
}
int get(int val){
return get1(val,1,s,1);
}
void update1(int l,int r,int lx,int rx,int x){
if(lx>r||rx<l) return;
if(lx>=l&&rx<=r){
tree[x]=0;
return;
}
int mid=(lx+rx)/2;
update1(l,r,lx,mid,x*2);
update1(l,r,mid+1,rx,x*2+1);
tree[x]=tree[x*2]+tree[x*2+1];
}
void update(int l,int r){
update1(l,r,1,s,1);
}
void print(){
for(int i=0;i<s*2;i++){
cout<<tree[i]<<" ";
}cout<<"\n";
}
int fr(){
return tree[1];
}
}tree;
pair<int,int> dfs(int u,int p){
pair<int,int> ans = make_pair(0,ranges[u].first);
for(auto v:edges[u]){
if(v==p) continue;
auto it=dfs(v,u);
ans = (it.first>ans.first ? it:ans);
}
if(pref[ranges[u].second-1] - pref[ranges[u].first-1] == 0)
ans.first++;
return ans;
}
bool cmp(pair<int,int> a,pair<int,int> b){
if(a.first==b.first) return a.second>b.second;
return a.first < b.first;
}
int GetBestPosition(int N, int C, int R, int *K, int *S, int *E) {
//cout<<"WORKS\n";
n=N, c=C, last=R;
for(int i=0;i<n-1;i++) k[i+1]=K[i];
for(int i=0;i<c;i++) s[i]=S[i]+1, e[i]=E[i]+1;
tree.mtree(n);
for(int i=0;i<c;i++){
int l = tree.get(s[i]);
int r = ((e[i]+1 > tree.fr()) ? n : tree.get(e[i]+1)-1 );
//cout<<l<<" "<<r<<"\n";
ranges.push_back({l,r});
tree.update(l+1,r);
}
sort(ranges.begin(), ranges.end(), cmp);
pref.assign(n+1,0);
for(int i=1;i<n;i++) {
pref[i] += pref[i-1];
if(k[i-1] > last) pref[i]++;
}
edges.resize(n+1);
for(int i=1; i<ranges.size(); i++){
int j=i-1, fr=ranges[i].first, sc=ranges[i].second;
while(!(fr>=ranges[j].first && sc<=ranges[j].second)&&j>=0) j--;
edges[j].push_back(i);
edges[i].push_back(j);
}
auto ans=dfs(0,-1);
return ans.second--;
}
/*
5 3
3
1 0 2 4
1 3
0 1
0 1
*/
컴파일 시 표준 에러 (stderr) 메시지
tournament.cpp: In function 'int GetBestPosition(int, int, int, int*, int*, int*)':
tournament.cpp:95:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
95 | for(int i=1; i<ranges.size(); i++){
| ~^~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |