Submission #229816

# Submission time Handle Problem Language Result Execution time Memory
229816 2020-05-06T18:32:54 Z 534351 Intergalactic ship (IZhO19_xorsum) C++17
17 / 100
2000 ms 512 KB
#include <bits/stdc++.h>

using namespace std;

template<class T, class U>
void ckmin(T &a, U b)
{
    if (a > b) a = b;
}

template<class T, class U>
void ckmax(T &a, U b)
{
    if (a < b) a = b;
}

#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define SZ(x) ((int) (x).size())
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;

const int MAXN = 1013;
const int MAXV = 128;
const int MAXQ = 100013;
const int INF = 1000000007;
const int HALF = (INF + 1) >> 1;

int add(int a, int b)
{
    a += b; if (a >= INF) a -= INF; return a;
}
int mul(int a, int b)
{
    return (ll) a * b % INF;
}
int sub(int a, int b)
{
    a -= b; if (a < 0) a += INF; return a;
}
int pwr(int a, int b)
{
    int res = 1;
    while(b > 0)
    {
        if (b & 1) res = mul(res, a);
        b >>= 1;
        a = mul(a, a);
    }
    return res;
}
int inv(int a)
{
    return pwr(a, INF - 2);
}
int dvd(int a, int b)
{
    return mul(a, inv(b));
}

int N, Q;
int arr[MAXN];
pair<pii, int> query[MAXN];
int stor[MAXV][MAXN];
int sum[MAXN][MAXN];
vi st[MAXN], en[MAXN];
int lt[MAXV], both[MAXV], rt[MAXV];
int pl[MAXN], pr[MAXN];
int ans;

int calc(int x, int y)
{
    fill(pl, pl + MAXV, 0);
    fill(pr, pr + MAXV, 0);
    int res = 0;
    vi lb, mb, rb;
    FORD(i, MAXV, 0)
    {
        if (lt[i])
        {
            int w = i;
            for (int x : lb) ckmin(w, w ^ x);
            if (w) lb.PB(w);
        }
        if (both[i])
        {
            int w = i;
            for (int x : mb) ckmin(w, w ^ x);
            if (w) mb.PB(w);
        }
        if (rt[i])
        {
            int w = i;
            for (int x : rb) ckmin(w, w ^ x);
            if (w) rb.PB(w);
        }
    }
    // for (int x : mb)
    // {
    //     cerr << x << ' ';
    // }
    // cerr << endl;
    int p = pwr(HALF, SZ(lb));
    FOR(i, 0, (1 << SZ(lb)))
    {
        int val = 0;
        FOR(j, 0, SZ(lb))
        {
            if (!(i & (1 << j))) continue;
            val ^= lb[j];
        }
        pl[x ^ val] += p;
        // cerr << "pl " << (x ^ val) << endl;
    }
    p = pwr(HALF, SZ(rb));
    FOR(i, 0, (1 << SZ(rb)))
    {
        int val = 0;
        FOR(j, 0, SZ(rb))
        {
            if (!(i & (1 << j))) continue;
            val ^= rb[j];
        }
        pr[y ^ val] += p;
        // cerr << "pr " << (y ^ val) << endl;
    }
    p = pwr(HALF, SZ(mb));
    FOR(i, 0, (1 << SZ(mb)))
    {
        int val = 0;
        FOR(j, 0, SZ(mb))
        {
            if (!(i & (1 << j))) continue;
            val ^= mb[j];
        }
        FOR(j, 0, MAXV)
        {
            FOR(k, 0, MAXV)
            {
                int prob = mul(p, mul(pl[j ^ val], pr[k ^ val]));
                if (prob)
                // cerr << "prob " << j << ' ' << k << " += " << prob << endl;
                res = add(res, mul(prob, mul(j, k)));
            }
        }
    }
    // cerr << "result " << res << endl;
    return res;
}

