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分治)的更多相关文章

  1. 【题解】Radio stations Codeforces 762E CDQ分治

    虽然说好像这题有其他做法,但是在问题转化之后,使用CDQ分治是显而易见的 并且如果CDQ打的熟练的话,码量也不算大,打的也很快,思维难度也很小 没学过CDQ分治的话,可以去看看我的另一篇博客,是CDQ ...

  2. 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$降序排, ...

  3. Codeforces 669E cdq分治

    题意:你需要维护一个multiset,支持以下操作: 1:在某个时间点向multiset插入一个数. 2:在某个时间点在multiset中删除一个数. 3:在某个时间点查询multiset的某个数的个 ...

  4. Tufurama CodeForces - 961E (cdq分治)

    题面 One day Polycarp decided to rewatch his absolute favourite episode of well-known TV series " ...

  5. AI robots CodeForces - 1045G (cdq分治)

    大意: n个机器人, 位置$x_i$, 可以看到$[x_i-r_i,x_i+r_i]$, 智商$q_i$, 求智商差不超过$k$且能互相看到的机器人对数. 这个题挺好的, 关键是要求互相看到这个条件, ...

  6. Codeforces 848C Goodbye Souvenir(CDQ 分治)

    题面传送门 考虑记录每个点的前驱 \(pre_x\),显然答案为 \(\sum\limits_{i=l}^{r} i-pre_i (pre_i \geq l)\) 我们建立一个平面直角坐标系,\(x\ ...

  7. Codeforces 848C Goodbye Souvenir [CDQ分治,二维数点]

    洛谷 Codeforces 这题我写了四种做法-- 思路 不管做法怎样,思路都是一样的. 好吧,其实不一样,有细微的差别. 第一种 考虑位置\(x\)对区间\([l,r]\)有\(\pm x\)的贡献 ...

  8. Codeforces 1093E Intersection of Permutations [CDQ分治]

    洛谷 Codeforces 思路 一开始想到莫队+bitset,发现要T. 再想到分块+bitset,脑子一抽竟然直接开始写了,当然也T了. 最后发现这就是个裸的CDQ分治-- 发现\(a\)不变,可 ...

  9. Codeforces 1045G AI robots [CDQ分治]

    洛谷 Codeforces 简单的CDQ分治题. 由于对话要求互相看见,无法简单地用树套树切掉,考虑CDQ分治. 按视野从大到小排序,这样只要右边能看见左边就可以保证互相看见. 发现\(K\)固定,那 ...

随机推荐

  1. ES6—带默认值的函数参数及其作用域

    在学习ES6函数一章时,发现了一个有意思的现象,原文描述如下: 这段话主要state了3个事实: ①函数参数有默认值时,会在声明初始化阶段形成一个单独的作用域 ②这个作用域在初始化结束后消失 ③没默认 ...

  2. 个人作业(alpha)

    这个作业属于哪个课程  https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass1/ 这个作业要求在哪里  https://edu.cn ...

  3. 在自己的工程中使用ijkplayer的功能

    最近在做一个软解视频叠加硬解视频的方案,网上看了很多教程,始终不得要领.虽然ijkplayer提供了ijkplayer-example这个示例工程,但对于初入安卓的人来说,要将ijkplayer整合到 ...

  4. sql语句分为三类(DML,DDL,DCL)-介绍

    本文知识来源自:<Oracle专家高级编程> 分享作者:Vashon 时间:20150415 DDL is Data Definition Language statements. Som ...

  5. Docker - Image创建

    自己创建Image会有一些好处,可以选择最新的版本,而且从国内的镜像创建时更新软件也会从该镜像获取,速度更快. (1)安装debootstrap zhouh1@uhome:/media/zhouh1/ ...

  6. HTTP 方法:GET 对比 POST 转自w3school

    两种最常用的 HTTP 方法是:GET 和 POST. 什么是 HTTP? 超文本传输协议(HTTP)的设计目的是保证客户机与服务器之间的通信. HTTP 的工作方式是客户机与服务器之间的请求-应答协 ...

  7. java语言基础-类型运算细节

    代码一: public class varDemo{ public static void main(String[] args) { byte a2; a2=3+4; System.out.prin ...

  8. Java JDK装配置

     1- 介绍 本文章介绍JAVA开发环境安装是基于:  Java8(JDK8) 2- 下载JDK http://www.oracle.com/technetwork/java/javase/dow ...

  9. javaee 第五周作业

    一.Ajax技术 AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML). AJAX 不是新的编程语言,而是一种使用现有标准的新方法. ...

  10. liunx防火墙的基本操作

    centos7中的使用 CentOS 7.0默认使用的是firewall作为防火墙 查看防火墙状态------->firewall-cmd --state 停止firewall-------&g ...