結果
問題 |
No.3165 [Cherry 7th Tune A] Croissants Continu
|
ユーザー |
![]() |
提出日時 | 2025-06-28 14:55:03 |
言語 | Java (openjdk 23) |
結果 |
RE
|
実行時間 | - |
コード長 | 1,784 bytes |
コンパイル時間 | 3,068 ms |
コンパイル使用メモリ | 81,720 KB |
実行使用メモリ | 53,612 KB |
最終ジャッジ日時 | 2025-06-28 14:55:14 |
合計ジャッジ時間 | 10,185 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | RE * 1 |
other | WA * 1 RE * 30 |
ソースコード
package no3164_la_vie_en_rose; import java.util.*; public class Main { public static void main(String[] args) { // TODO 自動生成されたメソッド・スタブ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); UnionFind uf = new UnionFind(n + 1); for(int i = 1;i <= n;i++) { uf.val[i] = Integer.parseInt(sc.next()); uf.size[i] = uf.val[i]; }for(int i = 1;i <= n;i++) { if(uf.val[i - 1] > 0 && uf.val[i] > 0) { uf.unite(i - 1, i); } }int q = sc.nextInt(); StringBuilder sb = new StringBuilder(); for(int i = 0;i < q;i++){ int ind = Integer.parseInt(sc.next()); int val = Integer.parseInt(sc.next()); long ans = 0; int root = uf.getRoot(ind); if(uf.val[ind] == 0) { int b = uf.getRoot(ind - 1); int c = uf.getRoot(ind + 1); ans = val + uf.size[b] + uf.size[c]; }else { ans = uf.size[root] + val - uf.val[ind]; } sb.append(ans + "\n"); }System.out.print(sb); }public static class UnionFind{ public int[] root; public long[] size; public long[] val; public UnionFind(int n) { this.root = new int[n + 1]; this.size = new long[n + 1]; this.val = new long[n + 1]; for(int i = 1;i < n + 1;i++) { this.root[i] = i; } } public void unite(int x,int y) { x = getRoot(x); int yc = getRoot(y); if(x != y){ size[x] += size[yc]; } this.root[yc] = this.root[x]; } public void rootPlus(int x,int y) { this.root[x] = y; this.size[y]++; } public int getRoot(int x) { if(x == this.root[x]) { return x; }else return this.root[x] = getRoot(this.root[x]); } public boolean same(int x,int y) { x = getRoot(x); y = getRoot(y); return x == y; } } }