이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define int long long
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
const int LOG=20;
using namespace std;
signed main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
int n,m,q; cin >> n >> m >> q;
vector<vector<pair<int,int>>> sum(n*2);
vector<pair<int,int>> v;
vector<int> ed;
for (int i = 0; i < n-1; i++)
{
int a,b; cin >> a >> b;
ed.push_back(min(a-1,b-1));
}
for (int i = 0; i < m; i++){
int p,c; cin >> p >> c;
v.push_back({c,ed[p-1]});
}
vector<int> prefix(n*2);
for (int i = 0; i < sz(v); i++)
{
if(i>0) prefix[i]+=prefix[i-1];
}
sort(v.begin(),v.end());
for (int i = 0; i < m; i++){
sum[v[i].second].push_back({v[i].first,i});
}
int square=sqrt(m)+2;
int squareQ=sqrt(q)+2;
vector<vector<pair<pair<pair<int,int>,pair<int,int>>,int>>> queries((q/squareQ)*2);
vector<vector<int>> coins((q/squareQ)*2);
vector<vector<int>> qrt((m/square)*2);
vector<int> qrtSUM((m/square)*2);
vector<int> qrtCOUNT((m/square)*2);
vector<int> ans(q);
vector<pair<pair<int,int>,pair<int,int>>> copyQUERY(q);
for (int Q = 0; Q < q; Q++)
{
int s,t,x,y; cin >> s >> t >> x >> y;
copyQUERY[Q]={{s,t},{x,y}};
prefix[s-1]++;
}
for (int i = 1; i < n; i++) prefix[i]+=prefix[i-1];
for (int Q = 0; Q < q; Q++)
{
int s=copyQUERY[Q].first.first,t=copyQUERY[Q].first.second,x=copyQUERY[Q].second.first,y=copyQUERY[Q].second.second;
if(s>t) swap(s,t);
queries[(prefix[s-1]/squareQ)].push_back({{{t-1, s-1},{x,y}},Q});
}
for (int i = 0; i < sz(queries); i++) {
sort(all(queries[i]));
}
for (int i = 0; i < sz(qrt); i++) qrt[i].resize(square+1);
int l=-1,r=-1;
int totalCOUNT=0;
for (int i = 0; i < sz(queries); i++){
for (int j = 0; j < sz(queries[i]); j++){
int s=queries[i][j].first.first.second,t=queries[i][j].first.first.first,x=queries[i][j].first.second.first,y=queries[i][j].first.second.second,qr=queries[i][j].second;
if(r==-1) {
r=s;
l=s;
}
for (int k = r; k < t; k++) {
for (auto u : sum[k]) {
qrtSUM[(u.second/square)]+=u.first;
qrtCOUNT[(u.second/square)]++;
totalCOUNT++;
qrt[(u.second/square)][(u.second%square)]=u.first;
}
}
for (int k = t; k < r; k++) {
for (auto u : sum[k]) {
qrtSUM[(u.second/square)]-=u.first;
qrtCOUNT[(u.second/square)]--;
totalCOUNT--;
qrt[(u.second/square)][(u.second%square)]=0;
}
}
for (int k = l; k < s; k++) {
for (auto u : sum[k]) {
qrtSUM[(u.second/square)]-=u.first;
qrtCOUNT[(u.second/square)]--;
totalCOUNT--;
qrt[(u.second/square)][(u.second%square)]=0;
}
}
for (int k = s; k < l; k++) {
for (auto u : sum[k]) {
qrtSUM[(u.second/square)]+=u.first;
qrtCOUNT[(u.second/square)]++;
totalCOUNT++;
qrt[(u.second/square)][(u.second%square)]=u.first;
}
}
int cqrt=0;
int csum=0;
int cnt=0;
while(cqrt<sz(qrtSUM)){
csum+=qrtSUM[cqrt];
if(csum>y) break;
cnt+=qrtCOUNT[cqrt];
cqrt++;
}
if(cqrt==sz(qrtSUM)) {
ans[qr]=x;
}
else {
csum-=qrtSUM[cqrt];
int _i=0;
while(_i<sz(qrt[cqrt]))
{
if(qrt[cqrt][_i]>0) cnt++;
csum+=qrt[cqrt][_i];
if(csum>y) {
cnt--;
break;
}
_i++;
}
ans[qr]=max(-1LL, x-(totalCOUNT-cnt));
}
l=s; r=t;
}
}
for (int i = 0; i < sz(ans); i++) cout << ans[i] << "\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |