Submission #249606

# Submission time Handle Problem Language Result Execution time Memory
249606 2020-07-15T10:40:04 Z huuducpro Werewolf (IOI18_werewolf) C++14
100 / 100
775 ms 98276 KB
#include <bits/stdc++.h>
#include "werewolf.h"
using namespace std;
const int N=200002;
typedef vector <int> vi;
typedef pair <int,int> pp;
int m,q,n;
vector <pp> pl[N],pr[N];
vector <int> A[N],vl[N],vr[N];
int dsu[N],gl[N],gr[N],fl[N],fr[N],x[N],y[N],rx[N],ry[N];
int type,dem;
int bit[N];
vector <int> res;
int root(int u)
{
    while (dsu[u]>0) u=dsu[u];
    return u;
}
void unionn(int u,int v)
{
    if (dsu[u]>dsu[v]) swap(u,v);
    dsu[u]+=dsu[v];dsu[v]=u;
    if (type==0) vl[u].push_back(v);
    else vr[u].push_back(v);
   // cout<<type<<' '<<u<<' '<<v<<endl;
}
void dfsl(int u)
{
    x[u]=++dem;
    for (int v:vl[u])
    {
        if (x[v]>0) continue;
        dfsl(v);
    }
    rx[u]=dem;
}
void dfsr(int u)
{
    y[u]=++dem;
   // cout<<u<<endl;
    for (int v:vr[u])
    {
        if (y[v]>0) continue;
        dfsr(v);
    }
    ry[u]=dem;
}
void build()
{
    type=0;
    for (int i=1;i<=n;i++)
        dsu[i]=-1;
    for (int i=1;i<=n;i++)
    {
        for (int v:A[i])
        {
            if (v>i) continue;
            int a=root(i);int b=root(v);
            if (a!=b) unionn(a,b);
        }
        for (int j=0;j<pl[i].size();j++)
        {
            int u=pl[i][j].first;
            int pos=pl[i][j].second;
            u=root(u);
            fl[pos]=u;
            gl[pos]=vl[u].size();
        }
    }
    //for (int i=1;i<=n;i++)
       // for (int j=1;j<=n;j++) assert(root(i)==root(j));
    dem=0;dfsl(root(1));type=1;
    for (int i=1;i<=n;i++)
        dsu[i]=-1;
    for (int i=n;i>=1;i--)
    {
        for (int v:A[i])
        {
            if (v<i) continue;
            int a=root(i);int b=root(v);
            if (a!=b) unionn(a,b);
        }
        for (int j=0;j<pr[i].size();j++)
        {
            int u=pr[i][j].first;
            int pos=pr[i][j].second;
            u=root(u);
            fr[pos]=u;
            gr[pos]=vr[u].size();
        }
    }
    dem=0;dfsr(root(1));

}
void update(int i,int val)
{
    for (i;i<=n;i+=i&(-i))
        bit[i]+=val;
}
int get(int i)
{
    int ans=0;
    for (i;i>=1;i-=i&(-i))
        ans+=bit[i];
    return ans;
}
void solve_rec()
{
    vector <int> X[N];
    for (int i=1;i<=n;i++)
        X[x[i]].push_back(y[i]);
    vector <pair <pp,int> > Pl[N],Pr[N];
    res.resize(q,0);
    for (int i=0;i<q;i++)
    {
        int u=fl[i];
        //if (gl[i]==0||gr[i]==0) continue;
        int llx=x[u];int rrx=llx;if (gl[i]>0) rrx=rx[vl[u][gl[i]-1]];
        u=fr[i];
        int lly=y[u];int rry=lly;if (gr[i]>0) rry=ry[vr[u][gr[i]-1]];
        Pl[llx-1].push_back({{lly,rry},i});
        Pr[rrx].push_back({{lly,rry},i});

        //cout<<i<<' '<<fl[i]<<' '<<gl[i]<<' '<<llx<<' '<<rrx<<' '<<lly<<' '<<rry<<endl;
    }

   // for (int i=1;i<=n;i++)
    //    cout<<x[i]<<' '<<y[i]<<endl;
    for (int i=1;i<=n;i++)
    {
        for (int y:X[i]) update(y,1);
        for (int j=0;j<Pl[i].size();j++)
        {
            int a=Pl[i][j].first.first;int b=Pl[i][j].first.second;
            int pos=Pl[i][j].second;
            res[pos]-=get(b)-get(a-1);
        }
        for (int j=0;j<Pr[i].size();j++)
        {
            int a=Pr[i][j].first.first;int b=Pr[i][j].first.second;
            int pos=Pr[i][j].second;
            res[pos]+=get(b)-get(a-1);
        }
    }
    for (int i=0;i<q;i++)
        res[i]=(res[i]>0);
}
vector <int> check_validity(int nn,vi X,vi Y,vi S,vi E,vi L,vi R)
{
    n=nn;m=X.size();q=S.size();
    for (int i=0;i<m;i++)
    {
        X[i]++;Y[i]++;
        A[X[i]].push_back(Y[i]);
        A[Y[i]].push_back(X[i]);
    }
    for (int i=0;i<q;i++)
    {
        S[i]++;E[i]++;L[i]++;R[i]++;
        pl[R[i]].push_back({E[i],i});
        pr[L[i]].push_back({S[i],i});
    }
    build();
    solve_rec();
    return res;
}
/*
namespace {

int read_int() {
  int x;
  if (scanf("%d", &x) != 1) {
    fprintf(stderr, "Error while reading input\n");
    exit(1);
  }
  return x;
}

}  // namespace

int main()
{
    freopen("wereworf.inp","r",stdin);
    freopen("wereworf.out","w",stdout);
    int N = read_int();
  int M = read_int();
  int Q = read_int();
  std::vector<int> X(M), Y(M), S(Q), E(Q), L(Q), R(Q);
  for (int j = 0; j < M; ++j) {
    X[j] = read_int();
    Y[j] = read_int();
  }
  for (int i = 0; i < Q; ++i) {
    S[i] = read_int();
    E[i] = read_int();
    L[i] = read_int();
    R[i] = read_int();
  }


  vector<int> A = check_validity(N, X, Y, S, E, L, R);
  for (int i = 0; i < A.size(); ++i) {
    printf("%d\n", A[i]);
  }
  return 0;
}
*/

