/*#pragma GCC optimize("O3")
#pragma GCC optimization("Ofast,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")*/
#include <bits/stdc++.h>
//#include "algotester_generator.h"
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>*/
#define ll long long
#define ll1 long long
#define ull unsigned long long
#define dou long double
#define str string
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define vbool vector<bool>
#define vstr vector<str>
#define vvll vector<vll>
#define pb push_back
#define pf push_front
#define endl "\n"
#define fr first
#define se second
// #define sortcmp(a) sort(a.begin(), a.end(), cmp)
#define sort(a) sort(a.begin(), a.end())
#define reverse(a) reverse(a.begin(), a.end())
#define speed ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define INF 1000000000000000000
#define ordered_set tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update>
using namespace std;
//using namespace __gnu_pbds;
mt19937 rng(time(0));
ll randll(ll l, ll r){
return uniform_int_distribution<ll>(l, r)(rng);
}
vll p, s;
ll res = 0;
ll f(ll n){
return n*(n+1)/2;
}
ll find(ll v){
if(p[v] == v) return v;
return p[v] = find(p[v]);
}
void unite(ll a, ll b){
a = find(a);
b = find(b);
if(a == b) return;
if(s[a] < s[b]){
p[a] = b;
res -= f(s[a])+f(s[b]);
s[b] += s[a];
res += f(s[b]);
}
else{
p[b] = a;
res -= f(s[a])+f(s[b]);
s[a] += s[b];
res += f(s[a]);
}
}
int main(){
speed;
ll n, m;
cin >> n >> m;
vll a(n);
vpll b(m);
p.resize(n);
s.resize(n, 1);
set<pll> st;
for(int i =0 ; i < n; i ++){
cin >> a[i];
p[i] = i;
st.insert({a[i], i});
}
for(int i = 0; i < m; i ++){
cin >> b[i].fr;
b[i].se = i;
}
sort(b);
vbool us(n, 0);
vll ans(m);
for(auto i : b){
while(!st.empty() && (*st.begin()).fr <= i.fr){
pll p = *st.begin();
st.erase(st.begin());
us[p.se] = 1;
res ++;
if(p.se+1 < n && us[p.se+1]) unite(p.se, p.se+1);
if(p.se-1 >= 0 && us[p.se-1]) unite(p.se, p.se-1);
}
ans[i.se] = res;
}
for(int i = 0; i < m; i ++){
cout << ans[i] << endl;
}
}
# | 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... |