2018 Nowcoder Multi-University Training Contest 1

Practice Link

J. Different Integers

题意:
给出\(n\)个数,每次询问\((l_i, r_i)\),表示\(a_1, \cdots, a_i, a_j, \cdots, a_n\)中有多少个不同的数。

思路:
先分别离线求出\(a_1, \cdots a_i\)以及\(a_j, \cdots, a_n\)中有多少个不同的数。
再考虑有多少个数既在\([1, i]\)中也在\([j, n]\)中,再离线做一次。
考虑一个数第一次出现的时候,那么这个数下一次出现的位置以及之后的所有询问区间都要减去一个贡献。

代码:

#include <bits/stdc++.h>
using namespace std;

#define N 100010
int n, q, a[N], b[N], c[N], nx[N], ans[N];
struct node {
    int l, r, id;
    node() {}
    void scan(int id) {
        this->id = id;
        scanf("%d%d", &l, &r);
        if (l >= r) {
            l = 1;
            r = 2;
        }
    }
}qrr[N];

struct BIT {
    int a[N];
    void init() {
        memset(a, 0, sizeof a);
    }
    void update(int x, int v) {
        for (; x > 0; x -= x & -x) {
            a[x] += v;
        }
    }
    int query(int x) {
        int res = 0;
        for (; x < N; x += x & -x) {
            res += a[x];
        }
        return res;
    }
    int query(int l, int r) {
        return query(l) - query(r + 1);
    }
}bit; 

int main() {
    while (scanf("%d%d", &n, &q) != EOF) {
        for (int i = 1; i <= n; ++i) {
            scanf("%d", a + i);
        }
        for (int i = 1; i <= q; ++i) {
            qrr[i].scan(i);
        }
        if (n == 1) {
            for (int i = 1; i <= q; ++i) {
                printf("1\n");
            }
            continue;
        }
        sort(qrr + 1, qrr + 1 + q, [&](node x, node y) {
            return x.l < y.l;
        });
        memset(b, 0, sizeof b);
        for (int i = 1, j = 1, k = 0; i <= q; ++i) {
            while (j <= n && j <= qrr[i].l) {
                if (b[a[j]] == 0) {
                    b[a[j]] = 1;
                    ++k;
                }
                ++j;
            }
            ans[qrr[i].id] = k;
        }
        sort(qrr + 1, qrr + 1 + q, [&](node x, node y){
            return x.r > y.r;
        });
        memset(b, 0, sizeof b);
        for (int i = 1, j = n, k = 0; i <= q; ++i) {
            while (j >= 1 && j >= qrr[i].r) {
                if (b[a[j]] == 0) {
                    b[a[j]] = 1;
                    ++k;
                }
                --j;
            }
            ans[qrr[i].id] += k;
        }
        memset(b, 0, sizeof b);
        for (int i = 1; i <= n; ++i) {
            nx[i] = n + 1;
        }
        for (int i = n; i >= 1; --i) {
            c[i] = nx[a[i]];
            if (nx[a[i]] == n + 1) {
                nx[a[i]] = i;
            }
        }
        bit.init();
        sort(qrr + 1, qrr + 1 + q, [&](node x, node y){
            return x.l < y.l;
        });
        for (int i = 1, j = 1; i <= q; ++i) {
            while (j <= n && j <= qrr[i].l) {
                if (b[a[j]] == 0) {
                    bit.update(c[j], -1);
                    b[a[j]] = 1;
                }
                ++j;
            }
            ans[qrr[i].id] += bit.query(qrr[i].r, n);
        }
        for (int i = 1; i <= q; ++i) {
            printf("%d\n", ans[i]);
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/Dup4/p/11108577.html

时间: 2024-11-05 22:00:24

2018 Nowcoder Multi-University Training Contest 1的相关文章

HDU 2018 Multi-University Training Contest 3 Problem A. Ascending Rating 【单调队列优化】

任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6319 Problem A. Ascending Rating Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 5943    Accepted Submission(s): 2004 Problem Description Before

杭电2018多校第一场(2018 Multi-University Training Contest 1) 1001.Maximum Multiple (HDU6298)-数学思维题(脑子是个好东西,可惜我没有)

暑假杭电多校第一场,这一场是贪心场,很多贪心的题目,但是自己太菜,姿势挫死了,把自己都写吐了... 2018 Multi-University Training Contest 1 HDU6298.Maximum Multiple 题目意思就是给你一个n,找出来三个数x,y,z, 使得n=x+y+z,而且x,y,z都是n的因数,并且x*y*z为最大值,让你输出来x*y*z的最大值.如果没有满足条件的情况就输出-1. 由1=1/2+1/3+1/6=1/3+1/3+1/3=1/2+1/4+1/4,所

2018 Multi-University Training Contest 4

Problem D. Nothing is Impossible Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 504    Accepted Submission(s): 302 Problem Description m students, including Kazari, will take an exam tomorrow

HDU 6396 Swordsman --------2018 Multi-University Training Contest 7 (模拟+读入挂)

原题地址: 打怪升级 一开始有N个怪物:主角有K个能力:只有K个能力都击败怪物才能斩杀怪物并获得K个能力的增值:问最多能杀几个怪物: 做法: 用优先队列把怪物能力装进去:能力小放前面: 最重要的是数据量要用读入挂才能过:(读入挂太神奇了!!) Swordsman Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2049    Acce

2018 Multi-University Training Contest 3

2018 Multi-University Training Contest 2 题解 A - Problem A. Ascending Rating 题目描述:给定一个序列,分别求出所有长度为\(m\)的区间的\(maxrating, count\),对于每个长度为\(m\)的区间,一开始\(maxrating=-1, count=0\),然后从左往右扫,扫到一个大于\(maxrating\)的值时,\(count+1, maxrating=\)那个数. solution 从左往右做,用单调队

2018 Multi-University Training Contest 1 Distinct Values 【贪心 + set】

任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6301 Distinct Values Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5312    Accepted Submission(s): 1823 Problem Description Chiaki has an array of

2018 Multi-University Training Contest 4 Problem L. Graph Theory Homework 【YY】

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6343 Problem L. Graph Theory Homework Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1536    Accepted Submission(s): 830 Problem Description Ther

2018 Multi-University Training Contest 4 Problem K. Expression in Memories 【模拟】

任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6342 Problem K. Expression in Memories Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 2150    Accepted Submission(s): 772Special Judge Problem De

2018 Multi-University Training Contest 4 Problem J. Let Sudoku Rotate 【DFS+剪枝+矩阵旋转】

任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6341 Problem J. Let Sudoku Rotate Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1363    Accepted Submission(s): 717 Problem Description Sudoku i

HDU4888 Redraw Beautiful Drawings(2014 Multi-University Training Contest 3)

Redraw Beautiful Drawings Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description Alice and Bob are playing together. Alice is crazy about art and she has visited many museums around the world. She has