QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#287645#5669. Traveling in Jade CityThMinh__RE 1ms5528kbC++143.2kb2023-12-20 21:05:332023-12-20 21:05:35

Judging History

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

  • [2023-12-20 21:05:35]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:5528kb
  • [2023-12-20 21:05:33]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10;
int n, k, m, q;
bool fix[N << 1];
int s[N << 1], g[N << 1], tr[N << 1];

struct rail {
   int u, v, t;
} c[N << 1];

struct query {
   char ch;
   int i, u, v;
} Q[N];

void init() {
   cin>>n>>k>>m>>q;
   for(int i = 1; i <= n; ++i) {
      int t; cin>>t;
      c[i] = {i, i % n + 1, t};
   }

   for(int i = 0; i <= m; ++i) {
      int t; cin>>t;
      if(!i) c[n + i + 1] = {1, n + 1, t};
      else if(i == m) c[n + i + 1] = {n + m, k, t};
      else c[n + i + 1] = {n + i, n + i + 1, t};
   }

   for(int i = 1; i <= q; ++i) {
      cin>>Q[i].ch;
      if(Q[i].ch == 'q') cin>>Q[i].u>>Q[i].v;
      else cin>>Q[i].i;
   }
}

void up(int x, int val) {
   while(x <= n + m + 1) {
      tr[x] += val;
      x += x & -x;
   }
}

int get(int x) {
   int res = 0;
   while(x) {
      res += tr[x];
      x -= x & -x;
   }
   return res;
}

int dis(int f, int l) {
   if(f > l) swap(f, l);
   bool ok = 1;
   if(f > n) ok = 0;

   int sum = 1e9, cnt = get(l - 1) - get(f - 1);
   if(ok) {
      int val1 = cnt ? 1e9 : s[l - 1] - s[f - 1];
      sum = min(sum, val1);

      int val2 = (get(n) - cnt) ? 1e9 : s[n] - (s[l - 1] - s[f - 1]);
      sum = min(sum, val2);
   }
   else if(!cnt) sum = min(sum, g[l - 1] - g[f - 1]);

   return sum;
}

void solve() {
   for(int i = 1; i <= n + m + 1; ++i) {
      if(i <= n) s[i] = s[i - 1] + c[i].t;
      if(i > n) g[i] = g[i - 1] + c[i].t;
   }

   for(int i = 1; i <= q; ++i) {
      char ch = Q[i].ch;
      if(ch == 'c') {
         int j = Q[i].i;
         int val = fix[j] ? -1 : 1;

         up(j, val);
         fix[j] ^= 1;
      }
      else if(ch == 'x') {
         int j = Q[i].i;
         int val = fix[n + j + 1] ? -1 : 1;

         up(n + j + 1, val);
         fix[n + j + 1] ^= 1;
      }
      else {
         int u = Q[i].u;
         int v = Q[i].v;
         if(u > v) swap(u, v);
         if(u == v) {
            cout<<0<<"\n";
            continue;
         }

         int ans = 1e9;

         if(u <= n && v <= n) {
            ans = min(ans, dis(u, v));

            if(get(n + m + 1) - get(n) == 0) {
               ans = min(ans, g[n + m + 1] + dis(1, u) + dis(k, v));
               ans = min(ans, g[n + m + 1] + dis(k, u) + dis(1, v));
            }
         }
         else if(u <= n && v > n) {
            ans = min(ans, dis(v + 1, n + 1) + dis(1, u));
            ans = min(ans, dis(v + 1, n + m + 2) + dis(k, u));
         }
         else {
            ans = min(ans, dis(u + 1, v + 1));

            int res = 1e9;
            res = min(res, dis(n + 1, u + 1) + dis(v + 1, n + m + 2));
            res = min(res, dis(n + m + 2, u + 1) + dis(v + 1, n + 1));

            ans = min(ans, res + dis(1, k));
         }

         if(ans >= 1e9) cout<<"impossible\n";
         else cout<<ans<<"\n";
      }
   }
}

int main () {
   cin.tie(0)->sync_with_stdio(0);
   if(fopen("Task.inp", "r")) {
      freopen("Task.inp", "r", stdin);
      freopen("WA.out", "w", stdout);
   }
   if(fopen("Train.inp", "r")) {
      freopen("Train.inp", "r", stdin);
      freopen("Train.out", "w", stdout);
   }

   init();

   return solve(), 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 5528kb

input:

4 3 1 9
2 3 8 4
1 1
q 3 4
x 0
q 3 4
c 3
q 3 4
c 1
q 3 4
x 0
q 3 4

output:

6
8
9
impossible
6

result:

ok 5 lines

Test #2:

score: 0
Accepted
time: 1ms
memory: 5416kb

input:

4 3 1 9
2 3 8 4
1 1
q 3 4
x 0
q 3 4
c 3
q 3 4
c 1
q 3 4
x 0
q 3 4

output:

6
8
9
impossible
6

result:

ok 5 lines

Test #3:

score: -100
Runtime Error

input:

1000000 999999 1000000 1000000
200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 2...

output:


result: