Serega and Fun CodeForces - 455D (分块 或 splay)
大意:给定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)的更多相关文章
- CodeForces 455D 分块
题目链接:http://codeforces.com/problemset/problem/455/D 题意:给定一个长度为n的序列a[]. m次操作.共有两种操作 1 l r:将序列的a[l].a[ ...
- Serega and Fun Codeforces - 455D || queue
https://codeforces.com/problemset/problem/455/D 其实方法很多,然而当初一个也想不到... 1.分块,块内用链表维护 修改[l,r]就当成删除第r个元素, ...
- Codeforces 455D 分块+链表
题意: 给定一个长度为 N 的序列两种操作1 l r 将[l,r]的数向右循环移位 2 l r 询问[l,r]内有多少个数等于 k其中 N,Q≤105,ai≤N 强制在线 思路: 1. 每块用一个链表 ...
- CodeForces 444C 分块
题目链接:http://codeforces.com/problemset/problem/444/C 题意:给定一个长度为n的序列a[].起初a[i]=i,然后还有一个色度的序列b[],起初b[i] ...
- CodeForces 551E 分块
题目链接:http://codeforces.com/problemset/problem/551/E 题意:给定一个长度为N的序列. 有2个操作 1 l r v:序列第l项到第r项加v(区间加), ...
- CodeForces 103D 分块处理
题目链接:http://codeforces.com/problemset/problem/103/D 题意:给定一个长度为n的序列.然后q个询问.每个询问为(a,b),表示从序列第a项开始每b项的加 ...
- Codeforces 675D Tree Construction Splay伸展树
链接:https://codeforces.com/problemset/problem/675/D 题意: 给一个二叉搜索树,一开始为空,不断插入数字,每次插入之后,询问他的父亲节点的权值 题解: ...
- CodeForces - 455D
Serega loves fun. However, everyone has fun in the unique manner. Serega has fun by solving query pr ...
- 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 ...
随机推荐
- JavaScript 创建动态表格
JavaScript 创建动态表格 版权声明:未经授权,严禁转载! 案例代码 <div id="data"></div> <script> va ...
- 05:ModelForm 数据验证 & 生成html & 数据库操作
目录:Django其他篇 01:Django基础篇 02:Django进阶篇 03:Django数据库操作--->Model 04: Form 验证用户数据 & 生成html 05:Mo ...
- mysql-cluster 7.3.5安装部署
集群环境 管理节点 10.0.0.19 数据节点 10.0.0.12 10.0.0.17 sql节点 10.0.0.18 10.0.0.22 添加mysql用户 groupadd mysql user ...
- 20145122《Java面向对象程序设计》实验二实验报告
实验名称: Java面向对象程序设计 实验内容: 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 PSP时间 步骤 ...
- 20145324王嘉澜 《网络对抗》进阶实践之 shellcode注入和Return-to-libc攻击深入
Shellcode注入 •Shellcode实际是一段代码,但却作为数据发送给受攻击服务器,将代码存储到对方的堆栈中,并将堆栈的返回地址利用缓冲区溢出,覆盖成为指向 shellcode的地址 •实验参 ...
- win32 自定义右键菜单
/**************************************************************************** 几大主要问题: 1.通过处理WM_MOUSE ...
- JQuery使用教程
jQuery简介 jQuery由美国人John Resig创建,至今已吸引了来自世界各地的众多 javascript高手加入其team jQuery是对原生JavaScript二次封装的工具函数集合 ...
- git如何列出最简短的commit(tag和head名都不显示)
答:git log --oneline --no-decorate --oneline: 将commit显示成一行 --no-decorate: 将tag和head名隐藏掉
- Windows下卸载软件时提示 等待先前的卸载完成? 终止 dllhost.exe 进程
只要结束进程中的 "dllhost" 进程就好了. 估计原因是, 当卸载某些 "所谓的"较大型的软件的时候, 要去更新, 更改系统对dll链接库的注册, 更新. ...
- 51NOD 1117 聪明的木匠
来源:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1117 挑战原题吧 大概 每次挑选最小的两个,合起来 #inclu ...