Berland Regional(前缀和,分块,莫队)

Polycarp is an organizer of a Berland ICPC regional event. There are nn universities in Berland numbered from 11 to nn. Polycarp knows all competitive programmers in the region. There are nn students: the ii-th student is enrolled at a university uiui and has a programming skill sisi.

Polycarp has to decide on the rules now. In particular, the number of members in the team.

Polycarp knows that if he chooses the size of the team to be some integer kk, each university will send their kk strongest (with the highest programming skill ss) students in the first team, the next kk strongest students in the second team and so on. If there are fewer than kk students left, then the team can't be formed. Note that there might be universities that send zero teams.

The strength of the region is the total skill of the members of all present teams. If there are no teams present, then the strength is 00.

Help Polycarp to find the strength of the region for each choice of kk from 11 to nn.

Input

The first line contains a single integer tt (1≤t≤10001≤t≤1000) — the number of testcases.

The first line of each testcase contains a single integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of universities and the number of students.

The second line of each testcase contains nn integers u1,u2,…,unu1,u2,…,un (1≤ui≤n1≤ui≤n) — the university the ii-th student is enrolled at.

The third line of each testcase contains nn integers s1,s2,…,sns1,s2,…,sn (1≤si≤1091≤si≤109) — the programming skill of the ii-th student.

The sum of nn over all testcases doesn't exceed 2⋅1052⋅105.

Output

For each testcase print nn integers: the strength of the region — the total skill of the members of the present teams — for each choice of team size kk.

Example

input

Copy

4
7
1 2 1 2 1 2 1
6 8 3 1 5 1 5
10
1 1 1 2 2 2 2 3 3 3
3435 3014 2241 2233 2893 2102 2286 2175 1961 2567
6
3 3 3 3 3 3
5 9 6 7 9 7
1
1
3083

output

Copy

29 28 26 19 0 0 0 
24907 20705 22805 9514 0 0 0 0 0 0 
43 43 43 32 38 43 
3083 

Note

In the first testcase the teams from each university for each kk are:

  • k=1k=1:
    • university 11: [6],[5],[5],[3][6],[5],[5],[3];
    • university 22: [8],[1],[1][8],[1],[1];
  • k=2k=2:
    • university 11: [6,5],[5,3][6,5],[5,3];
    • university 22: [8,1][8,1];
  • k=3k=3:
    • university 11: [6,5,5][6,5,5];
    • university 22: [8,1,1][8,1,1];
  • k=4k=4:
    • university 11: [6,5,5,3][6,5,5,3];

思路:

挺水的一道题,详细思路请看代码

代码:

int u[maxj];//大学
int len[maxj];//数量计数
int ans[maxj];//答案存储
void solve(){//sort是从小到大的自然排序,分块、莫队、前缀和。
    int n;cin>>n;
    for(int i=1;i<=n;++i)cin>>u[i];
    vector<int>uni[n+1];
    for(int i=1;i<=n;++i){
        int x;cin>>x;
        uni[u[i]].emplace_back(x);
    }
    for(int i=1;i<=n;++i){
        sort(uni[i].begin(),uni[i].end(),cmp1);
        len[i]=uni[i].size();
        for(int j=1;j<len[i];++j)uni[i][j]=uni[i][j-1]+uni[i][j];//前缀和
        for(int j=1;j<=len[i];++j){
            int t=len[i]-len[i]%j;//分块
            ans[j]+=uni[i][t-1];
        }
    }for(int i=1;i<=n;++i){//莫队
        cout<<ans[i]<<' ';
        ans[i]=0;
    }cout<<'\n';
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值