Educational Codeforces Round 22E
给你n和k,n个数,每个数范围1e5,m次查询,每次查询区间(l,r),在区间中的每个数,如果超过k次只算k次,否则算原来的次数,求总次数,强制在线
解法:线段树维护区间中每个数经过k次到达的点pos,然后其中pos<=r的就是能满足条件的加到答案上即可
因为假设原来的点是x(l<=x<=r),经过k次是pos(l<=pos<=r),那么此时我们不能同时算这两个点的贡献,所以只需要算后一个点的贡献,当pos的经过k次到达的点也<=r时,那么pos点也不能算进来,以此类推
线段树每个区间维护一个vector,vector要sort一下,然后查询lowerbound,复杂度O(nlognlogn)
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define mod 1000000007
#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pil pair<int,ll>
#define pii pair<int,int>
#define ull unsigned long long
#define base 1000000000000000000
#define fio ios::sync_with_stdio(false);cin.tie(0) using namespace std; const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=0x3f3f3f3f,INF=0x3f3f3f3f3f3f3f3f; int a[maxn],ans[maxn],num[maxn];
int Next[maxn],id[maxn];
vector<int>v[N];
void pushup(int rt)
{
v[rt].clear();
// v[rt].resize(v[rt<<1].size()+v[rt<<1|1].size());
for(int i=;i<v[rt<<].size();i++)
v[rt].pb(v[rt<<][i]);
for(int i=;i<v[rt<<|].size();i++)
v[rt].pb(v[rt<<|][i]);
sort(v[rt].begin(),v[rt].end());
}
void build(int l,int r,int rt)
{
if(l==r)
{
v[rt].pb(ans[l]);
return ;
}
int m=(l+r)>>;
build(ls);build(rs);
pushup(rt);
}
int query(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
// for(int i=0;i<v[rt].size();i++)
// printf("%d ",v[rt][i]);
int p=upper_bound(v[rt].begin(),v[rt].end(),R)-v[rt].begin();
return p;
}
int m=(l+r)>>,ans=;
if(L<=m)ans+=query(L,R,ls);
if(m<R)ans+=query(L,R,rs);
return ans;
}
int main()
{
int n,k;
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
ans[i]=id[i]=n+;
}
for(int i=n;i>=;i--)
{
Next[i]=id[a[i]];
id[a[i]]=i;
}
memset(id,,sizeof id);
for(int i=n;i>=;i--)id[a[i]]=i;
for(int i=;i<=n;i++)
{
if(num[a[i]]==k)
{
ans[id[a[i]]]=i;
id[a[i]]=Next[id[a[i]]];
}
else num[a[i]]++;
}
// for(int i=1;i<=n;i++)
// printf("%d ",ans[i]);
// puts("");
build(,n,);
// printf("%d\n",2-1+1-query(1,2,1,n,1));
int q,last=;
scanf("%d",&q);
while(q--)
{
int l,r;
scanf("%d%d",&l,&r);
l=(l+last)%n+,r=(r+last)%n+;
if(l>r)swap(l,r);
last=r-l+-query(l,r,,n,);
printf("%d\n",last);
}
return ;
}
/******************** ********************/
Educational Codeforces Round 22E的更多相关文章
- [Educational Codeforces Round 16]E. Generate a String
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...
- [Educational Codeforces Round 16]D. Two Arithmetic Progressions
[Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...
- [Educational Codeforces Round 16]C. Magic Odd Square
[Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...
- [Educational Codeforces Round 16]B. Optimal Point on a Line
[Educational Codeforces Round 16]B. Optimal Point on a Line 试题描述 You are given n points on a line wi ...
- [Educational Codeforces Round 16]A. King Moves
[Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...
- Educational Codeforces Round 6 C. Pearls in a Row
Educational Codeforces Round 6 C. Pearls in a Row 题意:一个3e5范围的序列:要你分成最多数量的子序列,其中子序列必须是只有两个数相同, 其余的数只能 ...
- Educational Codeforces Round 9
Educational Codeforces Round 9 Longest Subsequence 题目描述:给出一个序列,从中抽出若干个数,使它们的公倍数小于等于\(m\),问最多能抽出多少个数, ...
- Educational Codeforces Round 37
Educational Codeforces Round 37 这场有点炸,题目比较水,但只做了3题QAQ.还是实力不够啊! 写下题解算了--(写的比较粗糙,细节或者bug可以私聊2333) A. W ...
- Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship
Problem Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...
随机推荐
- Java 之 GUI 编程
GUI (Graphical User Interface, 图形用户接口) CLI (Command line User Interface, 命令行用户接口) Java 为 GUI 提供的对象都存 ...
- Type Java compiler level does not match the version of the installed Java project facet.项目内容没错但是项目上报错,不影响运行
1.Window->Show View->Problems 2.在项目上右键properties->project Facets->修改右侧的version 保持一致 3.w ...
- TempData、ViewData和ViewBag异同
Data ViewData ViewBag都可以用来保存数据. 它们之间的区别如下: TempData:保存在Session中,Controller每次执行请求的时候,会从Session中先获取Tem ...
- django-应用中和amdin使用富文本编辑器kindeditor
文章描述.新闻详情和产品介绍等,都需要大量的文字描述信息或图片.视频.文字的编辑等,这个时候我们就需要介绍第三方富文本编辑器. 今天介绍的是django中绑定和应用kindeditor编辑器: 效果如 ...
- Linux运维工程师:30道面试题整理
1.linux 如何挂在 windows 下的共享目录 mount.cifs //192.168.1.3/server /mnt/server -o user=administrator,pass=1 ...
- HDU1838:Chessboard(线性dp)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1838 这题也挺不错的.首先题目说了,棋盘的右下角一定是'1',另外棋盘里面至少包含一个1,所以最小值是1, ...
- getElementsByClassName - 兼容详细介绍
概述 JavaScript中getElementsByClassName()方法IE8及以下不支持.本文通过使用正则表达式实 现1个兼容方案. 本文内容分为3个部分. 浏览器原生getElements ...
- lucene实现初级搜索引擎
一.系统设计 搜索引擎项目代码主要分为三个部分,第一部分是构建索引,全文检索:第二部分是输入问题,对问题进行分词.提取关键词.关键词扩展:第三部分是将搜索结果输出到GUI图形用户界面. 二.搜索引擎 ...
- C/C++中的输出对齐设置
输出对齐有两个方面,一是输出宽度,一是左对齐还是又对齐. 在C++里面,默认是右对齐,可以通过cout.setf(std::ios::left)调整为左对齐,而且这种调整是全局的 ,一次设置,后面都有 ...
- Notepad++ c编译环境 64
准备: mingw64(我是从西西软件园下的) 个人微盘共享地址: http://url.cn/24RAhTf notepad++ 安装 mingw64 系统path路径(bin目录下) Notepa ...