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. [BZOJ3527][ZJOI2014]力 FFT+数学

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3527 首先卷积的形式是$h(i)=\sum_{i=0}^jf(i)g(i-j)$,如果我们 ...

  2. 【学习笔记】深入理解js原型和闭包(5)——instanceof

    又介绍一个老朋友——instanceof. 对于值类型,你可以通过typeof判断,string/number/boolean都很清楚,但是typeof在判断到引用类型的时候,返回值只有object/ ...

  3. 设计 REST API 的13个最佳实践

    写在前面 之所以翻译这篇文章,是因为自从成为一名前端码农之后,调接口这件事情就成为了家常便饭,并且,还伴随着无数的争论与无奈.编写友好的 restful api 不论对于你的同事,还是将来作为第三方服 ...

  4. 关于bin和obj文件夹。debug 和release的区别(转)

    关于bin和obj文件夹. 楼主hcaihao(影子男孩)2002-05-29 20:04:24 在 .NET技术 / C# 提问 VS.Net会生成bin和obj文件夹以及它们下面的Debug和Re ...

  5. Redis学习笔记(二)字符串进阶

    1.增减操作 字符串可以储存字节串.整数.浮点数三种类型的值,如果值无法被解释为十进制的整数或浮点数,那么对此值进行增减操作会返回错误,如果对一个不存在的或者保存了空串的键进行增减操作,Redis将当 ...

  6. js单线程和js异步操作的几种方法

    一.为什么JavaScript是单线程? JavaScript语言的一大特点就是单线程,也就是说,同一个时间只能做一件事. JavaScript的单线程,与它的用途有关.作为浏览器脚本语言,JavaS ...

  7. 在SAP UI中使用纯JavaScript显示产品主数据的3D模型视图

    在Jerry写这篇文章时,通过Google才知道,SAP其实是有自己的3D模型视图显示解决方案的. 故事要从Right Hemisphere说起,这是一家专业的企业级2D/3D模型浏览及转换的软件供应 ...

  8. (转)在编写Spring框架的配置文件时,标签无提示符的解决办法

    http://blog.csdn.net/yerenyuan_pku/article/details/52831618 问题描述 初学者在学习Spring框架的过程中,大概会碰到这样一个问题:在编写S ...

  9. (三)docker 的启动,重启,关闭命令

    docker启动命令,docker重启命令,docker关闭命令  启动    systemctl start docker 守护进程重启 sudo systemctl daemon-reload 重 ...

  10. Django请求,响应,ajax以及CSRF问题

    二.request对象常用属性: Attribute Description path 请求页面的全路径,不包括域名端口参数.例如: /users/index method 一个全大写的字符串,表示请 ...