[APIO2019]奇怪装置

本文分享了一次编程竞赛经历,详细解析了一道关于寻找循环节并利用线段交解决的问题。作者在有限的时间内完成了代码编写,尽管最终成绩不尽如人意,但通过分析题目和分享解决方案,展现了竞赛编程的魅力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在考场上就想出来的,因为时间只有最后40分钟,再加上我做事磨磨唧唧,码完之后没有调试就结束了,然后$APIO$就光荣打铁了(合肥八中历史最低分)(说出来都丢脸,不过我还是觉得没有省选的时候丢脸)。没考过$AYSN$也就算了(都是八中学子,同甘共苦)。但是如果加上这些分,能考过$why$的(话说$why$好像从初中以来一直吊打我,虽然我们共同拥有一个菜的抠脚的物理老师$wqq$),可能就有脸去找一下女神了吧,但是现在打铁了,真是不可原谅自己,只能等到$NOIP2019$一雪前耻了。

题面
https://loj.ac/problem/3144

题解:
非常简单。
推一波公式,找到循环节,然后维护线段交就可以了。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define ri register int
#define N 1000050
#define LL long long
using namespace std;

int n;
LL a,b;
LL l[N],r[N],L;

LL gcd(LL a, LL b) {
  if (b==0) return a; else return gcd(b,a%b);
}

void solve1() {
  LL a,b,ret=0;
  for (ri i=1;i<=n;i++) {
    scanf("%lld %lld",&a,&b);
    ret+=b-a+1;
  }
  printf("%lld",ret);
  return;
}

int ton[N<<2]; LL siz[N<<2];

struct jiao {
  struct node {
    LL x,id;
    bool operator < (const node &rhs) const {
      return x<rhs.x; 
    }
  } a[N<<1];
  LL cnt;
  LL lil[N],lir[N];
  void insert(LL l,LL r) {
    cnt++;
    a[cnt*2-1].x=l; a[cnt*2-1].id=cnt;
    a[cnt*2].x=r+1; a[cnt*2].id=cnt;
  }
  LL solve() {
    int cc=1;
    sort(a+1,a+cnt*2+1);
    a[0].x=0;
    a[cnt*2+1].x=L;
    siz[1]=1;
    for (ri i=1;i<=cnt*2+1;i++) {
      if (a[i].x!=a[i-1].x) {
        if (a[i].x-a[i-1].x>1) {
          cc++;
          siz[cc]=a[i].x-a[i-1].x-1;
        }
        cc++;
        siz[cc]=1;
      }
      if (lil[a[i].id]==0) lil[a[i].id]=cc; else lir[a[i].id]=cc;
    }
    for (ri i=1;i<=cnt;i++) {
      ton[lil[i]]++;
      ton[lir[i]]--;
    }
    for (ri i=1;i<=cc;i++) ton[i]+=ton[i-1];
    LL ret=0LL;
    for (ri i=0; i<=cc; i++) if (ton[i]>0) ret+=siz[i];
    return ret;
  }
} y;

int main() {
  scanf("%d %lld %lld",&n,&a,&b);
  LL d=gcd(a,b+1);
  long double aa=a, bb=b, dd=d;
    L=a/d*(b+1)-a/d;
    for (ri i=1;i<=n;i++) {
      scanf("%lld %lld",&l[i],&r[i]);
      LL nl=l[i]%L;
      if (r[i]-l[i]>=L-1) {
      cout<<L<<endl;
      return 0;
    }
    LL nr=nl+(r[i]-l[i]);
    if (nr>=L) {
      nr=nr-L;
      y.insert(0,nr);
      y.insert(nl,L-1);
    } 
    else {
      y.insert(nl,nr);
    }
  }
  printf("%lld",y.solve());
  return 0;
}

 

#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>#define ri register int#define N 1000050#define LL long longusing namespace std;
int n;LL a,b;LL l[N],r[N],L;
LL gcd(LL a, LL b) {  if (b==0) return a; else return gcd(b,a%b);}
void solve1() {  LL a,b,ret=0;  for (ri i=1;i<=n;i++) {    scanf("%lld %lld",&a,&b);    ret+=b-a+1;  }  printf("%lld",ret);  return;}
int ton[N<<2]; LL siz[N<<2];
struct jiao {  struct node {    LL x,id;    bool operator < (const node &rhs) const {      return x<rhs.x;     }  } a[N<<1];  LL cnt;  LL lil[N],lir[N];  void insert(LL l,LL r) {    cnt++;    a[cnt*2-1].x=l; a[cnt*2-1].id=cnt;    a[cnt*2].x=r+1; a[cnt*2].id=cnt;  }  LL solve() {    int cc=1;    sort(a+1,a+cnt*2+1);    a[0].x=0;    a[cnt*2+1].x=L;    siz[1]=1;    for (ri i=1;i<=cnt*2+1;i++) {      if (a[i].x!=a[i-1].x) {        if (a[i].x-a[i-1].x>1) {          cc++;          siz[cc]=a[i].x-a[i-1].x-1;        }        cc++;        siz[cc]=1;      }      if (lil[a[i].id]==0) lil[a[i].id]=cc; else lir[a[i].id]=cc;    }    for (ri i=1;i<=cnt;i++) {      ton[lil[i]]++;      ton[lir[i]]--;    }    for (ri i=1;i<=cc;i++) ton[i]+=ton[i-1];    LL ret=0LL;    for (ri i=0; i<=cc; i++) if (ton[i]>0) ret+=siz[i];    return ret;  }} y;
int main() {  scanf("%d %lld %lld",&n,&a,&b);  LL d=gcd(a,b+1);  long double aa=a, bb=b, dd=d;    L=a/d*(b+1)-a/d;    for (ri i=1;i<=n;i++) {      scanf("%lld %lld",&l[i],&r[i]);      LL nl=l[i]%L;      if (r[i]-l[i]>=L-1) {      cout<<L<<endl;      return 0;    }    LL nr=nl+(r[i]-l[i]);    if (nr>=L) {      nr=nr-L;      y.insert(0,nr);      y.insert(nl,L-1);    }     else {      y.insert(nl,nr);    }  }  printf("%lld",y.solve());  return 0;}

转载于:https://www.cnblogs.com/shxnb666/p/11135939.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值