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
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include <cstdio>
#include <unordered_set>
#include <string>
#include <algorithm>
#include <vector>

#include <chrono>
#include <iostream>

#define MAX_N 15010
#define MAX_T 840
#define MAX_Q 1000100

#define D(x) 
#define DC(x) 
#define DT(x) 

using namespace std;
int n,m,nm[2], q;

unordered_set<string> s[2][MAX_N];
char s_t[2][MAX_T][MAX_N];

void calc_s_t(int ori, char str_v) {
    for(int i=0;i < nm[ori]; i++) {
        char tbuf[MAX_T];
        for(int t=0;t<MAX_T;t++) tbuf[t] = 1;
        for(const string &str: s[ori][i]) {
            int l = str.size(),si = 0;
            for(int t=0;t<MAX_T;t++) {
                if(str[si++] != str_v) tbuf[t] = 0;
                if(si>=l) si-=l;
            }
        }
        for(int t=0;t<MAX_T;t++) s_t[ori][t][i] = tbuf[t];
    }
}

struct Query {
    int v;
    int child[2];

    int t;
    int ab[2],cd[2];
    int t_start;
    int t_stop;
};

Query query[MAX_Q];
vector<int> query_t_idx[MAX_T];
int query_done_count;

#define MAX_DIR 3
#define DIR(x) (1+(x))
int axis[2][MAX_DIR][MAX_N];


int rndd() {
    static const int _rndd[] = {0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0 };
    static int _rndd_idx = 0;
    _rndd_idx = (_rndd_idx + 1) & 0xff;
    return _rndd[_rndd_idx];
}


int calc_ori(int t_mod) {
    int ori;
    for(ori=0;ori<2;ori++)
        for(int i=0;i<nm[ori];i++)
            if (s_t[ori][t_mod][i]) return ori;
    return ori;
}

void node_done(int qidx, Query &v, int t, int limit, const char* str) {
        v.t_stop = t;
        query_done_count++;
        D(printf("%s : q_idx: %d  t(%d,%d,%d) v:%d %d\n", str, qidx, v.t, v.t_start, v.t_stop, v.v, limit));
        v.child[0] = v.child[1] = 0; 
}


void query_done(int q_idx, int t) {
    std:vector<int> buf = {q_idx};
    while(!buf.empty()) {
        int q = buf.back();
        buf.pop_back();
        if (q > 0) {
            Query & v = query[q];
            buf.push_back(v.child[0]);
            buf.push_back(v.child[1]);
            node_done(q, v, t, -1, "-Remove point3!");
        }
    }
}

void _print_tree(int q_idx) {
    printf("[%d ", q_idx);
    if(q_idx) {
        _print_tree(query[q_idx].child[0]);
        _print_tree(query[q_idx].child[1]);
    }
    printf("]");
}
void print_tree(int q_idx, const char * s ="tree: ") {
    printf("%s",s);
    _print_tree(q_idx);
    printf("\n");
}


void _concat_tree(int *save, int a, int b, int dir) {
    if (a == 0) {
        *save = b;
    } else  if (b == 0) {
        *save = a;
    }  else {      
        int rnd = rndd();
        if (dir*query[a].v >= dir*query[b].v) {
            *save = b;
            _concat_tree(&query[b].child[rnd], query[b].child[rnd], a, dir);
        } else {
            *save = a;
            _concat_tree(&query[a].child[rnd], query[a].child[rnd], b, dir);
        }
    }
}
void concat_tree(int *save, int a, int b, int dir) {
    DC(printf("concat: dir: %d\n", dir));
    DC(print_tree(a, "tree a: "));
    DC(print_tree(b, "tree b: "));
    _concat_tree(save, a, b, dir);
    DC(print_tree(*save, "result:"));
}

void remove_smaller(int *save, int limit, int dir, int t) {
    while(*save > 0 && dir*limit >= dir*query[*save].v)  {
        int qidx= *save;
        Query &v = query[*save];
        int rnd = rndd();
        *save = v.child[rnd];
        concat_tree(save, *save, v.child[!rnd], dir);

        node_done(qidx, v, t, limit, "-Remove point2!");
    }
}

void compact(int from, int to, int place, int ori, int dir, int t) {
//    printf("compact: [%d - %d] dir: %d\n", from, to, dir);
    //suffle?
//    for(int i=from;i<=(from+to)/2;i++) 
//        if(rndd()) swap(axis[ori][DIR(dir)][i], axis[ori][DIR(dir)][to-(i-from)]);
    
    for(int i=from;i<=to;i++) {
        if (i == place) continue;
        if(axis[ori][DIR(dir)][i]) {
            concat_tree(&axis[ori][DIR(dir)][place], axis[ori][DIR(dir)][place], axis[ori][DIR(dir)][i], dir);
            axis[ori][DIR(dir)][i] = 0;
        }
    }
//    print_tree(axis[ori][DIR(dir)][place]);
    remove_smaller(&axis[ori][DIR(dir)][place], place, dir, t);
}

