Submission #1039484

# Submission time Handle Problem Language Result Execution time Memory
1039484 2024-07-31T00:07:05 Z Trent Spy 3 (JOI24_spy3) C++17
100 / 100
274 ms 8276 KB
#include "Aoi.h"
#include "bits/stdc++.h"
 
using namespace std;
#define forR(i, x) for(int i = 0; i < (x); ++i)
#define REP(i, a, b) for(int i = (a); i < (b); ++i)
#define all(x) x.begin(), x.end()
 
namespace {
	typedef long long ll;
	struct pii{int a, b;};
	bool operator <(pii a, pii b) {return a.a < b.a || a.a == b.a && a.b < b.b;}
	struct edge{int t; ll len;};
	struct node{int i; ll to;};
	bool operator <(node a, node b){
		return a.to > b.to;
	}
	const ll INF = 1e17;
	string toBinNumber(int x, int bits) {
		string ret;
		forR(i, bits) {
			ret.push_back(x % 2 == 0 ? '0' : '1');
			x /= 2;
		}
		reverse(all(ret));
		assert(x == 0);
		return ret;
	}
}
 
std::string aoi(int N, int M, int Q, int K, std::vector<int> A,
                std::vector<int> B, std::vector<long long> C,
                std::vector<int> T, std::vector<int> X) {
	vector<vector<edge>> adj(N);
	map<pii, int> xi;
	forR(i, M){
		adj[A[i]].push_back({B[i], C[i]});
		adj[B[i]].push_back({A[i], C[i]});
	}
	forR(i, K){
		xi.insert({{A[X[i]], B[X[i]]}, i});
		xi.insert({{B[X[i]], A[X[i]]}, i});
	}
 
	vector<int> indIn(K, Q);
	vector<int> lOld(Q);
	// dijkstra
	forR(i, Q){
		vector<ll> dis(N, INF);
		vector<bool> vis(N);
		vector<int> pre(N);
		priority_queue<node> dij;
		dij.push({0, 0});
		dis[0] = 0;
		while(!dij.empty()){
			auto [cur, len] = dij.top();
			dij.pop();
			if(!vis[cur]){
				vis[cur] = true;
				for(auto [to, el] : adj[cur]){
					if(len + el < dis[to]){
						dis[to] = len + el;
						pre[to] = cur;
						dij.push({to, dis[to]});
					}
				}
			}
		}
 
		vector<int> pth;
		for(int j = T[i]; j != 0; j = pre[j]) pth.push_back(j);
		pth.push_back(0);
		reverse(all(pth));
		int lasUsed = -1;
		forR(j, (int) pth.size() - 1) {
			if(xi.count({pth[j], pth[j+1]})) {
				int eInd = xi[{pth[j], pth[j+1]}];
				if(indIn[eInd] == Q) {
					indIn[eInd] = i;
				} else {
					lasUsed = eInd;
				}
			}
		}
		lOld[i] = lasUsed == -1 ? -1 : lasUsed;
	}
    set<int> sUsedX;
    for(int i : lOld) if(i != -1) {
        sUsedX.insert(i);
    }
    vector<int> usedX;
    for(int i : sUsedX) usedX.push_back(i);
 
	for(int i : indIn) cerr << i << ' ';
	cerr << '\n';
	for(int i : lOld) cerr << i << ' ';
	cerr << '\n';
    for(int i : usedX) cerr << i << ' ';
    cerr << '\n';

	string ret;
	forR(i, indIn.size()) {
        if(!sUsedX.count(i)) ret += toBinNumber(indIn[i], 5);
        else {
            assert(indIn[i] + 1 < Q);
            ret += toBinNumber(indIn[i] + Q + 1, 5);
        }
	}
    assert(lOld[0] == -1);
    REP(j, 1, lOld.size()){
        if(lOld[j] == -1) ret += toBinNumber(usedX.size(), 4);
        else {
            bool found = false;
            forR(k, usedX.size()) if(usedX[k] == lOld[j]) {
                ret += toBinNumber(k, 4);
                found = true;
                break;
            }
            assert(found);
        }
    }
	cerr << ret << '\n';
	return ret;
}
#include "Bitaro.h"
#include "bits/stdc++.h"
 
using namespace std;
#define forR(i, x) for(int i = 0; i < (x); ++i)
#define REP(i, a, b) for(int i = (a); i < (b); ++i)
#define all(x) x.begin(), x.end()
 
namespace {
	typedef long long ll;
	struct pii{int a, b;};
	bool operator <(pii a, pii b) {return a.a < b.a || a.a == b.a && a.b < b.b;}
	struct edge{int t, ind; ll len;};
	struct node{int i; ll to;};
	bool operator <(node a, node b){
		return a.to > b.to;
	}
	const ll INF = 1e17;
	int fromBinNumber(string s) {
		int ret = 0;
		for(char i : s) ret = ret * 2 + (i == '0' ? 0 : 1);
		return ret;
	}
}
 
