UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#197421#3447. 远恋Zeardoe100720ms32824kbC++113.5kb2023-11-12 09:09:282023-11-12 13:12:46

answer

/*
[templates]: 
duipai
spjdp
compre
addhis
floor_sum
treedfs
matrix
network_flow
polynomial
lca
bitset
valuesgt
fenwick
erbitree
*/
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx")
#include<bits/stdc++.h>
using namespace std;
#define int long long
//use ll instead of int.
#define f(i, a, b) for(int i = (a); i <= (b); i++)
#define cl(i, n) i.clear(),i.resize(n);
#define endl '\n'
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int inf = 1e18;
//#define cerr if(false)cerr
//#define freopen if(false)freopen
mt19937 rng(time(0)); 
int rnd(int l, int r) {return rng() % (r-l+1) + l; }
#define watch(x) cerr  << (#x) << ' '<<'i'<<'s'<<' ' << x << endl
void pofe(int number, int bitnum) {
    string s; f(i, 0, bitnum) {s += char(number & 1) + '0'; number >>= 1; } 
    reverse(s.begin(), s.end()); cerr << s << endl; 
    return;
}
template <typename TYP> void cmax(TYP &x, TYP y) {if(x < y) x = y;}
template <typename TYP> void cmin(TYP &x, TYP y) {if(x > y) x = y;}
//调不出来给我对拍!
//use std::array.
const int N = 100000; 
int fa[N + 10], sz[N + 10]; int cnt; 
int ans[N + 10]; 
vector<pii> vec[2 * N + 10]; 
stack<int> stk; 
int getfa(int x) {
    if(fa[x] == x) return fa[x]; 
    return getfa(fa[x]); 
}
void merge(int x, int y) {
    int fx = getfa(x), fy = getfa(y);
    if(sz[fx] < sz[fy]) swap(fx, fy); 
    stk.push(fy); 
    cnt -= (sz[fy] >= 2); 
    cnt -= (sz[fx] >= 2); 
    fa[fy] = fx; sz[fx] += sz[fy]; 
    cnt += (sz[fx] >= 2); 
}
void del() {
    int fy = stk.top(); stk.pop(); 
    cnt -= (sz[fa[fy]] >= 2); 
    sz[fa[fy]] -= sz[fy]; 
    cnt += (sz[fa[fy]] >= 2); 
    fa[fy] = fy; 
    cnt += (sz[fy] >= 2); 
}
int id(int l, int r) {return (l + r) | (l != r); }
void ins(int now, int l, int r, int x, int y, pii ed) {
    if(l >= x && r <= y) {
        // cerr << l << " " << r << " " << ed.first << " " << ed.second << endl; 
        vec[now].push_back(ed); 
        return; 
    }
    if(l > y || r < x) return; 
    int mid = (l + r) >> 1; 
    ins(id(l, mid), l, mid, x, y, ed); 
    ins(id(mid + 1, r), mid + 1, r, x, y, ed); 
    return; 
} 
void dfs(int now, int l, int r) {
    for(pii it : vec[now]) {
        merge(it.first, it.second); 
    }
    if(l == r) {
        ans[l] = cnt; 
    }
    else {
        int mid = (l + r) >> 1; 
        dfs(id(l, mid), l, mid); 
        dfs(id(mid + 1, r), mid + 1, r);
    }
    for(pii it __attribute__((unused)) : vec[now]) {
        del(); 
    }
}
signed main() {
    ios::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
    //freopen();
    //freopen();
    //time_t start = clock();
    //think twice,code once.
    //think once,debug forever.
    map<pii, int> s; 
    int n, q; cin >> n >> q; 
    f(i, 1, n) fa[i] = i, sz[i] = 1; 
    f(i, 1, q) {
        int u, x, y; cin >> u >> x >> y; if(x > y) swap(x, y); 
        if(u == 1) {
            if(s.count({x, y})) continue; 
            else s[{x, y}] = i; 
        }
        else {
            if(!s.count({x, y})) continue; 
            else {
                ins(id(1, q), 1, q, s[{x, y}], i - 1, {x, y});
                s.erase({x, y}); 
            }
        }
    }
    for(auto it : s) ins(id(1, q), 1, q, it.second, q, it.first);
    dfs(id(1, q), 1, q); 
    f(i, 1, q) cout << ans[i] << endl; 
    //time_t finish = clock();
    //cout << "time used:" << (finish-start) * 1.0 / CLOCKS_PER_SEC <<"s"<< endl;
    return 0;
}

Details

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 33ms
memory: 8560kb

input:

