Solution

线段树好题.

我们考虑用last[i]表示\(i\)这个位置的颜色的上一个出现位置. 考虑以一个位置\(R\)为右端点的区间最远能向左延伸到什么位置: \(L = \max_{i \le j} last[j]\).

而我们的答案就等于

\[sum_{i = 1}^n (i - (max_{1 \le j \le i} last[j])) = \sum_{i = 1}^n i - \sum_{i = 1}^n max_{1 \le j \le i} last[j]
\]

第一项可以直接计算, 考虑如何维护第二项.

我们开一颗线段树, 假设一个节点所维护的区间是\([L, R]\), 则节点维护\(\sum_{i = L}^R max_{L \le j \le i} last[j]\). 具体怎么实现, 看代码即可.

#include <cstdio>
#include <cctype>
#include <set>
#include <algorithm>
#define iter set<int>::iterator using namespace std;
namespace Zeonfai
{
inline int getInt()
{
int a = 0, sgn = 1; char c;
while(! isdigit(c = getchar())) if(c == '-') sgn *= -1;
while(isdigit(c)) a = a * 10 + c - '0', c = getchar();
return a * sgn;
}
}
const int N = (int)1e5;
int n;
int col[N + 1], pre[N + 1];
set<int> st[N + 1];
inline iter getPrevious(iter p) {return -- p;}
inline iter getNext(iter p) {return ++ p;}
struct segmentTree
{
struct node
{
int mx;
long long sum;
}nd[N << 2];
long long work(int u, int L, int R, int val)
{
int mid = L + R >> 1;
if(L == R) {return max(val, nd[u].mx);}
else if(nd[u].mx <= val) {return (long long)(R - L + 1) * val;}
else if(nd[u << 1].mx <= val) return (long long)val * (mid - L + 1) + work(u << 1 | 1, mid + 1, R, val);
else return work(u << 1, L, mid, val) + nd[u].sum - nd[u << 1].sum;
}
inline void pushUp(int u, int L, int R)
{
int mid = L + R >> 1;
nd[u].mx = max(nd[u << 1].mx, nd[u << 1 | 1].mx);
nd[u].sum = (long long)nd[u << 1].sum + work(u << 1 | 1, mid + 1, R, nd[u << 1].mx);
}
void build(int u, int L, int R)
{
if(L == R) {nd[u].mx = nd[u].sum = pre[L]; return;}
int mid = L + R >> 1;
build(u << 1, L, mid); build(u << 1 | 1, mid + 1, R);
pushUp(u, L, R);
}
inline void build() {build(1, 1, n);}
void modify(int u, int L, int R, int pos, int val)
{
if(L == R) {nd[u].sum = nd[u].mx = val; return;}
int mid = L + R >> 1;
if(pos <= mid) modify(u << 1, L, mid, pos, val); else modify(u << 1 | 1, mid + 1, R, pos, val);
pushUp(u, L, R);
}
inline void modify(int pos, int val) {modify(1, 1, n, pos, val);}
}seg;
int main()
{ #ifndef ONLINE_JUDGE freopen("array.in", "r", stdin);
freopen("array.out", "w", stdout); #endif using namespace Zeonfai;
n = getInt();
for(int i = 1; i <= n; ++ i) st[i].clear();
for(int i = 1; i <= n; ++ i)
{
col[i] = getInt();
if(st[col[i]].empty()) pre[i] = 0; else pre[i] = *getPrevious(st[col[i]].find(i));
st[col[i]].insert(i);
}
seg.build();
int m = getInt();
for(int i = 0; i < m; ++ i)
{
int opt = getInt();
if(opt)
{
int pos = getInt(), x = getInt();
iter p = getNext(st[col[pos]].find(pos));
st[col[pos]].erase(st[col[pos]].find(pos));
if(p != st[col[pos]].end()) pre[*p] = p == st[col[pos]].begin() ? 0 : *getPrevious(p);
if(p != st[x].end()) seg.modify(*p, pre[*p]);
col[pos] = x; st[x].insert(pos);
pre[pos] = st[x].find(pos) == st[x].begin() ? 0 : *getPrevious(st[x].find(pos));
p = getNext(st[x].find(pos)); if(p != st[x].end()) pre[*p] = pos;
if(p != st[x].end()) seg.modify(*p, pre[*p]);
seg.modify(pos, pre[pos]);
}
else printf("%lld\n", (long long)n * (n + 1) / 2 - seg.nd[1].sum);
}
}

