QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#95243 | #6127. Kawa Exam | George_Plover | AC ✓ | 991ms | 43644kb | C++14 | 5.4kb | 2023-04-09 12:44:02 | 2023-04-09 12:44:06 |
Judging History
answer
#include <map>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define PII pair<int,int>
#define vint vector<int>
#define X first
#define Y second
#define MAXN 110000
#define MAXM 210000
using namespace std;
struct Segment_Tree{
struct node{
int l,r;
int v;
}tr[MAXN*4];
int num;
void init(){
num=1;
}
void pushup(int x){
tr[x].v=max(tr[tr[x].l].v,tr[tr[x].r].v);
}
void Build(int x,int L,int R){
if(L==R){
tr[x].v=0;
return;
}
int mid=(L+R)/2;
tr[x].l=++num;
Build(tr[x].l,L,mid);
tr[x].r=++num;
Build(tr[x].r,mid+1,R);
pushup(x);
}
void Update(int x,int L,int R,int aim,int v){
if(L==R){
tr[x].v+=v;
return;
}
int mid=(L+R)/2;
if(aim<=mid)Update(tr[x].l,L,mid,aim,v);
else Update(tr[x].r,mid+1,R,aim,v);
pushup(x);
}
}seg,sub;
int T,n,m;
int a[MAXN];
bool is_bridge[MAXN],vis[MAXN];
int low[MAXN],rloc[MAXN],dfn[MAXN],num,stk[MAXN],cnt;
int tot=1,pre[MAXN],to[MAXM],lin[MAXM],eid[MAXM];
int SCC,belong[MAXN];
map<int,int> b[MAXN];
PII p[MAXN];
vint son[MAXN],sid[MAXN];
int siz[MAXN],loc[MAXN],hson[MAXN],fa[MAXN],anti[MAXN];
int ans,out[MAXN];
void init(){
cnt=num=SCC=ans=0;
tot=1;
for(int i=1;i<=n;i++){
pre[i]=0;
dfn[i]=0;
b[i].clear();
belong[i]=0;
son[i].clear();sid[i].clear();
loc[i]=hson[i]=fa[i]=rloc[i]=0;
vis[i]=0;
}
for(int i=1;i<=m;i++){
is_bridge[i]=0;
out[i]=0;
}
}
void add(int x,int y,int z){
tot++;lin[tot]=pre[x];pre[x]=tot;to[tot]=y;eid[tot]=z;
}
void tarjan(int x,int in_edge){
low[x]=dfn[x]=++num;
stk[++cnt]=x;
for(int i=pre[x];i;i=lin[i]){
int v=to[i];
if(!dfn[v]){
tarjan(v,i);
low[x]=min(low[x],low[v]);
if(low[v]>dfn[x]){
is_bridge[eid[i]]=1;
SCC++;
int t;
do{
t=stk[cnt--];
belong[t]=SCC;
b[SCC][a[t]]++;
}while(t!=v);
}
}
else if(i!=(in_edge^1)){
low[x]=min(low[x],dfn[v]);
}
}
}
void dfs(int x,map<int,int> &CNT){
vis[x]=1;
loc[x]=++num;
anti[num]=x;
siz[x]=b[x].size();
for(auto &i: b[x]){
CNT[i.first]+=i.second;
}
for(auto &v:son[x]){
if(v!=fa[x]){
fa[v]=x;
dfs(v,CNT);
siz[x]+=siz[v];
if(siz[hson[x]]<siz[v])
hson[x]=v;
}
}
rloc[x]=num;
}
void deal_sub_tree(int x,int sig){
for(int i=loc[x];i<=rloc[x];i++){
int y=anti[i];
for(auto &j: b[y]){
seg.Update(1,1,MAXN,j.X,j.Y*sig);
sub.Update(1,1,MAXN,j.X,-j.Y*sig);
}
}
}
int TMP_ANS;
void dsu(int x,int in){
int t=0,hsonid;
for(auto &v:son[x]){
if(v!=hson[x]){
dsu(v,sid[x][t]);
deal_sub_tree(v,-1);
}
else hsonid = sid[x][t];
t++;
}
if(hson[x]){
dsu(hson[x],hsonid);
}
for(auto &v:son[x]){
if(v!=hson[x]){
deal_sub_tree(v,1);
}
}
for(auto &i: b[x]){
seg.Update(1,1,MAXN,i.X,i.Y);
sub.Update(1,1,MAXN,i.X,-i.Y);
}
//cout<<x<<" "<<seg.tr[1].v<<" "<<sub.tr[1].v<<endl;
if(in){
out[in] += (-TMP_ANS)+seg.tr[1].v+sub.tr[1].v;
}
}
void work(){
for(int i=1;i<=n;i++){
if(!vis[belong[i]]){
//cout<<i<<endl;
num=0;
map<int,int> CNT;
dfs(belong[i],CNT);
int t=0;
for(auto &j: CNT){
t=max(t,j.second);
sub.Update(1,1,MAXN,j.first,j.second);
}
TMP_ANS = t;
ans+=t;
dsu(belong[i],0);
for(auto &j: CNT){
seg.Update(1,1,MAXN,j.first,-j.second);
}
}
}
}
int main(){
scanf("%d",&T);
seg.init();sub.init();
seg.Build(1,1,MAXN);sub.Build(1,1,MAXN);
while(T--){
scanf("%d%d",&n,&m);
init();
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
for(int i=1;i<=m;i++){
scanf("%d%d",&p[i].X,&p[i].Y);
add(p[i].X,p[i].Y,i);
add(p[i].Y,p[i].X,i);
}
for(int i=1;i<=n;i++){
if(!dfn[i]){
tarjan(i,0);
SCC++;
while(cnt){
int t=stk[cnt--];
belong[t]=SCC;
b[SCC][a[t]]++;
}
}
}
for(int i=1;i<=n;i++){
//cout<<i<<" "<<belong[i]<<endl;
for(int j=pre[i];j;j=lin[j]){
int u=i,v=to[j];
int U = belong[u], V = belong[v];
if(U>V){
son[U].push_back(V);
sid[U].push_back(eid[j]);
}
}
}
work();
for(int i=1;i<=m;i++){
printf("%d%c",ans+out[i],i==m?'\n':' ');
}
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 4ms
memory: 19448kb
input:
3 7 5 1 2 1 2 1 2 1 1 2 1 3 2 4 5 6 5 7 3 3 1 2 3 1 2 1 3 2 3 2 3 12345 54321 1 2 1 2 1 1
output:
6 5 5 5 4 1 1 1 1 1 1
result:
ok 3 lines
Test #2:
score: 0
Accepted
time: 991ms
memory: 43644kb
input:
5557 2 7 79960 79960 2 2 1 1 1 1 2 2 1 1 2 1 1 2 9 8 21881 70740 70740 21881 22458 22458 639 21881 70740 3 3 1 6 5 8 7 5 5 7 2 3 5 1 7 6 6 7 13064 20716 6746 13064 6746 69225 5 5 4 1 4 1 1 6 4 5 3 2 3 2 8 4 45146 14400 45146 45146 14400 72969 14400 45146 8 6 1 3 4 6 8 3 18 13 48132 37949 92338 92338...
output:
2 2 2 2 2 2 2 6 6 7 6 6 6 6 6 3 3 3 4 4 3 3 7 7 7 7 9 9 9 8 9 8 9 8 9 9 10 9 9 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 7 8 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 9 10 9 16 16 16 16 16 17 16 16 10 10 11 10 12 11 10 10 10 10 10 10 10 12 10 10 10 10 10 11 10 9 9 9 9 9 9 9 9 9 9 9 9 9 10 ...
result:
ok 5557 lines
Test #3:
score: 0
Accepted
time: 789ms
memory: 37408kb
input:
10 100000 99999 3983 3983 20157 97983 20157 20157 3983 3983 97983 20157 20157 3983 97983 20157 3983 20157 20157 3983 3983 3983 97983 97983 20157 3983 3983 97983 20157 97983 20157 97983 3983 97983 97983 3983 20157 3983 20157 20157 97983 3983 3983 3983 3983 97983 97983 3983 97983 97983 3983 20157 3983...
output:
33392 33393 33393 33393 33393 33392 33392 33393 33393 33393 33392 33393 33393 33392 33393 33393 33392 33392 33392 33393 33393 33393 33392 33392 33393 33393 33393 33393 33393 33392 33393 33393 33392 33393 33392 33393 33393 33393 33392 33392 33392 33392 33393 33393 33392 33393 33393 33392 33393 33392 ...
result:
ok 10 lines
Test #4:
score: 0
Accepted
time: 816ms
memory: 34516kb
input:
10 100000 99999 27534 27534 3780 3780 27534 53544 27534 3780 3780 53544 53544 27534 53544 53544 3780 3780 3780 3780 53544 27534 3780 3780 53544 27534 27534 53544 27534 27534 53544 27534 27534 27534 3780 27534 27534 3780 3780 3780 27534 53544 3780 53544 27534 3780 3780 3780 27534 27534 27534 3780 275...
output:
33613 33601 33601 33600 33600 33601 33601 33601 33600 33601 33600 33600 33601 33601 33601 33601 33601 33601 33600 33600 33601 33601 33601 33601 33600 33601 33601 33600 33601 33600 33601 33600 33601 33601 33601 33601 33600 33601 33601 33601 33601 33601 33601 33601 33601 33601 33600 33601 33600 33601 ...
result:
ok 10 lines
Test #5:
score: 0
Accepted
time: 890ms
memory: 35620kb
input:
10 100000 99999 92499 92270 92270 92499 92499 92499 92270 54017 92270 92270 92270 54017 54017 54017 54017 92270 92499 54017 92270 54017 92499 92499 92270 92270 54017 54017 54017 54017 92270 92270 92499 54017 54017 92499 92499 54017 92270 92270 54017 92499 92270 92270 54017 54017 54017 92499 92499 54...
output:
33506 33482 33507 33482 33508 33483 33508 33483 33508 33483 33507 33483 33506 33483 33505 33483 33503 33483 33503 33482 33504 33483 33505 33483 33504 33483 33502 33483 33501 33483 33500 33482 33502 33483 33500 33483 33501 33482 33502 33483 33501 33483 33500 33482 33500 33483 33498 33483 33499 33483 ...
result:
ok 10 lines
Test #6:
score: 0
Accepted
time: 917ms
memory: 38764kb
input:
10 100000 99999 76207 76207 88551 88551 98176 76207 98176 88551 88551 98176 88551 76207 76207 98176 98176 76207 76207 88551 76207 88551 76207 88551 88551 76207 88551 76207 98176 88551 76207 98176 88551 88551 76207 88551 98176 88551 76207 76207 98176 88551 76207 98176 76207 88551 88551 88551 88551 76...
output:
33484 33484 33476 33484 33477 33485 33476 33485 33477 33485 33477 33486 33477 33484 33477 33485 33476 33485 33476 33485 33476 33483 33477 33483 33477 33485 33476 33485 33477 33485 33476 33487 33476 33487 33476 33486 33477 33486 33476 33486 33477 33486 33476 33486 33476 33486 33477 33487 33477 33487 ...
result:
ok 10 lines
Test #7:
score: 0
Accepted
time: 907ms
memory: 35148kb
input:
10 100000 99999 70486 49904 70486 49904 87935 49904 49904 87935 87935 49904 49904 87935 49904 87935 87935 70486 49904 87935 87935 49904 70486 87935 49904 70486 87935 87935 49904 49904 49904 87935 70486 70486 70486 49904 70486 87935 87935 87935 70486 87935 70486 49904 87935 49904 49904 87935 70486 87...
output:
33491 33486 33489 33486 33489 33486 33489 33486 33487 33486 33487 33486 33486 33485 33486 33486 33486 33486 33485 33486 33485 33485 33486 33486 33485 33486 33485 33486 33485 33485 33485 33486 33485 33486 33485 33486 33485 33486 33485 33486 33485 33486 33485 33486 33485 33486 33485 33485 33486 33485 ...
result:
ok 10 lines
Test #8:
score: 0
Accepted
time: 929ms
memory: 35092kb
input:
10 100000 99999 98004 33580 98004 98004 98004 92291 92291 98004 98004 92291 92291 33580 98004 92291 33580 98004 98004 33580 98004 92291 92291 33580 92291 92291 98004 33580 98004 33580 33580 98004 33580 92291 33580 33580 92291 92291 92291 98004 33580 98004 92291 92291 33580 92291 98004 98004 92291 92...
output:
33462 33463 33421 33463 33422 33465 33421 33463 33422 33464 33422 33462 33422 33464 33421 33464 33422 33464 33422 33465 33422 33463 33422 33462 33422 33463 33422 33465 33421 33464 33422 33464 33422 33463 33422 33463 33421 33463 33421 33462 33422 33460 33422 33461 33421 33461 33422 33460 33422 33459 ...
result:
ok 10 lines