可以推出

min[i]要么是i要么是1,当a序列中存在这个数是1

max[i]的话就比较麻烦了

首先对于i来说,如果还没有被提到第一位的话,他的max可由他后面的这部分序列中 j>=i 的不同数多少所决定,这个可以用树状数组解决

其次就是两次被提到第一位的中间的空当,这个空当中不同的数的大小,也会决定max,这里的解法比较多样,我用的是主席树

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define MP make_pair
#define ll long long
#define ld long double
#define null NULL
#define all(a) a.begin(), a.end()
#define forn(i, n) for (int i = 0; i < n; ++i)
#define sz(a) (int)a.size()
// #define lson l , m , rt << 1
// #define rson m + 1 , r , rt << 1 | 1
#define bitCount(a) __builtin_popcount(a)
template<class T> int gmax(T &a, T b) { if (b > a) { a = b; return 1; } return 0; }
template<class T> int gmin(T &a, T b) { if (b < a) { a = b; return 1; } return 0; }
using namespace std;
const int INF = 0x3f3f3f3f;
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string((string) s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B>
string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; }
template <typename A>
string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; }
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); }
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif // const int MAXN = 3e5 + 5;
// const int M = MAXN * 100;
int n, m;
vector<int> T, lson, rson, c;
int tot = 0; int newNode() {
lson.push_back(0); rson.push_back(0); c.push_back(0);
return tot ++;
}
int build(int l, int r) {
int root = newNode();
c[root] = 0;
if(l != r) {
int mid = (l + r) >> 1;
lson[root] = build(l, mid);
rson[root] = build(mid + 1, r);
}
return root;
} int update(int root, int pos, int val) {
int newroot = newNode(), tmp = newroot;
c[newroot] = c[root] + val;
int l = 1, r = n;
while(l < r) {
int mid = (l + r) >> 1;
if(pos <= mid) {
lson[newroot] = newNode(); rson[newroot] = rson[root];
newroot = lson[newroot]; root = lson[root];
r = mid;
} else {
rson[newroot] = newNode(); lson[newroot] = lson[root];
newroot = rson[newroot]; root = rson[root];
l = mid + 1;
}
c[newroot] = c[root] + val;
}
return tmp;
} int query(int root, int pos) {
int ret = 0;
int l = 1, r = n;
while(pos < r) {
int mid = (l + r) >> 1;
if(pos <= mid) {
r = mid;
root = lson[root];
} else {
ret += c[lson[root]];
root = rson[root];
l = mid + 1;
}
}
return ret + c[root];
} class BIT {
private:
vector<int> tree;
int treesize;
public:
BIT(int x) {
tree.resize(x + 5, 0);
treesize = x + 5;
} int Sum(int x) {
if(x <= 0) return 0;
if(x > treesize) x = treesize;
int ans = 0;
while(x > 0) {
ans += tree[x];
x -= x & -x;
}
return ans;
}
void Add(int x, int d) {
// debug(x);
while(x <= treesize) {
tree[x] += d;
x += x & -x;
}
}
}; int main() { while(~scanf("%d %d", &n, &m)) {
vector<int> vc;
vector<int> mp(max(n,m) + 5, 0);
vector<int> maxx(n + 5, 0);
vector<int> minn(n + 5, 0);
vector<vector<int> > E(n + 5, vector<int>());
for(int i = 1; i <= n; ++i) {
maxx[i] = minn[i] = i;
} for(int i = 0; i < m; ++i) {
int t; scanf("%d", &t);
vc.push_back(t);
E[t].push_back(i + 1);
minn[t] = 1;
} BIT bit = BIT(n + 5);
// set<int> st;
for(int i = 0; i < m; ++i) {
if(mp[vc[i]] == 0) {
bit.Add(vc[i], 1);
maxx[vc[i]] = max(maxx[vc[i]], vc[i] + bit.Sum(n) - bit.Sum(vc[i]));
debug(i, vc[i], maxx[vc[i]]);
}
mp[vc[i]] = 1;
} for(int i = 1; i <= n; ++i) {
if(mp[i] == 0) {
maxx[i] = max(maxx[i], i + bit.Sum(n) - bit.Sum(i));
// debug(i, maxx[i]);
}
} tot = 0;
T.clear();
for(int i = 0; i < m + 5; ++i) T.push_back(i);
T[m + 1] = build(1, m);
for(int i = 1; i <= n; ++i) mp[i] = 0;
for(int i = m; i >= 1; -- i) {
int target = vc[i-1];
if(mp[target] == 0) {
T[i] = update(T[i + 1], i, 1);
} else {
int tmp = update(T[i + 1], mp[target], -1);
T[i] = update(tmp, i, 1);
}
mp[target] = i;
} debug(query(T[m + 1], m + 1), query(T[m + 1], m)); for(int i = 1; i <= n; ++i) {
int pre = m + 1;
for(int j = E[i].size() - 1; j >= 0; --j) {
int tmp = query(T[E[i][j]], pre - 1);
maxx[i] = max(maxx[i], tmp);
// debug(i, E[i][j] + 1, pre - 1, tmp);
pre = E[i][j];
}
} for(int i = 1; i <= n; ++i) {
printf("%d %d\n", minn[i], maxx[i]);
}
}
return 0;
}

