#include <bits/stdc++.h> #include <algorithm> using namespace std; // bool v[5000+1][5000+1]; long long sciezka[200002]; vector<vector<bool> > v; vector<vector<bool> > kierunek; int main() { ios_base::sync_with_stdio(0); int k,n,m,r,c,z,x = 0; cin>>n>>m; v.resize(n+1, vector<bool>(m+1)); kierunek.resize(n+1, vector<bool>(m+1)); for(int i =0;i<n;i++) for(int j =0;j<m;j++)kierunek[i][j]=v[i][j]=false; vector< pair<int,int> > q; vector< pair<int,int> > cofnij; for(int i = 0; i <= m; i++)v[n][i]=true; for(int i = 0; i <= n; i++)v[i][m]=true; cin>>k; bool jest_na_sciezce = true; int pierwszy_przebieg = false; while(k--){ cin>>r>>c>>z; int rx = (r^x)%n; int cx = (c^x)%m; bool ok = false; if(pierwszy_przebieg){ long long w = (long long)rx*(long long)m+(long long)cx; long long *p = find(sciezka,sciezka+(n+m-1),w); if (p != sciezka+(n+m-1)){ jest_na_sciezce = true; }else{ jest_na_sciezce = false; ok = true; } } pierwszy_przebieg = true; v[rx][cx]=true; //fast weryfikacja if(jest_na_sciezce){ q.clear(); q.push_back(pair<int,int>(0,0)); v[0][0]=true; cofnij.clear(); cofnij.push_back(pair<int,int>(0,0)); while(!q.empty()){ pair<int,int> p = q.back(); // cout<<p.first<<" "<<p.second<<"\n"; if(p.first >= n-1 && p.second >= m-1){ ok = true; break; } if(p.first >= n || p.second >= m){ ok = true; break; } q.pop_back(); if(!v[p.first][p.second+1]){ q.push_back(pair<int,int>(p.first,p.second+1)); cofnij.push_back(pair<int,int>(p.first,p.second+1)); v[p.first][p.second+1] = true; kierunek[p.first][p.second+1]=true; } if(!v[p.first+1][p.second]){ q.push_back(pair<int,int>(p.first+1,p.second)); cofnij.push_back(pair<int,int>(p.first+1,p.second)); v[p.first+1][p.second] = true; } } //aktualizuj_sciezke if(ok){ int xx = n-1; int yy = m-1; //wypisz_sciezke for(int i = 0;i<n+m-1;i++){ sciezka[i]=(long long)xx*(long long)m+(long long)yy; if(kierunek[xx][yy]){ yy--; }else{ xx--; } // cout<<xx<<" "<<yy<<"\n"; } for(int i =0;i<cofnij.size(); i++) kierunek[cofnij[i].first][cofnij[i].second]=v[cofnij[i].first][cofnij[i].second]=false; //blokuj for(int i = 1;i<n+m-1;i++){ int y1 = (sciezka[i]%m); int x1 = (sciezka[i]-y1)/m; int py1 = (sciezka[i-1]%m); int px1 = (sciezka[i-1]-py1)/m; if(!v[x1+1][y1] && py1 != y1 ) v[x1+1][y1]=true; } sort(sciezka,sciezka+(n+m-1)); }else{ for(int i =0;i<cofnij.size(); i++) kierunek[cofnij[i].first][cofnij[i].second]=v[cofnij[i].first][cofnij[i].second]=false; } //int hh; //cin>>hh; // czyszcze } if(ok){ cout<<"NIE\n"; }else{ v[rx][cx]=false; x^=z; cout<<"TAK\n"; } } return 0; }
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 | #include <bits/stdc++.h> #include <algorithm> using namespace std; // bool v[5000+1][5000+1]; long long sciezka[200002]; vector<vector<bool> > v; vector<vector<bool> > kierunek; int main() { ios_base::sync_with_stdio(0); int k,n,m,r,c,z,x = 0; cin>>n>>m; v.resize(n+1, vector<bool>(m+1)); kierunek.resize(n+1, vector<bool>(m+1)); for(int i =0;i<n;i++) for(int j =0;j<m;j++)kierunek[i][j]=v[i][j]=false; vector< pair<int,int> > q; vector< pair<int,int> > cofnij; for(int i = 0; i <= m; i++)v[n][i]=true; for(int i = 0; i <= n; i++)v[i][m]=true; cin>>k; bool jest_na_sciezce = true; int pierwszy_przebieg = false; while(k--){ cin>>r>>c>>z; int rx = (r^x)%n; int cx = (c^x)%m; bool ok = false; if(pierwszy_przebieg){ long long w = (long long)rx*(long long)m+(long long)cx; long long *p = find(sciezka,sciezka+(n+m-1),w); if (p != sciezka+(n+m-1)){ jest_na_sciezce = true; }else{ jest_na_sciezce = false; ok = true; } } pierwszy_przebieg = true; v[rx][cx]=true; //fast weryfikacja if(jest_na_sciezce){ q.clear(); q.push_back(pair<int,int>(0,0)); v[0][0]=true; cofnij.clear(); cofnij.push_back(pair<int,int>(0,0)); while(!q.empty()){ pair<int,int> p = q.back(); // cout<<p.first<<" "<<p.second<<"\n"; if(p.first >= n-1 && p.second >= m-1){ ok = true; break; } if(p.first >= n || p.second >= m){ ok = true; break; } q.pop_back(); if(!v[p.first][p.second+1]){ q.push_back(pair<int,int>(p.first,p.second+1)); cofnij.push_back(pair<int,int>(p.first,p.second+1)); v[p.first][p.second+1] = true; kierunek[p.first][p.second+1]=true; } if(!v[p.first+1][p.second]){ q.push_back(pair<int,int>(p.first+1,p.second)); cofnij.push_back(pair<int,int>(p.first+1,p.second)); v[p.first+1][p.second] = true; } } //aktualizuj_sciezke if(ok){ int xx = n-1; int yy = m-1; //wypisz_sciezke for(int i = 0;i<n+m-1;i++){ sciezka[i]=(long long)xx*(long long)m+(long long)yy; if(kierunek[xx][yy]){ yy--; }else{ xx--; } // cout<<xx<<" "<<yy<<"\n"; } for(int i =0;i<cofnij.size(); i++) kierunek[cofnij[i].first][cofnij[i].second]=v[cofnij[i].first][cofnij[i].second]=false; //blokuj for(int i = 1;i<n+m-1;i++){ int y1 = (sciezka[i]%m); int x1 = (sciezka[i]-y1)/m; int py1 = (sciezka[i-1]%m); int px1 = (sciezka[i-1]-py1)/m; if(!v[x1+1][y1] && py1 != y1 ) v[x1+1][y1]=true; } sort(sciezka,sciezka+(n+m-1)); }else{ for(int i =0;i<cofnij.size(); i++) kierunek[cofnij[i].first][cofnij[i].second]=v[cofnij[i].first][cofnij[i].second]=false; } //int hh; //cin>>hh; // czyszcze } if(ok){ cout<<"NIE\n"; }else{ v[rx][cx]=false; x^=z; cout<<"TAK\n"; } } return 0; } |