Submission #382273

# Submission time Handle Problem Language Result Execution time Memory
382273 2021-03-26T21:46:04 Z BartolM Intergalactic ship (IZhO19_xorsum) C++17
100 / 100
647 ms 17000 KB
#include <bits/stdc++.h>

using namespace std;

#define X first
#define Y second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <int, pii> pip;
typedef pair <pii, int> ppi;
typedef pair <ll, ll> pll;

const int INF=0x3f3f3f3f;
const int LOG=7;
const int N=1005;
const int MOD=1e9+7;
const int Q=1e5+5;

int add(int a, int b) {
    a+=b;
    if (a>=MOD) a-=MOD;
    return a;
}

int mul(int a, int b) {
    return (ll)a*b%MOD;
}

int n, q;
int p[N], pot[Q], uk1[N], uk2[N], isti[N], sweep[N];
vector <pii> vec[LOG], v[LOG][LOG];
vector <int> que[N];
#define DEBUG 0

int solve(int bit1, int bit2) {
    int sol=0;
    for (pii pp:vec[bit1]) uk1[pp.X]++, uk1[pp.Y+1]--;
    for (pii pp:vec[bit2]) uk2[pp.X]++, uk2[pp.Y+1]--;
    for (pii pp:v[bit1][bit2]) que[pp.X].pb(pp.Y);

    for (int i=1; i<n; ++i) uk1[i]+=uk1[i-1], uk2[i]+=uk2[i-1];


    for (int i=0; i<n; ++i) {
        for (int j=i; j<n; ++j) sweep[j]=0;
        for (int r:que[i]) sweep[r]++;
        for (int j=n-1; j>=i; j--) {
            sweep[j]+=sweep[j+1];
            isti[j]+=sweep[j];
        }
        int curr_bit=!!(p[i] & (1<<bit1));

        for (int j=i; j<n; ++j) {

            int interi=mul(i+1, n-j);
            int komb_i=curr_bit, komb_j=!!(p[j] & (1<<bit2));
            int unija=uk1[i]+uk2[j]-isti[j];
            int curr=1;

            if (i!=j) interi=mul(interi, 2);
            else if (bit1==bit2) {
                curr=mul(pot[q-unija], isti[j] ? pot[isti[j]-1] : curr_bit);
                sol=add(sol, mul(curr, interi));
                #if DEBUG
                    printf("1. slucaj, curr: %d, interi: %d\n", curr, interi);
                #endif
                continue;
            }



            if (uk1[i]>isti[j]) {
                komb_i=pot[uk1[i]-isti[j]-1];
                if (uk2[j]>isti[j]) curr=mul(pot[isti[j]], mul(pot[uk2[j]-isti[j]-1], komb_i));
                else curr=mul(isti[j] ? pot[isti[j]-1] : komb_j, komb_i);
            }
            else {
                if (uk2[j]>isti[j]) curr=mul(isti[j] ? pot[isti[j]-1] : komb_i, pot[uk2[j]-isti[j]-1]);
                else if (isti[j]) curr=mul(pot[isti[j]-1], !(komb_i^komb_j));
                else curr=komb_i==komb_j && komb_i==1;
            }
            curr=mul(curr, pot[q-unija]);
            #if DEBUG
                printf("i: %d, j: %d, isti: %d\n", i, j, isti[j]);
                printf("uk1: %d, uk2: %d, unija: %d, preostali: %d\n", uk1[i], uk2[j], unija, q-unija);
                printf("interi: %d, curr: %d\n\n", interi, curr);
            #endif
            sol=add(sol, mul(curr, interi));
        }
    }
    #if DEBUG
        printf("bit1: %d, bit2: %d, sol == %d\n", bit1, bit2, sol);
    #endif // DEBUG
    return sol;
}

void reset() {
    for (int i=0; i<n; ++i) que[i].clear();
    memset(uk1, 0, sizeof uk1);
    memset(uk2, 0, sizeof uk2);
    memset(isti, 0, sizeof isti);
}

void load() {
    scanf("%d", &n);
    for (int i=0; i<n; ++i) scanf("%d", p+i);
    scanf("%d", &q);
    for (int i=0; i<q; ++i) {
        int l, r, x;
        scanf("%d %d %d", &l, &r, &x); l--; r--;
        for (int i=0; i<LOG; ++i) {
            if ((1<<i) & x) vec[i].pb(mp(l, r));
            else continue;
            for (int j=0; j<LOG; ++j) {
                if ((1<<j) & x) v[i][j].pb(mp(l, r));
            }
        }
    }
}

int main() {

    load();

    pot[0]=1;
    for (int i=1; i<=q; ++i) pot[i]=mul(pot[i-1], 2);

    int sol=0;
    for (int i=0; i<LOG; ++i) {
        for (int j=0; j<LOG; ++j) {
            reset();
            int br=mul((1<<i), (1<<j));
            sol=add(sol, mul(solve(i, j), br));
        }
    }
    printf("%d\n", sol);
    return 0;
}

/*
2
3 1
2
2 2 2
1 2 0

2
1 1
1
1 2 3

*/

Compilation message

xorsum.cpp: In function 'void load()':
xorsum.cpp:107:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  107 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
xorsum.cpp:108:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  108 |     for (int i=0; i<n; ++i) scanf("%d", p+i);
      |                             ~~~~~^~~~~~~~~~~
xorsum.cpp:109:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  109 |     scanf("%d", &q);
      |     ~~~~~^~~~~~~~~~
xorsum.cpp:112:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  112 |         scanf("%d %d %d", &l, &r, &x); l--; r--;
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 6 ms 364 KB Output is correct
7 Correct 5 ms 364 KB Output is correct
8 Correct 5 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 72 ms 10000 KB Output is correct
2 Correct 14 ms 1644 KB Output is correct
3 Correct 6 ms 492 KB Output is correct
4 Correct 5 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 463 ms 620 KB Output is correct
2 Correct 437 ms 512 KB Output is correct
3 Correct 405 ms 492 KB Output is correct
4 Correct 398 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 364 KB Output is correct
2 Correct 2 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 364 KB Output is correct
2 Correct 2 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 5 ms 1004 KB Output is correct
5 Correct 5 ms 1132 KB Output is correct
6 Correct 5 ms 1004 KB Output is correct
7 Correct 5 ms 1004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 6 ms 364 KB Output is correct
7 Correct 5 ms 364 KB Output is correct
8 Correct 5 ms 364 KB Output is correct
9 Correct 2 ms 364 KB Output is correct
10 Correct 2 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 7 ms 492 KB Output is correct
13 Correct 8 ms 492 KB Output is correct
14 Correct 7 ms 492 KB Output is correct
15 Correct 7 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 6 ms 364 KB Output is correct
7 Correct 5 ms 364 KB Output is correct
8 Correct 5 ms 364 KB Output is correct
9 Correct 2 ms 364 KB Output is correct
10 Correct 2 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 5 ms 1004 KB Output is correct
13 Correct 5 ms 1132 KB Output is correct
14 Correct 5 ms 1004 KB Output is correct
15 Correct 5 ms 1004 KB Output is correct
16 Correct 7 ms 492 KB Output is correct
17 Correct 8 ms 492 KB Output is correct
18 Correct 7 ms 492 KB Output is correct
19 Correct 7 ms 364 KB Output is correct
20 Correct 234 ms 17000 KB Output is correct
21 Correct 246 ms 16772 KB Output is correct
22 Correct 172 ms 3244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 6 ms 364 KB Output is correct
7 Correct 5 ms 364 KB Output is correct
8 Correct 5 ms 364 KB Output is correct
9 Correct 72 ms 10000 KB Output is correct
10 Correct 14 ms 1644 KB Output is correct
11 Correct 6 ms 492 KB Output is correct
12 Correct 5 ms 364 KB Output is correct
13 Correct 463 ms 620 KB Output is correct
14 Correct 437 ms 512 KB Output is correct
15 Correct 405 ms 492 KB Output is correct
16 Correct 398 ms 492 KB Output is correct
17 Correct 2 ms 364 KB Output is correct
18 Correct 2 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 5 ms 1004 KB Output is correct
21 Correct 5 ms 1132 KB Output is correct
22 Correct 5 ms 1004 KB Output is correct
23 Correct 5 ms 1004 KB Output is correct
24 Correct 7 ms 492 KB Output is correct
25 Correct 8 ms 492 KB Output is correct
26 Correct 7 ms 492 KB Output is correct
27 Correct 7 ms 364 KB Output is correct
28 Correct 234 ms 17000 KB Output is correct
29 Correct 246 ms 16772 KB Output is correct
30 Correct 172 ms 3244 KB Output is correct
31 Correct 647 ms 16548 KB Output is correct
32 Correct 622 ms 16900 KB Output is correct
33 Correct 564 ms 3052 KB Output is correct
34 Correct 575 ms 13328 KB Output is correct
35 Correct 582 ms 13444 KB Output is correct
36 Correct 580 ms 13068 KB Output is correct