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 考虑怎么卖最赚钱: 肯定是只卖不买啊(笑) 虽然说上面的想法很扯淡, 但它确实能给我们提供一种思路, 我们能不买就不买; 要买的时候就买最便宜的. 我们用一个优先队列来维护股票的价格 ...
随机推荐
- javascript 实现九九乘法表
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- bash shell命令与监测的那点事(三)
bash shell命令与监测的那点事之df与du 前两篇介绍了bash shell的进程监控指令,但是有时候你需要知道在某个设备上还有多少磁盘空间.首先介绍df命令: df命令 df命令就是用来轻松 ...
- 【Swap Nodes in Pairs】cpp
题目: Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1-> ...
- 【Remove Duplicates from Sorted Array II】cpp
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- STL学习笔记3--deque
看这一节,是为了下一节的使用,在ogre3d里有些操作要使用到deque. C++ Deque(双向队列) 的使用 Deque结合了vector 和list 优缺点,是一种使用简单的容器. deq ...
- 带线表格据gt生成无线表格
json解析练习,python图像处理练习,表格包围框毛刺问题待解决. # -*- coding: utf-8 -*- # coding: utf-8 from PIL import Image, I ...
- Leetcode 507.完美数
完美数 对于一个 正整数,如果它和除了它自身以外的所有正因子之和相等,我们称它为"完美数". 给定一个 正整数 n, 如果他是完美数,返回 True,否则返回 False 示例: ...
- [python][django学习篇][15]博客侧栏--自定义模板标签
我们的博客侧边栏有四项内容:最新文章.归档.分类和标签云. 这些内容相对比较固定,且在各个页面都会显示,如果像文章列表或者文章详情一样,从视图函数中获取然后传递给模板,则每个页面对应的视图函数里都要写 ...
- gym101532 2017 JUST Programming Contest 4.0
台州学院ICPC赛前训练5 人生第一次ak,而且ak得还蛮快的,感谢队友带我飞 A 直接用claris的模板啊,他模板确实比较强大,其实就是因为更新的很快 #include<bits/stdc+ ...
- SQLServer存储引擎——06.索引的遍历与维护
一.遍历 索引树的每个节点都是一个页面. 索引树有三种类型的节点:根节点.中间节点.叶子节点. (1) 根节点与中间节点一样,只包含下一层节点的入口值与入口指针,它们称为索引节点: (2) 叶子节点包 ...