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 ...
随机推荐
- 树梅派3B kali2.0 启用SSH进行远程登录
工具/原料 kali 2.0 ssh SSH连接工具(XShell)or PUTTY vi /etc/ssh/sshd_config 将#PasswordAuthentication no的注释去掉, ...
- LeetCode-177:第N高的薪水
第N高的薪水与第二高的薪水,解题思路是一样的,只要对LeetCode-176的SQL做一下变形,便可以满足这题,详见:https://www.cnblogs.com/zouqf/p/10282392. ...
- 爬虫学习(十九)——Scrapy的学习及其使用
Scrapy框架的介绍 Scrapy,非常的强悍,通过python语言编写的,非常知名的爬虫框架 框架工作流程 框架流程图 基本工作流程; 1.引擎向spiders要url 2.引擎将要爬取的url给 ...
- 两种js方法发起微信支付:WeixinJSBridge,wx.chooseWXPay区别
原文链接:https://www.2cto.com/weixin/201507/412752.html 1.为什么会有两种JS方法可以发起微信支付? 当你登陆微信公众号之后,左边有两个菜单栏,一个是微 ...
- 根据html页面模板动态生成html页面(c#类)
本文转载自:http://www.cnblogs.com/yuanbao/archive/2008/01/06/1027985.html点击打开链接 一直以为动态生成静态页面不好做,昨天在网上找了下, ...
- 宁夏邀请赛F FLOYD
Moving On Firdaws and Fatinah are living in a country with nn cities, numbered from 11 to nn.Each ci ...
- 7-1 寻找大富翁 PTA 堆排序
7-1 寻找大富翁 (25 分) 胡润研究院的调查显示,截至2017年底,中国个人资产超过1亿元的高净值人群达15万人.假设给出N个人的个人资产值,请快速找出资产排前M位的大富翁. 输入格式: 输入首 ...
- Highest Tower 18中南多校第一场H题
一.题意 给出N个方块,要求给出一个方案,使得1. 所有方块都被使用到(题目数据保证这点) 2.所有方块垒成一个塔,且上面的方块宽度小于下面的方块 3.每个方块只能用一次,可以横着或者竖着. n范围5 ...
- Bootstrap3适配IE8浏览器的方法
<!--[if lte IE 8]> <script src="js/respond.min.js"></script> <script ...
- linux c scanf()小解
今天学习了新的内容,关于c语言的scanf()函数. scanf()函数,读取指定格式的值赋值给相应变量.空格(‘ ‘),回车('\n'),TAB是分隔符,轻易不会被读取.还有,该函数的返回值是正确读 ...