Submission #847789

# Submission time Handle Problem Language Result Execution time Memory
847789 2023-09-10T12:19:42 Z HoriaHaivas RMQ (NOI17_rmq) C++14
100 / 100
67 ms 11208 KB
/*
    "TLE is like the wind, always by my side"
    - Yasuo - 2022 -
*/
#include<bits/stdc++.h>
#define debug(x) cerr << #x << " " << x << "\n"
#define debugs(x) cerr << #x << " " << x << " "
#pragma GCC optimize("Ofast")

using namespace std;

struct qq
{
    int l;
    int r;
    int x;
};

struct ptval
{
    int nr;
    int poz;
};

ptval val[100001];
const int inf=INT_MAX;
qq query[100001];
qq query2[100001];
qq aux[100001];
qq aux2[100001];
int st[100001];
int finl[100001];
bool placed[100001];
bool used[100001];
int lazy[400001];
int aint[400001];

bool cmp (qq a, qq b)
{
    if (a.x!=b.x)
        return a.x<b.x;
    else if (a.l!=b.l)
        return a.l<b.l;
    else
        return a.r<b.r;
}

bool cmp2(ptval a, ptval b)
{
    if (a.nr!=b.nr)
        return a.nr>b.nr;
    else
        return a.poz<b.poz;
}

bool cmp3 (qq a, qq b)
{
    if (a.l!=b.l)
        return a.l<b.l;
    else if (a.r!=b.r)
        return a.r<b.r;
    else
        return a.x<b.x;
}

void propagate(int parent, int child)
{
    lazy[child]=lazy[parent];
}

void lazy_update (int l, int r, int val, int qa, int qb, int x)
{
    if (lazy[val]!=-1)
    {
        if (r!=l)
        {
            propagate(val,val*2);
            propagate(val,val*2+1);
        }
        lazy[val]=-1;
    }
    if (l>qb || r<qa)
        return;
    if (qa<=l && r<=qb)
    {
        lazy[val]=x;
        return;
    }
    int mid;
    mid=(l+r)/2;
    if (qa<=mid)
        lazy_update(l,mid,val*2,qa,qb,x);
    if (qb>mid)
        lazy_update(mid+1,r,val*2+1,qa,qb,x);
}

int qry (int l, int r, int val, int qa, int qb)
{
    if (qa<=l && r<=qb)
    {
        return lazy[val];
    }
    if (lazy[val]!=-1)
    {
        if (r!=l)
        {
            propagate(val,val*2);
            propagate(val,val*2+1);
        }
        lazy[val]=-1;
    }
    if (r<qa || l>qb)
        return inf;
    int mid,ans;
    mid=(l+r)/2;
    ans=inf;
    if (qa<=mid)
        ans=min(ans,qry(l,mid,val*2,qa,qb));
    if (qb>mid)
        ans=min(ans,qry(mid+1,r,val*2+1,qa,qb));
    return ans;
}

void build(int l, int r, int val)
{
    if (l==r)
    {
        aint[val]=finl[l];
        return;
    }
    int mid;
    mid=(l+r)/2;
    build(l,mid,val*2);
    build(mid+1,r,val*2+1);
    aint[val]=min(aint[val*2],aint[val*2+1]);
}

int que(int l, int r, int val, int qa, int qb)
{
    if (r<qa || l>qb)
        return inf;
    if (qa<=l && r<=qb)
        return aint[val];
    int mid,ans;
    mid=(l+r)/2;
    ans=inf;
    if (qa<=mid)
        ans=min(ans,que(l,mid,val*2,qa,qb));
    if (qb>mid)
        ans=min(ans,que(mid+1,r,val*2+1,qa,qb));
    return ans;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie();
    cout.tie();
    int n,q,i,j,t,q2,big,dif,y,res,cnt;
    bool ok;
    ok=true;
    cin >> n >> q;
    y=q;
    for (i=1; i<=q; i++)
    {
        cin >> query[i].l >> query[i].r >> query[i].x;
        aux2[i]=query[i];
        query2[i]=query[i];
    }
    sort(query+1,query+1+q,cmp);
    sort(query2+1,query2+1+q,cmp);
    t=0;
    for (j=1; j<=q; j++)
    {
        if (t==0 || query[j].x!=query[st[t]].x || (query[j].r<query[st[t]].l || query[j].l>query[st[t]].r))
        {
            t++;
            st[t]=j;
        }
        else
        {
            query[st[t]].l=max(query[st[t]].l,query[j].l);
            query[st[t]].r=min(query[st[t]].r,query[j].r);
        }
    }
    q2=q;
    q=t;
    while (t>0)
    {
        aux[t]=query[st[t]];
        t--;
    }
    for (j=1; j<=q; j++)
    {
        query[j]=aux[j];
        if (query[j].l==query[j-1].l && query[j].r==query[j-1].r && query[j].x!=query[j-1].x)
            ok=false;
    }
    t=0;
    for (j=1; j<=q2; j++)
    {
        ///cout << query2[j].l << " " << query2[j].r << " " << query2[j].x << "\n";
        if (t==0 || query2[j].x!=query2[st[t]].x || (query2[j].r<query2[st[t]].l || query2[j].l>query2[st[t]].r))
        {
            t++;
            st[t]=j;
        }
        else
        {
            query2[st[t]].l=min(query2[st[t]].l,query2[j].l);
            query2[st[t]].r=max(query2[st[t]].r,query2[j].r);
        }
    }
    q2=t;
    while (t>0)
    {
        aux[t]=query2[st[t]];
        t--;
    }
    for (j=1; j<=q2; j++)
    {
        query2[j]=aux[j];
        if (query2[j].l==query2[j-1].l && query2[j].r==query2[j-1].r && query2[j].x!=query2[j-1].x)
            ok=false;
    }
    /*
    build(1,n,1);
    for (j=1; j<=y; j++)
    {
        res=que(1,n,1,aux2[j].l+1,aux2[j].r+1);
        if (res!=aux2[j].x)
        {
            debug(aux2[j].l+1);
            debug(aux2[j].r+1);
            debug(aux2[j].x);
            debug(res);
        }
    }
    */
    for (j=0; j<=4*n; j++)
    {
        lazy[j]=-1;
    }
    for (j=1; j<=q2; j++)
    {
        lazy_update(1,n,1,query2[j].l+1,query2[j].r+1,query2[j].x);
    }
    for (j=1; j<=n; j++)
    {
        val[j].nr=qry(1,n,1,j,j);
        val[j].poz=j;
    }
    sort(val+1,val+1+n,cmp2);
    dif=0;
    cnt=q;
    for (j=1; j<=n; j++)
    {
        if (val[j].nr!=-1 && !placed[val[j].nr] && !used[val[j].poz] && query[cnt].l+1<=val[j].poz && query[cnt].r+1>=val[j].poz)
        {
            dif++;
            cnt--;
            placed[val[j].nr]=1;
            used[val[j].poz]=1;
            finl[val[j].poz]=val[j].nr;
        }
    }
    if (dif!=q)
        ok=false;
    big=n-1;
    for (j=1; j<=n; j++)
    {
        if (!used[val[j].poz])
        {
            while (big>=0 && placed[big])
            {
                big--;
            }
            if (big<val[j].nr)
            {
                ok=false;
                break;
            }
            placed[big]=1;
            used[val[j].poz]=1;
            finl[val[j].poz]=big;
        }
    }
    if (ok==false)
    {
        for (j=1; j<=n; j++)
        {
            cout << -1 << " ";
        }
    }
    else
    {
        for (j=1;j<=n;j++)
        {
             cout << finl[j] << " ";
        }
    }
    return 0;
}

Compilation message

rmq.cpp: In function 'int main()':
rmq.cpp:159:30: warning: variable 'y' set but not used [-Wunused-but-set-variable]
  159 |     int n,q,i,j,t,q2,big,dif,y,res,cnt;
      |                              ^
rmq.cpp:159:32: warning: unused variable 'res' [-Wunused-variable]
  159 |     int n,q,i,j,t,q2,big,dif,y,res,cnt;
      |                                ^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8656 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 1 ms 8792 KB Output is correct
5 Correct 1 ms 8540 KB Output is correct
6 Correct 1 ms 8540 KB Output is correct
7 Correct 1 ms 8540 KB Output is correct
8 Correct 1 ms 8540 KB Output is correct
9 Correct 1 ms 8540 KB Output is correct
10 Correct 1 ms 8540 KB Output is correct
11 Correct 1 ms 8536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8656 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 1 ms 8792 KB Output is correct
5 Correct 1 ms 8540 KB Output is correct
6 Correct 1 ms 8540 KB Output is correct
7 Correct 1 ms 8540 KB Output is correct
8 Correct 1 ms 8540 KB Output is correct
9 Correct 1 ms 8540 KB Output is correct
10 Correct 1 ms 8540 KB Output is correct
11 Correct 1 ms 8536 KB Output is correct
12 Correct 1 ms 8540 KB Output is correct
13 Correct 2 ms 8536 KB Output is correct
14 Correct 2 ms 8540 KB Output is correct
15 Correct 1 ms 8540 KB Output is correct
16 Correct 2 ms 8540 KB Output is correct
17 Correct 2 ms 8540 KB Output is correct
18 Correct 2 ms 8540 KB Output is correct
19 Correct 1 ms 8540 KB Output is correct
20 Correct 1 ms 8540 KB Output is correct
21 Correct 2 ms 8660 KB Output is correct
22 Correct 1 ms 8664 KB Output is correct
23 Correct 1 ms 8536 KB Output is correct
24 Correct 2 ms 8792 KB Output is correct
25 Correct 1 ms 8540 KB Output is correct
26 Correct 1 ms 8540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8656 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 1 ms 8792 KB Output is correct
5 Correct 1 ms 8540 KB Output is correct
6 Correct 1 ms 8540 KB Output is correct
7 Correct 1 ms 8540 KB Output is correct
8 Correct 1 ms 8540 KB Output is correct
9 Correct 1 ms 8540 KB Output is correct
10 Correct 1 ms 8540 KB Output is correct
11 Correct 1 ms 8536 KB Output is correct
12 Correct 1 ms 8540 KB Output is correct
13 Correct 2 ms 8536 KB Output is correct
14 Correct 2 ms 8540 KB Output is correct
15 Correct 1 ms 8540 KB Output is correct
16 Correct 2 ms 8540 KB Output is correct
17 Correct 2 ms 8540 KB Output is correct
18 Correct 2 ms 8540 KB Output is correct
19 Correct 1 ms 8540 KB Output is correct
20 Correct 1 ms 8540 KB Output is correct
21 Correct 2 ms 8660 KB Output is correct
22 Correct 1 ms 8664 KB Output is correct
23 Correct 1 ms 8536 KB Output is correct
24 Correct 2 ms 8792 KB Output is correct
25 Correct 1 ms 8540 KB Output is correct
26 Correct 1 ms 8540 KB Output is correct
27 Correct 54 ms 11208 KB Output is correct
28 Correct 49 ms 10936 KB Output is correct
29 Correct 40 ms 10320 KB Output is correct
30 Correct 55 ms 11092 KB Output is correct
31 Correct 43 ms 10552 KB Output is correct
32 Correct 54 ms 11012 KB Output is correct
33 Correct 18 ms 9560 KB Output is correct
34 Correct 13 ms 9304 KB Output is correct
35 Correct 24 ms 9820 KB Output is correct
36 Correct 57 ms 10316 KB Output is correct
37 Correct 67 ms 11088 KB Output is correct
38 Correct 15 ms 9304 KB Output is correct
39 Correct 47 ms 10576 KB Output is correct
40 Correct 1 ms 8540 KB Output is correct
41 Correct 1 ms 8792 KB Output is correct