传送门

Time Limit: 3 Sec  Memory Limit: 128 MB

Description

Alice likes playing games. So she will take part in the movements of M within N days, and each game is represented in an integer between 1 and M. Roommates have Q magic questions: How many different kinds of games does Alice participate between Lth day and Rth day(including Lth day and Rth day)?

Input

You will be given a number of cases; each case contains blocks of several lines. The first line contains 2 numbers of N and M. The second line contains N numbers implying the game numbers that Alice take part in within N days. The third line contains a number of Q. Then Q lines is entered. Each line contain two numbers of L and R.

1≤N,M,Q≤100000

Output

There should be Q output lines per test case containing Q answers required.

Sample Input

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

Sample Output

3 2 3

HINT


这是今年校赛的K题,一道经典题目,但现场没A。

在线可以用主席树,目前还不会。有一个巧妙的利用数状数组的离线解法,比较好写。

要点是:

1.将查询按右端点从小到大排序。

2.将每个数上一次出现的位置记录下来。当这个数再次出现时,将它上次出现位置上的计数消除。

Implementation:

主体是个双指针。

#include <bits/stdc++.h>
using namespace std;const int N(1e5+5);
int n, m, q, a[N], pos[N], bit[N], ans[N];void add(int x, int v){for(; x<=n; bit[x]+=v, x+=x&-x);
}int sum(int x){int res=0;for(; x; res+=bit[x], x-=x&-x);return res;
}struct P{int l, r, id;P(int l, int r, int id):l(l),r(r),id(id){}P(){};bool operator<(const P&b)const{return r<b.r;}
}p[N];int main(){// ios::sync_with_stdio(false);for(; ~scanf("%d%d", &n, &m); ){for(int i=1; i<=n; i++) scanf("%d", a+i);scanf("%d", &q);for(int l, r, i=0; i<q; i++) scanf("%d%d", &l, &r), p[i]={l, r, i};sort(p, p+q);   //error-pronememset(bit, 0, sizeof(bit));memset(pos, 0, sizeof(pos));for(int i=1, j=0, k; j<q&&i<=n; ){  //error-pronefor(; i<=p[j].r; i++){if(pos[a[i]]) add(pos[a[i]], -1);pos[a[i]]=i;add(i, 1);}for(k=j; k<q&&p[k].r==p[j].r; k++)  //error-proneans[p[k].id]=sum(p[k].r)-sum(p[k].l-1);j=k;}for(int i=0; i<q; i++) printf("%d\n", ans[i]); //error-prone
    }return 0;
}

转载于:https://www.cnblogs.com/Patt/p/5410711.html

DLUTOJ #1394 Magic Questions相关推荐

  1. magic feature_停止将PostgREST称为“ MAGIC”!

    magic feature by Ruslan Talpă 通过RuslanTalpă 停止将PostgREST称为" MAGIC"! (Stop calling PostgRES ...

  2. 2019 ICPC南昌网络赛 E题 Magic Master 【双向队列】

    传送门:E.Magic Master John is not only a magic master but also a shuffling master. Famous though he is, h ...

  3. Dx bad class file magic (cafebabe) or version (0033.0000) 导致 NoClassDefFoundError 的问题

    在做一个二维码扫描的app , 在项目中需要添加so 文件,以及配套的 *.jar 文档 . 由于项目是远程合作共同开发的 , 项目整体进展很好,等扫码功能解决之后, 他那边同步code编译死活不行, ...

  4. 2020 Gartner Magic Quadrant for Analytics and BI Platforms

    2020 Gartner Magic Quadrant for Analytics and BI Platforms 2020 Gartner 数据分析与BI平台魔力象限图 Published 11 ...

  5. Avegant “Light Field” Display – Magic Leap at 1/100th the Investment?

    [转] http://www.kguttag.com/2017/03/09/avegant-light-field-display-magic-leap/ Surprised at CES 2017 ...

  6. Educational Codeforces Round 9 F. Magic Matrix 最小生成树

    F. Magic Matrix 题目连接: http://www.codeforces.com/contest/632/problem/F Description You're given a mat ...

  7. 我花了三个小时写了一道题的六千字题解....(POJ 2888 Magic Bracelet)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 前置知识:小学生都能看懂的群论从入门到升天教程 <群论全家桶> 一道简单的题目 Probl ...

  8. R使用LSTM模型构建深度学习文本分类模型(Quora Insincere Questions Classification)

    R使用LSTM模型构建深度学习文本分类模型(Quora Insincere Questions Classification) Long Short Term 网络-- 一般就叫做 LSTM --是一 ...

  9. 安装报错_RG Magic Bullet安装报错修复方法

    RG Magic Bullet安装报错修复方法 安装和谐版红巨星插件发现有很大的概率报错.这几天重装电脑被这个插件搞得头大.网上查了一些排除错误的方法,基本错误可以靠删除一些重复的文件夹和老版本来修复 ...

  10. magic系统将来能升鸿蒙,荣耀30和V30将首批搭载Magic UI 4.0 后续可升级鸿蒙系统

    腾讯科技讯 9月10日下午15点30分,华为消费者业务软件部总裁王成录在华为开发者大会上发表了题为<连接无限可能-全场景终端软件发布>的主题演讲,EMUI 11和Magic UI 4.0同 ...

最新文章

  1. Windows下MySQL安装
  2. 命令行以及git基础使用
  3. 解决linux下内网域名的ping结果和nslookup结果不一致
  4. 神经网络优化(二) - 滑动平均
  5. 模拟导入系统通讯录5000+手机号 校验大量数据处理
  6. Hadoop 系列之 1.0 和2.0 架构
  7. Python中的全局变量与局部变量2
  8. 【联系】—— Beta 分布与二项分布、共轭分布
  9. 23. stdin, stdout, stderr
  10. 4. Javascript 函数
  11. DDOS 攻击工具
  12. git mac 可视化diff_Git可视化比较工具P4Merge
  13. MES系统的功能详细以及应用价值介绍
  14. 通过串口波特率计算数据传输速率(每秒字节数)
  15. 程序员职业发展路径图:从菜鸟工程师到高级架构师(转)
  16. Arduino学习笔记(2)-- Arduino基本函数
  17. 拼多多推广效果不好,哪里需要改善?
  18. 图片缩放库 Photoview 和 Gif 控件 GifView 的使用
  19. JFreeChart饼状图显示数字
  20. Logback 快速定位用户在一次请求中的所有日志

热门文章

  1. 软件工程 第四章 概要设计
  2. model.load_state_dict(checkpoint[‘state_dict‘]) KeyError: ‘state_dict‘
  3. 强大的PyTorch:10分钟让你了解深度学习领域新流行的框架
  4. cv2.error: OpenCV(4.5.3) :-1: error: (-5:Bad argument) in function ‘line‘ 报错解决
  5. Ubuntu16.04 设置自启动脚本,系统重启自动执行自定义脚本任务
  6. 双一流2020年调整时间_“双一流”调整名单预测,985大学两升一降,两校退出名校阵营...
  7. python django 网页 html \n不换行
  8. 数学建模(一)层次分析法
  9. 在sql语句中该如何处理null值
  10. Linux添加浮动路由,Linux路由配置详情