void bitaro(int N, int M, int Q, int K, std::vector<int> A, std::vector<int> B,
            std::vector<long long> C, std::vector<int> T, std::vector<int> X,
            std::string s) {
	map<pii, int> xi;
	forR(i, K){
		xi.insert({{A[X[i]], B[X[i]]}, i});
		xi.insert({{B[X[i]], A[X[i]]}, i});
	}
 
	vector<vector<edge>> adj(N);
	map<pii, int> ei;
	forR(i, M){
		int cxi = -1;
		if(xi.count({A[i], B[i]})){
			cxi = xi[{A[i], B[i]}];
		}
		adj[A[i]].push_back({B[i], cxi, C[i]});
		adj[B[i]].push_back({A[i], cxi, C[i]});
		ei[{A[i], B[i]}] = i;
		ei[{B[i], A[i]}] = i;
	}
 
	vector<int> indIn;
	vector<int> lOld;
    vector<int> usedX;
	int ci=0;
	forR(i, K) {
        int val = fromBinNumber(s.substr(ci, 5));
        if(val >= Q + 1) {
            usedX.push_back(i);
            indIn.push_back(val - Q - 1);
        } else {
            indIn.push_back(val);
        }
		ci += 5;
	}
    lOld.push_back(-1);
	REP(j, 1, Q) {
        int val = fromBinNumber(s.substr(ci, 4));
        if(val == usedX.size()) lOld.push_back(-1);
        else {
            lOld.push_back(usedX[val]);
        }
		ci += 4;
	}
	for(int i : indIn) cerr << i << ' ';
	cerr << '\n';
	for(int i : lOld) cerr << i << ' ';
	cerr << '\n';
    for(int i : usedX) cerr << i << ' ';
    cerr << '\n';
 
	vector<bool> solid(N, false);
	vector<int> sPre(N);
	forR(i, Q){
		vector<ll> dis(N, INF);
		vector<bool> vis(N);
		vector<int> pre(N);
		int stNode;
		if(lOld[i] == -1) stNode = 0;
		else {
			int eInd = X[lOld[i]];
			stNode = sPre[A[eInd]] == B[eInd] ? A[eInd] : B[eInd];
			assert(solid[stNode]);
		}
 
		priority_queue<node> dij;
		dij.push({stNode, 0});
		dis[stNode] = 0;
		while(!dij.empty()){
			auto [cur, len] = dij.top();
			dij.pop();
			if(!vis[cur]){
				vis[cur] = true;
				for(auto [to, ind, el] : adj[cur]){
					if(ind == -1 || indIn[ind] == i){
						el = ind == -1 ? el : 0;
						if(len + el < dis[to]){
							dis[to] = len + el;
							pre[to] = cur;
							dij.push({to, dis[to]});
						}
					}
				}	
			}
		}
		vector<int> pth;
		for(int j = T[i]; j != stNode; j = pre[j]) pth.push_back(j);
		for(int j = stNode; j != 0; j = sPre[j]) pth.push_back(j);
		pth.push_back(0);
		reverse(all(pth));
		for(int j : pth) cerr << j << ' ';
		cerr << '\n';
		REP(j, 1, pth.size()) {
			solid[pth[j]] = true;
			sPre[pth[j]] = pth[j-1];
		}
		vector<int> ret;
		forR(j, (int) pth.size() - 1) ret.push_back(ei[{pth[j], pth[j+1]}]);
		answer(ret);
	}
}

Compilation message

Aoi.cpp: In function 'bool {anonymous}::operator<({anonymous}::pii, {anonymous}::pii)':
Aoi.cpp:12:64: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   12 |  bool operator <(pii a, pii b) {return a.a < b.a || a.a == b.a && a.b < b.b;}
      |                                                     ~~~~~~~~~~~^~~~~~~~~~~~
Aoi.cpp: In function 'std::string aoi(int, int, int, int, std::vector<int>, std::vector<int>, std::vector<long long int>, std::vector<int>, std::vector<int>)':
Aoi.cpp:5:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    5 | #define forR(i, x) for(int i = 0; i < (x); ++i)
      |                                     ^
