結果

問題 No.2311 [Cherry 5th Tune] Cherry Month
ユーザー 👑 potato167potato167
提出日時 2023-05-19 22:57:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 393 ms / 4,600 ms
コード長 4,254 bytes
コンパイル時間 3,611 ms
コンパイル使用メモリ 222,416 KB
実行使用メモリ 53,000 KB
最終ジャッジ日時 2023-08-23 00:58:41
合計ジャッジ時間 27,459 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,356 KB
testcase_01 AC 1 ms
4,356 KB
testcase_02 AC 381 ms
44,148 KB
testcase_03 AC 154 ms
17,144 KB
testcase_04 AC 152 ms
20,612 KB
testcase_05 AC 143 ms
20,884 KB
testcase_06 AC 185 ms
27,196 KB
testcase_07 AC 140 ms
16,464 KB
testcase_08 AC 307 ms
35,752 KB
testcase_09 AC 235 ms
33,412 KB
testcase_10 AC 190 ms
30,376 KB
testcase_11 AC 277 ms
31,756 KB
testcase_12 AC 329 ms
34,116 KB
testcase_13 AC 305 ms
30,496 KB
testcase_14 AC 240 ms
43,252 KB
testcase_15 AC 311 ms
39,412 KB
testcase_16 AC 156 ms
33,224 KB
testcase_17 AC 272 ms
42,564 KB
testcase_18 AC 238 ms
41,704 KB
testcase_19 AC 296 ms
40,740 KB
testcase_20 AC 288 ms
28,924 KB
testcase_21 AC 265 ms
28,592 KB
testcase_22 AC 241 ms
31,828 KB
testcase_23 AC 306 ms
33,808 KB
testcase_24 AC 298 ms
30,052 KB
testcase_25 AC 184 ms
41,088 KB
testcase_26 AC 367 ms
37,496 KB
testcase_27 AC 304 ms
48,092 KB
testcase_28 AC 310 ms
47,856 KB
testcase_29 AC 309 ms
48,088 KB
testcase_30 AC 184 ms
35,176 KB
testcase_31 AC 188 ms
35,252 KB
testcase_32 AC 183 ms
35,180 KB
testcase_33 AC 289 ms
47,444 KB
testcase_34 AC 277 ms
47,208 KB
testcase_35 AC 291 ms
47,520 KB
testcase_36 AC 344 ms
47,520 KB
testcase_37 AC 357 ms
47,404 KB
testcase_38 AC 365 ms
47,644 KB
testcase_39 AC 393 ms
48,184 KB
testcase_40 AC 381 ms
47,736 KB
testcase_41 AC 390 ms
48,456 KB
testcase_42 AC 375 ms
47,704 KB
testcase_43 AC 387 ms
49,092 KB
testcase_44 AC 329 ms
53,000 KB
testcase_45 AC 326 ms
52,996 KB
testcase_46 AC 282 ms
39,592 KB
testcase_47 AC 335 ms
52,812 KB
testcase_48 AC 323 ms
50,296 KB
testcase_49 AC 385 ms
50,172 KB
testcase_50 AC 307 ms
50,172 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
ll ILL=2167167167167167167;
const int INF=2100000000;
const int mod=998244353;
#define rep(i,a,b) for (ll i=a;i<b;i++)
#define all(p) p.begin(),p.end()
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
void yneos(bool a){if(a) cout<<"Yes\n"; else cout<<"No\n";}
template<class T> void vec_out(vector<T> &p){for(int i=0;i<(int)(p.size());i++){if(i) cout<<" ";cout<<p[i];}cout<<"\n";}
template<class T> T vec_min(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmin(ans,x);return ans;}
template<class T> T vec_max(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmax(ans,x);return ans;}
template<class T> T vec_sum(vector<T> &a){assert(!a.empty());T ans=a[0]-a[0];for(auto &x:a) ans+=x;return ans;}
int pop_count(long long a){int res=0;while(a){res+=(a&1),a>>=1;}return res;}

