Army Creation CodeForces - 813E (水题)

题意: 给定序列, 每次询问一个区间[l,r], 问[l,r]中最多能选多少个数且每种数字不超过k

相当于加强版 HH的项链, 对于一个数t, 主席树维护上k次出现的位置pre[t], 每次查询相当于求区间内pre<左端点的总数

#include <iostream>
#include <queue>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define mid ((l+r)>>1)
using namespace std;
const int N = 1e5+10, INF = 0x3f3f3f3f;
int tot, n, m, k;
int cnt[N], rt[N];
deque<int> pre[N];

struct _ {int l,r,sum;} tr[N<<5];

void ins(int &o, int l, int r, int x) {
    ++tot,tr[tot]=tr[o],o=tot,++tr[o].sum;
    if (l==r) return;
    if (mid>=x) ins(tr[o].l,l,mid,x);
    else ins(tr[o].r,mid+1,r,x);
}

int query(int u, int v, int l, int r, int x) {
    if (l==r) return 0;
    if (mid>=x) return query(tr[u].l,tr[v].l,l,mid,x);
    return tr[tr[v].l].sum-tr[tr[u].l].sum+query(tr[u].r,tr[v].r,mid+1,r,x);
}

int main() {
    scanf("%d%d", &n, &k);
    REP(i,1,n) {
        int t, x=0;
        scanf("%d", &t); rt[i] = rt[i-1];
        if (pre[t].size()==k) {
            x = pre[t].front();
            pre[t].pop_front();
        }
        ins(rt[i],0,n,x);
        pre[t].push_back(i);
    }
    scanf("%d", &m);
    int ans = 0;
    REP(i,1,m) {
        int l, r;
        scanf("%d%d", &l, &r);
        l = (l+ans)%n+1;
        r = (r+ans)%n+1;
        if (l>r) swap(l,r);
        printf("%d\n", ans=query(rt[l-1],rt[r],0,n,l));
    }
}

原文地址:https://www.cnblogs.com/uid001/p/10217674.html

时间: 2024-10-02 00:18:38

Army Creation CodeForces - 813E (水题)的相关文章

Pearls in a Row CodeForces 620C 水题

题目:http://codeforces.com/problemset/problem/620/C 文章末有一些测试数据仅供参考 题目大意:就是给你一个数字串,然后将分成几个部分,要求每个部分中必须有一对儿相等的数字,每个数字都属于某个部分,输出划分的部分数量以及对应区间. 很简单一道题,输入的数据都不用存的,输入一个检测一个就好了,用map打标记,用容器存一下要输出的区间端点值,最后挨个儿输出就好了 代码如下: #include<iostream> #include<map> #

Codeforces 813E - Army Creation

813E - Army Creation 思路: 线段树+二分 先预处理每个点往后走k步的下标 线段树二叉树的每个节点用vector维护这些下标,给这些下标排个序 询问区间L,R,那么把下标小于等于R的位置都减掉,因为只要后面连续k个就够了 代码: #include<bits/stdc++.h> using namespace std; #define LL long long #define pb push_back #define ls rt<<1,l,m #define rs

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

E. Army Creation As you might remember from our previous rounds, Vova really likes computer games. Now he is playing a strategy game known as Rage of Empires. In the game Vova can hire n different warriors; ith warrior has the type ai. Vova wants to

codeforces Gym 100187L L. Ministry of Truth 水题

L. Ministry of Truth Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/problem/K Description Andrey works in the Ministry of Truth. His work is changing articles in newspapers and magazines so that they praise the Party an

水题 Codeforces Round #303 (Div. 2) A. Toy Cars

题目传送门 1 /* 2 题意:5种情况对应对应第i或j辆车翻了没 3 水题:其实就看对角线的上半边就可以了,vis判断,可惜WA了一次 4 3: if both cars turned over during the collision. 5 是指i,j两辆车,而不是全部 6 */ 7 #include <cstdio> 8 #include <algorithm> 9 #include <cstring> 10 #include <cmath> 11 #

codeforces 505B Mr. Kitayuta&#39;s Colorful Graph(水题)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Mr. Kitayuta's Colorful Graph Mr. Kitayuta has just bought an undirected graph consisting of n vertices and m edges. The vertices of the graph are numbered from 1 to n. Each edge, namely edge

CodeForces 22D Segments 排序水题

题目链接:点击打开链接 右端点升序,取右端点 暴力删边 #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <iostream> #include <map> #include <set> #include <math.h> using namespace std; #define inf 10

水题 Codeforces Round #299 (Div. 2) A. Tavas and Nafas

题目传送门 1 /* 2 很简单的水题,晚上累了,刷刷水题开心一下:) 3 */ 4 #include <bits/stdc++.h> 5 using namespace std; 6 7 char s1[11][10] = {"zero", "one", "two", "three", "four", "five", "six", "seven

Educational Codeforces Round 22 E. Army Creation(主席树)

题目链接:Educational Codeforces Round 22 E. Army Creation 题意: 给你n个数和一个数k,然后有q个询问. 每个询问 有一个区间[l,r],问你这个区间内在满足每一种数不超过k的情况下,最大能选多少个数出来. 强制在线. 题解: 一看就要用到主席树,和主席数求区间内有多少不同的数的个数处理方法相同. 依次将每个数插入,当这个数出现的个数等于k了,就把最前面的那个数删掉. 然后询问就访问root[r]就行了. 第一次写完数据结构没有调试一遍过样例,一