官方给出了一种比较新颖的直接线段树的做法,我觉得写的非常有趣,

#include <bits/stdc++.h>

#define forn(i, n) for (int i = 0; i < int(n); i++)
#define x first
#define y second using namespace std; const int N = 300 * 1000 + 13; typedef pair<int, int> pt; int n;
int a[N];
vector<int> pos[N];
pt ans[N];
int prv[N]; vector<int> t[4 * N]; void build(int v, int l, int r){
if (l == r - 1){
t[v].push_back(prv[l]);
return;
}
int m = (l + r) / 2;
build(v * 2, l, m);
build(v * 2 + 1, m, r);
t[v].resize(r - l);
merge(t[v * 2].begin(), t[v * 2].end(), t[v * 2 + 1].begin(), t[v * 2 + 1].end(), t[v].begin());
} int get(int v, int l, int r, int L, int R, int val){
if (L >= R)
return 0;
if (l == L && r == R)
return lower_bound(t[v].begin(), t[v].end(), val) - t[v].begin();
int m = (l + r) / 2;
return get(v * 2, l, m, L, min(m, R), val) + get(v * 2 + 1, m, r, max(m, L), R, val);
} int f[N]; void upd(int x){
for (int i = x; i >= 0; i = (i & (i + 1)) - 1)
++f[i];
} int get(int x){
int res = 0;
for (int i = x; i < N; i |= i + 1)
res += f[i];
return res;
} int main() {
int n, m;
scanf("%d%d", &n, &m);
forn(i, m){
scanf("%d", &a[i]);
--a[i];
}
forn(i, m){
pos[a[i]].push_back(i);
} vector<pt> qr;
forn(i, n){
for (int j = 1; j < int(pos[i].size()); ++j)
qr.push_back(make_pair(pos[i][j - 1] + 1, pos[i][j] - 1));
if (!pos[i].empty())
qr.push_back(make_pair(pos[i].back() + 1, m - 1));
} forn(i, n) ans[i] = {i, i};
forn(i, m) ans[a[i]].x = 0; forn(i, n){
int cur = -1;
for (auto it : pos[i]){
prv[it] = cur;
cur = it;
}
}
build(1, 0, m); forn(i, qr.size()){
int l = qr[i].x;
int r = qr[i].y;
if (r < l) continue;
int x = a[qr[i].x - 1];
int cnt = get(1, 0, m, l, r + 1, l);
ans[x].y = max(ans[x].y, cnt);
} forn(i, m){
if (i == pos[a[i]][0]){
ans[a[i]].y = max(ans[a[i]].y, a[i] + get(a[i]));
upd(a[i]);
}
}
forn(i, n) if (pos[i].empty()){
ans[i].y = max(ans[i].y, i + get(i));
} forn(i, n) printf("%d %d\n", ans[i].x + 1, ans[i].y + 1);
return 0;
}

