#include <bits/stdc++.h>
#define el '\n'
#define FNAME "NOI19_pilot"
#define allof(x) x.begin(),x.end()
#define allof1(x) x.begin()+1,x.end()
#define mset(x,n) memset(x,(n),sizeof(x))
using namespace std;
const long long MOD = (long long) 1e9+7;
template<class X,class Y> bool minimize(X &a,Y b){ if (a>b) {a=b; return true;} return false;}
template<class X,class Y> bool maximize(X &a,Y b){ if (a<b) {a=b; return true;} return false;}
void setup(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
if (fopen(FNAME".inp","r")){
freopen(FNAME".inp","r",stdin);
freopen(FNAME".out","w",stdout);
}
}
struct DSU{
int n;
vector<int> parent,power;
DSU(int N=0){
n=N;
if (n>0){
parent.resize(n+1,0);
power.resize(n+1,1);
}
}
void make_set(){
for (int i=1;i<=n;i++){
parent[i]=i;
power[i]=1;
}
}
int find(int u){
if (u==parent[u]) return u;
return parent[u]=find(parent[u]);
}
long long getVal(int u){
u = find(u);
return 1ll * power[u] * (power[u] + 1) / 2;
}
bool Union(int u,int v,long long &res){
u=find(u);
v=find(v);
if (u==v) return false;
if (power[u]<power[v]) swap(u,v);
res -= getVal(v);
res -= getVal(u);
parent[v]=u;
power[u]+=power[v];
res += getVal(u);
return true;
}
};
struct Peak{
int height,ID;
Peak(int H,int id): height(H),ID(id) {}
bool operator < (const Peak &other) const{
return height < other.height;
}
};
int n,q;
vector<Peak> sortH;
vector<Peak> querries;
void init(){
cin>>n>>q;
for (int i=0;i<n;i++){
int x; cin>>x;
sortH.emplace_back(x,i+1);
}
sort(allof(sortH));
for (int i=0;i<q;i++){
int x; cin>>x;
querries.emplace_back(x,i);
}
sort(allof(querries));
}
void sol(){
DSU dsu(n);
dsu.make_set();
vector<long long> resQ(q,0);
vector<bool> reachable(n+1,0);
int ptr=0;
long long curRes=0;
for (Peak qr : querries){
int maxH = qr.height;
int ID = qr.ID;
while (ptr<n and sortH[ptr].height<=maxH){
int pos = sortH[ptr].ID;
curRes++;
reachable[pos] = 1;
if (pos>1 and reachable[pos-1]) dsu.Union(pos,pos-1,curRes);
if (pos<n and reachable[pos+1]) dsu.Union(pos,pos+1,curRes);
ptr++;
}
resQ[ID] = curRes;
}
for (long long x : resQ) cout<<x<<el;
}
int main(){
setup();
init();
sol();
}
컴파일 시 표준 에러 (stderr) 메시지
pilot.cpp: In function 'void setup()':
pilot.cpp:16:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
16 | freopen(FNAME".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
pilot.cpp:17:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
17 | freopen(FNAME".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |