QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#736000#6533. Traveling in Cells_xqy_RE 472ms26492kbC++204.0kb2024-11-11 23:27:422024-11-11 23:27:42

Judging History

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

  • [2024-11-11 23:27:42]
  • 评测
  • 测评结果:RE
  • 用时:472ms
  • 内存:26492kb
  • [2024-11-11 23:27:42]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using LL = long long;

template<typename T>
struct Fenwick{
    int n;
    std::vector<T> tr;

    Fenwick(int n) : n(n), tr(n + 1, 0){}

    int lowbit(int x){
        return x & -x;
    }

    void modify(int x, T c){
        for(int i = x; i <= n; i += lowbit(i)) tr[i] += c;
    }

    void modify(int l, int r, T c){
        modify(l, c);
        if (r + 1 <= n) modify(r + 1, -c);
    }

    T query(int x){
        T res = T();
        for(int i = x; i; i -= lowbit(i)) res += tr[i];
        return res;
    }

    T query(int l, int r){
        return query(r) - query(l - 1);
    }

    int find_first(T sum){
        int ans = 0; T val = 0;
        for(int i = std::__lg(n); i >= 0; i--){
            if ((ans | (1 << i)) <= n && val + tr[ans | (1 << i)] < sum){
                ans |= 1 << i;
                val += tr[ans];
            }
        }
        return ans + 1;
    }

    int find_last(T sum){
        int ans = 0; T val = 0;
        for(int i = std::__lg(n); i >= 0; i--){
            if ((ans | (1 << i)) <= n && val + tr[ans | (1 << i)] <= sum){
                ans |= 1 << i;
                val += tr[ans];
            }
        }
        return ans;
    }

};
using BIT = Fenwick<LL>;

// 比 unordered_map 更快的哈希表
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
struct chash {
    int operator()(int x) const { return x ^ RANDOM; }
};
typedef gp_hash_table<int, int, chash> hash_t;

constexpr int N = 1E5 + 5;
hash_t T[N];

int n,q;

int lowbit(int x) {
	return x & (-x);
}
void modify(int x , int c , int val) {
	for ( ; x <= n ; x += lowbit(x)) {
		T[x][c] += val;
	}
}
int query(int x,vector<int>& s) {
	int cnt = 0;
	for (; x ; x -= lowbit(x)) {
		for (auto c : s) {
			auto it = T[x].find(c);
			if (it != T[x].end()) {
				cnt += it->second;
			}
		}
	}
	return cnt;
}
int queryl(int lim,vector<int>& s) {
	
	int base = query(lim , s);
	if (base == lim) return 1;

	int b = 1;
	while (b <= n) b <<= 1;
		
	int now = 0 , cnt = 0;
	for (b >>= 1 ; b ; b >>= 1) {
		int nxt = now | b , tmp = 0;
		for (auto c : s) {
			auto it = T[nxt].find(c);
			if (it != T[nxt].end()) {
				tmp += it->second;
			}
		}
		if (!(nxt > lim || base - (cnt + tmp) == lim - nxt)) {
			now = nxt , cnt += tmp;
		}
	}

	//所以 "1" 位置进行了特判
	return now + 2;
}
int queryr(int lim , vector<int>& s) {
	int base = query(lim , s);
	
	int b = 1;
	while (b <= n) b <<= 1;

	int now = 0 , cnt = 0;
	for (b >>= 1 ; b ; b >>= 1) {
		int nxt = now | b , tmp = 0;
		for (auto c : s) {
			auto it = T[nxt].find(c);
			if (it != T[nxt].end()) {
				tmp += it->second;
			}
		}
		if (nxt < lim || (cnt + tmp) - base == nxt - lim) {
			now = nxt , cnt += tmp;
		}
	}
	return now;
}

void solve() {

	cin >> n >> q;

	vector<int> c(n + 1) , v(n + 1);
	for (int i = 1 ; i <= n ; ++i) {
		cin >> c[i];
	}
	for (int i = 1 ; i <= n ; ++i) {
		cin >> v[i];
	}

	BIT tr(n);
	for (int i = 1 ; i <= n ; ++i) {
		tr.modify(i , v[i]);
	}
	for (int i = 1 ; i <= n ; ++i) {
		modify(i , c[i] , 1);
	}

	for (int i = 1 ; i <= q ; ++i) {
		int opt;
		cin >> opt;
		if (opt == 1) {
			int p,x;
			cin >> p >> x;
			if (c[p] == x) continue;
			modify(p , c[p] , -1);
			c[p] = x;
			modify(p , c[p] , 1);
		} else if (opt == 2) {
			int p,x;
			cin >> p >> x;
			if (v[p] == x) continue;
			tr.modify(p , x - v[p]);
			v[p] = x;
		} else {
			int x,k;
			cin >> x >> k;
			vector<int> s(k);
			for (int j = 0 ; j < k ; ++j) {
				cin >> s[j];
			}
			int l = queryl(x , s);
			int r = queryr(x , s);
			cout << tr.query(r) - tr.query(l - 1) << "\n";
		}
	}

	for (int i = 1 ; i <= n ; ++i) {
		T[i].clear();
	}
}

