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 ...
随机推荐
- Ansi、GB2312、GBK、Unicode(utf8、16、32)
关于ansi,一般默认为本地编码方式,中文应该是gb编码 他们之间的关系在这边文章里描写的很清楚:http://blog.csdn.net/ldanduo/article/details/820353 ...
- rest_framework之url控制器详解
一 自定义路由(原始方式) from django.conf.urls import url from app01 import views urlpatterns = [ url(r'^books/ ...
- Andrew Ng机器学习总结(自用)
监督学习: 线性回归,逻辑回归,神经网络,支持向量机. 非监督学习: K-means,PCA,异常检测 应用: 推荐系统,大规模机器学习 机器学习系统优化: 偏差/方差,正则化,下一步要进行的工作:评 ...
- blogCMS中出现的错误整理
1.在写日期归档的时候,出现如下错误: not enough values to unpack (expected 2, got 1) 出现这个错误是因为:字符串需要能够split成2份才能赋值给2个 ...
- React:快速上手(5)——掌握Redux(2)
React:快速上手(5)——掌握Redux(2) 本文部分内容参考阮一峰的Redux教程. React-Redux原理 React-Redux运行机制 我觉得这张图清楚地描述React-Redux的 ...
- $python包管理工具pip的安装和使用
pip是python的一个非常好用的包管理工具,可以用来很方便地安装和管理各种三方库,本文对其基本用法进行介绍. 安装pip Windows系统上安装 python 2.7.9及以上版本的window ...
- Sublime Text 3 快捷键 一览
Sublime Text 3 快捷键精华版 Ctrl+Shift+P:打开命令面板 Ctrl+P:搜索项目中的文件 Ctrl+G:跳转到第几行 Ctrl+W:关闭当前打开文件 Ctrl+Shift+W ...
- svn如何给新文件加锁
第一步: 右键subversion(版本控制) 第二部:找到 subversion properties 第二部: add进行添加
- 【Flask】Flask Restful api
### 安装: Flask-Restful需要在Flask 0.8以上的版本,在Python2.6或者Python3.3上运行.通过pip install flask-restful即可安装. ### ...
- Scala:scala的一些简单操作命令
Scala是一门多范式的编程语言,一种类似java的编程语言,设计初衷是实现可伸缩的语言.并集成面向对象编程和函数式编程的各种特性. 不太久之前编程语言还可以毫无疑意地归类成“命令式”或者“函数式”或 ...