Codeforces 609F Frogs and mosquitoes 线段树
用线段树维护每个点覆盖的最小id, 用multiset维护没有吃的蚊子。
#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 = 2e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = ;
const double eps = 1e-;
const double PI = acos(-); struct Forg {
int x, cnt, id;
LL t;
bool operator < (const Forg& rhs) const {
return x < rhs.x;
}
void print() {
printf("id: %d x: %d cnt: %d t: %lld\n", id, x, cnt, t);
}
}; int n, m;
Forg frog[N];
PII mos[N]; LL anst[N], ansc[N]; vector<LL> oo;
multiset<PII> uneat; int getPosL(LL x) {
return lower_bound(oo.begin(), oo.end(), x) - oo.begin() + ;
}
int getPosR(LL x) {
return upper_bound(oo.begin(), oo.end(), x) - oo.begin();
} namespace SGT {
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
int a[N << ];
void init() {
memset(a, inf, sizeof(a));
}
void update(int L, int R, int val, int l, int r, int rt) {
if(L > R) return;
if(l >= L && r <= R) {
a[rt] = min(a[rt], val);
return;
}
int mid = l + r >> ;
if(L <= mid) update(L, R, val, lson);
if(R > mid) update(L, R, val, rson);
}
int query(int p, int l, int r, int rt) {
if(l == r) return a[rt];
int mid = l + r >> ;
if(p <= mid) return min(a[rt], query(p, lson));
else return min(a[rt], query(p, rson));
}
} int main() {
scanf("%d%d", &n, &m);
for(int i = ; i <= n; i++) {
scanf("%d%lld", &frog[i].x, &frog[i].t);
frog[i].id = i;
}
sort(frog + , frog + + n);
for(int i = ; i <= m; i++) {
scanf("%d%d", &mos[i].fi, &mos[i].se);
oo.push_back(mos[i].fi);
}
sort(oo.begin(), oo.end());
oo.erase(unique(oo.begin(), oo.end()), oo.end());
SGT::init();
for(int i = ; i <= n; i++) {
SGT::update(getPosL(frog[i].x), getPosR(frog[i].x + frog[i].t), i, , SZ(oo), );
}
for(int i = ; i <= m; i++) {
int p = mos[i].fi, b = mos[i].se;
int who = SGT::query(getPosL(p), , SZ(oo), );
if(who < inf) {
LL pre = frog[who].x + frog[who].t;
frog[who].t += b;
frog[who].cnt++;
LL now = frog[who].x + frog[who].t;
SGT::update(getPosL(pre + ), getPosR(now), who, , SZ(oo), );
while(SZ(uneat)) {
auto it = uneat.lower_bound(mk(pre + , -inf));
if(it == uneat.end()) break;
who = SGT::query(getPosL(it->fi), , SZ(oo), );
if(who >= inf) break;
frog[who].t += it->se;
frog[who].cnt++;
now = frog[who].x + frog[who].t;
SGT::update(getPosL(pre + ), getPosR(now), who, , SZ(oo), );
uneat.erase(it);
}
} else {
uneat.insert(mos[i]);
}
}
for(int i = ; i <= n; i++)
anst[frog[i].id] = frog[i].t, ansc[frog[i].id] = frog[i].cnt;
for(int i = ; i <= n; i++) printf("%lld %lld\n", ansc[i], anst[i]);
return ;
} /*
*/
Codeforces 609F Frogs and mosquitoes 线段树的更多相关文章
- [Educational Round 3][Codeforces 609F. Frogs and mosquitoes]
这题拖了快一周_(:з」∠)_就把这货单独拿出来溜溜吧~ 本文归属:Educational Codeforces Round 3 题目链接:609F - Frogs and mosquitoes 题目 ...
- codeforces 609F. Frogs and mosquitoes 二分+线段树
题目链接 F. Frogs and mosquitoes time limit per test 2 seconds memory limit per test 512 megabytes input ...
- codeforces Good bye 2016 E 线段树维护dp区间合并
codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...
- codeforces 22E XOR on Segment 线段树
题目链接: http://codeforces.com/problemset/problem/242/E E. XOR on Segment time limit per test 4 seconds ...
- Codeforces 588E. A Simple Task (线段树+计数排序思想)
题目链接:http://codeforces.com/contest/558/problem/E 题意:有一串字符串,有两个操作:1操作是将l到r的字符串升序排序,0操作是降序排序. 题解:建立26棵 ...
- Codeforces Gym 100803G Flipping Parentheses 线段树+二分
Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...
- Codeforces GYM 100114 D. Selection 线段树维护DP
D. Selection Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descriptio ...
- Codeforces 444C DZY Loves Colors(线段树)
题目大意:Codeforces 444C DZY Loves Colors 题目大意:两种操作,1是改动区间上l到r上面德值为x,2是询问l到r区间总的改动值. 解题思路:线段树模板题. #inclu ...
- Codeforces 85D Sum of Medians(线段树)
题目链接:Codeforces 85D - Sum of Medians 题目大意:N个操作,add x:向集合中加入x:del x:删除集合中的x:sum:将集合排序后,将集合中全部下标i % 5 ...
随机推荐
- STM32F1-GPIO的操作
GPIO 即通用输入输出口.凡事都要熟悉,熟能生巧.一定要掌握MDK软件的工程操作方法. 对于GPIO的操作. Project里面要有以下几个文件夹(开发之前需要包含相应的库文件,这里默认已经包含) ...
- [C]gcc编译器的一些常用语法
简单的GCC语法: 如果你只有一个文件(或者只有几个文件),那么就可以不写Makefile文件(当然有Makefile更加方便),用gcc直接编译就行了.在这里我们只介绍几个我经常用的几个参数,第一是 ...
- css之坑
1.background-size要放在background后边才会生效. 2.隐藏滚动条,内容可以滑动 body::-webkit-scrollbar { display: none /* 隐藏滚动 ...
- UINavigationController 、navigationBar 、 UINavigationItem、UIBarButtonItem之间的关系
- deepin 桌面突然卡死
deepin桌面突然卡死 使用快捷键Ctrl+alt+F2 重启systemctl
- netstat常见基本用法(转)
netstat 简介 Netstat 是一款命令行工具,可用于列出系统上所有的网络套接字连接情况,包括 tcp, udp 以及 unix 套接字,另外它还能列出处于监听状态(即等待接入请求)的套接字. ...
- 《剑指offer》 二叉树的镜像
本题来自<剑指offer>二叉树的镜像 题目: 操作给定的二叉树,将其变换为源二叉树的镜像. 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 ...
- laravel 不理解的call方法
返回结果: 原来是调用同控制器的这四个方法之一...vendor\zhiyicx\plus-question\src\API2\Controllers\UserQuestionController.p ...
- HTML&javaSkcript&CSS&jQuery&ajax(十)
HTML 1.SVG直接嵌入HTML网页 ,SVG 是使用XML描述2D图像的语言,Canvas通过JavaScript来绘制2D <svg xmlns="http://www.w3. ...
- Java基本语法(一)
一.标识符 (1)标识符就是在编写程序时给类,变量,方法等起的名字 (2)标识符的命名规则:标识符由字母,数字,下划线和$组成;第一个字符不能是数字;不能与关键字重名 二.关键字 定义:也称保留字,是 ...