Submission #125050

# Submission time Handle Problem Language Result Execution time Memory
125050 2019-07-04T13:55:08 Z hugo_pm Long Mansion (JOI17_long_mansion) C++17
25 / 100
3000 ms 93352 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define form2(i, a, b) for (int i = (a); i < (b); ++i)
#define ford2(i, a, b) for (int i = (a-1); i >= (b); --i)
#define form(i, n) form2(i, 0, n)
#define ford(i, n) ford2(i, n, 0)

#define chmax(x, v) x = max(x, (v))
#define chmin(x, v) x = min(x, (v))
#define fi first
#define se second

const long long BIG = 1000000000000000000LL;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

void solve();
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	solve();
	return 0;
}


// Version classique du segment tree
// Opérations en O(log N)
// Indices 0-indexés

class SegtreeClassic
{
    public:
        void init(vector<int> tab);
        int requete(int gauche, int droite);
        void modifier(int index, int val);

    private:
        const int UNDEF = 1e9 + 171;
        int nbElements;
        int combiner(int a, int b);
        vector<int> interne;
};

void SegtreeClassic::init(vector<int> tab)
{
    nbElements = tab.size();
    interne.resize(2 * nbElements);

    for (int ind = 0; ind < nbElements; ++ind)
        interne[ind + nbElements] = tab[ind];

    for (int ind = nbElements - 1; ind >= 0; --ind)
        interne[ind] = combiner(interne[2*ind], interne[2*ind + 1]);
}

int SegtreeClassic::requete(int gauche, int droite)
{
    int res = UNDEF;
    ++droite; // Pour passer à un intervalle semi-ouvert [l ; r[
    gauche += nbElements;
    droite += nbElements;

    while (gauche < droite)
    {
        // Si gauche est impair, le noeud est inclus mais pas le parent
        // On ajoute donc ce noeud et on passera plus tard au noeud à droite du parent
        if (gauche & 1)
            res = combiner(res, interne[gauche++]);
        // Même raisonnement sachant que droite est exclus, d'où la {pré}-décrémentation
        if (droite & 1)
            res = combiner(res, interne[--droite]);
        
        gauche /= 2;
        droite /= 2;
    }

    return res;
}

void SegtreeClassic::modifier(int index, int val)
{
    index += nbElements;
    interne[index] = val;
    index /= 2;
    while (index > 0)
    {
        interne[index] = combiner(interne[2*index], interne[2*index+1]);
        index /= 2;
    }
}

int SegtreeClassic::combiner(int a, int b)
{
    if (a == UNDEF) return b;
    if (b == UNDEF) return a;
    return min(a, b);
}



const int borne = 501*1000;
int nbElem, nbReq;
int reqKey[borne];
int conLeft[borne], conRight[borne];
vector<int> keyPos[borne];
vector<int> iniTab[borne];

int lastLigneDroite[borne];
int li[borne], ri[borne];

