【Question】arc068_e-Snuke Line——Using special properties to discuss

Obviously, the complexity itself is only \(O(n \log n)\) for a brute force sweep over all spans .
It is easy to think of adding the number of intervals that cover this position to each arriving position. The remaining question is how to deal with intervals that cover multiple locations.
Records that have been accessed or deduplicated are difficult to achieve. But notice one property:

All intervals whose length is not less than the span must be accessed, and all intervals with a length of light rain span can be accessed at most once.

So we enumerate the span, maintain the number of intervals whose length is not less than the current span, and add the interval whose length is less than the current span to a data structure that supports interval addition and single-point query.
Time complexity \(O(n \log ^2 n)\) .

#include <bits/stdc++.h>
using namespace std;
const int N = 300010, M = 100010;
#define lowbit(x) ((x) & (-(x)))
int v[M],n,m,s,ans,p=1;
struct data {
  int l,r;
  bool operator < (const data& x) const {
    return r - l < x.r - x.l;
  }
} dat[N];
void add(int p,int val) {
  for (; p <= s ; p += lowbit(p))
    v[p] += val;
}
int query(int p) {
  int res = 0;
  for (; p ; p -= lowbit(p))
    res += v[p];
  return res;
}
int main() {
  scanf("%d%d",&n,&m);
  for (int i = 1 ; i <= n ; ++ i)
    scanf("%d%d",&dat[i].l,&dat[i].r);
  sort(dat+1,dat+n+1);
  s = m+1;
  for (int i = 1 ; i <= m ; ++ i) {
    while (dat[p].r - dat[p].l + 1 < i && p <= n) {
      add(dat[p].l+1,1);
      add(dat[p].r+2,-1);
      p ++;
    }
    ans = n - p + 1;
    for (int j = 0 ; j <= m ; j += i)
      ans += query(j+1);
    printf("%d\n",ans);
  }
  return 0;
}



Summary: Using the special nature of the topic to classify and discuss can effectively simplify the topic.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325339133&siteId=291194637