• HDU6274 Master of Sequence


    时间限制: 10 Sec  内存限制: 128 MB
    提交: 98  解决: 32
    [提交] [状态] [命题人: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 

    还是菜,训练的时候只想到二分,把a相同的项合并到一起来优化时间复杂度。但是没想出来怎么解决合并后向下取整的问题。

    $frac{t-b_i}{a_i}$中令$t=k_1*a_i+c_1$,$b_i=k_2*a_i+c_2$,则$frac{t-b_i}{a_i} = frac{k_1*a_i+c_1-k_2*a_i-c_2}{a_i}$

    我们只需记录所有$k_2$的和,然后t对于每一个ai计算$c_1<c_2$的情况(记为s),$sum_{i=1}^{1000} k_{1_i} + sum_{i=1}^{1000} k_{2_i} - sum_{i=1}^{1000} s_i$即为S(t)的值

    #include "bits/stdc++.h"
    
    using namespace std;
    typedef long long ll;
    const int maxn = 1e6;
    int a[maxn], b[maxn];
    int f[1100][1100];//f[x][y]表示对于所有a[i]==x时的b[i]%a[i]余数大于等于y的数目
    
    bool check(ll t, ll s) {
        ll ret = 0;
        for (int i = 1; i <= 1000; i++) {
            ret += t / i * f[i][0];
            ret -= f[i][t % i + 1];
        }
        return ret >= s;
    }
    
    
    int main() {
       // freopen("input.txt", "r", stdin);
        int _, n, m;
        scanf("%d", &_);
        while (_--) {
            ll ret = 0;
            scanf("%d %d", &n, &m);
            memset(f, 0, sizeof(f));
            for (int i = 1; i <= n; i++)
                scanf("%d", &a[i]);
            for (int i = 1; i <= n; i++) {
                scanf("%d", &b[i]);
                ret += b[i] / a[i];
                f[a[i]][b[i] % a[i]]++;
            }
            for (int i = 1; i <= 1000; i++) {
                for (int j = i - 1; j >= 0; j--) {
                    f[i][j] += f[i][j + 1];
                }
            }
    
    
            int swi, x, y;
            while (m--) {
                scanf("%d %d", &swi, &x);
                if (swi == 1) {
                    scanf("%d", &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 (swi == 2) {
                    scanf("%d", &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 l = 1, r = 1e13, mid;
                    while (l < r) {
                        mid = (l + r) >> 1;
                        if (check(mid, ret + x))
                            r = mid;
                        else
                            l = mid + 1;
                    }
                    printf("%lld
    ", l);
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    快速排序和归并排序的迭代实现
    Longest Increasing Subsequence Review
    IOCCC 1987 最佳单行代码解读
    C++类的成员函数对应的链接器符号的解析
    Scalable Global ID Generator Design
    欧拉回路 (Euler Circuit) POJ 1780
    深入理解函数内静态局部变量初始化
    memcached 线程模型
    类的加载与ClassLoader的理解
    字符集常见码表说明
  • 原文地址:https://www.cnblogs.com/albert-biu/p/10661069.html
Copyright © 2020-2023  润新知