POJ 3145 HDU 3303 Harmony Forever 线段树 + 鸽巢定理

56 篇文章 0 订阅
32 篇文章 0 订阅

大意就是对一个集合,有两种操作,一个是将某个元素加入集合中,一个是问当前集合中mod y 最小的数,如果有多个,输出最近加入的那个元素,当然题目要求的是输出他们加入集合的时间是什么。


题目的数据范围一看就是线段树类型的。

然后对每个y,由鸽巢定理,y+1个数中必然存在mod y相同的数,那么分为多个区间进行查询,即[0, y - 1] [y , 2 * y - 1]…… 然后取最优解即可

但是这样可能会退化,当y等于1的时候就发现,要查询50W个区间,每次查询都是log(n)级别,还不如直接遍历当前序列效率高,因为序列最多也就4W个数据。

所以可以推测,当y比较小的时候,直接遍历会更快。

实际上离散化会更靠谱,因为只有4W个数据,然后建树查询的时候就方便多了 ,经过验证,离散前是1360ms,离散化后就成800多ms了,瞬间进前5

/*
ID: sdj22251
PROG: subset
LANG: C++
*/
#include <iostream>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cmath>
#include <ctime>
#define MAXN 510005
#define MAXM 164444
#define INF 100000000
#define eps 1e-7
#define L(X) X<<1
#define R(X) X<<1|1
using namespace std;
struct node
{
    int left, right;
    int mi;
}tree[4 * MAXN];
int a[MAXN], pos[MAXN];
void up(int C)
{
    tree[C].mi = min(tree[L(C)].mi, tree[R(C)].mi);
}
void make_tree(int s, int e, int C)
{
    tree[C].left = s;
    tree[C].right = e;
    tree[C].mi = INF;
    if(s == e) return;
    int mid = (s + e) >> 1;
    make_tree(s, mid, L(C));
    make_tree(mid + 1, e, R(C));
}
void update(int p, int C)
{
    if(tree[C].left == tree[C].right)
    {
        tree[C].mi = p;
        return;
    }
    int mid = (tree[C].left + tree[C].right) >> 1;
    if(mid >= p) update(p, L(C));
    else update(p, R(C));
    up(C);
}
int ans;
void query(int s, int e, int C)
{
    if(tree[C].left >= s && tree[C].right <= e)
    {
        ans = min(ans, tree[C].mi);
        return;
    }
    int mid = (tree[C].left + tree[C].right) >> 1;
    if(mid >= s) query(s, e, L(C));
    if(mid < e) query(s, e, R(C));
}
int in()
{
    int flag = 1;
    char ch;
    int a = 0;
    while((ch = getchar()) == ' ' || ch == '\n');
    if(ch == '-') flag = -1;
    else
    a += ch - '0';
    while((ch = getchar()) != ' ' && ch != '\n')
    {
        a *= 10;
        a += ch - '0';
    }
    return flag * a;
}
void out(int a)
{
    if(a >= 10)out(a / 10);
    putchar(a % 10 + '0');
}
int main()
{
    int t, x, cas = 0;
    char s;
    while(scanf("%d", &t) != EOF && t)
    {
        if(cas) puts("");
        int cnt = 0;
        memset(pos, -1, sizeof(pos));
        printf("Case %d:\n", ++cas);
        make_tree(1, MAXN - 1, 1);
        getchar();
        while(t--)
        {
            s = getchar();
            x = in();
            if(s == 'A')
            {

                if(x < 5000)
                {
                    ans = INF;
                    int tx = x;
                    for(int i = cnt - 1; i >= 0; i--)
                    {
                        if(a[i] % x < tx)
                        {
                            tx = a[i] % x;
                            ans = a[i];
                        }
                        if(tx == 0) break;
                    }
                    if(ans == INF) {puts("-1");}
                    else {out(pos[ans] + 1); putchar('\n');}
                }
                else
                {
                    int bd = 500055 / x;
                    int res = INF;
                    int tx = x;
                    for(int i = 0; i <= bd; i++)
                    {
                        ans = INF;
                        int low = i * x;
                        if(low < 1) low = 1;
                        int high = (i + 1) * x - 1;
                        if(high > 500054) high = 500054;
                        query(low, high, 1);
                        if(ans == INF) continue;
                        if(ans % x < tx)
                        {
                            tx = ans % x;
                            res = ans;
                        }
                        else if(ans % x == tx)
                        {
                            if(pos[ans] > pos[res]) res = ans;
                        }
                    }
                    if(res == INF) {puts("-1");}
                    else {out(pos[res] + 1); putchar('\n');}
                }
            }
            else
            {
                if(pos[x] == -1)
                {
                    pos[x] = cnt;
                    a[cnt++] = x;
                    update(x, 1);
                }
            }
        }
    }
    return 0;
}


