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. python操作三大主流数据库(2)python操作mysql②python对mysql进行简单的增删改查

    python操作mysql②python对mysql进行简单的增删改查 1.设计mysql的数据库和表 id:新闻的唯一标示 title:新闻的标题 content:新闻的内容 created_at: ...

  2. 转载:UML学习(三)-----序列图(silent)

    原文:http://www.cnblogs.com/silent2012/archive/2011/09/14/2172219.html UML的模型中可分为两种,动态模型和静态模型.用例图.类图和对 ...

  3. ubuntu 问题

    1.打开ubuntu之后的开启页面出现:所选模式均不匹配可能的模式:为 CRTC 63 尝试模式CRTC 63:尝试 800x600@60Hz 模式输出在 1366x768@60Hz (通过 0)CR ...

  4. Ex 2_22 两个有序列表合并后的第k小元素..._第四次作业

    package org.xiu68.ch02; public class Ex2_22 { public static void main(String[] args) { // TODO Auto- ...

  5. python元组,集合类型,及字典补充

    一.元组 元组与列表基本相同,不同之处在于元组只能存不能取,当多个值没有改的需求时,用元组更合适 元组的基本操作 1.创建元组: t = (1, 2, 3, 4, 2,4,) t = (1,) #单个 ...

  6. 牛客网 python 求解立方根

    •计算一个数字的立方根,不使用库函数 详细描述: •接口说明 原型: public static double getCubeRoot(double input) 输入:double 待求解参数 返回 ...

  7. django 中自带的加密方法

    导入django 自带的加密算法 和flask中的哈希加密有一曲同工之妙.        from django.contrib.auth.hashers import make_password, ...

  8. Advanced Wlan Attacks (RADIUS)

    1.查询连接到无线接入点的情况 使用命令 airodump-ng  wlan0mon  可以看到 有用的信息.我们知道如果有一个客户端使用验证码成功连接到. 顺便查一下其中一个连接的设备的MAC地址的 ...

  9. HTML添加图像和超链接

    添加图像 <img src="图像的文件地址" alt="加载失败" height="350" width="350&quo ...

  10. NPOI操作Excel(二)--创建Excel并设置样式

    由于XSSF中的XSSFWorkbook和HSSF中的HSSFWorkbook拥有的属性.方法等都是一样的,故下面就已一个为例做为展示,他们都继承与一个接口:IWorkbook(命名空间:using  ...