QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#539044#8547. Whose Land?IllusionaryDominanceWA 206ms48888kbC++206.6kb2024-08-31 13:51:332024-08-31 13:51:33

Judging History

你现在查看的是最新测评结果

  • [2024-08-31 13:51:33]
  • 评测
  • 测评结果:WA
  • 用时:206ms
  • 内存:48888kb
  • [2024-08-31 13:51:33]
  • 提交

answer

#include <bits/stdc++.h>

template <class T>
inline int qlog(T a) {
    if (!a) return 0;
	double x = a;
	return ((*(long long*) & x) >> 52 & 2047) - 1023;
}

#define cin Mizuhashi
#define cout Parsee
#define endl '\n'

class Reader{
	private:
	    static const int BUF_SIZE = 1 << 22;
	    char BUF_R[BUF_SIZE], *csy1, *csy2;
	    #ifndef _LOCAL_RUNNING
		    #define GC (csy1 == csy2 && (csy2 = (csy1 = BUF_R) + fread(BUF_R, 1, BUF_SIZE, stdin), csy1 == csy2) ? EOF : *csy1 ++)
		#else
		    char cur;
		    #define GC (cur = getchar())
		#endif
	    #define IL inline
		
    public:
        IL bool eof() {
            #ifndef _LOCAL_RUNNING
                return csy1 == csy2 && (csy2 = (csy1 = BUF_R) + fread(BUF_R, 1, BUF_SIZE, stdin), csy1 == csy2);
            #else
                return cur == EOF;
            #endif
        }
        template <class Ty>
        IL Reader& operator >> (Ty &t) {
            int u = 0;
            char c = GC;
	    	for (t = 0; c < 48 || c > 57; c = GC)
                if (c == EOF) break;
                else if (c == '-') u = 1;
	    	for ( ; c > 47 && c < 58; c = GC) t = (t << 1) + (t << 3) + (c ^ 48);
	    	t = u ? -t : t; return *this;
        }
    	IL Reader& operator >> (double &t) {
            int tmp, u = 0; char c = GC;
	    	for (tmp = 0; c < 48 || c > 57; c = GC)
                if (c == EOF) break;
                else if (c == '-') u = 1;
	    	for ( ; c > 47 && c < 58; c = GC) tmp = (tmp << 1) + (tmp << 3) + (c ^ 48);
	    	t = (tmp = u ? -tmp : tmp);
    	    if (c == '.') {
    	        double x = 1;
    	        for (c = GC; c > 47 && c < 58; c = GC) t += (x /= 10) * (c ^ 48);
    	    }
    	    return *this;
    	}
    	IL Reader& operator >> (char *s) {
			char c = GC;
			for (*s = 0; c < 33; c = GC) if (c == EOF) return *this;
			for ( ; c > 32; c = GC) *s ++ = c;
			*s = 0; return *this;
		}
        IL Reader& operator >> (char &c) {
			for (c = GC; c < 33; c = GC) if (c == EOF) return *this;
            return *this;
        }
}cin;
class Writer{
	private:
	    static const int BUF_SIZE = 1 << 22;
	    char BUF_W[BUF_SIZE], *csy;
	    #define IL inline
		inline void WC(const char c) {
			if (csy - BUF_W == BUF_SIZE) fwrite(BUF_W, 1, BUF_SIZE, stdout), csy = BUF_W;
			*csy ++ = c;
		}
	
	public:
		Writer() : csy(BUF_W) {}
		~ Writer() {fwrite(BUF_W, 1, csy - BUF_W, stdout);}
		IL void flush() {fwrite(BUF_W, 1, csy - BUF_W, stdout); csy = BUF_W;}
		template <class Ty>
		IL Writer& operator << (Ty x) {
		    static int sta[32], top;
			if (x < 0) {
				WC('-');
                do sta[top ++] = - (x % 10); while (x /= 10);
			}else do sta[top ++] = x % 10; while (x /= 10);
			while (top) WC(sta[-- top] ^ 48);
			return *this;
		}
		IL Writer& operator << (const char &c) {WC(c); return *this;}
		IL Writer& operator << (const char *s) {while (*s) WC(*s ++); return *this;}
		IL Writer& operator << (char *s) {while (*s) WC(*s ++); return *this;}
}cout;

using namespace std;

typedef long long ll;
typedef pair <int, int> pii;
typedef unsigned int u32;
typedef unsigned long long u64;
typedef vector <int> vi;
typedef vector <ll> vl;

const int MAX_N = 500000 + 5;

int N, M, K, Q;
struct Edge{
    int y, prev;
}e[MAX_N << 1];
int elast[MAX_N], ecnt;
int dep[MAX_N], fa[MAX_N], cnt[MAX_N];
pii f[MAX_N][21];
struct Node{
    int l, r, v;
    Node (int l_ = 0, int r_ = 0, int v_ = 0) : l(l_), r(r_), v(v_) {}
    inline bool operator < (const Node &comp) const {return l < comp.l;}
};
set <Node> s[MAX_N];
vector <pii> qry[MAX_N];
int c[MAX_N], ans[MAX_N];

