90分求卡常
查看原帖
90分求卡常
1238611
harmis_yz楼主2023/12/26 16:12

Rt.最后一个和倒数第三个 TLE 了 pwp。

#include<bits/stdc++.h>
using namespace std;
//#define int long long
#define re register
#define il inline

il int read(){
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();}
	while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
	return x*f;
}
il void write(long long x) {    
    if(x<0) putchar('-'),x=-x;    
    if(x>9) write(x/10);    
    putchar(x%10+'0');    
    return;    
}   

const int N=1e6+10;
const long long p=1e9+7;
int n,m;
struct node{
	int x,y;
	long long v;
}pt[N];
vector<int> pt1[N],pt2[N];
long long sum[N],tag[N];

il long long qmi(long long a,long long b){
	long long ans=1;
	while(b){
		if(b&1) ans=ans*a%p;
		a=a*a%p,b>>=1;
	}
	return ans;
}
il void solve(){
	n=read(),m=read();int len=1200;
	for(re int i=1;i<=n;++i) 
		tag[i]=1,pt[i].x=read(),pt[i].y=read(),pt[i].v=read()%p,pt1[pt[i].x].push_back(i);
	for(re int i=1;i<=n;++i)
		if((int)(pt1[pt[i].x].size())<=len) sum[pt[i].y]=(sum[pt[i].y]+pt[i].v)%p;
		else pt2[pt[i].y].push_back(i);
	for(re int i=1;i<=m;++i){
		int op=read();int x,y;
		if(op==1){
			x=read();
			if((int)(pt1[x].size())<=len){
				for(auto j:pt1[x]){
					sum[pt[j].y]=((sum[pt[j].y]-pt[j].v)%p+p)%p; 
					pt[j].v=pt[j].v*pt[j].v%p;
					sum[pt[j].y]=sum[pt[j].y]+pt[j].v;
					if(sum[pt[j].y]>=p) sum[pt[j].y]-=p; 
				}
			}
			else{
				tag[x]=tag[x]<<1;if(tag[x]>=p-1) tag[x]-=p-1;
			}
		}
		else{
			y=read();long long ans=sum[y];
			for(auto j:pt2[y]) ans=(ans+qmi(pt[j].v,tag[pt[j].x]));
			write(ans%p),putchar('\n');
		}
	}
	return ;
}

signed main(){
	solve();
	return 0;
}
2023/12/26 16:12
加载中...