답안 #879716

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
879716 2023-11-28T01:31:04 Z vjudge1 Tourism (JOI23_tourism) C++17
100 / 100
1420 ms 39248 KB
#include<stdio.h>
#include<algorithm>
#include<string.h>
#define N 100009
#define LG 19
#define B 9
#define pr pair<int*,int> 
using namespace std;
inline char nc()
{
	static char buf[99999],*l,*r;
	return l==r&&(r=(l=buf)+fread(buf,1,99999,stdin),l==r)?EOF:*l++;
}
inline void read(int&x)
{
	char c=nc();for(;c<'0'||'9'<c;c=nc());
	for(x=0;'0'<=c&&c<='9';x=(x<<3)+(x<<1)+(c^48),c=nc());
}
int n,m,q,h[N],e[N<<1],nxt[N<<1],dfn[N],now,st[LG][N*3],lg[N*3];
int dep[N],a[N],cnt[N*3],lft[N*3],rgt[N*3],l,r,rsz;
long long ss,sss,ans[N];pr rbk[N*3];
inline void dfs(int i,int f)
{
	dfn[i]=now;st[0][now++]=dep[i]=dep[f]+1;
	for(int j=h[i];j;j=nxt[j])if(e[j]^f)dfs(e[j],i),st[0][now++]=dep[i];
}
struct node
{
	int id,l,r;
	inline bool operator<(node kkk)const
		{return l>>B^kkk.l>>B?l<kkk.l:r>kkk.r;}
}b[N];
inline int qry(int l,int r)
{
	if(l>r)l^=r^=l^=r;
	int i=lg[r-l];
	return min(st[i][l],st[i][r-(1<<i)+1]);
}
main()
{
	read(n);read(m);read(q);
	for(int i=2;i<=n*3;++i)lg[i]=lg[i>>1]+1;
	for(int i=1,u,v;i<n;++i)
		read(u),read(v),--u,--v,
		e[i]=v,nxt[i]=h[u],h[u]=i,
		e[i+n]=u,nxt[i+n]=h[v],h[v]=i+n;
	dfs(0,0);
	for(int i=now-1;i>=0;--i)for(int j=1;j<LG;++j)
		if(i+(1<<j-1)>=now)st[j][i]=st[j-1][i];
		else st[j][i]=min(st[j-1][i],st[j-1][i+(1<<j-1)]);
	for(int i=0;i<m;++i)read(a[i]),a[i]=dfn[--a[i]];
	for(int i=0;i<q;++i)b[i].id=i,read(b[i].l),read(b[i].r),
		--b[i].l,--b[i].r;
	sort(b,b+q);
	for(int i=0;i<q;++i)
	{
		if(!i||(b[i].l>>B^b[i-1].l>>B))
		{
			memset(cnt,0,sizeof(cnt));
			l=b[i].l>>B<<B;r=m-1;ss=0;
			for(int j=l;j<=r;++cnt[a[j++]]);
			int lst;
			for(lst=now-1;!cnt[lst];--lst);
			for(int j=0;j<now;++j)if(cnt[j])lft[j]=lst,lst=j;
			for(lst=0;!cnt[lst];++lst);
			for(int j=now-1;j>=0;--j)if(cnt[j])rgt[j]=lst,lst=j;
			for(int j=0;j<now;++j)if(cnt[j])
				ss+=st[0][j]-qry(j,rgt[j]);
		}
		for(int j;r>b[i].r;--r)if(!--cnt[j=a[r]])
			ss-=st[0][j],
			ss-=qry(lft[j],rgt[j])-qry(lft[j],j)-qry(j,rgt[j]),
			rgt[lft[j]]=rgt[j],lft[rgt[j]]=lft[j];
		sss=ss;rsz=0;
		for(int j;l<b[i].l;++l)
		{
			j=a[l];
			rbk[rsz++]=(pr){cnt+j,cnt[j]};
			if(!--cnt[j])
			{
				sss-=st[0][j],
				sss-=qry(lft[j],rgt[j])-qry(lft[j],j)-qry(j,rgt[j]);
				rbk[rsz++]=(pr){rgt+lft[j],rgt[lft[j]]};
				rbk[rsz++]=(pr){lft+rgt[j],lft[rgt[j]]};
				rgt[lft[j]]=rgt[j],lft[rgt[j]]=lft[j];
			}
		}
		ans[b[i].id]=sss;
		for(l=b[i].l>>B<<B;rsz--;*rbk[rsz].first=rbk[rsz].second);
	}
	for(int i=0;i<q;printf("%lld\n",ans[i++]+1));
}

