HDU 4605

#pragma comment(linker,"/STACK:102400000,102400000")  
#include <bitset>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#include <vector>
#include <iterator>
using namespace std;
typedef vector<int> VI;
typedef pair<int, int> PII;
const int maxn = 211111;
PII p[maxn];
int c[maxn + 111], N, M, X, V;
int R[maxn + 111];
int arr[maxn];
int weight[maxn];
PII ans[maxn];
int head[maxn], e;
struct node {
    int next, id, X;
    node(int next = -1, int id = -1, int X = 0) :
            next(next), id(id), X(X) {
    }
} edge[maxn << 2];
void add_edge(int u, int id, int X) {
    edge[e] = node(head[u], id, X);
    head[u] = e++;
}
int lowbit(int x) {
    return x & (-x);
}
int cnt;
void update(int p, int c[], int t) {
    while (p <= cnt) {
        c[p] += t;
        p += lowbit(p);
    }
}
int query(int p, int c[]) {
    int s = 0;
    while (p) {
        s += c[p];
        p -= lowbit(p);
    }
    return s;
}
int getindex(int X) {
    return (lower_bound(arr, arr + cnt, X) - arr) + 1;
}
int Right = 0;
void dfs(int u, int pre = -1) {
    int lhs = p[u].first, rhs = p[u].second;
    int i, X, id, x, y, k;
    for (i = head[u]; ~i; i = edge[i].next) {

        X = edge[i].X;
        id = edge[i].id;
        k = getindex(X);
        x = query(k, c);
        y = query(k - 1, c);
        ///printf("%d %d %d Right %d\n", u, x, y, Right);
        //printf("lhs %d rhs %d X %d query %d\n", lhs, rhs, X, query(cnt));
        if (x - y > 0)
            ans[id] = PII(-1, -1);
        else {
            ans[id].first = query(k - 1, R);
            ans[id].second = y * 3 + (query(cnt, c) - x);
        }
    }

    if (lhs < 0)
        return;

    k = getindex(weight[u]);

    update(k, c, 1);

    dfs(lhs);

    update(k, R, 1);
    dfs(rhs);

    update(k, c, -1);
    update(k, R, -1);

}
int main() {
    int i, u, a, b;
    int T;
    scanf("%d", &T);
    while (T--) {

        scanf("%d", &N);
        cnt = Right = 0;
        for (i = 1; i <= N; ++i)
            p[i] = PII(-1, -1);
        for (i = 1; i <= N; ++i) {
            scanf("%d", weight + i);
            arr[cnt++] = weight[i];
        }

        scanf("%d", &M);
        for (i = 0; i < M; ++i) {
            scanf("%d%d%d", &u, &a, &b);
            p[u] = PII(a, b);
        }

        memset(head, -1, sizeof(head));
        e = 0;

        int q;
        scanf("%d", &q);
        for (i = 0; i < q; ++i) {
            scanf("%d%d", &u, &X);
            add_edge(u, i, X);
            arr[cnt++] = X;
        }

        sort(arr, arr + cnt);
        cnt = unique(arr, arr + cnt) - arr;

        dfs(1);

        for (i = 0; i < q; ++i) {
            if (ans[i].first < 0)
                puts("0");
            else
                printf("%d %d\n", ans[i].first, ans[i].second);
        }
    }
    return 0;
}
/*
 11
 5 4 6 3 1 9 5 6 8 11 2
 5
 1 2 3
 2 4 5
 3 6 7
 7 8 9
 9 10 11
 5
 5 7
 1 6
 3 8
 2 9
 11 4
 */

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值