CF 86D Powerful array 【分块算法,n*sqrt(n)】
2014-05-30 09:14
288 查看
分类: ACM 题解 数据结构2013-07-29
17:51 731人阅读 评论(3) 收藏 举报
给定一个数列:A1, A2,……,An,定义Ks为区间(l,r)中s出现的次数。
t个查询,每个查询l,r,对区间内所有a[i],求sigma(K^2*a[i])
离线+分块
将n个数分成sqrt(n)块。
对所有询问进行排序,排序标准:
1. Q[i].left /block_size < Q[j].left / block_size (块号优先排序)
2. 如果1相同,则 Q[i].right < Q[j].right (按照查询的右边界排序)
问题求解:
从上一个查询后的结果推出当前查询的结果。(这个看程序中query的部分)
如果一个数已经出现了x次,那么需要累加(2*x+1)*a[i],因为(x+1)^2*a[i] = (x^2 +2*x + 1)*a[i],x^2*a[i]是出现x次的结果,(x+1)^2 * a[i]是出现x+1次的结果。
时间复杂度分析:
排完序后,对于相邻的两个查询,left值之间的差最大为sqrt(n),则相邻两个查询左端点移动的次数<=sqrt(n),总共有t个查询,则复杂度为O(t*sqrt(n))。
又对于相同块内的查询,right端点单调上升,每一块所有操作,右端点最多移动O(n)次,总块数位sqrt(n),则复杂度为O(sqrt(n)*n)。
right和left的复杂度是独立的,因此总的时间复杂度为O(t*sqrt(n) + n*sqrt(n))。
[cpp] view
plaincopy
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
#define N 200100
typedef long long ll;
ll a
, cnt[N*5], ans
, res;
int L, R;
struct node {
int x, y, l, p;
} q
;
bool cmp(const node &x, const node &y) {
if (x.l == y.l) return x.y < y.y;
return x.l < y.l;
}
void query(int x, int y, int flag) {
if (flag) {
for (int i=x; i<L; i++) {
res += ((cnt[a[i]]<<1)+1)*a[i];
cnt[a[i]]++;
}
for (int i=L; i<x; i++) {
cnt[a[i]]--;
res -= ((cnt[a[i]]<<1)+1)*a[i];
}
for (int i=y+1; i<=R; i++) {
cnt[a[i]]--;
res -= ((cnt[a[i]]<<1)+1)*a[i];
}
for (int i=R+1; i<=y; i++) {
res += ((cnt[a[i]]<<1)+1)*a[i];
cnt[a[i]]++;
}
} else {
for (int i=x; i<=y; i++) {
res += ((cnt[a[i]]<<1)+1)*a[i];
cnt[a[i]]++;
}
}
L = x, R = y;
}
int main() {
int n, t;
scanf("%d%d", &n, &t);
for (int i=1; i<=n; i++) scanf("%I64d", &a[i]);
int block_size = sqrt(n);
for (int i=0; i<t; i++) {
scanf("%d%d", &q[i].x, &q[i].y);
q[i].l = q[i].x / block_size;
q[i].p = i;
}
sort(q, q+t, cmp);
memset(cnt, 0, sizeof(cnt));
res = 0;
for (int i=0; i<t; i++) {
query(q[i].x, q[i].y, i);
ans[q[i].p] = res;
}
for (int i=0; i<t; i++) printf("%I64d\n", ans[i]);
return 0;
}
分类: ACM 题解 数据结构2013-07-29
19:56 310人阅读 评论(0) 收藏 举报
题目描述:
一条直线上n个点,每个点有个“弹力”,可以把当前位置x上面的ball弹到x+a[x]上面。
两种操作
0. 修改a处的弹力值,编程b
1. 询问a点的ball经过多少次能跳出n个点外(就是出界了)。。。。求出弹跳的次数和最后落脚的点。
块状链表就是用来暴力模拟的。
用块状链表可以把时间复杂度从O(n)变成O(sqrt(n))。
这道题目的复杂度为O(m*sqrt(n))。
具体实现还是直接看代码容易理解……
[cpp] view
plaincopy
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;
#define N 100100
int block
, c
, next
, a
, end
;
int n, m, block_size;
void make(int x) {
int y = x + a[x];
if (y <= n && block[x] == block[y]) {
c[x] = c[y] + 1;
next[x] = next[y];
end[x] = end[y];
} else {
end[x] = x;
c[x] = 1;
next[x] = y;
}
}
void change(int x, int y) {
a[x] = y;
for (y=x; y && block[y]==block[x]; y--) make(y);
}
void ask(int x) {
int ret = 0, ans;
for (; x<=n; x=next[x]) {
ret += c[x];
ans = end[x];
}
cout << ans << " " << ret << endl;
}
int main() {
cin >> n >> m;
block_size = sqrt(n);
for (int i=1; i<=n; i++) {
cin >> a[i];
block[i] = i/block_size;
}
for (int i=n; i; i--) make(i);
int op, x, y;
while (m--) {
cin >> op;
if (op == 0) {
cin >> x >> y;
change(x, y);
} else {
cin >> x;
ask(x);
}
}
return 0;
}
17:51 731人阅读 评论(3) 收藏 举报
给定一个数列:A1, A2,……,An,定义Ks为区间(l,r)中s出现的次数。
t个查询,每个查询l,r,对区间内所有a[i],求sigma(K^2*a[i])
离线+分块
将n个数分成sqrt(n)块。
对所有询问进行排序,排序标准:
1. Q[i].left /block_size < Q[j].left / block_size (块号优先排序)
2. 如果1相同,则 Q[i].right < Q[j].right (按照查询的右边界排序)
问题求解:
从上一个查询后的结果推出当前查询的结果。(这个看程序中query的部分)
如果一个数已经出现了x次,那么需要累加(2*x+1)*a[i],因为(x+1)^2*a[i] = (x^2 +2*x + 1)*a[i],x^2*a[i]是出现x次的结果,(x+1)^2 * a[i]是出现x+1次的结果。
时间复杂度分析:
排完序后,对于相邻的两个查询,left值之间的差最大为sqrt(n),则相邻两个查询左端点移动的次数<=sqrt(n),总共有t个查询,则复杂度为O(t*sqrt(n))。
又对于相同块内的查询,right端点单调上升,每一块所有操作,右端点最多移动O(n)次,总块数位sqrt(n),则复杂度为O(sqrt(n)*n)。
right和left的复杂度是独立的,因此总的时间复杂度为O(t*sqrt(n) + n*sqrt(n))。
[cpp] view
plaincopy
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
#define N 200100
typedef long long ll;
ll a
, cnt[N*5], ans
, res;
int L, R;
struct node {
int x, y, l, p;
} q
;
bool cmp(const node &x, const node &y) {
if (x.l == y.l) return x.y < y.y;
return x.l < y.l;
}
void query(int x, int y, int flag) {
if (flag) {
for (int i=x; i<L; i++) {
res += ((cnt[a[i]]<<1)+1)*a[i];
cnt[a[i]]++;
}
for (int i=L; i<x; i++) {
cnt[a[i]]--;
res -= ((cnt[a[i]]<<1)+1)*a[i];
}
for (int i=y+1; i<=R; i++) {
cnt[a[i]]--;
res -= ((cnt[a[i]]<<1)+1)*a[i];
}
for (int i=R+1; i<=y; i++) {
res += ((cnt[a[i]]<<1)+1)*a[i];
cnt[a[i]]++;
}
} else {
for (int i=x; i<=y; i++) {
res += ((cnt[a[i]]<<1)+1)*a[i];
cnt[a[i]]++;
}
}
L = x, R = y;
}
int main() {
int n, t;
scanf("%d%d", &n, &t);
for (int i=1; i<=n; i++) scanf("%I64d", &a[i]);
int block_size = sqrt(n);
for (int i=0; i<t; i++) {
scanf("%d%d", &q[i].x, &q[i].y);
q[i].l = q[i].x / block_size;
q[i].p = i;
}
sort(q, q+t, cmp);
memset(cnt, 0, sizeof(cnt));
res = 0;
for (int i=0; i<t; i++) {
query(q[i].x, q[i].y, i);
ans[q[i].p] = res;
}
for (int i=0; i<t; i++) printf("%I64d\n", ans[i]);
return 0;
}
CF 13E Holes 【块状链表】
分类: ACM 题解 数据结构2013-07-2919:56 310人阅读 评论(0) 收藏 举报
题目描述:
一条直线上n个点,每个点有个“弹力”,可以把当前位置x上面的ball弹到x+a[x]上面。
两种操作
0. 修改a处的弹力值,编程b
1. 询问a点的ball经过多少次能跳出n个点外(就是出界了)。。。。求出弹跳的次数和最后落脚的点。
块状链表就是用来暴力模拟的。
用块状链表可以把时间复杂度从O(n)变成O(sqrt(n))。
这道题目的复杂度为O(m*sqrt(n))。
具体实现还是直接看代码容易理解……
[cpp] view
plaincopy
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;
#define N 100100
int block
, c
, next
, a
, end
;
int n, m, block_size;
void make(int x) {
int y = x + a[x];
if (y <= n && block[x] == block[y]) {
c[x] = c[y] + 1;
next[x] = next[y];
end[x] = end[y];
} else {
end[x] = x;
c[x] = 1;
next[x] = y;
}
}
void change(int x, int y) {
a[x] = y;
for (y=x; y && block[y]==block[x]; y--) make(y);
}
void ask(int x) {
int ret = 0, ans;
for (; x<=n; x=next[x]) {
ret += c[x];
ans = end[x];
}
cout << ans << " " << ret << endl;
}
int main() {
cin >> n >> m;
block_size = sqrt(n);
for (int i=1; i<=n; i++) {
cin >> a[i];
block[i] = i/block_size;
}
for (int i=n; i; i--) make(i);
int op, x, y;
while (m--) {
cin >> op;
if (op == 0) {
cin >> x >> y;
change(x, y);
} else {
cin >> x;
ask(x);
}
}
return 0;
}
相关文章推荐
- CF 86D Powerful array 【分块算法,n*sqrt(n)】
- CF 86D Powerful array 【分块算法,n*sqrt(n)】
- CF 86D Powerful array 分块算法入门,n*sqrt(n)
- CF 86D Powerful array 分块算法入门,n*sqrt(n)
- [置顶] CF 86D Powerful array 分块算法入门,n*sqrt(n)
- 算法—快速排序Sqrt (C语言)
- 算法系列——Sqrt(x)
- 【打CF,学算法——五星级】CodeForces 478D (dp计数)
- 分块算法(cf 785E)
- Algorithms - 平方根(sqrt)算法 的 详解 及 扩展
- 【算法】一个比系统自带的sqrt函数快四倍的sqrt
- CF 671E XOR and Favorite Number (莫队算法)
- CF 530A(Quadratic equation-注意(-b+sqrt(Δ))/2a<(-b-sqrt(Δ))/2a的情况)[Picat]
- 【打CF,学算法——二星级】Codeforces Round #313 (Div. 2) B. Gerald is into Art(水题)
- 【打CF,学算法——三星级】Codeforces 9C Hexadecimal's Numbers (解法汇总)
- 推荐算法之用户推荐(UserCF)和物品推荐(ItemCF)对比
- 【打CF,学算法——二星级】Codeforces Round #312 (Div. 2) A Lala Land and Apple Trees
- 【打CF,学算法——三星级】CodeForces 689B Mike and Shortcuts (最短路+spfa)
- 【打CF,学算法——二星级】CodeForces 96B Lucky Numbers (构造/dfs)
- 【打CF,学算法——三星级】CodeForces 513C Second price auction (进制状态表示)