int main() {

	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int t = 1;
	cin >> t;
	while (t--) {
		solve();
	}

	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 8ms
memory: 24628kb

input:

2
5 10
1 2 3 1 2
1 10 100 1000 10000
3 3 1 3
3 3 2 2 3
2 5 20000
2 3 200
3 3 2 1 3
3 3 3 1 2 3
1 3 4
2 1 100000
1 2 2
3 1 2 1 2
4 1
1 2 3 4
1000000 1000000 1000000 1000000
3 4 4 1 2 3 4

output:

100
110
1200
21211
100010
4000000

result:

ok 6 numbers

Test #2:

score: 0
Accepted
time: 279ms
memory: 24700kb

input:

20000
15 15
1 1 3 3 2 3 3 3 3 3 2 3 2 3 3
634593973 158136379 707704004 998369647 831633445 263092797 937841412 451774682 552617416 483763379 50360475 794662797 74247465 537217773 901809831
3 6 4 1 3 5 10
3 5 7 1 2 3 4 5 9 10
3 4 3 3 8 9
2 13 608033129
3 15 2 3 5
1 9 3
3 8 4 1 3 7 10
2 6 727472865
3...

output:

2689089686
8377825475
1706073651
1439027604
2689089686
792730352
8904867081
8904867081
8270273108
831633445
692051585
2782432626
697783016
883944422
184057757
287523250
184057757
696388752
184057757
1675459344
2667693025
2614711258
4006992373
1767091974
5348541057
5348541057
390631780
2290216252
942...

result:

ok 200062 numbers

Test #3:

score: 0
Accepted
time: 332ms
memory: 24876kb

input:

2000
150 150
8 3 8 8 8 6 8 4 2 7 6 8 8 5 8 7 7 8 5 6 8 8 6 8 8 8 8 7 8 6 6 8 8 8 6 2 3 4 8 8 7 8 5 8 2 6 8 7 8 8 6 8 6 8 3 8 8 8 8 4 7 8 7 3 7 6 7 5 5 8 6 8 8 6 3 8 6 7 6 8 8 7 4 8 6 7 8 7 7 7 7 8 8 8 8 2 5 2 8 8 6 7 6 3 8 8 7 8 8 8 6 6 8 6 6 7 5 8 8 8 7 8 7 7 6 8 8 8 8 8 8 6 5 7 5 5 8 7 8 7 7 7 6 5...

output:

4449391171
3290849667
852793841
5178673994
995994209
11431868919
4327723427
5071541023
3032743466
962345334
2997656427
4534278452
3851900075
3611231417
5071541023
1477584218
1299005818
1299005818
2145605244
854143763
886347565
2081234124
2333808475
2455955801
4179722063
2328504333
1473735464
4107685...

result:

ok 199987 numbers

Test #4:

score: 0
Accepted
time: 472ms
memory: 26492kb

input:

10
30000 30000
3 4 2 4 4 4 4 3 4 3 4 3 4 3 4 4 2 4 4 4 4 4 3 3 3 4 3 4 3 4 3 3 4 2 4 3 3 3 3 4 3 4 4 4 4 2 3 3 4 2 3 4 4 4 4 1 4 4 4 4 4 4 4 4 3 3 3 4 4 4 4 4 2 3 4 4 4 4 3 4 4 3 3 3 4 4 3 4 4 2 3 4 4 4 4 3 2 4 3 4 3 2 4 4 3 4 2 2 4 4 4 4 2 4 3 2 4 4 3 4 4 4 2 4 4 3 2 3 2 3 3 3 4 2 4 3 4 1 4 3 4 4 4...

output:

6959437173
934970676
72461245502
8365928740
8384151048
984567228
12482909122
1904927816
15134139942
3759040688
92670874909
332468911
5936663371
3562978848
1300592004
10314009201
5581540905
131246926443
15087184135864
4077066271
1124704817
1520626740
4388174158
744377942
2770411457
6231852240
1508724...

result:

ok 200135 numbers

Test #5:

score: -100
Runtime Error

input:

3
100000 100000
6 6 2 6 5 3 6 5 4 6 4 6 6 6 6 5 2 5 2 6 6 6 1 6 5 6 4 5 6 6 5 4 5 4 3 4 5 5 6 6 5 6 6 5 2 5 6 5 4 2 5 6 6 6 5 2 5 6 6 4 5 6 3 3 6 5 6 5 5 5 5 4 4 4 4 3 6 5 4 5 6 5 6 6 6 6 3 6 5 6 5 4 3 5 6 4 5 3 6 5 3 5 6 4 6 5 4 5 5 5 2 5 4 6 6 3 5 5 5 5 5 4 5 5 6 5 5 6 6 6 5 5 4 6 5 4 4 2 6 6 6 5 ...

output:


result: