codeforces 455D 分块
来源:互联网 发布:ubuntu 迅雷 编辑:程序博客网 时间:2024/05/21 11:11
简略题意:初始给出长度为
有两种操作,共q次。
要求强制在线。
看起来就是个数据结构,然而我们很难找到一个成熟的模型来套。
那么考虑分块,对于操作1, 若
对于操作2同理,
总复杂度
写起来还是需要注意细节的。
#include <bits/stdc++.h>#define all(x) x.begin(), x.end()using namespace std;typedef long long LL;const int maxn = 100002;int block, num, belong[maxn];int mp[400][100001];pair<int, int> mark[maxn];deque<int> V[400];int n;void build() { block = sqrt(n); num = n / block; if(n % block) num++; for(int i = 1; i <= n; i++) belong[i] = (i-1)/block+1;}void update(int l, int r) { pair<int, int> pos1 = mark[l], pos2 = mark[r]; int f1 = pos1.first, s1 = pos1.second; int f2 = pos2.first, s2 = pos2.second; int valr = V[f2][s2]; deque<int> &dq = V[f1]; if(f1 == f2) { dq.insert(dq.begin() + s1, valr); dq.erase(dq.begin() + s2 + 1); return ; } mp[f1][valr]++; V[f1].insert(dq.begin() + s1, valr); int vv = *V[f1].rbegin(); V[f1].pop_back(); mp[f1][vv]--; for(int i = f1 + 1; i < f2; i++) { V[i].push_front(vv); mp[i][vv]++; vv = *V[i].rbegin(); mp[i][vv]--; V[i].pop_back(); } mp[f2][valr]--; V[f2].erase(V[f2].begin() + s2); mp[f2][vv]++; V[f2].push_front(vv);}LL lans = 0;int decode(int x) { return (x + lans - 1)%n+1;}int ask(int l, int r, int k) { pair<int, int> pos1 = mark[l], pos2 = mark[r]; int f1 = pos1.first, s1 = pos1.second; int f2 = pos2.first, s2 = pos2.second; int cnt = 0; if(f1 == f2) { for(int i = s1; i <= s2; i++) if(V[f1][i] == k) cnt++; } else { for(int i = s1; i < V[f1].size(); i++) if(V[f1][i] == k) cnt++; for(int i = 0; i <= s2; i++) if(V[f2][i] == k) cnt++; for(int i = f1 + 1; i < f2; i++) cnt+=mp[i][k]; } return cnt;}void show() { for(int i = 1; i <= num; i++) for(int j =0; j < V[i].size(); j++) cout<<V[i][j]<<" "; puts("");}int main() { scanf("%d", &n); build(); for(int i = 1; i <= n; i++) { int v; scanf("%d", &v); V[belong[i]].push_back(v); mark[i] = {belong[i], V[belong[i]].size()-1}; mp[belong[i]][v]++; } int q; scanf("%d", &q); for(int i = 1; i <= q; i++) { int c, u, v; scanf("%d%d%d", &c, &u, &v); u = decode(u), v = decode(v); if(u > v) swap(u, v); if(c == 1) { update(u, v); } else { int k; scanf("%d", &k); k = decode(k); lans = ask(u, v, k); printf("%d\n", lans); } } return 0;}
阅读全文
0 0
- codeforces 455D 分块
- 【分块】 codeforces 455D - Serega and Fun
- [Codeforces 455D] Serega and Fun (分块)
- Codeforces 86D Powerful array 分块
- Codeforces 487D. Conveyor Belts 分块+DP
- codeforces 86D. Powerful array(分块)
- 【CodeForces】455D Serega and Fun 双向链表分块暴力
- CodeForces 455D Serega and Fun (分块+双端队列)
- codeforces 455 D. Serega and Fun (分块+双向链表)
- 【并查集分块】Codeforces 475D CGCDSSQ
- codeforces #308 D.Vanya and Triangles(枚举+斜率分块)
- Codeforces#86D Powerful array(分块暴力)
- 【CodeForces】80D Time to Raid Cowavans 分块
- codeforces#326-D - Duff in Beach- dp+分块
- [CodeForces #80 Div 1 D] 分块+树状数组/线段树
- codeforces 375D. Tree and Queries (莫队+分块)
- Codeforces 103D Time to Raid Cowavans 分块思想
- Codeforces 86D Powerful array 莫队算法 分块
- GCJ 2008 Round1A A Minimum Scalar Product
- gdb调试
- UVA-10128 Queue
- UVA-10082 WERTYU
- Thread与Runnable的区别
- codeforces 455D 分块
- 数码影像学到的知识汇总
- 抓包浏览器操作
- 漏洞银行/逆向工程学习日记(2)-8086指令系统之通用寄存器
- 解决Java抛出的异常,log中不含具体的堆栈信息
- 简述为什么不能用父类给子类赋值
- CentOS7(64位)下运行IPython3出错解决办法
- 史上最全最强SpringMVC详细示例实战教程
- 在IntelliJ IDEA中创建Spring项目