QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#396927#5425. Proposition Composition2745518585TL 376ms153672kbC++205.1kb2024-04-23 14:25:212024-04-23 14:25:22

Judging History

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

  • [2024-04-23 14:25:22]
  • 评测
  • 测评结果:TL
  • 用时:376ms
  • 内存:153672kb
  • [2024-04-23 14:25:21]
  • 提交

answer

#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
const int N=1000001;
int n,m;
namespace fhq
{
    int tot;
    struct tree
    {
        int k,l,r,s,h,f;
    }T[N];
    void pushup(int x)
    {
        T[x].s=T[T[x].l].s+T[T[x].r].s+1;
        if(T[x].l) T[T[x].l].f=x;
        if(T[x].r) T[T[x].r].f=x;
        T[x].f=0;
    }
    void split(int x,int k,int &x1,int &x2)
    {
        if(x==0)
        {
            x1=x2=0;
            return;
        }
        if(T[x].k<=k)
        {
            x1=x;
            split(T[x].r,k,T[x].r,x2);
        }
        else
        {
            x2=x;
            split(T[x].l,k,x1,T[x].l);
        }
        pushup(x);
    }
    int merge(int x1,int x2)
    {
        if(x1==0||x2==0) return x1|x2;
        if(T[x1].h<T[x2].h)
        {
            T[x1].r=merge(T[x1].r,x2);
            pushup(x1);
            return x1;
        }
        else
        {
            T[x2].l=merge(x1,T[x2].l);
            pushup(x2);
            return x2;
        }
    }
    void add(int &rt,int k)
    {
        int x1,x2;
        split(rt,k,x1,x2);
        T[++tot].k=k;
        T[tot].l=T[tot].r=T[tot].f=0;
        T[tot].s=1;
        T[tot].h=rand();
        rt=merge(merge(x1,tot),x2);
    }
    int sum(int x,int k)
    {
        if(x==0) return 0;
        if(k<=T[T[x].l].s) return sum(T[x].l,k);
        else if(k<=T[T[x].l].s+1) return x;
        else return sum(T[x].r,k-T[T[x].l].s-1);
    }
}
namespace sgt1
{
    struct tree
    {
        int l,r;
        vector<int> s;
    }T[N<<2];
    void build(int x,int l,int r)
    {
        T[x].l=l,T[x].r=r;
        T[x].s.clear();
        if(l==r) return;
        int z=l+r>>1;
        build(x<<1,l,z);
        build(x<<1|1,z+1,r);
    }
    void add(int x,int l,int r,int k)
    {
        if(l>r) return;
        if(T[x].l>=l&&T[x].r<=r)
        {
            T[x].s.push_back(k);
            return;
        }
        int z=T[x].l+T[x].r>>1;
        if(l<=z) add(x<<1,l,r,k);
        if(r>z) add(x<<1|1,l,r,k);
    }
    void get(int x,int q,vector<int> &p)
    {
        for(auto i:T[x].s) p.push_back(i);
        T[x].s.clear();
        if(T[x].l==T[x].r) return;
        int z=T[x].l+T[x].r>>1;
        if(q<=z) get(x<<1,q,p);
        else get(x<<1|1,q,p);
    }
}
namespace sgt2
{
    struct tree
    {
        int l,r,s0,s1,k;
    }T[N<<2];
    void pushup(int x)
    {
        T[x].s0=T[x<<1].s0+T[x<<1|1].s0;
        T[x].s1=T[x<<1].s1+T[x<<1|1].s1;
    }
    void maketag(int x,int k)
    {
        if(k==1) T[x].s1=T[x].s0,T[x].s0=0;
        else if(k>1) T[x].s0=T[x].s1=0;
        T[x].k+=k;
    }
    void pushdown(int x)
    {
        if(T[x].k==0) return;
        maketag(x<<1,T[x].k);
        maketag(x<<1|1,T[x].k);
        T[x].k=0;
    }
    void build(int x,int l,int r)
    {
        T[x].l=l,T[x].r=r;
        T[x].s0=T[x].s1=T[x].k=0;
        if(l==r)
        {
            T[x].s0=1;
            return;
        }
        int z=l+r>>1;
        build(x<<1,l,z);
        build(x<<1|1,z+1,r);
        pushup(x);
    }
    void add(int x,int l,int r,int k)
    {
        if(l>r) return;
        if(T[x].l>=l&&T[x].r<=r)
        {
            maketag(x,k);
            return;
        }
        pushdown(x);
        int z=T[x].l+T[x].r>>1;
        if(l<=z) add(x<<1,l,r,k);
        if(r>z) add(x<<1|1,l,r,k);
        pushup(x);
    }
}
ll sum(ll x)
{
    return x*(x-1)/2;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        if(n==1)
        {
            for(int i=1;i<=m;++i)
            {
                scanf("%*d%*d");
                printf("0\n");
            }
            continue;
        }
        fhq::tot=0;
        int rt=0;
        for(int i=1;i<=n-1;++i) fhq::add(rt,i);
        sgt1::build(1,1,n);
        sgt2::build(1,1,n-1);
        for(int i=1;i<=n-1;++i) sgt1::add(1,i,i,i);
        ll s=sum(n-1);
        for(int i=1;i<=m;++i)
        {
            int l,r;
            scanf("%d%d",&l,&r);
            if(l>r) swap(l,r);
            if(l<r)
            {
                vector<int> p;
                sgt1::get(1,l,p);
                sgt1::get(1,r,p);
                for(auto rt:p)
                {
                    while(fhq::T[rt].f) rt=fhq::T[rt].f;
                    s-=sum(fhq::T[rt].s);
                    int x1,x2,x3;
                    fhq::split(rt,l-1,x1,x2);
                    fhq::split(x2,r-1,x2,x3);
                    int p1=fhq::sum(x1,fhq::T[x1].s),p2=fhq::sum(x3,1);
                    x1=fhq::merge(x1,x3);
                    s+=sum(fhq::T[x1].s)+sum(fhq::T[x2].s);
                    if(p1!=0&&p2!=0) sgt1::add(1,p1+1,p2,p1);
                }
                sgt2::add(1,l,r-1,1);
            }
            int w0=sgt2::T[1].s0,w1=sgt2::T[1].s1;
            // printf("%lld %d %d\n",s,w0,w1);
            printf("%lld\n",(s-sum(w0))+((ll)w0*(n+i-2)-sum(w0))+(w1));
        }
    }
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 18ms
memory: 128816kb

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: 79ms
memory: 128760kb

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: 167ms
memory: 133952kb

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: 197ms
memory: 139724kb

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: 0
Accepted
time: 376ms
memory: 153672kb

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:

