QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#350033#6805. Data StructurejthisWA 6970ms65500kbC++144.1kb2024-03-10 12:58:052024-03-10 12:58:06

Judging History

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

  • [2024-03-10 12:58:06]
  • 评测
  • 测评结果:WA
  • 用时:6970ms
  • 内存:65500kb
  • [2024-03-10 12:58:05]
  • 提交

answer

#include <iostream>
#include <vector>
#include <vector>
#include <set>
#include "map"
#include <array>
#include <algorithm>
#include <random>
#include <bitset>
#include <chrono>
using namespace std;
#define all(x)x.begin(),x.end()
#define pack(x)sort(all(x));x.erase(unique(all(x)),x.end())
#define gi(x,v)lower_bound(all(x),v)-x.begin()
using ll=long long;
using ld=long double;
using tu=array<ll,5>;
vector<int> v[300'010];
const int sq=550;
struct seg{
    int* tree;
    int N;
    void init(int n){
        N=n;
        if(N==0)return;
        tree=new int[N+1];
    }void clear(){
        if(N==0)return;
        N=0;
        delete[] tree;
    }
    
    int query(int i) {
        i++;
        int sum = 0;
        while (i > 0) {
            sum += tree[i];
            i -= i&-i;
        }
        return sum;
    }void update(int i, ll diff) {
        while (i <= N) {
            tree[i] += diff;
            i += i&-i;
        }
    }
    
    void update(int i, int j, int diff) {
        i++;j++;
        update(i, diff);
        update(j+1, -diff);
    }
}dep[300'010];
vector<seg> line[sq+10];
///node = y mod x
vector<vector<int>> vl[sq+10];
vector<int> vd[300'010];
int deps[300'010];
int in[300'010],out[300'010],pv;
int D;
void dfs(int n,int p,int d){
    in[n]=++pv;
    deps[n]=d;D=max(D,d+1);
    vd[d].push_back(pv);
    for(auto i:v[n]){
        if(i==p)continue;
        dfs(i,n,d+1);
    }out[n]=pv;
}
vector<tu> quer;
void solve(){
    int n,m;quer.clear();
    cin>>n>>m;
    D=0;
    int a,b,c,d,f;
    for(int i=2;i<=n;i++){
        cin>>a;
        v[a].push_back(i);
    }
    pv=0;
    dfs(1,0,1);
    
    for(int j=0;j<m;j++){
        cin>>a>>b;
        if(a==1){
            int s=in[b];
            int e=out[b];
            cin>>c>>d>>f;
            quer.push_back({a,b,c,d,f});
            if(c<=sq){
                d+=deps[b];
                d%=c;
                vl[c][d].push_back(s);
                vl[c][d].push_back(e+1);
            }
        }else{
            quer.push_back({a,b});
        }
    }
    for(int i=1;i<=sq;i++){
        for(int j=0;j<i;j++){
vl[i][j].push_back(1);
vl[i][j].push_back(n+2);
            if(vl[i][j].empty())continue;
            line[i][j].init(vl[i][j].size());
            pack(vl[i][j]);////정렬 - 해결
        }
    }for(int i=1;i<=D;i++){
        dep[i].init(vd[i].size());
    }
    for(int j=0;j<m;j++){
        a=quer[j][0];
        b=quer[j][1];
        c=quer[j][2];
        d=quer[j][3];
        f=quer[j][4];
        if(a==1){
            int s=in[b];
            int e=out[b];
            ///node = d mod c -> add f
            if(c<=sq){
                d+=deps[b];
                d%=c;
                int sv=gi(vl[c][d],s);
                int ev=gi(vl[c][d],e+1)-1;
                line[c][d].update(sv,ev,f);
            }else{
                for(int i=deps[b]+d;i<=D;i+=c){
                    int sv=gi(vd[i],s);
                    int ev=gi(vd[i],e+1)-1;
                    dep[i].update(sv,ev,f);
                }
            }
        }else{
            int dx=deps[b];
            int idx=in[b];
            int ans=dep[dx].query(gi(vd[dx],idx));
            for(int i=1;i<=sq;i++){
                int p=dx%i;
                ans+=line[i][p].query(gi(vl[i][p],idx+1)-1);
            }cout<<ans<<'\n';
        }
    }
    
    
    for(int i=1;i<=sq;i++){
        for(int j=0;j<i;j++){
            line[i][j].clear();
            vl[i][j].clear();
            vl[i][j].shrink_to_fit();
        }
    }
    for(int i=1;i<=n;i++){
        dep[i].clear();
        vd[i].clear();
        vd[i].shrink_to_fit();
        v[i].clear();
        v[i].shrink_to_fit();
    }
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int tc;
    cin>>tc;
    for(int i=1;i<=sq;i++){
        line[i].resize(i);
        line[i].shrink_to_fit();
        vl[i].resize(i);
        vl[i].shrink_to_fit();
    }
    while(tc--){
        solve();
    }
 
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 16ms
memory: 32636kb

input:

1
5 5
1 1 2 1
1 1 5 4 1
1 1 4 1 5
1 2 1 0 4
2 3
2 1

output:

5
0

result:

ok 2 lines

Test #2:

score: -100
Wrong Answer
time: 6970ms
memory: 65500kb

input:

4
300000 300000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 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...

output:

5149565
5151773
5153085
5150323
5152610
5155838
5157026
5161740
5163537
5163924
728978098
5170607
5179704
729278534
5177848
5180212
5179805
5185057
1522317340
5186432
5189006
5195954
5195332
5198117
5196613
5199350
5200143
5210026
5213829
1126279019
-766557166
5220889
730381398
5223135
5224918
52249...

result:

wrong answer 1st lines differ - expected: '1130', found: '5149565'