답안 #672131

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
672131 2022-12-14T19:56:15 Z Victor Superpozicija (COCI22_superpozicija) C++17
110 / 110
117 ms 42440 KB
// #pragma GCC target ("avx,avx2,fma")
// #pragma GCC optimize ("Ofast,inline") // O1 - O2 - O3 - Os - Ofast
// #pragma GCC optimize ("unroll-loops")
#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define per(i, a, b) for (int i = (b) - 1; i >= (a); --i)
#define trav(a, x) for (auto &a : x)

#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define pb push_back
#define debug(x) cout<<#x<<" = "<<x<<endl

#define umap unordered_map
#define uset unordered_set

typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;

typedef long long ll;
typedef pair<ll,ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;

const int INF = 1'000'000'007;

int n,prefix[200005];
string s;
vii pairs;
umap<int,int> active;

struct Node {
    Node *l,*r;
    int lo,hi,val,pos,madd=0;

    Node(int L,int R,int arr[]) : lo(L),hi(R) {
        if(hi-lo==1) val=arr[lo],pos=lo;
        else {
            int mid=(lo+hi)/2;
            l=new Node(lo,mid,arr);
            r=new Node(mid,hi,arr);
            tie(val,pos)=min(ii(l->val,l->pos),ii(r->val,r->pos));
        }
    }

    ii query(int L,int R) {
        if(hi<=L||R<=lo) return {INF,INF};
        if(L<=lo&&hi<=R) return {val,pos};
        push();
        return min(l->query(L,R),r->query(L,R));
    }

    int walk() {
        if(hi-lo==1) return lo+(val==0);
        push();
        if(r->val==0) return r->walk();
        return l->walk();
    }

    void add(int L,int R,int x) {
        if(hi<=L||R<=lo) return;
        if(L<=lo&&hi<=R) {
            madd+=x;
            val+=x;
            return;
        }

        push();
        l->add(L,R,x);
        r->add(L,R,x);
        tie(val,pos)=min(ii(l->val,l->pos),ii(r->val,r->pos));
    }

    void push() {
        if(madd) {
            l->add(lo,hi,madd);
            r->add(lo,hi,madd);
            madd=0;
        }
    }
};

int A[100005],B[100005];
int arr[200005];

