QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#307408#7439. 铃原露露OccDreamer0 652ms108856kbC++144.1kb2024-01-18 15:37:472024-01-18 15:37:48

Judging History

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

  • [2024-01-18 15:37:48]
  • 评测
  • 测评结果:0
  • 用时:652ms
  • 内存:108856kb
  • [2024-01-18 15:37:47]
  • 提交

answer

//code by Nobody.Emissary
#include<bits/stdc++.h>

#define fi first
#define se second
#define vc vector
#define db double
#define ll long long
#define mk make_pair
#define pb push_back
#define PI pair<int,int>
#define ull unsigned long long
#define err cerr << "   -_-   " << endl
#define debug cerr << " ------------------- " << endl

#define input(x) freopen(#x".in","r",stdin)
#define output(x) freopen(#x".out","w",stdout)

#define NO puts("No")
#define YES puts("Yes")

//#define int long long

using namespace std;

namespace IO{
	inline int read(){
		int X=0, W=0; char ch=getchar();
		while(!isdigit(ch)) W|=ch=='-', ch=getchar();
		while(isdigit(ch)) X=(X<<1)+(X<<3)+(ch^48), ch=getchar();
		return W?-X:X;
	}
	inline void write(ll x){
		if(x<0) x=-x, putchar('-');
		if(x>9) write(x/10);
		putchar(x%10+'0');
	}
	inline void sprint(ll x){write(x), putchar(32);}
	inline void eprint(ll x){write(x), putchar(10);}
}using namespace IO;

const int MAXN = 2e5+5;
const int mod = 998244353;
//const int mod = 1e9+7;

struct op{
	int l, r, add;
};

struct Qask{
	int l, r, id, v;
};

vc<op> Add[MAXN];
vc<Qask> Q[MAXN];

int n, m, id[MAXN], a[MAXN];
int head[MAXN], ne[MAXN<<1], to[MAXN<<1], ecnt;

set<int> S[MAXN];
set<int> :: iterator it;

ll ans[MAXN<<2], tree[MAXN<<2], tag[MAXN<<2], laz[MAXN<<2], cnt[MAXN<<2], sum[MAXN<<2];

inline void add(int x, int y){++ecnt;to[ecnt]=y;ne[ecnt]=head[x];head[x]=ecnt;}

inline void pushup(int p){
	sum[p]=sum[p<<1]+sum[p<<1|1];
	tree[p]=min(tree[p<<1],tree[p<<1|1]);
	cnt[p]=(tree[p]==tree[p<<1])*cnt[p<<1]+(tree[p]==tree[p<<1|1])*cnt[p<<1|1];
	return ;
}

inline void pushdown(int p){
	laz[p<<1]+=laz[p];
	laz[p<<1|1]+=laz[p];
	tree[p<<1]+=laz[p];
	tree[p<<1|1]+=laz[p];
	if(tag[p]){
		if(tree[p<<1]==tree[p]) tag[p<<1]+=tag[p], sum[p<<1]+=1ll*cnt[p<<1]*tag[p];
		if(tree[p<<1|1]==tree[p]) tag[p<<1|1]+=tag[p], sum[p<<1|1]+=1ll*cnt[p<<1|1]*tag[p];
		tag[p]=0; 
	}
	laz[p]=0;
	return ;
}

inline void add(int p, int l, int r, int L, int R, int v){
	if(L>R) return ;
	if(L<=l && r<=R){
		tree[p]+=v; laz[p]+=v;
		return ;
	}
	int mid=(l+r)>>1; pushdown(p);
	if(L<=mid) add(p<<1,l,mid,L,R,v);
	if(R>mid) add(p<<1|1,mid+1,r,L,R,v);
	return pushup(p);
}

inline ll ask(int p, int l, int r, int L, int R){
	if(L<=l && r<=R) return sum[p];
	int mid=(l+r)>>1; ll s=0; pushdown(p);
	if(L<=mid) s+=ask(p<<1,l,mid,L,R);
	if(R>mid) s+=ask(p<<1|1,mid+1,r,L,R);
	return s; 
}

