HDU 4605

来源:互联网 发布:红辣椒乐队 知乎 编辑:程序博客网 时间:2024/06/05 00:32
#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 */

原创粉丝点击