void modify(int x, int v) {
    assert(x > 0);
    for (int i = x; i <= N; i += i & -i) c[i] += v;
}

int query(int l, int r) {
    assert(l > 0 && r > 0);
    l --;
    int res = 0;
    while (l < r) {
        res += c[r];
        r -= r & -r;
    }
    while (r < l) {
        res -= c[l];
        l -= l & -l;
    }
    return res;
}

void Build(int x, int y) {
    ecnt ++;
    e[ecnt].y = y;
    e[ecnt].prev = elast[x];
    elast[x] = ecnt;
}

inline pii operator | (const pii &a, const pii &b) {
    return pair(min(a.first, b.first), max(a.second, b.second));
}

inline pii& operator |= (pii &a, const pii &b) {
    return (a = a | b);
}

void dfs(int u, int fath) {
    fa[u] = fath;
    dep[u] = dep[fath] + 1;
    M = max(M, dep[u]);
    cnt[dep[u]] ++;
    f[u][0] = pair(cnt[dep[u]], cnt[dep[u]]);
    for (int i = 1; i <= K; i ++) f[u][i] = pair(N, 0);
    for (int i = elast[u]; i; i = e[i].prev) {
        int v = e[i].y;
        if (v != fath) {
            dfs(v, u);
            for (int j = 1; j <= K; j ++) {
                f[u][j] |= f[v][j - 1];
            }
        }
    }
}

void modify(set <Node> &s, int l, int r, int x) {
    while (!s.empty()) {
        auto it = s.upper_bound(Node(r));
        if (it == s.begin()) break;
        auto [l_, r_, v] = *(-- it);
        if (r_ < r) break;
        modify(v, -(min(r_, r) - max(l_, l) + 1));
        s.erase(it);
        if (r_ > r) {
            s.insert(Node(r + 1, r_, v));
        }
        if (l_ < l) {
            s.insert(Node(l_, l - 1, v));
        }
    }
    modify(x, r - l + 1);
    s.insert(Node(l, r, x));
}

void modify(int u) {
    static pii mdf[64];
    for (int i = 0; i < 64; i ++) mdf[i] = pair(N, 0);
    for (int i = K, v = u; i >= 0 && v; i --, v = fa[v]) {
        for (int j = 0; j <= i; j ++) {
            mdf[i + j] |= f[v][j];
        }
    }
    for (int i = 0; i <= (K << 1); i ++) {
        if (mdf[i].first <= mdf[i].second) {
            int d = dep[u] - K + i;
            assert(d > 0 && d <= M);
            modify(s[d], mdf[i].first, mdf[i].second, u);
        }
    }
}

void solve() {
    ecnt = 0; M = 0;
    memset(c, 0, sizeof(int) * (N + 1));
    memset(cnt, 0, sizeof(int) * (N + 1));
    memset(elast, 0, sizeof(int) * (N + 1));
    
    cin >> N >> K >> Q;
    for (int i = 1; i < N; i ++) {
        int x, y;
        cin >> x >> y;
        Build(x, y);
        Build(y, x);
        qry[i].clear();
    }
    qry[N].clear();
    dfs(1, 0);
    for (int i = 1; i <= M; i ++) {
        s[i].clear();
    }
    for (int i = 1; i <= Q; i ++) {
        int l, r;
        cin >> l >> r;
        qry[r].emplace_back(l, i);
    }
    for (int i = 1; i <= N; i ++) {
        modify(i);
        for (auto [l, idx] : qry[i]) {
            ans[idx] = query(l, i);
        }
    }
    for (int i = 1; i <= Q; i ++) cout << ans[i] << endl;
}

int main() {
    int T;
    cin >> T;
    while (T --) solve();
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 3ms
memory: 42688kb

input:

2
5 1 2
1 2
1 3
2 4
2 5
2 2
2 3
8 2 3
1 2
1 3
2 4
2 5
4 6
5 7
7 8
2 2
2 5
3 4

output:

4
5
7
8
6

result:

ok 5 number(s): "4 5 7 8 6"

Test #2:

score: -100
Wrong Answer
time: 206ms
memory: 48888kb

input:

1000
500 1 500
291 2
406 9
207 13
71 15
351 17
442 18
496 19
104 20
208 23
448 34
80 42
187 44
352 45
290 46
116 47
318 50
226 52
129 55
83 59
100 60
54 61
73 65
63 66
454 67
43 71
26 74
68 26
320 75
388 76
425 79
170 81
350 83
48 85
153 86
221 90
290 95
130 96
82 98
124 82
36 99
213 100
121 101
132...

output:

268
436
380
124
343
370
512
8
357
484
463
377
189
259
385
646
151
44
381
276
93
359
38
320
272
71
140
537
176
24
166
527
305
347
266
358
572
551
79
398
57
468
419
227
436
292
195
271
145
71
221
497
510
376
326
560
347
173
316
461
219
577
208
197
633
62
570
130
311
215
311
411
119
448
597
396
124
195...

result:

wrong answer 1st numbers differ - expected: '255', found: '268'