/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 464.0 KiB
#2 Accepted 1ms 560.0 KiB
#3 Accepted 85ms 9.75 MiB
#4 Accepted 78ms 9.926 MiB
#5 Accepted 63ms 9.855 MiB
#6 Accepted 69ms 9.773 MiB
#7 Time Exceeded ≥1087ms ≥59.465 MiB
#8 Time Exceeded ≥1095ms ≥59.559 MiB

Code


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long int
using namespace std;
using namespace __gnu_pbds;
const ll md=1e9+7;
const ll smpl=4*1e5;
const ll p=31;


void dfs(int i,int vis[],int ara[],int &j,map<int,vector<int>>&tree){
vis[i]++;
ara[j]=i;
j++;
for(auto it:tree[i]){
    if(!vis[it]){
        dfs(it,vis,ara,j,tree);
    }
}
ara[j]=i;
j++;


}
void fxd(int i,int j,int ara[],int ara1[],int seg[],int node){

if(i==j){
    if(ara[ara1[i]]==1)seg[node]=1;
    else seg[node]=0;
    return;
}
int mid=(i+j)/2;
fxd(i,mid,ara,ara1,seg,node*2);
fxd(mid+1,j,ara,ara1,seg,node*2+1);
seg[node]=seg[node*2]+seg[node*2+1];
}

void updt(int i,int j,int l,int r,int seg[],int seg1[],int node){
if(i>r||j<l){
    if(seg1[node]%2!=0)seg[node]=(j-i+1)-seg[node];
    if(i!=j){
    seg1[node*2]+=seg1[node];
    seg1[node*2+1]+=seg1[node];
    }
    seg1[node]=0;
    return;
}
if(i>=l&&j<=r){
    seg1[node]++;
    if(seg1[node]%2!=0)seg[node]=(j-i+1)-seg[node];
    if(i!=j){
    seg1[node*2]+=seg1[node];
    seg1[node*2+1]+=seg1[node];
    }
    seg1[node]=0;
    return;
}
 if(seg1[node]%2!=0)seg[node]=(j-i+1)-seg[node];
seg1[node*2]+=seg1[node];
    seg1[node*2+1]+=seg1[node];
    int mid=(i+j)/2;
    updt(i,mid,l,r,seg,seg1,node*2);
    updt(mid+1,j,l,r,seg,seg1,node*2+1);
    seg[node]=seg[node*2+1]+seg[node*2];
    seg1[node]=0;
}
ll fnd(int i,int j,int l,int r,int seg[],int seg1[],int node){

if(i>r||j<l){

        if(seg1[node]%2!=0)seg[node]=(j-i+1)-seg[node];
    if(i!=j){
    seg1[node*2]+=seg1[node];
    seg1[node*2+1]+=seg1[node];
    }
    seg1[node]=0;
    return 0;

}
if(i>=l&&j<=r){
     if(seg1[node]%2!=0)seg[node]=(j-i+1)-seg[node];
    if(i!=j){
    seg1[node*2]+=seg1[node];
    seg1[node*2+1]+=seg1[node];
    }
   seg1[node]=0;
    return  seg[node];
}
int mid=(i+j)/2;
 if(seg1[node]%2!=0)seg[node]=(j-i+1)-seg[node];
 seg1[node*2]+=seg1[node];
    seg1[node*2+1]+=seg1[node];

int x= fnd(i,mid,l,r,seg,seg1,node*2)+fnd(mid+1,j,l,r,seg,seg1,node*2+1);
seg[node]=seg[node*2]+seg[node*2+1];
seg1[node]=0;
return x;
}
void sufi() {
    map<int,vector<int>>tree;
  int n;
  cin>>n;
 int  ara[n+1];
  for(int i=1;i<=n;i++)cin>>ara[i];
  for(int i=1;i<n;i++){
    int x,y;
    cin>>x>>y;
    tree[x].push_back(y);
    tree[y].push_back(x);
  }
 int  ara1[2*n+9];
  int vis[n+1]={0};
  int j=1;
  dfs(1,vis,ara1,j,tree);
 int seg[8*n+10];
  int seg1[8*n+10]={0};
  fxd(1,2*n,ara,ara1,seg,1);
  int q;
  cin>>q;
  map<int,vector<int>>mp;
  for(int i=1;i<=2*n;i++)mp[ara1[i]].push_back(i);
  while(q--){
    int k;
    cin>>k;
    if(k==1){
        int x;
        cin>>x;
        updt(1,2*n,mp[x][0],mp[x][1],seg,seg1,1);
        continue;
    }
    else{
            int x;
    cin>>x;
            int c=0;
        ll tmp=fnd(1,2*n,mp[x][0],mp[x][1],seg,seg1,1);
    cout<<tmp/2<<endl;
    }

  }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    t=1;
    while(t--)
    sufi();
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1101 Mr. Heart and the Enchanted Lights
Language
C++17 (G++ 13.2.0)
Submit At
2024-10-09 20:26:04
Judged At
2024-11-11 02:38:07
Judged By
Score
30
Total Time
≥1095ms
Peak Memory
≥59.559 MiB