ZOJ 3676 排序+二分

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std ;
#define N 101000
#define M 10100
int n, t ;
struct Node{
    int p, q ;
    bool operator<(const Node & a)const{
        return (q - p < a.q - a.p) ;
    }
}s[N] ;
int f[N] ;
int sum1[N], sum2[N] ;
int main(){
    //freopen("input.txt", "r", stdin) ;
    while(scanf("%d %d", &n, &t) != EOF){
        for(int i=1; i<=n; i++)  scanf("%d %d", &s[i].p, &s[i].q) ;
        sort(s+1, s+1+n) ;
        for(int i=1; i<=n; i++)  f[i] = s[i].q - s[i].p ;
        sum1[0] = sum2[n+1] = 0 ;
        for(int i=1; i<=n; i++) sum1[i] = sum1[i-1] + s[i].p ;
        for(int i=n; i>0; i--)  sum2[i] = sum2[i+1] + s[i].q ;
        while(t--){
            int m ;
            scanf("%d", &m) ;
            int idx = lower_bound(f+1, f+1+n, m) - f ;
            int ans = sum1[idx-1] + sum2[idx]- (n - idx + 1) * m ;
            printf("%d\n", ans) ;
        }
    }
    return 0 ;
}

你可能感兴趣的:(ZOJ 3676 排序+二分)