2021-05-24


/*
本题可以使用Indexed Tree求解
1.Indexed Tree 一种类型题,通过向树上增减1进行计数、给定一个区间和S,求解/寻找[1,x]的区间和==S,求得区间结束点X
2.需要对序列进行排序,找出每个数的最终位置idx,也就是 offset+idx
3.新增一个数时,update(offset+idx,1) 减去一个数时update(offset+idx,-1)
4.给定一个区间和 S,寻找区间[1,x]的结束点x
5.查找过程从tree[1]开始比较目标S与tree[i]的大小
   5.1  if(tree[i<<1] < S) S在tree[i]右孩子中, i=(i<<1)+1, S-=tree[i]
   5.2  else if(tree[i<<1] >= S) S在tree[i]左孩子中,i=(i<<1)
   5.3  直到 offset < i <= offset+N,此时i即为所求x
*/

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.*;

public class Solution {
    
    
    static final StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    static final int MaxN = 300_000;
    static final int[] tree = new int[MaxN * 4];
    static final int[][] d = new int[MaxN + 5][3];
    static final int[] map = new int[MaxN + 5];
    static final HashMap<Integer, LinkedList<Integer>> online = new HashMap<>();
    static long ans;
    static int N, X, Y, offset;

    static int nextInt() throws IOException {
    
    
        in.nextToken();
        return (int) in.nval;
    }

    public static void main(String[] args) throws IOException {
    
    
        int T = nextInt();
        for (int tc = 1; tc <= T; tc++) {
    
    
            readCase();
            work();
            System.out.println("#" + tc + " " + ans);
        }
    }

    static void readCase() throws IOException {
    
    
        N = nextInt();
        X = nextInt();
        Y = nextInt();
        for (int i = 1; i <= N; i++) {
    
    
            d[i][0] = i;
            d[i][1] = 0;
            d[i][2] = nextInt();
        }
        offset = 1;
        while (offset <= N) offset <<= 1;
        offset--;
    }

    static void work() {
    
    
        Arrays.sort(d, 1, N + 1, (o1, o2) -> o1[2] == o2[2] ? o1[0] - o2[0] : o1[2] - o2[2]);
        for (int i = 1; i <= N; i++) {
    
    
            d[i][1] = i;
            map[i] = d[i][2];
        }
        Arrays.sort(d, 1, N + 1, Comparator.comparingInt(o -> o[0]));
        for (int i = 1; i <= N; i++) {
    
    
            if (d[i][2] > 0) {
    
    
                update(d[i][1], 1);
                LinkedList<Integer> set = online.getOrDefault(d[i][2], new LinkedList<>());
                set.add(d[i][1]);//记录已经使用indexed Tree处理过的value对应的index
                online.put(d[i][2], set);
            } else {
    
    
                int idx = online.get(-d[i][2]).poll();//删除最先出现的
                update(idx, -1);
            }
            if (tree[1] % Y == 0) {
    
    
                int target = X * (tree[1] / Y);
                int idx = query(target);
                ans += map[idx];
            }
        }
        for (int i = 0; i <= N; i++) online.get(i).clear();
    }

    static int query(int target) {
    
    
        int idx = 1;
        while (idx <= offset) {
    
    
            idx <<= 1;//left child node
            if (tree[idx] < target) {
    
    
                target -= tree[idx];
                idx += 1;//right child node
            }
        }
        return idx - offset;
    }

    static void update(int idx, int val) {
    
    
        int pos = offset + idx;
        while (pos > 0) {
    
    
            tree[pos] += val;
            pos <<= 1;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/awp0011/article/details/117231896