結果
問題 |
No.3164 [Chery 7th Tune B] La vie en rose
|
ユーザー |
![]() |
提出日時 | 2025-06-28 14:39:37 |
言語 | Java (openjdk 23) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,739 bytes |
コンパイル時間 | 2,458 ms |
コンパイル使用メモリ | 87,632 KB |
実行使用メモリ | 94,124 KB |
最終ジャッジ日時 | 2025-06-28 14:40:44 |
合計ジャッジ時間 | 59,422 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 31 TLE * 3 |
ソースコード
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] = sc.nextInt(); 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 = sc.nextInt(); int val = sc.nextInt(); 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; } } }