您的位置:首页 > 产品设计 > UI/UE

CF# Educational Codeforces Round 3 F. Frogs and mosquitoes

2015-12-22 22:10 399 查看
F. Frogs and mosquitoes

time limit per test
2 seconds

memory limit per test
512 megabytes

input
standard input

output
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.

Input
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.

Output
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.

Sample test(s)

input
4 6
10 2
15 0
6 1
0 1
110 10
1 1
6 0
15 10
14 100
12 2


output
3 114
1 10
1 1
1 2


input
1 2
10 2
20 2
12 1


output
1 3

题意:给出n只青蛙,m只蚊子,每只青蛙有一个舌头长度。蚊子一只一只的出现,如果这只蚊子在青蛙的右边,且距离不超过青蛙的舌头长度,那么这只青蛙是可以吃掉这只蚊子的。如果有多只青蛙可以吃掉同一只蚊子,那么最左端的青蛙吃掉这只蚊子。

只有当青蛙吃完或者无法再吃在现场的蚊子时,下一只蚊子才会出现。

问,每只蚊子最后的舌头长度,和吃掉的蚊子数。

分析:我们可以把每只青蛙可以吃掉的范围抽象成线段,然后蚊子就相当于延长某一条线段,然后维护这些线段即可。

/**
Create By yzx - stupidboy
*/
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <ctime>
#include <iomanip>
using namespace std;
typedef long long LL;
typedef double DB;
#define MIT (2147483647)
#define INF (1000000001)
#define MLL (1000000000000000001LL)
#define sz(x) ((int) (x).size())
#define clr(x, y) memset(x, y, sizeof(x))
#define puf push_front
#define pub push_back
#define pof pop_front
#define pob pop_back
#define mk make_pair

inline int Getint()
{
int Ret = 0;
char Ch = ' ';
bool Flag = 0;
while(!(Ch >= '0' && Ch <= '9'))
{
if(Ch == '-') Flag ^= 1;
Ch = getchar();
}
while(Ch >= '0' && Ch <= '9')
{
Ret = Ret * 10 + Ch - '0';
Ch = getchar();
}
return Flag ? -Ret : Ret;
}

const int N = 200010;
int n, m;
struct Frogs
{
int left, right, index;
Frogs(){}
Frogs(int l, int r, int idx)
{
left = l, right = r, index = idx;
}

inline bool operator <(const Frogs &t) const
{
if(left != t.left) return left < t.left;
if(right != t.right) return right < t.right;
return index < t.index;
}
} arr
;
set<Frogs> store;
int number
;
LL ans
;
struct Mosquito
{
int pos, size;
Mosquito() {}
Mosquito(int p, int s)
{
pos = p, size = s;
}

inline bool operator <(const Mosquito &t) const
{
if(pos != t.pos) return pos < t.pos;
return size < t.size;
}
} ;
multiset<Mosquito> que;

inline void Insert(int left, int right, int index)
{
Frogs t = Frogs(left, 0, 0);
if(!store.empty())
{
set<Frogs>::iterator it = store.lower_bound(t);
while(it != store.end())
{
if(it->left > right) break;
if(it->right <= right) store.erase(it);
else
{
Frogs tn = *it;
tn.left = right + 1;
store.erase(it);
store.insert(tn);
}
it = store.lower_bound(t);
}
}

t = Frogs(left, right, index);
store.insert(t);
}

inline void Input()
{
n = Getint();
m = Getint();
for(int i = 1; i <= n; i++)
{
int pos = Getint();
int len = Getint();
ans[i] = len, number[i] = 0;
arr[i].left = pos, arr[i].right = pos + len, arr[i].index = i;
}
}

inline bool Eaten(int pos, int size)
{
if(store.empty()) return 0;
set<Frogs>::iterator it = store.lower_bound(Frogs(pos, 0, 0));
if(it == store.end()) it--;
else if(it->left > pos)
{
if(it == store.begin()) return 0;
it--;
}

if(it->right < pos) return 0;
Frogs t = *it;
store.erase(it);
t.right += size;
t.right = min(t.right, INF);
number[t.index]++, ans[t.index] += size;
set<Mosquito>::iterator mos = que.lower_bound(Mosquito(t.left, 0));
while(mos != que.end())
{
if(mos->pos > t.right) break;
t.right += mos->size;
t.right = min(t.right, INF);
number[t.index]++, ans[t.index] += mos->size;
que.erase(mos);
mos = que.lower_bound(Mosquito(t.left, 0));
}
Insert(t.left, t.right, t.index);
return 1;
}

inline void Solve()
{
int rightmost = -1;
sort(arr + 1, arr + 1 + n);
for(int i = 1; i <= n; i++)
if(rightmost < arr[i].right)
{
Insert(max(arr[i].left, rightmost + 1), arr[i].right, arr[i].index);
rightmost = arr[i].right;
}

while(m--)
{
int pos = Getint();
int size = Getint();
if(!Eaten(pos, size))
que.insert(Mosquito(pos, size));
}

for(int i = 1; i <= n; i++) printf("%d %I64d\n", number[i], ans[i]);
}

int main()
{
freopen("a.in", "r", stdin);
freopen("a.out", "w", stdout);
Input();
Solve();
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: