10.27

· · 个人记录

U125682

经典应用,注意到输入的时候就是按照y坐标来排序给出的,所以直接把y固定就好了(这里给固定x)

#include <bits/stdc++.h>
using namespace std ;
long long n , f[20005] , c[33005] ;
struct node {
    long long x , y ;
} a[20005] ;
bool cmp(node x , node y) {
    return (x.x != y.x ? x.x < y.x : x.y < y.y) ;
}
long long query(long long x) {
    long long ans = 0 ;
    while(x > 0) {
        ans += c[x] ;
        x -= (x & -x) ;
    }
    return ans ;
}
void add(long long x) {
    while(x <= 32001) {
        c[x] ++ ;
        x += (x & -x) ;
    }
}
int main() {
    cin >> n ;
    for(int i = 1 ; i <= n ; i ++) {
        cin >> a[i].x >> a[i].y ;
        a[i].y ++ ;
    }
    sort(a + 1 , a + n + 1 , cmp) ;
    for(int i = 1 ; i <= n ; i ++) {
        f[query(a[i].y)] ++ ;
        add(a[i].y) ;
    }
    for(int i = 0 ; i < n ; i ++) {
        cout << f[i] << "\n" ;
    }
    return 0 ;
}
  1. 首先把奶牛按照v排序,这样子每头奶牛在他的前缀里都是最大的,取消了\max
  2. 然后对于每一头奶牛x会提供v_x\times\sum_{i=1}^{x-1}|x_i-x_v|=v_x\times(数量+坐标和),然后分成左边和右边
  3. 然后用两个树状数组,一个来维护数量,一个来维护坐标和
  4. 最后面展开推公式就好了。
    #include <bits/stdc++.h>
    using namespace std ;
    long long n , c[2][50005] , ans , mod = 998244353 ;
    struct node {
     long long x , v ;
    } a[200005] ;
    bool cmp(node x , node y) {
     return x.v < y.v ;
    }
    long long query(long long x , long long id) {
     long long ans = 0 ;
     while(x > 0) {
         ans += c[id][x] ;
         x -= (x & -x) ;
     }
     return ans ;
    }
    void add(long long x , long long id , long long y) {
     while(x <= 50001) {
         c[id][x] += y ;
         x += (x & -x) ;
     }
    }
    int main() {
     cin >> n ;
     for(int i = 1 ; i <= n ; i ++) {
         cin >> a[i].v >> a[i].x ;
     }
     sort(a + 1 , a + n + 1 , cmp) ;
     for(int i = 1 ; i <= n ; i ++) {
         long long l = a[i].v * (a[i].x * query(a[i].x , 0) - query(a[i].x , 1)) % mod ;
         long long r = a[i].v * ((query(50001 , 1) - query(a[i].x , 1)) - a[i].x * (query(50001 , 0) - query(a[i].x , 0))) % mod ;
         ans = (ans + l + r) % mod ;
         add(a[i].x , 0 , 1) ;
         add(a[i].x , 1 , a[i].x) ;
     }
     cout << ans ;
     return 0 ;
    }
  1. 如果我们把一个区间[s,e]当做二维平面的坐标(s,e),那么就跟摘星星一个样子了,这里我们按照第一关键字为s从小到大第二关键字e从大到小排序
  2. 注意这里的坐标会有重叠所以如果相邻的两个点重叠了那么这一个的答案直接继承上一个的,否则才计算
  3. 不管重叠没重叠都要加点不然后面的就会少算
  4. 因为他要你输出每颗星星的答案所以结构体里面还要接一个id
    #include <bits/stdc++.h>
    using namespace std ;
    long long t , n , c[200005] , ans[200005] ;
    struct node {
     long long x , y , id ;
    } a[200005] ;
    bool cmp(node x , node y) {
     return (x.x != y.x ? x.x < y.x : x.y > y.y) ;
    }
    long long query(long long x) {
     long long ans = 0 ;
     while(x > 0) {
         ans += c[x] ;
         x -= (x & -x) ;
     }
     return ans ;
    }
    void add(long long x) {
     while(x <= 1e5) {
         c[x] ++ ;
         x += (x & -x) ;
     }
    }
    int main() {
     while(cin >> n && n != 0) {
         fill(c + 1 , c + 100000 + 1 , 0) ;
         for(int i = 1 ; i <= n ; i ++) {
             cin >> a[i].x >> a[i].y ;
             a[i].id = i ;
         }
         sort(a + 1 , a + n + 1 , cmp) ;
         for(int i = 1 ; i <= n ; i ++) {
             if(a[i].x == a[i - 1].x && a[i].y == a[i - 1].y) {
                 ans[a[i].id] = ans[a[i - 1].id] ;
             } else {
                 ans[a[i].id] = i - 1 - query(a[i].y - 1) ;
             }
             add(a[i].y) ;
         }
         for(int i = 1 ; i <= n ; i ++) {
             cout << ans[i] << " " ;
         }
         cout << "\n" ;
     }
     return 0 ;
    }