void solve()
{
	cin >> nbElem;
	form(i, nbElem-1) { cin >> reqKey[i]; }
	form(iPos, nbElem) {
		int nbKeys; cin >> nbKeys;
		form(iRcp, nbKeys) {
			int keyId; cin >> keyId;
			keyPos[keyId].push_back(iPos);
			iniTab[iPos].push_back(keyId);
		}
	}

	conLeft[0] = BIG;
	conRight[nbElem-1] = -BIG;

	form(iElem, nbElem) {
		if (iElem > 0) {
			int keyToLeft = reqKey[iElem-1];
			auto &listePos = keyPos[keyToLeft];
			
			auto it = lower_bound(listePos.begin(), listePos.end(), iElem);
			if (listePos.empty() || it == listePos.end()) {
				conLeft[iElem] = BIG;
			} else {
				conLeft[iElem] = *it;
			}
		}

		if (iElem+1 < nbElem) {
			int keyToRight = reqKey[iElem];
			auto &listePos = keyPos[keyToRight];

			auto it = upper_bound(listePos.begin(), listePos.end(), iElem);
			if (listePos.empty() || it == listePos.begin()) {
				conRight[iElem] = -BIG;
			} else {
				--it;
				conRight[iElem] = *it;
			}
		}
	}


	vector<int> lstPos(borne, BIG);
	SegtreeClassic sc; sc.init(lstPos);

	ford(iElem, nbElem) {

		if (conRight[iElem] >= 0) sc.modifier(conRight[iElem]+1, iElem);
		else sc.modifier(0, iElem);

		lastLigneDroite[iElem] = sc.requete(0, iElem); // TODO A VERIFIER
	}

	li[0] = 0; ri[0] = lastLigneDroite[0];
	form2(iElem, 1, nbElem) {
		li[iElem] = iElem;
		ri[iElem] = lastLigneDroite[iElem];
		if (conLeft[iElem] <= ri[iElem]) {
			li[iElem] = li[iElem-1];
			chmax(ri[iElem], ri[iElem-1]);
		}
		while (true) {
			if (conLeft[li[iElem]] <= ri[iElem]) --li[iElem];
			else if (conRight[ri[iElem]] >= li[iElem]) ++ri[iElem];
			else break;
		}
	}

	cin >> nbReq;
	form(iReq, nbReq) {
		int x, y; cin >> x >> y;
		--x; --y;
		if (li[x] <= y && y <= ri[x]) {
			cout << "YES\n";
		} else {
			cout << "NO\n";
		}
	}
}
# Verdict Execution time Memory Grader output
1 Correct 42 ms 39928 KB Output is correct
2 Correct 43 ms 39848 KB Output is correct
3 Correct 45 ms 40056 KB Output is correct
4 Correct 41 ms 39800 KB Output is correct
5 Correct 42 ms 39792 KB Output is correct
6 Correct 42 ms 39876 KB Output is correct
7 Correct 41 ms 39800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 39928 KB Output is correct
2 Correct 43 ms 39848 KB Output is correct
3 Correct 45 ms 40056 KB Output is correct
4 Correct 41 ms 39800 KB Output is correct
5 Correct 42 ms 39792 KB Output is correct
6 Correct 42 ms 39876 KB Output is correct
7 Correct 41 ms 39800 KB Output is correct
8 Correct 159 ms 41872 KB Output is correct
9 Correct 153 ms 41896 KB Output is correct
10 Correct 159 ms 42508 KB Output is correct
11 Correct 160 ms 42684 KB Output is correct
12 Correct 141 ms 41384 KB Output is correct
13 Correct 151 ms 42064 KB Output is correct
14 Correct 153 ms 42156 KB Output is correct
15 Correct 153 ms 42024 KB Output is correct
16 Correct 148 ms 41772 KB Output is correct
17 Correct 155 ms 42040 KB Output is correct
18 Correct 151 ms 42028 KB Output is correct
19 Correct 151 ms 41984 KB Output is correct
20 Correct 148 ms 42024 KB Output is correct
21 Correct 148 ms 41740 KB Output is correct
22 Correct 149 ms 41896 KB Output is correct
23 Correct 152 ms 41768 KB Output is correct
24 Correct 171 ms 41740 KB Output is correct
25 Correct 154 ms 41768 KB Output is correct
26 Correct 154 ms 41804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 685 ms 59192 KB Output is correct
2 Correct 989 ms 58620 KB Output is correct
3 Correct 1234 ms 57700 KB Output is correct
4 Correct 799 ms 58844 KB Output is correct
5 Correct 852 ms 58612 KB Output is correct
6 Correct 533 ms 53932 KB Output is correct
7 Correct 299 ms 53672 KB Output is correct
8 Correct 270 ms 53620 KB Output is correct
9 Correct 268 ms 53604 KB Output is correct
10 Correct 250 ms 53464 KB Output is correct
11 Correct 246 ms 53288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 39928 KB Output is correct
2 Correct 43 ms 39848 KB Output is correct
3 Correct 45 ms 40056 KB Output is correct
4 Correct 41 ms 39800 KB Output is correct
5 Correct 42 ms 39792 KB Output is correct
6 Correct 42 ms 39876 KB Output is correct
7 Correct 41 ms 39800 KB Output is correct
8 Correct 159 ms 41872 KB Output is correct
9 Correct 153 ms 41896 KB Output is correct
10 Correct 159 ms 42508 KB Output is correct
11 Correct 160 ms 42684 KB Output is correct
12 Correct 141 ms 41384 KB Output is correct
13 Correct 151 ms 42064 KB Output is correct
14 Correct 153 ms 42156 KB Output is correct
15 Correct 153 ms 42024 KB Output is correct
16 Correct 148 ms 41772 KB Output is correct
17 Correct 155 ms 42040 KB Output is correct
18 Correct 151 ms 42028 KB Output is correct
19 Correct 151 ms 41984 KB Output is correct
20 Correct 148 ms 42024 KB Output is correct
21 Correct 148 ms 41740 KB Output is correct
22 Correct 149 ms 41896 KB Output is correct
23 Correct 152 ms 41768 KB Output is correct
24 Correct 171 ms 41740 KB Output is correct
25 Correct 154 ms 41768 KB Output is correct
26 Correct 154 ms 41804 KB Output is correct
27 Correct 685 ms 59192 KB Output is correct
28 Correct 989 ms 58620 KB Output is correct
29 Correct 1234 ms 57700 KB Output is correct
30 Correct 799 ms 58844 KB Output is correct
31 Correct 852 ms 58612 KB Output is correct
32 Correct 533 ms 53932 KB Output is correct
33 Correct 299 ms 53672 KB Output is correct
34 Correct 270 ms 53620 KB Output is correct
35 Correct 268 ms 53604 KB Output is correct
36 Correct 250 ms 53464 KB Output is correct
37 Correct 246 ms 53288 KB Output is correct
38 Correct 571 ms 85952 KB Output is correct
39 Correct 596 ms 93352 KB Output is correct
40 Correct 502 ms 77864 KB Output is correct
41 Execution timed out 3053 ms 75140 KB Time limit exceeded
42 Halted 0 ms 0 KB -