George and Cards

我们找到每个要被删的数字左边和右边第一个比它小的没被删的数字的位置。然后从小到大枚举要被删的数, 求答案。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long
using namespace std; const int N = 1e6 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-; int n, k, top, p[N], b[N];
int L[N], R[N];
bool ban[N];
PII stk[N];
LL ans;
vector<int> vc; struct Bit {
int a[N];
inline void modify(int x, int v) {
for(int i = x; i < N; i += i & -i)
a[i] += v;
}
inline int sum(int x) {
int ans = ;
for(int i = x; i; i -= i & -i)
ans += a[i];
return ans;
}
inline int query(int L, int R) {
if(L > R) return ;
return sum(R) - sum(L - );
}
} bit; bool cmp(const int& a, const int& b) {
return p[a] < p[b];
} int main() {
scanf("%d%d", &n, &k);
for(int i = ; i <= n; i++) bit.modify(i, );
for(int i = ; i <= n; i++) scanf("%d", &p[i]);
for(int i = ; i <= k; i++) scanf("%d", &b[i]), ban[b[i]] = true;
for(int i = ; i <= n; i++) {
if(ban[p[i]]) {
while(top && stk[top].fi > p[i]) top--;
stk[++top] = mk(p[i], i);
} else {
int pos = lower_bound(stk + , stk + top + , mk(p[i], )) - stk - ;
L[i] = pos ? stk[pos].se : ;
}
}
top = ;
for(int i = n; i >= ; i--) {
if(ban[p[i]]) {
while(top && stk[top].fi > p[i]) top--;
stk[++top] = mk(p[i], i);
} else {
int pos = lower_bound(stk + , stk + top + , mk(p[i], )) - stk - ;
R[i] = pos ? stk[pos].se : n + ;
}
}
for(int i = ; i <= n; i++)
if(!ban[p[i]]) vc.push_back(i);
sort(vc.begin(), vc.end(), cmp);
for(auto& x : vc) {
ans += bit.query(L[x] + , R[x] - );
bit.modify(x, -);
}
printf("%lld\n", ans);
return ;
} /*
*/

Codeforces 387E George and Cards的更多相关文章

  1. Codeforces Round #227 (Div. 2) E. George and Cards set内二分+树状数组

    E. George and Cards   George is a cat, so he loves playing very much. Vitaly put n cards in a row in ...

  2. Codeforces Round #227 (Div. 2) E. George and Cards 线段树+set

    题目链接: 题目 E. George and Cards time limit per test:2 seconds memory limit per test:256 megabytes 问题描述 ...

  3. Codeforces 731 F. Video Cards(前缀和)

    Codeforces 731 F. Video Cards 题目大意:给一组数,从中选一个数作lead,要求其他所有数减少为其倍数,再求和.问所求和的最大值. 思路:统计每个数字出现的个数,再做前缀和 ...

  4. Codeforces 467C George and Job(DP)

    题目 Source http://codeforces.com/contest/467/problem/C Description The new ITone 6 has been released ...

  5. codeforces B. George and Round 解题报告

    题目链接:http://codeforces.com/contest/387/problem/B 题目意思:给出1-n个问题,以及要满足是good rounde条件下这n个问题分别需要达到的compl ...

  6. codeforces 467C.George and Job 解题报告

    题目链接:http://codeforces.com/problemset/problem/467/C 题目意思:给出一条含有 n 个数的序列,需要从中找出 k 对,每对长度为 m 的子序列,使得 找 ...

  7. Codeforces 467C. George and Job (dp)

    题目链接:http://codeforces.com/contest/467/problem/C 求k个不重叠长m的连续子序列的最大和. dp[i][j]表示第i个数的位置个序列的最大和. 前缀和一下 ...

  8. George and Cards

    Codeforces Round #227 (Div. 2) E:http://codeforces.com/contest/387/problem/E 题意:给你一个n个数的序列,然后给你一个标准序 ...

  9. cf E. George and Cards

    http://codeforces.com/contest/387/problem/E 题意:给你n个数,然后在输入k个数,这k个数都在n个数中出现,进行每一次操作就是在n个数中选择长度为w的连续序列 ...

随机推荐

  1. interface{} 泛型编程

    转自: 张晓龙 中兴开发者社区 https://mp.weixin.qq.com/s/EEUtTykcrXhcM2hJT01SoQ 序言 众所周知,Golang中不支持类似C++/Java中的标记式泛 ...

  2. HBase Snapshot简介

    一.简介 HBase 从0.95开始引入了Snapshot,可以对table进行Snapshot,也可以Restore到Snapshot.Snapshot可以在线做,也可以离线做.Snapshot的实 ...

  3. <转载>ford-fulkerson算法2

    原文链接https://www.cnblogs.com/luweiseu/archive/2012/07/14/2591573.html 作者:wlu 7. 网络流算法--Ford-Fulkerson ...

  4. Windows平台下,Java性能分析工具VisualVM的Tomcat8的配置

    VisualVM在JDK6版本及以上已经自带这个应用. 位置:C:\Program Files (x86)\Java\jdk1.8.0_60\bin\jvisualvm.exe   在Windows环 ...

  5. http请求在ie中F12查看显示已挂起

    页面有解析和运算工作之后 http新请求在ie中F12查看显示已挂起,http post ,请求返回少量数据

  6. 腾讯云CVM之间配置免密钥登录

    背景: 1客户A和B俩台主机之间需要实现免密钥登录,已绑定腾讯云申请的密钥对 系统:centos7.3 A:192.168.0.100 B:192.168.0.84 A主机的私钥文件:aaa B主机的 ...

  7. PID控制器开发笔记之六:不完全微分PID控制器的实现

    从PID控制的基本原理我们知道,微分信号的引入可改善系统的动态特性,但也存在一个问题,那就是容易引进高频干扰,在偏差扰动突变时尤其显出微分项的不足.为了解决这个问题人们引入低通滤波方式来解决这一问题. ...

  8. Confluence 6 MBeans

    你可以使用下面的 Confluence MBeans  来实时查看你 Confluence 实例运行的实时信息. CacheStatistics 这个 MBean 显示了 Confluence 有关的 ...

  9. D3.js+Es6+webpack构建人物关系图(力导向图),动态更新数据,点击增加节点,拖拽增加连线...

    觉得不错的麻烦加个Star:https://github.com/zhangzn3/D3-Es6 在线预览地址:https://zhangzn3.github.io/D3-Es6 功能列表:1. 增加 ...

  10. 深入理解 Vue Computed 计算属性

    Computed 计算属性是 Vue 中常用的一个功能,我们今天来说一下他的执行过长 拿官网简单的例子来看一下: <div id="example"> <p> ...