Submission #984366

# Submission time Handle Problem Language Result Execution time Memory
984366 2024-05-16T14:57:29 Z bachhoangxuan Hexagonal Territory (APIO21_hexagon) C++17
66 / 100
2000 ms 461076 KB
#include "hexagon.h"
#include <bits/stdc++.h>
using namespace std;
using i32 = int;
#define int long long
#define pii pair<int,int>
#define fi first
#define se second
const int inf = 1e9;
const int mod = 1e9+7;
const int inv2 = (mod+1)/2;
const int inv3 = (mod+1)/3;
int dx[]={0,0,1,1,0,-1,-1},
    dy[]={0,1,1,0,-1,-1,0};

i32 draw_territory(i32 N, i32 A, i32 B,
                   std::vector<i32> D, std::vector<i32> L) {
    int X=0,Y=0;
    int a=0,b=0;
    for(int i=0;i<N;i++){
        b+=L[i];
        int nX=X+L[i]*dx[D[i]];
        int nY=Y+L[i]*dy[D[i]];
        a+=X*nY-Y*nX;
        X=nX;Y=nY;
    }
    a=(abs(a)+b)%mod;
    a=a*inv2%mod;
    a=(a+1)%mod;

    int T=0;
    if(B){
        if(N==3){
            int S=L[0];
            int d1=(S*(S+1)/2)%mod;
            int d2=d1*(2*S+1)%mod*inv3%mod;
            T=(d1+d2)%mod;
        }
        else{
            X=0,Y=0;
            vector<pii> p;
            set<pii> ss;
            for(int i=0;i<N;i++){
                for(int j=0;j<L[i];j++){
                    X+=dx[D[i]];
                    Y+=dy[D[i]];
                    p.push_back({X,Y});
                    for(int t=1;t<=6;t++) ss.insert({X+dx[t],Y+dy[t]});
                }
            }
            for(pii x:p) ss.erase(x);

            set<pii> outers;
            queue<pii> q;
            q.push(*ss.begin());
            outers.insert(*ss.begin());

            while(!q.empty()){
                auto [x,y]=q.front();q.pop();
                for(int t=1;t<=6;t++){
                    pii cc={x+dx[t],y+dy[t]};
                    if(ss.find(cc)!=ss.end() && outers.find(cc)==outers.end()){
                        q.push(cc);
                        outers.insert(cc);
                    }
                }
            }

            map<int,vector<int>> mp;
            for(pii x:p){
                if(outers.find({x.fi,x.se-1})!=outers.end()) mp[x.fi].push_back(x.se);
                if(outers.find({x.fi,x.se+1})!=outers.end()) mp[x.fi].push_back(x.se);
            }

            int rt=-1,M=0;
            vector<int> cx;
            vector<pii> range;
            vector<vector<int>> edge;

            int px=-inf;
            vector<pii> pre;
            for(auto [x,v]:mp){
                sort(v.begin(),v.end());
                vector<pii> cc;
                for(int i=0;i<(int)v.size();i+=2){
                    cc.push_back({v[i],v[i+1]});
                    //cout << x << ' ' << v[i] << ' ' << v[i+1] << '\n';
                }
                if(px+1==x){
                    int pos=0,S=M-(int)pre.size();
                    for(auto p:cc){
                        int l=p.fi,r=p.se;
                        if(x==0 && l<=0 && 0<=r) rt=M;
                        cx.push_back(x);
                        edge.push_back({});
                        range.push_back({l,r});

                        while(pos<(int)pre.size() && pre[pos].se+1<l) pos++;
                        int lst=pos;
                        while(pos<(int)pre.size() && pre[pos].fi<=r){
                            edge[S+pos].push_back(M);
                            edge[M].push_back(S+pos);
                            lst=pos++;
                        }
                        pos=lst;M++;
                    }
                }
                else{
                    for(auto p:cc){
                        if(x==0 && p.fi<=0 && 0<=p.se) rt=M;
                        cx.push_back(x);
                        edge.push_back({});
                        range.push_back({p.fi,p.se});
                        M++;
                    }
                }
                px=x;pre=cc;
            }
            function<void(int,int,int,int,int)> dfs = [&](int u,int p,int l,int r,int d){
                auto [L,R]=range[u];
                //cout << u << ' ' << l << ' ' << r << ' ' << d << '\n';
                T=(T+(R-L+1)*d+(l-L)*(l-L+1)/2+(R-r)*(R-r+1)/2)%mod;
                for(int v:edge[u]){
                    if(v==p) continue;
                    auto [nL,nR]=range[v];
                    int nl=l-(cx[v]<cx[u]),nr=r+(cx[v]>cx[u]);
                    int cl=max(nl,nL),cr=min(nr,nR);
                    if(cl<=cr) dfs(v,u,cl,cr,d+1);
                    else if(nr<nL) dfs(v,u,nL,nL,d+nL-nr+1);
                    else dfs(v,u,nR,nR,d+nl-nR+1);
                }
            };
            dfs(rt,-1,0,0,0);
        }
    }
    return (a*A+T*B)%mod;
}
#undef int
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 436 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 420 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 2 ms 604 KB Output is correct
3 Correct 3 ms 860 KB Output is correct
4 Correct 2 ms 604 KB Output is correct
5 Correct 3 ms 604 KB Output is correct
6 Correct 3 ms 604 KB Output is correct
7 Correct 3 ms 604 KB Output is correct
8 Correct 2 ms 604 KB Output is correct
9 Correct 3 ms 688 KB Output is correct
10 Correct 3 ms 860 KB Output is correct
11 Correct 3 ms 800 KB Output is correct
12 Correct 2 ms 860 KB Output is correct
13 Correct 2 ms 860 KB Output is correct
14 Correct 3 ms 860 KB Output is correct
15 Correct 3 ms 860 KB Output is correct
16 Correct 3 ms 860 KB Output is correct
17 Correct 2 ms 604 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 504 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 2 ms 348 KB Output is correct
7 Correct 4 ms 860 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 6 ms 1116 KB Output is correct
12 Correct 7 ms 1116 KB Output is correct
13 Correct 6 ms 1064 KB Output is correct
14 Correct 7 ms 1116 KB Output is correct
15 Correct 1 ms 408 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 448 KB Output is correct
9 Correct 4 ms 860 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 6 ms 1184 KB Output is correct
14 Correct 6 ms 1372 KB Output is correct
15 Correct 8 ms 1260 KB Output is correct
16 Correct 6 ms 1372 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 11 ms 2140 KB Output is correct
21 Correct 2 ms 604 KB Output is correct
22 Correct 1 ms 444 KB Output is correct
23 Correct 14 ms 2748 KB Output is correct
24 Correct 25 ms 4220 KB Output is correct
25 Correct 24 ms 4444 KB Output is correct
26 Correct 12 ms 2396 KB Output is correct
27 Correct 8 ms 1884 KB Output is correct
28 Correct 6 ms 1364 KB Output is correct
29 Correct 26 ms 4692 KB Output is correct
30 Correct 29 ms 4688 KB Output is correct
31 Correct 26 ms 4788 KB Output is correct
32 Correct 28 ms 4704 KB Output is correct
33 Correct 12 ms 2188 KB Output is correct
34 Correct 6 ms 1372 KB Output is correct
35 Correct 1 ms 344 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 2 ms 604 KB Output is correct
3 Correct 3 ms 860 KB Output is correct
4 Correct 2 ms 604 KB Output is correct
5 Correct 2 ms 604 KB Output is correct
6 Correct 3 ms 604 KB Output is correct
7 Correct 2 ms 600 KB Output is correct
8 Correct 2 ms 604 KB Output is correct
9 Correct 3 ms 672 KB Output is correct
10 Correct 3 ms 860 KB Output is correct
11 Correct 3 ms 860 KB Output is correct
12 Correct 2 ms 860 KB Output is correct
13 Correct 2 ms 860 KB Output is correct
14 Correct 3 ms 860 KB Output is correct
15 Correct 3 ms 1112 KB Output is correct
16 Correct 3 ms 860 KB Output is correct
17 Correct 2 ms 604 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 2 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 4 ms 860 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 6 ms 1116 KB Output is correct
29 Correct 6 ms 1112 KB Output is correct
30 Correct 6 ms 1116 KB Output is correct
31 Correct 6 ms 1112 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 203 ms 33984 KB Output is correct
35 Correct 215 ms 28184 KB Output is correct
36 Correct 180 ms 26576 KB Output is correct
37 Correct 305 ms 42084 KB Output is correct
38 Correct 316 ms 43444 KB Output is correct
39 Correct 342 ms 48900 KB Output is correct
40 Correct 339 ms 42416 KB Output is correct
41 Correct 293 ms 45524 KB Output is correct
42 Correct 387 ms 50448 KB Output is correct
43 Correct 435 ms 55036 KB Output is correct
44 Correct 294 ms 54344 KB Output is correct
45 Correct 295 ms 45444 KB Output is correct
46 Correct 262 ms 42684 KB Output is correct
47 Correct 255 ms 31160 KB Output is correct
48 Correct 360 ms 47448 KB Output is correct
49 Correct 361 ms 53424 KB Output is correct
50 Correct 0 ms 344 KB Output is correct
51 Correct 0 ms 344 KB Output is correct
52 Correct 0 ms 348 KB Output is correct
53 Correct 0 ms 348 KB Output is correct
54 Correct 0 ms 348 KB Output is correct
55 Correct 0 ms 348 KB Output is correct
56 Correct 0 ms 344 KB Output is correct
57 Correct 0 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Execution timed out 2106 ms 461076 KB Time limit exceeded
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 2 ms 604 KB Output is correct
7 Correct 3 ms 860 KB Output is correct
8 Correct 2 ms 600 KB Output is correct
9 Correct 2 ms 604 KB Output is correct
10 Correct 3 ms 600 KB Output is correct
11 Correct 2 ms 600 KB Output is correct
12 Correct 2 ms 600 KB Output is correct
13 Correct 3 ms 860 KB Output is correct
14 Correct 3 ms 860 KB Output is correct
15 Correct 3 ms 860 KB Output is correct
16 Correct 3 ms 860 KB Output is correct
17 Correct 3 ms 860 KB Output is correct
18 Correct 3 ms 860 KB Output is correct
19 Correct 3 ms 860 KB Output is correct
20 Correct 3 ms 856 KB Output is correct
21 Correct 2 ms 604 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 4 ms 860 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 6 ms 1368 KB Output is correct
33 Correct 7 ms 1368 KB Output is correct
34 Correct 6 ms 1116 KB Output is correct
35 Correct 6 ms 1372 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 1 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 11 ms 2140 KB Output is correct
40 Correct 2 ms 604 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 14 ms 2652 KB Output is correct
43 Correct 27 ms 4188 KB Output is correct
44 Correct 23 ms 4444 KB Output is correct
45 Correct 12 ms 2392 KB Output is correct
46 Correct 8 ms 1880 KB Output is correct
47 Correct 6 ms 1372 KB Output is correct
48 Correct 26 ms 4796 KB Output is correct
49 Correct 26 ms 4692 KB Output is correct
50 Correct 26 ms 5068 KB Output is correct
51 Correct 25 ms 4444 KB Output is correct
52 Correct 12 ms 2396 KB Output is correct
53 Correct 6 ms 1372 KB Output is correct
54 Correct 200 ms 33860 KB Output is correct
55 Correct 221 ms 28060 KB Output is correct
56 Correct 180 ms 26812 KB Output is correct
57 Correct 298 ms 42824 KB Output is correct
58 Correct 316 ms 42924 KB Output is correct
59 Correct 346 ms 47620 KB Output is correct
60 Correct 336 ms 42264 KB Output is correct
61 Correct 286 ms 45744 KB Output is correct
62 Correct 389 ms 50424 KB Output is correct
63 Correct 411 ms 56524 KB Output is correct
64 Correct 295 ms 53652 KB Output is correct
65 Correct 315 ms 45340 KB Output is correct
66 Correct 284 ms 42520 KB Output is correct
67 Correct 240 ms 31672 KB Output is correct
68 Correct 376 ms 47308 KB Output is correct
69 Correct 353 ms 52632 KB Output is correct
70 Execution timed out 2116 ms 461016 KB Time limit exceeded
71 Halted 0 ms 0 KB -