codeforces 609F. Frogs and mosquitoes 二分+线段树
2 seconds
512 megabytes
standard input
standard output
There are n frogs sitting on the coordinate axis Ox. For each frog two values xi, ti are known — the position and the initial length of the tongue of the i-th frog (it is guaranteed that all positions xi are different). m mosquitoes one by one are landing to the coordinate axis. For each mosquito two values are known pj — the coordinate of the position where the j-th mosquito lands and bj — the size of the j-th mosquito. Frogs and mosquitoes are represented as points on the coordinate axis.
The frog can eat mosquito if mosquito is in the same position with the frog or to the right, and the distance between them is not greater than the length of the tongue of the frog.
If at some moment several frogs can eat a mosquito the leftmost frog will eat it (with minimal xi). After eating a mosquito the length of the tongue of a frog increases with the value of the size of eaten mosquito. It's possible that after it the frog will be able to eat some other mosquitoes (the frog should eat them in this case).
For each frog print two values — the number of eaten mosquitoes and the length of the tongue after landing all mosquitoes and after eating all possible mosquitoes by frogs.
Each mosquito is landing to the coordinate axis only after frogs eat all possible mosquitoes landed before. Mosquitoes are given in order of their landing to the coordinate axis.
First line contains two integers n, m (1 ≤ n, m ≤ 2·105) — the number of frogs and mosquitoes.
Each of the next n lines contains two integers xi, ti (0 ≤ xi, ti ≤ 109) — the position and the initial length of the tongue of the i-th frog. It is guaranteed that all xi are different.
Next m lines contain two integers each pj, bj (0 ≤ pj, bj ≤ 109) — the position and the size of the j-th mosquito.
Print n lines. The i-th line should contain two integer values ci, li — the number of mosquitoes eaten by the i-th frog and the length of the tongue of the i-th frog.
4 6
10 2
15 0
6 1
0 1
110 10
1 1
6 0
15 10
14 100
12 2
3 114
1 10
1 1
1 2
1 2
10 2
20 2
12 1
1 3 题意: 给出n只青蛙, 每个青蛙两个属性, 坐标x, 以及舌头的长度t。 在给出m个蚊子, 每个蚊子两个属性, 坐标x以及值val。 青蛙只能吃和它在同一个点的蚊子和在他右边并且它舌头够得到的蚊子。 如果一个蚊子可以被多只青蛙吃, 那么它被最左边的吃。 青蛙吃掉一个蚊子后, 舌头会变长, 变长的值为val。 蚊子的顺序按照蚊子降落的顺序给出。
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
const int maxn = 2e5+;
ll maxx[maxn<<];
void pushUp(int rt) {
maxx[rt] = max(maxx[rt<<], maxx[rt<<|]);
}
void update(int p, int val, int l, int r, int rt) {
if(l == r) {
maxx[rt] += val;
return ;
}
int m = l+r>>;
if(p<=m)
update(p, val, lson);
else
update(p, val, rson);
pushUp(rt);
}
ll query(int L, int R, int l, int r, int rt) {
if(L<=l&&R>=r) {
return maxx[rt];
}
int m = l+r>>;
ll ret = ;
if(L<=m)
ret = max(ret, query(L, R, lson));
if(R>m)
ret = max(ret, query(L, R, rson));
return ret;
}
pair<int, ll> frog[maxn];
ll tog[maxn];
int id[maxn], sum[maxn], a[maxn], n, b[maxn];
multiset <pll> mp;
int check(int num) {
int pos = upper_bound(a+, a+n+, num)-a;
if(pos == || query(, pos-, , n, )<num)
return -;
int l = , r = pos-, tmp = ;
while(l <= r) {
int mid = l+r>>;
if(query(, mid, , n, )>=num) {
r = mid-;
tmp = mid;
} else {
l = mid+;
}
}
return tmp;
}
int main() {
int m;
cin>>n>>m;
for(int i = ; i<=n; i++) {
scanf("%d%d", &frog[i].fi, &frog[i].se);
a[i] = frog[i].fi;
}
sort(a+, a+n+);
for(int i = ; i<=n; i++) {
int pos = lower_bound(a+, a+n+, frog[i].fi)-a;
b[i] = pos;
id[pos] = i;
update(pos, frog[i].fi+frog[i].se, , n, );
}
while(m--) {
int x, y;
scanf("%d%d", &x, &y);
int pos = check(x);
if(pos == -) {
mp.insert(mk(x, y));
} else {
tog[pos] += y;
sum[pos]++;
update(pos, y, , n, );
while(!mp.empty()) {
auto it = mp.lower_bound(mk(frog[id[pos]].first, ));
if(it == mp.end() || it->first>frog[id[pos]].second+tog[pos]+frog[id[pos]].first)
break;
sum[pos]++;
tog[pos] += it->second;
update(pos, it->second, , n, );
mp.erase(it);
}
}
}
for(int i = ; i<=n; i++) {
printf("%d %I64d\n", sum[b[i]], tog[b[i]]+frog[i].second);
}
}
codeforces 609F. Frogs and mosquitoes 二分+线段树的更多相关文章
- Codeforces 609F Frogs and mosquitoes 线段树
Frogs and mosquitoes 用线段树维护每个点覆盖的最小id, 用multiset维护没有吃的蚊子. #include<bits/stdc++.h> #define LL l ...
- [Educational Round 3][Codeforces 609F. Frogs and mosquitoes]
这题拖了快一周_(:з」∠)_就把这货单独拿出来溜溜吧~ 本文归属:Educational Codeforces Round 3 题目链接:609F - Frogs and mosquitoes 题目 ...
- Codeforces 889F Letters Removing(二分 + 线段树 || 树状数组)
Letters Removing 题意:给你一个长度为n的字符串,然后进行m次删除操作,每次删除区间[l,r]内的某个字符,删除后并且将字符串往前补位,求删除完之后的字符串. 题解:先开80个set ...
- Educational Codeforces Round 61 D 二分 + 线段树
https://codeforces.com/contest/1132/problem/D 二分 + 线段树(弃用结构体型线段树) 题意 有n台电脑,只有一个充电器,每台电脑一开始有a[i]电量,每秒 ...
- HDU4614 Vases and Flowers 二分+线段树
分析:感觉一看就是二分+线段树,没啥好想的,唯一注意,当开始摆花时,注意和最多能放的比大小 #include<iostream> #include<cmath> #includ ...
- J - Joseph and Tests Gym - 102020J (二分+线段树)
题目链接:https://cn.vjudge.net/contest/283920#problem/J 题目大意:首先给你n个门的高度,然后q次询问,每一次询问包括两种操作,第一种操作是将当前的门的高 ...
- 【BZOJ-3110】K大数查询 整体二分 + 线段树
3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 6265 Solved: 2060[Submit][Sta ...
- hdu6070 Dirt Ratio 二分+线段树
/** 题目:hdu6070 Dirt Ratio 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6070 题意:给定n个数,求1.0*x/y最小是多少.x ...
- [Codeforces 266E]More Queries to Array...(线段树+二项式定理)
[Codeforces 266E]More Queries to Array...(线段树+二项式定理) 题面 维护一个长度为\(n\)的序列\(a\),\(m\)个操作 区间赋值为\(x\) 查询\ ...
随机推荐
- 使用session插件并且实现登录验证
var express = require('express'); var cookieParser = require('cookie-parser'); var bodyParser = requ ...
- JDBC增强
JDBC增强 批处理:批量处理sql语句,比如批量添加用户信息. addBatch() //pstmt.addBatch() 就是替换一条一条执行的execute****** executeBat ...
- Largest Rectangle in a Histogram(最大矩形面积,动态规划思想)
Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- HDOJ 4252 A Famous City 单调栈
单调栈: 维护一个单调栈 A Famous City Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- Windows内核之进程的终止和子进程
1 进程终止的方法: <1>主线程的进入点函数返回(最好使用这种方法) <2>进程中的一个线程调用ExitProcesss函数(应该避免使用这样的方法). <3>还 ...
- stl之map 排序
排序问题,STL中默认是采用小于号来排序的,因为设置int等类型做key,它本身支持小于号运算,在一些特殊情况,比如关键字是一个结构体,涉及到排序就会出现问题,因为它没有小于号操作,insert等函数 ...
- 转载:SQL语句Where中使用别名作为判断条件
原文地址:http://www.cnblogs.com/dwfbenben/p/3307941.html 当我们使用某个表达式作为输出的一列时,我们无法再Where条件中直接使用该列作判断条件. ...
- sql server把一个表中数据复制到另一个表
insert into A(ID,Name,Sex,Address,DID,...) from (select ID,Name,Sex,Address, 5 DID)
- GCD的使用和面试题集锦
GCD 分为异步和同步 异步: dispatch_async ( 参数1 , { } 同步: dispatch_sync( 参数1 , { } 参数1 :队列 队列分为两种: dispatch_get ...
- Hive学习之动态分区及HQL
Hive动态分区 1.首先创建一个分区表create table t10(name string) partitioned by(dt string,value string)row format d ...