Compilation message

werewolf.cpp: In function 'void build()':
werewolf.cpp:61:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int j=0;j<pl[i].size();j++)
                      ~^~~~~~~~~~~~~
werewolf.cpp:83:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int j=0;j<pr[i].size();j++)
                      ~^~~~~~~~~~~~~
werewolf.cpp: In function 'void update(int, int)':
werewolf.cpp:97:11: warning: statement has no effect [-Wunused-value]
     for (i;i<=n;i+=i&(-i))
           ^
werewolf.cpp: In function 'int get(int)':
werewolf.cpp:103:11: warning: statement has no effect [-Wunused-value]
     for (i;i>=1;i-=i&(-i))
           ^
werewolf.cpp: In function 'void solve_rec()':
werewolf.cpp:132:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int j=0;j<Pl[i].size();j++)
                      ~^~~~~~~~~~~~~
werewolf.cpp:138:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int j=0;j<Pr[i].size();j++)
                      ~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 23 ms 38016 KB Output is correct
2 Correct 32 ms 38016 KB Output is correct
3 Correct 22 ms 37900 KB Output is correct
4 Correct 22 ms 37880 KB Output is correct
5 Correct 22 ms 38016 KB Output is correct
6 Correct 21 ms 37924 KB Output is correct
7 Correct 45 ms 38008 KB Output is correct
8 Correct 20 ms 38016 KB Output is correct
9 Correct 36 ms 38008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 38016 KB Output is correct
2 Correct 32 ms 38016 KB Output is correct
3 Correct 22 ms 37900 KB Output is correct
4 Correct 22 ms 37880 KB Output is correct
5 Correct 22 ms 38016 KB Output is correct
6 Correct 21 ms 37924 KB Output is correct
7 Correct 45 ms 38008 KB Output is correct
8 Correct 20 ms 38016 KB Output is correct
9 Correct 36 ms 38008 KB Output is correct
10 Correct 27 ms 38776 KB Output is correct
11 Correct 45 ms 38776 KB Output is correct
12 Correct 27 ms 38912 KB Output is correct
13 Correct 43 ms 38776 KB Output is correct
14 Correct 26 ms 38784 KB Output is correct
15 Correct 46 ms 38904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 775 ms 90872 KB Output is correct
2 Correct 533 ms 94308 KB Output is correct
3 Correct 520 ms 94244 KB Output is correct
4 Correct 573 ms 94412 KB Output is correct
5 Correct 618 ms 94628 KB Output is correct
6 Correct 657 ms 95364 KB Output is correct
7 Correct 601 ms 92264 KB Output is correct
8 Correct 552 ms 94344 KB Output is correct
9 Correct 516 ms 93400 KB Output is correct
10 Correct 572 ms 92872 KB Output is correct
11 Correct 510 ms 94132 KB Output is correct
12 Correct 595 ms 94636 KB Output is correct
13 Correct 550 ms 92468 KB Output is correct
14 Correct 556 ms 92580 KB Output is correct
15 Correct 540 ms 92180 KB Output is correct
16 Correct 563 ms 92380 KB Output is correct
17 Correct 600 ms 92396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 38016 KB Output is correct
2 Correct 32 ms 38016 KB Output is correct
3 Correct 22 ms 37900 KB Output is correct
4 Correct 22 ms 37880 KB Output is correct
5 Correct 22 ms 38016 KB Output is correct
6 Correct 21 ms 37924 KB Output is correct
7 Correct 45 ms 38008 KB Output is correct
8 Correct 20 ms 38016 KB Output is correct
9 Correct 36 ms 38008 KB Output is correct
10 Correct 27 ms 38776 KB Output is correct
11 Correct 45 ms 38776 KB Output is correct
12 Correct 27 ms 38912 KB Output is correct
13 Correct 43 ms 38776 KB Output is correct
14 Correct 26 ms 38784 KB Output is correct
15 Correct 46 ms 38904 KB Output is correct
16 Correct 775 ms 90872 KB Output is correct
17 Correct 533 ms 94308 KB Output is correct
18 Correct 520 ms 94244 KB Output is correct
19 Correct 573 ms 94412 KB Output is correct
20 Correct 618 ms 94628 KB Output is correct
21 Correct 657 ms 95364 KB Output is correct
22 Correct 601 ms 92264 KB Output is correct
23 Correct 552 ms 94344 KB Output is correct
24 Correct 516 ms 93400 KB Output is correct
25 Correct 572 ms 92872 KB Output is correct
26 Correct 510 ms 94132 KB Output is correct
27 Correct 595 ms 94636 KB Output is correct
28 Correct 550 ms 92468 KB Output is correct
29 Correct 556 ms 92580 KB Output is correct
30 Correct 540 ms 92180 KB Output is correct
31 Correct 563 ms 92380 KB Output is correct
32 Correct 600 ms 92396 KB Output is correct
33 Correct 669 ms 95932 KB Output is correct
34 Correct 520 ms 82348 KB Output is correct
35 Correct 651 ms 95848 KB Output is correct
36 Correct 738 ms 95920 KB Output is correct
37 Correct 668 ms 95768 KB Output is correct
38 Correct 773 ms 96032 KB Output is correct
39 Correct 607 ms 94088 KB Output is correct
40 Correct 713 ms 98276 KB Output is correct
41 Correct 641 ms 94484 KB Output is correct
42 Correct 575 ms 93552 KB Output is correct
43 Correct 703 ms 97384 KB Output is correct
44 Correct 644 ms 95080 KB Output is correct
45 Correct 600 ms 93540 KB Output is correct
46 Correct 582 ms 93444 KB Output is correct
47 Correct 617 ms 92636 KB Output is correct
48 Correct 621 ms 92380 KB Output is correct
49 Correct 578 ms 92676 KB Output is correct
50 Correct 571 ms 92508 KB Output is correct
51 Correct 699 ms 97368 KB Output is correct
52 Correct 656 ms 97372 KB Output is correct