hdu 6276 Master of Sequence 二分加预处理

问题 K: Master of Sequence

时间限制: 10 Sec  内存限制: 128 MB
提交: 106  解决: 38
[提交] [状态] [命题人:admin]

题目描述

There are two sequences a1,a2,...,an , b1,b2,...,bn . Let . There are m operations within three kinds as following:
• 1 x y: change value ax to y.
• 2 x y: change value bx to y.
• 3 k: ask min{t|k≤S(t)}

 

输入

The first line contains a integer T (1≤T≤5) representing the number of test cases.
For each test case, the first line contains two integers n (1≤n≤100000), m (1≤m≤10000).
The following line contains n integers representing the sequence a1,a2,...,an .
The following line contains n integers representing the sequence b1,b2,...,bn .
The following m lines, each line contains two or three integers representing an operation mentioned above.
It is guaranteed that, at any time, we have 1≤ai≤1000, 1≤bi,k≤109 . And the number of queries (type 3 operation) in each test case will not exceed 1000.

 

输出

For each query operation (type 3 operation), print the answer in one line.

 

样例输入

复制样例数据

2
4 6
2 4 6 8
1 3 5 7
1 2 3
2 3 3
3 15
1 3 8
3 90
3 66
8 5
2 4 8 3 1 3 6 24
2 2 39 28 85 25 98 35
3 67
3 28
3 73
3 724
3 7775

样例输出

17
87
65
72
58
74
310
2875

 

[提交][状态]

 

对于原式子S(t)=\sum_{i=1}^{n} \left \lfloor \frac{t- b_{i}}{ a_{i}}\right \rfloor ,令t=a_{i} * k_{1} + c_{1}b_{i} = a_{i} * k _{2} + c_{2},则原式子可以化为 k_{1} - k_{2} + \frac{c_{1} - c_{2}}{a_{i}}

当 c_{1}>= c_{2} 时,值为 k_{1} - k_{2}

当 c_{1} < c_{2} 时,值为 k_{1} - k_{2} -1 ,对于这种情况,向下取整会多算1,所以减去这种情况就可以了

那么s(t) = \sum_{i=1}^{n} (k_{1} - k_{2} - (c_{1} < c_{2})),数组b a都是固定的,可以先处理出所有的 k_{2} 

式子就变成了 \sum_{i=1}^{n} (k_{1} - (c_{1} < c_{2})) >= k + \sum_{i=1}^{n} k_{2} ,式子是单调变化的

二分check时 t 确定,就可以直接计算了

a的范围到1000,那么就可以直接预处理

用一个二维数组f[ i ][ j ] 表示当a==i时,b%a >= j的情况有多少种

代码:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn = 1e5 + 100;
const int inf = 1e9;
const int mod = 998244353;
int a[maxn], b[maxn];
int f[1100][1100];

bool check(ll t, ll x) {
    ll res = 0;
    for (int i = 1; i <= 1000; i++) {
        res += t / i * f[i][0];
        res -= f[i][t % i + 1];  //减去c1<c2的情况
    }
    return res >= x;
}

ll sol(ll x) {
    ll l = 0, r = 1e13;
    ll ans = 0;
    while (l <= r) {
        ll mid = (l + r) >> 1;
        if (check(mid, x)) {
            ans = mid;
            r = mid - 1;
        } else l = mid + 1;
    }
    return ans;
}

int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        int n, m;
        scanf("%d%d", &n, &m);
        for (int i = 1; i <= n; i++) {
            scanf("%d", &a[i]);
        }
        ll ret = 0;
        for (int i = 1; i <= n; i++) {
            scanf("%d", &b[i]);
            ret += b[i] / a[i]; //ret就是k2
            f[a[i]][b[i] % a[i]]++;
        }
        for (int i = 1; i < 1005; i++) {
            for (int j = i - 1; j >= 0; j--) {
                f[i][j] += f[i][j + 1];
            }
        }
        int p, x, y;
        while (m--) {
            scanf("%d", &p);
            if (p == 1) {
                scanf("%d%d", &x, &y);
                ret -= b[x] / a[x];
                ret += b[x] / y;
                for (int i = b[x] % a[x]; i >= 0; i--)
                    f[a[x]][i]--;
                for (int i = b[x] % y; i >= 0; i--)
                    f[y][i]++;
                a[x] = y;
            } else if (p == 2) {
                scanf("%d%d", &x, &y);
                ret -= b[x] / a[x];
                ret += y / a[x];
                for (int i = b[x] % a[x]; i >= 0; i--)
                    f[a[x]][i]--;
                for (int i = y % a[x]; i >= 0; i--)
                    f[a[x]][i]++;
                b[x] = y;
            } else {
                ll k;
                scanf("%lld", &k);
                printf("%lld\n", sol(ret + k));
            }
        }
        memset(f, 0, sizeof(f));
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值