void print_axis(int ori, int t){
    printf("axis  1:");
    for(int i=0;i<=nm[ori];i++) printf("%d%c", axis[ori][DIR(1)][i], s_t[ori][t%MAX_T][i]? '|' : ' ');
    printf("\naxis -1:");
    for(int i=0;i<=nm[ori];i++) printf("%d%c", axis[ori][DIR(-1)][i], s_t[ori][t%MAX_T][i]? '|' : ' ');
    printf("\n");
} 

int main() {
    auto start = chrono::steady_clock::now();
    scanf("%d %d %d",&n,&m,&q);
    for(int i=0;i<n;i++) {
        for(int j=0;j<m;j++) {
            char str[100];
            scanf(" %s", str);
            s[0][i].emplace(str);
            s[1][j].emplace(str);
        }
    }
    DT(cerr << "t0: " << chrono::duration_cast<chrono::milliseconds>(chrono::steady_clock::now() - start).count() << " ms" << endl);

    nm[0] = n; nm[1] = m;
    calc_s_t(0, '1');
    calc_s_t(1, '0');

    DT(cerr << "t1: " << chrono::duration_cast<chrono::milliseconds>(chrono::steady_clock::now() - start).count() << " ms" << endl);

    for(int i=1;i<=q;i++) {
        Query & v = query[i];
        scanf("%d %d %d %d %d ", &v.t, &v.ab[0], &v.ab[1], &v.cd[0], &v.cd[1]);
        query_t_idx[v.t%MAX_T].push_back(i);
    }
    DT(cerr << "t2: " << chrono::duration_cast<chrono::milliseconds>(chrono::steady_clock::now() - start).count() << " ms" << endl);

    int t;
    long tt1 = 0, tt2 = 0, tt3 = 0;
    for(t = 0; query_done_count < q;t++) {

        DT(auto tt_start = chrono::steady_clock::now();)

    //for(int t = 0; t < 2*MAX_T;t++) {
        int ori = 0,t_mod = t%MAX_T;
        ori = calc_ori(t_mod);

        D(printf("---------------------------------: t:%d ori:%d\n", t, ori));
        if(t < MAX_T) {
            //add new queries
            //shuffle?
            //random_shuffle(query_t_idx[t].begin(), query_t_idx[t].end());
            for(int q_idx: query_t_idx[t]) {
                Query &v = query[q_idx];
                v.t_start = t;
                if(ori > 1 || v.ab[ori] == v.cd[ori]) {
                    v.t_stop = t;
                    query_done_count++;
                    D(printf("query done++1!\n"));
                } else {
                    int dir;
                    v.v = v.cd[ori];
                    if(v.ab[ori] <= v.cd[ori]) dir = 1;
                    else dir =-1;
                    concat_tree(&axis[ori][DIR(dir)][v.ab[ori]], axis[ori][DIR(dir)][v.ab[ori]], q_idx, dir);
                    D(printf("-Add point: qidx: %d t:%d pos:%d v:%d dir:%d\n", q_idx, v.t, v.ab[ori], v.v, dir));
                }
            }
        }
        DT(tt1+=chrono::duration_cast<chrono::microseconds>(chrono::steady_clock::now() - tt_start).count());

        {
            D(printf("--remove other\n"));
            for(int n_ori = 0;n_ori < 2; n_ori++) {
                if (n_ori == ori) continue;
                for(int i=0;i<=nm[n_ori];i++) {
                    query_done(axis[n_ori][DIR(-1)][i], t);
                    query_done(axis[n_ori][DIR( 1)][i], t);
                    axis[n_ori][DIR(-1)][i] = 0;
                    axis[n_ori][DIR( 1)][i] = 0;
                }
            }
        }

        DT(tt2+=chrono::duration_cast<chrono::microseconds>(chrono::steady_clock::now() - tt_start).count());

        if(ori > 1) continue;
        {
            D(printf("--compact\n"));
            D(print_axis(ori, t));
            int from = 0, to;
            for(to=0;to<nm[ori];to++) {
                if(s_t[ori][t_mod][to]) {
                    if(to-from>=1) {
                        compact(from,to, to,  ori,  1, t);
                        compact(from,to, from,ori, -1, t);
                    }
                    from = to+1;
                }
            }
            if(to-from>=1) {
                compact(from,to, to,  ori,  1, t);
                compact(from,to, from,ori, -1, t);
            }
            D(print_axis(ori, t));
        }

        DT(tt3+=chrono::duration_cast<chrono::microseconds>(chrono::steady_clock::now() - tt_start).count());

    }
    D(
        printf("q: %d %d \n" ,q, query_done_count);
        for(int i=1;i<=q;i++) {
            printf("%d : %d %d %d\n", query[i].t + query[i].t_stop - query[i].t_start, query[i].t , query[i].t_start , query[i].t_stop);
        }
    )
    for(int i=1;i<=q;i++) {
        printf("%d\n", query[i].t + query[i].t_stop - query[i].t_start);
    }
    DT(cerr << "t:" << t << endl);
    DT(cerr << "tt1:" << tt1/1000  << " tt2:" << (tt2 - tt1)/1000 << " tt3:" << (tt3 -tt2)/1000 << endl);
    DT(cerr << "tend: " << chrono::duration_cast<chrono::milliseconds>(chrono::steady_clock::now() - start).count() << " ms" << endl);
}