Compilation message

tourism.cpp:39:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   39 | main()
      | ^~~~
tourism.cpp: In function 'int main()':
tourism.cpp:49:13: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   49 |   if(i+(1<<j-1)>=now)st[j][i]=st[j-1][i];
      |            ~^~
tourism.cpp:50:47: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   50 |   else st[j][i]=min(st[j-1][i],st[j-1][i+(1<<j-1)]);
      |                                              ~^~
tourism.cpp:51:37: warning: operation on 'a[i]' may be undefined [-Wsequence-point]
   51 |  for(int i=0;i<m;++i)read(a[i]),a[i]=dfn[--a[i]];
      |                                 ~~~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 29788 KB Output is correct
2 Correct 4 ms 29788 KB Output is correct
3 Correct 4 ms 29644 KB Output is correct
4 Correct 4 ms 29696 KB Output is correct
5 Correct 4 ms 30040 KB Output is correct
6 Correct 4 ms 29788 KB Output is correct
7 Correct 4 ms 29788 KB Output is correct
8 Correct 4 ms 29788 KB Output is correct
9 Correct 4 ms 29788 KB Output is correct
10 Correct 4 ms 29788 KB Output is correct
11 Correct 4 ms 29788 KB Output is correct
12 Correct 3 ms 29788 KB Output is correct
13 Correct 4 ms 29788 KB Output is correct
14 Correct 3 ms 29788 KB Output is correct
15 Correct 4 ms 29784 KB Output is correct
16 Correct 4 ms 29788 KB Output is correct
17 Correct 4 ms 29788 KB Output is correct
18 Correct 4 ms 29872 KB Output is correct
19 Correct 4 ms 29784 KB Output is correct
20 Correct 4 ms 29788 KB Output is correct
21 Correct 4 ms 29788 KB Output is correct
22 Correct 6 ms 29788 KB Output is correct
23 Correct 4 ms 29788 KB Output is correct
24 Correct 4 ms 29788 KB Output is correct
25 Correct 4 ms 29788 KB Output is correct
26 Correct 4 ms 29788 KB Output is correct
27 Correct 4 ms 27740 KB Output is correct
28 Correct 3 ms 29888 KB Output is correct
29 Correct 4 ms 29788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 29788 KB Output is correct
2 Correct 4 ms 29788 KB Output is correct
3 Correct 4 ms 29644 KB Output is correct
4 Correct 4 ms 29696 KB Output is correct
5 Correct 4 ms 30040 KB Output is correct
6 Correct 4 ms 29788 KB Output is correct
7 Correct 4 ms 29788 KB Output is correct
8 Correct 4 ms 29788 KB Output is correct
9 Correct 4 ms 29788 KB Output is correct
10 Correct 4 ms 29788 KB Output is correct
11 Correct 4 ms 29788 KB Output is correct
12 Correct 3 ms 29788 KB Output is correct
13 Correct 4 ms 29788 KB Output is correct
14 Correct 3 ms 29788 KB Output is correct
15 Correct 4 ms 29784 KB Output is correct
16 Correct 4 ms 29788 KB Output is correct
17 Correct 4 ms 29788 KB Output is correct
18 Correct 4 ms 29872 KB Output is correct
19 Correct 4 ms 29784 KB Output is correct
20 Correct 4 ms 29788 KB Output is correct
21 Correct 4 ms 29788 KB Output is correct
22 Correct 6 ms 29788 KB Output is correct
23 Correct 4 ms 29788 KB Output is correct
24 Correct 4 ms 29788 KB Output is correct
25 Correct 4 ms 29788 KB Output is correct
26 Correct 4 ms 29788 KB Output is correct
27 Correct 4 ms 27740 KB Output is correct
28 Correct 3 ms 29888 KB Output is correct
29 Correct 4 ms 29788 KB Output is correct
30 Correct 9 ms 29788 KB Output is correct
31 Correct 11 ms 29996 KB Output is correct
32 Correct 13 ms 30060 KB Output is correct
33 Correct 13 ms 30044 KB Output is correct
34 Correct 13 ms 30044 KB Output is correct
35 Correct 4 ms 30044 KB Output is correct
36 Correct 4 ms 30044 KB Output is correct
37 Correct 5 ms 30044 KB Output is correct
38 Correct 13 ms 30044 KB Output is correct
39 Correct 12 ms 30088 KB Output is correct
40 Correct 12 ms 30096 KB Output is correct
41 Correct 4 ms 29912 KB Output is correct
42 Correct 4 ms 30084 KB Output is correct
43 Correct 4 ms 30044 KB Output is correct
44 Correct 12 ms 30064 KB Output is correct
45 Correct 13 ms 30040 KB Output is correct
46 Correct 12 ms 30044 KB Output is correct
47 Correct 5 ms 30044 KB Output is correct
48 Correct 4 ms 30028 KB Output is correct
49 Correct 5 ms 30044 KB Output is correct
50 Correct 13 ms 30064 KB Output is correct
51 Correct 12 ms 30044 KB Output is correct
52 Correct 13 ms 30044 KB Output is correct
53 Correct 12 ms 30048 KB Output is correct
54 Correct 12 ms 30052 KB Output is correct
55 Correct 13 ms 30040 KB Output is correct
56 Correct 6 ms 27928 KB Output is correct
57 Correct 4 ms 29788 KB Output is correct
58 Correct 16 ms 30044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 29788 KB Output is correct
2 Correct 3 ms 27820 KB Output is correct
3 Correct 7 ms 27740 KB Output is correct
4 Correct 661 ms 35900 KB Output is correct
5 Correct 612 ms 36620 KB Output is correct
6 Correct 687 ms 37460 KB Output is correct
7 Correct 1120 ms 39112 KB Output is correct
8 Correct 1071 ms 39116 KB Output is correct
9 Correct 1081 ms 39248 KB Output is correct
10 Correct 1077 ms 39248 KB Output is correct
11 Correct 1072 ms 39120 KB Output is correct
12 Correct 158 ms 38892 KB Output is correct
13 Correct 168 ms 38888 KB Output is correct
14 Correct 156 ms 38888 KB Output is correct
15 Correct 30 ms 37180 KB Output is correct
16 Correct 1284 ms 38740 KB Output is correct
17 Correct 194 ms 30812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 29788 KB Output is correct
2 Correct 180 ms 32152 KB Output is correct
3 Correct 375 ms 32572 KB Output is correct
4 Correct 261 ms 32344 KB Output is correct
5 Correct 21 ms 33620 KB Output is correct
6 Correct 24 ms 33592 KB Output is correct
7 Correct 42 ms 33524 KB Output is correct
8 Correct 97 ms 33668 KB Output is correct
9 Correct 284 ms 33908 KB Output is correct
10 Correct 536 ms 33692 KB Output is correct
11 Correct 520 ms 33700 KB Output is correct
12 Correct 580 ms 33872 KB Output is correct
13 Correct 644 ms 34132 KB Output is correct
14 Correct 865 ms 34388 KB Output is correct
15 Correct 1385 ms 36004 KB Output is correct
16 Correct 306 ms 34080 KB Output is correct
17 Correct 337 ms 33908 KB Output is correct
18 Correct 351 ms 33928 KB Output is correct
19 Correct 18 ms 33624 KB Output is correct
20 Correct 20 ms 33624 KB Output is correct
21 Correct 29 ms 33580 KB Output is correct
22 Correct 45 ms 33716 KB Output is correct
23 Correct 70 ms 33724 KB Output is correct
24 Correct 133 ms 33720 KB Output is correct
25 Correct 256 ms 33620 KB Output is correct
26 Correct 491 ms 33732 KB Output is correct
27 Correct 491 ms 33620 KB Output is correct
28 Correct 514 ms 33736 KB Output is correct
29 Correct 537 ms 33776 KB Output is correct
30 Correct 576 ms 33840 KB Output is correct
31 Correct 605 ms 33880 KB Output is correct
32 Correct 659 ms 34180 KB Output is correct
33 Correct 935 ms 34876 KB Output is correct
34 Correct 1320 ms 36072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 29788 KB Output is correct
2 Correct 4 ms 27740 KB Output is correct
3 Correct 6 ms 27740 KB Output is correct
4 Correct 831 ms 35208 KB Output is correct
5 Correct 811 ms 35116 KB Output is correct
6 Correct 1083 ms 36004 KB Output is correct
7 Correct 1173 ms 36516 KB Output is correct
8 Correct 1142 ms 36440 KB Output is correct
9 Correct 1155 ms 36504 KB Output is correct
10 Correct 1125 ms 36692 KB Output is correct
11 Correct 1174 ms 36500 KB Output is correct
12 Correct 1186 ms 36504 KB Output is correct
13 Correct 1164 ms 36500 KB Output is correct
14 Correct 197 ms 30808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 29788 KB Output is correct
2 Correct 4 ms 29788 KB Output is correct
3 Correct 4 ms 29644 KB Output is correct
4 Correct 4 ms 29696 KB Output is correct
5 Correct 4 ms 30040 KB Output is correct
6 Correct 4 ms 29788 KB Output is correct
7 Correct 4 ms 29788 KB Output is correct
8 Correct 4 ms 29788 KB Output is correct
9 Correct 4 ms 29788 KB Output is correct
10 Correct 4 ms 29788 KB Output is correct
11 Correct 4 ms 29788 KB Output is correct
12 Correct 3 ms 29788 KB Output is correct
13 Correct 4 ms 29788 KB Output is correct
14 Correct 3 ms 29788 KB Output is correct
15 Correct 4 ms 29784 KB Output is correct
16 Correct 4 ms 29788 KB Output is correct
17 Correct 4 ms 29788 KB Output is correct
18 Correct 4 ms 29872 KB Output is correct
19 Correct 4 ms 29784 KB Output is correct
20 Correct 4 ms 29788 KB Output is correct
21 Correct 4 ms 29788 KB Output is correct
22 Correct 6 ms 29788 KB Output is correct
23 Correct 4 ms 29788 KB Output is correct
24 Correct 4 ms 29788 KB Output is correct
25 Correct 4 ms 29788 KB Output is correct
26 Correct 4 ms 29788 KB Output is correct
27 Correct 4 ms 27740 KB Output is correct
28 Correct 3 ms 29888 KB Output is correct
29 Correct 4 ms 29788 KB Output is correct
30 Correct 9 ms 29788 KB Output is correct
31 Correct 11 ms 29996 KB Output is correct
32 Correct 13 ms 30060 KB Output is correct
33 Correct 13 ms 30044 KB Output is correct
34 Correct 13 ms 30044 KB Output is correct
35 Correct 4 ms 30044 KB Output is correct
36 Correct 4 ms 30044 KB Output is correct
37 Correct 5 ms 30044 KB Output is correct
38 Correct 13 ms 30044 KB Output is correct
39 Correct 12 ms 30088 KB Output is correct
40 Correct 12 ms 30096 KB Output is correct
41 Correct 4 ms 29912 KB Output is correct
42 Correct 4 ms 30084 KB Output is correct
43 Correct 4 ms 30044 KB Output is correct
44 Correct 12 ms 30064 KB Output is correct
45 Correct 13 ms 30040 KB Output is correct
46 Correct 12 ms 30044 KB Output is correct
47 Correct 5 ms 30044 KB Output is correct
48 Correct 4 ms 30028 KB Output is correct
49 Correct 5 ms 30044 KB Output is correct
50 Correct 13 ms 30064 KB Output is correct
51 Correct 12 ms 30044 KB Output is correct
52 Correct 13 ms 30044 KB Output is correct
53 Correct 12 ms 30048 KB Output is correct
54 Correct 12 ms 30052 KB Output is correct
55 Correct 13 ms 30040 KB Output is correct
56 Correct 6 ms 27928 KB Output is correct
57 Correct 4 ms 29788 KB Output is correct
58 Correct 16 ms 30044 KB Output is correct
59 Correct 3 ms 29788 KB Output is correct
60 Correct 3 ms 27820 KB Output is correct
61 Correct 7 ms 27740 KB Output is correct
62 Correct 661 ms 35900 KB Output is correct
63 Correct 612 ms 36620 KB Output is correct
64 Correct 687 ms 37460 KB Output is correct
65 Correct 1120 ms 39112 KB Output is correct
66 Correct 1071 ms 39116 KB Output is correct
67 Correct 1081 ms 39248 KB Output is correct
68 Correct 1077 ms 39248 KB Output is correct
69 Correct 1072 ms 39120 KB Output is correct
70 Correct 158 ms 38892 KB Output is correct
71 Correct 168 ms 38888 KB Output is correct
72 Correct 156 ms 38888 KB Output is correct
73 Correct 30 ms 37180 KB Output is correct
74 Correct 1284 ms 38740 KB Output is correct
75 Correct 194 ms 30812 KB Output is correct
76 Correct 4 ms 29788 KB Output is correct
77 Correct 180 ms 32152 KB Output is correct
78 Correct 375 ms 32572 KB Output is correct
79 Correct 261 ms 32344 KB Output is correct
80 Correct 21 ms 33620 KB Output is correct
81 Correct 24 ms 33592 KB Output is correct
82 Correct 42 ms 33524 KB Output is correct
83 Correct 97 ms 33668 KB Output is correct
84 Correct 284 ms 33908 KB Output is correct
85 Correct 536 ms 33692 KB Output is correct
86 Correct 520 ms 33700 KB Output is correct
87 Correct 580 ms 33872 KB Output is correct
88 Correct 644 ms 34132 KB Output is correct
89 Correct 865 ms 34388 KB Output is correct
90 Correct 1385 ms 36004 KB Output is correct
91 Correct 306 ms 34080 KB Output is correct
92 Correct 337 ms 33908 KB Output is correct
93 Correct 351 ms 33928 KB Output is correct
94 Correct 18 ms 33624 KB Output is correct
95 Correct 20 ms 33624 KB Output is correct
96 Correct 29 ms 33580 KB Output is correct
97 Correct 45 ms 33716 KB Output is correct
98 Correct 70 ms 33724 KB Output is correct
99 Correct 133 ms 33720 KB Output is correct
100 Correct 256 ms 33620 KB Output is correct
101 Correct 491 ms 33732 KB Output is correct
102 Correct 491 ms 33620 KB Output is correct
103 Correct 514 ms 33736 KB Output is correct
104 Correct 537 ms 33776 KB Output is correct
105 Correct 576 ms 33840 KB Output is correct
106 Correct 605 ms 33880 KB Output is correct
107 Correct 659 ms 34180 KB Output is correct
108 Correct 935 ms 34876 KB Output is correct
109 Correct 1320 ms 36072 KB Output is correct
110 Correct 4 ms 29788 KB Output is correct
111 Correct 4 ms 27740 KB Output is correct
112 Correct 6 ms 27740 KB Output is correct
113 Correct 831 ms 35208 KB Output is correct
114 Correct 811 ms 35116 KB Output is correct
115 Correct 1083 ms 36004 KB Output is correct
116 Correct 1173 ms 36516 KB Output is correct
117 Correct 1142 ms 36440 KB Output is correct
118 Correct 1155 ms 36504 KB Output is correct
119 Correct 1125 ms 36692 KB Output is correct
120 Correct 1174 ms 36500 KB Output is correct
121 Correct 1186 ms 36504 KB Output is correct
122 Correct 1164 ms 36500 KB Output is correct
123 Correct 197 ms 30808 KB Output is correct
124 Correct 989 ms 36324 KB Output is correct
125 Correct 659 ms 34820 KB Output is correct
126 Correct 1192 ms 36528 KB Output is correct
127 Correct 1297 ms 36508 KB Output is correct
128 Correct 1118 ms 36516 KB Output is correct
129 Correct 1177 ms 36692 KB Output is correct
130 Correct 1134 ms 36508 KB Output is correct
131 Correct 1093 ms 38724 KB Output is correct
132 Correct 1070 ms 39248 KB Output is correct
133 Correct 1074 ms 37816 KB Output is correct
134 Correct 1219 ms 36576 KB Output is correct
135 Correct 1109 ms 36564 KB Output is correct
136 Correct 1167 ms 36552 KB Output is correct
137 Correct 1144 ms 36524 KB Output is correct
138 Correct 1156 ms 36364 KB Output is correct
139 Correct 1164 ms 36536 KB Output is correct
140 Correct 1155 ms 36536 KB Output is correct
141 Correct 1162 ms 36776 KB Output is correct
142 Correct 1211 ms 36660 KB Output is correct
143 Correct 25 ms 34284 KB Output is correct
144 Correct 1420 ms 35996 KB Output is correct