3 solutions

  • 0
    @ 2022-6-13 14:52:56
    
    
    import java.util.Scanner;
    
    public class T6 {
    static long loc(long p) {
    double d = (Math.sqrt(1 + 8 * p) - 1) / 2;
    return d==(long)d?(long)d:(long)d+1;
    }
    
    static long nsum(long i) {
    //计算n*(n+1)/2这个数列的和
    return (i * i * i + 3 * i * i + 2 * i)/6;
    }
    
    static long f(long from, long end) {
    long sum = 0;
    long locfrom = loc(from);
    //计算起始位置到最近分界点的和
    long n = locfrom * (locfrom + 1) / 2;
    sum += (n - from + 1) * (locfrom + locfrom - (n - from))/2;
    //计算分界点到距离end最近前面分界点的和
    
    long locend = loc(end);
    sum += nsum(locend-1) - nsum(locfrom);
    //计算机距离end最近分界点到end的和
    long ncp = locend * (locend - 1) / 2;
    sum += (end - ncp) * (1 + end - ncp) / 2;
    return sum;
    }
    
    public static void main(String[] args) {
    
    Scanner scanner = new Scanner(System.in);
    int count = scanner.nextInt();
    for (int i = 0; i < count; i++) {
    long from = scanner.nextLong();
    long end = scanner.nextLong();
    System.out.println(f(from, end));
    }
    scanner.close();
    
    }
    }
    
    

    Information

    ID
    1844
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    4
    Tags
    # Submissions
    54
    Accepted
    9
    Uploaded By