#include <bits/stdc++.h>
using namespace std;
const int N = 1e6+10, INF = 1e9+7;
using ll = long long;
using pi = pair<int,int>;
int n,Q,h[N],num,sz[N];
ll ans, ansp[N];
vector<pi> vec,qq;
map<int, int> E,S; //E for end point of subarray, S for start point of subarray
void rem(int id){
bool okl = false, okr = false;
auto ir = S.upper_bound(id);
if(ir != S.end() and ir->first == id+1){ //the current is connected to the previous subarray to the left
okr = true;
}
auto il = E.lower_bound(id);
if(il != E.begin()){
il--;
if(il->first == id-1)
okl = true;
}
if (!okl and !okr){ //create new subarray
E[id] = ++num;
S[id] = num;
sz[num] = 1;
ans += 1;
} else if(okl and okr){ //merge left and right subarray together
int tid = il->second; //change index of end point of right side to this one
int tsr = sz[ir->second], tsl = sz[il->second];
int tsz = tsr + tsl + 1;
ans -= 1ll*tsr*(tsr+1)/2; //delete previous ans and add new one later
ans -= 1ll*tsl*(tsl+1)/2;
ans += 1ll*tsz*(tsz+1)/2;
auto ire = E.lower_bound(ir->first);
ire->second = tid;
sz[tid] = tsr + tsl + 1;
S.erase(ir);
E.erase(il);
} else if(okr){
int tid = ir->second;
int tsr = sz[tid];
ans -= 1ll*tsr*(tsr+1)/2;
ans += 1ll*(tsr+1)*(tsr+2)/2;
sz[tid]++;
S.erase(ir);
S[id] = tid;
} else if(okl){
int tid = il->second;
int tsl = sz[tid];
ans -= 1ll*tsl*(tsl+1)/2;
ans += 1ll*(tsl+1)*(tsl+2)/2;
sz[tid]++;
E.erase(il);
E[id] = tid;
}
return;
}
int main(void){
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> Q;
for(int i = 1; i <= n; i++){
cin >> h[i];
vec.emplace_back(h[i], i);
}
for(int i = 1; i <= Q; i++){
int x; cin >> x;
qq.emplace_back(x, i);
}
sort(qq.begin(), qq.end());
sort(vec.begin(), vec.end());
cout << "TEST\n";
int vi = 0; //pointer of vector's index
for(auto [want, iq] : qq){
while(vi < n and vec[vi].first <= want){
rem(vec[vi].second);
vi++;
}
ansp[iq] = ans;
}
for(int i = 1; i <= Q; i++){
cout << ansp[i] << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
320 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
320 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
320 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
320 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
66 ms |
4208 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
34 ms |
4280 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
36 ms |
4268 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
320 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
320 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
320 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |