Submission #1139185

#TimeUsernameProblemLanguageResultExecution timeMemory
1139185agrim_09RMQ (NOI17_rmq)C++20
67 / 100
1097 ms12020 KiB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define endl '\n';
#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define py cout << "YES" << endl;
#define pn cout << "NO" << endl;
#define pb push_back
#define int long long
typedef long double lld;
#define double lld
typedef long long ll;
typedef unsigned long long ull;
const ll inf = 1e18;
const ll mod = 1e9+7;
const int N = 2e5;
vector<int>primes = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97};

// Check for queue, priorioty_queue, stack, ordered_set solutions
// stack => LIFO (whatever goes in last comes out last)
// queue => FIFO (whatever goes in first comes out first)
// priority_queue => Dynamic queries of minimum/maximum
// ordered_set => set in vector form
//[order_of_key(k) gives number of elements less than k, while find_by_order(i) gives i^th element]

// To comment multiple lines : ctrl + /
// To find and replace : ctrl+H

void judge(){
    srand(time(NULL));
    #ifndef ONLINE_JUDGE
    freopen("1.txt","r",stdin);
    freopen("2.txt","w",stdout);
    freopen("Error.txt", "w", stderr);
    #define debug(x...) cerr << #x <<" = "; _print(x); cerr << endl;
    #else
    #define debug(x...)
    #endif
}

void usaco(string s) {
    freopen((s + ".in").c_str(),"r",stdin);
    freopen((s + ".out").c_str(),"w",stdout);
}

struct node {
    int sum;  // The sum of the range (used for sum queries)
    node(int x = 0){
        sum = x;
    }
};

void bad(int n){
    for(int i = 0;i<n-1;i++){
        cout << -1 << ' ';
    }
    cout << -1;
}

class SegTree{
    int n; vector<node>t; vector<int>a;

    node unite(node a, node b){ // Make changes here    
        return node(min(a.sum,b.sum));
    }

    private:
    #define mid (start+end)/2
    #define lc x+1
    #define rc 2*(mid-start+1) + x
        void build(int x, int start, int end){ // Make changes here
            if(start==end){
                t[x] = node(a[start]);
                return;
            }
            build(lc,start,mid);
            build(rc,mid+1,end);
            t[x] = unite(t[lc],t[rc]);
            return;
        }

        void update(int x, int start, int end, int idx, int val){
            if(start==end){
                t[x] = node(val); return;
            }
            if(idx<=mid){
                update(lc,start,mid,idx,val);
            }
            else{
                update(rc,mid+1,end,idx,val);
            }
            t[x] = unite(t[lc],t[rc]); return;
        }

        node query(int x, int start, int end, int l, int r){
            if(l<=start and end<=r){
                return t[x];
            }
            if(r<=mid){
                return query(lc,start,mid,l,r);
            }
            else if(mid<l){
                return query(rc,mid+1,end,l,r);
            }
            else{
                return unite(query(lc,start,mid,l,r),query(rc,mid+1,end,l,r));
            }
        }  
    
    public:
        SegTree(vector<int>& tmp){
            n = tmp.size();
            t.resize(2 * n);
            a = tmp;
            build(0, 0, n - 1);
        }

        node query(int l, int r){
            return query(0, 0, n - 1, l, r);
        }

        void update(int pos, int val){ // Make changes here
            update(0, 0, n - 1, pos, val);
        }
};


signed main(){
    fastio; //judge();
    int n,q; cin >> n >> q;
    vector<pair<int,int>>a(n,{-1,inf});
    vector<vector<pair<int,int>>>all(n);
    set<int>fs; set<int>others; set<int>fs2;
    vector<bool>vis(n,false);
    while(q--){
        int l,r,x; cin >> l >> r >> x;
        all[x].pb({l,r});
        a[x].first = max(a[x].first,l);
        a[x].second = min(a[x].second,r);
        vis[x] = true;
    }
    for(int i = 0;i<n;i++){
        if(vis[i]){
            fs.insert(i);
        }
        else{
            others.insert(i);
        }
    }
    // Put all the numbers in their "smaller" bound limits
    vector<int>tmp(n,-1);
    vector<bool>remove(n,false);
    vector<vector<int>>add(n);
    vector<vector<int>>sub(n);
    for(int i = 0;i<n;i++){
        int curr_l = -1, curr_r = -1;
        for(auto [l,r]:all[i]){
            if(curr_l==-1){
                curr_l = l, curr_r = r;
            }
            else{
                if(curr_r+1<l or r+1<curr_l){
                    bad(n); return 0;
                }
                curr_l = min(curr_l,l);
                curr_r = max(curr_r,r);
            }
        }
        if(curr_l==-1 and curr_r==-1){
            continue;
        }
        add[curr_l].pb(i);
        if(curr_r+1<n){
            sub[curr_r+1].pb(i);
        }
    }
    priority_queue<int>pq;
    pq.push(-1);    
    for(int i = 0;i<n;i++){
        //tmp[i] = seg.query(i,i).sum;
        for(auto t : sub[i]){
            remove[t] = true;
        }
        for(auto t : add[i]){
            pq.push(t);
        }
        while(true){
            int x = pq.top();
            if(x==-1){
                break;
            }
            if(remove[x]){
                pq.pop();
            }
            else{
                break;
            }
        }
        tmp[i] = pq.top();
    }
    vector<int>ans(n,-1);
    for(int i = 0;i<n;i++){
        if(tmp[i]==-1){
            continue;
        }
        if(fs.count(tmp[i]) and a[tmp[i]].first<=i and i<=a[tmp[i]].second){
            ans[i] = tmp[i];
            fs.erase(fs.find(tmp[i]));
        }
    }
    if(fs.size()!=0){
        bad(n); return 0;
    }
    // // Put the other numbers
    // If there are restrictions
    for(int i = 0;i<n;i++){
        if(tmp[i]==-1 or ans[i]!=-1){
            continue;
        }
        auto it = lower_bound(others.begin(),others.end(),tmp[i]);
        if(n<=1000){
            if(it==others.end()){
                bad(n); return 0;
            }
            ans[i] = *it;
            others.erase(it);
        }
    }
    if(n<=1000){

    // // If no restricitons left
    for(int i = 0;i<n;i++){
        if(ans[i]==-1){
            if(others.size()==0){
                bad(n); return 0;
            }
            auto it = others.begin();
            ans[i] = *it;
            others.erase(it);
        }
    }   
    SegTree seg3(ans);
    for(int i = 0;i<n;i++){
        for(auto [l,r]:all[i]){
            if(seg3.query(l,r).sum!=i){
                bad(n); return 0;
            }
        }
    }
    for(auto x : ans) cout << x << ' ';
    }
}

Compilation message (stderr)

rmq.cpp: In function 'void judge()':
rmq.cpp:37:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |     freopen("1.txt","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~
rmq.cpp:38:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   38 |     freopen("2.txt","w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~
rmq.cpp:39:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   39 |     freopen("Error.txt", "w", stderr);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
rmq.cpp: In function 'void usaco(std::string)':
rmq.cpp:47:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   47 |     freopen((s + ".in").c_str(),"r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
rmq.cpp:48:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   48 |     freopen((s + ".out").c_str(),"w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...