31249875000
31249875000
31249625001
31249375003
31249375003
31249125006
31249125006
31248875010
31248625015
31248625015
31248375021
31248125028
31248125028
31247875036
31247875036
31247875036
31247875036
31247625045
31247625045
31247625045
31247625045
31247625045
31247625045
31247375055
31247375055
...

result:

ok 250000 numbers

Test #6:

score: 0
Accepted
time: 97ms
memory: 128920kb

input:

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

output:

36
29
28
16
16
16
8
45
29
45
53
61
36
18
16
8
15
5
4
4
4
2
2
45
36
17
16
15
15
15
0
0
0
0
28
3
4
1
1
1
1
1
10
3
1
1
1
1
1
0
0
0
3
1
3
3
3
1
0
0
6
36
30
12
8
8
3
4
5
5
1
0
0
0
0
0
6
5
6
1
0
0
0
0
0
0
28
32
19
20
11
7
7
21
17
17
12
4
3
2
1
1
21
11
7
6
3
3
1
2
1
1
28
25
20
21
22
22
23
11
1
1
28
1
1
0
0...

result:

ok 249141 numbers

Test #7:

score: -100
Time Limit Exceeded

input:

2517
14 35
2 13
14 1
14 14
1 14
7 7
1 14
7 7
2 14
9 11
2 4
2 13
11 12
14 2
13 1
14 1
13 2
12 11
2 14
1 13
12 11
1 14
4 5
2 14
14 14
13 13
10 9
14 2
1 14
14 2
13 2
14 2
7 8
6 6
1 1
13 1
51 125
30 30
40 39
25 24
51 1
1 51
40 40
8 7
50 2
2 50
7 9
50 1
30 30
47 49
14 16
2 4
1 51
1 50
6 6
1 51
4 4
50 2
2...

output:

91
58
58
56
56
56
56
55
37
23
23
17
17
17
17
17
17
17
17
17
17
12
12
12
12
11
11
11
11
11
11
7
7
7
7
1275
1324
1370
1176
1128
1128
1081
991
991
947
946
946
862
782
706
706
706
706
706
706
706
706
706
668
598
598
598
598
532
532
532
532
532
532
532
500
469
469
469
469
411
383
383
383
331
331
331
331
...

result: