#include<bits/stdc++.h>
using namespace std;
#define NAME "CONCERT"
#define nl '\n'
#define allofa(x,sz) x,x+sz+1
#define allof(x) x.begin(),x.end()
#define allof1(x) x.begin()+1,x.end()
#define mset(x,val) memset(x,val,sizeof(x))
template<class T> T Abs(T &x) {return (x>=0 ? x : -x);};
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;};
typedef long long ll;
const ll mod = (long long)1e9+7;
const ll LINF = (long long)1e15;
const int INF = (int)1e9;
const int MAXN = (int)1e6+5;
vector<int> sorted;
ll res[MAXN];
int a[MAXN];
int n,q;
void ccps() {
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
if(fopen(NAME".inp","r")) {
freopen(NAME".inp","r",stdin);
freopen(NAME".out","w",stdout);
}
}
namespace BruteForce {
bool check() {
return n<=1e3 and q<=1e3;
}
ll nCk(int n) {
return 1LL*n*(n+1)/2;
}
void sol() {
while(q--) {
int upper; cin >> upper;
ll res = 0;
int cnt = 0;
for (int i = 1; i<=n; i++) {
if(a[i]<=upper) {
cnt++;
res++;
}
else {
res+=nCk(cnt);
cnt=0;
}
}
res+=nCk(cnt);
cout << res << nl;
}
}
}
namespace FinalSol {
ll nCk(int n) {
return 1LL*n*(n+1)/2;
}
struct DSU {
int par[MAXN],power[MAXN];
void makeSet(int n) {
for (int i = 1; i<=n; i++) {
par[i]=i;
power[i]=1;
}
}
int find(int u) {
return (u==par[u] ? u : par[u]=find(par[u]));
}
ll getVal(int u) {
u=find(u);
return nCk(power[u]);
}
bool Union(int u, int v, ll &res) {
u=find(u);v=find(v);
if(u==v) return false;
if(power[u]<power[v]) swap(u,v);
res-=getVal(u);
res-=getVal(v);
par[v]=u;
power[u]+=power[v];
res+=getVal(u);
return true;
}
} dsu;
struct query {
int height, ID;
bool operator <(const query &ot) const {
return height<ot.height;
}
};
struct info {
int val,ID;
bool operator <(const info &ot) const {
return val<ot.val;
}
};
void sol() {
vector<query> queries;
vector<info> sorted;
bool canReach[n+5];
mset(canReach,false);
for (int i = 1; i<=n; i++) {
sorted.push_back({a[i],i});
}
for (int i = 1; i<=q; i++) {
int height; cin >> height;
queries.push_back({height,i});
}
int ptr = 0;
ll curRes = 0;
sort(allof(sorted));
sort(allof(queries));
dsu.makeSet(n);
for (query x : queries) {
int h = x.height;
int id = x.ID;
while(ptr<n and sorted[ptr].val<=h) {
int pos = sorted[ptr].ID;
canReach[pos]=true;
curRes++;
if(pos>1 and canReach[pos-1]) dsu.Union(pos,pos-1,curRes);
if(pos<n and canReach[pos+1]) dsu.Union(pos,pos+1,curRes);
ptr++;
}
res[id] = curRes;
}
for (int i = 1; i<=q; i++) {
cout << res[i] << nl;
}
}
}
signed main() {
ccps();
cin >> n >> q;
for (int i = 1; i<=n; i++) {
cin >> a[i];
sorted.push_back(a[i]);
}
if(BruteForce::check()) BruteForce::sol();
else FinalSol::sol();
}
컴파일 시 표준 에러 (stderr) 메시지
pilot.cpp: In function 'void ccps()':
pilot.cpp:26:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
26 | freopen(NAME".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
pilot.cpp:27:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
27 | freopen(NAME".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... |