大意:给定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. git必备命令

    git status 查看git的状态git add <path>的形式把我们<path>添加到索引库中,<path>可以是文件也可以是目录.git add -u ...

  2. Qt学习之路(45): 自定义model之一

    前面我们说了Qt提供的几个预定义model.但是,面对变化万千的需求,那几个model是远远不能满足我们的需要的.另外,对于Qt这种框架来说,model的选择首先要能满足绝大多数功能的需要,这就是说, ...

  3. CentOS安装mysql并配置远程访问

    最近上班挺无聊,每天就是不停的重启重启重启,然后抓log.于是有事儿没事儿的看卡闲书,搞搞其他事情. 但是,公司笔记本装太多乱其八糟的东西也还是不太好. 于是,想到了我那个当VPN server的VP ...

  4. 02: python3使用email和smtplib库发送邮件

    1.1 发送qq邮箱 注:python代理登录qq邮箱发邮件,是需要更改自己qq邮箱设置的.在这里大家需要做两件事情:邮箱开启SMTP功能 .获得授权码 教程链接 1.给单个人发邮件 参考 from ...

  5. msf辅助模块的应用——20145301

    msf辅助模块的应用 实验步骤 创建msf所需的数据库 service postgresql start msfdb start 开启msf,输入命令 use auxiliary/scanner/di ...

  6. FTP-Linux中ftp服务器搭建

    一.FTP工作原理 (1)FTP使用端口 [root@localhost ~]# cat /etc/services | grep ftp ftp-data 20/tcp #数据链路:端口20 ftp ...

  7. 关于Session的概念和测试点

    Session概要 Session 是用于保持状态的基于 Web 服务器的方法,在 Web 服务器上保持用户的状态信息供在任何时间从任何页访问. Session 允许通过将对象存储在 Web 服务器的 ...

  8. Go第六篇之结构体剖析

    Go 语言通过用自定义的方式形成新的类型,结构体是类型中带有成员的复合类型.Go 语言使用结构体和结构体成员来描述真实世界的实体和实体对应的各种属性. Go 语言中的类型可以被实例化,使用new或&a ...

  9. 苹果电脑macbook 安装 Burp Suite pro_v1.7.37破解版

    1.先去官网下载最新版本 Burp Suite Community Edition v1.7.36安装完成 https://portswigger.net/burp/communitydownload ...

  10. 深入理解Java面向对象三大特性 封装 继承 多态

    1.封装 封装的定义: 首先是抽象,把事物抽象成一个类,其次才是封装,将事物拥有的属性和动作隐藏起来,只保留特定的方法与外界联系 为什么需要封装: 封装符合面向对象设计原则的第一条:单一性原则,一个类 ...