大意:给定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. emoj表情过滤

    用法:  isEmojiCharacter(input_value)   //  提交时候校验.true:emoj表情   undefined:无   if(isEmojiCharacter(val) ...

  2. 干货:Java多线程详解(内附源码)

      线程是程序执行的最小单元,多线程是指程序同一时间可以有多个执行单元运行(这个与你的CPU核心有关). 在java中开启一个新线程非常简单,创建一个Thread对象,然后调用它的start方法,一个 ...

  3. ThinkPHP5跨控制器调用

    1.在application\index\controller\文件夹里新建User.php <?php namespace app\index\controller; class User{ ...

  4. 20145220韩旭飞《网络对抗》Exp6 信息搜集与漏洞扫描

    20145220韩旭飞<网络对抗>Exp6 信息搜集与漏洞扫描 信息搜集 whois查询 以百度的网址为例,使用whois查询域名注册信息: 从上图中可以得到3R注册信息,包括注册人的名字 ...

  5. Disruptor学习笔记(一):基本原理和概念

    一.Disruptor基本原理 在多线程开发中,我们常常遇到这样一种场景:一些线程接受用户请求,另外一些线程处理这些请求.比如日志处理中的日志输入和告警.这种典型的生产者消费者场景十分常见,而生产者消 ...

  6. NOIP模拟题 2017.7.3 - 模拟 - 贪心 - 记忆化搜索

    直接暴力模拟,注意判数据结构为空时的取出操作. Code #include<iostream> #include<cstdio> #include<ctime> # ...

  7. noip 2014 提高组 Day 2

    1.无线网络发射器选址 这道题数据范围很小,就直接暴力枚举就好了.为了提高速度,就从每个有公共场所的点枚举周围在(x,y)放无线网路发射器可以增加的公共场所数量,加到一个数组里.所有公共场所都处理完了 ...

  8. Python3基础 __len__,__getitem__ 记录列表中元素访问的次数 定制不可变序列,下标字典

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

  9. 记录openwrt下补丁apply的过程中出错,但是可以单独打上该补丁

    背景: 在openwrt的编译框架下无法正确打上补丁,而单独使用git却可以成功 这个补丁到底与其它补丁有何不同? 该补丁的生成的过程解析: 旧文件:vi 打开旧文件会提示no newline at ...

  10. 卸载vs2017

    卸载enterprise版本 Microsoft.FSharp.SDK.Core卸载失败Package 'Microsoft.FSharp.SDK.Core,version=15.7.20180605 ...