Educational Codeforces Round 80 (Rated for Div. 2) E. Messenger Simulator
可以推出
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的更多相关文章
- Educational Codeforces Round 80 (Rated for Div. 2)
A. Deadline 题目链接:https://codeforces.com/contest/1288/problem/A 题意: 给你一个 N 和 D,问是否存在一个 X , 使得 $x+\lce ...
- 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][ ...
- Educational Codeforces Round 80 (Rated for Div. 2)部分题解
A. Deadline 题目链接 题目大意 给你\(n,d\)两个数,问是否存在\(x\)使得\(x+\frac{d}{x+1}\leq n\),其中\(\frac{d}{x+1}\)向上取整. 解题 ...
- Educational Codeforces Round 80 (Rated for Div. 2)(A-E)
C D E 这三道题感觉挺好 决定程序是否能通过优化在要求的时间内完成,程序运行时间为t,你可以选择花X天来优化,优化后程序的运行时间为t/(x+1)取上整,花费的时间为程序运行时间加上优 ...
- Educational Codeforces Round 80 (Rated for Div. 2)E(树状数组,模拟,思维)
#define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; ],mx[],a[],pos[],sum ...
- Educational Codeforces Round 80 (Rated for Div. 2)D(二分答案,状压检验)
这题1<<M为255,可以logN二分答案后,N*M扫一遍表把N行数据转化为一个小于等于255的数字,再255^2检验答案(比扫一遍表复杂度低),复杂度约为N*M*logN #define ...
- Educational Codeforces Round 80 (Rated for Div. 2)C(DP)
#define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; ; ][],temp[][]; int ...
- 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 ...
- 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 ...
随机推荐
- Comet OJ - Contest #5
Comet OJ - Contest #5 总有一天,我会拿掉给\(dyj\)的小裙子的. A 显然 \(ans = min(cnt_1/3,cnt_4/2,cnt5)\) B 我们可以感性理解一下, ...
- 2018-2-13-Xamarin-Forms-进度条控件
title author date CreateTime categories Xamarin Forms 进度条控件 lindexi 2018-2-13 17:23:3 +0800 2018-2-1 ...
- Spring MVC 模拟
在Spring MVC中,将一个普通的java类标注上Controller注解之后,再将类中的方法使用RequestMapping注解标注,那么这个普通的java类就够处理Web请求,示例代码如下: ...
- ASP.NET MVC API与JS进行POST请求时传递参数 -CHPowerljp原创
在API前添加 [HttpPost] 表示只允许POST方式请求 [HttpPost] public IHttpActionResult Get_BIGDATA([FromBody]Datas ...
- 【一起学源码-微服务】Nexflix Eureka 源码九:服务续约源码分析
前言 前情回顾 上一讲 我们讲解了服务发现的相关逻辑,所谓服务发现 其实就是注册表抓取,服务实例默认每隔30s去注册中心抓取一下注册表增量数据,然后合并本地注册表数据,最后有个hash对比的操作. 本 ...
- 一文带你了解 OAuth2 协议与 Spring Security OAuth2 集成!
OAuth 2.0 允许第三方应用程序访问受限的HTTP资源的授权协议,像平常大家使用Github.Google账号来登陆其他系统时使用的就是 OAuth 2.0 授权框架,下图就是使用Github账 ...
- $vjudge-dp$专题题解
因为感觉题解写不了多少,,,就懒得一道道题目慢慢写了,汇总了算了$QAQ$ 昂然后因为我估计以后还会有些什么$dp$专题啊$balabala$的,,,然后谢总肯定又会建一堆小组啥的,,,所以还是放个链 ...
- Eclipse和Tomcat的版本问题---已解决
Eclipse和Tomcat的版本问题---已解决 这篇文章主要是解决版本匹配的问题 我的电脑上装的是jdk10,如图: Tomcat装的是9: 接着配置好环境变量,直接上图: 然后启功Tomcat, ...
- HDU3709 Balanced Number 题解 数位DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3709 题目大意: 求区间 \([x, y]\) 范围内"平衡数"的数量. 所谓平衡 ...
- 2020面试还搞不懂MyBatis?快看看这27道面试题!(含答案和思维导图)
前言 MyBatis是一个优秀的持久层ORM框架,它对jdbc的操作数据库的过程进行封装,使开发者只需要关注SQL 本身,而不需要花费精力去处理例如注册驱动.创建connection.创建statem ...