int32_t main()
{
    cout << fixed << setprecision(12);
    cerr << fixed << setprecision(4);
    ios_base::sync_with_stdio(false); cin.tie(0);
    cin >> N;
    FOR(i, 0, N)
    {
        cin >> arr[i];
    }
    cin >> Q;
    FOR(i, 0, Q)
    {
        int l, r, x;
        cin >> l >> r >> x; l--; r--;
        query[i] = {{l, r}, x};
        st[l].PB(i);
        en[r].PB(i);
    }
    //the last bit probably has smth to do w the fact that you only care about 7 vectors because basis blah blah blah crap.
    FOR(i, 0, N)
    {
        fill(lt, lt + MAXV, 0);
        fill(both, both + MAXV, 0);
        fill(rt, rt + MAXV, 0);
        //it affects left only, both
        FOR(j, 0, Q)
        {
            if (query[j].fi.fi <= i && query[j].fi.se >= i)
            {
                both[query[j].se]++;
            }
        }
        int co = mul(i + 1, N - i);
        // cerr << "solve " << i << ' ' << i << endl;
        ans = add(ans, mul(co, calc(arr[i], arr[i])));
        FOR(j, i + 1, N)
        {
            for (int q : en[j - 1])
            {
                if (query[q].fi.fi <= i)
                {
                    both[query[q].se]--;
                    lt[query[q].se]++;
                }
                else
                {
                    rt[query[q].se]--;
                }
            }
            for (int q : st[j])
            {
                rt[query[q].se]++;
            }
            co = mul(2, mul(i + 1, N - j));
            // cerr << "solve " << i << ' ' << j << " co = " << co << endl;
            ans = add(ans, mul(co, calc(arr[i], arr[j])));
            //it's actually a range query
        }
    }
    // cerr << "ans == " << ans << endl;
    FOR(i, 0, Q)
    {
        ans = add(ans, ans);
    }
    cout << ans << '\n';
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 6 ms 384 KB Output is correct
3 Correct 60 ms 384 KB Output is correct
4 Correct 78 ms 512 KB Output is correct
5 Correct 10 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 6 ms 384 KB Output is correct
3 Correct 60 ms 384 KB Output is correct
4 Correct 78 ms 512 KB Output is correct
5 Correct 10 ms 512 KB Output is correct
6 Correct 1970 ms 504 KB Output is correct
7 Correct 382 ms 504 KB Output is correct
8 Correct 597 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 2083 ms 512 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2090 ms 512 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 426 ms 504 KB Output is correct
2 Correct 463 ms 384 KB Output is correct
3 Correct 385 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 426 ms 504 KB Output is correct
2 Correct 463 ms 384 KB Output is correct
3 Correct 385 ms 504 KB Output is correct
4 Incorrect 294 ms 512 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 6 ms 384 KB Output is correct
3 Correct 60 ms 384 KB Output is correct
4 Correct 78 ms 512 KB Output is correct
5 Correct 10 ms 512 KB Output is correct
6 Correct 1970 ms 504 KB Output is correct
7 Correct 382 ms 504 KB Output is correct
8 Correct 597 ms 504 KB Output is correct
9 Correct 426 ms 504 KB Output is correct
10 Correct 463 ms 384 KB Output is correct
11 Correct 385 ms 504 KB Output is correct
12 Execution timed out 2082 ms 488 KB Time limit exceeded
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 6 ms 384 KB Output is correct
3 Correct 60 ms 384 KB Output is correct
4 Correct 78 ms 512 KB Output is correct
5 Correct 10 ms 512 KB Output is correct
6 Correct 1970 ms 504 KB Output is correct
7 Correct 382 ms 504 KB Output is correct
8 Correct 597 ms 504 KB Output is correct
9 Correct 426 ms 504 KB Output is correct
10 Correct 463 ms 384 KB Output is correct
11 Correct 385 ms 504 KB Output is correct
12 Incorrect 294 ms 512 KB Output isn't correct
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 6 ms 384 KB Output is correct
3 Correct 60 ms 384 KB Output is correct
4 Correct 78 ms 512 KB Output is correct
5 Correct 10 ms 512 KB Output is correct
6 Correct 1970 ms 504 KB Output is correct
7 Correct 382 ms 504 KB Output is correct
8 Correct 597 ms 504 KB Output is correct
9 Execution timed out 2083 ms 512 KB Time limit exceeded
10 Halted 0 ms 0 KB -