QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#350036 | #6805. Data Structure | jthis | AC ✓ | 6530ms | 67084kb | C++14 | 4.1kb | 2024-03-10 13:02:03 | 2024-03-10 13:02:04 |
Judging History
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+3];
for(int i=0;i<=n;i++)tree[i]=0;
}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);
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: 18ms
memory: 36112kb
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: 0
Accepted
time: 6530ms
memory: 67084kb
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:
1130 1509 2343 2426 4524 5096 8031 10312 15081 15081 0 21803 28276 507 29392 31408 31656 36062 507 37628 40202 45396 47064 48157 48157 50507 50866 60827 63565 0 398 71612 398 73533 76075 76524 398 78559 81801 1205 94975 1205 104994 106433 109726 110163 115355 116799 118169 119008 119008 398 120377 1...
result:
ok 479718 lines