转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove

出题人题解没看懂。。。囧。

然后看了下tourist代码,很短,也很好理解。。。

我们将b排序之后,很显然如果组合的话肯定是贪心。

那么对于a的某个子串a'要满足条件的话,那么显然是所有的数和b中最大元素相加不小于h。

至少有len - 1个数的b中次大元素相加不小于h。。。以此类推那么首先预处理出对于a中的每个元素,和b串的哪些元素相加不小于h,显然是排序之后的二分

那么选中某个区间的数,就是一个区间覆盖,判断b中第i大元素是否至少被覆盖了i次。

为了方便,我们先将第i位减去一个i,然后判断区间最小值是否非负。

#include <iostream>
#include <queue>
#include <algorithm>
#include <cstdio>
#include <cstring>
#define lson step << 1
#define rson step << 1 | 1
using namespace std;
typedef long long LL;
const int N = 150005;
struct Node {
int left , right , add , mn;
}L[N << 2];
int n , h , b[N] , len , a[N];
void bulid (int step , int l , int r) {
L[step].left = l;
L[step].right = r;
L[step].add = 0;
L[step].mn = 0;
if (l == r) return ;
int m = (l + r) >> 1;
bulid (lson , l , m);
bulid (rson , m + 1 , r);
}
void update (int step , int l , int r , int v);
void push_down (int step) {
int l = L[step].left , r = L[step].right , m = (l + r) >> 1;
if (L[step].add) {
update (lson , l , m , L[step].add);
update (rson , m + 1 , r , L[step].add);
L[step].add = 0;
}
}
void push_up (int step) {
L[step].mn = min (L[lson].mn , L[rson].mn);
}
void update (int step , int l , int r , int v) {
if (L[step].left == l && L[step].right == r) {
L[step].mn += v;
L[step].add += v;
return ;
}
push_down (step);
int m = (L[step].left + L[step].right) >> 1;
if (r <= m) update (lson , l , r , v);
else if (l > m) update (rson , l , r , v);
else {
update (lson , l , m , v);
update (rson , m + 1 , r , v);
}
push_up (step);
}
int main() {
int t;
#ifndef ONLINE_JUDGE
freopen ("input.txt" , "r" , stdin);
// freopen ("output.txt" , "w" , stdout);
#endif
scanf ("%d %d %d" , &n , &len , &h);
for (int i = 0 ; i < len ; i ++)
scanf ("%d" , &b[i]);
sort (b , b + len);
bulid (1 , 0 , len);
for (int i = 0 ; i < n ; i ++) {
scanf ("%d" , &a[i]);
a[i] = lower_bound (b , b + len , h - a[i]) - b;
}
for (int i = 0 ; i < len ; i ++)
update (1 , i , i , -(i + 1));
for (int i = 0 ; i < len - 1 ; i ++)
update (1 , a[i] , len , 1);
int ans = 0;
for (int i = len - 1 ; i < n ; i ++) {
update (1 , a[i] , len , 1);
if (L[1].mn >= 0) ans ++;
update (1 , a[i - len + 1] , len , -1);
}
printf ("%d\n" , ans);
return 0;
}

