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. java中同步(synchronized)详解

    一.开山篇: 1.synchronized的使用 一个程序中,如果该类中的代码可能运行于多线程环境下,那么就要考虑同步的问题.在Java中内置了语言级的同步原语--synchronized,这也大大简 ...

  2. knockout Observable Array(监控数组)

    Observable Array(监控数组)的作用 列表操作是经常会遇到的一个场景,使用监控数组,你可以: 保存列表对象,并且使用Ko提供的丰富的API操作列表元素(支持内建js Array的方法,以 ...

  3. VBA 从sql存储过程-记录集-导入

    cnn.Open cnnstr cmd.ActiveConnection = cnn cmd.CommandTimeout = 120 cmd.CommandText = "dbo.t_bi ...

  4. 关于 propertychange 兼容性问题

    on 事件 $('body').on('property input','.class',function(){ alert(123); });

  5. Android(java)学习笔记167:横竖屏切换时Activity的生命周期

    1.横竖屏切换的生命周期     默认情况下横竖屏切换,先销毁再创建 2.有的时候,默认情况下的横竖屏切换(先销毁再创建),对应用户体验是不好的,比如是手机游戏横竖屏切换对游戏体验非常不好,下面两种方 ...

  6. 因JQUERY版本而产生的问题,需要加上迁移文件

    IMG_01_history控制台报错 IMG_02_history代码报错

  7. DROP SEQUENCE - 删除一个序列

    SYNOPSIS DROP SEQUENCE name [, ...] [ CASCADE | RESTRICT ] DESCRIPTION 描述 DROP SEQUENCE 从数据库中删除序列号生成 ...

  8. ubuntu命令行转换图片像素大小

    convert -resize 512x256 00433.png 00001.png 1.512和256之间是x(就是字母那个x),用' * '反而会报错 2.这个命令会按照原图的比例进行转换 3. ...

  9. RNN静态与动态

    静态.多层RNN:import numpy as np import tensorflow as tf # 导入 MINST 数据集 from tensorflow.examples.tutorial ...

  10. css--字体和文本样式

    文字样式属性 字体:font-family 文字大小:font-size 文字颜色:font-color 文字粗细:font-weight 文字样式:font-style font-family字体属 ...