Pagini recente » Cod sursa (job #509830) | Cod sursa (job #1798582) | Sandbox (cutiuţa cu năsip) | Cod sursa (job #2693062) | Cod sursa (job #2934361)
#include <fstream>
#include <climits>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <bitset>
#include <map>
#include <cstring>
#include <algorithm>
#include <cmath>
#define NMAX 100003
using namespace std;
//Solutia cu Smenul lui Batog
//50 puncte
int n,k,nrB;
int v[NMAX];
ifstream fin("arbint.in");
ofstream fout("arbint.out");
struct bucket {
int st, dr;
int maxim;
}buckets[NMAX/2];
int query(int st, int dr)
{
int indice;
for (indice = 1; indice <= nrB && buckets[indice].dr < st; indice++);
int maxim = -1;
//in solSt am indicele bucket-ului
for (int i = st; i <= buckets[indice].dr; i++)
{
maxim = max(v[i], maxim);
}
indice++;
while (indice <= nrB && buckets[indice].dr <= dr)
{
maxim = max(maxim, buckets[indice].maxim);//maximul din bucket-urile intermediare
indice++;
}
//maximul din ultimul bucket partial
for (int i = buckets[indice].st; i <= dr && indice <= nrB; i++)
{
maxim = max(v[i], maxim);
}
return maxim;
}
void update(int poz, int x)
{
int indice;
for (indice = 1; indice <= nrB && !(buckets[indice].st <= poz && poz <= buckets[indice].dr); indice++);
//reactualizez bucket-ul indice
v[poz] = x;
buckets[indice].maxim = -1;
for (int i = buckets[indice].st; i <= buckets[indice].dr; i++)
{
buckets[indice].maxim = max(buckets[indice].maxim, v[i]);
}
}
int main()
{
fin >> n>>k;
for (int i = 1; i <= n; i++)
{
fin >> v[i];
}
int lg = sqrt(n);//o sa am lg elemente in fiecare bucket
nrB = n / lg;//numarul de bucket-uri
for (int i = 1; i <= nrB; i++)
{
buckets[i].st = buckets[i - 1].st + 1;
buckets[i].dr = i*lg;
//imi iau maximul pe secventa
buckets[i].maxim = -1;
for (int j = buckets[i].st; j <= buckets[i].dr; j++)
{
buckets[i].maxim = max(buckets[i].maxim, v[j]);
}
}
//e posibil sa imi mai ramana elemente
if (n % lg != 0)
{
nrB++;
buckets[nrB].st = buckets[nrB - 1].dr + 1;
buckets[nrB].dr = n;
buckets[nrB].maxim = -1;
for (int j = buckets[nrB].st; j <= buckets[nrB].dr; j++)
{
buckets[nrB].maxim = max(buckets[nrB].maxim, v[j]);
}
}
for (int i = 1; i <= k; i++)
{
int cerinta;
fin >> cerinta;
if (cerinta == 0)
{
//se va face query
int indSt, indDr;
fin >> indSt >> indDr;
fout << query(indSt, indDr) << "\n";
}
else {
//se face update
int poz,val;
fin >> poz>>val;
update(poz,val);
}
}
return 0;
}