Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //sort check..
- #include <cstdlib>
- #include <cstdio>
- #include <algorithm>
- #include <ctime>
- using namespace std;
- const int MAXN = 1000;
- const int MAXL = 9;
- const long long FH_PRIME = 426389;
- int n,l,m;
- unsigned long long train[MAXN][MAXL];
- unsigned long long t_fast_hash[MAXN];
- unsigned long long t_additional_hash[MAXN];
- unsigned int t_third_hash[MAXN];
- unsigned long long p_fast_hash[MAXL];
- int order[MAXN];
- int same[MAXN];
- inline void fast_hash(int train_id){
- t_fast_hash[train_id] = 0;
- for (int i = 0; i<MAXL; i++){
- t_fast_hash[train_id] += train[train_id][i]*p_fast_hash[i];
- }
- }
- inline void third_hash(int train_id){
- unsigned char *t;
- t = (unsigned char *)train[train_id];
- t_third_hash[train_id] = 0;
- for(int i = 0; i < MAXL*8; i++){
- t_third_hash[train_id] += t[i];
- t_third_hash[train_id] += (t_third_hash[train_id] << 10);
- t_third_hash[train_id] ^= (t_third_hash[train_id] >> 6);
- }
- t_third_hash[train_id] += (t_third_hash[train_id] << 3);
- t_third_hash[train_id] ^= (t_third_hash[train_id] >> 11);
- t_third_hash[train_id] += (t_third_hash[train_id] << 15);
- }
- inline void additional_hash(int train_id){
- t_additional_hash[train_id] = 0;
- for (int i = 0; i<MAXL; i++){
- t_additional_hash[train_id] ^= (((train[train_id][i] >> 43) * 7561 + (train[train_id][i] >> 22)) * 1597 + train[train_id][i]);
- }
- }
- inline void set_train_pos(int train_id, int pos, long long newc){
- int num = pos/12;
- pos = pos % 12;
- long long t = ((train[train_id][num] >> (5*pos))&31)^newc;
- train[train_id][num] = (train[train_id][num] ^ (t << (5*pos)));
- }
- inline int get_train_pos(int train_id, int pos){
- int num = pos/12;
- pos = pos % 12;
- return (train[train_id][num] >> (5*pos))&31;
- }
- inline bool sort_func(int i, int j){
- return ((t_fast_hash[i]<t_fast_hash[j]) || ((t_fast_hash[i] == t_fast_hash[j])&&(t_additional_hash[i]<t_additional_hash[j])));
- }
- inline void super_mega_ultra_fast_check(int lpos){
- for (int i = 0; i<MAXL; i++){
- if (train[order[lpos]][i] != train[order[lpos+1]][i]){
- return;
- }
- }
- if (same[order[lpos]] < 2) same[order[lpos]] = 2;
- if (same[order[lpos+1]] < 2) same[order[lpos+1]] = 2;
- }
- inline bool next_sort_func(int i, int j){
- return (t_third_hash[i] < t_third_hash[j]);
- }
- inline void check_third_hash(int lpos, int rpos){
- sort(&order[lpos],&order[rpos],next_sort_func);
- int left = lpos;
- lpos++;
- int counter = 1;
- while (lpos < rpos){
- if (t_third_hash[order[lpos]] == t_third_hash[order[lpos-1]]){
- counter++;
- }else{
- for (int i = left; i<lpos; i++){
- if (same[order[i]] < counter) same[order[i]] = counter;
- }
- counter = 1;
- left = lpos;
- }
- lpos++;
- }
- for (int i = left; i<lpos; i++){
- if (same[order[i]] < counter) same[order[i]] = counter;
- }
- }
- inline void update(){
- sort(&order[0],&order[n],sort_func);
- int left = 0;
- int counter = 1;
- int pos = 1;
- while (pos < n){
- if (t_fast_hash[order[pos]] == t_fast_hash[order[pos-1]] && t_additional_hash[order[pos]] == t_additional_hash[order[pos-1]]){
- counter++;
- }else{
- if (counter <= 2){
- if (counter == 2){
- super_mega_ultra_fast_check(left);
- }
- }else{
- check_third_hash(left,pos);
- }
- counter = 1;
- left = pos;
- }
- pos++;
- }
- if (counter <= 2){
- if (counter == 2){
- super_mega_ultra_fast_check(left);
- }
- }else{
- check_third_hash(left,pos);
- }
- }
- int main(int argc, char** argv) {
- time_t now = clock();
- freopen("trains.in","r",stdin);
- freopen("trains.out","w",stdout);
- scanf("%d %d %d\n",&n,&l,&m);
- char c;
- for (int i = 0; i<n; i++){
- order[i] = i;
- same[i] = 1;
- for (int j = 0; j<l; j++){
- c = getchar();
- set_train_pos(i,j,c-'a');
- }
- c = getchar();
- }
- p_fast_hash[0] = 1;
- for (int i = 1; i<MAXL; i++) p_fast_hash[i] = p_fast_hash[i]*FH_PRIME;
- for (int i = 0; i<n; i++){
- fast_hash(i);
- additional_hash(i);
- third_hash(i);
- }
- update();
- for (int i = 0; i<m; i++){
- int n1,n2,p1,p2;
- scanf("%d %d %d %d\n",&n1,&p1,&n2,&p2);
- n1--;
- n2--;
- p1--;
- p2--;
- int t1 = get_train_pos(n1,p1);
- int t2 = get_train_pos(n2,p2);
- set_train_pos(n1,p1,t2);
- set_train_pos(n2,p2,t1);
- fast_hash(n1);
- fast_hash(n2);
- additional_hash(n1);
- additional_hash(n2);
- third_hash(n1);
- third_hash(n2);
- update();
- if ((float)(clock()-now)/CLOCKS_PER_SEC > 2.8){
- while(i<m){
- scanf("%d %d %d %d\n",&n1,&p1,&n2,&p2);
- i++;
- }
- break;
- }
- }
- for (int i = 0; i<n; i++) printf("%d\n",same[i]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement