大意:给定n元素序列, 2种操作

  • 将区间$[l,r]$循环右移1位
  • 询问$[l,r]$中有多少个等于k的元素

现在给定q个操作, 输出操作2的询问结果, 强制在线

思路1: 分块

每个块内维护一个链表, 循环右移相当于删除一个元素, 再插入一个元素, 每个块内再维护一个桶统计元素个数即可

分块好久没写过了, 先放个分块大致流程

void init() {
//sqn是分块数, blo[i]是位置i所属块的编号
//L[i], R[i]是位置i所属块的左右边界
sqn = sqrt(n);
REP(i,1,n) {
blo[i] = (i-1)/sqn+1;
L[i] = (blo[i]-1)*sqn+1;
R[i] = blo[i]*sqn;
}
}
void work(int l, int r) {
if (blo[l]==blo[r]) {
//在一块直接暴力
return;
}
REP(i,l,R[l]) {
//第一块暴力
}
REP(i,blo[l]+1,blo[r]+1) {
//处理中间每个块
}
REP(i,L[r],r) {
//最后一块暴力
}
}

该题的代码如下. deque用的还是不熟练, 很简单的思路还是码了一个多小时.

#include <iostream>
#include <math.h>
#include <algorithm>
#include <cstdio>
#include <queue>
#define REP(i,a,n) for(int i=a;i<=n;++i)
using namespace std;
typedef long long ll; const int N = 1e5+10, M = 333;
int n, m, sqn;
int L[N], R[N], blo[N];
int sum[M][N];
deque<int> q[N]; void work(int l, int r) {
int x;
auto t = q[blo[r]].begin();
REP(i,L[r],r-1) ++t;
x = *t;
--sum[blo[r]][x];
q[blo[r]].erase(t);
t = q[blo[l]].begin();
REP(i,L[l],l-1) ++t;
++sum[blo[l]][x];
q[blo[l]].insert(t,x);
if (blo[l]==blo[r]) return;
x = q[blo[l]].back();
--sum[blo[l]][x];
q[blo[l]].pop_back();
REP(i,blo[l]+1,blo[r]-1) {
++sum[i][x];
q[i].push_front(x);
x = q[i].back();
--sum[i][x];
q[i].pop_back();
}
++sum[blo[r]][x];
q[blo[r]].push_front(x);
} int query(int l, int r, int k) {
ll ans = 0;
if (blo[l]==blo[r]) {
REP(i,l,r) ans += (q[blo[l]][i-L[i]]==k);
return ans;
}
REP(i,l,R[l]) ans += (q[blo[l]][i-L[i]]==k);
REP(i,blo[l]+1,blo[r]-1) ans += sum[i][k];
REP(i,L[r],r) ans += (q[blo[r]][i-L[i]]==k);
return ans;
} int main() {
scanf("%d", &n), sqn=sqrt(n);
REP(i,1,n) {
int t;
scanf("%d", &t);
blo[i]=(i-1)/sqn+1;
++sum[blo[i]][t];
q[blo[i]].push_back(t);
L[i]=(blo[i]-1)*sqn+1,R[i]=blo[i]*sqn;
}
scanf("%d", &m);
int ans = 0;
REP(i,1,m) {
int op, l, r, k;
scanf("%d%d%d", &op, &l, &r);
l = (l+ans-1)%n+1, r = (r+ans-1)%n+1;
if (l>r) swap(l,r);
if (op==1) work(l,r);
else {
scanf("%d", &k);
k = (k+ans-1)%n+1;
printf("%d\n", ans=query(l,r,k));
}
}
}

思路2: splay

先留着以后填吧......

