此代码可不可以再优化下,让速度更快些(求大佬指点)

P1001 A+B Problem

xzwdqjdx @ 2023-10-03 21:20:32

#include<iostream>
using namespace std;
int main()
{
    int a,b;
    cin>>a>>b;
    int c;
    c=a+b;
    cout<<c;
    return 0;
}

by DANNNqwq @ 2023-10-03 21:56:52

@gongziwen 啊?为什么结果会差这么多啊???求解释


by User586768 @ 2023-10-04 17:17:49

namespace改用std::


by User586768 @ 2023-10-04 17:19:41

inline int read(){ ... } cout<<read()+read();


by User586768 @ 2023-10-04 17:47:20

不知道能不能快一点


by User586768 @ 2023-10-04 17:53:29

#include <iostream>
inline int read(void){
    int x=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
    return x*f;
}
int main(void){
    std::ios::sync_with_stdio(false);
    std::cout.tie(nullptr);
    std::cout<<read()+read();
   return 0;
}

by User586768 @ 2023-10-04 18:04:54

或者来个快写

#include <cstdio>
inline int read(void){
    int x=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
    return x*f;
}
inline int write(int x){
    if(!x)putchar('0');
    char F[200];int cnt=0,tmp=x>0?x:-x;
    if(x<0)putchar('-');
    while(tmp>0){
        F[cnt++]=tmp%10+'0';
        tmp/=10;
    }
    while(cnt>0)putchar(F[--cnt]);
}
int main(void){
    write(read()+read());
   return 0;
}

by yhyyyds @ 2023-10-05 17:15:26

优化代码如下:

#include<bits/stdc++.h>
using namespace std;
struct node
{
    int data,rev,sum;
    node *son[2],*pre;
    bool judge();
    bool isroot();
    void pushdown();
    void update();
    void setson(node *child,int lr);
}lct[233];
int top,a,b;
node *getnew(int x)
{
    node *now=lct+ ++top;
    now->data=x;
    now->pre=now->son[1]=now->son[0]=lct;
    now->sum=0;
    now->rev=0;
    return now;
}
bool node::judge()
{
    return pre->son[1]==this;
}
bool node::isroot()
{
    if(pre==lct)return true;
    return !(pre->son[1]==this||pre->son[0]==this);
}
void node::pushdown()
{
    if(this==lct||!rev)return;
    swap(son[0],son[1]);
    son[0]->rev^=1;
    son[1]->rev^=1;
    rev=0;
}
void node::update()
{
    sum=son[1]->sum+son[0]->sum+data;
}
void node::setson(node *child,int lr)
{
    this->pushdown();
    child->pre=this;
    son[lr]=child;
    this->update();
}
void rotate(node *now)
{
    node *father=now->pre,*grandfa=father->pre;
    if(!father->isroot()) grandfa->pushdown();
    father->pushdown();
    now->pushdown();
    int lr=now->judge();
    father->setson(now->son[lr^1],lr);
    if(father->isroot()) now->pre=grandfa;
    else grandfa->setson(now,father->judge());
    now->setson(father,lr^1);
    father->update();
    now->update();
    if(grandfa!=lct) grandfa->update();
}
void splay(node *now)
{
    if(now->isroot())return;
    for(; !now->isroot(); rotate(now))
    if(!now->pre->isroot())
    now->judge()==now->pre->judge()?rotate(now->pre):rotate(now);
}
node *access(node *now)
{
    node *last=lct;
    for(; now!=lct; last=now,now=now->pre) {
        splay(now);
        now->setson(last,1);
    }
    return last;
}
void changeroot(node *now)
{
    access(now)->rev^=1;
    splay(now);
}
void connect(node *x,node *y)
{
    changeroot(x);
    x->pre=y;
    access(x);
}
void cut(node *x,node *y)
{
    changeroot(x);
    access(y);
    splay(x);
    x->pushdown();
    x->son[1]=y->pre=lct;
    x->update();
}
int query(node *x,node *y)
{
    changeroot(x);
    node *now=access(y);
    return now->sum;
}
int main()
{
    scanf("%d%d",&a,&b);
    node *A=getnew(a);
    node *B=getnew(b);
    connect(A,B);
    cut(A,B);
    connect(A,B);
    printf("%d",query(A,B));
    return 0;
}

by dqs147852 @ 2023-10-05 17:15:55

@yhyyyds 呃呃呃《真。优化》


by orgn @ 2023-10-05 18:10:00

@DANNNsth 快读>关同步>scanf


by xzwdqjdx @ 2023-10-06 14:25:10

@RockyChen 谢谢指点。


上一页 | 下一页