Aoi.cpp:102:2: note: in expansion of macro 'forR'
  102 |  forR(i, indIn.size()) {
      |  ^~~~
Aoi.cpp:6:41: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    6 | #define REP(i, a, b) for(int i = (a); i < (b); ++i)
      |                                         ^
Aoi.cpp:110:5: note: in expansion of macro 'REP'
  110 |     REP(j, 1, lOld.size()){
      |     ^~~
Aoi.cpp:5:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    5 | #define forR(i, x) for(int i = 0; i < (x); ++i)
      |                                     ^
Aoi.cpp:114:13: note: in expansion of macro 'forR'
  114 |             forR(k, usedX.size()) if(usedX[k] == lOld[j]) {
      |             ^~~~

Bitaro.cpp: In function 'bool {anonymous}::operator<({anonymous}::pii, {anonymous}::pii)':
Bitaro.cpp:12:64: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   12 |  bool operator <(pii a, pii b) {return a.a < b.a || a.a == b.a && a.b < b.b;}
      |                                                     ~~~~~~~~~~~^~~~~~~~~~~~
Bitaro.cpp: In function 'void bitaro(int, int, int, int, std::vector<int>, std::vector<int>, std::vector<long long int>, std::vector<int>, std::vector<int>, std::string)':
Bitaro.cpp:65:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |         if(val == usedX.size()) lOld.push_back(-1);
      |            ~~~~^~~~~~~~~~~~~~~
Bitaro.cpp:6:41: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    6 | #define REP(i, a, b) for(int i = (a); i < (b); ++i)
      |                                         ^
Bitaro.cpp:119:3: note: in expansion of macro 'REP'
  119 |   REP(j, 1, pth.size()) {
      |   ^~~
# Verdict Execution time Memory Grader output
1 Correct 27 ms 6128 KB Output is correct
2 Correct 1 ms 772 KB Output is correct
3 Correct 55 ms 7008 KB Output is correct
4 Correct 172 ms 7084 KB Output is correct
5 Correct 57 ms 7084 KB Output is correct
6 Correct 79 ms 7088 KB Output is correct
7 Correct 51 ms 6828 KB Output is correct
8 Correct 71 ms 7008 KB Output is correct
9 Correct 145 ms 6724 KB Output is correct
10 Correct 37 ms 6584 KB Output is correct
11 Correct 49 ms 6836 KB Output is correct
12 Correct 88 ms 7224 KB Output is correct
13 Correct 68 ms 7088 KB Output is correct
14 Correct 47 ms 7004 KB Output is correct
15 Correct 132 ms 6956 KB Output is correct
16 Correct 32 ms 6580 KB Output is correct
17 Correct 259 ms 6996 KB Output is correct
18 Correct 274 ms 7092 KB Output is correct
19 Correct 62 ms 8096 KB Output is correct
20 Correct 53 ms 8104 KB Output is correct
21 Correct 63 ms 8176 KB Output is correct
22 Correct 79 ms 8092 KB Output is correct
23 Correct 60 ms 8092 KB Output is correct
24 Correct 78 ms 8124 KB Output is correct
25 Correct 210 ms 7204 KB Output is correct
26 Correct 223 ms 7068 KB Output is correct
27 Correct 2 ms 1284 KB Output is correct
28 Correct 65 ms 7396 KB Output is correct
29 Correct 82 ms 5160 KB Output is correct
30 Correct 87 ms 7348 KB Output is correct
31 Correct 43 ms 7428 KB Output is correct
32 Correct 64 ms 7360 KB Output is correct
33 Correct 59 ms 7092 KB Output is correct
34 Correct 104 ms 7552 KB Output is correct
35 Correct 98 ms 7588 KB Output is correct
36 Correct 80 ms 7600 KB Output is correct
37 Correct 69 ms 4068 KB Output is correct
38 Correct 194 ms 5384 KB Output is correct
39 Correct 181 ms 5268 KB Output is correct
40 Correct 28 ms 4728 KB Output is correct
41 Correct 75 ms 7792 KB Output is correct
42 Correct 54 ms 8276 KB Output is correct
43 Correct 107 ms 8012 KB Output is correct
44 Correct 32 ms 7860 KB Output is correct
45 Correct 74 ms 3964 KB Output is correct
46 Correct 92 ms 4800 KB Output is correct
47 Correct 110 ms 4788 KB Output is correct
48 Correct 0 ms 772 KB Output is correct
49 Correct 1 ms 784 KB Output is correct
50 Correct 24 ms 6612 KB Output is correct
51 Correct 4 ms 1032 KB Output is correct
52 Correct 2 ms 784 KB Output is correct
53 Correct 29 ms 6684 KB Output is correct
54 Correct 19 ms 4332 KB Output is correct
55 Correct 40 ms 5076 KB Output is correct
56 Correct 39 ms 7496 KB Output is correct
57 Correct 53 ms 8240 KB Output is correct
58 Correct 46 ms 6072 KB Output is correct
59 Correct 63 ms 8084 KB Output is correct
60 Correct 55 ms 7688 KB Output is correct
61 Correct 70 ms 7884 KB Output is correct
62 Correct 69 ms 7408 KB Output is correct
63 Correct 70 ms 7872 KB Output is correct
64 Correct 26 ms 6924 KB Output is correct
65 Correct 39 ms 5316 KB Output is correct
66 Correct 38 ms 8108 KB Output is correct
67 Correct 40 ms 5372 KB Output is correct
68 Correct 41 ms 8188 KB Output is correct
69 Correct 0 ms 776 KB Output is correct
70 Correct 0 ms 776 KB Output is correct
71 Correct 2 ms 776 KB Output is correct
72 Correct 19 ms 3860 KB Output is correct
73 Correct 32 ms 4688 KB Output is correct
74 Correct 33 ms 4536 KB Output is correct
75 Correct 16 ms 4636 KB Output is correct
76 Correct 0 ms 784 KB Output is correct
77 Correct 45 ms 7092 KB Output is correct
78 Correct 51 ms 7320 KB Output is correct
79 Correct 54 ms 7328 KB Output is correct
80 Correct 0 ms 776 KB Output is correct
81 Correct 53 ms 6836 KB Output is correct
82 Correct 48 ms 7048 KB Output is correct
83 Correct 61 ms 6968 KB Output is correct