Educational Codeforces Round 80 (Rated for Div. 2) E. Messenger Simulator的更多相关文章

  1. Educational Codeforces Round 80 (Rated for Div. 2)

    A. Deadline 题目链接:https://codeforces.com/contest/1288/problem/A 题意: 给你一个 N 和 D,问是否存在一个 X , 使得 $x+\lce ...

  2. Educational Codeforces Round 80 (Rated for Div. 2)D E

    D枚举子集 题:https://codeforces.com/contest/1288/problem/D题意:给定n个序列,每个序列m个数,求第i个和第j个序列组成b序列,b序列=max(a[i][ ...

  3. Educational Codeforces Round 80 (Rated for Div. 2)部分题解

    A. Deadline 题目链接 题目大意 给你\(n,d\)两个数,问是否存在\(x\)使得\(x+\frac{d}{x+1}\leq n\),其中\(\frac{d}{x+1}\)向上取整. 解题 ...

  4. Educational Codeforces Round 80 (Rated for Div. 2)(A-E)

    C D E 这三道题感觉挺好       决定程序是否能通过优化在要求的时间内完成,程序运行时间为t,你可以选择花X天来优化,优化后程序的运行时间为t/(x+1)取上整,花费的时间为程序运行时间加上优 ...

  5. Educational Codeforces Round 80 (Rated for Div. 2)E(树状数组,模拟,思维)

    #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; ],mx[],a[],pos[],sum ...

  6. Educational Codeforces Round 80 (Rated for Div. 2)D(二分答案,状压检验)

    这题1<<M为255,可以logN二分答案后,N*M扫一遍表把N行数据转化为一个小于等于255的数字,再255^2检验答案(比扫一遍表复杂度低),复杂度约为N*M*logN #define ...

  7. Educational Codeforces Round 80 (Rated for Div. 2)C(DP)

    #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; ; ][],temp[][]; int ...

  8. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

  9. Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...

随机推荐

  1. luoguP4313 文理分科

    luoguP4313 文理分科 复习完之后做了道典型题目. 这道题条件有点多 我们逐个分析 如果没有\(sameart\)或者\(samescience\)的限制,就是一个裸的最大权闭合子图的问题了 ...

  2. Android Simulator Shortcut keys

    按钮 快捷键 Back Ctrl+Backspace Battery Ctrl+Shift+B Cellular Ctrl+Shift+C D-pad Ctrl+Shift+D Enter zoom ...

  3. 思数云hadoop目录

    全文检索.数据分析挖掘.推荐系统.广告系统.图像识别.海量存储.快速查询 l Hadoop介绍 n Hadoop来源与历史 n Hadoop版本 n Hadoop开源与商业 l HDFS系统架构 n ...

  4. Docker应用容器引擎

    1.Docker概述 1.1.Docker简介 Docker 是一个开源的应用容器引擎,基于 Go 语言开发.Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级.可移植的容器中,然后发布到 ...

  5. ELK系统分析nginx日志

    一.nginx nginx 服务器日志的log_format格式: log_format main '$remote_addr - $remote_user [$time_local] "$ ...

  6. react-native-vector-icons 安装、使用

    react-native-vector-icons 安装.使用 前言 任何库的安装与使用都离不开官文,按照官方文档一步步操作可以规避大多数问题.不过很多库只有英文文档,想要完全参透需要时间.react ...

  7. 泛圈科技Yottachain区块链云存储打破传统云迎来价值数据存储

    随着物联网时代的发展,更多的数据随之产生.从智能设备到电脑再到视频游戏机,各种各样的信息从不同的电子产品源源不断地涌入.通常,人们将数据存储在本地驱动器中.但是,由于产生的数据量是无限的,超过了本地存 ...

  8. 通过nginx搭建基于python的web环境

    前言: 在搭建开始前,我们先来梳理下web服务工作流程,先看下图: 1.用户(PC)向web服务器发起http请求 2.web服务器判断用户请求文件是否为静态文件,是则直接读取静态文件并返回给用户,不 ...

  9. HTTP 安全头配置

    在本篇中,我将介绍常用的安全头信息设置,并对每个响应头设置给出一个示例. HTTP安全头说明 Content-Security-Policy 内容安全策略(CSP)常用来通过指定允许加载哪些资源来防止 ...

  10. 解决echarts中的点击事件点击后走多次接口

    使用echarts图点击图之后,走了很多次接口,后来发现添加一个off事件就可以解决了,具体如下: