Submission #284366

# Submission time Handle Problem Language Result Execution time Memory
284366 2020-08-27T09:32:46 Z MKopchev Werewolf (IOI18_werewolf) C++14
100 / 100
807 ms 84324 KB
#include "werewolf.h"
#include<bits/stdc++.h>
using namespace std;

const int nmax=4e5+42;

struct edge
{
    int u,v,cost;
};
edge all[nmax];

bool cmp_1(edge a,edge b)
{
    return a.cost<b.cost;
}

bool cmp_2(edge a,edge b)
{
    return a.cost>b.cost;
}

int parent[nmax];

int root(int node)
{
    if(parent[node]==node)return node;
    parent[node]=root(parent[node]);
    return parent[node];
}

vector<int> seen[nmax];

int le[nmax],ri[nmax];

void my_merge(int u,int v)
{
    u=root(u);
    v=root(v);

    if(u==v)return;

    //cout<<"my_merge "<<u<<" "<<v<<endl;

    if(seen[u].size()<seen[v].size())swap(u,v);

    for(auto k:seen[v])
        seen[u].push_back(k);

    le[u]=min(le[u],le[v]);
    ri[u]=max(ri[u],ri[v]);

    parent[v]=u;
}

int order_mini[nmax],order_maxi[nmax];

vector< pair<int,int> > queries[nmax];

pair<int,int> mem_mini[nmax],mem_maxi[nmax];


int where[nmax];

pair<int,int> points[nmax];

struct rect
{
    int x1,y1,x2,y2,id;
};
rect help[nmax];

bool cmp_rect(rect a,rect b)
{
    return a.x2<b.x2;
}

int tree[4*nmax];

void update(int node,int l,int r,int pos,int val)
{
    tree[node]=max(tree[node],val);

    if(l==r)return;

    int av=(l+r)/2;

    if(pos<=av)update(node*2,l,av,pos,val);
    else update(node*2+1,av+1,r,pos,val);
}

int query(int node,int l,int r,int lq,int rq)
{
    if(l==lq&&r==rq)return tree[node];

    int av=(l+r)/2,ret=-1;

    if(lq<=av)ret=max(ret,query(node*2,l,av,lq,min(av,rq)));
    if(av<rq)ret=max(ret,query(node*2+1,av+1,r,max(av+1,lq),rq));

    return ret;
}
std::vector<int> check_validity(int N, std::vector<int> X, std::vector<int> Y,
                                std::vector<int> S, std::vector<int> E,
                                std::vector<int> L, std::vector<int> R)
{
        int M=X.size();
    //min tree

    for(int i=0;i<M;i++)
    {
        all[i].u=X[i];
        all[i].v=Y[i];
        all[i].cost=max(X[i],Y[i]);
    }

    for(int i=0;i<N;i++)parent[i]=i;

    sort(all,all+M,cmp_1);

    for(int i=0;i<N;i++)seen[i]={i};

    for(int i=0;i<M;i++)
    {
        my_merge(all[i].u,all[i].v);
        /*
        for(int j=0;j<N;j++)
        {
            cout<<j<<" -> "<<root(j)<<" : ";for(auto k:seen[j])cout<<k<<" ";cout<<endl;
        }
        */
    }


    for(int i=0;i<N;i++)
    {
        order_mini[i]=seen[root(0)][i];
        le[order_mini[i]]=i;
        ri[order_mini[i]]=i;

        //cout<<i<<" -> "<<order_mini[i]<<endl;
    }

    for(int i=0;i<L.size();i++)
        queries[R[i]].push_back({E[i],i});

    for(int i=0;i<N;i++)parent[i]=i;

    for(int i=0;i<N;i++)seen[i]={};

    int pointer=0;
    for(int t=0;t<N;t++)
    {
        while(pointer<M&&all[pointer].cost==t)
        {
            my_merge(all[pointer].u,all[pointer].v);
            pointer++;
        }

        for(auto k:queries[t])
        {
            mem_mini[k.second]={le[root(k.first)],ri[root(k.first)]};
        }
    }

    //max tree
    for(int i=0;i<M;i++)
    {
        all[i].u=X[i];
        all[i].v=Y[i];
        all[i].cost=min(X[i],Y[i]);
    }

    for(int i=0;i<N;i++)parent[i]=i;

    sort(all,all+M,cmp_2);

    for(int i=0;i<N;i++)seen[i]={i};

    for(int i=0;i<M;i++)
        my_merge(all[i].u,all[i].v);

    for(int i=0;i<N;i++)
    {
        order_maxi[i]=seen[root(0)][i];
        le[order_maxi[i]]=i;
        ri[order_maxi[i]]=i;

        //cout<<i<<" -> "<<order_maxi[i]<<endl;
    }

    for(int i=0;i<N;i++)
        queries[i]={};

    for(int i=0;i<R.size();i++)
        queries[L[i]].push_back({S[i],i});

    for(int i=0;i<N;i++)parent[i]=i;

    for(int i=0;i<N;i++)seen[i]={};

    pointer=0;

    for(int t=N-1;t>=0;t--)
    {
        while(pointer<M&&all[pointer].cost==t)
        {
            my_merge(all[pointer].u,all[pointer].v);
            pointer++;
        }

        for(auto k:queries[t])
        {
            mem_maxi[k.second]={le[root(k.first)],ri[root(k.first)]};
        }
    }

    for(int i=0;i<N;i++)
    {
        where[order_mini[i]]=i;
    }

    for(int i=0;i<N;i++)
    {
        points[i]={where[order_maxi[i]],i};

        //cout<<points[i].first<<" "<<points[i].second<<endl;
    }

    sort(points,points+N);

    for(int i=0;i<L.size();i++)
    {
        help[i].x1=mem_mini[i].first;
        help[i].x2=mem_mini[i].second;

        help[i].y1=mem_maxi[i].first;
        help[i].y2=mem_maxi[i].second;

        //cout<<i<<" -> "<<help[i].x1<<" "<<help[i].y1<<" "<<help[i].x2<<" "<<help[i].y2<<endl;

        help[i].id=i;
    }

    sort(help,help+L.size(),cmp_rect);

    memset(tree,-1,sizeof(tree));

    vector<int> ret(L.size(),0);

    int pointer_points=0,pointer_queries=0;

    for(int x=0;x<N;x++)
    {

        while(pointer_points<N&&points[pointer_points].first==x)
        {
            update(1,0,N-1,points[pointer_points].second,points[pointer_points].first);

            //cout<<"update "<<points[pointer_points].second<<" with "<<points[pointer_points].first<<endl;

            pointer_points++;
        }

        //for(int j=0;j<N;j++)cout<<j<<" -> "<<query(1,0,N-1,j,j)<<endl;

        while(pointer_queries<L.size()&&help[pointer_queries].x2==x)
        {
            ret[help[pointer_queries].id]=(query(1,0,N-1,help[pointer_queries].y1,help[pointer_queries].y2)>=help[pointer_queries].x1);

            //cout<<query(1,0,N-1,help[pointer_queries].y1,help[pointer_queries].y2)<<" wanted "<<help[pointer_queries].x1<<endl;

            pointer_queries++;
        }

        //cout<<"---"<<endl;
    }

    return ret;
}

Compilation message

werewolf.cpp: In function 'std::vector<int> check_validity(int, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
werewolf.cpp:144:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  144 |     for(int i=0;i<L.size();i++)
      |                 ~^~~~~~~~~
werewolf.cpp:195:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  195 |     for(int i=0;i<R.size();i++)
      |                 ~^~~~~~~~~
werewolf.cpp:232:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  232 |     for(int i=0;i<L.size();i++)
      |                 ~^~~~~~~~~
werewolf.cpp:267:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  267 |         while(pointer_queries<L.size()&&help[pointer_queries].x2==x)
      |               ~~~~~~~~~~~~~~~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 16 ms 25472 KB Output is correct
2 Correct 16 ms 25472 KB Output is correct
3 Correct 16 ms 25472 KB Output is correct
4 Correct 16 ms 25472 KB Output is correct
5 Correct 16 ms 25472 KB Output is correct
6 Correct 16 ms 25472 KB Output is correct
7 Correct 16 ms 25472 KB Output is correct
8 Correct 16 ms 25472 KB Output is correct
9 Correct 16 ms 25472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 25472 KB Output is correct
2 Correct 16 ms 25472 KB Output is correct
3 Correct 16 ms 25472 KB Output is correct
4 Correct 16 ms 25472 KB Output is correct
5 Correct 16 ms 25472 KB Output is correct
6 Correct 16 ms 25472 KB Output is correct
7 Correct 16 ms 25472 KB Output is correct
8 Correct 16 ms 25472 KB Output is correct
9 Correct 16 ms 25472 KB Output is correct
10 Correct 22 ms 26240 KB Output is correct
11 Correct 22 ms 26240 KB Output is correct
12 Correct 22 ms 26236 KB Output is correct
13 Correct 28 ms 26240 KB Output is correct
14 Correct 23 ms 26240 KB Output is correct
15 Correct 24 ms 26368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 679 ms 75240 KB Output is correct
2 Correct 565 ms 75244 KB Output is correct
3 Correct 591 ms 77292 KB Output is correct
4 Correct 610 ms 79824 KB Output is correct
5 Correct 604 ms 80240 KB Output is correct
6 Correct 679 ms 81516 KB Output is correct
7 Correct 629 ms 84324 KB Output is correct
8 Correct 546 ms 75244 KB Output is correct
9 Correct 527 ms 77460 KB Output is correct
10 Correct 558 ms 79468 KB Output is correct
11 Correct 570 ms 79980 KB Output is correct
12 Correct 655 ms 81464 KB Output is correct
13 Correct 619 ms 74976 KB Output is correct
14 Correct 577 ms 74964 KB Output is correct
15 Correct 576 ms 75088 KB Output is correct
16 Correct 582 ms 75136 KB Output is correct
17 Correct 611 ms 83608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 25472 KB Output is correct
2 Correct 16 ms 25472 KB Output is correct
3 Correct 16 ms 25472 KB Output is correct
4 Correct 16 ms 25472 KB Output is correct
5 Correct 16 ms 25472 KB Output is correct
6 Correct 16 ms 25472 KB Output is correct
7 Correct 16 ms 25472 KB Output is correct
8 Correct 16 ms 25472 KB Output is correct
9 Correct 16 ms 25472 KB Output is correct
10 Correct 22 ms 26240 KB Output is correct
11 Correct 22 ms 26240 KB Output is correct
12 Correct 22 ms 26236 KB Output is correct
13 Correct 28 ms 26240 KB Output is correct
14 Correct 23 ms 26240 KB Output is correct
15 Correct 24 ms 26368 KB Output is correct
16 Correct 679 ms 75240 KB Output is correct
17 Correct 565 ms 75244 KB Output is correct
18 Correct 591 ms 77292 KB Output is correct
19 Correct 610 ms 79824 KB Output is correct
20 Correct 604 ms 80240 KB Output is correct
21 Correct 679 ms 81516 KB Output is correct
22 Correct 629 ms 84324 KB Output is correct
23 Correct 546 ms 75244 KB Output is correct
24 Correct 527 ms 77460 KB Output is correct
25 Correct 558 ms 79468 KB Output is correct
26 Correct 570 ms 79980 KB Output is correct
27 Correct 655 ms 81464 KB Output is correct
28 Correct 619 ms 74976 KB Output is correct
29 Correct 577 ms 74964 KB Output is correct
30 Correct 576 ms 75088 KB Output is correct
31 Correct 582 ms 75136 KB Output is correct
32 Correct 611 ms 83608 KB Output is correct
33 Correct 684 ms 77296 KB Output is correct
34 Correct 472 ms 66424 KB Output is correct
35 Correct 688 ms 76248 KB Output is correct
36 Correct 729 ms 78156 KB Output is correct
37 Correct 688 ms 75884 KB Output is correct
38 Correct 727 ms 77676 KB Output is correct
39 Correct 699 ms 76652 KB Output is correct
40 Correct 732 ms 83540 KB Output is correct
41 Correct 683 ms 75884 KB Output is correct
42 Correct 666 ms 78188 KB Output is correct
43 Correct 807 ms 79340 KB Output is correct
44 Correct 706 ms 76012 KB Output is correct
45 Correct 620 ms 76268 KB Output is correct
46 Correct 612 ms 77420 KB Output is correct
47 Correct 600 ms 75252 KB Output is correct
48 Correct 618 ms 74976 KB Output is correct
49 Correct 589 ms 75284 KB Output is correct
50 Correct 589 ms 75316 KB Output is correct
51 Correct 724 ms 82252 KB Output is correct
52 Correct 721 ms 82388 KB Output is correct