#include <iostream>
#include <set>
#include <unordered_map>
#include <cstdint>
#include <vector>
#include <numeric>
using namespace std;
struct Elem
{
int32_t m_iNum;
int32_t m_iMaxLenSoFar;
unordered_map<int32_t, int32_t> m_umJump2MaxLen;
bool operator<(int32_t iNum) const
{
return m_iNum < iNum;
}
void Update(int32_t iJump, int32_t iMaxLen)
{
if (m_umJump2MaxLen.size() == 0)
{
m_umJump2MaxLen[iJump] = iMaxLen;
m_iMaxLenSoFar = iMaxLen;
return;
}
if (m_umJump2MaxLen.count(iJump) == 0 || m_umJump2MaxLen[iJump] < iMaxLen)
{
m_umJump2MaxLen[iJump] = iMaxLen;
}
if (iMaxLen > m_iMaxLenSoFar)
m_iMaxLenSoFar = iMaxLen;
}
};
static size_t iCnt, iGoodInVectCnt;
static set<int32_t>::const_iterator itSet;
static set<int32_t> setNumbers;
static vector<Elem> vElems;
static inline void MakeUpdate(int32_t iNum)
{
int iMyIndex = (int) iGoodInVectCnt;
Elem & el2Update = vElems[iMyIndex];
el2Update.m_iNum = iNum;
el2Update.m_umJump2MaxLen.clear();
el2Update.m_iMaxLenSoFar = 1;
if (iMyIndex == 0)
return;
if (iMyIndex == 1)
{
int32_t iJump = iNum - vElems[0].m_iNum;
if (iJump == 1)
return;
el2Update.m_iMaxLenSoFar = 2;
el2Update.m_umJump2MaxLen[iJump] = 2;
return;
}
for (int iLowerIndex = iMyIndex - 1; iLowerIndex >= 0; --iLowerIndex)
{
const Elem & elLower = vElems[iLowerIndex];
int32_t iJump = iNum - elLower.m_iNum;
if (iJump == 1)
continue;
el2Update.Update(iJump, 2);
for (auto itLower = elLower.m_umJump2MaxLen.begin(); itLower != elLower.m_umJump2MaxLen.end(); ++itLower)
{
int32_t iLowerJump = itLower->first;
int32_t iLowerLen = itLower->second;
int32_t iGcd = gcd(iJump, iLowerJump);
if (iGcd == 1) continue;
el2Update.Update(iGcd, iLowerLen + 1);
}
}
if (vElems[iMyIndex - 1].m_iMaxLenSoFar > el2Update.m_iMaxLenSoFar)
{
el2Update.m_iMaxLenSoFar = vElems[iMyIndex - 1].m_iMaxLenSoFar;
}
}
static inline void MakeUpdateLoop()
{
for (; itSet != setNumbers.end(); ++itSet, ++iGoodInVectCnt)
{
MakeUpdate(*itSet);
}
}
static inline void FindUpdatePositions(int32_t iNum)
{
itSet = setNumbers.lower_bound(iNum);
auto itVect = lower_bound(vElems.begin(), vElems.end(), iNum);
if (itVect == vElems.end())
{
iGoodInVectCnt = iCnt;
}
else
{
iGoodInVectCnt = itVect - vElems.begin();
}
}
static void InsertNumber(int32_t iNum)
{
setNumbers.insert(iNum);
FindUpdatePositions(iNum);
iCnt++;
if (vElems.size() < setNumbers.size())
{
vElems.resize(vElems.size() + 1);
}
MakeUpdateLoop();
}
static void DeleteNumber(int32_t iNum)
{
iCnt--;
setNumbers.erase(iNum);
FindUpdatePositions(iNum);
if (vElems.size() > setNumbers.size())
{
vElems.resize(vElems.size() - 1);
}
MakeUpdateLoop();
}
static inline void DoOp(int32_t iNum)
{
if (setNumbers.count(iNum) == 0)
{
InsertNumber(iNum);
}
else
{
DeleteNumber(iNum);
}
}
static inline int GetAnswer()
{
if (iCnt <= 2)
return (int) iCnt;
return vElems.back().m_iMaxLenSoFar;
}
int main()
{
int iDummy, iOpCnt, iNum;
// freopen("sample_input.txt", "r", stdin);
// freopen("sample_output.txt", "w", stdout);
cin >> iDummy >> iOpCnt;
iCnt = 0;
for (int i = 1;; ++i)
{
cin >> iNum;
DoOp(iNum);
int iAnswer = GetAnswer();
cout << iAnswer;
if (i == iOpCnt) break;
cout << endl;
}
}
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 | #include <iostream> #include <set> #include <unordered_map> #include <cstdint> #include <vector> #include <numeric> using namespace std; struct Elem { int32_t m_iNum; int32_t m_iMaxLenSoFar; unordered_map<int32_t, int32_t> m_umJump2MaxLen; bool operator<(int32_t iNum) const { return m_iNum < iNum; } void Update(int32_t iJump, int32_t iMaxLen) { if (m_umJump2MaxLen.size() == 0) { m_umJump2MaxLen[iJump] = iMaxLen; m_iMaxLenSoFar = iMaxLen; return; } if (m_umJump2MaxLen.count(iJump) == 0 || m_umJump2MaxLen[iJump] < iMaxLen) { m_umJump2MaxLen[iJump] = iMaxLen; } if (iMaxLen > m_iMaxLenSoFar) m_iMaxLenSoFar = iMaxLen; } }; static size_t iCnt, iGoodInVectCnt; static set<int32_t>::const_iterator itSet; static set<int32_t> setNumbers; static vector<Elem> vElems; static inline void MakeUpdate(int32_t iNum) { int iMyIndex = (int) iGoodInVectCnt; Elem & el2Update = vElems[iMyIndex]; el2Update.m_iNum = iNum; el2Update.m_umJump2MaxLen.clear(); el2Update.m_iMaxLenSoFar = 1; if (iMyIndex == 0) return; if (iMyIndex == 1) { int32_t iJump = iNum - vElems[0].m_iNum; if (iJump == 1) return; el2Update.m_iMaxLenSoFar = 2; el2Update.m_umJump2MaxLen[iJump] = 2; return; } for (int iLowerIndex = iMyIndex - 1; iLowerIndex >= 0; --iLowerIndex) { const Elem & elLower = vElems[iLowerIndex]; int32_t iJump = iNum - elLower.m_iNum; if (iJump == 1) continue; el2Update.Update(iJump, 2); for (auto itLower = elLower.m_umJump2MaxLen.begin(); itLower != elLower.m_umJump2MaxLen.end(); ++itLower) { int32_t iLowerJump = itLower->first; int32_t iLowerLen = itLower->second; int32_t iGcd = gcd(iJump, iLowerJump); if (iGcd == 1) continue; el2Update.Update(iGcd, iLowerLen + 1); } } if (vElems[iMyIndex - 1].m_iMaxLenSoFar > el2Update.m_iMaxLenSoFar) { el2Update.m_iMaxLenSoFar = vElems[iMyIndex - 1].m_iMaxLenSoFar; } } static inline void MakeUpdateLoop() { for (; itSet != setNumbers.end(); ++itSet, ++iGoodInVectCnt) { MakeUpdate(*itSet); } } static inline void FindUpdatePositions(int32_t iNum) { itSet = setNumbers.lower_bound(iNum); auto itVect = lower_bound(vElems.begin(), vElems.end(), iNum); if (itVect == vElems.end()) { iGoodInVectCnt = iCnt; } else { iGoodInVectCnt = itVect - vElems.begin(); } } static void InsertNumber(int32_t iNum) { setNumbers.insert(iNum); FindUpdatePositions(iNum); iCnt++; if (vElems.size() < setNumbers.size()) { vElems.resize(vElems.size() + 1); } MakeUpdateLoop(); } static void DeleteNumber(int32_t iNum) { iCnt--; setNumbers.erase(iNum); FindUpdatePositions(iNum); if (vElems.size() > setNumbers.size()) { vElems.resize(vElems.size() - 1); } MakeUpdateLoop(); } static inline void DoOp(int32_t iNum) { if (setNumbers.count(iNum) == 0) { InsertNumber(iNum); } else { DeleteNumber(iNum); } } static inline int GetAnswer() { if (iCnt <= 2) return (int) iCnt; return vElems.back().m_iMaxLenSoFar; } int main() { int iDummy, iOpCnt, iNum; // freopen("sample_input.txt", "r", stdin); // freopen("sample_output.txt", "w", stdout); cin >> iDummy >> iOpCnt; iCnt = 0; for (int i = 1;; ++i) { cin >> iNum; DoOp(iNum); int iAnswer = GetAnswer(); cout << iAnswer; if (i == iOpCnt) break; cout << endl; } } |
English