Cod sursa(job #2750079)

Utilizator VanillaSoltan Marian Vanilla Data 9 mai 2021 20:39:51
Problema Plantatie Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.5 kb
#include <bits/stdc++.h>
#include <fstream>
using namespace std;
typedef long long int64;
typedef vector<int> vec;
typedef vector<int64> vec64;
    string __fname = "plantatie";
    ifstream in (__fname + ".in");
    ofstream out (__fname + ".out");
    #define cin in
    #define cout out
#define ss cout << " ";
#define nn cout << "\n";
#define ct(x) cout << x;
#define cts(x) cout << x << " ";
#define ctn(x) cout << x << "\n";
#define db(x) cout << "> " << #x << ": " << x << "\n";
#define qr queries();
void solve(int);
void YN(bool b){if (b){ctn("YES");}else{ctn ("NO");}};
void yn(bool b){if (b){ctn("Yes");}else{ctn ("No");}};
void queries(){int n;cin >> n;for (int i = 1; i <= n; i++) solve(i);}
int64 ceildiv(int64 a, int64 b) {return a / b + !!(a % b);}
// // // // // // // // // // // // // // // // // // // // // // 
/*                  TEMPLATE - VANILLA                         */
// // // // // // // // // // // // // // // // // // // // // //
const int maxn = 200200;
const int64 mod = 1000000007;
const double pi = 3.14159265359;
const int ddx[] = {-1, -1, 0, 1, 1, 1, 0, -1};
const int ddy[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
 
void solve(int id){

    return;
}
 
template <class T> class rmq {
    private: 
        vector <vector <T> > dp;
        vector <T> a;

    public:
        rmq(vector <T> in) {
            int n = in.size();
            a = in;
            dp.resize(n, vector <T> (log2(n) + 1));
            for (int i = 0; i < n; i++) {
                dp[i][0] = a[i];
            }
            for (int j = 1; j <= log2(n); j++) {
                for (int i = 0; i + (1 << j) <= n; i++) {
                    dp[i][j] = max(dp[i][j-1], dp[i + (1 << (j - 1))][j-1]);
                }
            }
        }

        T get_max(int l, int r) {
            int pw = log2(r - l + 1);
            T rs = max(dp[l][pw], dp[r - (1 << pw) + 1][pw]);
            return rs;
        }
};

int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    int n, k;
    cin >> n >> k;
    vector <vector <int> > a (n, vector <int> (n));
    vector <rmq <int> > v;
    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++) { 
            cin >> a[i][j];
        }
        rmq <int> r(a[i]);
        v.push_back(r);
    }
    while (k--) {
        int x,y,l;
        cin >> x >> y >> l;
        x--; y--;
        int rs = 0;
        for (int i = 0; i < l; i++) {
            rs = max(rs, v[x + i].get_max(y, y + l - 1));
        }
        ctn(rs);
    }
    return 0;
}