然后是离散化版本的,速度快了许多啊


/*
ID: sdj22251
PROG: subset
LANG: C++
*/
#include <iostream>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cmath>
#include <ctime>
#define MAXN 45555
#define MAXM 164444
#define INF 100000000
#define eps 1e-7
#define L(X) X<<1
#define R(X) X<<1|1
using namespace std;
struct node
{
    int left, right;
    int mi;
}tree[4 * MAXN];
int a[MAXN], pos[555555], x[MAXN], ax[MAXN];
void up(int C)
{
    tree[C].mi = min(tree[L(C)].mi, tree[R(C)].mi);
}
void make_tree(int s, int e, int C)
{
    tree[C].left = s;
    tree[C].right = e;
    tree[C].mi = INF;
    if(s == e) return;
    int mid = (s + e) >> 1;
    make_tree(s, mid, L(C));
    make_tree(mid + 1, e, R(C));
}
void update(int p, int C)
{
    if(tree[C].left == tree[C].right)
    {
        tree[C].mi = x[p];
        return;
    }
    int mid = (tree[C].left + tree[C].right) >> 1;
    if(mid >= p) update(p, L(C));
    else update(p, R(C));
    up(C);
}
int ans;
void query(int s, int e, int C)
{
    if(tree[C].left >= s && tree[C].right <= e)
    {
        ans = min(ans, tree[C].mi);
        return;
    }
    int mid = (tree[C].left + tree[C].right) >> 1;
    if(mid >= s) query(s, e, L(C));
    if(mid < e) query(s, e, R(C));
}
int in()
{
    int flag = 1;
    char ch;
    int a = 0;
    while((ch = getchar()) == ' ' || ch == '\n');
    if(ch == '-') flag = -1;
    else
    a += ch - '0';
    while((ch = getchar()) != ' ' && ch != '\n')
    {
        a *= 10;
        a += ch - '0';
    }
    return flag * a;
}
void out(int a)
{
    if(a >= 10)out(a / 10);
    putchar(a % 10 + '0');
}
int bin(int v, int bound)
{
    int low = 0, high = bound - 1, mid;
    while(low <= high)
    {
        mid = (low + high) >> 1;
        if(x[mid] == v) return mid;
        if(x[mid] < v) low = mid + 1;
        else high = mid - 1;
    }
    return high;
}
char s[40005];
int main()
{
    int t, cas = 0;
    while(scanf("%d", &t) != EOF && t)
    {
        if(cas) puts("");
        int cnt = 0;
        memset(pos, -1, sizeof(pos));
        printf("Case %d:\n", ++cas);
        int nt = 0;
        getchar();
        for(int i = 0; i < t; i++)
        {
            s[i] = getchar();
            ax[i] = in();
            if(s[i] == 'B') x[nt++] = ax[i];
        }
        x[nt++] = 1;
        x[nt++] = 500100;
        sort(x, x + nt);
        nt = unique(x, x + nt) - x;
        make_tree(0, nt - 1, 1);
        for(int i = 0; i < t; i++)
        {
            if(s[i] == 'A')
            {
                if(ax[i] < 500)
                {
                    ans = INF;
                    int tx = ax[i];
                    for(int j = cnt - 1; j >= 0; j--)
                    {
                        if(a[j] % ax[i] < tx)
                        {
                            tx = a[j] % ax[i];
                            ans = a[j];
                        }
                        if(tx == 0) break;
                    }
                    if(ans == INF) {puts("-1");}
                    else {out(pos[ans] + 1); putchar('\n');}
                }
                else
                {
                    int bd = 500055 / ax[i];
                    int res = INF;
                    int tx = ax[i];
                    for(int j = 0; j <= bd; j++)
                    {
                        ans = INF;
                        int low = j * ax[i];
                        if(low < 1) low = 1;
                        int high = (j + 1) * ax[i] - 1;
                        if(high > 500054) high = 500054;
                        int ll = bin(low, nt);
                        int rr = bin(high, nt);
                        if(x[ll] < low) ll++;
                        if(ll > rr) continue;
                        query(ll, rr, 1);
                        if(ans == INF) continue;
                        if(ans % ax[i] < tx)
                        {
                            tx = ans % ax[i];
                            res = ans;
                        }
                        else if(ans % ax[i] == tx)
                        {
                            if(pos[ans] > pos[res]) res = ans;
                        }
                    }
                    if(res == INF) {puts("-1");}
                    else {out(pos[res] + 1); putchar('\n');}
                }
            }
            else
            {
                if(pos[ax[i]] == -1)
                {
                    pos[ax[i]] = cnt;
                    a[cnt++] = ax[i];
                    int l = bin(ax[i], nt);
                    update(l, 1);
                }
            }
        }
    }
    return 0;
}