2016北京集训测试赛(十七)Problem C: 数组的更多相关文章

  1. [2016北京集训测试赛7]isn-[树状数组+dp+容斥]

    Description Solution 定义dp[i][j]为在1到i个数中选了j个数,并且保证选了i的选法总数. dp[i][j]为所有满足A[k]>A[i]的k(k<i)的dp[k] ...

  2. 2016北京集训测试赛(十七)Problem B: 银河战舰

    Solution 好题, 又是长链剖分2333 考虑怎么统计答案, 我场上的思路是统计以一个点作为结尾的最长上升链, 但这显然是很难处理的. 正解的方法是统计以每个点作为折弯点的最长上升链. 具体的内 ...

  3. 2016北京集训测试赛(十七)Problem A: crash的游戏

    Solution 相当于要你计算这样一个式子: \[ \sum_{x = 0}^m \left( \begin{array}{} m \\ x \end{array} \right) \left( \ ...

  4. 2016北京集训测试赛(十六)Problem C: ball

    Solution 这是一道好题. 考虑球体的体积是怎么计算的: 我们令\(f_k(r)\)表示\(x\)维单位球的体积, 则 \[ f_k(1) = \int_{-1}^1 f_{k - 1}(\sq ...

  5. 2016北京集训测试赛(十六)Problem B: river

    Solution 这题实际上并不是构造题, 而是一道网络流. 我们考虑题目要求的一条路径应该是什么样子的: 它是一个环, 并且满足每个点有且仅有一条出边, 一条入边, 同时这两条边的权值还必须不一样. ...

  6. 2016北京集训测试赛(十六)Problem A: 任务安排

    Solution 这道题告诉我们, 不能看着数据范围来推测正解的时间复杂度. 事实证明, 只要常数足够小, \(5 \times 10^6\)也是可以跑\(O(n \log n)\)算法的!!! 这道 ...

  7. BZOJ 4543 2016北京集训测试赛(二)Problem B: thr 既 长链剖分学习笔记

    Solution 这题的解法很妙啊... 考虑这三个点可能的形态: 令它们的重心为距离到这三个点都相同的节点, 则其中两个点分别在重心的两棵子树中, 且到重心的距离相等; 第三个点可能在重心的一棵不同 ...

  8. 2016北京集训测试赛(十四)Problem B: 股神小D

    Solution 正解是一个\(\log\)的link-cut tree. 将一条边拆成两个事件, 按照事件排序, link-cut tree维护联通块大小即可. link-cut tree维护子树大 ...

  9. 2016北京集训测试赛(十四)Problem A: 股神小L

    Solution 考虑怎么卖最赚钱: 肯定是只卖不买啊(笑) 虽然说上面的想法很扯淡, 但它确实能给我们提供一种思路, 我们能不买就不买; 要买的时候就买最便宜的. 我们用一个优先队列来维护股票的价格 ...

随机推荐

  1. Spring核心技术(十四)——ApplicationContext的额外功能

    在前文的介绍中我们知道,org.springframework.beans.factory包提供了一些基本的功能来管理和控制Bean,甚至通过编程的方式来实现.org.springframework. ...

  2. CSU 1326: The contest(分组背包)

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1326 题意: n个题目,每个题目都有一个价值Pi和相对能力消耗Wi,但是有些题目因为太坑不能同时做 ...

  3. Leetcode 516.最长回文子序列

    最长回文子序列 给定一个字符串s,找到其中最长的回文子序列.可以假设s的最大长度为1000. 示例 1:输入: "bbbab" 输出: 4 一个可能的最长回文子序列为 " ...

  4. [转]Linux 技巧:让进程在后台可靠运行的几种方法

    转自: https://www.ibm.com/developerworks/cn/linux/l-cn-nohup/index.html 我们经常会碰到这样的问题,用 telnet/ssh 登录了远 ...

  5. Spring整合Hibernate与Struts

    整合S2SH 一.导入jar包 Spring jar包 Hibernate jar包 Struts2 jar包 以上就是整合需要的所有jar包,当然其中有重复的包,(对比之后去掉版本低的就可以了,还有 ...

  6. CH Round #59 - OrzCC杯NOIP模拟赛day1

    第一题:队爷的新书 题意简述:给定n个闭区间,求出一个数p使它与包含它的区间数的积最大,输出这个积. 分析:使用一个差分数组g,每个区间[l,r],l位置加1,r+1的位置减1,从前往后统计,得到对于 ...

  7. 【bzoj2179】FFT快速傅立叶 FFT

    题目描述 给出两个n位10进制整数x和y,你需要计算x*y. 输入 第一行一个正整数n. 第二行描述一个位数为n的正整数x. 第三行描述一个位数为n的正整数y. 输出 输出一行,即x*y的结果. 样例 ...

  8. modulus CRT

    (吐槽)额..CRT本来就是modulus的么.. CRT是可以每次加一个条件的(当然要保证coprime) 那么我们考虑 x=a (mod p1) x=b (mod p2) 这样的话我们知道 x=a ...

  9. 使用Google的Gson实现对象和json字符串之间的转换

    使用Google的Gson实现对象和json字符串之间的转换 需要gson.jar 1.JsonUtil.java package com.snail.json; import java.lang.r ...

  10. hdu 3535 背包综合题

    /* 有n组背包,每组都有限制 0.至少选一项 1.最多选一项 2.任意选 */ #include <iostream> #include <cstdio> #include ...