777C - Alyona and Spreadsheet - CodeForces Solution


binary search data structures dp greedy implementation two pointers *1600

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,
tree_order_statistics_node_update> ordered_set;
typedef tree<int,null_type,less_equal<int>,rb_tree_tag,
tree_order_statistics_node_update> ordered_multiset;
#define os ordered_set
#define oms ordered_multiset
#define int long long int
#define ull unsigned long long int
#define F first
#define S second
#define pb push_back
#define MSB(x) (63 - __builtin_clzll(x))
#define rep(i, st, end) for(int i = st; i < end; i++)
#define all(x) (x).begin(),(x).end()
#define pr(x) cout<<x<<'\n'
#define prd(x) cout<<x<<endl
typedef vector<int> vi; typedef vector<vector<int>> vvi; typedef map<int, int> mii;
const int mod = 1000000007;
using ii = pair<int, int>;

void solve(){
    int n, m;
    cin>>n>>m;
    int grid[n+1][m+1];
    memset(grid, 0, sizeof(grid));
    rep(i,1,n+1){
        rep(j,1,m+1){
            cin>>grid[i][j];
        }
    }
    int arr[n+1][m+1];
    memset(arr, 0, sizeof(arr));
    for(int j=1; j<=m; j++){
        for(int i=1; i<=n; i++){
            if(grid[i][j] >= grid[i-1][j]){
                arr[i][j] = arr[i-1][j] + 1;
            }
            else{
                arr[i][j] = 1;
            }
        }
    }
    int best[n+1] = {0};
    for(int i=1; i<=n; i++){
        for(int j=1; j<=m; j++){
            best[i] = max(best[i], arr[i][j]);   
        }
    }
    
    int q;
    cin>>q;
    while(q--){
        int l, r;
        cin>>l>>r;
        if(best[r] - 1 >= r - l){
            pr("Yes");
        }
        else{
            pr("No");
        }
    }
}


// always check indexing if using some kindof offset
// if nested loop, check if involved array indices are correct
// for bitmask dp, iterative, first itearate on mask then on others
signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);


    int t = 1; 
    // cin>>t; 
    for(int i=1; i<=t; i++){
        // cout << "Case #" << t << 
        solve();
    }
}


Comments

Submit
0 Comments
More Questions

344. Reverse String
1047. Remove All Adjacent Duplicates In String
977. Squares of a Sorted Array
852. Peak Index in a Mountain Array
461. Hamming Distance
1748. Sum of Unique Elements
897. Increasing Order Search Tree
905. Sort Array By Parity
1351. Count Negative Numbers in a Sorted Matrix
617. Merge Two Binary Trees
1450. Number of Students Doing Homework at a Given Time
700. Search in a Binary Search Tree
590. N-ary Tree Postorder Traversal
589. N-ary Tree Preorder Traversal
1299. Replace Elements with Greatest Element on Right Side
1768. Merge Strings Alternately
561. Array Partition I
1374. Generate a String With Characters That Have Odd Counts
1822. Sign of the Product of an Array
1464. Maximum Product of Two Elements in an Array
1323. Maximum 69 Number
832. Flipping an Image
1295. Find Numbers with Even Number of Digits
1704. Determine if String Halves Are Alike
1732. Find the Highest Altitude
709. To Lower Case
1688. Count of Matches in Tournament
1684. Count the Number of Consistent Strings
1588. Sum of All Odd Length Subarrays
1662. Check If Two String Arrays are Equivalent