|
| 1 | +```java |
| 2 | +import java.util.*; |
| 3 | +import java.io.*; |
| 4 | + |
| 5 | +class IOController { |
| 6 | + BufferedReader br; |
| 7 | + BufferedWriter bw; |
| 8 | + StringTokenizer st; |
| 9 | + |
| 10 | + public IOController() { |
| 11 | + br = new BufferedReader(new InputStreamReader(System.in)); |
| 12 | + bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 13 | + st = new StringTokenizer(""); |
| 14 | + } |
| 15 | + |
| 16 | + String nextLine() throws Exception { |
| 17 | + String line = br.readLine(); |
| 18 | + st = new StringTokenizer(line); |
| 19 | + return line; |
| 20 | + } |
| 21 | + |
| 22 | + String nextToken() throws Exception { |
| 23 | + while (!st.hasMoreTokens()) nextLine(); |
| 24 | + return st.nextToken(); |
| 25 | + } |
| 26 | + |
| 27 | + int nextInt() throws Exception { |
| 28 | + return Integer.parseInt(nextToken()); |
| 29 | + } |
| 30 | + |
| 31 | + long nextLong() throws Exception { |
| 32 | + return Long.parseLong(nextToken()); |
| 33 | + } |
| 34 | + |
| 35 | + double nextDouble() throws Exception { |
| 36 | + return Double.parseDouble(nextToken()); |
| 37 | + } |
| 38 | + |
| 39 | + void close() throws Exception { |
| 40 | + bw.flush(); |
| 41 | + bw.close(); |
| 42 | + } |
| 43 | + |
| 44 | + void write(String content) throws Exception { |
| 45 | + bw.write(content); |
| 46 | + } |
| 47 | + |
| 48 | +} |
| 49 | + |
| 50 | +public class Main { |
| 51 | + |
| 52 | + static IOController io; |
| 53 | + |
| 54 | + // |
| 55 | + |
| 56 | + static int N, Q; |
| 57 | + static int[] C, A; |
| 58 | + static long[] sum, cnt; |
| 59 | + |
| 60 | + public static void main(String[] args) throws Exception { |
| 61 | + |
| 62 | + io = new IOController(); |
| 63 | + |
| 64 | + init(); |
| 65 | + solve(); |
| 66 | + |
| 67 | + io.close(); |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + public static void init() throws Exception { |
| 72 | + |
| 73 | + N = io.nextInt(); |
| 74 | + Q = io.nextInt(); |
| 75 | + C = new int[100001]; |
| 76 | + A = new int[N+Q+1]; |
| 77 | + for(int i=1;i<=N;i++) { |
| 78 | + A[i] = io.nextInt(); |
| 79 | + C[A[i]]++; |
| 80 | + } |
| 81 | + sum = new long[262144]; |
| 82 | + cnt = new long[262144]; |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | + static void solve() throws Exception { |
| 87 | + |
| 88 | + init(1,100000,1); |
| 89 | + while(Q-->0) { |
| 90 | + int op = io.nextInt(); |
| 91 | + if(op == 1) { |
| 92 | + int j = io.nextInt(), v = io.nextInt(); |
| 93 | + query(1,100000,A[j],-1,1); |
| 94 | + A[j] = v; |
| 95 | + query(1, 100000, A[j], 1, 1); |
| 96 | + } |
| 97 | + else if(op == 2) { |
| 98 | + int T = io.nextInt(); |
| 99 | + int idx = find(1,100000,T,1); |
| 100 | + long ans = findcnt(1,100000,1,idx-1,1); |
| 101 | + long res = findsum(1,100000,1,idx-1,1); |
| 102 | + T -= res; |
| 103 | + // idx를 몇 개 가져올 수 있는지? |
| 104 | + ans += Math.min(T/idx, findcnt(1,100000,idx,idx,1)); |
| 105 | + io.write(ans + "\n"); |
| 106 | + } |
| 107 | + else { |
| 108 | + A[++N] = io.nextInt(); |
| 109 | + query(1,100000,A[N],1,1); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + } |
| 114 | + |
| 115 | + static void init(int s, int e, int n) { |
| 116 | + if(s == e) { |
| 117 | + sum[n] = (long)C[s]*s; |
| 118 | + cnt[n] = C[s]; |
| 119 | + return; |
| 120 | + } |
| 121 | + int m=(s+e)>>1; |
| 122 | + init(s,m,n*2); |
| 123 | + init(m+1,e,n*2+1); |
| 124 | + sum[n] = sum[n*2] + sum[n*2+1]; |
| 125 | + cnt[n] = cnt[n*2] + cnt[n*2+1]; |
| 126 | + } |
| 127 | + |
| 128 | + static void query(int s, int e, int i, int v, int n) { |
| 129 | + if(s == e) { |
| 130 | + sum[n] += v*s; |
| 131 | + if(v > 0) cnt[n]++; |
| 132 | + else cnt[n]--; |
| 133 | + return; |
| 134 | + } |
| 135 | + int m = (s+e)>>1; |
| 136 | + if(i <= m) query(s,m,i,v,n*2); |
| 137 | + else query(m+1,e,i,v,n*2+1); |
| 138 | + sum[n] = sum[n*2] + sum[n*2+1]; |
| 139 | + cnt[n] = cnt[n*2] + cnt[n*2+1]; |
| 140 | + } |
| 141 | + |
| 142 | + static int find(int s, int e, long v, int n) { |
| 143 | + if(s == e) return s; |
| 144 | + int m = (s+e)>>1; |
| 145 | + if(v <= sum[n*2]) return find(s,m,v,n*2); |
| 146 | + return find(m+1,e,v-sum[n*2],n*2+1); |
| 147 | + } |
| 148 | + |
| 149 | + static long findsum(int s, int e, int l, int r, int n) { |
| 150 | + if(l>r || l>e || r<s) return 0; |
| 151 | + if(l<=s && e<=r) return sum[n]; |
| 152 | + int m=(s+e)>>1; |
| 153 | + return findsum(s,m,l,r,n*2) + findsum(m+1,e,l,r,n*2+1); |
| 154 | + } |
| 155 | + |
| 156 | + static long findcnt(int s, int e, int l, int r, int n) { |
| 157 | + if(l>r || l>e || r<s) return 0; |
| 158 | + if(l<=s && e<=r) return cnt[n]; |
| 159 | + int m=(s+e)>>1; |
| 160 | + return findcnt(s,m,l,r,n*2) + findcnt(m+1,e,l,r,n*2+1); |
| 161 | + } |
| 162 | + |
| 163 | +} |
| 164 | +``` |
0 commit comments