zoukankan      html  css  js  c++  java
  • Different Integers 牛客多校第一场只会签到题

    Given a sequence of integers a1, a2, ..., an and q pairs of integers (l1, r1), (l2, r2), ..., (lq, rq), find count(l1, r1), count(l2, r2), ..., count(lq, rq) where count(i, j) is the number of different integers among a1, a2, ..., ai, aj, aj + 1, ..., an.
    输入描述:
    The input consists of several test cases and is terminated by end-of-file.The first line of each test cases contains two integers n and q.The second line contains n integers a1, a2, ..., an.The i-th of the following q lines contains two integers li and ri.
    输出描述:
    For each test case, print q integers which denote the result.

    示例1

    输入
    复制

    3 2
    1 2 1
    1 2
    1 3
    4 1
    1 2 3 4
    1 3

    输出
    复制

    2
    1
    3


    备注:
    * 1 ≤ n, q ≤ 105* 1 ≤ ai ≤ n* 1 ≤ li, ri ≤ n* The number of test cases does not exceed 10.

    这是唯一一题签到题 ,现场找的板子,修改一下过的

    最简单粗暴的方法,把数组扩张一倍

    修改一下查询区间。板子题的变形吧

     1 #include <cstdio>
     2 #include <iostream>
     3 #include <algorithm>
     4 #include <cmath>
     5 #include <cstdlib>
     6 #include <cstring>
     7 #include <vector>
     8 #include <list>
     9 #include <map>
    10 #include <stack>
    11 #include <queue>
    12 using namespace std;
    13 #define ll long long
    14 const int maxn1 = 2000005;
    15 int cur[maxn1];
    16 int then[maxn1];
    17 int ans[maxn1];
    18 int limit, n, m;
    19 struct node {
    20     int l, r, id;
    21 } que[maxn1];
    22 bool cmp(node x, node y) {
    23     if(x.l / limit == y.l / limit)  return x.r < y.r;
    24     return x.l / limit < y.l / limit;
    25 }
    26 void solve() {
    27     int L, R, ans1;
    28     L = R = 0;
    29     ans1 = 0;
    30     for(int i = 1; i <= m; i++) {
    31         while(que[i].l > L) {
    32             then[cur[L]]--;
    33             if(then[cur[L]] == 0)
    34                 ans1--;
    35             L++;
    36         }
    37         while(que[i].r < R) {
    38             then[cur[R]]--;
    39             if(then[cur[R]] == 0)
    40                 ans1--;
    41             R--;
    42         }
    43         while(que[i].l < L) {
    44             L--;
    45             then[cur[L]]++;
    46             if(then[cur[L]] == 1)
    47                 ans1++;
    48         }
    49         while(que[i].r > R) {
    50             R++;
    51             then[cur[R]]++;
    52             if(then[cur[R]] == 1)
    53                 ans1++;
    54         }
    55         ans[que[i].id] = ans1;
    56     }
    57     for(int i = 1; i <= m; i++)
    58         printf("%d
    ", ans[i]);
    59 }
    60 int main() {
    61     while(scanf("%d", &n) != EOF) {
    62         scanf("%d", &m);
    63         memset(cur, 0, sizeof(cur));
    64         memset(then, 0, sizeof(then));
    65         memset(ans, 0, sizeof(ans));
    66         for(int i = 1; i <= n; i++)
    67             scanf("%d", &cur[i]);
    68         for (int i = n + 1 ; i <= 2 * n ; i++)
    69             cur[i] = cur[i - n];
    70         for(int i = 1; i <= m; i++) {
    71             scanf("%d%d", &que[i].l, &que[i].r);
    72             int temp1 = que[i].l, temp2 = que[i].r;
    73             que[i].l = temp2, que[i].r = temp1 + n;
    74             que[i].id = i;
    75         }
    76         limit = (int)(sqrt(2 * n) + 0.5);
    77         memset(then, 0, sizeof(then));
    78         sort(que + 1, que + 1 + m, cmp);
    79         solve();
    80     }
    81     return 0;
    82 }
  • 相关阅读:
    window 10 node.js 安装 2502 2503错误解决方法
    高清方案在手机微信上的一个奇葩问题,当字数变多,会莫名其妙的变大
    非node环境 vue-rouder 学习笔录4 命名视图
    非node环境 vue-rouder 学习笔录3 嵌套路由
    非node环境 vue-rouder 学习笔录2 路由监听和动态路由匹配
    非node环境 vue-rouder 学习笔录1
    VUE入门实例,模版组件用法
    用swiper可隐藏滑动导航和内容滑动联动
    在一次引入vue的时候使用swiper出现swiper无效情况
    [BZOJ 3230]相似子串
  • 原文地址:https://www.cnblogs.com/qldabiaoge/p/9337556.html
Copyright © 2011-2022 走看看