#include<bits/stdc++.h>
using namespace std;
#define int long long
map<int , pair<int , int>> seg;
int n , m , q;
auto split(int pos){
if(pos > n) return seg.end();
auto it = seg.upper_bound(pos);
it--;
int l0 = it->first;
int r0 = it->second.first;
int last = it->second.second;
if(pos == l0)return it;
it->second.first = pos - 1;
seg[pos] = {r0 , last};
return seg.find(pos);
}
signed main(){
cin>>n>>m>>q;
vector<int> L(m+1) , R(m+1);
for(int i = 1 ; i<= m ; i++){
cin>>L[i]>>R[i];
}
vector<int> s(q);
for(int i =0;i<q;i++)cin>>s[i];
vector<int> base(m+1) , len(m+1);
base[0] = 0 , len[0] = 0;
for(int i = 1 ; i <= m ;i++){
len[i] = R[i] - L[i] + 1;
base[i] = base[i-1] + len[i-1];
}
seg[1] = {n , 0};
vector<pair<int , int>> g;
for(int i = 1 ; i <= m ;i++){
int l = L[i] , r = R[i];
auto it1 = split(l);
auto it2 = split(r+1);
vector<int> toerase;
for(auto it=it1 ; it != it2 ; it++){
int l0 = it->first;
int r0 = it->second.first;
int last = it->second.second;
int cnt = r0 - l0 + 1;
if(last > 0){
int gap = (base[i] - base[last]) + (L[last] - L[i]);
g.push_back({gap , cnt});
}
toerase.push_back(l0);
}
for(auto s : toerase){
seg.erase(s);
}
seg[l] = {r , i};
}
if(g.empty()){
for(int i = 0 ; i < q ; i++)cout<<0 <<" ";
return 0;
}
sort(g.begin() , g.end());
vector<pair<int , int>> agg;
for(int i = 0 ; i < g.size() ; i++){
int go = g[i].first , cnt = 0 , j = i;
while(j < g.size() && g[i].first == g[j].first){
cnt += (g[j].second);
j++;
}
agg.emplace_back(go , cnt);
i = j - 1;
}
vector<int> pref(agg.size() + 1);
for(int i = 1 ; i <= agg.size() ; i++){
pref[i] = pref[i-1] + agg[i-1].second;
}
int total = pref[agg.size()];
for(int i = 0 ; i < q ; i++){
int s0 = s[i];
int l = 0 , r = agg.size() - 1 , idx =agg.size();
while(l <= r){
// cout<<1;
int m = (l + r) / 2;
if(agg[m].first <= s0) l = m+ 1;
else {
idx = m;
r = m - 1;
}
}
int ans = total - pref[idx];
cout<<ans << " ";
}
}
| # | 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... |