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 ...
随机推荐
- Vscode插件--微信小程序格式化以及高亮组件wxml-vscode
wxml-vscode wxml-vscode 仓库 提问题 安装 通过 F1 或者 CMD + Shift + P 输入 install. 选择: Install Extension. 特性 格式化 ...
- OCCI的迭代修改
传统的在执行多行DML(INSERT.UPDATE.DELETE)时,我们是多次调用executeUpdate():注意!当我们调用一次此函数时,则执行一次网络往返,当数据量大时则效率非常低.不过 O ...
- C#+Winform开发窗体程序
学习笔记 第一章:winform基础 一.概述 1.Windows Form(简称WinForm) 是微软.NET平台下用于开发"图形界面"应用程序的组件. 2.C/S架构 客户机 ...
- hosts 文件的位置及作用
一.位置 1.Window系统位置 C:\Windows\System32\drivers\etc 2.Linux系统位置 /etc/hosts 二.作用 综述:Hosts文件中指定了域名和IP地址的 ...
- Leetcode 653. 两数之和 IV - 输入 BST
题目链接 https://leetcode.com/problems/two-sum-iv-input-is-a-bst/description/ 题目描述 给定一个二叉搜索树和一个目标结果,如果 B ...
- Android stadio Switch repository Android stadio切换仓库
Android stadio 有时候,有很多module. 这些module 都有自己的仓库.也就是不在一块.那么,Android stadio 默认管理的就是根git. 如图,画对号的就是默认的. ...
- shell参数 传递
$# 是传给脚本的参数个数 $0 是脚本本身的名字 $1 是传递给该shell脚本的第一个参数 $2 是传递给该shell脚本的第二个参数 $@ 是传给脚本的所有参数的列表 $* 是以一个单字符串显示 ...
- string函数Contains()实例
public bool Contains(string value)如果值参数出现在此字符串内,或者值为空字符串(“”),则为true; 否则为false using System; class Ex ...
- 有哪些值得一读的优秀开源 JS 代码
有哪些值得一读的优秀开源 JS 代码 采纳 首先,没有“必须”读的源代码(我发现我特喜欢说首先……),因为读源代码不是做功课,只有用到或是非常好奇才会去读,当成“日常”去做是没有意义的. 当然有些人会 ...
- 软引用SoftReference
本文介绍对象的强.软.弱和虚引用的概念.应用及其在UML中的表示. 1.对象的强.软.弱和虚引用 在JDK 1.2以前的版本中,若一个对象不被任何变量引用,那么程序就无法再使用这个对象.也就是说,只有 ...