zoukankan      html  css  js  c++  java
  • codeforces 609F. Frogs and mosquitoes 二分+线段树

    题目链接

    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只青蛙, 每个青蛙两个属性, 坐标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-8;
    const int mod = 1e9+7;
    const int inf = 1061109567;
    const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
    const int maxn = 2e5+5;
    ll maxx[maxn<<2];
    void pushUp(int rt) {
        maxx[rt] = max(maxx[rt<<1], maxx[rt<<1|1]);
    }
    void update(int p, int val, int l, int r, int rt) {
        if(l == r) {
            maxx[rt] += val;
            return ;
        }
        int m = l+r>>1;
        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>>1;
        ll ret = 0;
        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+1, a+n+1, num)-a;
        if(pos == 1 || query(1, pos-1, 1, n, 1)<num)
            return -1;
        int l = 1, r = pos-1, tmp = 0;
        while(l <= r) {
            int mid = l+r>>1;
            if(query(1, mid, 1, n, 1)>=num) {
                r = mid-1;
                tmp = mid;
            } else {
                l = mid+1;
            }
        }
        return tmp;
    }
    int main() {
        int m;
        cin>>n>>m;
        for(int i = 1; i<=n; i++) {
            scanf("%d%d", &frog[i].fi, &frog[i].se);
            a[i] = frog[i].fi;
        }
        sort(a+1, a+n+1);
        for(int i = 1; i<=n; i++) {
            int pos = lower_bound(a+1, a+n+1, frog[i].fi)-a;
            b[i] = pos;
            id[pos] = i;
            update(pos, frog[i].fi+frog[i].se, 1, n, 1);
        }
        while(m--) {
            int x, y;
            scanf("%d%d", &x, &y);
            int pos = check(x);
            if(pos == -1) {
                mp.insert(mk(x, y));
            } else {
                tog[pos] += y;
                sum[pos]++;
                update(pos, y, 1, n, 1);
                while(!mp.empty()) {
                    auto it = mp.lower_bound(mk(frog[id[pos]].first, 0));
                    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, 1, n, 1);
                    mp.erase(it);
                }
            }
        }
        for(int i = 1; i<=n; i++) {
            printf("%d %I64d
    ", sum[b[i]], tog[b[i]]+frog[i].second);
        }
    }
  • 相关阅读:
    java如何操作注册表(Preferences类)(在windows的注册表中保存、读取)
    转-正则表达式
    js数字格式化千分位格式
    es6严格模式需要注意的地方
    html5手势原理知识
    js事件总结
    js键盘相关知识总结
    html5 拖放
    js学习日记-隐式转换相关的坑及知识
    移动端各种分辨率匹配
  • 原文地址:https://www.cnblogs.com/yohaha/p/5211809.html
Copyright © 2011-2022 走看看