CF 338E Optimize! (线段树)的更多相关文章

  1. cf 786 B 线段树优化建图

    cf 786 B 链接 CF 思路 n个点,3种建边方式,规模\(O(n^2)\) 线段树优化建图 注意 读入的数据好坑啊,说好的v,u变成了u,v. 两棵树,一棵出,一棵入.线段树的作用只不过是按照 ...

  2. [CF 474E] Pillars (线段树+dp)

    题目链接:http://codeforces.com/contest/474/problem/F 意思是给你两个数n和d,下面给你n座山的高度. 一个人任意选择一座山作为起始点,向右跳,但是只能跳到高 ...

  3. CF 19D - Points 线段树套平衡树

    题目在这: 给出三种操作: 1.增加点(x,y) 2.删除点(x,y) 3.询问在点(x,y)右上方的点,如果有相同,输出最左边的,如果还有相同,输出最低的那个点 分析: 线段树套平衡树. 我们先离散 ...

  4. Codeforces 338E - Optimize!(Hall 定理+线段树)

    题面传送门 首先 \(b_i\) 的顺序肯定不会影响匹配,故我们可以直接将 \(b\) 数组从小到大排个序. 我们考虑分析一下什么样的长度为 \(m\) 的数组 \(a_1,a_2,\dots,a_m ...

  5. CF 666E Forensic Examination 【SAM 倍增 线段树合并】

    CF 666E Forensic Examination 题意: 给出一个串\(s\)和\(n\)个串\(t_i\),\(q\)次询问,每次询问串\(s\)的子串\(s[p_l:p_r]\)在串\(t ...

  6. CF 552(div 3) E Two Teams 线段树,模拟链表

    题目链接:http://codeforces.com/contest/1154/problem/E 题意:两个人轮流取最大值与旁边k个数,问最后这所有的数分别被谁给取走了 分析:看这道题一点思路都没有 ...

  7. CF 1023D Array Restoration - 线段树

    题解 非常容易想到的线段树, 还可以用并查集来. 还有一位大神用了$O(n)$ 就过了Orz 要判断是否能染色出输入给出的序列,必须满足两个条件: 1. 序列中必须存在一个$q$ 2. 两个相同的数$ ...

  8. CF 787D Legacy(线段树思想构图+最短路)

    D. Legacy time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...

  9. DFS序+线段树+bitset CF 620E New Year Tree(圣诞树)

    题目链接 题意: 一棵以1为根的树,树上每个节点有颜色标记(<=60),有两种操作: 1. 可以把某个节点的子树的节点(包括本身)都改成某种颜色 2. 查询某个节点的子树上(包括本身)有多少个不 ...

随机推荐

  1. iOS开发之第三方登录QQ -- 史上最全最新第三方登录QQ方式实现

    项目地址 :  https://github.com/zhonggaorong/QQLoginDemo/tree/master 最新版本的qq登录实现步骤实现: 1. 首先,你需要去向腾讯申请账号. ...

  2. mina教程

    关于mina介绍这里不做阐述..... 我们先做一个关于mina的helloworld 首先先下载mina包:http://mina.apache.org/ (如果你已经下载,此步骤忽略) 下载下来以 ...

  3. win7 VMware Workstation Centos6.5虚机桥接上网设置 详解(靠谱)

    1.VMware Workstation 设置 2. vim /etc/sysconfig/network-scripts/ifcfg-eth0 NAME="System eth0" ...

  4. python2.7_1.2_打印设备名和IPv4地址

    代码如下: # -*- coding: utf-8 -*- import socket def print_machine_info(): host_name = socket.gethostname ...

  5. [LeetCode]题解(python):066-Plus One

    题目来源: https://leetcode.com/problems/plus-one/ 题意分析: 给定一个数组,将数加一,返回新的数组.比如[9,9],返回[1,0,0]. 题目思路: 这道题目 ...

  6. Nginx阅读笔记(二)之location的用法

    两个配置文件 一: server { listen 80; # # 在本机所有ip上监听80,也可以写为192.168.1.202:80,这样的话,就只监听192.168.1.202上的80口 ser ...

  7. Windows Phone 8初学者开发—第13部分:设置LongListSelector中磁贴的样式

    原文 Windows Phone 8初学者开发—第13部分:设置LongListSelector中磁贴的样式 第13部分:设置LongListSelector中磁贴的样式 原文地址: http://c ...

  8. 隐藏APK在Launcher中的启动图标 android开发教程

    隐藏APK在Launcher中的启动图标: APK的AndroidManifest.xml文件的主Activity中删除 intent-filter 中的 <category android:n ...

  9. Roseonly:互联网轻奢品牌之路-搜狐IT

    Roseonly:互联网轻奢品牌之路-搜狐IT Roseonly:互联网轻奢品牌之路

  10. MFC下的日历表

    // CalenderDlg.h : header file // #if !defined(AFX_CALENDERDLG_H__8DC8F113_2A47_45B8_8266_75CB406D68 ...