inline void build(int p, int l, int r){
	if(l==r) return tree[p]=0, cnt[p]=1, void();
	int mid=(l+r)>>1;
	build(p<<1,l,mid); build(p<<1|1,mid+1,r);
	return pushup(p);
}

inline void dfs(int x){
	int val=a[x]; vc<int> son; int cur, t, v;
	for(int i=head[x];i;i=ne[i]) dfs(to[i]), son.pb(to[i]);
	if(son.size()==0) return ;
	cur=id[son.back()]; son.pop_back();
	for(auto i:son){
		t=id[i];
		if(S[cur].size()<S[t].size()) swap(cur,t);
		for(auto j:S[t]){
			it=S[cur].lower_bound(j);
			if(it!=S[cur].end()){
				v=*it;
				if(j>val) Add[val+1].pb(op{v,n,1}), Add[j+1].pb(op{v,n,-1});
				else Add[1].pb(op{v,val-1,1}), Add[j+1].pb(op{v,val-1,-1});
			}
			if(it!=S[cur].begin()){
				--it;
				v=*it;
				if(j>val) Add[val+1].pb(op{j,n,1}), Add[v+1].pb(op{j,n,-1});
				else Add[1].pb(op{j,val-1,1}), Add[v+1].pb(op{j,val-1,-1});
			}
		}
		for(auto j:S[t]) S[cur].insert(j);
		S[t].clear(); 
	}
	S[cur].insert(a[x]); id[x]=cur;
	return ;
}

signed main(){
	n=read(), m=read(); int f;
	for(int i=1;i<=n;++i) a[i]=read();
	for(int i=2;i<=n;++i) f=read(), add(f,i);
	for(int i=1;i<=n;++i) id[i]=i, S[i].insert(a[i]); dfs(1);
	for(int i=1;i<=m;++i){
		int l, r;
		l=read(), r=read();
		Q[l-1].pb(Qask{l,r,i,-1});
		Q[r].pb(Qask{l,r,i,1});
	}
	build(1,1,n);
	for(int i=2;i<=n;++i) Add[i].pb(op{i-1,i-1,1});
	for(int i=1;i<=n;++i){
		for(auto j:Add[i]) add(1,1,n,j.l,j.r,j.add);
		if(tree[1]==0) tag[1]++, sum[1]+=cnt[1];
		for(auto j:Q[i]) ans[j.id]+=j.v*ask(1,1,n,j.l,j.r);
	}
	for(int i=1;i<=m;++i) eprint(ans[i]);
	return 0;
}





































































詳細信息

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 33368kb

input:

100 100
5 29 12 16 25 36 18 37 27 47 34 40 20 3 1 42 26 19 33 41 6 22 8 58 32 62 24 15 35 17 59 30 50 61 43 49 39 67 44 21 13 31 68 69 65 64 10 28 38 54 70 63 9 46 66 52 23 7 48 60 55 56 51 2 57 11 53 14 45 4 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
...

output:

9
85
37
4
13
13
46
17
173
32
44
29
66
38
21
28
43
111
32
35
36
39
83
28
8
62
35
67
1
42
102
5
43
38
4
51
6
14
84
16
61
34
76
7
43
72
82
17
4
51
72
310
78
26
6
54
27
4
81
11
20
39
8
192
76
67
73
66
51
16
2
13
6
47
80
47
11
89
60
57
97
79
90
7
1
41
90
7
22
53
84
56
32
17
27
81
70
10
24
26

result:

wrong answer 2nd numbers differ - expected: '87', found: '85'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Wrong Answer

Test #21:

score: 0
Wrong Answer
time: 652ms
memory: 108856kb

input:

200000 1
73119 155820 110077 139724 136809 18709 57745 43535 89117 43647 20295 60551 108184 188031 180437 52363 72969 130559 179796 75852 53879 96998 63387 76458 193661 142318 28260 40465 80050 188507 143795 141018 94880 71333 7644 109237 105208 109509 9779 159914 135096 47638 175577 182927 173100 1...

output:

216931

result:

wrong answer 1st numbers differ - expected: '216932', found: '216931'

Subtask #4:

score: 0
Skipped

Dependency #1:

0%