Poj 3067 Japan (line segment tree), poj3067

Source: Internet
Author: User

Poj 3067 Japan (line segment tree), poj3067

Link: poj 3067 Japan

Given N and M, it indicates the number of cities in the East and West, and then K railways, each railway connects east and west cities, and asks how many intersections there will be.

Solution: The line segment tree can be maintained. Each side is sorted by x smaller and y smaller, and then y + 1 to M can be queried each time.

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 1005;#define lson(x) ((x)<<1)#define rson(x) (((x)<<1)|1)int lc[maxn << 2], rc[maxn << 2], s[maxn << 2];inline void pushup(int u) {    s[u] = s[lson(u)] + s[rson(u)];}void build (int u, int l, int r) {    lc[u] = l;    rc[u] = r;    s[u] = 0;    if (l == r)        return ;    int mid = (l + r) >> 1;    build(lson(u), l, mid);    build(rson(u), mid + 1, r);    pushup(u);}void modify(int u, int x, int w) {    if (x == lc[u] && rc[u] == x) {        s[u] += w;        return;    }        int mid = (lc[u] + rc[u]) >> 1;    if (x <= mid)        modify(lson(u), x, w);    else        modify(rson(u), x, w);    pushup(u);}int query(int u, int l, int r) {    if (l > r)        return 0;    if (l <= lc[u] && rc[u] <= r)        return s[u];    int mid = (lc[u] + rc[u]) >> 1, ret = 0;    if (l <= mid)        ret += query(lson(u), l, r);    if (r > mid)        ret += query(rson(u), l, r);    return ret;}int N, M, Q;struct point {    int x, y;    friend bool operator < (const point& a, const point& b) {        if (a.x != b.x)            return a.x < b.x;        return a.y < b.y;    }}p[maxn * maxn];int main () {    int cas;    scanf("%d", &cas);    for (int kcas = 1; kcas <= cas; kcas++) {        scanf("%d%d%d", &N, &M, &Q);        build(1, 1, M);        long long ans = 0;        for (int i = 0; i < Q; i++)            scanf("%d%d", &p[i].x, &p[i].y);        sort(p, p + Q);        for (int i = 0; i < Q; i++) {            ans += query(1, p[i].y + 1, M);            modify(1, p[i].y, 1);        }        printf("Test case %d: %lld\n", kcas, ans);    }    return 0;}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.