【bzoj4320】ShangHai2006 Homework

若Y小于等于sqrt(300000),暴力,对所有的插入的数都更新mn[i]。 
若Y大于sqrt(300000),枚举kY,用并查集维护>=i的第一个数,这样只支持删除操作是O(1),然后倒着枚举一边,删除一个数x那么就fa[x]=fa[x+1]

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;

#define MAXN 300010
#define N 300000

struct Node
{
    int t,k,ans;
}q[MAXN];

int n;

int fa[MAXN],f[MAXN],m[310];

char ch[10];

int find(int x)
{
    return fa[x]==x ? fa[x] : fa[x]=find(fa[x]);
}

int main()
{
    memset(m,127/3,sizeof(m));
    scanf("%d",&n);
    for (int i=1;i<=N+1;i++)
        fa[i]=i;
    for (int i=1;i<=n;i++)
    {
        scanf("%s%d",ch,&q[i].k);
        q[i].t=ch[0]-‘A‘;
        if (q[i].t==0)
        {
            for (int j=1;j<=300;j++)
                m[j]=min(m[j],q[i].k%j);
            f[q[i].k]=1;
        }
        if (q[i].t==1 && q[i].k<=300)
            q[i].ans=m[q[i].k];
    }
    for (int i=1;i<=N;i++)
        if (!f[i])
            fa[i]=i+1;
    for (int i=n;i>=1;i--)
    {
        if (q[i].t==0)
            fa[q[i].k]=q[i].k+1;
        else
        {
            int t,minn=N;
            if (q[i].k>300)
            {
                for (int j=0;j<=N;j+=q[i].k)
                {
                    t=find(max(j,1));
                    if (t<=N)
                        minn=min(minn,t%q[i].k);
                }
                q[i].ans=minn;
            }
        }
    }
    for (int i=1;i<=n;i++)
        if (q[i].t)
        printf("%d\n",q[i].ans);
    return 0;
}

  

时间: 2024-07-30 23:51:48

【bzoj4320】ShangHai2006 Homework的相关文章

【BZOJ4320】ShangHai2006 Homework 分段+并查集

[BZOJ4320]ShangHai2006 Homework Description 1:在人物集合 S 中加入一个新的程序员,其代号为 X,保证 X 在当前集合中不存在. 2:在当前的人物集合中询问程序员的mod Y 最小的值. (为什么统计这个?因为拯救过世界的人太多了,只能取模) Input 第一行为用空格隔开的一个个正整数 N. 接下来有 N 行,若该行第一个字符为“A” ,则表示操作 1:若为“B”,表示操作 2: 其中 对于 100%的数据:N≤100000, 1≤X,Y≤3000

【题解】HDU Homework(倍增)

[题解]HDU Homework(倍增) 矩阵题一定要多多检查一下是否行列反了... 一百个递推项一定要存101个 说多了都是泪啊 一下午就做了这一道题因为实在是太菜了太久没写这种矩阵的题目... 设一个行向量\(e\),和一个增逛矩阵\(A\),他们咋定义的见我那篇讲线性递推博客 现在我们再预处理\(st\)矩阵数组,其中\(st_i=A^{2^i}\). 考虑这样一种做法,我们考虑让\(e\)总共有101个值,然后当第一个值被增逛为\(f_{q.n-100}\)时,暴力将\(e\)中的第\(

hdoj 1789 Doing Homework again 【贪心】

贪心策略:先按分数从大到小排序,分数一样,再按时间从小到大排序 分最高的越靠近期限完成,越好 话不多说直接看代码 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1789 代码: #include<cstdio> #include<cstring> #include<algorithm> using std::sort; typedef struct{ int sco, time; }str; str s[1005]; bo

【软件测试】Homework 1 Briefly describe an error

[要求] Briefly describe an error from your past projects that you have recently completed or an error from other projects which impress you most. State the reason, impact of the error and how did you find it. [解答] 在上学期的考勤系统制作过程中,我们后台采用的是Spring Boot和Hib

【转】对于杭电OJ题目的分类

[好像博客园不能直接转载,所以我复制过来了..] 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDI

HDU1009_FatMouse&amp;#39; Trade【贪心】【水题】

FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 44470    Accepted Submission(s): 14872 Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats g

【POJ2094】【差分序列】Angry Teacher

Description Mr. O'Cruel is teaching Math to ninth grade students. Students of course are very lazy, so they do not like to do their homework. On the other side, Mr. O'Cruel doesn't like lazy students. Recently Andrew failed to do his homework again,

HDU1009_FatMouse&#39; Trade【贪心】【水题】

FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 44470    Accepted Submission(s): 14872 Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats g

HDU1009 FatMouse&#39; Trade 【贪心】

FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 42786    Accepted Submission(s): 14274 Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats g