QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#65540#3847. AirlineUBB_Zalau00#RE 0ms0kbC++142.9kb2022-12-01 17:58:332022-12-01 17:58:35

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-12-01 17:58:35]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2022-12-01 17:58:33]
  • 提交

answer

#include <bits/stdc++.h>


using namespace std;

class InParser {
private:
  static const int BUFF_SIZE = 1 << 12;
  FILE *f;
  char buff[BUFF_SIZE];
  int sp = BUFF_SIZE;

  char read_ch() {
    if (sp == BUFF_SIZE) {
      sp = 0;
      fread(buff, 1, BUFF_SIZE, f);
    }
    return buff[sp++];
  }

public:
  InParser() {
    f = stdin;
  }

  InParser& operator >> (int &n) {
    char c;
    while (!isdigit(c = read_ch()) && c != '-');
    int sgn = 1;
    if (c == '-') {
      n = 0;
      sgn = -1;
    } else {
      n = c - '0';
    }
    while (isdigit(c = read_ch())) {
      n = 10 * n + c - '0';
    }
    n *= sgn;
    return *this;
  }

  InParser& operator >> (long long &n) {
    char c;
    n = 0;
    while (!isdigit(c = read_ch()) && c != '-');
    long long sgn = 1;
    if (c == '-') {
      n = 0;
      sgn = -1;
    } else {
      n = c - '0';
    }
    while (isdigit(c = read_ch())) {
      n = 10 * n + c - '0';
    }
    n *= sgn;
    return *this;
  }
};
const int nmax = 1000*1000+5;
int w[nmax],lev[nmax],tt[nmax];
vector<int> v[nmax];
void dfs(int x){
    w[x] = 1;
    for(auto son: v[x])
        if(!w[son]){
           lev[son] = lev[x] + 1;
           tt[son] = x;
           dfs(son);
           w[x] += w[son];
        }
}
int nr1, nr2, nr;
int pathA[nmax], pathB[nmax], path[nmax];
long long sum[nmax];
int lca, n, q;
int a, b, x, y;
void getPath(int A, int B){
    nr1=nr2=nr=0;
    if(lev[A] < lev[B]){
        swap(A,B);
    }
    while(lev[A] > lev[B]){
        pathA[++nr1] = A;
        A=tt[A];
    }
    while(A!=B){
        pathA[++nr1] = A;
        pathB[++nr2] = B;
        A=tt[A];B=tt[B];
    }
    pathA[++nr1] = A;
    for(int i=1;i<=nr1;i++){
        path[i] = pathA[i];
    }
    lca=A;
    nr = nr1+nr2;
    for(int i=1;i<=nr2;i++)
        path[nr1+i] = pathB[nr2-i+1];
}
int getSums(){
    for(int i=1;i<=nr;i++){
        sum[i] = sum[i-1];
        int act = w[path[i]];
        if(i>1&&lev[path[i-1]]>lev[path[i]]){
            act-=w[path[i-1]];
        }
        if(path[i]==lca){
            act+=n-w[path[i]];
        }
        if(i<nr&&lev[path[i+1]]>lev[path[i]]){
            act-=w[path[i+1]];
        }
        sum[i] += act;
    }
}
long long twoPointers(){
    int left=1;
    long long ans=0;
    for(int right=1;right<=nr;right++){
        while(left<right && nr-right+left<right-left){
            left++;
        }
        long long f1=sum[right]-sum[right-1];
        long long f2=sum[left-1];
        ans += 1LL*f1*f2;
    }
    return ans;
}
int main()
{
    //freopen("data.in","r",stdin);
    InParser f;
    f>>n>>q;
    for(int i=1;i<=n-1;i++){
        f>>a>>b;
        v[a].push_back(b);
        v[b].push_back(a);
    }
    dfs(1);
    for(int cnt=1;cnt<=q;cnt++){
        f>>x>>y;
        getPath(x, y);
        getSums();
        cout<<twoPointers()<<'\n';
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Runtime Error

input:

1000 100000
552 9
456 720
540 790
628 695
848 478
66 268
798 360
773 277
116 471
874 792
912 784
502 831
359 965
925 434
677 629
271 670
76 755
92 200
814 422
922 266
617 44
480 331
18 662
153 753
669 491
368 187
99 867
476 808
774 509
98 147
724 478
447 182
923 469
881 665
674 589
770 613
436 310
8...

output:


result: