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的更多相关文章

  1. 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 ...

  2. Educational Codeforces Round 22 E. Army Creation 主席树 或 分块

    http://codeforces.com/contest/813/problem/E 题目大意: 给出长度为n的数组和k,  大小是1e5级别. 要求在线询问区间[l, r]权值,  权值定义为对于 ...

  3. [Educational Codeforces Round#22]

    来自FallDream的博客,未经允许,请勿转载,谢谢. 晚上去clj博客逛来逛去很开心,突然同学提醒了一下,发现cf已经开始40分钟了,慌的一B,从B题开始写,写完了B到E最后收掉了A,结果太着急B ...

  4. Educational Codeforces Round 22 补题 CF 813 A-F

    A The Contest 直接粗暴贪心 略过 #include<bits/stdc++.h> using namespace std; int main() {//freopen(&qu ...

  5. Educational Codeforces Round 22.B 暴力

    B. The Golden Age time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  6. Educational Codeforces Round 22 B. The Golden Age(暴力)

    题目链接:http://codeforces.com/contest/813/problem/B 题意:就是有一个数叫做不幸运数,满足题目的 n = x^a + y^b,现在给你一个区间[l,r],让 ...

  7. 【Educational Codeforces Round 22】

    又打了一场EDU,感觉这场比23难多了啊…… 艹还是我太弱了. A. 随便贪心一下. #include<bits/stdc++.h> using namespace std; ,ans=- ...

  8. Educational Codeforces Round 40千名记

    人生第二场codeforces.然而遇上了Education场这种东西 Educational Codeforces Round 40 下午先在家里睡了波觉,起来离开场还有10分钟. 但是突然想起来还 ...

  9. [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 ...

随机推荐

  1. thymeleaf单选回显,多选回显,选回显,下拉默认选中第一个

    //默认选中第一个<input type ="radio" name="repaymentType" th:each ="repaymentTy ...

  2. Dapper and Repository Pattern in MVC

    大家好,首先原谅我标题是英文的,因为我想不出好的中文标题. 这里我个人写了一个Dapper.net 的Repository模式的底层基础框架. 涉及内容: Dapper.net结合Repository ...

  3. 多通道CNN

    在读Convolutional Neural Networks for Sentence Classification 这个文章的时候,它在论文中提出一种模型变种就是 CNN-multichannel ...

  4. Pythony的数据类型和变量使用方法详解

    数据类型:计算机顾名思义就是可以做数学计算的机器,因此,计算机程序理所当然地可以处理各种数值.但是,计算机能处理的远不止数值,还可以处理文本.图形.音频.视频.网页等各种各样的数据,不同的数据,需要定 ...

  5. 477. Total Hamming Distance

    class Solution { public: int totalHammingDistance(vector<int>& nums) { ; ; i < ; i++) { ...

  6. VIM配置IDE

    转载于:https://blog.csdn.net/andre617/article/details/53496490#%E8%84%9A%E6%B3%A8 由于YCM需要vim支持python,需要 ...

  7. Codeforces Round #459 (Div. 2) D. MADMAX DFS+博弈

    D. MADMAX time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...

  8. 006---Django静态文件配置

    静态文件:Js.Css.Fonts.Image等 这个不难.在setting.py文件加一行 # 别名 用户在url地址栏输入127.0.0.1:8000/static/文件 可以直接访问static ...

  9. mybatis在where中比较复杂的判断

    <if test="param.applicationStateInNumber != null and param.applicationStateInNumber != ''&qu ...

  10. Gold Balanced Lineup POJ - 3274

    Description Farmer John's N cows (1 ≤ N ≤ 100,000) share many similarities. In fact, FJ has been abl ...