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 ...
随机推荐
- js基础练习--控制多组图片切换
js基础练习题,一个按钮控制两组图片的切换,做这题的时候我忽然想到了将num1.mun2……都存放在一个数组中,根据索引值匹配到对应相应组的图片,这样不管有多少组图片都简单的搞定切换,可惜js基础都没 ...
- 对比MySQL,你究竟在什么时候更需要MongoDB(转)
译文:对比MySQL,你究竟在什么时候更需要MongoDB 原文链接: When Should I Use MongoDB rather than MySQL (or other RDBMS): Th ...
- 吴超老师课程---ZooKeeper介绍和集群安装
1.ZooKeeper 1.1 zk可以用来保证数据在zk集群之间的数据的事务性一致.2.如何搭建ZooKeeper服务器集群 2.1 zk服务器集群规模不小于3个节点,要求各服务器之间系 ...
- hibernate 一对多、多对多的配置
一对多 <class name="Question" table="questions" dynamic-insert="true" ...
- Java架构搜集
1. 2.
- 左连接、右连接、内连接和where
首先可以看下w3school写的关于join的介绍: http://www.w3school.com.cn/sql/sql_join.asp on是关联条件,where是筛选条件 数据库在通过连接两张 ...
- Android bluetooth介绍
Android bluetooth介绍(一):基本概念及硬件接口Android bluetooth介绍(二): android 蓝牙代码架构及其uart 到rfcomm流程Android blueto ...
- Java结对编程之挑战出题
Java结对编程之挑战出题 需求分析 需求 对于挑战出题来说最主要的就是要产生的式子并将重复的式子去掉. 设计思路 具体的思路: 思路一: 原先我打算用集合中的元素的不重复性进行去重,这种思路的好处就 ...
- c语言网络通信杂笔记
1.sin_addr.s_addr = INADDR_ANY;设置成本地IP 2.pthread_create();线程生成函数 3.在linux下,sleep(1)是睡眠1s
- Spring AOP(2)