QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#331008 | #7767. 数据结构 | cool_milo | 35 | 1292ms | 330176kb | C++14 | 6.6kb | 2024-02-17 22:03:09 | 2024-02-17 22:03:09 |
Judging History
answer
#include<bits/stdc++.h>
//zhouAKngyang 神
//zhouAKngyang 神
//zhouAKngyang 神
#define sz(a) ((int) (a).size())
using namespace std;
typedef unsigned long long ll;
typedef pair<int, int> pii;
typedef vector<pii> VC;
const int P = 998244353;
const int N = 2e5+5;
template<typename T>inline void read(T &a)
{
a = 0;
int f = 1;
char c = getchar();
while(!isdigit(c))
{
if(c == '-') f = -1;
c = getchar();
}
while(isdigit(c))
a = a * 10 + c - 48,c = getchar();
a *= f;
}
template<typename T,typename ...L> inline void read(T &a,L &...l)
{
read(a),read(l...);
}
const int K = 4;
int n, m, u, op, x, y, k, v, fa[N], up[N], sze[N], dfn[N], timestamp, dep[N];
VC kson[N][K], kdis[N][K], fh[N][K], sub[N], kans[N][K];
/*
五个数组分别是:恰好为 k 阶的儿子,距离一个点在k以内的点,父亲除了自己以外的恰好k阶儿子,子树信息,一个点到根的链及其距离为k以内的点。
*/
vector<int> G[N], sons[N];
inline void mg(VC &o) {
VC ans;
sort(o.begin(), o.end());
for(auto it:o) {
if(ans.size() && ans.back().second + 1 == it.first) {
ans.back().second = it.second;
}
else {
ans.emplace_back(it);
}
}
o = ans;//对不相交线段进行合并
}
inline VC getdel(int x, int y, int k) {//在x中去掉y得到答案。注意这里y一定是x的一个子集
VC u = kans[x][k], v = kans[y][k];
sort(u.begin(), u.end()), sort(v.begin(), v.end());
int l = 0;
VC ans;
for(auto it:u) {
while(l < sz(v) && v[l].second < it.first) {
l++;
}
int r = l;
while(r < sz(v) && v[r].second <= it.second) {
r++;
}
if(l == r) {
ans.emplace_back(it);
}
else {
int lstl = it.first;
for(int i = l; i < r; i++) {
if(v[i].first > lstl) {
ans.emplace_back(lstl, v[i].first - 1);
}
lstl = v[i].second + 1;
}
if(lstl <= it.second) {
ans.emplace_back(lstl, it.second);
}
}
}
return ans;
}
struct BIT {
ll a[N], b[N];
inline void mdf(int x, int v) {
ll xx = x;
for(; x < N; x += x & -x) {
a[x] += v, b[x] += xx * v;
}
}
void add(int l, int r, int x) {
//cerr<<l<<' '<<r<<' '<<x<<"??\n";
mdf(l, x), mdf(r + 1, -x);
}
inline ll sum(int x) {
ll xx = x;
ll ansa = 0, ansb = 0;
for(; x; x -= x & -x) {
ansa += a[x], ansb += b[x];
}
return (xx + 1) * ansa - ansb;
}
ll query(int l, int r) {
//cerr<<l<<' '<<r<<endl;
return sum(r) - sum(l - 1);
}
}bit;
inline int LCA(int a, int b) {
while(up[a] != up[b]) {
if(dep[up[a]] > dep[up[b]]) swap(a, b);
b = fa[up[b]];
}
return dep[a] < dep[b] ? a : b;
}
void DFS1(int u, int f) {
fa[u] = f, sze[u] = 1, dep[u] = dep[f] + 1;
for(auto it:G[u]) {
if(it == f) continue;
DFS1(it, u);
sze[u] += sze[it];
sons[u].emplace_back(it);
}
sort(sons[u].begin(), sons[u].end(), [&](int a, int b) {
return sze[a] > sze[b];
});
}
void iter(int u, int i) {
if(i == 1) {
if(!dfn[u]) {
dfn[u] = ++timestamp;
}
return;
}
for(auto it:sons[u]) {
iter(it, i - 1);//相当于BFS的过程
}
}
void DFS2(int u) {//这里的参数一定是所有重链的端点
vector<int> heavy;
for(int uu = u; uu; uu = sons[uu][0]) {
up[uu] = u;
heavy.emplace_back(uu);
if(!dfn[uu]) {
dfn[uu] = ++timestamp;
}
if(!sz(sons[uu])) {
break;
}
}
for(int i = 1; i <= 3; i++) {
for(auto uu:heavy) {
for(int j = 1; j < sz(sons[uu]); j++) {
iter(sons[uu][j], i);//从uu开始迭代三层
}
}
}
for(auto uu:heavy) {
for(int j = 1; j < sz(sons[uu]); j++) {
DFS2(sons[uu][j]);
}
}
}
void operator += (VC &A, VC B) {
for(auto it:B) {
A.emplace_back(it);
}
mg(A);
}
void DFS3(int u) {//it's really magic...
pii now = make_pair(dfn[u], dfn[u]);
kson[u][0].emplace_back(now);
sub[u].emplace_back(now);
for(auto it:sons[u]) {
DFS3(it);
sub[u] += sub[it];
for(int j = 0; j < K; j++) {
fh[it][j] += kson[u][j];
}
for(int j = 1; j < K; j++) {
kson[u][j] += kson[it][j - 1];
}
}
VC tmp[K];
for(int t = sz(sons[u]) - 1; t >= 0; t--) {
int it = sons[u][t];
for(int j = 0; j < K; j++) {
fh[it][j] += tmp[j];//倒序循环,j级儿子
}
for(int j = 1; j < K; j++) {
tmp[j] += kson[it][j - 1];
}
}
}
void DFS4(int u) {
for(int i = 0; i < K; i++) {
kans[u][i] = kson[u][i];
kans[u][i] += kans[fa[u]][i];//这是最神仙的一步!
//这里看似没有统计到距离1小于k的位置,但是由于是两个变量作差,所以没有影响。
}
for(auto it:sons[u]) {
DFS4(it);
}
for(int i = 0; i < K; i++) {
for(int j = 0; j <= i; j++) {
kdis[u][i] += kson[u][j];
}
int uu = u;
for(int j = i - 1; j >= 0; j--) {
kdis[u][i] += fh[uu][j];
uu = fa[uu];
}
}
}
VC split(int x, int y, int k) {
int l = LCA(x, y);
VC ans = getdel(x, l, k);
ans += getdel(y, l, k);
ans += kdis[l][k];
return ans;
}
inline VC get_son(int u) {
return sub[u];
}
int main() {
read(n, m);
for(int i = 1; i < n; i++) {
read(u, v);
G[u].emplace_back(v), G[v].emplace_back(u);
}
DFS1(1, 0), DFS2(1), DFS3(1), DFS4(1);
for(int i = 1; i <= m; i++) {
read(op);
switch(op) {
case 1: {//链
read(x, y, k, v);
VC now = split(x, y, k);
for(auto it:now) {
bit.add(it.first, it.second, v);
}
break;
}
case 2: {//子树
read(x, v);
VC now = get_son(x);
for(auto it:now) {
bit.add(it.first, it.second, v);
}
break;
}
case 3: {//查询链
read(x, y, k);
VC now = split(x, y, k);
ll ans = 0;
for(auto it:now) {
ans += bit.query(it.first, it.second);
}
printf("%llu\n", ans);
break;
}
case 4: {//查询子树
read(x);
VC now = get_son(x);
ll ans = 0;
for(auto it:now) {
ans += bit.query(it.first, it.second);
}
printf("%llu\n", ans);
break;
}
}
}
}
/*
start coding at:2024/2/17 19:55
finish debugging at:
stubid mistakes:没有调用DFS,跳重链的终止条件是sze非零,DFS2(sons[uu][j])而不是 DFS2(j),查询前应该找回原来的xx
*/
/*
吾日三省吾身:
你排序了吗?
你取模了吗?
你用%lld输出long long 了吗?
1LL<<x写对了吗?
判断=0用了abs()吗?
算组合数判了a<b吗?
线段树build()了吗?
.size()加了(signed)吗?
树链剖分的DFS序是在第二次更新的吗?
修改在询问前面吗?
线段树合并到叶子结点return了吗?
__builtin_ctz后面需要加ll吗?
可撤销并查集真的要路径压缩吗?
*/
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 29ms
memory: 101980kb
input:
5000 5000 1 2 2 3 3 4 4 5 5 6 5 7 6 8 7 9 9 10 8 11 11 12 12 13 12 14 14 15 15 16 16 17 16 18 18 19 18 20 20 21 20 22 22 23 23 24 23 25 23 26 26 27 27 28 28 29 27 30 30 31 29 32 32 33 33 34 32 35 35 36 36 37 35 38 36 39 38 40 38 41 41 42 42 43 43 44 42 45 44 46 45 47 47 48 48 49 48 50 49 51 51 52 52...
output:
36282180522 2134414313248 2794367845468 1308900968876 1696278722918 2678958746332 6688161965793 2086292026895 5786332239171 2186592622 4014965079076 1674542130 6524658548 7093370803092 10059064557343 11588105409348 492570862 3348941084329 2834694279 4195122521413 4395772262 4221137767 9630829210 991...
result:
wrong answer 1st numbers differ - expected: '37227703492', found: '36282180522'
Subtask #2:
score: 10
Accepted
Test #3:
score: 10
Accepted
time: 1120ms
memory: 306164kb
input:
200000 200000 1 2 1 3 1 4 3 5 1 6 1 7 7 8 8 9 2 10 1 11 5 12 1 13 7 14 10 15 2 16 7 17 11 18 5 19 5 20 1 21 16 22 1 23 3 24 20 25 14 26 2 27 6 28 15 29 10 30 15 31 5 32 13 33 12 34 31 35 31 36 36 37 36 38 1 39 28 40 5 41 12 42 41 43 20 44 30 45 22 46 11 47 47 48 45 49 14 50 41 51 3 52 30 53 29 54 6 ...
output:
0 0 0 0 0 0 0 0 7615073807 4176911055 0 4745654848 6222845818 0 0 9739142819 0 1424960716 5224818790 9459319003 13717923473 8673060864 0 11610197664 0 0 9587792729 0 0 0 2747489046 12425650803 0 0 11191496476 0 37597503993 0 0 15164651949 14868775382 15559673116 0 16391028892 0 15726757336 0 2421390...
result:
ok 100169 numbers
Test #4:
score: 0
Accepted
time: 1292ms
memory: 330176kb
input:
200000 200000 121679 2 13340 3 45206 4 112138 5 47397 6 88216 7 173469 8 109861 9 58662 10 130056 11 61155 12 4313 13 196310 14 46189 15 32349 16 143798 17 103215 18 159921 19 27365 20 14332 21 49504 22 64451 23 106931 24 59878 25 177587 26 100555 27 86848 28 793 29 79845 30 150813 31 42854 32 11551...
output:
77900221111 0 0 476439705914 0 216029652830 0 0 631267909751 508097390689 0 29277716182 695169620128 0 809294022024 0 0 829507748883 260130797154 0 1005527232590 109198360548 821333235719 0 0 1265757368752 738460021055 296232170804 845184728833 0 434366813420 0 1922343637889 0 0 0 229703081048 0 441...
result:
ok 100073 numbers
Subtask #3:
score: 0
Wrong Answer
Test #5:
score: 0
Wrong Answer
time: 465ms
memory: 256392kb
input:
200000 200000 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 20 21 21 22 22 23 23 24 24 25 25 26 26 27 27 28 28 29 29 30 30 31 31 32 32 33 33 34 34 35 35 36 36 37 37 38 38 39 39 40 40 41 41 42 42 43 43 44 44 45 45 46 46 47 47 48 48 49 49 50 50 51 51 ...
output:
0 134315028896099 38210069287857 75888509018259 25202593262786 179527248054069 75824472356207 156951569638953 246509631358547 251383207461179 181645879044485 285463143130322 213796043841569 244908385583039 53375895750478 450587856840 379327308818961 720758846292557 768646001239774 224741684687567 18...
result:
wrong answer 2nd numbers differ - expected: '134315201201061', found: '134315028896099'
Subtask #4:
score: 10
Accepted
Test #7:
score: 10
Accepted
time: 1027ms
memory: 311020kb
input:
200000 200000 1 2 2 3 3 4 1 5 3 6 5 7 5 8 7 9 2 10 7 11 11 12 10 13 6 14 3 15 14 16 4 17 11 18 3 19 14 20 4 21 4 22 12 23 18 24 5 25 5 26 14 27 13 28 24 29 11 30 26 31 29 32 28 33 31 34 23 35 33 36 6 37 11 38 22 39 13 40 35 41 37 42 21 43 12 44 4 45 16 46 12 47 21 48 1 49 26 50 45 51 41 52 46 53 7 5...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
ok 99786 numbers
Test #8:
score: 0
Accepted
time: 717ms
memory: 255068kb
input:
200000 200000 1 2 2 3 1 4 1 5 2 6 3 7 6 8 8 9 8 10 9 11 8 12 12 13 13 14 11 15 13 16 13 17 16 18 17 19 18 20 19 21 19 22 21 23 21 24 21 25 24 26 23 27 26 28 27 29 26 30 30 31 28 32 29 33 32 34 32 35 33 36 36 37 35 38 38 39 38 40 40 41 39 42 42 43 43 44 41 45 45 46 43 47 45 48 46 49 49 50 50 51 51 52...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
ok 100404 numbers
Test #9:
score: 0
Accepted
time: 1114ms
memory: 312316kb
input:
200000 200000 166945 2 60190 3 101888 4 154621 5 188595 6 175999 7 140051 8 54071 9 167394 10 54228 11 48270 12 14564 13 25727 14 138072 15 77670 16 77795 17 155644 18 171648 19 94412 20 65323 21 130225 22 6359 23 17410 24 8580 25 142556 26 152863 27 166869 28 115234 29 87099 30 160349 31 98200 32 1...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
ok 99768 numbers
Subtask #5:
score: 0
Wrong Answer
Dependency #4:
100%
Accepted
Test #10:
score: 0
Wrong Answer
time: 1079ms
memory: 310984kb
input:
200000 200000 1 2 1 3 2 4 1 5 2 6 2 7 2 8 5 9 3 10 10 11 5 12 4 13 5 14 9 15 11 16 14 17 12 18 13 19 2 20 16 21 3 22 16 23 2 24 7 25 8 26 20 27 21 28 11 29 12 30 4 31 2 32 21 33 14 34 29 35 16 36 21 37 28 38 22 39 27 40 12 41 36 42 32 43 30 44 3 45 43 46 4 47 14 48 44 49 9 50 37 51 20 52 11 53 31 54...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
wrong answer 348th numbers differ - expected: '1896761708', found: '948380854'
Subtask #6:
score: 0
Skipped
Dependency #4:
100%
Accepted
Dependency #5:
0%
Subtask #7:
score: 15
Accepted
Dependency #2:
100%
Accepted
Dependency #4:
100%
Accepted
Test #17:
score: 15
Accepted
time: 1127ms
memory: 308804kb
input:
200000 200000 1 2 1 3 3 4 1 5 2 6 3 7 1 8 5 9 6 10 9 11 2 12 8 13 3 14 4 15 12 16 10 17 2 18 2 19 14 20 12 21 9 22 19 23 14 24 3 25 13 26 21 27 11 28 5 29 9 30 13 31 13 32 4 33 6 34 14 35 14 36 31 37 13 38 10 39 4 40 28 41 13 42 14 43 20 44 37 45 8 46 14 47 32 48 21 49 40 50 46 51 20 52 44 53 15 54 ...
output:
0 12712803164 0 0 8193968417 14426787309 12950092190 0 32958618613 26764906955 8493139167 0 42785505564 55243799113 0 0 0 0 0 16857343623 27991363871 14151995536 55690427136 85838206842 0 26020925273 22490232861 0 0 1662166464 24948089565 0 26561191640 202942896717 0 91771091757 152033022013 2500525...
result:
ok 100098 numbers
Test #18:
score: 0
Accepted
time: 987ms
memory: 260404kb
input:
200000 200000 1 2 1 3 2 4 2 5 3 6 3 7 4 8 4 9 5 10 5 11 6 12 6 13 7 14 7 15 8 16 8 17 9 18 9 19 10 20 10 21 11 22 11 23 12 24 12 25 13 26 13 27 14 28 14 29 15 30 15 31 16 32 16 33 17 34 17 35 18 36 18 37 19 38 19 39 20 40 20 41 21 42 21 43 22 44 22 45 23 46 23 47 24 48 24 49 25 50 25 51 26 52 26 53 ...
output:
1506 2433795 1964764 29809 23904 32308 3956297 23683 26194 24496 0 5108618 44498 10494148 1493614 8948 11034059 10983525 9340774 68288 15623342 65908 14673691 63760 62016 68610 3785842 68409 71500 4760847 1843709 83560 86276 3800228 24415019 102280 92867 17894104 25995202 96677 14391781 110124 12872...
result:
ok 99928 numbers
Test #19:
score: 0
Accepted
time: 1156ms
memory: 298044kb
input:
200000 200000 1 2 1 3 2 4 2 5 3 6 3 7 4 8 4 9 5 10 5 11 6 12 6 13 7 14 7 15 8 16 8 17 9 18 9 19 10 20 10 21 11 22 11 23 12 24 12 25 13 26 13 27 14 28 14 29 15 30 15 31 16 32 16 33 17 34 17 35 18 36 18 37 19 38 19 39 20 40 20 41 21 42 21 43 22 44 22 45 23 46 23 47 24 48 24 49 25 50 25 51 26 52 26 53 ...
output:
0 6432 5572 2146 5686 15024 5924 10437 10836 16481 12732 37909 15933 53994 38493 121364 89269 36850 69524 53517 239542 137860 93122 118688 151902 9279 120973 73158 28901 122575 43850 21781 119189 28818 71155 127221 157054 65948 40570 183418 150579 145757 147361 89223 4664 78041 329787 100673 50134 6...
result:
ok 100068 numbers
Subtask #8:
score: 0
Skipped
Dependency #1:
0%