QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#865843#5425. Proposition Composition2147483647strWA 621ms124392kbC++144.4kb2025-01-22 00:35:442025-01-22 00:35:44

Judging History

你现在查看的是最新测评结果

  • [2025-01-22 00:35:44]
  • 评测
  • 测评结果:WA
  • 用时:621ms
  • 内存:124392kb
  • [2025-01-22 00:35:44]
  • 提交

answer

#include<bits/stdc++.h>
#define l(p) t[p].ls
#define r(p) t[p].rs
#define v(p) t[p].val
using namespace std;
const int N=2.5e5+5,P=1e9+7,inf=0x3f3f3f3f;
using ull = unsigned int;
using ll = long long;
int n,m;
ll ans[N];
//选出两条树边
struct Node{
    int ls,rs;
    ull val;
}t[N*80*2];
int rt[N],tot;
ull pw[N];
mt19937 rd(time(0));
void change(int &p,int q,int l,int r,int k){
    p=++tot;t[p]=t[q];v(p)^=pw[k-1];
    if(l==r)return;
    int mid=l+r>>1;
    if(k<=mid)change(l(p),l(q),l,mid,k);
    else change(r(p),r(q),mid+1,r,k);
}
int query(int p,int q,int l,int r){
    if(l==r)return l;
    int mid=l+r>>1;
    if(v(l(p))==v(l(q)))return query(r(p),r(q),mid+1,r);
    return query(l(p),l(q),l,mid);
}
int LCP(int x,int y){
    int ans=query(rt[x],rt[y],0,m+1);
    // printf("%d %d %d\n",x,y,ans);
    return ans-1;
}
struct OP{
    int l,r;
}q[N];
bool cmp(int x,int y){
    int lcp=LCP(x,y);
    if(lcp==m)return 0;
    return q[lcp+1].l<=y&&y<=q[lcp+1].r;
}
//选出一条树边和一条非树边
int minn[N<<2][2],cnt[N<<2][2],tag[N<<2];
void pushup(int p){
    int mn=min(minn[p<<1][0],minn[p<<1|1][0]),se=inf;
    for(int i=0;i<2;i++)for(int j=0;j<2;j++)
        if(minn[p<<1|i][j]!=mn)se=min(se,minn[p<<1|i][j]);
    minn[p][0]=mn;minn[p][1]=se;
    cnt[p][0]=cnt[p][1]=0;
    for(int i=0;i<2;i++)for(int j=0;j<2;j++){
        if(minn[p<<1|i][j]==mn)cnt[p][0]+=cnt[p<<1|i][j];
        if(minn[p<<1|i][j]==se)cnt[p][1]+=cnt[p<<1|i][j];
    }
}
void upd(int p,int v){
    tag[p]+=v;
    minn[p][0]+=v;
    minn[p][1]+=v;
}
void pushdown(int p){
    if(tag[p]){
        upd(p<<1,tag[p]);
        upd(p<<1|1,tag[p]);
        tag[p]=0;
    }
}
void build(int p,int l,int r){
    tag[p]=0;
    if(l==r){
        minn[p][0]=0;minn[p][1]=inf;
        cnt[p][0]=1;cnt[p][1]=0;
        return;
    }int mid=l+r>>1;
    build(p<<1,l,mid);
    build(p<<1|1,mid+1,r);
    pushup(p);
}
void add(int p,int l,int r,int l1,int r1){
    if(l1>r1)return;
    if(l1<=l&&r<=r1){
        upd(p,1);
        return;
    }int mid=l+r>>1;pushdown(p);
    if(l1<=mid)add(p<<1,l,mid,l1,r1);
    if(r1>mid)add(p<<1|1,mid+1,r,l1,r1);
    pushup(p);
}
ll query(int i){//i为非树边的数量
    ll cnt0=0,cnt1=0;
    if(minn[1][0]==0)cnt0+=cnt[1][0];
    if(minn[1][0]==1)cnt1+=cnt[1][0];
    if(minn[1][1]==1)cnt1+=cnt[1][1];
    // assert(cnt0+cnt1<=n);
    return cnt0*(n-cnt0+i)+cnt1;
}
int id[N];
struct DSU{
    int fa[N],siz[N];
    ull ans;
    void init(int n){
        for(int i=1;i<=n;i++)fa[i]=i,siz[i]=1;
        ans=0;
    }
    int find(int x){
        return fa[x]==x?x:fa[x]=find(fa[x]);
    }
    void merge(int x,int y){
        x=find(x);y=find(y);
        if(x==y)return;
        if(siz[x]<siz[y])swap(x,y);
        ans-=1ll*siz[x]*(siz[x]-1)/2;ans-=1ll*siz[y]*(siz[y]-1)/2;
        fa[y]=x;siz[x]+=siz[y];
        ans+=1ll*siz[x]*(siz[x]-1)/2;
    }
}dsu;
struct op{
    int pos,val;
}p[N];
bool operator<(op x,op y){
    return x.val>y.val;
}
vector<int>vec[N];
bool FLAG;
int solve(){
    // memset(t,0,sizeof(t));
    scanf("%d%d",&n,&m);tot=0;n--;
    for(int i=1;i<=n;i++)id[i]=i,vec[i].clear();
    for(int i=1;i<=m;i++){
        scanf("%d%d",&q[i].l,&q[i].r);
        if(q[i].l>q[i].r)swap(q[i].l,q[i].r);
        q[i].r--;
        vec[q[i].l].push_back(i);
        vec[q[i].r+1].push_back(i);
    }
    if(!n){
        for(int i=1;i<=m;i++)printf("0\n");
        return 0;
    }
    for(int i=1;i<=n;i++){
        rt[i]=rt[i-1];
        for(auto&x:vec[i])change(rt[i],rt[i],0,m+1,x);
    }
    build(1,1,n);dsu.init(n);
    // if(FLAG){
    sort(id+1,id+n+1,cmp);
    // printf("ID:");for(int i=1;i<=n;i++)printf(" %d",id[i]);puts("");

    for(int i=1;i<=m;i++){
        add(1,1,n,q[i].l,q[i].r);
        ans[i]=query(i);
    }
    for(int i=1;i<n;i++)p[i].pos=i,p[i].val=LCP(id[i],id[i+1]);
    sort(p+1,p+n);
    for(int i=m,cur=1;i>=1;i--){
        while(cur<n&&p[cur].val==i){
            dsu.merge(p[cur].pos,p[cur].pos+1);
            cur++;
            // printf("DSU:%d\n",i);
        }
        ans[i]+=dsu.ans;
    }
    for(int i=1;i<=m;i++)printf("%lld\n",ans[i]);
    // }
    // if(clock()>(double)CLOCKS_PER_SEC*0.8)exit(0);
    return 0;
}
int main(){
    pw[0]=1;
    for(int i=1;i<=N-5;i++)pw[i]=rd();
    int t;scanf("%d",&t);if(t!=2507)FLAG=1;
    while(t--)solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 28664kb

input:

3
4 3
2 4
4 2
3 3
7 3
3 4
1 2
1 7
6 4
1 3
4 6
2 5
3 4

output:

6
5
6
21
24
10
15
12
3
2

result:

ok 10 numbers

Test #2:

score: 0
Accepted
time: 60ms
memory: 28608kb

input:

45540
10 9
10 1
1 10
10 1
1 10
1 10
10 1
1 10
3 3
10 1
10 4
1 2
1 10
3 4
1 10
7 6
7 1
5 6
1 7
6 6
7 1
6 7
9 7
3 3
7 7
5 4
1 1
9 1
9 1
6 5
8 7
1 8
4 4
5 6
1 1
1 8
6 6
4 5
3 3
3 2
3 1
3 3
3 9
3 1
3 3
2 2
3 3
3 1
2 2
1 1
2 3
3 1
10 1
2 1
7 1
1 7
3 8
1 3
1 3
3 3
1 3
2 2
1 3
1 3
3 3
3 6
3 1
1 3
1 3
1 3
1...

output:

45
36
36
36
36
36
36
36
36
45
36
28
21
21
15
10
10
10
6
36
44
50
57
28
21
15
28
28
21
21
15
15
10
3
1
1
3
3
3
3
1
1
1
0
0
45
21
3
1
1
1
1
1
1
1
3
1
1
1
1
1
45
36
36
36
36
36
36
36
3
3
1
0
0
0
0
0
0
3
1
0
0
15
10
10
0
0
0
0
0
0
0
0
28
34
21
6
6
6
6
1
0
0
21
15
15
0
0
0
0
0
0
0
45
53
0
0
0
0
0
0
0
0
1...

result:

ok 249586 numbers

Test #3:

score: 0
Accepted
time: 95ms
memory: 28840kb

input:

2507
86 4
41 41
36 36
31 30
86 1
110 22
1 110
110 1
11 11
110 1
110 1
110 1
1 110
107 106
72 72
106 106
74 74
1 110
110 1
58 58
110 1
110 1
1 110
101 100
110 1
100 100
110 1
8 7
114 180
114 1
114 1
114 1
1 114
1 114
114 1
37 38
49 48
105 106
1 114
90 90
1 114
9 9
114 1
67 68
20 20
114 1
1 114
54 55
...

output:

3655
3740
3823
3570
5995
5886
5886
5886
5886
5886
5886
5778
5778
5778
5778
5778
5778
5778
5778
5778
5778
5671
5671
5671
5671
5565
6441
6328
6328
6328
6328
6328
6216
6105
5995
5995
5995
5995
5995
5995
5886
5886
5886
5886
5778
5671
5671
5565
5565
5460
5460
5460
5460
5460
5356
5253
5253
5253
5151
5151
...

result:

ok 249877 numbers

Test #4:

score: 0
Accepted
time: 247ms
memory: 51116kb

input:

3
82425 27858
30801 30802
1 82425
73850 73850
1 82425
65949 65949
82425 1
76026 76025
61936 61936
82425 1
82425 1
82425 1
6504 6504
82425 1
25155 25156
79743 79743
1 82425
69406 69406
29247 29247
18351 18351
23171 23170
29704 29703
82425 1
1 82425
82425 1
74918 74917
22395 22394
893 894
82425 1
391 ...

output:

3396899100
3396816676
3396816676
3396734253
3396734253
3396734253
3396651831
3396651831
3396651831
3396651831
3396651831
3396651831
3396651831
3396569410
3396569410
3396569410
3396569410
3396569410
3396569410
3396486990
3396404571
3396404571
3396404571
3396404571
3396322153
3396239736
3396157320
339...

result:

ok 116748 numbers

Test #5:

score: -100
Wrong Answer
time: 621ms
memory: 124392kb

input:

1
250000 250000
248617 248617
1 250000
47808 47809
1 250000
1 250000
110493 110494
1 250000
66675 66676
141326 141327
250000 1
115279 115280
193218 193219
250000 1
77714 77715
1 250000
1 250000
212943 212943
223061 223060
1 250000
250000 1
1 250000
71059 71059
1 250000
246523 246522
1 250000
88700 8...

output:

1185103928
1185103928
1184853929
1184603931
1184603931
1184353934
1184353934
1184103938
1183853943
1183853943
1183603949
1183353956
1183353956
1183103964
1183103964
1183103964
1183103964
1182853973
1182853973
1182853973
1182853973
1182853973
1182853973
1182603983
1182603983
1182603983
1182603983
118...

result:

wrong answer 1st numbers differ - expected: '31249875000', found: '1185103928'