Serega and Fun CodeForces - 455D (分块 或 splay)的更多相关文章

  1. CodeForces 455D 分块

    题目链接:http://codeforces.com/problemset/problem/455/D 题意:给定一个长度为n的序列a[]. m次操作.共有两种操作 1 l r:将序列的a[l].a[ ...

  2. Serega and Fun Codeforces - 455D || queue

    https://codeforces.com/problemset/problem/455/D 其实方法很多,然而当初一个也想不到... 1.分块,块内用链表维护 修改[l,r]就当成删除第r个元素, ...

  3. Codeforces 455D 分块+链表

    题意: 给定一个长度为 N 的序列两种操作1 l r 将[l,r]的数向右循环移位 2 l r 询问[l,r]内有多少个数等于 k其中 N,Q≤105,ai≤N 强制在线 思路: 1. 每块用一个链表 ...

  4. CodeForces 444C 分块

    题目链接:http://codeforces.com/problemset/problem/444/C 题意:给定一个长度为n的序列a[].起初a[i]=i,然后还有一个色度的序列b[],起初b[i] ...

  5. CodeForces 551E 分块

    题目链接:http://codeforces.com/problemset/problem/551/E 题意:给定一个长度为N的序列. 有2个操作 1 l r v:序列第l项到第r项加v(区间加), ...

  6. CodeForces 103D 分块处理

    题目链接:http://codeforces.com/problemset/problem/103/D 题意:给定一个长度为n的序列.然后q个询问.每个询问为(a,b),表示从序列第a项开始每b项的加 ...

  7. Codeforces 675D Tree Construction Splay伸展树

    链接:https://codeforces.com/problemset/problem/675/D 题意: 给一个二叉搜索树,一开始为空,不断插入数字,每次插入之后,询问他的父亲节点的权值 题解: ...

  8. CodeForces - 455D

    Serega loves fun. However, everyone has fun in the unique manner. Serega has fun by solving query pr ...

  9. Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem E (Codeforces 828E) - 分块

    Everyone knows that DNA strands consist of nucleotides. There are four types of nucleotides: "A ...

随机推荐

  1. bzoj1643 / P2666 [Usaco2007 Oct]Bessie's Secret Pasture 贝茜的秘密草坪

    [Usaco2007 Oct]Bessie's Secret Pasture 贝茜的秘密草坪 简单的dfs题 枚举前3个完全平方数,判断最后一个是不是完全平方数,统计合法方案数即可. (zz选手竟把数 ...

  2. 20145118 《Java程序设计》 实验报告二

    实验二 Java面向对象程序设计 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 实验要求 1.没有Lin ...

  3. TF-调整矩阵维度 tf.reshape 介绍

    函数原型为 def reshape(tensor, shape, name=None) 第1个参数为被调整维度的张量. 第2个参数为要调整为的形状. 返回一个shape形状的新tensor 注意sha ...

  4. intent bundle的使用

    1.什么是bundle Bundle主要用于传递数据:它保存的数据,是以key-value(键值对)的形式存在的.我们经常使用Bundle在Activity之间传递数据,传递的数据可以是boolean ...

  5. Python3基础 else 循环完整结束才执行

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  6. redis.conf 配置 详解 中文 2.8

        # redis version 2.8.19   # 1k => 1000 bytes# 1kb => 1024 bytes# 1m => 1000000 bytes# 1m ...

  7. v-bind绑定属性样式

    一.class的四种绑定方式 1.布尔值的绑定方式 <div id="demo"> <span v-bind:class="{'class-a':isA ...

  8. C++课程小结 继承与派生

    单继承与多重继承的区别 单继承:一个子类(派生类)只有一个父类(只由一个基类派生而成) 多继承:一个子类(派生类)有多个父类(由多个基类派生而成) 派生类的构成 (1) 从基类继承过来的成员(包括数据 ...

  9. UVa 10801 电梯换乘

    https://vjudge.net/problem/UVA-10801 题意:有多个电梯,每个电梯只能到达指定楼层,每个电梯有速度,如果中途换乘电梯,需要额外加60s,求从0层到达k层的最少时间. ...

  10. shell 逻辑操作符

    Shell还提供了与( -a ).或( -o ).非( ! )三个逻辑操作符用于将测试条件连接起来,其优先级为:"!"最高,"-a"次之,"-o&qu ...