2 100000
1 2 1
2 1 2
1 1 2
1 1 2
1 2 1
2 2 1
2 2 1
2 1 2
2 2 1
2 1 2
1 1 2
2 1 2
2 1 2
2 2 1
1 2 1
1...

output:

1
0
1
1
1
0
0
0
0
0
1
0
0
0
1
1
0
1
0
0
1
1
1
0
1
1
0
0
1
0
0
0
0
1
0
1
1
1
1
0
0
0
0
1
0
0
0
1
0
1
...

result:

ok 100000 numbers

Test #2:

score: 10
Accepted
time: 36ms
memory: 8564kb

input:

2 100000
2 1 2
2 1 2
2 2 1
2 2 1
1 1 2
1 1 2
1 1 2
1 2 1
2 1 2
1 1 2
2 2 1
2 1 2
2 2 1
2 1 2
2 2 1
2...

output:

0
0
0
0
1
1
1
1
0
1
0
0
0
0
0
0
0
0
1
1
1
0
0
0
1
0
1
0
0
1
1
1
1
0
0
1
0
1
1
1
1
0
1
1
1
1
1
0
1
0
...

result:

ok 100000 numbers

Test #3:

score: 10
Accepted
time: 0ms
memory: 7644kb

input:

2000 2000
1 964 329
2 1284 1313
1 787 509
2 945 658
1 1053 1194
1 1964 1595
2 1504 1111
2 1425 469
2...

output:

1
1
2
2
3
4
4
4
4
5
5
6
6
7
8
9
10
11
12
12
13
14
15
16
17
17
18
19
20
20
20
21
21
22
22
23
23
23
24...

result:

ok 2000 numbers

Test #4:

score: 10
Accepted
time: 0ms
memory: 7640kb

input:

2000 2000
2 315 602
1 649 1798
2 26 785
1 586 1610
2 1854 883
1 1861 622
2 756 1668
2 687 98
2 1619 ...

output:

0
1
1
2
2
3
3
3
3
4
5
6
6
7
8
9
10
11
11
12
12
13
14
15
16
16
16
16
16
16
16
17
18
18
19
20
21
22
23...

result:

ok 2000 numbers

Test #5:

score: 10
Accepted
time: 4ms
memory: 8524kb

input:

100000 2000
1 92779 34174
2 24442 50350
2 95908 70204
1 24040 90714
1 72654 11859
2 92767 96242
1 72...

output:

1
1
1
2
3
3
4
4
5
5
6
6
7
8
8
9
9
10
11
11
12
12
13
13
14
15
15
15
16
17
17
18
19
20
20
20
20
20
21
...

result:

ok 2000 numbers

Test #6:

score: 10
Accepted
time: 4ms
memory: 8500kb

input:

100000 2000
2 51154 58009
2 60818 30639
1 19247 71910
2 14664 29829
1 4253 44694
1 32318 94691
2 939...

output:

0
0
1
1
2
3
3
4
4
5
6
7
7
7
7
8
8
9
10
11
11
11
12
13
13
14
15
15
16
16
17
18
18
18
19
19
20
20
20
2...

result:

ok 2000 numbers

Test #7:

score: 10
Accepted
time: 207ms
memory: 32824kb

input:

100000 99999
1 83203 7892
1 89609 30806
1 17204 58475
1 94078 40656
1 12175 54519
1 96583 57951
1 64...

output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
3...

result:

ok 99999 numbers

Test #8:

score: 10
Accepted
time: 203ms
memory: 32780kb

input:

100000 99999
1 60027 76791
1 94226 32512
1 43832 3682
1 52120 92287
1 39908 42152
1 29887 44511
1 74...

output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
3...

result:

ok 99999 numbers

Test #9:

score: 10
Accepted
time: 121ms
memory: 20800kb

input:

100000 100000
2 74884 54626
2 10396 73081
1 18384 59139
1 48884 27924
1 2340 74923
2 38603 14867
1 9...

output:

0
0
1
2
3
3
4
4
4
4
4
4
4
4
4
5
5
6
7
7
8
8
9
9
9
9
9
10
11
11
11
11
11
11
12
13
13
14
15
15
16
16
1...

result:

ok 100000 numbers

Test #10:

score: 10
Accepted
time: 112ms
memory: 20788kb

input:

100000 100000
1 68584 71139
1 62909 25950
1 81680 54537
1 25264 95674
1 16361 23965
1 81367 39665
1 ...

output:

1
2
3
4
5
6
7
8
9
9
10
10
10
11
11
11
11
12
13
13
14
14
15
15
15
16
17
18
18
18
19
20
20
20
20
20
20...

result:

ok 100000 numbers

Extra Test:

score: 0
Extra Test Passed