int main() {
    cin.tie(0)->sync_with_stdio(0);
    cin.exceptions(cin.failbit);

    int tc;
    cin>>tc;
    while(tc--) {
        cin>>n>>s;
        rep(i,0,2*n) prefix[i]=0,arr[i]=INF;
        pairs.clear();
        active.clear();

        int open=0,closed=0;
        rep(i,0,n) {
            int a,b;
            cin>>a>>b;
            --a; --b;
            A[i]=a;
            B[i]=b;

            if(s[a]==s[b]) {
                if(s[a]=='(') prefix[a]=1,++open;
                else prefix[b]=-1,++closed;
            }
            else pairs.emplace_back(a,b);
        }

        if(open>n/2||closed>n/2||n%2) {
            cout<<-1<<'\n';
            continue;
        }

        trav(pair,pairs) {
            int a,b;
            tie(a,b)=pair;
            ++open;
            arr[a]=-b;
            if(s[a]=='(') prefix[a]=1;
            else prefix[b]=1;
        }

        rep(i,0,2*n) {
            if(prefix[i]) active[i]=prefix[i];
            if(i) prefix[i]+=prefix[i-1];
        }

        Node walk_tree(0,2*n,prefix);
        Node query_tree(0,2*n,arr);
        rep(_,0,open-n/2) {
            int lo=walk_tree.walk();
            /*rep(i,0,sz(pairs)) if(pairs[i].first>=lo) {
                if(best==-1) best=i;
                else if(pairs[i].second>pairs[best].second) best=i;
            }
            if(best==-1) {
                walk_tree.val=-1;
                break;
            }*/

            int a,b;
            tie(a,b)=query_tree.query(lo,2*n);
            //cout<<"a = "<<a<<" b = "<<b<<endl;
            if(a>=INF) {
                walk_tree.val=-1;
                break;
            }
            a=-a;
            query_tree.add(b,b+1,INF);

            if(active.count(a)) {
                active.erase(a);
                active[b]=-1;

            } else {
                active.erase(b);
                active[a]=-1;
            }

            walk_tree.add(a,2*n,-1);
            walk_tree.add(b,2*n,-1);
            if(walk_tree.val<0) break;
        }

        if(walk_tree.val<0) {
            cout<<-1<<'\n';
            continue;
        }
        assert(sz(active)==n);
        //trav(item,active) cout<<"pos = "<<item.first<<" type = "<<item.second<<endl;
        rep(i,0,n) cout<<active.count(B[i])<<' ';
        cout<<'\n';
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 63 ms 33740 KB Output is correct
2 Correct 13 ms 356 KB Output is correct
3 Correct 66 ms 34764 KB Output is correct
4 Correct 63 ms 34732 KB Output is correct
5 Correct 64 ms 34768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 3152 KB Output is correct
2 Correct 14 ms 628 KB Output is correct
3 Correct 12 ms 852 KB Output is correct
4 Correct 14 ms 1152 KB Output is correct
5 Correct 12 ms 1400 KB Output is correct
6 Correct 8 ms 1664 KB Output is correct
7 Correct 10 ms 1924 KB Output is correct
8 Correct 12 ms 2240 KB Output is correct
9 Correct 15 ms 2488 KB Output is correct
10 Correct 17 ms 2732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 596 KB Output is correct
2 Correct 60 ms 37800 KB Output is correct
3 Correct 60 ms 35220 KB Output is correct
4 Correct 57 ms 31892 KB Output is correct
5 Correct 66 ms 36392 KB Output is correct
6 Correct 69 ms 33264 KB Output is correct
7 Correct 30 ms 22820 KB Output is correct
8 Correct 52 ms 27216 KB Output is correct
9 Correct 53 ms 32464 KB Output is correct
10 Correct 76 ms 37192 KB Output is correct
11 Correct 89 ms 42440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 63 ms 33740 KB Output is correct
2 Correct 13 ms 356 KB Output is correct
3 Correct 66 ms 34764 KB Output is correct
4 Correct 63 ms 34732 KB Output is correct
5 Correct 64 ms 34768 KB Output is correct
6 Correct 16 ms 3152 KB Output is correct
7 Correct 14 ms 628 KB Output is correct
8 Correct 12 ms 852 KB Output is correct
9 Correct 14 ms 1152 KB Output is correct
10 Correct 12 ms 1400 KB Output is correct
11 Correct 8 ms 1664 KB Output is correct
12 Correct 10 ms 1924 KB Output is correct
13 Correct 12 ms 2240 KB Output is correct
14 Correct 15 ms 2488 KB Output is correct
15 Correct 17 ms 2732 KB Output is correct
16 Correct 1 ms 596 KB Output is correct
17 Correct 60 ms 37800 KB Output is correct
18 Correct 60 ms 35220 KB Output is correct
19 Correct 57 ms 31892 KB Output is correct
20 Correct 66 ms 36392 KB Output is correct
21 Correct 69 ms 33264 KB Output is correct
22 Correct 30 ms 22820 KB Output is correct
23 Correct 52 ms 27216 KB Output is correct
24 Correct 53 ms 32464 KB Output is correct
25 Correct 76 ms 37192 KB Output is correct
26 Correct 89 ms 42440 KB Output is correct
27 Correct 73 ms 38736 KB Output is correct
28 Correct 90 ms 36428 KB Output is correct
29 Correct 87 ms 33100 KB Output is correct
30 Correct 104 ms 37740 KB Output is correct
31 Correct 95 ms 34404 KB Output is correct
32 Correct 61 ms 23528 KB Output is correct
33 Correct 78 ms 28116 KB Output is correct
34 Correct 97 ms 32652 KB Output is correct
35 Correct 112 ms 37156 KB Output is correct
36 Correct 117 ms 42416 KB Output is correct