Codeforces 848C (cdq分治)
Codeforces 848C Goodbye Souvenir
Problem :
给一个长度为n的序列,有q个询问。一种询问是修改某个位置的数,另一种询问是询问一段区间,对于每一种值出现的最右端点的下标与最左端点的下标的差值求和。
Solution :
定义pre[i] 为 第i个位置的数字上一次出现位置,对于询问l, r 就是对于所有满足
l <= pre[i] < i <= r 的点求和,权值为 i - pre[i]。
因此可以把这个看作是三维偏序的问题,第一维时间,第二维,第三维pre[i], i,用cdq分治求解。
对每一种值开一个set进行预处理,把每一次修改造成的影响表示成 pre[i], i, val 的形式。
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 8;
int n, q, tot, num;
int a[N];
long long ans[N];
struct node
{
int type, x, y, id;
bool operator < (const node &b) const
{
return x < b.x || x == b.x && type < b.type;
}
void print()
{
cout << type << " " << x << " " << y << " " << id << endl;
}
}Q[N], tmp[N];
set <int> S[N];
struct BIT
{
long long a[N];
int len;
void init(int l)
{
len = l;
for (int i = 0; i < len; ++i) a[i] = 0;
}
void insert(int x, int y)
{
for (int i = x; i < len; i += i & (-i))
a[i] += y;
}
long long query(int x)
{
long long res = 0;
for (int i = x; i > 0; i -= i & (-i))
res += a[i];
return res;
}
void clear(int x)
{
for (int i = x; i < len; i += i & (-i))
if (a[i] != 0) a[i] = 0; else break;
}
}T;
void cdq(int l, int r)
{
if (l == r) return;
int mid = l + r >> 1;
cdq(l, mid);
cdq(mid + 1, r);
int i = l, j = mid + 1;
for (int k = l; k <= r; ++k)
if (j > r || i <= mid && Q[i] < Q[j])
{
tmp[k] = Q[i++];
if (tmp[k].type == 1)
{
T.insert(n - tmp[k].y + 1, tmp[k].id);
}
}
else
{
tmp[k] = Q[j++];
if (tmp[k].type == 2)
{
ans[tmp[k].id] += T.query(n - tmp[k].y + 1);
}
}
for (int k = l; k <= r; ++k)
{
Q[k] = tmp[k];
T.clear(n - tmp[k].y + 1);
}
}
void update(int pos, int y)
{
if (a[pos] == y) return;
auto it = S[a[pos]].find(pos);
auto it1 = it; it1--;
auto it2 = it; it2++;
Q[++tot] = (node){1, *it, *it1, *it1 - *it};
Q[++tot] = (node){1, *it2, *it, *it - *it2};
Q[++tot] = (node){1, *it2, *it1, *it2 - *it1};
S[a[pos]].erase(pos);
a[pos] = y; S[a[pos]].insert(pos);
it = S[a[pos]].find(pos);
it1 = it; it1--;
it2 = it; it2++;
Q[++tot] = (node){1, *it, *it1, *it - *it1};
Q[++tot] = (node){1, *it2, *it, *it2 - *it};
Q[++tot] = (node){1, *it2, *it1, *it1 - *it2};
}
void init()
{
cin >> n >> q;
tot = 0;
for (int i = 1; i <= n; ++i) S[i].insert(0), S[i].insert(n + 1);
for (int i = 1; i <= n; ++i)
{
int x; cin >> x; a[i] = x;
S[x].insert(i);
auto it = S[x].find(i);
it--;
Q[++tot] = (node){1, i, *it, i - (*it)};
}
num = 0;
for (int i = 1; i <= q; ++i)
{
int type, x, y;
cin >> type >> x >> y;
if (type == 2)
{
Q[++tot] = (node){2, y, x, ++num};
}
else
{
update(x, y);
}
}
}
void solve()
{
T.init(n + 10);
cdq(1, tot);
for (int i = 1; i <= num; ++i)
cout << ans[i] << endl;
}
int main()
{
cin.sync_with_stdio(0);
init();
solve();
}
Codeforces 848C (cdq分治)的更多相关文章
- 【题解】Radio stations Codeforces 762E CDQ分治
虽然说好像这题有其他做法,但是在问题转化之后,使用CDQ分治是显而易见的 并且如果CDQ打的熟练的话,码量也不算大,打的也很快,思维难度也很小 没学过CDQ分治的话,可以去看看我的另一篇博客,是CDQ ...
- Radio stations CodeForces - 762E (cdq分治)
大意: 给定$n$个三元组$(x,r,f)$, 求所有对$(i,j)$, 满足$i<j, |f_i-f_j|\le k, min(r_i,r_j)\ge |x_i-x_j|$ 按$r$降序排, ...
- Codeforces 669E cdq分治
题意:你需要维护一个multiset,支持以下操作: 1:在某个时间点向multiset插入一个数. 2:在某个时间点在multiset中删除一个数. 3:在某个时间点查询multiset的某个数的个 ...
- Tufurama CodeForces - 961E (cdq分治)
题面 One day Polycarp decided to rewatch his absolute favourite episode of well-known TV series " ...
- AI robots CodeForces - 1045G (cdq分治)
大意: n个机器人, 位置$x_i$, 可以看到$[x_i-r_i,x_i+r_i]$, 智商$q_i$, 求智商差不超过$k$且能互相看到的机器人对数. 这个题挺好的, 关键是要求互相看到这个条件, ...
- Codeforces 848C Goodbye Souvenir(CDQ 分治)
题面传送门 考虑记录每个点的前驱 \(pre_x\),显然答案为 \(\sum\limits_{i=l}^{r} i-pre_i (pre_i \geq l)\) 我们建立一个平面直角坐标系,\(x\ ...
- Codeforces 848C Goodbye Souvenir [CDQ分治,二维数点]
洛谷 Codeforces 这题我写了四种做法-- 思路 不管做法怎样,思路都是一样的. 好吧,其实不一样,有细微的差别. 第一种 考虑位置\(x\)对区间\([l,r]\)有\(\pm x\)的贡献 ...
- Codeforces 1093E Intersection of Permutations [CDQ分治]
洛谷 Codeforces 思路 一开始想到莫队+bitset,发现要T. 再想到分块+bitset,脑子一抽竟然直接开始写了,当然也T了. 最后发现这就是个裸的CDQ分治-- 发现\(a\)不变,可 ...
- Codeforces 1045G AI robots [CDQ分治]
洛谷 Codeforces 简单的CDQ分治题. 由于对话要求互相看见,无法简单地用树套树切掉,考虑CDQ分治. 按视野从大到小排序,这样只要右边能看见左边就可以保证互相看见. 发现\(K\)固定,那 ...
随机推荐
- [ AHOI 2013 ] 作业 & [ BZOJ 3809 ] Gty的二逼妹子序列
\(\\\) Description 给出一个长为 \(n\) 的数列 \(A\) 和 \(k\),多次询问: 对于一个区间 \([L_i,R_i]\),问区间内有多少个数在 \([a_i,b_i]\ ...
- 新手玩CSS中的一些黑科技
哎哎 1.鼠标移进网页里,不见了= = *{ cursor: none!important; } 2.简单的文字模糊效果 *{ color: transparent; text-shadow: #11 ...
- Scala 学习记录(一)
1. 相对于java,scala的值修饰用val,变量修饰用var.值相当于java的final 修饰了. package demo object ScalaBase extends App { pr ...
- 移除sql数据所有链接用户
use master; go declare @temp nvarchar(20) declare myCurse cursor for select spid from sy ...
- COGS 1570. [POJ3461]乌力波
★☆ 输入文件:oulipo.in 输出文件:oulipo.out 简单对比时间限制:1 s 内存限制:256 MB [题目描述] 法国作家乔治·佩雷克(Georges Perec,1 ...
- CentOS7.6 修改密码
一.重启系统,在开机过程中,按下键盘上的e,进入编辑模式 三.将光标一直移动到 LANG=en_US.UTF-8 后面,空格,再追加init=/bin/sh.这里特别注意,需要写在UTF-8后,保 ...
- Linux下使用vi命令后退出方式
退出Vi 当编辑完文件,准备退出Vi返回到shell时,可以使用以下几种方法之一. 在命令模式中,连按两次大写字母Z,若当前编辑的文件曾被修改过,则Vi保存该文件后退出 ...
- Xcode导入第三方库图文
Three20这个与facebook亲戚的开源库是蜚声iPhone开发界,很多App都有它的影子,主要是其真得是功能强大.那么如何将Three20库添加到自己的项目中应用呢?一种是Python命令方式 ...
- codevs 2853 方格游戏--棋盘dp
方格游戏:http://codevs.cn/problem/2853/ 这和传纸条和noip方格取数这两个题有一定的相似性,当第一眼看到的时候我们就会想到设计$dp[i][j][k][l]$(i,j表 ...
- <Spring Cloud>入门三 Ribbon
1.Ribbon 客户端软负载均衡组件 1.1配置 搭建了三个消费者供客户端调用: 1.修改yml eureka: client: service-url: defaultZone: http://e ...