Symmetric Construction#12309

Symmetric Construction (Editorial)

Problem Author:

Hasan Saleh (MathModel)

Prepared by:

Hasan Saleh (MathModel)

Edited by:

Hasan Saleh (MathModel)

Solution

notice that their "symmetric" condition is really about pairwise sums inside a subarray: for we need a[l+i] + a[r-i] to be the same for all valid . write the equations for two consecutive : a[l+i] + a[r-i] = S and a[l+i+1] + a[r-i-1] = S. subtract them and you get a[l+i+1] - a[l+i] = a[r-i] - a[r-i-1].

now let d[i] = a[i+1] - a[i] for 1 \le i \le n-1, then that relation is exactly d[l+i] = d[r-1-i].

so the whole symmetric condition on collapses to: the segment d[l..r-1] must be a palindrome. and the queries now say: for each on , we care about [l, r] = [L, R-1] on . if x = 1, the segment must be palindromic, if x = 0 it must not be. once we assign any consistent d[1..n-1], we can recover (up to a constant shift) by prefix sums, so it's enough to build .

then notice that " is a palindrome" is the same as " equals its own reverse". so let's glue with a reversed copy into one index space: positions 1..n-1 are the original , and positions n..2n-2 conceptually represent reversed. for each , we know that forward index and its reversed counterpart (the place where appears in the reversed copy) must always be equal, so we unify those two positions right away.

now for a query [L, R, x] on , after we convert to we get [l, r] = [L, R-1] and \text{len} = r - l + 1. the reversed segment corresponding to starts at some index in that doubled array, so " is palindrome" just means "segment of length starting at is equal to segment of length starting at " inside this doubled array. we've reduced every x = 1 query to a bunch of segment-equality constraints on a single 1D array of size 2 \cdot (n-1).

this can be collapsed down to DSU on segments: we want to enforce "segment [L..L+\text{len}-1] equals segment [R..R+\text{len}-1]" while being able later to check if another pair of segments is already forced equal by previous constraints. if we naïvely tried to union all positions per query we'd get , too big. with the classic "power-of-two blocks" trick it can be optimized to O((n+q)\log n).

we build where each node in represents a block [u..u+2^k-1]. if we decide that two blocks of size 2^k starting at and must be equal, we union them in and recursively enforce their left halves and right halves equal in \text{dsu}[k-1], which eventually unites the underlying single positions in . any arbitrary-length segment equality "length = " can be decomposed into at most two power-of-two blocks: take \text{lg} = \lfloor \log_2(\text{len}) \rfloor, equalize the left big block of size 2^{\text{lg}} and the right big block of size 2^{\text{lg}}. so every x = 1 query becomes two calls to "unify these two blocks", and through recursion we propagate equality all the way down to the cells.

to test an x = 0 query, we use a mirrored "check" procedure over the same power-of-two decomposition: it answers "are these two segments of length already guaranteed equal under the unions we've done so far?". if the forward segment and its reverse are already forced equal, then no matter how we assign numbers, will be palindromic, meaning is always symmetric, so an x = 0 request is impossible. if they're not forced equal, somewhere there is at least one pair of positions that still belong to different DSU components, which means later we're free to give them different values and break the palindrome.

at the end, we assign a distinct value per DSU component: for each base position in the forward array we just let d[i] = \text{find}(i) (the representative of its set), then reconstruct as a prefix sum starting from . any positions DSU-linked by x = 1 constraints share the same representative same difference value palindromic segments appear where needed; and by construction, for every x = 0 query we ensured the two segments aren't fully linked, so at least one forward/reverse pair has different representatives, which makes that diff segment non-palindromic and the corresponding subarray non-symmetric.

Complexity

We omit the time complexity of DSU (with path compression and union by size) because it is negligible.

X = 1 Queries

First, note that we will have at most n \log_2 n union operations. Whenever we perform a union operation at a state, we will not branch from that state again. Therefore, the total time complexity for these queries is O((n + q) \log_2 n).

X = 0 Queries

We only traverse the right child of a query if the left child is satisfied. This means that when we reach a right node, either a union operation was performed on the left child, or the left child is already satisfied, which takes time to check.

Since there are at most n \log_2 n union operations, and each query involves at most O(\log_2 n) checks that return false, the total time complexity for all X = 0 queries is also O((n + q) \log_2 n).

#include<bits/stdc++.h>
using namespace std;
const int mxn = 2e6 + 5;
int n, q;
int l[mxn + 1], r[mxn + 1], x[mxn + 1];
struct DSU{
    int pa[mxn + 1];
    void init(){
        for(int i = 1; i <= 2 * (n - 1); i++)pa[i] = -1;
    }
    int fp(int u){
        if(pa[u] < 0)return(u);
        return(pa[u] = fp(pa[u]));
    }
    void unon(int u, int v){
        u = fp(u); v = fp(v);
        if(u == v)return;
        if(-pa[u] < -pa[v])swap(u, v);
        pa[u] += pa[v]; pa[v] = u;
    }
    bool check(int l, int r){
        return(fp(l) == fp(r));
    }
};
DSU dsu[20];
void go(int bit, int L, int R){
    if(dsu[bit].check(L, R))return;
    dsu[bit].unon(L, R);
    if(bit > 0){
        go(bit - 1, L, R); go(bit - 1, L + (1 << (bit - 1)), R + (1 << (bit - 1)));
    }
}
void add_edge(int L, int R, int len){
    int lg = __lg(len);
    go(lg, L, R); go(lg, L + len - (1 << lg), R + len - (1 << lg));
}
bool go2(int bit, int L, int R){
    if(dsu[bit].check(L, R))return(true);
    if(bit == 0)return(false);
    if(!go2(bit - 1, L, R))return(false);
    if(go2(bit - 1, L + (1 << (bit - 1)), R + (1 << (bit - 1)))){
        dsu[bit].unon(L, R); return(true);
    }
    return(false);
}
bool check(int L, int R, int len){
    int lg = __lg(len);
    if(!go2(lg, L, R))return(false);
    return(go2(lg, L + len - (1 << lg), R + len - (1 << lg)));
}
long long d[mxn + 1];
void solve(){
    cin >> n >> q;
    for(int i = 0; i < q; i++){
        cin >> l[i] >> r[i] >> x[i]; --r[i];
    }
    for(int i = 0; i < 20; i++){
        dsu[i].init();
    }
    //1 2 3 4 .. n - 1 n - 1 n - 2.. 1
    for(int i = 1; i <= n - 1; i++){
        dsu[0].unon(i, n - 1 + (n - i));
    }
    for(int i = 0; i < q; i++){
        if(x[i] == 1){
            if(l[i] > r[i])continue;
            add_edge(l[i], n - 1 + (n - r[i]), r[i] - l[i] + 1);
        }
    }
    for(int i = 0; i < q; i++){
        if(x[i] == 0){
            if(l[i] > r[i]){
                cout << -1; return;
            }
            if(check(l[i], n - 1 + (n - r[i]), r[i] - l[i] + 1)){
                cout << -1; return;
            }
        }
    }
    
    for(int i = 1; i <= n - 1; i++){
        d[i] = dsu[0].fp(i);
    }
    cout << 0 << " ";
    for(int i = 1; i <= n - 1; i++){
        if(i > 1)d[i] += d[i - 1];
        cout << d[i] << " ";
    } 
    cout<<"\n";
    
}   
 
    
 
 
signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    //freopen("robot.in", "r", stdin);
    //freopen("robot.out", "w", stdout);
    int tt;
    cin>>tt;
    while(tt--){
        solve();
 
    }
    return(0);
}