2016北京集训测试赛(十七)Problem C: 数组

Solution
线段树好题.
我们考虑用last[i]表示\(i\)这个位置的颜色的上一个出现位置. 考虑以一个位置\(R\)为右端点的区间最远能向左延伸到什么位置: \(L = \max_{i \le j} 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: 数组的更多相关文章
- [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] ...
- 2016北京集训测试赛(十七)Problem B: 银河战舰
Solution 好题, 又是长链剖分2333 考虑怎么统计答案, 我场上的思路是统计以一个点作为结尾的最长上升链, 但这显然是很难处理的. 正解的方法是统计以每个点作为折弯点的最长上升链. 具体的内 ...
- 2016北京集训测试赛(十七)Problem A: crash的游戏
Solution 相当于要你计算这样一个式子: \[ \sum_{x = 0}^m \left( \begin{array}{} m \\ x \end{array} \right) \left( \ ...
- 2016北京集训测试赛(十六)Problem C: ball
Solution 这是一道好题. 考虑球体的体积是怎么计算的: 我们令\(f_k(r)\)表示\(x\)维单位球的体积, 则 \[ f_k(1) = \int_{-1}^1 f_{k - 1}(\sq ...
- 2016北京集训测试赛(十六)Problem B: river
Solution 这题实际上并不是构造题, 而是一道网络流. 我们考虑题目要求的一条路径应该是什么样子的: 它是一个环, 并且满足每个点有且仅有一条出边, 一条入边, 同时这两条边的权值还必须不一样. ...
- 2016北京集训测试赛(十六)Problem A: 任务安排
Solution 这道题告诉我们, 不能看着数据范围来推测正解的时间复杂度. 事实证明, 只要常数足够小, \(5 \times 10^6\)也是可以跑\(O(n \log n)\)算法的!!! 这道 ...
- BZOJ 4543 2016北京集训测试赛(二)Problem B: thr 既 长链剖分学习笔记
Solution 这题的解法很妙啊... 考虑这三个点可能的形态: 令它们的重心为距离到这三个点都相同的节点, 则其中两个点分别在重心的两棵子树中, 且到重心的距离相等; 第三个点可能在重心的一棵不同 ...
- 2016北京集训测试赛(十四)Problem B: 股神小D
Solution 正解是一个\(\log\)的link-cut tree. 将一条边拆成两个事件, 按照事件排序, link-cut tree维护联通块大小即可. link-cut tree维护子树大 ...
- 2016北京集训测试赛(十四)Problem A: 股神小L
Solution 考虑怎么卖最赚钱: 肯定是只卖不买啊(笑) 虽然说上面的想法很扯淡, 但它确实能给我们提供一种思路, 我们能不买就不买; 要买的时候就买最便宜的. 我们用一个优先队列来维护股票的价格 ...
随机推荐
- 有关js的一些小问题
忘了从哪里找来抄下来的: js执行顺序问题 1.函数的声明和调用 “定义式”函数声明 function fn1() {......} "赋值式"函数声明 var f=functio ...
- Algorithms(fourth edition)——无向图
1.设计图基本操作API 2.用什么数据结构来表示图并实现API 要求:(1)要预留足够空间 (2)实例方法实现要快 三个选择: 邻接矩阵:布尔矩阵,不满足条件一,而且无法表示平行边 边的数组:不满足 ...
- Java资料整理
Java资料整理 原创 2017年08月25日 17:20:44 14211 1.LocalThread的应用场景,数据传输适合用LocalThread么 2.linux的基本命令 软链接.更 ...
- Python人工智能-基于百度AI接口
参考百度AI官网:http://ai.baidu.com/ 准备工作: 支持Python版本:2.7.+ ,3.+ 安装使用Python SDK有如下方式 >如果已经安装了pip,执行 pip ...
- zookeeper 下载安装
下载:wget https://www-us.apache.org/dist/zookeeper/zookeeper-3.4.13/zookeeper-3.4.13.tar.gz 解压:tar -zx ...
- selenium webdriver——元素定位
元素定位: >>WebDriver提供了八种元素定位方法,在Python语言中,所对应的方法如下: >>id属性定位:有唯一性 find_element_by_id(" ...
- 【转】UGUI EventSystem
EventSystem The EventSystem is a way of sending events to objects in the application based on inpu ...
- 【转】Visual Studio 2013 Tools for Unity安装目录,Visual Studio 2013 Tools.unitypackage
http://blog.csdn.net/dynastyting/article/details/46505349 Visual Studio 2013 Tools for Unity安装目录 D:\ ...
- easyui在datagrid只想选择一条
<table class="" id="jgrid" data-options="fitColumns:true,rownumbers: tru ...
- iOS自定义控件创建原理(持续更新)
前言 因为如果要创建各种自定义控件根据需求的不同会有很多的差别,所以我就在这里,分析一些自定义控件的创建实现方法 弹出视图 1.把要弹出的视图装在一个控制器里面,自定义转场动画 2.创建一个弹出视图, ...