namespace po167{
struct UFtree_yuki_2311
{
	using _F=int;
	int _n;
	vector<ll> val1,val2;
	vector<vector<int>> q;
	int component;
	UFtree_yuki_2311(int n):_n(n),val1(n),val2(n),q(n),component(n),par(n){
		for(int i=0;i<n;i++){
			par[i]=i;
			q[i]={i};
			val1[i]=0;
			val2[i]=0;
		}
	}
	//根っこを返す
	int root(int a){
		assert(0<=a&&a<_n);
		if(a==par[a]) return a;
		return par[a]=root(par[a]);
	}
	//trueなら1,falseなら0
	int same(int a,int b){
		assert(0<=a&&a<_n);
		assert(0<=b&&b<_n);
		if(root(a)==root(b)) return 1;
		else return 0;
	}
	//a,bが違う根っこの元なら結合する,結合したらtrueを返す
	bool unite(int a,int b){
		a=root(a),b=root(b);
		if(a==b) return false;
		if(q[a].size()<q[b].size()) std::swap(a,b);
		//merge
		for(auto x:q[b]){
			q[a].push_back(x);
			val2[x]=val1[a]-calc(x);
		}
		par[b]=a;
		component--;
		return true;
	}
	void fall(int v,ll K){
		assert(0<=v&&v<_n);
		val1[root(v)]+=K;
	}
	ll calc(int v){
		assert(0<=v&&v<_n);
		return val1[root(v)]-val2[v];
	}
	private:
	std::vector<int> par;
};
}



void solve();
// oddloop
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int t=1;
	//cin>>t;
	rep(i,0,t) solve();
}

void solve(){
	int N;
	cin>>N;
	vector<ll> A(N);
	rep(i,0,N) cin>>A[i];
	ll T;
	cin>>T;
	vector ev(T,vector<ll>(3));
	rep(i,0,T){
		rep(j,0,3) cin>>ev[i][j];
		ev[i][1]--;
		if(ev[i][0]==1) ev[i][2]--;
	}
	vector<vector<int>> H(T);
	ll Q;
	cin>>Q;
	vector<ll> ans(Q);
	vector<ll> S(Q),I(Q);
	rep(i,0,Q){
		cin>>S[i]>>I[i];
		S[i]--,I[i]--;
		if(S[i]!=-1) H[S[i]].push_back(i);
	}
	// 2 and 3
	{
		vector<int> large(N);
		int B=444;
		rep(i,0,T){
			if(ev[i][0]==1) large[ev[i][1]]++,large[ev[i][2]]++;
		}
		rep(i,0,N) if(large[i]<B) large[i]=0;
		vector<ll> v1(N),v2(N);
		vector<vector<int>> g1(N),g2(N);
		rep(i,0,T){
			if(ev[i][0]==1){
				if(large[ev[i][2]]){
					g2[ev[i][1]].push_back(ev[i][2]);
				}
				else{
					g1[ev[i][2]].push_back(ev[i][1]);
				}
				if(large[ev[i][1]]){
					g2[ev[i][2]].push_back(ev[i][1]);
				}
				else{
					g1[ev[i][1]].push_back(ev[i][2]);
				}
			}
			if(ev[i][0]==2){
				v2[ev[i][1]]+=ev[i][2];
			}
			if(ev[i][0]==3){
				v2[ev[i][1]]+=ev[i][2];
				if(large[ev[i][1]]){
					v1[ev[i][1]]+=ev[i][2];
				}
				else{
					for(auto x:g1[ev[i][1]]){
						v2[x]+=ev[i][2];
					}
				}
			}
			for(auto x:H[i]){
				ans[x]+=v2[I[x]];
				for(auto y:g2[I[x]]) ans[x]+=v1[y];
			}
		}
	}
	
	// 4
	{
		po167::UFtree_yuki_2311 uft(N);
		rep(i,0,T){
			if(ev[i][0]==1) uft.unite(ev[i][1],ev[i][2]);
			if(ev[i][0]==4) uft.fall(ev[i][1],ev[i][2]);
			for(auto x:H[i]){
				ans[x]+=uft.calc(I[x]);
			}
		}
	}
	rep(i,0,Q){
		cout<<max(0ll,A[I[i]]-ans[i])<<"\n";
	}
}
0