poj2528 poj1436

来源:互联网 发布:mac如何使用财务软件 编辑:程序博客网 时间:2024/05/20 18:40

离散一下线段数 区间更新,用hash超时

#include <iostream>#include <cstdio>#include <algorithm>#include <cmath>#include <string.h>#include <string>#include <map>using namespace std;const int MAXN = 20000 + 10;struct Node{    int l, r, col;}ps[MAXN], node[MAXN << 2];bool vis[MAXN * 2];unsigned short hashs[10000000 + 10];int temp[MAXN * 2], n, ans;void build(int i, int l, int r){    node[i].l = l, node[i].r = r;    node[i].col = 0;    if (l == r)    {        return ;    }    int mid = (l + r) >> 1;    build(i * 2, l, mid);    build(i * 2 + 1, mid + 1, r);}void update(int i, int x, int y, int col){    if (y < node[i].l || x > node[i].r)    {        return ;    }    if (node[i].l >= x && node[i].r <= y)    {        node[i].col = col;        return ;    }    if (node[i].col >= 0)    {        node[i * 2].col = node[i * 2 + 1].col = node[i].col;        node[i].col = -1;    }    update(i * 2, x, y, col);    update(i * 2 + 1, x, y, col);}void query(int i){    if (node[i].col == 0)    {        return ;    }    if (node[i].col > 0)    {        if (!vis[node[i].col])        {            vis[node[i].col] = true;            ans++;        }        return ;    }    query(i * 2);    query(i * 2 + 1);}void input(){    int t;    scanf("%d", &t);    while (t--)    {        memset(vis, false, sizeof(vis));        memset(hashs, 0, sizeof(hashs));        scanf("%d", &n);        for (int i = 0; i < n; i++)        {            scanf("%d %d", &ps[i].l, &ps[i].r);            temp[i * 2] = ps[i].l;            temp[i * 2 + 1] = ps[i].r;        }        sort(temp, temp + 2 * n);        int num = 0;        ans = 0;        for (int i = 0; i < 2 * n; i++)        {             if (!hashs[temp[i]]) hashs[temp[i]] = ++num;        }        build(1, 1, num);        for (int i = 0; i < n; i++)        {            update(1, hashs[ps[i].l], hashs[ps[i].r], i + 1);        }        query(1);        printf("%d\n", ans);    }}int main(){    input();    return 0;}


#include <iostream>#include <cstdio>#include <algorithm>#include <cmath>#include <string.h>#include <string>using namespace std;const int MAXN = 8000 + 50;int b, n;struct SNode{    int y1, y2, x;}line[MAXN];bool vis[MAXN][MAXN];struct Node{    int l, r, col;}node[4 * MAXN];bool cmp(SNode a, SNode b){    return a.x < b.x;}void build(int i, int l, int r){    node[i].l = l, node[i].r = r, node[i].col = 0;    if (l == r)    {        return ;    }    int mid = (l + r) >> 1;    build(i * 2, l, mid);    build(i * 2 + 1, mid + 1, r);}void update(int i, int x, int y, int val){    if (y < node[i].l || x > node[i].r)    {        return ;    }    if (x <= node[i].l && y >= node[i].r)    {        node[i].col = val;        return ;    }    if (node[i].col)    {        node[i * 2].col = node[i * 2 + 1].col = node[i].col;        node[i].col = 0;    }    update(i * 2, x, y, val);    update(i * 2 + 1, x, y, val);}void query(int i, int x, int y, int id){    if (y < node[i].l || x > node[i].r)    {        return ;    }    if (node[i].col)    {        vis[id][node[i].col] = vis[node[i].col][id] = true;        return ;    }    if (node[i].l == node[i].r)    {        return ;    }    query(i * 2, x, y, id);    query(i * 2 + 1, x, y, id);}void input(){    int t;    scanf("%d", &t);    while (t--)    {        memset(vis, false, sizeof(vis));        scanf("%d", &n);        b = 0;        for (int i = 0; i < n; i++)        {            scanf("%d %d %d", &line[i].y1, &line[i].y2, &line[i].x);            b = max(b, line[i].y2);        }        sort(line, line + n, cmp);        build(1, 0, 2 * b);        for (int i = 0; i < n; i++)        {            query(1, line[i].y1 * 2, line[i].y2 * 2, i + 1);            update(1, line[i].y1 * 2, line[i].y2 * 2, i + 1);        }        int ans = 0;        for (int i = 1; i <= n; i++)        {            for (int j = i + 1; j <= n; j++)            {                if (vis[i][j])                {                    for (int k = j + 1; k <= n; k++)                    {                        if (vis[i][k] && vis[j][k])                        {                            ans++;                        }                    }                }            }        }        printf("%d\n", ans);    }}int main(){    input();    return 0;}

0 0