QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#307647 | #7439. 铃原露露 | raymond_7 | 0 | 0ms | 40756kb | C++14 | 3.8kb | 2024-01-18 23:26:18 | 2024-01-18 23:26:19 |
Judging History
answer
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <iostream>
#include <vector>
#include <queue>
#include <set>
using namespace std;
#define int long long
#define PII pair<int,int>
#define x first
#define y second
#define For(i, l, r) for(int i = l; i <= r; i ++)
#define Rep(i, l, r) for(int i = l; i >= r; i --)
bool START;
void in(int &x)
{
char c = getchar();
while(c > '9' || c < '0') c = getchar();
for(x = 0; c >= '0' && c <= '9'; c = getchar())
x = x * 10 + c - '0';
}
const int N = 2e5 + 50;
int n, m;
int a[N];
struct node {int l, r, op, id;};
vector<node> vec[N], Q[N];
int Ans[N];
vector<int> G[N];
int id[N], sz[N], son[N];
set<int> S[N];
void init(int x, int y, int z)
{
// printf("(%lld, %lld, %lld)\n", x, y, z);
if(x <= z && z <= y) return;
if(z > y)
{
vec[y].push_back({1, x, 1});
vec[z].push_back({1, x, -1});
}
else
{
vec[y].push_back({z, x - 1, 1});
}
}
void Dfs(int u, int pa)
{
sz[u] = 1;
for(int v: G[u]) if(v != pa)
{
Dfs(v, u); sz[u] += sz[v];
if(sz[v] > sz[son[u]]) son[u] = v;
}
if(!son[u]) return ;
S[u] = S[son[u]]; S[son[u]].clear();
S[u].insert(a[u]);
int z = a[u];
for(int v : G[u]) if(v != pa && v != son[u])
{
for(int x : S[v])
{
auto it = S[u].lower_bound(x);
if(it != S[u].end())
{
int y = *it; init(x, y, z);
}
if(it != S[u].begin())
{
it --; int y = *it;
init(y, x, z);
}
}
for(int x : S[v]) S[u].insert(x);
}
}
#define ls (u << 1)
#define rs (u << 1 | 1)
int sum[N << 2], tag1[N << 2], tag2[N << 2], cnt[N << 2], mn[N << 2];
void up(int u)
{
mn[u] = min(mn[ls], mn[rs]);
sum[u] = sum[ls] + sum[rs];
cnt[u] = 0;
if(mn[u] == mn[ls]) cnt[u] += cnt[ls];
if(mn[u] == mn[rs]) cnt[u] += cnt[rs];
}
void build(int u, int l, int r)
{
cnt[u] = r - l + 1;
if(l == r) return ;
int mid = l + r >> 1; build(ls, l, mid); build(rs, mid + 1, r);
}
void Add(int u, int v)
{
mn[u] += v; tag1[u] += v;
}
void Add_2(int u, int v)
{
sum[u] += cnt[u] * v; tag2[u] += v;
}
void down(int u)
{
if(tag1[u])
{
Add(ls, tag1[u]); Add(rs, tag1[u]); tag1[u] = 0;
}
if(tag2[u])
{
if(mn[ls] == mn[u]) Add_2(ls, tag2[u]);
if(mn[rs] == mn[u]) Add_2(rs, tag2[u]); tag2[u] = 0;
}
}
void upd1(int u, int l, int r, int x, int y, int v)
{
if(x <= l && r <= y) {Add(u, v); return ;}
down(u); int mid = l + r>>1;
if(x <= mid) upd1(ls, l, mid, x, y, v);
if(y > mid) upd1(rs, mid + 1, r, x, y, v); up(u);
}
void upd2(int u, int l, int r, int x, int y, int v)
{
if(mn[u] > 0) return ;
if(x <= l && r <= y) {Add_2(u, v); return ;}
int mid = l + r >> 1; down(u);
if(x <= mid) upd2(ls, l, mid, x, y, v);
if(y > mid) upd2(rs, mid + 1, r, x, y, v);
up(u);
}
int query(int u, int l, int r, int x, int y)
{
if(x <= l && r <= y) return sum[u];
int mid = l + r >> 1, s = 0; down(u);
if(x <= mid) s += query(ls, l, mid, x, y);
if(y > mid) s += query(rs, mid + 1, r, x, y);
return s;
}
#undef ls
#undef rs
int b[N];
bool ENDPOS = 0;
signed main()
{
in(n); in(m);
For(i, 1, n) in(a[i]); //a is a permutaiton, very good!
For(i, 2, n)
{
int pa; in(pa); G[pa].push_back(i);
}
For(i, 1, n) id[a[i]] = i, S[i].insert(a[i]);
Dfs(1, 0);
build(1, 1, n);
For(i, 1, m)
{
int l, r; in(l); in(r);
Q[r].push_back({l, r, 1, i});
}
build(1, 1, n);
For(i, 1, n)
{
for(node E : vec[i])
{
upd1(1, 1, n, E.l, E.r, E.op);
}
upd2(1, 1, n, 1, i, 1);
for(node E : Q[i])
{
Ans[E.id] = query(1, 1, n, E.l, E.r);
}
}
For(i, 1, m) printf("%lld\n", Ans[i]);
cerr << (&ENDPOS - &START) * 1.0 / 1024 / 1024 << endl; return 0;
}
/*
5 5
2 1 5 4 3
1
1
3
3
1 1
1 4
2 3
1 5
3 5
*/
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 40756kb
input:
100 100 5 29 12 16 25 36 18 37 27 47 34 40 20 3 1 42 26 19 33 41 6 22 8 58 32 62 24 15 35 17 59 30 50 61 43 49 39 67 44 21 13 31 68 69 65 64 10 28 38 54 70 63 9 46 66 52 23 7 48 60 55 56 51 2 57 11 53 14 45 4 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 99 100 ...
output:
17 156 68 6 22 24 86 31 202 54 82 54 119 70 39 47 75 140 59 62 59 68 154 47 14 111 60 121 1 78 183 8 75 72 7 78 12 24 151 30 109 63 137 12 81 129 150 32 7 90 126 339 144 43 11 95 52 6 149 17 38 66 15 235 138 121 132 119 89 29 3 23 10 83 147 83 21 118 89 102 179 146 157 15 1 76 165 11 28 96 153 101 6...
result:
wrong answer 1st numbers differ - expected: '9', found: '17'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Time Limit Exceeded
Test #21:
score: 0
Time Limit Exceeded
input:
200000 1 73119 155820 110077 139724 136809 18709 57745 43535 89117 43647 20295 60551 108184 188031 180437 52363 72969 130559 179796 75852 53879 96998 63387 76458 193661 142318 28260 40465 80050 188507 143795 141018 94880 71333 7644 109237 105208 109509 9779 159914 135096 47638 175577 182927 173100 1...
output:
result:
Subtask #4:
score: 0
Skipped
Dependency #1:
0%