Educational Codeforces Round 22 E. Army Creation
Educational Codeforces Round 22 E. Army Creation
题意:求区间[L,R]内数字次数不超过k次的这些数字的数量的和
思路:和求区间内不同数字的数量类似,由于这里强制在线,主席树或者整体二分来做这道题,把pre[i]前驱改一下 变成前k个a[i]的位置
对于每个点,在root[i]这棵树中i处+1,pre[i]处-1,对于查询[L,R]就是查询root[R]中区间[L,R]的值
#include<bits/stdc++.h>
#define LL long long
#define P pair<int,int>
#define ls T[i].lc
#define rs T[i].rc
using namespace std;
const int N = 1e5 + 10;
int read(){
int x = 0;
char c = getchar();
while(c < '0' || c >'9') c = getchar();
while(c >= '0' && c <= '9') x = x * 10 + c - 48,c = getchar();
return x;
}
int n, k, q;
queue<int>qu[N];
int a[N];
struct node{
int lc,rc,cnt;
}T[N * 50];
int tot,root[N];
void update(int &i,int l,int r,int pos,int val){
T[++tot] = T[i];
i = tot;
T[i].cnt += val;
if(l >= r) return ;
int m = l + r >> 1;
if(pos <= m) update(ls,l,m,pos,val);
else update(rs,m+1,r,pos,val);
}
int query(int i,int pos,int l,int r){
if(r < pos) return 0;
if(l >= pos) return T[i].cnt;
int m = l + r >> 1;
int ans = 0;
if(pos <= m) ans += query(ls,pos,l,m);
return query(rs,pos,m+1,r) + ans;
}
int main()
{
n = read(),k = read();
tot = 0,root[0] = 0;
for(int i = 1;i <= n;i++){
a[i] = read();
root[i] = root[i-1];
update(root[i],1,n,i,1);
qu[a[i]].push(i);
if((int)qu[a[i]].size()>k){
int top = qu[a[i]].front();
update(root[i],1,n,top,-1);
qu[a[i]].pop();
}
}
q = read();
int l,r,last = 0;
while(q--){
l = (read() + last)%n + 1;
r = (read() + last)%n + 1;
if(l > r) swap(l,r);
printf("%d\n",last=query(root[r],l,1,n));
}
return 0;
}
Educational Codeforces Round 22 E. Army Creation的更多相关文章
- Educational Codeforces Round 22 E. Army Creation(分块好题)
E. Army Creation time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- Educational Codeforces Round 22 E. Army Creation 主席树 或 分块
http://codeforces.com/contest/813/problem/E 题目大意: 给出长度为n的数组和k, 大小是1e5级别. 要求在线询问区间[l, r]权值, 权值定义为对于 ...
- [Educational Codeforces Round#22]
来自FallDream的博客,未经允许,请勿转载,谢谢. 晚上去clj博客逛来逛去很开心,突然同学提醒了一下,发现cf已经开始40分钟了,慌的一B,从B题开始写,写完了B到E最后收掉了A,结果太着急B ...
- Educational Codeforces Round 22 补题 CF 813 A-F
A The Contest 直接粗暴贪心 略过 #include<bits/stdc++.h> using namespace std; int main() {//freopen(&qu ...
- Educational Codeforces Round 22.B 暴力
B. The Golden Age time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- Educational Codeforces Round 22 B. The Golden Age(暴力)
题目链接:http://codeforces.com/contest/813/problem/B 题意:就是有一个数叫做不幸运数,满足题目的 n = x^a + y^b,现在给你一个区间[l,r],让 ...
- 【Educational Codeforces Round 22】
又打了一场EDU,感觉这场比23难多了啊…… 艹还是我太弱了. A. 随便贪心一下. #include<bits/stdc++.h> using namespace std; ,ans=- ...
- Educational Codeforces Round 40千名记
人生第二场codeforces.然而遇上了Education场这种东西 Educational Codeforces Round 40 下午先在家里睡了波觉,起来离开场还有10分钟. 但是突然想起来还 ...
- [Educational Codeforces Round 63 ] D. Beautiful Array (思维+DP)
Educational Codeforces Round 63 (Rated for Div. 2) D. Beautiful Array time limit per test 2 seconds ...
随机推荐
- 第33题:LeetCode255 Verify Preorder Sequence in Binary Search Tree 验证先序遍历是否符合二叉搜索树
题目 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. 考点 1.BST 二叉搜索树 2.递归 思路 1.后序 ...
- docker基础——关于安装、常用指令以及镜像制作初体验
为什么使用docker docker就是一个轻量级的虚拟机,他解决的是服务迁移部署的时候环境配置问题.比如常见的web服务依赖于jdk.Tomcat.数据库等工具,迁移项目就需要在新的机器重新配置这些 ...
- Spring Security 简介
本文引自:https://blog.csdn.net/xlecho/article/details/80026527 在 Web 应用开发中,安全一直是非常重要的一个方面.安全虽然属于应用的非功能性需 ...
- 【shopex】真正可用的app开发机制
shopex的app开发机制详解 网上流传的shopex4.8.5的app开发教程,不仅说得不明不白,而且由于版本问题,照着做根本是做不成的. 知其然,亦要知其所以然. shopex提供了的一个干 ...
- VS2013未能正确加载的问题【转载】
今天使用电脑,关机重启时,WINDOWS提示“正在配置中,请勿关机” 的提醒,等重启后,打开VS2013就提示了未加载成功的问题,如下图: 我的解决方法是:找到VS2013开发人员命令提示:在窗口中输 ...
- 解决scp命令pemission denied,please try again的问题
问题描述:输入命令scp a.txt root@192.168.0.105:/tmp(将当前目录下的文件a.txt复制到服务器IP为192.168.0.105的root用户的/tmp/目录下),结果会 ...
- Python学习笔记:PEP8常用编程规范
PEP8编码规范是一种非常优秀的编码规范,也得到了Python程序员的普遍认可,如果实践中或者项目中没有统一的编码规范,建议尽量遵循PEP8编码规范,当然如果项目中已经有了自身的编码规范,应当优先遵循 ...
- 汉罗塔问题——Python
汉罗塔问题就是一个循环的过程:* (有两种情况) 如果被移动盘只有一个盘子,可以直接移动到目的盘 但是被移动盘有多个盘子,就先需要将上面的n-1个盘子通过目的盘移动到辅助盘,然后将被移动盘最下面一个盘 ...
- C语言进阶—— 逻辑运算符分析15
印象中的逻辑运算符: ---学生:老师,在我的印象中,逻辑运算符用在条件判断的时候,真挺简单的,还有必要深究吗? ---老师:逻辑运算符确实在条件判断的时候用的比较多,但是并不能说简单... 请思考下 ...
- Python os.walk() 简介
Table of Contents 1. os.walk目录遍历 1.1. os.walk 1.2. 例子 1.2.1. 测试topdown 1.2.2. 运行时修改遍历目录 2. 参考资料 os.w ...