请问这两种写法有什么区别,为什么时间差距这么大。
查看原帖
请问这两种写法有什么区别,为什么时间差距这么大。
194093
天梦楼主2021/6/6 07:16
#include<bits/stdc++.h>
#define dd double
#define ld long double
#define ll long long
#define uint unsigned int
#define ull unsigned long long
#define N 150000
#define M 1000100
using namespace std;

const int INF=0x3f3f3f3f;

template<typename T> inline void read(T &x) {
    x=0; int f=1;
    char c=getchar();
    for(;!isdigit(c);c=getchar()) if(c == '-') f=-f;
    for(;isdigit(c);c=getchar()) x=x*10+c-'0';
    x*=f;
}

int kuailen;
inline int kuaiid(int x){return x/kuailen;}

struct rode{
    int l,r,id,t;
    inline bool operator < (const rode &b) const{
        return (kuaiid(l)^kuaiid(b.l))?l<b.l:(kuaiid(r)^kuaiid(b.r)?((kuaiid(l)&1)?r<b.r:r>b.r):t<b.t);
    }
};
rode a[N];
pair<int,int> b[N];
int n,m,c[N],tot1,tot2,cnt[M],nowans,ans[M];

inline void del(int k){cnt[c[k]]--;if(!cnt[c[k]]) nowans--;}
inline void add(int k){if(!cnt[c[k]]) nowans++;cnt[c[k]]++;}

int main(){
    read(n);read(m);
    for(int i=1;i<=n;i++) read(c[i]);
    for(int i=1;i<=m;i++){
        scanf("\n");
        if(getchar()=='R'){
            tot1++;read(b[tot1].first);read(b[tot1].second);
        }
        else{
            tot2++;read(a[tot2].l);read(a[tot2].r);a[tot2].id=tot2;a[tot2].t=tot1;
        }
    }
    // kuailen=ceil(exp(log(n*n)/3));

    kuailen=pow(n,2.0/3);
    sort(a+1,a+tot2+1);
    int l=1,r=0,t=0;
    for(int i=1;i<=tot2;i++){
        while(l>a[i].l) add(--l);
        while(r<a[i].r) add(++r);
        while(l<a[i].l) del(l++);
        while(r>a[i].r) del(r--);
        while(t<a[i].t){
            int tmp=c[b[++t].first];
            if(b[t].first>=a[i].l&&b[t].first<=a[i].r) del(b[t].first);
            c[b[t].first]=b[t].second;
            if(b[t].first>=a[i].l&&b[t].first<=a[i].r) add(b[t].first);
            b[t].second=tmp;
        }
        while(t>a[i].t){
            int tmp=c[b[t].first];
            if(b[t].first>=a[i].l&&b[t].first<=a[i].r) del(b[t].first);
            c[b[t].first]=b[t].second;
            if(b[t].first>=a[i].l&&b[t].first<=a[i].r) add(b[t].first);
            b[t].second=tmp;
            t--;
        }
        ans[a[i].id]=nowans;
    }
    for(int i=1;i<=tot2;i++) printf("%d\n",ans[i]);
    return 0;
}
```cpp
2021/6/6 07:16
加载中...