1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1011 1012 1013 1014 1015 1017 1018 1019 1028 1032 1035 1040 1042 1045 1046 1047 1050 1056 1061 1062 1063 1065 1067 1068 1080 1083 1088 1089 1091 1094 1095 1102 1111 1113 1117 1118 1125 1126 1127 1129 1130 1131 1132 1141 1142 1144 1149 1151 1157 1159 1160 1163 1164 1166 1174 1177 1182 1183 1186 1188 1189 1190 1191 1195 1200 1201 1207 1218 1226 1251 1256 1258 1260 1273 1274 1276 1283 1298 1305 1306 1308 1315 1316 1319 1321 1323 1324 1325 1328 1338 1339 1364 1389 1401 1422 1423 1426 1455 1458 1459 1469 1477 1485 1511 1517 1519 1523 1552 1562 1564 1565 1573 1579 1651 1654 1655 1656 1658 1659 1663 1664 1699 1700 1703 1716 1730 1737 1740 1753 1771 1797 1799 1804 1833 1837 1840 1844 1861 1887 1906 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1970 1979 1980 1985 1988 1989 2000 2001 2002 2018 2019 2021 2027 2033 2044 2051 2081 2084 2104 2109 2112 2135 2136 2137 2153 2155 2181 2182 2184 2185 2186 2187 2188 2190 2192 2195 2228 2229 2234 2236 2241 2242 2245 2247 2248 2249 2253 2264 2287 2299 2301 2309 2336 2337 2348 2352 2353 2362 2371 2378 2386 2387 2388 2389 2392 2393 2394 2402 2403 2406 2411 2413 2419 2421 2446 2449 2456 2479 2488 2492 2503 2509 2513 2524 2528 2531 2533 2545 2553 2559 2564 2575 2576 2586 2591 2593 2594 2602 2623 2632 2656 2676 2680 2707 2750 2774 2777 2780 2782 2812 2818 2840 2908 2922 2934 2965 2983 2993 2996 3020 3041 3168 3169 3176 3183 3184 3185 3191 3193 3214 3219 3224 3250 3253 3255 3256 3257 3258 3259 3264 3267 3273 3275 3277 3278 3279 3280 3295 3297 3302 3303 3311 3312 3321 3325 3348 3349 3355 3356 3357 3368 3372 3386 3400 3421 3424 3425 3427 3428 3438 3452 3468 3486 3517 3561 3585 3589 3602 3612 3614 3615 3616 3617 3618 3619 3620 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3637 3660 3661 3662 3663 3664 3665 3666 3668 3669 3670 3671 3672 3673 3687
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值