• 第十三届蓝桥杯 C++ C 组省赛 J 题——重复的数 (AC)


    1.重复的数

    1.题目描述

    给定一个数列 A = ( a 1 , a 2 , ⋯   , a n ) A=\left(a_{1}, a_{2}, \cdots, a_{n}\right) A=(a1,a2,,an), 给出若干询问, 每次询问某个区间 [ l i , r i ] \left[l_{i}, r_{i}\right] [li,ri] 内恰好出现 k i k_{i} ki次的数有多少个。

    2.输入格式

    输入第一行包含一个整数 n n n 表示数列长度。

    第二行包含 n n n 个整数 a 1 , a 2 , ⋯   , a n a_{1}, a_{2}, \cdots, a_{n} a1,a2,,an , 表示数列中的数。

    第三行包含一个整数 m m m 表示询问次数。

    接下来 m m m 行描述询问, 其中第 i i i 行包含三个整数 l i , r i , k i l_{i}, r_{i}, k_{i} li,ri,ki表示询问 [ l i , r i ] \left[l_{i}, r_{i}\right] [li,ri] 区 间内有多少数出现了 k i k_{i} ki次。

    3.输出格式

    输出 m m m 行, 分别对应每个询问的答案。

    4.样例输入

    3
    1 2 2
    5
    1 1 1
    1 1 2
    1 2 1
    1 2 2
    1 3 2

    5.样例输出

    1
    0
    2
    0
    1

    6.数据范围

    1 ≤ n , m ≤ 100000 , 1 ≤ a 1 , a 2 , ⋯ , a n ≤ 100000 , 1 ≤ l i ≤ r i ≤ n , 1 ≤ k i ≤ n 。  1≤n,m≤100000,1≤a1 ,a2 ,⋯,an≤100000,1≤li≤ r_{i} \leq n, 1 \leq k_{i} \leq n_{\text {。 }} 1n,m100000,1a1,a2,,an100000,1lirin,1kin 

    2.解题思路

    莫队的模板题,学过的能一眼看出来,没学过下面链接跳转学习一下:
    莫队

    3.Ac_code

    #include
    using namespace std;
    typedef long long LL;
    typedef unsigned long long uLL;
    typedef pair<int, int> PII;
    #define pb(s) push_back(s);
    #define SZ(s) ((int)s.size());
    #define ms(s,x) memset(s, x, sizeof(s))
    #define all(s) s.begin(),s.end()
    const int inf = 0x3f3f3f3f;
    const int mod = 1000000007;
    const int N = 200010;
    
    // 莫队
    int n, q, c[N], cnt[N], freq[N], ans[N];
    int sq;
    struct query
    {
    	int l, r, k, id;
    	bool operator<(const query &o) const
    	{
    		if (l / sq != o.l / sq)
    			return l < o.l;
    		if (l / sq & 1)
    			return r < o.r;
    		return r > o.r;
    	}
    } que[N];
    void add(int x) {
    	cnt[freq[c[x]]]--;
    	freq[c[x]]++;
    	cnt[freq[c[x]]]++;
    };
    void del (int x) {
    	cnt[freq[c[x]]]--;
    	freq[c[x]]--;
    	cnt[freq[c[x]]]++;
    };
    void solve()
    {
    	cin >> n;
    	sq = sqrt(n);
    	for (int i = 1; i <= n; ++i) {
    		cin >> c[i];
    	}
    	cin >> q;
    	for (int i = 0; i < q; ++i) {
    		int l, r, k;
    		cin >> l >> r >> k;
    		que[i] = {l, r, k, i};
    	}
    	int B = 500;
    	sort(que, que + q);
    	int l = 1, r = 0;
    	for (int i = 0; i < q; ++i) {
    		while (r < que[i].r) r++, add(r);
    		while (l > que[i].l) l--, add(l);
    		while (r > que[i].r) del(r), r--;
    		while (l < que[i].l) del(l), l++;
    		ans[que[i].id] = cnt[que[i].k];
    	}
    	for (int i = 0; i < q; ++i) {
    		cout << ans[i] << '\n';
    	}
    }
    int main()
    {
    	ios_base :: sync_with_stdio(false);
    	cin.tie(nullptr);
    	int t = 1;
    	while (t--)
    	{
    		solve();
    	}
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76

  • 相关阅读:
    基于SSM的电影网站设计与实现
    RIP路由配置
    华为机试 - 学生方阵
    在Java中使用io实现TCP协议编程
    渗透测试-WEB安全梳理-中间件(apache、IIS、tomcat、weblogic、websphere、jboos、nginx)
    Linux 网络编程 tcp server 笔记
    第三十六章 使用 CSP 进行基于标签的开发 - 使用尽可能少的#server和#call调用
    Unity websocket
    2022年SQL经典面试题总结(带解析)
    限流的几种方式
  • 原文地址:https://blog.csdn.net/m0_57487901/article/details/128164839