#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = a; i <= b; ++i)
#define REP(i, a, b) for(int i = a; i >= b; --i)
#define mp make_pair
#define all(v) v.begin(), v.end()
#define uni(v) v.erase(unique(all(v)), v.end())
const int mod = 1e9 + 7;
inline bool maximize(int &u, int v){
if(v > u){
u = v;
return true;
}
return false;
}
inline bool minimize(int &u, int v){
if(v < u){
u = v;
return true;
}
return false;
}
inline bool maximizell(long long &u, long long v){
if(v > u){
u = v;
return true;
}
return false;
}
inline bool minimizell(long long &u, long long v){
if(v < u){
u = v;
return true;
}
return false;
}
inline int fastPow(int a, int n){
if(n == 0) return 1;
int t = fastPow(a, n >> 1);
t = 1ll * t * t % mod;
if(n & 1) t = 1ll * t * a % mod;
return t;
}
inline void add(int &u, int v){
u += v;
if(u >= mod) u -= mod;
}
inline void sub(int &u, int v){
u -= v;
if(u < 0) u += mod;
}
const int maxN = 1e6 + 5;
int n, q;
int root[maxN], sz[maxN];
pair<int, int> h[maxN], y[maxN];
bool mark[maxN];
long long answer[maxN];
long long rem = 0;
int find_root(int v){
return root[v] = root[v] == v ? v : find_root(root[v]);
}
bool union_set(int u, int v){
u = find_root(u);
v = find_root(v);
if(u == v) return false;
if(sz[u] > sz[v])swap(u, v);
rem -= 1ll * sz[u] * (sz[u] + 1) / 2;
rem -= 1ll * sz[v] * (sz[v] + 1) / 2;
root[v] = u;
sz[u] += sz[v];
rem += 1ll * sz[u] * (sz[u] + 1) / 2;
return true;
}
void process(){
cin >> n >> q;
FOR(i, 1, n){
cin >> h[i].first;
h[i].second = i;
root[i] = i;
sz[i] = 1;
}
sort(h + 1, h + 1 + n);
FOR(i, 1, q){
cin >> y[i].first;
y[i].second = i;
}
sort(y + 1, y + 1 + q);
int j = 1;
FOR(i, 1, q){
while(j <= n && h[j].first <= y[i].first){
++rem;
mark[h[j].second] = true;
if(h[j].second >= 2 && mark[h[j].second - 1])union_set(h[j].second, h[j].second - 1);
if(h[j].second + 1 <= n && mark[h[j].second + 1])union_set(h[j].second, h[j].second + 1);
++j;
}
answer[y[i].second] = rem;
}
FOR(i, 1, q)cout << answer[i] << '\n';
}
#define NAME ""
int main(){
if(fopen(NAME".inp", "r")){
freopen(NAME".inp", "r", stdin);
freopen(NAME".out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while(t--)
process();
cerr << '\n' << "Time elapsed: " << (1.0 * clock() / CLOCKS_PER_SEC) << " s\n" ;
return 0;
}
Compilation message (stderr)
pilot.cpp: In function 'int main()':
pilot.cpp:105:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
105 | freopen(NAME".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
pilot.cpp:106:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
106 | 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... |